-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_map.py
288 lines (244 loc) · 10.4 KB
/
game_map.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import tcod as libtcod
from tile import Tile
from rectangle import Rect
from entity import Entity
from components.item import Item
from components.stairs import Stairs
from components.equipment import EquipmentSlots
from components.equippable import Equippable
from components.shop import Shop
from components.chest import Chest
from components.purse import Purse
from components.monster_factory import make_monster_random, make_monster
from components.factory import make_item
from message_log import Message
from random import randint
from globals import RenderOrder
import math
class GameMap:
def __init__(self, width, height, dungeon_level=1):
self.width = width
self.height = height
self.tiles = self.initialize_tiles()
self.shopkeeper = None
self.dungeon_level = dungeon_level
def is_blocked(self, x, y):
return self.tiles[x][y].blocked
def create_room(self, room):
for x in range(room.x1 + 1, room.x2):
for y in range(room.y1 + 1, room.y2):
self.tiles[x][y].blocked = False
self.tiles[x][y].block_sight = False
def create_tunnel(self, a1, a2, b, h_dir=True):
for a in range(min(a1,a2), max(a1,a2) + 1):
if h_dir:
self.tiles[a][b].blocked = False
self.tiles[a][b].block_sight = False
else:
self.tiles[b][a].blocked = False
self.tiles[b][a].block_sight = False
def create_h_tunnel(self, x1, x2, y):
for x in range(min(x1, x2), max(x1, x2) + 1):
self.tiles[x][y].blocked = False
self.tiles[x][y].block_sight = False
def create_v_tunnel(self, y1, y2, x):
for y in range(min(y1, y2), max(y1, y2) + 1):
self.tiles[x][y].blocked = False
self.tiles[x][y].block_sight = False
def is_free(self, entities, x, y):
if not self.tiles[x][y].blocked:
for entity in entities:
if entity.x == x and entity.y == y:
return False
return True
else:
return False
def place_entities(self, room, entities, max_monsters, max_items, component, chest_chance=40):
max_monsters += math.floor(self.dungeon_level/10)
number_of_monsters = randint(0, max_monsters)
number_of_items = randint(0, max_items)
for i in range(number_of_monsters):
x = randint(room.x1 + 1, room.x2 - 1)
y = randint(room.y1 + 1, room.y2 - 1)
if not any([entity for entity in entities if entity.x == x and entity.y == y]):
monster = make_monster_random(self.dungeon_level)
monster.x = x
monster.y = y
entities.append(monster)
if randint(0,100) < chest_chance:
x = randint(room.x1 + 1, room.x2 - 1)
y = randint(room.y1 + 1, room.y2 - 1)
if not any([entity for entity in entities if entity.x == x and entity.y == y]):
gold_coins = randint(2*self.dungeon_level,10*self.dungeon_level)
item_component = Chest(money=gold_coins)
chest = Entity(x, y, "C", libtcod.darker_orange, 'Chest', True,
render_order=RenderOrder.ITEM,
container=item_component)
entities.append(chest)
for i in range(max_items):
x = randint(room.x1 + 1, room.x2 - 1)
y = randint(room.y1 + 1, room.y2 - 1)
if not any([entity for entity in entities if entity.x == x and entity.y == y]):
# Item drop roll
item_chance = randint(0, 100)
if item_chance < 30: # 30%
item = make_item("health_tome")
elif item_chance < 60: # 30%
item = make_item("magic_missiles")
elif item_chance < 80: # 20%
item = make_item("paralysis")
else: #20%
item = make_item("fireball")
item.x = x
item.y = y
entities.append(item)
def next_floor(self, player, message_log, config, component):
self.dungeon_level += 1
entities = [player]
self.tiles = self.initialize_tiles()
self.make_map(
config.get('MAX_ROOMS'),
config.get('ROOM_MIN_SIZE'),
config.get('ROOM_MAX_SIZE'),
player,
entities,
config.get('MAX_MONSTERS'),
config.get('MAX_ITEMS'),
component)
player.fighter.heal(player.fighter.max_hp // 2)
message_log.add_message(
Message('You take a moment to rest', libtcod.light_violet)
)
return entities
def make_boss_map(self, player, entities):
new_room = Rect(10, 10, 10, 10)
self.create_room(new_room)
player.x = 12
player.y = 15
if (self.dungeon_level <= 7):
dragon = make_monster('dragon', self.dungeon_level+3, boss=True)
dragon.x = 18
dragon.y = 15
entities.append(dragon)
elif (self.dungeon_level <= 14):
dragon = make_monster('dragon', self.dungeon_level+3, boss=True)
dragon.x = 18
dragon.y = 15
warlock1 = make_monster('warlock', self.dungeon_level+2)
warlock1.x = 19
warlock1.y = 14
warlock2 = make_monster('warlock', self.dungeon_level+2)
warlock2.x = 19
warlock2.y = 16
entities.append(dragon)
entities.append(warlock1)
entities.append(warlock2)
elif (self.dungeon_level <= 28):
dragon = make_monster('red_dragon', self.dungeon_level+4, boss=True)
dragon.x = 18
dragon.y = 15
warlock1 = make_monster('warlock', self.dungeon_level+2)
warlock1.x = 19
warlock1.y = 11
warlock2 = make_monster('warlock', self.dungeon_level+2)
warlock2.x = 19
warlock2.y = 19
troll1 = make_monster('troll', self.dungeon_level+3)
troll1.x = 14
troll1.y = 16
entities.append(dragon)
entities.append(warlock1)
entities.append(warlock2)
else:
dragon = make_monster('elder_dragon', self.dungeon_level+4, boss=True)
dragon.x = 18
dragon.y = 15
troll1 = make_monster('troll_chief', self.dungeon_level+3)
troll1.x = 14
troll1.y = 16
warlock1 = make_monster('shaman', self.dungeon_level+2)
warlock1.x = 13
warlock1.y = 11
warlock2 = make_monster('shaman', self.dungeon_level+2)
warlock2.x = 14
warlock2.y = 19
entities.append(dragon)
entities.append(warlock1)
entities.append(warlock2)
def make_light_example(self, player, entities):
player.x = 12
player.y = 12
new_room = Rect(10, 10, 10, 10)
self.create_room(new_room)
new_room_2 = Rect(25, 5, 10, 20)
self.create_room(new_room_2)
self.create_tunnel(20, 25, 15, True)
self.tiles[27][12].blocked = True
self.tiles[27][12].block_sight = True
self.tiles[29][12].blocked = True
self.tiles[29][12].block_sight = True
self.tiles[31][12].blocked = True
self.tiles[31][12].block_sight = True
self.tiles[33][12].blocked = True
self.tiles[33][12].block_sight = True
self.tiles[27][18].blocked = True
self.tiles[27][18].block_sight = True
self.tiles[29][18].blocked = True
self.tiles[29][18].block_sight = True
self.tiles[31][18].blocked = True
self.tiles[31][18].block_sight = True
self.tiles[33][18].blocked = True
self.tiles[33][18].block_sight = True
def make_map(self, max_rooms, min_size, max_size, player, entities, max_monsters, max_items, components):
if False:
return self.make_light_example(player, entities)
if ((self.dungeon_level % 7) == 0):
return self.make_boss_map(player, entities)
rooms = []
num_rooms = 0
last_room_x = None
last_room_y = None
for r in range(max_rooms):
w = randint(min_size, max_size)
h = randint(min_size, max_size)
x = randint(0, self.width - w - 1)
y = randint(0, self.height - h - 1)
new_room = Rect(x, y, w, h)
for other_room in rooms:
if new_room.intersect(other_room):
break
else:
self.create_room(new_room)
(new_x, new_y) = new_room.center()
last_room_x = new_x
last_room_y = new_y
last_corner_x = x+1
last_corner_y = y+1
if num_rooms == 0:
player.x = new_x
player.y = new_y
else:
(prev_x, prev_y) = rooms[num_rooms - 1].center()
if randint(0, 1) == 1:
self.create_tunnel(prev_x, new_x, prev_y, True)
self.create_tunnel(prev_y, new_y, new_x, False)
else:
self.create_tunnel(prev_y, new_y, prev_x, False)
self.create_tunnel(prev_x, new_x, new_y, True)
self.place_entities(new_room, entities, max_monsters, max_items, components)
rooms.append(new_room)
num_rooms += 1
stairs_component = Stairs(self.dungeon_level+1)
down_stairs = Entity(last_room_x, last_room_y, '>', libtcod.white, 'Stairs',
render_order=RenderOrder.STAIRS, stairs=stairs_component)
entities.append(down_stairs)
shop_component = Shop(self.dungeon_level)
self.shopkeeper = Entity(down_stairs.x+1, down_stairs.y+1, '@', libtcod.light_blue, 'Shopkeeper', True,
render_order=RenderOrder.ACTOR, shop=shop_component)
entities.append(self.shopkeeper)
for e in entities:
if e.x == self.shopkeeper.x and e.y == self.shopkeeper.y and e != self.shopkeeper:
entities.remove(e)
def initialize_tiles(self):
tiles = [[Tile(True) for y in range(self.height)] for x in range(self.width)]
return tiles