-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcell_map.py
375 lines (279 loc) · 11.5 KB
/
cell_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
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
from copy import deepcopy
import numpy as np
from biome import *
from cell import *
from utils import dist
class MapType(Enum):
""" Maptype Enum
"""
island="island"
land="land"
class Map:
"""Cell Map class
"""
"""
Constants
"""
VIABLE_MIN_DIST_DIV = 2
ROUTE_WIDTH = 3
TC_MAX_RANGE_DIV = 15
TC_MIN_DIST_DIV = 40
FOREST_CHUNK_SIZE = 5
FOREST_MIN_DIST = 15
FOREST_MAX_DIST = 20
FOREST_MIN_NO = 30
FOREST_MAX_NO = 60
FISH_CHUNK_SIZE = 4
HUNT_CHUNK_SIZE = 5
def __init__(self, size, rand):
"""Initializer
Args:
size (int): Size of the map
rand (np.random.RandomState): Numpy random class object
"""
self.size = size
self.rand = rand
self.cells = []
self.placements = []
def set_biome(self, biome):
"""Set main terrain biome and fill initial cells
Args:
biome (Biomes): Main terrain biome
"""
cells = []
# Fill everything with main biome
for y in range(self.size):
row = []
for x in range(self.size):
row.append(Cell((y,x), biome))
cells.append(row)
cells = np.array(cells)
# Remove OOB cells
mask = self.create_circular_mask()
cells[~mask] = Cell((-1, -1), biome=CellType.OOB.value, status=Status.OOB)
self.cells = cells
def create_circular_mask(self):
"""Create a circular mask
Returns:
list: List of lists with bool values
"""
center = (int(self.size/2), int(self.size/2))
radius = min(center[0], center[1], self.size-center[0], self.size-center[1])
y, x = np.ogrid[:self.size, :self.size]
dist_from_center = np.sqrt((x - center[0])**2 + (y - center[1])**2)
mask = dist_from_center <= radius
return mask
def get_viable_cells(self, min_dist=0):
"""Get a list of viable (empty) cells
Args:
min_dist (int, optional): Exclude cells within dist of other placements. Defaults to 0.
Returns:
list: List of coordinate tuples
"""
coordinates = []
middle = self.size // 2, self.size // 2
for y in range(len(self.cells)):
for x in range(len(self.cells[y])):
cell = self.cells[y][x]
# Check cell status and distance to middle
if cell.status != Status.OOB and cell.status != Status.WATER and dist(middle, (y,x)) < self.size // self.VIABLE_MIN_DIST_DIV:
dist_to_buildings = [dist((y,x), val) for val in self.placements]
# Check distance to other placements
if all(val > min_dist for val in dist_to_buildings):
coordinates.append((y,x))
return coordinates
def get_viable_border_cells(self):
"""Get list of viable (empty) border cells
Returns:
list: List of coordinate tuples
"""
coordinates = []
for y in range(len(self.cells)):
for x in range(len(self.cells[y])):
if self.cells[y][x].status != Status.OOB:
if self.cells[y][x].status == Status.EMPTY:
coordinates.append((y,x))
if self.cells[y][self.size-x-1].status == Status.EMPTY:
coordinates.append((y,self.size - x))
break
return coordinates
def get_values_array(self):
"""Get color values array of the map
Returns:
list: List of lists containing color quadruples
"""
color_values = np.zeros((self.size, self.size, 4), dtype=np.uint8)
for y in range(len(self.cells)):
for x in range(len(self.cells[y])):
values = self.cells[y][x].get_values()
color_values[y][x] = values
return color_values
def draw_trade_route(self, pos1, pos2):
"""Draw trade route on the map
Args:
pos1 (tuple): Coordinate
pos2 (tuple): Coordinate
"""
if pos1[0] < pos2[0]:
start = deepcopy(list(pos1))
end = list(pos2)
else:
start = deepcopy(list(pos2))
end = list(pos1)
while not np.array_equal(start, end):
if start[0] < end[0]:
start[0] += 1
elif start[0] > end[0]:
start[0] -= 1
if start[1] > end[1]:
start[1] -= 1
elif start[1] < end[1]:
start[1] += 1
for i in range(self.ROUTE_WIDTH):
for j in range(self.ROUTE_WIDTH):
self.set_cell_biome((start[0]+i, start[1]+j), CellType.traderoute.value)
self.set_cell_biome((start[0]-i, start[1]-j), CellType.traderoute.value)
def close_to_biome(self, pos, biome, dist):
"""Checks if pos is wihtin dist of a biome
Args:
pos (tuple): Start coordinate
biome (Biome): Biome to search for
dist (int): Search distance
Returns:
bool: True if biome was found, False if not.
"""
for i in [-dist, dist]:
for j in range(-dist, dist):
if self.get_cell_biome((pos[0]+i, pos[1]+j)) == biome or self.get_cell_biome((pos[0]+j, pos[1]+i)) == biome:
return True
return False
def get_biome_coords(self, biome):
"""Get a list of coordinates of a given biome
Args:
biome (Biome): Biome to check
Returns:
list: List of biome coordinates
"""
coordinates = []
for y in range(len(self.cells)):
for x in range(len(self.cells[y])):
if self.get_cell_biome((y,x)) == biome:
coordinates.append((y,x))
return coordinates
def place_tc(self, pos):
"""Place a towncenter on the map and generate starting mine and hunt
Args:
pos (tuple): Town center position
Returns:
tuple: coordinate of the gold mine
"""
self.placements.append(pos)
# Get viable coordinates around TC
coordinates = []
max_range = self.size // self.TC_MAX_RANGE_DIV
for y in range(pos[0] - max_range, pos[0] + max_range):
for x in range(pos[1] - max_range, pos[1] + max_range):
if self.get_cell_status((y,x)) == Status.EMPTY and dist(pos, (y,x)) > self.size / self.TC_MIN_DIST_DIV:
coordinates.append((y,x))
# Place hunt and gold mine
rand_idx = self.rand.randint(0, len(coordinates))
gold_coord = coordinates[rand_idx]
hunt_coord = coordinates[-rand_idx]
self.place_placement(gold_coord, Status.GOLD)
self.place_hunt(hunt_coord)
self.set_cell_status(pos, Status.TC)
return gold_coord
def place_forest(self, pos):
"""Place forest on the map with pos coordinate as middle
Args:
pos (tuple): Forest middle coordinate
"""
distance = self.rand.randint(self.FOREST_MIN_DIST, self.FOREST_MAX_DIST)
amount = self.rand.randint(self.FOREST_MIN_NO, self.FOREST_MAX_NO)
y1 = self.rand.normal(pos[0], distance, size=(amount,)).astype('int')
x1 = self.rand.normal(pos[1], distance, size=(amount,)).astype('int')
for i in range(len(y1)):
for j in range(-self.FOREST_CHUNK_SIZE, self.FOREST_CHUNK_SIZE):
for k in range(-self.FOREST_CHUNK_SIZE, self.FOREST_CHUNK_SIZE):
y = y1[i] + j
x = x1[i] + k
if self.get_cell_status((y,x)) == Status.EMPTY:
self.set_cell_biome((y, x), CellType.forest.value)
def place_fish(self, pos, biome):
"""Place fish or whale biome in water cells
Args:
pos (tuple): Middle coordinate of fish chunk
biome (Biome): Biome to full. Use fish or whale.
"""
for j in range(-self.FISH_CHUNK_SIZE, self.FISH_CHUNK_SIZE):
for k in range(-self.FISH_CHUNK_SIZE, self.FISH_CHUNK_SIZE):
cur_pos = pos[0] + j, pos[1] + k
if self.get_cell_biome(cur_pos) == CellType.water.value:
self.set_cell_biome(cur_pos, biome)
def close_to_placement(self, pos, min_dist):
"""Check if pos is within min_dist of a placement
Args:
pos (tuple): coordinate
min_dist (float): Distance
Returns:
bool: True if a placement is in reach. False if not.
"""
for placement in self.placements:
if dist(pos, placement) < min_dist:
return True
return False
def place_hunt(self, pos):
"""Place hunt on map
Args:
pos (tuple): Hunt middle coordinate
"""
distance = self.rand.randint(5, 10)
amount = self.rand.randint(5, 10)
y1 = self.rand.normal(pos[0], distance, size=(amount,)).astype('int')
x1 = self.rand.normal(pos[1], distance, size=(amount,)).astype('int')
for i in range(len(y1)):
for j in range(-self.HUNT_CHUNK_SIZE, self.HUNT_CHUNK_SIZE):
for k in range(-self.HUNT_CHUNK_SIZE, self.HUNT_CHUNK_SIZE):
y = y1[i] + j
x = x1[i] + k
if self.get_cell_status((y,x)) == Status.EMPTY:
self.set_cell_biome((y,x), CellType.hunts.value)
def place_placement(self, pos, status):
"""Add a placement to the map
Args:
pos (tuple): Coordinate of placement
status (Status): Type of placement
"""
self.placements.append(pos)
self.set_cell_status(pos, status)
def legal_cell(self, pos):
"""Check if cell position is legal
Args:
pos (tuple): Position of cell
Returns:
bool: True if within bounds. False if not.
"""
return pos[0] >= 0 and pos[0] < self.size and pos[1] >= 0 and pos[1] < self.size
def is_in_bounds(self, pos):
"""Checks whether cell is in playable region or not
Args:
pos (tuple): Position of cell
Returns:
bool: True if status is not OOB. False if it is.
"""
return self.cells[pos].status != Status.OOB
def set_cell_color(self, pos, color):
if self.legal_cell(pos):
self.cells[pos].set_color(color)
def set_cell_biome(self, pos, biome):
if self.legal_cell(pos):
self.cells[pos].set_biome(biome)
def set_cell_status(self, pos, status):
if self.legal_cell(pos):
self.cells[pos].set_status(status)
def get_cell_status(self, pos):
if self.legal_cell(pos):
return self.cells[pos].get_status()
def get_cell_biome(self, pos):
if self.legal_cell(pos):
return self.cells[pos].get_biome()