-
Notifications
You must be signed in to change notification settings - Fork 1
/
maps.py
235 lines (195 loc) · 8.24 KB
/
maps.py
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import tdl
import random, math, sys
import monsters, colors, consts, utils, items, dungeons, forests, draw, area
class TerrainMap:
def __init__(self):
self.forest_level = 0
self.dungeon_level = 0
self.rooms = []
self.width = consts.WIDTH
self.height = consts.HEIGHT
self.dungeon = {}
self.reset_dungeon()
self.dungeons = []
# Restors the nth stored dungeon.
def restore_dungeon(self, n):
if n < len(self.dungeons):
self.dungeon = self.dungeons[n]
return True
return False
# Creates a new dungeon ready for creation.
def reset_dungeon(self):
self.dungeon = {
'monsters': [],
'monsters_alerted': False,
'water': {},
'areas': [],
'numbers': {},
'noise': None,
'down_stairs': (-20, -20),
'up_stairs': (-20, -20),
'decor': {},
'items': {},
'doors': {},
'rooms': [],
'player_starting_pos': (0, 0),
'lighted': tdl.map.Map(self.width, self.height),
'remembered_fov': set([])
}
# Checks if a water cell is adjacent to (x, y) co-ords.
def adjacent_water(self, p):
x, y = p
return (max(x-1, 0), y) in self.water or\
(max(x+1, 0), y) in self.water or\
(x, max(y+1, 0)) in self.water or\
(x, max(y-1, 0)) in self.water
# FIXME: Refactor somehow.
# This checks if there is a reasonable amount of floorspace around a point.
# (For forest levels.)
def all_around_floor(self, p):
x, y = p
return self.get_type(max(x-1, 0), y) == 'FLOOR' and\
self.get_type(max(x-2, 0), y) == 'FLOOR' and\
self.get_type(max(x-3, 0), y) == 'FLOOR' and\
self.get_type(max(x+1, 0), y) == 'FLOOR' and\
self.get_type(max(x+2, 0), y) == 'FLOOR' and\
self.get_type(max(x+3, 0), y) == 'FLOOR' and\
self.get_type(x, max(y+1, 0)) == 'FLOOR' and\
self.get_type(x, max(y+2, 0)) == 'FLOOR' and\
self.get_type(x, max(y+3, 0)) == 'FLOOR' and\
self.get_type(x, max(y-1, 0)) == 'FLOOR' and\
self.get_type(x, max(y-2, 0)) == 'FLOOR' and\
self.get_type(x, max(y-3, 0)) == 'FLOOR'
# Retreives the type of the cell at (x, y).
def get_type(self, p):
x, y = utils.clamp_point(p, maxs=(self.width, self.height))
if self.is_forests():
l = self.dungeon['noise'].get_point(x, y)
if l > consts.FLOOR_LEVEL and l < consts.STONE_LEVEL:
return 'TREE'
elif l >= consts.STONE_LEVEL:
return 'STONE'
elif l <= consts.FLOOR_LEVEL and l > consts.WATER_LEVEL:
return 'FLOOR'
elif l <= consts.WATER_LEVEL:
return 'WATER'
elif self.is_dungeons():
if (x, y) in self.dungeon['doors']:
return 'DOOR'
elif self.dungeon['lighted'].transparent[x, y]:
return 'FLOOR'
else:
return 'STONE'
# Checks if a point is on the map.
def on_map(self, p):
return p[0] >= 0 and p[1] >= 0 and p[0] < self.width and p[1] < self.height
def on_map_bordered(self, p):
return p[0] > 0 and p[1] > 0 and p[0] < self.width-1 and p[1] < self.height-1
# Returns a monster at the given point.
def monster_at(self, p):
for m in self.dungeon['monsters']:
if m.pos == p:
return m
return None
def in_area(self, p):
for a in self.dungeon['areas']:
if a.inside(p):
return a.area_type
return 'Regular'
# Checks if level is hard.
def is_hell_level(self):
return self.dungeon_level > math.floor(consts.DUNGEON_LEVELS/2)
# Checks if the character can stand on a point.
def is_walkable(self, p, player=(-1, -1)):
return self.on_map(p) and not self.monster_at(p)\
and p != player and self.dungeon['lighted'].walkable[p]
# Checks if the current level is in a forest.
def is_forests(self):
return self.forest_level < consts.FOREST_LEVELS
# Checks if the current level is in a dungeon.
def is_dungeons(self):
return self.dungeon_level < consts.DUNGEON_LEVELS
# Generates a new forest map (redirected to forests.py)
def generate_new_forest_map(self):
return forests.generate_new_forest_map(self)
def place_door(self, pnt):
self.dungeon['lighted'].transparent[pnt] = False
self.dungeon['lighted'].walkable[pnt] = False
self.dungeon['doors'][pnt] = True
# Generates a new dungeon map (ridirected to dungeons.py)
# and saves it to the level list.
def generate_new_dungeon_map(self):
self.dungeon_level += 1
return dungeons.generate_new_dungeon_map(self)
# Generates the final level.
# TODO: Not implemented
def generate_final_level(self):
return None
def generate_areas(self):
areas = []
while len(areas) < 6:
a = area.Area(random.randint(0, self.width-1),
random.randint(0, self.height-1),
random.randint(12, math.floor(self.width/4)),
random.randint(12, math.floor(self.height/4)))
if a.pos2 < (self.width-1, self.height-1) and a.pos1 > (0, 0):
areas.append(a)
return areas
# Generates the new map, forest or dungeon, appropriately by level number.
# FIXME: Calls generate_final_level, which will return None.
def generate_new_map(self):
self.dungeons.append(self.dungeon)
self.reset_dungeon()
self.dungeon['areas'] = self.generate_areas()
if self.is_dungeons():
result = self.generate_new_dungeon_map()
else:
result = self.generate_final_level()
temp = self.dungeon['lighted']
self.dungeon['lighted'] = tdl.map.Map(self.width+3, self.height+3)
for x in range(0, self.width):
for y in range(0, self.height):
if self.dungeon['player_starting_pos'] == (x, y):
sys.stdout.write('@')
elif temp.walkable[x, y]:
sys.stdout.write('.')
else:
sys.stdout.write('#')
self.dungeon['lighted'].walkable[x+1, y+1] = temp.walkable[x, y]
self.dungeon['lighted'].transparent[x+1, y+1] = temp.transparent[x, y]
print('')
return result
# Adds cell to terrain map.
def place_cell(self, p, is_wall=False):
pos = utils.clamp_point(p, maxs=(self.width-1, self.height-1))
self.dungeon['lighted'].transparent[pos] = not is_wall
self.dungeon['lighted'].walkable[pos] = not is_wall
# Calculates the player's visited map and current FoV, then draws the visited
# map while brightening the player's FoV.
def draw_map(self, GS, console, player, frame):
if consts.FOV:
rad = player.light_source_radius
x, y = player.pos
fov = [(f[0], f[1]) for f in
self.dungeon['lighted'].compute_fov(
x, y,
fov='DIAMOND',
radius=rad,
sphere=True)]
for p in fov:
self.dungeon['remembered_fov'].add(p)
else:
fov = self.dungeon['lighted']
self.dungeon['remembered_fov'] = set(fov)
# Draw up-stairs and down-stairs no matter what.
if len(self.dungeon['monsters']) == 0:
draw.draw_dungeon_tile(self, GS, console, self.dungeon['down_stairs'], (0,0,0))
# Draw map
for x, y in list(self.dungeon['remembered_fov']):
tint = (-90, -90, -90)
if (x, y) in fov:
tint = (0, 0, 0)
if self.is_forests():
draw.draw_forest_tile(self, console, (x, y), tint)
elif self.is_dungeons():
draw.draw_dungeon_tile(self, GS, console, (x, y), tint)