-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocgen.py
More file actions
182 lines (143 loc) · 5.88 KB
/
Copy pathprocgen.py
File metadata and controls
182 lines (143 loc) · 5.88 KB
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
from __future__ import annotations
from typing import Tuple, Iterator, List, TYPE_CHECKING, Dict
from game_map import GameMap
import entity_factories
import tile_types
import random
import tcod
import color
if TYPE_CHECKING:
from engine import Engine
from entity import Entity
max_items_by_floor = [
(1,1),
(4,2),
]
max_monsters_by_floor = [
(1,2),
(4,3),
(6,5),
]
item_chances: Dict[int, List[Tuple[Entity, int]]] = {
0: [(entity_factories.nanogauze, 35)],
2: [(entity_factories.braindos, 10)],
4: [(entity_factories.mathogen, 25), (entity_factories.automakatana, 5)],
6: [(entity_factories.empgrenade, 25), (entity_factories.datamail, 15)],
}
enemy_chances: Dict[int, List[Tuple[Entity, int]]] = {
0: [(entity_factories.guard, 80)],
2: [(entity_factories.shinobi, 15)],
4: [(entity_factories.shinobi, 30)],
6: [(entity_factories.shinobi, 60)],
}
def get_max_value_for_floor(max_value_by_floor: List[Tuple[int, int]], floor: int) -> int:
current_value = 0
for floor_minimum, value in max_value_by_floor:
if floor_minimum > floor:
break
else:
current_value = value
return current_value
def get_entities_at_random(
weighted_chances_by_floor: Dict[int, List[Tuple[Entity, int]]],
number_of_entities: int,
floor: int,
) -> List[Entity]:
entity_weighted_chances = {}
for key, values in weighted_chances_by_floor.items():
if key > floor:
break
else:
for value in values:
entity = value[0]
weighted_chance = value[1]
entity_weighted_chances[entity] = weighted_chance
entities = list(entity_weighted_chances.keys())
entity_weighted_chance_values = list(entity_weighted_chances.values())
chosen_entities = random.choices(
entities, weights=entity_weighted_chance_values, k=number_of_entities
)
return chosen_entities
class RectangularRoom:
def __init__(self, x: int, y: int, width: int, height: int):
self.x1 = x
self.y1 = y
self.x2 = x + width
self.y2 = y + height
@property
def center(self) -> Tuple[int, int]:
center_x = int((self.x1 + self.x2) / 2)
center_y = int((self.y1 + self.y2) / 2)
return center_x, center_y
@property
def inner(self) -> Tuple[slice, slice]:
return slice(self.x1 + 1, self.x2), slice(self.y1 + 1, self.y2)
def intersects(self, other: RectangularRoom) -> bool:
return (
self.x1 <= other.x2 and self.x2 >= other.x1 and
self.y1 <= other.y2 and self.y2 >= other.y1
)
def tunnel_between(start: Tuple[int, int], end: Tuple[int, int]) -> Iterator[Tuple[int, int]]:
x1, y1 = start
x2, y2 = end
if random.random() < 0.5:
corner_x, corner_y = x2, y1
else:
corner_x, corner_y = x1, y2
for x, y in tcod.los.bresenham((x1, y1), (corner_x, corner_y)).tolist():
yield x, y
for x, y in tcod.los.bresenham((corner_x, corner_y), (x2, y2)).tolist():
yield x, y
def place_entities(room: RectangularRoom, dungeon: GameMap, floor_number: int) -> None:
number_of_monsters = random.randint(0, get_max_value_for_floor(max_monsters_by_floor, floor_number))
number_of_items = random.randint(0, get_max_value_for_floor(max_items_by_floor, floor_number))
monsters: List[Entity] = get_entities_at_random(enemy_chances, number_of_monsters, floor_number)
items: List[Entity] = get_entities_at_random(item_chances, number_of_items, floor_number)
for entity in monsters + items:
x = random.randint(room.x1 + 1, room.x2 - 1)
y = random.randint(room.y1 + 1, room.y2 -1)
if not any(entity.x == x and entity.y == y for entity in dungeon.entities):
entity.spawn(dungeon, x, y)
def generate_dungeon(
max_rooms: int,
room_min_size: int,
room_max_size: int,
map_width: int,
map_height: int,
engine: Engine,
) -> GameMap:
player = engine.player
dungeon = GameMap(engine, map_width, map_height, entities=[player])
rooms: List[RectangularRoom] = []
center_of_last_room = (0, 0)
for r in range(max_rooms):
room_width = random.randint(room_min_size, room_max_size)
room_height = random.randint(room_min_size, room_max_size)
x = random.randint(0, dungeon.width - room_width - 1)
y = random.randint(0, dungeon.height - room_height - 1)
new_room = RectangularRoom(x, y, room_width, room_height)
if any(new_room.intersects(other_room) for other_room in rooms):
continue
dungeon.tiles[new_room.inner] = tile_types.floor
if len(rooms) == 0:
player.place(*new_room.center, dungeon)
else:
for x, y in tunnel_between(rooms[-1].center, new_room.center):
dungeon.tiles[x, y] = tile_types.floor
center_of_last_room = new_room.center
place_entities(new_room, dungeon, engine.game_world.current_floor)
dungeon.tiles[center_of_last_room] = tile_types.elevator
dungeon.elevator_location = center_of_last_room
rooms.append(new_room)
return dungeon
def generate_boss_floor(map_width: int, map_height: int, engine: Engine) -> GameMap:
player = engine.player
dungeon = GameMap(engine, map_width, map_height, entities=[player])
boss_room = RectangularRoom(37, 20, 10, 10)
dungeon.tiles[boss_room.inner] = tile_types.floor
playerpos = (boss_room.center[0], boss_room.center[1] - 3)
player.place(*playerpos, dungeon)
entity_factories.boss.spawn(dungeon, *boss_room.center)
engine.message_log.add_message("CONTRACT: ELIMINATE NETRUNNER", color.boss_message)
engine.message_log.add_message('???: "So it was a setup..."', color.white)
return dungeon