-
Notifications
You must be signed in to change notification settings - Fork 1
/
maps.py
223 lines (199 loc) · 9.32 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
from constants import *
from ai import USE_AI, STORY_MODE
from dungeon_generator import getMap, getEntrance
import time, random, heapq
from asciiart import *
from copy import deepcopy
class Tile(object):
def __init__(self):
self.visible = False
self.data = ' '
class Map(object):
def __init__(self, level):
map_grid = getMap(MAP_WIDTH, MAP_HEIGHT)
self.tiles = list()
self.stevendorf = False
self.boss_fight = False
for i in range(MAP_HEIGHT):
curr = list()
for j in range(MAP_WIDTH):
curr.append(Tile())
self.tiles.append(curr)
pass
pass
for i in range(MAP_HEIGHT):
for j in range(MAP_WIDTH):
self.tiles[i][j].data = map_grid[i][j]
pass
pass
self.player = (random.randint(0, MAP_HEIGHT - 1), random.randint(0, MAP_WIDTH - 1))
while self.tiles[self.player[0]][self.player[1]].data != '.':
self.player = (random.randint(0, MAP_HEIGHT - 1), random.randint(0, MAP_WIDTH - 1))
# generate a victory location
self.victory = (random.randint(0, MAP_HEIGHT - 1), random.randint(0, MAP_WIDTH - 1))
while self.tiles[self.victory[0]][self.victory[1]].data != '.' or self.player == self.victory:
self.victory = (random.randint(0, MAP_HEIGHT - 1), random.randint(0, MAP_WIDTH - 1))
# create player and victory location
self.tiles[self.player[0]][self.player[1]].data = 'X'
self._placeVictory(level)
# create victory location. If it's the 3rd level, create an asterisk.
for i in range(MAP_HEIGHT):
for j in range(MAP_WIDTH):
if self.tiles[i][j].data == '.':
self.tiles[i][j].data = ' '
pass
pass
self._visited = set()
self.revealRoom()
def _placeVictory(self, level):
self.tiles[self.victory[0]][self.victory[1]].data = '@'
if level == AMULET_LEVEL and STORY_MODE:
self.tiles[self.victory[0]][self.victory[1]].data = '*'
for i in range(-1,2):
for j in range(-1,2):
if i == 0 and j == 0:
continue
row = self.victory[0] + i
col = self.victory[1] + j
if 0 <= row < MAP_HEIGHT and \
0 <= col < MAP_WIDTH and \
self.tiles[row][col].data == '.':
self.tiles[row][col].data = '+'
def clear(self):
for i in range(MAP_HEIGHT):
for j in range(MAP_WIDTH):
self.tiles[i][j] = Tile()
def revealRoom(self):
visible = []
self.tiles[self.player[0]][self.player[1]].visible = True
visible.append(self.player)
while len(visible):
x,y = visible.pop()
if self.tiles[x][y].data in VISIBLE_TILES or (x,y) == self.player:
# make all adjacent tiles visible
left = down = up = right = False
if x < MAP_HEIGHT - 1 and not self.tiles[x+1][y].visible:
self.tiles[x+1][y].visible = True
visible.append((x+1,y))
if x > 0 and not self.tiles[x-1][y].visible:
self.tiles[x-1][y].visible = True
visible.append((x-1,y))
if y < MAP_WIDTH - 1 and not self.tiles[x][y+1].visible:
self.tiles[x][y+1].visible = True
visible.append((x,y+1))
if y > 0 and not self.tiles[x][y-1].visible:
self.tiles[x][y-1].visible = True
visible.append((x,y-1))
if x < MAP_HEIGHT - 1 and y < MAP_WIDTH - 1 and not self.tiles[x+1][y+1].visible:
self.tiles[x+1][y+1].visible = True
visible.append((x+1,y+1))
if x < MAP_HEIGHT - 1 and y > 0 and not self.tiles[x+1][y-1].visible:
self.tiles[x+1][y-1].visible = True
visible.append((x+1,y-1))
if x > 0 and y < MAP_WIDTH - 1 and not self.tiles[x-1][y+1].visible:
self.tiles[x-1][y+1].visible = True
visible.append((x-1,y+1))
if x > 0 and y > 0 and not self.tiles[x-1][y-1].visible:
self.tiles[x-1][y-1].visible = True
visible.append((x-1,y-1))
def printMap(self, danger, health, potions, message, floor, counter, h_danger, e_prob, escapes, fireballs, repels, rep_turns):
for i in range(3): print
#"Currently hiding!" if hiding else "Visible!"
print HEADER.format(DANGERS[danger], str(health), str(potions), message, floor, counter, DANGERS[h_danger], PROBS[e_prob], escapes, fireballs, repels, rep_turns)
print '- ' * (MAP_WIDTH + 1) + '-'
for i in range(MAP_HEIGHT):
output = ''
output += '| '
for j in range(MAP_WIDTH):
if self.tiles[i][j].visible:
output += " {0}".format(self.tiles[i][j].data)
else:
output += " "
output += '|'
print output
print '- ' * (MAP_WIDTH + 1) + '-'
def canMove(self, direction):
x,y = self.player
if direction == 'UP':
if self._isInBounds((x-1, y)) and self.tiles[x-1][y].data == '+': self.stevendorf = True
return x > 0 and self.tiles[x-1][y].data != '#'
elif direction == "DOWN":
if self._isInBounds((x+1, y)) and self.tiles[x+1][y].data == '+': self.stevendorf = True
return x < MAP_HEIGHT - 1 and self.tiles[x+1][y].data != '#'
elif direction == "RIGHT":
if self._isInBounds((x, y+1)) and self.tiles[x][y+1].data == '+': self.stevendorf = True
return y < MAP_WIDTH - 1 and self.tiles[x][y+1].data != '#'
elif direction == "LEFT":
if self._isInBounds((x, y-1)) and self.tiles[x][y-1].data == '+': self.stevendorf = True
return y > 0 and self.tiles[x][y-1].data != '#'
def mapMove(self, direction):
x,y = self.player
self.tiles[x][y].data = ' '
if direction == 'UP':
self.player = (x-1,y)
elif direction == "DOWN":
self.player = (x+1,y)
elif direction == "RIGHT":
self.player = (x,y+1)
elif direction == "LEFT":
self.player = (x,y-1)
if self.tiles[self.player[0]][self.player[1]].data == '*':
# player has found the Amulet of Awesomeness
return True
if self.tiles[self.player[0]][self.player[1]].data == '@':
return True
self.tiles[self.player[0]][self.player[1]].data = 'X'
self.revealRoom()
return False
def _isInBounds(self,loc):
return 0 <= loc[0] < MAP_HEIGHT and 0 <= loc[1] < MAP_WIDTH
def locIsFree(self, loc):
if not self._isInBounds(loc):
return False
return self.tiles[loc[0]][loc[1]].data == ' '
def _locIsDoor(self,loc):
if not self._isInBounds(loc):
return False
return self.tiles[loc[0]][loc[1]].data == '='
def _locIsWall(self,loc):
return self.tiles[loc[0]][loc[1]].data == '#'
@staticmethod
def manDist(src, dest):
return abs(src[0] - dest[0]) + abs(src[1] - dest[1])
def findPath(self):
src, dest = self.player, self.victory
visited = deepcopy(self._visited)
queue = [(self.manDist(src, dest), src, 0),]
cameFrom = {}
while queue:
currentLoc, cost = queue[0][1], queue[0][2]
if currentLoc == dest:
return self._constructPath(cameFrom, currentLoc)
heapq.heappop(queue)
visited.add(currentLoc)
for diff in (-1,1):
xLoc = (currentLoc[0] + diff, currentLoc[1])
if xLoc not in visited:
queueItem = (cost + self.manDist(xLoc, dest) + 1, xLoc, cost + 1)
if (self._isInBounds(xLoc) and not self._locIsWall(xLoc)) and (queueItem not in queue or (cost + 1 < cameFrom[xLoc][1])):
cameFrom[xLoc] = (currentLoc, cost + 1) # update cameFrom map so that xLoc -> where I came from, and distance
if queueItem not in queue:
heapq.heappush(queue, queueItem)
yLoc = (currentLoc[0], currentLoc[1] + diff)
if yLoc not in visited:
queueItem = (cost + self.manDist(yLoc, dest) + 1, yLoc, cost + 1)
if (self._isInBounds(yLoc) and not self._locIsWall(yLoc)) and (queueItem not in queue or (cost + 1 < cameFrom[yLoc][1])):
cameFrom[yLoc] = (currentLoc, cost + 1) # update cameFrom map so that yLoc -> where I came from, and distance
if queueItem not in queue:
heapq.heappush(queue, queueItem)
return None
def _constructPath(self, cameFromMap, current):
path = [current,]
while current in cameFromMap.keys():
tempKey = current
current = cameFromMap[current][0]
del cameFromMap[tempKey]
path.append(current)
path.reverse()
path.pop(0) # remove self.player position
return path