-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.py
302 lines (230 loc) · 12.6 KB
/
generator.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
from random import randint, randrange, random, choice, shuffle
import pygame as pyg
import pyghelper
import constants as co
import pattern as pat
import textures as tx
from tile import Tile, TileType
import utils
def generate_random_depths(count: int, start_y: int, height: int) -> list[int]:
gap = height // count
return [start_y + gap * i + randint(0, gap) for i in range(count)]
class TerrainGenerator:
def __init__(self) -> None:
self.depth: int = 0
# Pattern
self.in_pattern: list[int] = list()
self.patterns: list[str] = list()
self.pattern_x: list[int] = list()
self.pattern_depths: list[int] = list()
self.patterns_offset: list[int] = list()
# Ressources
self.current_resource_probability: float = 0
self.last_resource_type: TileType = TileType.WATER
# Bonus
self.first_bonus_layer: int = -1
self.current_bonus_probability: float = 0
def _equitable_resource(self, cycle: int = 1):
while True:
resources = [co.ResourceType.WATER, co.ResourceType.WATER, co.ResourceType.NITROGEN, co.ResourceType.PHOSPHORUS] * cycle
shuffle(resources)
yield from [TileType(resource.value) for resource in resources]
def _resources_quantities(self, min: int, max: int):
while True:
yield randint(min, max)
def _get_pattern_tiles(self, pattern_str: str, x: int, y: int) -> list[Tile]:
pattern: list[Tile] = []
for dx, c in enumerate(pattern_str):
if c == 'r':
tile = Tile(TileType.ROCK, x + dx, y)
elif c == 'x':
if random() < co.RESOURCE_TO_BONUS_PROBABILITY:
bonus_type = TileType(choice(list(co.BonusType)).value)
tile = Tile(bonus_type, x + dx, self.depth)
else:
tile = Tile(next(self.terrain_resources), x + dx, y)
tile.set_starting_resource(next(self.resources_quantities), utils.random_float(co.RESOURCE_ABSORPTION_MODIFIER_MIN, utils.clamped_lerp(self.depth, 1, co.RESOURCE_ABSORPTION_MODIFIER_MAX_MAX_DEPTH, co.RESOURCE_ABSORPTION_MODIFIER_MAX_MIN, co.RESOURCE_ABSORPTION_MODIFIER_MAX_MAX)))
self.last_resource_type = tile.type
elif c == 's':
if random() < co.RESOURCE_TO_BONUS_PROBABILITY:
bonus_type = TileType(choice(list(co.BonusType)).value)
tile = Tile(bonus_type, x + dx, self.depth)
else:
tile = Tile(self.last_resource_type, x + dx, y)
tile.set_starting_resource(next(self.resources_quantities), utils.random_float(co.RESOURCE_ABSORPTION_MODIFIER_MIN, utils.clamped_lerp(self.depth, 1, co.RESOURCE_ABSORPTION_MODIFIER_MAX_MAX_DEPTH, co.RESOURCE_ABSORPTION_MODIFIER_MAX_MIN, co.RESOURCE_ABSORPTION_MODIFIER_MAX_MAX)))
else:
tile = Tile(TileType.BASE, x + dx, y)
pattern.append(tile)
return pattern
def _generate_first_resource_layer(self) -> list[Tile]:
if self.depth % 2 == 1:
resource_x = randrange(5, co.TILES_X - 5)
resource_type = next(self.terrain_resources)
else:
resource_x = -1
row = []
for x in range(co.TILES_X):
if x == resource_x:
tile = Tile(resource_type, x, self.depth)
tile.set_starting_resource(randint(*co.FIRST_RESOURCE_LAYER_QUANTITY), utils.random_float(co.RESOURCE_ABSORPTION_MODIFIER_MIN, co.RESOURCE_ABSORPTION_MODIFIER_MAX_MIN))
row.append(tile)
else:
row.append(Tile(TileType.BASE, x, self.depth))
return row
def _generate_second_resource_layer(self) -> list[Tile]:
if self.depth % 4 == 1:
special_x = randrange(0, co.TILES_X)
resource_type = next(self.terrain_resources)
elif self.depth % 4 == 3:
special_x = randrange(10, co.TILES_X - 10)
resource_type = TileType.ROCK
else:
special_x = -1
row: list[Tile] = []
for x in range(co.TILES_X):
if x == special_x:
tile = Tile(resource_type, x, self.depth)
if tile.is_resource_tile():
tile.set_starting_resource(randint(*co.FIRST_RESOURCE_LAYER_QUANTITY), utils.random_float(co.RESOURCE_ABSORPTION_MODIFIER_MIN, co.RESOURCE_ABSORPTION_MODIFIER_MAX_MIN))
row.append(tile)
else:
row.append(Tile(TileType.BASE, x, self.depth))
if self.depth == self.first_bonus_layer:
bonus_x = randint(8, co.TILES_X - 8)
row[bonus_x] = Tile(choice((TileType.PRODUCTION, TileType.CONSUMPTION)), bonus_x, self.depth)
row[bonus_x].bonus_value *= 2
return row
def _generate_levels_layer(self, level: int):
if self.depth in self.pattern_depths:
for index, depth in enumerate(self.pattern_depths):
if depth == self.depth:
self.in_pattern.append(index)
row: list[Tile] = [None] * co.TILES_X
for index in self.in_pattern:
if self.patterns_offset[index] >= len(self.patterns[index]):
self.in_pattern.remove(index)
break
pattern_tiles = self._get_pattern_tiles(self.patterns[index][self.patterns_offset[index]], self.pattern_x[index], self.pattern_depths[index] + self.patterns_offset[index])
row[self.pattern_x[index]:self.pattern_x[index]+len(self.patterns[index][0])] = pattern_tiles
self.patterns_offset[index] += 1
rock_probability = co.LEVELS_ROCK_PROBABILITY[level] if level <= 4 else co.get_end_rock_probability(self.depth)
row = [
(
Tile(TileType.ROCK, x, self.depth)
if random() < rock_probability
else Tile(TileType.BASE, x, self.depth)
) if tile is None else tile
for x, tile in enumerate(row)
]
if random() < self.current_resource_probability:
resource_x = randrange(0, co.TILES_X)
resource_type = next(self.terrain_resources)
row[resource_x] = Tile(resource_type, resource_x, self.depth)
row[resource_x].set_starting_resource(next(self.resources_quantities), utils.random_float(co.RESOURCE_ABSORPTION_MODIFIER_MIN, utils.clamped_lerp(self.depth, 1, co.RESOURCE_ABSORPTION_MODIFIER_MAX_MAX_DEPTH, co.RESOURCE_ABSORPTION_MODIFIER_MAX_MIN, co.RESOURCE_ABSORPTION_MODIFIER_MAX_MAX)))
self.current_resource_probability = co.LEVELS_BASE_RESOURCE_PROBABILITY[level]
else:
self.current_resource_probability += co.LEVELS_RESOURCE_PROBABILITY_INCREASE[level]
if random() < self.current_bonus_probability:
bonus_x = randrange(1, co.TILES_X - 1)
bonus_type = TileType(choice(list(co.BonusType)).value)
row[bonus_x] = Tile(bonus_type, bonus_x, self.depth)
self.current_bonus_probability = co.BASE_BONUS_PROBABILITY
else:
self.current_bonus_probability += co.BONUS_PROBABILITY_INC
return row[:co.TILES_X]
def _setup_level_1(self):
self.terrain_resources = self._equitable_resource(2)
self.resources_quantities = self._resources_quantities(*co.LEVEL_1_RESOURCES_QUANTITY)
self.in_pattern = []
self.pattern_x = [randint(0, co.TILES_X) - 2 for index in range(co.LEVEL_1_PATTERN_COUNT)]
self.pattern_depths = generate_random_depths(co.LEVEL_1_PATTERN_COUNT, self.depth, co.LEVEL_1_HEIGHT)
self.patterns = [pat.get_pattern(pat.LEVEL_1, self.pattern_x[index], self.pattern_depths[index]) for index in range(co.LEVEL_1_PATTERN_COUNT)]
self.patterns_offset = [0] * co.LEVEL_1_PATTERN_COUNT
self.current_resource_probability = co.LEVELS_BASE_RESOURCE_PROBABILITY[1]
def _setup_level_2(self):
self.terrain_resources = self._equitable_resource(2)
self.resources_quantities = self._resources_quantities(*co.LEVEL_2_RESOURCES_QUANTITY)
self.in_pattern = []
self.pattern_x = [randint(0, co.TILES_X) - 2 for index in range(co.LEVEL_2_PATTERN_COUNT)]
self.pattern_depths = generate_random_depths(co.LEVEL_2_PATTERN_COUNT, self.depth, co.LEVEL_2_HEIGHT)
self.patterns = [pat.get_pattern(pat.LEVEL_2, self.pattern_x[index], self.pattern_depths[index]) for index in range(co.LEVEL_2_PATTERN_COUNT)]
self.patterns_offset = [0] * co.LEVEL_2_PATTERN_COUNT
self.current_resource_probability = co.LEVELS_BASE_RESOURCE_PROBABILITY[2]
def _setup_level_3(self):
self.terrain_resources = self._equitable_resource(2)
self.resources_quantities = self._resources_quantities(*co.LEVEL_3_RESOURCES_QUANTITY)
self.in_pattern = []
self.pattern_x = [randint(0, co.TILES_X) - 2 for index in range(co.LEVEL_3_PATTERN_COUNT)]
self.pattern_depths = generate_random_depths(co.LEVEL_3_PATTERN_COUNT, self.depth, co.LEVEL_3_HEIGHT)
self.patterns = [pat.get_pattern(pat.LEVEL_3, self.pattern_x[index], self.pattern_depths[index]) for index in range(co.LEVEL_3_PATTERN_COUNT)]
self.patterns_offset = [0] * co.LEVEL_3_PATTERN_COUNT
self.current_resource_probability = co.LEVELS_BASE_RESOURCE_PROBABILITY[3]
def _setup_level_4(self):
self.terrain_resources = self._equitable_resource(3)
self.resources_quantities = self._resources_quantities(*co.LEVEL_4_RESOURCES_QUANTITY)
self.in_pattern = []
self.pattern_x = [randint(0, co.TILES_X) - 2 for index in range(co.LEVEL_4_PATTERN_COUNT)]
self.pattern_depths = generate_random_depths(co.LEVEL_4_PATTERN_COUNT, self.depth, co.LEVEL_4_HEIGHT)
self.patterns = [pat.get_pattern(pat.LEVEL_4, self.pattern_x[index], self.pattern_depths[index]) for index in range(co.LEVEL_4_PATTERN_COUNT)]
self.patterns_offset = [0] * co.LEVEL_4_PATTERN_COUNT
self.current_resource_probability = co.LEVELS_BASE_RESOURCE_PROBABILITY[4]
def _setup_end(self):
self.terrain_resources = self._equitable_resource(3)
self.patterns = []
def __next__(self) -> list[Tile]:
self.depth += 1
if self.depth == 0:
self.terrain_resources = self._equitable_resource(1)
self.resources_quantities = self._resources_quantities(*co.FIRST_RESOURCE_LAYER_QUANTITY)
if self.depth <= 4:
return [Tile(TileType.BASE, x, self.depth) for x in range(co.TILES_X)]
if self.depth <= 20:
return self._generate_first_resource_layer()
if self.depth == 21:
self.first_bonus_layer = randint(21, 27)
if self.depth <= co.TILES_Y + 2:
return self._generate_second_resource_layer()
if self.depth == co.TILES_Y + 2 + 1:
self._setup_level_1()
if self.depth <= co.LEVEL_1_DEPTH:
return self._generate_levels_layer(1)
if self.depth == co.LEVEL_1_DEPTH + 1:
self._setup_level_2()
if self.depth <= co.LEVEL_2_DEPTH:
return self._generate_levels_layer(2)
if self.depth == co.LEVEL_2_DEPTH + 1:
self._setup_level_3()
if self.depth <= co.LEVEL_3_DEPTH:
return self._generate_levels_layer(3)
if self.depth == co.LEVEL_3_DEPTH + 1:
self._setup_level_4()
if self.depth <= co.LEVEL_4_DEPTH:
return self._generate_levels_layer(4)
if self.depth == co.LEVEL_4_DEPTH + 1:
self._setup_end()
self.resources_quantities = self._resources_quantities(*co.get_end_resource_quantities(self.depth))
if not self.in_pattern:
self.in_pattern = []
self.pattern_x = [randint(0, co.TILES_X) - 2]
self.pattern_depths = [randint(self.depth, self.depth + int(round(utils.clamped_lerp(self.depth - co.END_HEIGHT, 0, co.END_PATTERN_END_GAP_DEPTH, co.END_PATTERN_STARTING_GAP, co.END_PATTERN_END_GAP), 0)))]
self.patterns = [pat.get_pattern(pat.ALL_PATTERNS, self.pattern_x[0], self.pattern_depths[0])]
self.patterns_offset = [0]
return self._generate_levels_layer(5)
def starting_terrain(self) -> list[list[Tile]]:
self.depth = -1
terrain: list[list[Tile]] = []
for y in range(co.TILES_Y):
terrain.append(next(self))
return terrain
def get_level(self, depth):
if depth <= co.TILES_Y + 2:
return 0
if depth <= co.LEVEL_1_DEPTH:
return 1
if depth <= co.LEVEL_2_DEPTH:
return 2
if depth <= co.LEVEL_3_DEPTH:
return 3
if depth <= co.LEVEL_4_DEPTH:
return 4
return '*'