-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
359 lines (272 loc) · 10.3 KB
/
models.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
from __future__ import annotations
import io
from abc import ABC, abstractmethod
from enum import Enum
from typing import Optional, List
import config
import surfaces
from core import Coordinate, Directions, Event, Container
class HavingPosition(ABC):
@property
@abstractmethod
def position(self) -> Coordinate:
pass
@property
@abstractmethod
def x(self) -> int:
pass
@property
@abstractmethod
def y(self) -> int:
pass
class Speed(Enum):
slow = 1
medium = 2
fast = 3
def slow_down(self):
return Speed.slow if self == Speed.medium else Speed.medium if self == Speed.fast else Speed.slow
def speed_up(self):
return Speed.fast if self == Speed.medium else Speed.medium if self == Speed.slow else Speed.fast
class Unit(HavingPosition):
moved: Event = None # (destination: Directions)
turned: Event = None # (from: Directions, to: Directions)
route_calculated = None # (start: Coordinate, finish: Coordinate, route: Iterable[Directions])
path_completed = None # ()
def __init__(self, name: str, field: Field, position: Coordinate,
speed: Speed = Speed.medium, direction: Directions = Directions.east):
self.moved = Event()
self.turned = Event()
self.route_calculated = Event()
self.path_completed = Event()
self._original_speed = speed
self._direction = direction
self._position = position
self._field = field
self._speed = speed
self._name = name
self.field.at_point(position).unit_container.put(self)
@property
def name(self) -> str:
return self._name
@property
def x(self) -> int:
return self._position.x
@property
def y(self) -> int:
return self._position.y
@property
def position(self) -> Coordinate:
return self._position
@property
def direction(self) -> Directions:
return self._direction
@property
def speed(self) -> Speed:
return self._speed
@speed.setter
def speed(self, speed: Speed):
self._speed = speed
def turn(self, direction: Directions):
turned = False
previous = self._direction
if previous != direction:
self._direction = direction
self.turned.notify(previous, direction)
turned = True
return turned
@property
def field(self) -> Field:
return self._field
def move(self, direction: Directions) -> bool:
moved = False
new_position: Coordinate = self.position + direction.value
destination: Cell = self.field.at_point(new_position)
if destination and destination.passable:
self.field.at_point(self.position).unit_container.remove()
self._speed = self._original_speed
destination.unit_container.put(self)
if destination.surface.speed != 0:
self._speed = (self._speed.speed_up()
if destination.surface.speed > 0 else self._speed.slow_down())
self._position = new_position
self.moved.notify(direction)
moved = True
else:
self.path_completed.notify()
return moved
def generate_path(self, destination: Coordinate) -> Optional[List[Directions]]:
result = None
map_ = []
for y in range(self.field.width):
map_.append([])
for x in range(self.field.height):
cell = self.field.at(x, y)
map_[y].append(0 if cell.passable else -1)
# начальная точка маршрута
map_[self.y][self.x] = 1
if self._find_path(map_, destination):
result = self._generate_path(map_, destination)
self.route_calculated.notify(self.position, destination, result)
return result
@staticmethod
def _find_path(map_: List[List[int]], destination: Coordinate) -> bool:
weight = 1
for i in range(len(map_) * len(map_[0])):
weight += 1
for y in range(len(map_)):
for x in range(len(map_[y])):
if map_[y][x] == (weight - 1):
if y > 0 and map_[y - 1][x] == 0:
map_[y - 1][x] = weight
if y < (len(map_) - 1) and map_[y + 1][x] == 0:
map_[y + 1][x] = weight
if x > 0 and map_[y][x - 1] == 0:
map_[y][x - 1] = weight
if x < (len(map_[y]) - 1) and map_[y][x + 1] == 0:
map_[y][x + 1] = weight
if (abs(y - destination.y) + abs(x - destination.x)) == 1:
map_[destination.y][destination.x] = weight
return True
return False
@staticmethod
def _generate_path(map_: List[List], destination: Coordinate) -> List[Directions]:
y = destination.y
x = destination.x
weight = map_[y][x]
result = list(range(weight))
while weight:
weight -= 1
if y > 0 and map_[y - 1][x] == weight:
result[weight] = Directions.south
y -= 1
elif y < (len(map_) - 1) and map_[y + 1][x] == weight:
result[weight] = Directions.north
y += 1
elif x > 0 and map_[y][x - 1] == weight:
result[weight] = Directions.east
x -= 1
elif x < (len(map_[y]) - 1) and map_[y][x + 1] == weight:
result[weight] = Directions.west
x += 1
return result[1:]
class Structure:
def __init__(self, name: str, place: List[Coordinate],
passable: bool, direction: Directions = Directions.east):
self._start_pos: Optional[Coordinate] = None
self._direction = direction
self._passable = passable
self._is_placed = False
self._place = place
self._name = name
@property
def name(self) -> str:
return self._name
@property
def direction(self) -> Directions:
return self._direction
@property
def passable(self) -> bool:
return self._passable
def is_placed(self):
return self._is_placed
def place(self, field: Field, position: Coordinate, direction: Directions) -> bool:
if self._is_placed:
raise ValueError
result = False
for point in self._place:
cell = field.at_point(point + position)
if not cell.can_build:
break
else:
for point in self._place:
cell = field.at_point(point + position)
cell.struct_container.put(self)
self._is_placed = True
self._direction = direction
self._start_pos = position
result = True
return result
def destroy(self, field: Field) -> Optional[Item]:
for point in self._place:
cell = field.at_point(self._start_pos + point)
cell.struct_container.remove()
return None
class Item:
def __init__(self, name: str, quantity: int = 1):
self._quantity = quantity
self._name = name
@property
def name(self) -> str:
return self._name
@property
def quantity(self) -> int:
return self._quantity
class Cell(HavingPosition):
def __init__(self, surface: surfaces.Surface, position: Coordinate):
super().__init__()
self.struct_container: Container[Structure] = Container()
self.unit_container: Container[Unit] = Container()
self.item_container: Container[Item] = Container()
self._position = position
self._surface = surface
@property
def surface(self):
return self._surface
@property
def x(self) -> int:
return self._position.x
@property
def y(self) -> int:
return self._position.y
@property
def position(self) -> Coordinate:
return self._position
@property
def passable(self) -> bool:
return (self.surface.passable
and self.unit_container.is_empty()
and (self.struct_container.is_empty() or not self.struct_container.item.passable))
@property
def can_build(self) -> bool:
return (self.surface.type == surfaces.Type.hard
and self.struct_container.is_empty()
and self.item_container.is_empty()
and self.unit_container.is_empty())
def __repr__(self):
return f'Cell({self.surface.resource})'
class Field:
def __init__(self, width: int, height: int):
self._matrix = self._create_empty_matrix(width, height)
self._width = width
self._height = height
@property
def width(self) -> int:
return self._width
@property
def height(self) -> int:
return self._height
def at(self, x: int, y: int) -> Optional[Cell]:
if (0 <= x < self._width) and (0 <= y < self._height):
return self._matrix[y][x]
return None
def at_point(self, point: Coordinate) -> Cell:
return self.at(point.x, point.y)
def load(self, stream: io.TextIO):
lines = [l for l in stream]
self._height = len(lines)
self._width = len(lines[0].split(config.TAB_LITERAL))
self._matrix = self._create_empty_matrix(self._width, self._height)
for y, line in enumerate(lines):
for x, cell_name in enumerate(line.split(config.TAB_LITERAL)):
name, id_ = cell_name.strip().split(config.SURFACE_NAME_DELIMITER)
surface = surfaces.BY_RESOURCE_NAME[name]
surface.id = int(id_)
self._matrix[y][x] = Cell(surface, Coordinate(x, y))
def dump(self, stream: io.TextIOBase):
for y in range(self.height):
stream.write(config.TAB_LITERAL.join(
config.SURFACE_NAME_TEMPLATE.format(i.surface.name, i.surface.id) for i in self._matrix[y]))
stream.write(config.NL_LITERAL)
@staticmethod
def _create_empty_matrix(width: int, height: int) -> List[List]:
return [[Cell(surfaces.empty, Coordinate(i, j)) for i in range(width)] for j in range(height)]