-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgamemap.py
203 lines (166 loc) · 5.67 KB
/
gamemap.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
from geom import Point, Rect
from typing import Iterable, Tuple
from random import choice
from tcod.path import maxarray, dijkstra2d
from tcod.map import compute_fov
from tcod.constants import FOV_DIAMOND
import swatch as sw
import numpy as np
render_dt = np.dtype([("ch", np.int32), ("fg", "3B"), ("bg", "3B")])
tile_dt = np.dtype(
[
("walkable", bool),
("transparent", bool),
("dark", render_dt),
("light", render_dt),
]
)
def new_tile(
*,
walkable: int,
transparent: int,
dark=Tuple[int, Tuple[int, int, int], Tuple[int, int, int]],
light=Tuple[int, Tuple[int, int, int], Tuple[int, int, int]],
) -> np.ndarray:
return np.array((walkable, transparent, dark, light), dtype=tile_dt)
SHROUD = np.array((ord(" "), sw.WHITE, sw.BLACK), dtype=render_dt)
class GameMap:
"""Describes a game map."""
def __init__(
self,
id: str,
name: str,
width: int,
height: int,
dark: bool = False,
wall_fg: Tuple[int, int, int] = sw.STONE_LIGHT,
floor_fg: Tuple[int, int, int] = sw.STONE_DARK,
):
self.explored = np.zeros((width, height), dtype=bool, order="F")
self.visible = np.zeros((width, height), dtype=bool, order="F")
self.dist = maxarray((width, height), order="F")
self.cost = np.zeros((width, height), dtype=np.int32, order="F")
self.dark = dark
self.__id = id
self.__name = name
self.wall_tile = new_tile(
transparent=False,
walkable=False,
light=(ord("#"), wall_fg, sw.BLACK),
dark=(ord("#"), sw.dark(wall_fg), sw.BLACK),
)
self.floor_tile = new_tile(
transparent=True,
walkable=True,
light=(ord("."), floor_fg, sw.BLACK),
dark=(ord("."), sw.dark(floor_fg), sw.BLACK),
)
self.stairs_down_tile = new_tile(
transparent=True,
walkable=True,
light=(ord(">"), sw.TARGET, sw.BLACK),
dark=(ord(">"), sw.dark(sw.TARGET), sw.BLACK),
)
self.stairs_up_tile = new_tile(
transparent=True,
walkable=True,
light=(ord("<"), sw.TARGET, sw.BLACK),
dark=(ord("<"), sw.dark(sw.TARGET), sw.BLACK),
)
self.__tiles = np.full((width, height), fill_value=self.wall_tile, order="F")
@property
def id(self) -> str:
return self.__id
@property
def name(self) -> str:
return self.__name
@property
def width(self) -> int:
return self.__tiles.shape[0]
@property
def height(self) -> int:
return self.__tiles.shape[1]
@property
def tiles(self) -> np.ndarray:
return self.__tiles
def update_cost(self, pts: Iterable[Point] = None):
self.cost = np.select(
condlist=[self.tiles["walkable"]], choicelist=[1], default=0
)
if pts:
for pt in pts:
self.cost[pt.x, pt.y] = 10
def update_dmap(self, *goals: Point):
self.dist = maxarray((self.width, self.height), order="F")
for goal in goals:
self.dist[goal.x, goal.y] = 0
dijkstra2d(self.dist, self.cost, True, out=self.dist)
def update_fov(self, x: int, y: int, r: int):
self.visible = compute_fov(self.tiles["transparent"], (x, y), r, FOV_DIAMOND)
self.explored |= self.visible
def in_bounds(self, x: int, y: int) -> bool:
return 0 <= x < self.width and 0 <= y < self.height
def walkable(self, x: int, y: int) -> bool:
return self.__tiles[x, y]["walkable"]
def transparent(self, x: int, y: int) -> bool:
return self.__tiles[x, y]["transparent"]
def carve(self, x: int, y: int):
self.tiles[x, y] = self.floor_tile
def carve_rect(self, r: Rect):
self.tiles[r.x1 : r.x2 + 1, r.y1 : r.y2 + 1] = self.wall_tile
self.tiles[r.x1 + 1 : r.x2, r.y1 + 1 : r.y2] = self.floor_tile
def neighbors(self, x: int, y: int):
return [
Point(i, j)
for (i, j) in [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)]
if self.in_bounds(i, j)
]
def add_down_stair(self, x: int, y: int):
self.tiles[x, y] = self.stairs_down_tile
def add_up_stair(self, x: int, y: int):
self.tiles[x, y] = self.stairs_up_tile
def on_edge(self, x: int, y: int) -> bool:
return x == 0 or x == self.width - 1 or y == 0 or y == self.height - 1
def get_random_floor(self) -> Point:
cands = [
Point(x, y)
for x in range(0, self.width)
for y in range(0, self.height)
if self.in_bounds(x, y)
if self.walkable(x, y)
]
return choice(cands)
def arena(id: str, name: str, width: int, height: int, dark: bool = True) -> GameMap:
m = GameMap(id, name, width, height, dark)
m.carve_rect(Rect.from_xywh(0, 0, width, height))
m.update_cost()
return m
def drunk_walk(
id: str,
name: str,
width: int,
height: int,
coverage: float = 0.5,
dark: bool = True,
) -> GameMap:
m = GameMap(id, name, width, height, dark)
x = m.width // 2
y = m.height // 2
pt = Point(x, y)
stack = [pt]
floors = 0
desired = int(width * height * max(0.1, min(coverage, 1)))
m.carve(x, y)
def f(pt):
return not (m.walkable(pt.x, pt.y) or m.on_edge(pt.x, pt.y))
while floors < desired:
cands = list(filter(f, m.neighbors(pt.x, pt.y)))
if len(cands) > 0:
pt = choice(cands)
m.carve(pt.x, pt.y)
stack.append(pt)
floors += 1
else:
pt = stack.pop()
m.update_cost()
return m