-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapgen.py
649 lines (569 loc) · 20.5 KB
/
mapgen.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
import random
import copy
import math
class Element:
def __init__(self, elem_id, position, difficulty):
self.id = elem_id
self.position = position
self.difficulty = difficulty
def __repr__(self):
return f"<mapgen.Element id={self.id},position={self.position},difficulty={self.difficulty}>"
def __str__(self):
return self.id
class Creature(Element):
def __init__(self,health, elem_id, position, difficulty, speed, flying,ranged = False,cool = 1 ,idd = None):
super().__init__(elem_id, position, difficulty)
self.hp = health
self.speed = speed
self.flying = flying
self.has_key = False
self.strength = difficulty
self.ranged = ranged
self.cool = cool
self.idd = idd
class Item(Element):
def __init__(self, elem_id, sub_id, position, difficulty):
super().__init__(elem_id, position, difficulty)
self.sub_id = sub_id
class Trap:
def __init__(self, position):
self.position = position
def __repr__(self):
return f"<mapgen.Trap position={self.position},damage={self.damage}>"
class Weapon(Item):
def __init__(self, weapon_id, sub_id, position, difficulty, attack_cooldown, durability, damage, reach):
super().__init__(weapon_id, sub_id, position,difficulty)
self.attack_cooldown = attack_cooldown
self.durability = durability
self.damage = damage
self.reach = reach
def upgrade(self):
self.durability = round(1.2 * self.durability)
self.damage += 5
class Potion(Item):
def __init__(self, potion_id, sub_id, position, difficulty):
super().__init__(potion_id, sub_id, position,difficulty)
def upgrade(self):
pass
class Spell(Item):
def __init__(self, spell_id, sub_id, position, difficulty, damage, radius, speed, attack_cooldown):
super().__init__(spell_id, sub_id, position,difficulty)
self.damage = damage
self.radius = radius
self.speed = speed
self.attack_cooldown = attack_cooldown
def upgrade(self):
self.damage += 5
self.radius += 100
class Coord:
def __init__(self, x, y):
self.x = x
self.y = y
def __getitem__(self, key):
if key in (0, 1):
return (self.x, self.y)[key]
raise IndexError("Invalid key")
def __setitem__(self, key, value):
if key == 0:
self.x = value
elif key == 1:
self.y = value
else:
raise IndexError("Invalid key")
def __eq__(self, other):
return self.x == other.x and self.y == other.y
def __str__(self):
return f"({self.x},{self.y})"
def __repr__(self):
return f"<mapgen.Coord x={self.x},y={self.y}>"
def __add__(self, other):
return Coord(self.x + other.x, self.y + other.y)
def distance(self, other):
if not isinstance(other, Coord):
raise TypeError("Not a coord")
return math.sqrt((self.x - other.x) ** 2 + (self.y - other.y) ** 2)
class Treasure:
def __init__(self, position):
self.position = position
self.item = None
def __repr__(self):
return f"<mapgen.Treasure position={self.position},item={self.item}>"
class Stairs:
def __init__(self, position):
self.position = position
def __repr__(self):
return f"<mapgen.Stairs position={self.position}>"
class Path:
def __init__(self):
self.points = []
def __contains__(self, coord):
if not isinstance(coord, Coord):
raise TypeError("Not an Coord")
return coord in self.points
def __getitem__(self, key):
return self.points[key]
def __setitem__(self, key, value):
self.points[key] = value
def __len__(self):
return len(self.points)
def __repr__(self):
return f"<mapgen.Path nb_points={len(self.points)}>"
def add_point(self, coord):
"""Add new point to new path"""
self.points.append(coord)
class CircleRoom:
def __init__(self, center, radius):
self.center = center
self.radius = radius
self.size = radius
def __repr__(self):
return f"<mapgen.CircleRoom center={self.center},radius={self.radius}>"
def __contains__(self, coord):
if not isinstance(coord, Coord):
raise TypeError("Not an Coord")
return self.center.distance(coord) <= self.radius
def intersect_circle(self, other):
"""Check intersection with other circle room"""
return self.center.distance(other.center) <= self.radius + other.radius
def intersect_rect(self, other):
return self.center.distance(other.center) <= self.radius + math.sqrt(
(other.width**2 + other.height**2)
)
def intersect_with(self, other):
if isinstance(other, CircleRoom):
return self.intersect_circle(other)
return self.intersect_rect(other)
def is_overlapping(self, room_list):
return any([self.intersect_with(room) for room in room_list])
class Room:
def __init__(self, top_left, width, height):
self.top_left = top_left
self.width = width
self.height = height
self.size = min(width,height)
@property
def bottom_right(self):
return self.top_left + Coord(self.width - 1, self.height - 1)
@property
def center(self):
return Coord(
(self.top_left.x + self.bottom_right.x) // 2,
(self.top_left.y + self.bottom_right.y) // 2,
)
def __repr__(self):
return f"<mapgen.Room top_left={self.top_left},width={self.width},height={self.height}>"
def __contains__(self, coord):
if not isinstance(coord, Coord):
raise TypeError("Not an Coord")
correct_x = self.top_left.x <= coord.x <= self.top_left.x + self.width - 1
correct_y = self.top_left.y <= coord.y <= self.top_left.y + self.height - 1
return correct_x and correct_y
def intersect_x(self, other):
"""Check x axis intersection between two rooms"""
return (
other.top_left.y <= self.top_left.y <= other.bottom_right.y
or other.top_left.y <= self.bottom_right.y <= other.bottom_right.y
or self.top_left.y <= other.top_left.y <= self.bottom_right.y
or self.top_left.y <= other.bottom_right.y <= self.bottom_right.y
)
def intersect_y(self, other):
"""Check y axis intersection between two rooms"""
return (
other.top_left.x <= self.top_left.x <= other.bottom_right.x
or other.top_left.x <= self.bottom_right.x <= other.bottom_right.x
or self.top_left.x <= other.top_left.x <= self.bottom_right.x
or self.top_left.x <= other.bottom_right.x <= self.bottom_right.x
)
def intersect_with(self, other):
"""Check intersection between two rooms"""
return self.intersect_x(other) and self.intersect_y(other)
def is_overlapping(self, room_list):
"""Check intersection between multiples rooms"""
return any([self.intersect_with(room) for room in room_list])
class Map:
available_creatures = [
Creature(10,
["sprite_0.png", "sprite_1.png"],
position=None,
difficulty=1,
speed=0.2,
flying=True,
ranged = 4,
cool=1,
idd="lightning"
),
Creature(6,["spider.png","spider2.png"],None,1,0.1,flying=True,ranged=6,cool=2,idd="fire")
,
Creature(3,
["pac1.png","pac2.png","pac3.png"],
position=None,
difficulty=0.2,
speed=0.15,
flying=True,
cool=0.1
),
Creature(30,["golem.png","golem.png"],None,3,0.05,True,False,cool=3,idd="golem")
]
available_weapon = [
Weapon(
"sword",
sub_id="diamond_sword",
position=None,
difficulty=1,
durability=50,
damage=4,
reach=2,
attack_cooldown=0.2
),
Weapon(
"sword",
sub_id="amber_sword",
position=None,
difficulty=1,
durability=50,
damage=7,
reach=2,
attack_cooldown=0.5
),
Weapon(
"sword",
sub_id="emerald_sword",
position=None,
difficulty=1,
durability=50,
damage=1,
reach=2,
attack_cooldown=0.1
),
Weapon(
"sword",
sub_id="normal_sword",
position=None,
difficulty=1,
durability=50,
damage=2,
reach=2,
attack_cooldown=0.5
),
Weapon(
"sword",
sub_id="axe",
position=None,
difficulty=1,
durability=50,
damage=10,
reach=3,
attack_cooldown=3
),
Weapon(
"bow",
sub_id=None,
position=None,
difficulty=1,
durability=20,
damage=5,
reach=0,
attack_cooldown=1
)
]
available_spell = [
Spell(
"ice",
"ice",
None,
1,
0,
6,
speed=None,
attack_cooldown=8
),
Spell(
"fireball",
sub_id="fireball",
position=None,
difficulty=1,
damage=5,
radius=1,
speed=0.3,
attack_cooldown=1
),
Spell(
"lightning",
None,
None,
1,
1,
1,
0,
1
),
Spell(
"teleportation",
sub_id = None,
position = None,
difficulty = 2,
damage = 0,
radius = 375,
speed = None,
attack_cooldown = 1
)
]
available_potion = [
Potion(
"healing",
sub_id = None,
position = None,
difficulty = 1
),
Potion(
"mana",
sub_id = None,
position = None,
difficulty = 1
),
Potion(
"armor",
sub_id=None,
position=None,
difficulty=1
)
]
def __init__(self, width, height, max_rooms=4):
self.width = width
self.height = height
self.max_rooms = max_rooms
self.max_exot_rooms = 50
self.rooms = []
self.paths = []
self.creatures = []
self.items = []
self.traps = []
self.weapons = []
self.spells = []
self.potions = []
self.next_level_stair = None
self.treasure = None
def __repr__(self):
return f"<mapgen.Map width={self.width},height={self.height},nb_rooms={len(self.rooms)}>"
def random_room(self):
"""Generate a random room"""
x = random.randint(1, self.width - 4)
y = random.randint(1, self.height - 4)
width = random.randint(3, 8) if x+8 < self.width else random.randint(3,self.width-x-1)
height = random.randint(3, 8) if y+8 < self.height else random.randint(3,self.height-y-1)
return Room(Coord(x, y), width, height)
def random_circle_room(self):
radius = random.randint(3, 5)
x = random.randint(radius + 2, self.width - radius-2)
y = random.randint(radius + 2, self.height - radius-2)
return CircleRoom(Coord(x, y), radius)
def generate_random(self):
"""Fill map with random rooms"""
for _ in range(self.max_rooms):
room = self.random_room()
if not room.is_overlapping(self.rooms):
self.rooms.append(room)
def find_valid_random_coord(self, room):
valid_coord = False
position = None
while not valid_coord:
if isinstance(room, Room):
abs_pos_x = random.randint(0, room.width - 1)
abs_pos_y = random.randint(0, room.height - 1)
position = Coord(
room.top_left.x + abs_pos_x, room.top_left.y + abs_pos_y
)
elif isinstance(room, CircleRoom):
distance = random.randint(0, room.radius)
angle = random.uniform(0, 2 * math.pi)
abs_pos_x = round(distance * math.cos(angle))
abs_pos_y = round(distance * math.sin(angle))
position = Coord(room.center.x + abs_pos_x, room.center.y + abs_pos_y)
valid_coord = all(
[
position != element.position
for element in self.creatures + self.weapons + self.spells + self.potions
]
)
return position
def fill_with_elements(self):
# Traps generation
selected_rooms = random.choices(self.rooms[1:], k=2)
for room in selected_rooms:
a = self.find_valid_random_coord(room)
self.traps.append(Trap(a))
for room in self.rooms[1:]:
nb_creatures = random.randint(0, 4)
weights = list(map(lambda c: 1 / c.difficulty, Map.available_creatures))
for _ in range(nb_creatures):
a = self.find_valid_random_coord(room)
while a.distance(room.center) > room.size - 2:
a = self.find_valid_random_coord(room)
position = a
creature = copy.copy(
random.choices(Map.available_creatures, weights, k=1)[0]
)
creature.position = position
if creature.id == ["spider.png","spider1.png"]:
self.creatures.append(creature)
for i in range(30):
while a.distance(room.center) > room.size - 1:
a = self.find_valid_random_coord(room)
position = a
creature = copy.copy(Map.available_creatures[1])
creature.position = position
self.creatures.append(creature)
else :
self.creatures.append(creature)
nb_weapon = random.randint(0, 2)
weights = list(map(lambda c: 1 / c.difficulty, Map.available_weapon))
for _ in range(nb_weapon):
position = self.find_valid_random_coord(room)
item = copy.copy(random.choices(Map.available_weapon, weights,k=1)[0])
item.position = position
self.weapons.append(item)
nb_spell = random.randint(0, 2)
weights = list(map(lambda c: 1 / c.difficulty, Map.available_spell))
for _ in range(nb_spell):
position = self.find_valid_random_coord(room)
item = copy.copy(random.choices(Map.available_spell, weights,k=1)[0])
item.position = position
self.spells.append(item)
nb_potion = random.randint(0, 2)
weights = list(map(lambda c: 1 / c.difficulty, Map.available_potion))
for _ in range(nb_potion):
position = self.find_valid_random_coord(room)
item = copy.copy(random.choices(Map.available_potion, weights,k=1)[0])
item.position = position
self.potions.append(item)
#security to always have 1 mob x)
if not self.creatures:
weights = list(map(lambda c: 1 / c.difficulty, Map.available_creatures))
a = self.find_valid_random_coord(room)
while a.distance(room.center) > room.size - 2:
a = self.find_valid_random_coord(room)
position = a
creature = copy.copy(
random.choices(Map.available_creatures, weights, k=1)[0]
)
creature.position = position
self.creatures.append(creature)
random.choice(self.creatures).has_key = True
### generating 1 resting potion ###
pos = self.find_valid_random_coord(room)
item = Potion("resting", sub_id = None, position = pos, difficulty = 1)
self.potions.append(item)
def generate_stairs(self):
last_room = self.rooms[-1]
self.next_level_stair = Stairs(last_room.center)
def generate_treasure(self):
room = random.choice(self.rooms[1:])
position = self.find_valid_random_coord(room)
self.treasure = Treasure(position)
treasure_items = Map.available_weapon + Map.available_spell + Map.available_potion
for i in treasure_items:
i.upgrade()
self.treasure.item = copy.copy(random.choice(treasure_items))
self.treasure.item.position = None
def generate_random_circle(self):
for i in range(self.max_exot_rooms):
room = self.random_circle_room()
if not room.is_overlapping(self.rooms):
self.rooms.append(room)
def make_paths(self):
"""Create paths between rooms"""
for i in range(len(self.rooms) - 1):
first = self.rooms[i]
second = self.rooms[i + 1]
start = first.center
end = second.center
direction = None
position = Coord(start.x, start.y)
path = Path()
if start.y > end.y:
direction = Coord(0, -1)
else:
direction = Coord(0, 1)
for i in range(abs(end.y - start.y)):
path.add_point(position)
position += direction
path.add_point(position)
if start.x > end.x:
direction = Coord(-1, 0)
else:
direction = Coord(1, 0)
for i in range(abs(end.x - start.x)):
path.add_point(position)
position += direction
path.add_point(position)
self.paths.append(path)
def slice(self, top_left, width, height):
original_grid = self.grid()
result = []
for row in original_grid[top_left.y : top_left.y + width - 1]:
result.append(row[top_left.x : top_left.x + height - 1])
return result
def display(self, custom_grid=None):
"""Display grid on stdout"""
if custom_grid is None :
custom_grid = self.grid()
for line in custom_grid:
for elem in line:
print(elem, end="")
print("\n", end="")
def get_character_at(self, coord):
if coord == self.next_level_stair.position:
return "S"
elif any([coord == item.position for item in self.spells]):
return "L"
elif coord == self.treasure.position:
return "€"
elif any([coord == trap.position for trap in self.traps]):
return "T"
elif any([coord == creature.position for creature in self.creatures]):
return "x"
elif any([coord == item.position for item in self.weapons]):
return "w"
elif any([coord == item.position for item in self.potions]):
return "P"
elif any([coord in room for room in self.rooms]):
return "#"
elif any([coord in path for path in self.paths]):
return "%"
return "."
def grid(self):
"""Generate map grid"""
return [
[self.get_character_at(Coord(x, y)) for x in range(self.height)]
for y in range(self.width)
]
class Game:
def __init__(self, max_levels):
self.max_levels = max_levels
self.levels = []
self.active_level = 0
self.add_new_map()
def __repr__(self):
return f"<mapgen.Game max_levels={self.max_levels},active_level={self.active_level}"
@property
def current_map(self):
return self.levels[self.active_level]
def add_new_map(self):
new_map = Map(40, 40)
new_map.generate_random()
new_map.generate_random_circle()
new_map.make_paths()
new_map.generate_stairs()
new_map.fill_with_elements()
new_map.generate_treasure()
self.levels.append(new_map)
def move_up(self):
if self.active_level + 1 > self.max_levels - 1:
raise RuntimeError("Max level reached")
if self.active_level + 1 > len(self.levels) - 1:
self.add_new_map()
self.active_level += 1
def move_down(self):
if self.active_level - 1 < 0:
raise RuntimeError("Min level reached")
self.active_level -= 1