{-# LANGUAGE FlexibleContexts #-}

{-
  This module is part of Ironforge.
  Copyfree (f) 2014 Marvin Cohrs

  All wrongs revoked. Sharing is an act of love, not crime.
  Please share Ironforge with everyone you like.
  
  Redistribution and use in source and binary forms, with or without modification,
  are permitted provided that the following conditions are met:

      * Redistributions of source code must retain the above copyright notice,
        this list of conditions and the following disclaimer.
      * Redistributions in binary form must reproduce the above copyright notice,
        this list of conditions and the following disclaimer in the documentation
        and/or other materials provided with the distribution.
      * Neither the name of Marvin Cohrs nor the names of other
        contributors may be used to endorse or promote products derived
        from this software without specific prior written permission.
  
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-}

module Game.Antisplice.Dungeon.Ironforge where

import Text.Chatty.Printer
import Text.Chatty.Scanner
import Text.Chatty.Expansion
import Text.Chatty.Expansion.Vars
import System.Chatty.Misc
import Text.Chatty.Extended.Printer
import Game.Antisplice
import Control.Monad
import Control.Monad.Error.Class
import Data.Monoid

ironforge :: Constructor
ironforge = do
  --  IRONFORGE : THE GREAT FORGE
  greatForge <- constructRoom $ do
    ctorRoom "The Great Forge" "This is the Great Forge."
    addRoomObject $ do
      ctorSign
            ("Dear traveler,\n\nit might occur that this is your first dungeon. "++
             "You can play it by just typing in what you want to do, for instance "++
             "\"go east\" or \"look at green sign\". By the time you will learn new "++
             "actions by just trying them.\n\n"++
             "Ironforge is a dungeon that primarily plays in Azeroth, but includes "++
             "places and scenes from other narrations or reality. Most are intended "++
             "to be funny, but since the implementor is a fag you shouldn't expect "++
             "too much.\n\nAnyways, have fun and good luck :)")
            ["green"]
      addDescSeg "On one wall there is a green sign. Type \"read sign\" to read it."
    onFirstEnter $ do
      ebracket (Vivid Yellow) $ do
        mprintLn "** Welcome to IRONFORGE, an experimental dungeon on top of Antisplice."
        mprintLn "**"
        mprintLn "** This is not a stable branch, but rather a technical demo."
        mprintLn "**"
        mprintLn "** Antisplice (c) 2014 Marvin Cohrs"
        mprintLn "** Ironforge  (c) 2014 Marvin Cohrs"
      mprintLn ""
      --mprint "How may I call you? " >> mflush
      --mscanLn >>= (mputv "user" . Literal)
      mputv "user" $ Literal "Bernd"
      mprintLn ""
      mprintLn =<< expand "Alright $user, here you are!"
      mprintLn ""
      
  --  IRONFORGE : THE MILITARY WARD
  militaryW <- constructRoom $ do
    ctorRoom "The Military Ward" "This is where the anti-terror act of the Asocial Network will happen."
    addRoomObject $ ctorMob "a cangaroo" ["cangaroo","communist"] "This is a communist cangaroo, who has fought for Vietcong." []
  withRoom militaryW $ do
    onEnter $ do
      let laugh :: Handler
          laugh = guardVisible militaryW $ do
            mprintLn "\"Haha!\", the cangaroo says and laughs."
            schedule 10000 laugh
      schedule 10000 laugh
  bipath greatForge militaryW SouthEast
  
  --  IRONFORGE : THE COMMONS
  commonsW <- constructRoom $ do
    ctorRoom "The Commons" "Here you see the bank and the auction house."
    addRoomObject $ do
      ctorAcq "a cup" "One might fill this with coffee." ["cup"] []
      addDescSeg "There is a cup lying on the floor."
  unipath greatForge commonsW South
  unipath greatForge commonsW West
  unipath commonsW greatForge NorthEast
  bipath commonsW militaryW East
   
  --  IRONFORGE : THE TAVERN
  tavernW <- constructRoom $ do
    ctorRoom "The Tavern" "This is the tavern of Ironforge. It is known for its strong beer, so you might want to try one. To the west there is the toilet. The exit is to the east."
    addRoomObject $ do
      ctorAcq "a rusty dagger" "This is a rusty old dagger, probably lost by a rouge." ["dagger"] ["rusty","old"]
      addDescSeg "A rusty old dagger lies on the ground."
      addEquipSlot MainHand
      addEquipSlot OffHand
      rustyDaggerSte <- registerStereo $ statsStereo $
        \get k -> case k of
          AttackPower -> 7
          _ -> 0
      addFeature $ Stereo Worn $ rustyDaggerSte
  unipath tavernW commonsW East
  withRoom commonsW $ do
    addRoomObject $ do
      ctorRoomNesting "the tavern" "This is the Ironforge tavern." ["tavern"] [] tavernW
      addDescSeg "Straight in front of you there is the tavern of Ironforge."
      
  --  IRONFORGE : THE TAVERN'S TOILET
  toiletW <- constructRoom $ do
    ctorRoom "The Toilet" "This is the tavern's toilet."
  withRoom toiletW $ addRoomObject $ do
      ctorMob "Igor" ["igor"] "This is Igor. I guess he doesn't like you." []
      setMobRoute [tavernW]
      o <- getObjectState
      onSight $ schedule 0 $ guardVisible toiletW $ do
        mprintLn "You enter the occupied toilet and see Igor sitting on the pot."
        mprintLn "\"Oops, I'm sorry.\", you say and quickly return to the main room."
        enterAndAnnounce tavernW
        schedule 3000 $ continueMobRoute $ objectIdOf o
  bipath toiletW tavernW East
  
  --  IRONFORGE : THE MYSTIC WARD
  mysticSte <- registerStereoM $ do
    mergeStereo $ statsStereo $
      \get k -> case k of
        Intelligence -> get Intelligence * 99
        _ -> 0
    mergeSkill $ skill "pray" !+ implyGlobalCooldownA !+ bareAction (\_ -> mprintLn "You're falling to your knees praying.\nYou stand up again.")
  mysticW <- constructRoom $ do
    ctorRoom "The Mystic Ward" "Here you will find a lot of mages."
    addRoomObject $ do
      addFeature $ Stereo Near mysticSte
      addDescSeg "You feel intellectual."
  bipath greatForge mysticW NorthWest
  bipath commonsW mysticW North
  
  --  IRONFORGE : THE FORLORN CAVERN
  cavernW <- constructRoom $ ctorRoom "The Forlorn Cavern" "Uncanny."
  guardedPath mysticW cavernW East $ return False
  unipath cavernW mysticW West
  
  --  IRONFORGE : HALL OF EXPLORERS
  halloeW <- constructRoom $ ctorRoom "Hall of Explorers" "Here you can be rewarded."
  bipath cavernW halloeW East
  bipath greatForge halloeW NorthEast
  
  --  IRONFORGE : TINKER TOWN
  tinkerW <- constructRoom $ ctorRoom "Tinker Town" "Engineers all around you."
  bipath halloeW tinkerW South
  bipath tinkerW militaryW South
    
  -- IRONFORGE : DEEPRUN TRAM STATION
  stationW <- constructRoom $ do
    ctorRoom "Ironforge Deeprun Tram Station" "This is the tram station of Ironforge, Dun Morogh."
    addRoomObject $ ctorMob "the station overseer" ["overseer"]
      "This is the badly-paid station overseer. His job is to answer all your questions, but probably he won't be that friendly."
      ["badly-paid","unfriendly"]
  bipath tinkerW stationW East
  
  -- CAMBRIDGE : DEEPRUN TRAM STATION
  stationC <- constructRoom $ do
    ctorRoom "Cambridge Deeprun Tram Station" "This is the tram station of Cambridge, Massachusetts."
    addRoomObject $ addDescSeg "On the right there are stairs." -- TODO: Introduce OnUse
    addRoomObject $ do
      ctorMob "the station overseer" ["overseer","murderer"]
        "This one is a very special case. The company didn't check his background, so they hired a recorded murderer. I'd better keep in distance."
        ["recorded"]
      murdererA <- newAtom
      putAtom murdererA 0
      onLook $ do
        a <- getAtom murdererA
        case a :: Int of
          0 -> mprintLn "This one is a very special case. The company didn't check his background, so they hired a recorded murderer. I'd better keep in distance."
          1 -> mprintLn "He comes somewhat nearer."
          2 -> mprintLn "He stands just three meters away from you."
          3 -> mprintLn "The murderer finally arrives at you." >> schedule 2000 (eprintLn (Vivid Red) "The murderer takes his knife and kills you." >> throwError QuitError)
          _ -> noneM
        putAtom murdererA (a+1)
        
  -- Tram
  insertVocab "tram" Noun
  withRoom stationW $
    addRoomObject $ do
      setObjectTitle "the tram"
      setObjectDesc "This is the tram to Cambridge, Massachusetts."
      addObjectName "tram"
      addDescSeg "Enter the tram and ride to Cambridge, Massachusetts."
      onEnter $ do
        mprintLn "Immediately after you entered the tram, the overseer blows the whistle and the train departs."
        mprintLn "You have arrived at your destination: Cambridge, Massachusetts."
        enterAndAnnounce stationC
  withRoom stationC $
    addRoomObject $ do
      setObjectTitle "the tram"
      setObjectDesc "This is the tram to Ironforge, Dun Morogh."
      addObjectName "tram"
      addDescSeg "Enter the tram and ride to Ironforge, Dun Morogh."
      onEnter $ do
        mprintLn "The whistle is so loud that you almost get a tinnitus. The train begins to roll."
        mprintLn "You have arrived at your destination: Ironforge, Dun Morogh."
        enterAndAnnounce stationW
        
  -- CAMBRIDGE : GERT BROELEMANN AVENUE
  broeleC <- constructRoom $ ctorRoom "Gert Broelemann Avenue" "This is a long street to the east and to the west. You see the entrance to the Deeprun Tram."
  bipath stationC broeleC Up
  let broeleAv = constructRoom $ ctorRoom "Gert Broelemann Avenue" "This is a long street to the east and to the west."
  broele1WC <- broeleAv
  bipath broeleC broele1WC West
  broele2WC <- broeleAv
  bipath broele1WC broele2WC West
  unipath broele2WC broele2WC West
  broele1EC <- broeleAv
  bipath broeleC broele1EC East
  
  -- Kalle
  withRoom broeleC $ do
    addRoomObject $ do
      ctorMob "Karl-Heinz" ["kalle","karl-heinz","teacher"] "Karl-Heinz is an old maths teacher. Strange, but harmless." ["strange","harmless","old"]
      ctorRoute [stationC,broeleC,broele1EC,broeleC,broele1WC,broele2WC,broele1WC,broeleC] 10000
      
  -- Player
  steDef <- registerStereoM $ do
    mergeStereo defaultStereo
    mergeStereoM visualStereo
    mergeStereoM manualStereo
    mergeSkillM $ skill "oink" !+ bareAction (\_ -> mprintLn "Oink!") !+ implyGlobalCooldownA !+> implyCooldownA 5000
  subscribePlayer $ do
    setPlayerRoom greatForge
    setStatM Strength 6
    setStatM Agility 3
    setStatM Stamina 5
    setStatM Intelligence 1
    setStatM Spirit 2
    setStatM Armor 0
    setStatM AttackPower 5
    setStatM Haste 5
    setStatM CooldownDuration 1000
    addStereo steDef