/
LambdaHack.hs
84 lines (77 loc) · 2.8 KB
/
LambdaHack.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
module Main where
import System.Directory
import Control.Monad
import Data.List as L
import Data.Map as M
import Action
import Actor
import State
import Geometry
import Level
import Dungeon
import Perception
import Display2
import Random
import qualified Save
import Turn
import Item
import qualified Config
import Monster
main :: IO ()
main = startup start
-- | Either restore a saved game, or setup a new game.
start :: Session -> IO ()
start session =
do
-- check if we have a savegame
config <- Config.config
f <- Save.file config
x <- doesFileExist f
restored <- if x then do
displayBlankConfirm session "Restoring save game"
Save.restoreGame config
else return $ Right "Welcome to LambdaHack!" -- new game
case restored of
Right msg -> generate config session msg
Left state -> handlerToIO session state "Welcome back to LambdaHack."
handle
-- | Generate the dungeon for a new game, and start the game loop.
generate :: Config.CP -> Session -> String -> IO ()
generate config session msg =
let matchGenerator n Nothing = rogueRoom -- the default
matchGenerator n (Just "bigRoom") = bigRoom
matchGenerator n (Just "noiseRoom") = noiseRoom
matchGenerator n (Just "rogueRoom") = rogueRoom
matchGenerator n (Just s) =
error $ "findGenerator: unknown: " ++ show n ++ ", " ++ s
findGenerator n =
let genName =
Config.getOption config "dungeon" ("LambdaCave_" ++ show n)
generator = matchGenerator n genName
in rndToIO $ generator (defaultLevelConfig n) (LambdaCave n)
connect :: Maybe (Maybe DungeonLoc) ->
[(Maybe (Maybe DungeonLoc) -> Maybe (Maybe DungeonLoc) ->
Level, Loc, Loc)] ->
[Level]
connect au [(x,_,_)] = [x au Nothing]
connect au ((x,_,d):ys@((_,u,_):_)) =
let (z:zs) = connect (Just (Just (lname x',d))) ys
x' = x au (Just (Just (lname z,u)))
in x' : z : zs
in
do
let depth = Config.get config "dungeon" "depth"
levels <- mapM findGenerator [1..depth]
let lvls = connect (Just Nothing) levels
(lvl,dng) = (head lvls, dungeon (tail lvls))
-- generate item associations
assocs = M.fromList
[ (Potion PotionWater, Clear),
(Potion PotionHealing, White) ]
ploc = ((\ (_,x,_) -> x) (head levels))
hp = heroHP config
defState = defaultState (AHero 0) ploc dng lvl
state = defState { sassocs = assocs, sconfig = config }
k = Config.get config "heroes" "extraHeroes"
hstate = foldl' (addHero ploc hp) state [0..k]
handlerToIO session hstate msg handle