-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorldBuilder.hs
204 lines (148 loc) · 6.24 KB
/
WorldBuilder.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
module WorldBuilder (returnWorld) where
import System.Random
import qualified Data.Map as M
import Data.List
--import Helpers
import Random
import Types.World
import Types.Tiles
import Types.Common
import Types.Items
import Content.Base
import Content.Races
import Content.MonsterTypes
lengthBounds :: (Int, Int)
lengthBounds = (100, 200) -- minimum and maximum length of a level
nofLevels = 2 -- TODO: add more.
monsterDistanceBounds :: (Int, Int)
monsterDistanceBounds = (5, 8) -- average distance between monsters
doorDistanceBounds :: (Int, Int)
doorDistanceBounds = (20, 40) -- average door distance.
returnWorld :: IO World
returnWorld = do
gen <- getStdGen
let world = generateWorld gen
return world
generateWorld :: StdGen -> World
generateWorld gen = world
where
(levels, gen') = generateLevels gen nofLevels []
world = baseWorld { wLevel = head levels, wLevels = levels, wStdGen = gen' }
generateLevels :: StdGen -> Int -> [Level] -> ([Level], StdGen)
generateLevels g nofLevels prevLevels
| nofLevels == 1 = (lvl:prevLevels, g')
| otherwise = generateLevels g' (nofLevels-1) (lvl:prevLevels)
where
(l, g') = randomR lengthBounds g
(mD, _) = randomR monsterDistanceBounds g
nofMonsters = div l mD
(dD, _) = randomR doorDistanceBounds g
nofDoors = div l dD
(gf, gw) = split g
floor = generateFloor gf l
wall = generateWall gw l nofDoors
monsters = generateMonsters g nofMonsters l nofLevels wall
lvl = baseLevel { lSize = l,
lDepth = nofLevels,
lFloorTiles = floor,
lWallTiles = wall,
lEntities = monsters
}
-- Generate numbers that do not occur on the same level and are not placed in tiles with doors.
-- Possible issue: generators are not split, every temporary list generated uses the same generator.
generateMonsters :: StdGen -> Int -> Int -> Int -> M.Map Position WallTile -> M.Map Position Entity
generateMonsters g nofMonsters l levelDepth wallMap = monsterMap
where
doorCoords = getDoorCoords l wallMap
randCoords = randomizeCoordinates nofMonsters l doorCoords g
mIDs = [0..]
(generatorList, _) = makeGeneratorList g nofMonsters
zippedList = zip3 mIDs randCoords generatorList
randMonsters = map (\(id, pos,g) -> randomMonster id pos g) zippedList
monsterMap = M.fromList $ zip randCoords randMonsters
-- Helper functions for generateMonsters
----------------------------------------------
getDoorCoords :: Int -> M.Map Position WallTile -> [Position]
getDoorCoords 0 _ = []
getDoorCoords x wallMap
| doorCheck = (x,0):getDoorCoords (x-1) wallMap
| otherwise = getDoorCoords (x-1) wallMap
where
doorCheck = case M.lookup (x,0) wallMap of
Just Door -> True
_ -> False
randomizeCoordinates :: Int -> Int-> [Position] -> StdGen -> [Position]
randomizeCoordinates 0 _ _ _ = []
randomizeCoordinates nofM l takenPositions g_
= p:randomizeCoordinates (nofM-1) l (p:takenPositions) g_'
where
(p, g_') = randP g_ l takenPositions
-- rerolls if hitting a taken position. Ineffective? probably, but there is plenty of room in the level so i doubt it is an issue.
-- improve this if performance is an hindrance.
randP :: StdGen -> Int -> [Position] -> (Position, StdGen)
randP g__ l takenPositions
| notElem (rX, 0) takenPositions = ((rX, 0), g__')
| otherwise = randP g__' l takenPositions
where
(rX, g__') = randomR (1, l) g__
-------------------------------------------
-- Generate an actual monster.
-------------------------------------------
-- FUTURE: take into account which level the monster was generated on.
-- randomize inventories.
-- new argument: Excluded positions?
randomMonster :: Int -> Position -> StdGen -> Entity
randomMonster mID pos g = createMonster randomMonsterType randomRace inventory level mID pos
where
(randomMonsterType, g') = randomListMember monsterTypes g
(randomRace, g'') = randomListMember races g'
inventory = Inventory [] 0
-- make level +-2 levels from dungeon level
level = 1
createMonster :: MonsterType -> Race -> Inventory -> Int -> Int -> Position -> Entity
createMonster mt race inv level id position =
Monster { mType = mt,
mRace = race,
mInventory = inv,
mLevel = level,
mExperienceReward = 1, -- todo
mSpotted = False,
mBehaviorStack = mtBehaviorStack mt,
eCurrHP = mHP,
eMaxHP = mHP,
mID = id,
eCurrPos = position,
eOldPos = position,
eSpeed = mSpeed,
eNextMove = mSpeed,
eHitDie = mHitDie,
eDamageDie = mDamageDie,
eEvadeDie = mEvadeDie,
eMitigation = mMitigation,
eSkillEffects = []
}
where
mHitDie = (mtHitDie mt)
{ dMod = dMod (mtHitDie mt) + rHitModifier race }
mEvadeDie = (mtEvadeDie mt)
{ dMod = dMod (mtEvadeDie mt) + rEvasionModifier race }
mDamageDie = (mtDamageDie mt)
{ dMod = dMod (mtDamageDie mt) + rDamageModifier race }
mMitigation = mtMitigation mt + rMitigationModifier race
mHP =
round $ fromIntegral (mtBaseHP mt + (level * mtHPPerLevel mt)) * rHPMultiplier race
-- todo: experience.
mSpeed =
round $ fromIntegral (mtBaseSpeed mt) * rSpeedMultiplier race
--------------------------------------
generateFloor :: StdGen -> Int -> M.Map Position FloorTile
generateFloor g l = floorMap
where
randFloor = (take l $ randoms g) :: [FloorTile]
coords = [(x,y) | x <- [0..], y <- [1]]
floorMap = M.fromList $ zip coords randFloor
generateWall :: StdGen -> Int -> Int -> M.Map Position WallTile
generateWall g l nofDoors = wallMap
where
doorCoords = zip (take nofDoors $ randomRs (1, l - 1) g) $ replicate nofDoors 0
wallMap = M.fromList $ zip doorCoords $ replicate nofDoors Door