-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.py
492 lines (359 loc) · 11.2 KB
/
sample.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
"""
Файл-шаблон для всех последующих скриптов
Прошу, не добавляйте сюда никакой логики поведения, иначе я обижусь
"""
import json
from dataclasses import dataclass
from enum import Enum
from typing import List
# region Primitives
@dataclass
class Vector:
x: int
y: int
z: int
def __add__(self, other):
if not isinstance(other, Vector):
raise TypeError()
return Vector(self.x + other.x,
self.y + other.y,
self.z + other.z)
def __sub__(self, other):
if not isinstance(other, Vector):
raise TypeError()
return Vector(self.x - other.x,
self.y - other.y,
self.z - other.z)
def __mul__(self, other):
if not isinstance(other, int):
raise TypeError()
return Vector(self.x * other,
self.y * other,
self.z * other)
def __str__(self):
return f'{self.x}/{self.y}/{self.z}'
def __eq__(self, other):
return self.coords == other.coords
def __ne__(self, other):
return self.coords != other.coords
def __gt__(self, other):
return self.coords > other.coords
def __lt__(self, other):
return self.coords < other.coords
def __ge__(self, other):
return self.coords >= other.coords
def __le__(self, other):
return self.coords <= other.coords
@property
def coords(self):
return self.x, self.y, self.z
@classmethod
def from_json(cls, data: str):
x, y, z = map(int, data.split('/'))
return cls(x, y, z)
# endregion
# region Utils
class JSONCapability:
def to_json(self) -> dict:
return {k: str(v) if isinstance(v, Vector) else v
for k, v in self.__dict__.items() if v is not None}
class Physics:
@staticmethod
def clen(v: Vector) -> int:
"""Метрика Чебышёва"""
return max(map(abs, v.coords))
@staticmethod
def mlen(v: Vector) -> int:
"""Манхэттенская метрика"""
return sum(map(abs, v.coords))
@staticmethod
def get_len_vector(vector_diff: Vector) -> int:
"""Метод для нахождения длины разности векторов"""
return sum(value ** 2 for value in vector_diff.coords) ** 0.5
@staticmethod
def bresenham_ray(point1: Vector, point2: Vector, length: int = None) -> List[Vector]:
"""Метод для построение вектора по алгоритмы Брезенхама (https://clck.ru/Vbigh)"""
x1, y1, z1 = point1.coords
x2, y2, z2 = point2.coords
points = [(x1, y1, z1)]
x_shift = abs(x2 - x1)
y_shift = abs(y2 - y1)
z_shift = abs(z2 - z1)
x_step = int(x2 > x1)
y_step = int(y2 > y1)
z_step = int(z2 > z1)
# изменения поведения в зависимости от ведущей оси
if x_shift >= y_shift and x_shift >= z_shift:
# p1, p2 - смещение относительно ведущей оси, не стал изменять названия переменных
p1 = 2 * y_shift - x_shift
p2 = 2 * z_shift - x_shift
while x1 != x2:
x1 += x_step
if p1 >= 0:
y1 += y_step
p1 -= 2 * x_shift
if p2 >= 0:
z1 += z_step
p2 -= 2 * x_shift
p1 += 2 * y_shift
p2 += 2 * z_shift
points.append(Vector(x1, y1, z1))
elif y_shift >= x_shift and y_shift >= z_shift:
p1 = 2 * x_shift - y_shift
p2 = 2 * z_shift - y_shift
while y1 != y2:
y1 += y_step
if p1 >= 0:
x1 += x_step
p1 -= 2 * y_shift
if p2 >= 0:
z1 += z_step
p2 -= 2 * y_shift
p1 += 2 * x_shift
p2 += 2 * z_shift
points.append(Vector(x1, y1, z1))
else:
p1 = 2 * y_shift - z_shift
p2 = 2 * x_shift - z_shift
while z1 != z2:
z1 += z_step
if p1 >= 0:
y1 += y_step
p1 -= 2 * z_shift
if p2 >= 0:
x1 += x_step
p2 -= 2 * z_shift
p1 += 2 * y_shift
p2 += 2 * x_shift
points.append(Vector(x1, y1, z1))
return points[:length or 999] # не самый лучший вариант, зато в коде места не занимает
# endregion
# region Equipment
class BlockType(Enum):
Energy = 0
Gun = 1
Engine = 2
Health = 3
Shield = 4
Radar = 6
Heal = 7
class EffectType(Enum):
Blaster = 0
Railgun = 1
@dataclass
class Block(JSONCapability):
Name: str
Type: BlockType
@classmethod
def from_json(cls, data):
if BlockType(data['Type']) == BlockType.Energy:
return EnergyBlock(**data)
elif BlockType(data['Type']) == BlockType.Gun:
return GunBlock(**data)
elif BlockType(data['Type']) == BlockType.Engine:
return EngineBlock(**data)
elif BlockType(data['Type']) == BlockType.Health:
return HealthBlock(**data)
elif BlockType(data['Type']) == BlockType.Shield:
return ShieldBlock(**data)
elif BlockType(data['Type']) == BlockType.Radar:
return RadarBlock(**data)
elif BlockType(data['Type']) == BlockType.Heal:
return HealBlock(**data)
@dataclass
class EnergyBlock(Block):
Type = BlockType.Energy
IncrementPerTurn: int
MaxEnergy: int
StartEnergy: int
@dataclass
class GunBlock(Block):
Type = BlockType.Gun
Damage: int
EnergyPrice: int
Radius: int
EffectType: int
@dataclass
class EngineBlock(Block):
Type = BlockType.Engine
MaxAccelerate: int
@dataclass
class HealthBlock(Block):
Type = BlockType.Health
MaxHealth: int
StartHealth: int
@dataclass
class ShieldBlock(Block):
Type = BlockType.Shield
EnergyPrice: int
Armor: int
@dataclass
class RadarBlock(Block):
EnergyPrice: int
Radius: int
@dataclass
class HealBlock(Block):
Type = BlockType.Heal
EnergyPrice: int
Radius: int
HealthGain: int
EnergyGain: int
# endregion
# region Draft Input
@dataclass
class DraftCompleteShip(JSONCapability):
Id: str
Price: int
Equipment: List[str]
@classmethod
def from_json(cls, data):
return cls(**data)
@dataclass
class DraftEquipment(JSONCapability):
Size: int
Equipment: List[Block]
@classmethod
def from_json(cls, data):
data['Equipment'] = Block.from_json(data['Equipment'])
return cls(**data)
@dataclass
class MapRegion(JSONCapability):
From: Vector
To: Vector
@classmethod
def from_json(cls, data):
data['From'] = Vector.from_json(data['From'])
data['To'] = Vector.from_json(data['To'])
return cls(**data)
@dataclass
class DraftOptions(JSONCapability):
PlayerId: int
MapSize: int
Money: int
MaxShipsCount: int
StartArea: MapRegion
Equipment: List[DraftEquipment]
CompleteShips: List[DraftCompleteShip]
DraftTimeout: int = None
BattleRoundTimeout: int = None
@classmethod
def from_json(cls, data):
data['StartArea'] = MapRegion.from_json(data['StartArea'])
data['Equipment'] = list(map(DraftEquipment.from_json, data['Equipment']))
data['CompleteShips'] = list(map(DraftCompleteShip.from_json, data['CompleteShips']))
return cls(**data)
# endregion
# region Draft Output
@dataclass
class DraftShipChoice:
CompleteShipId: str
Position: Vector = None
@dataclass
class DraftChoice:
Ships: List[DraftShipChoice] = None
Message: str = None
# endregion
# region Battle Input
@dataclass
class Ship(JSONCapability):
Id: int
Position: Vector
Velocity: Vector
Health: int = None
Energy: int = None
Equipment: List[Block] = None
@classmethod
def from_json(cls, data):
if data.get('Equipment'): # не доступно для оппонента
data['Equipment'] = list(map(Block.from_json, data.get('Equipment', [])))
data['Position'] = Vector.from_json(data['Position'])
data['Velocity'] = Vector.from_json(data['Velocity'])
return cls(**data)
@dataclass
class FireInfo(JSONCapability):
Source: Vector
Target: Vector
EffectType: EffectType
@classmethod
def from_json(cls, data):
data['Source'] = Vector.from_json(data['Source'])
data['Target'] = Vector.from_json(data['Target'])
return cls(**data)
@dataclass
class State(JSONCapability):
My: List[Ship]
Opponent: List[Ship]
FireInfos: List[FireInfo]
@classmethod
def from_json(cls, data):
data['My'] = list(map(Ship.from_json, data['My']))
data['Opponent'] = list(map(Ship.from_json, data['Opponent']))
data['FireInfos'] = list(map(FireInfo.from_json, data['FireInfos']))
return cls(**data)
# endregion
# region Battle Output
MOVE = 'MOVE'
ACCELERATE = 'ACCELERATE'
ATTACK = 'ATTACK'
DEFEND = 'DEFEND'
SCAN = 'SCAN'
@dataclass
class CommandParameters(JSONCapability):
pass
@dataclass
class MoveParameters(CommandParameters):
Id: int
Target: Vector
@dataclass
class AccelerateParameters(CommandParameters):
Id: int
Vector: Vector
@dataclass
class AttackParameters(CommandParameters):
Id: int
Name: str
Target: Vector
@dataclass
class DefendParameters(CommandParameters):
Id: int
Name: str
@dataclass
class ScanParameters(CommandParameters):
Id: int
Name: str
@dataclass
class Command(JSONCapability):
Command: str
Parameters: CommandParameters
@dataclass
class UserOutput(JSONCapability):
UserCommands: List[Command] = None
Message: str = None
# endregion
class Game:
def __init__(self):
self.draft_options = None
def draft(self, data: dict) -> DraftChoice:
self.draft_options = DraftOptions.from_json(data)
draft_choice = DraftChoice()
# тут должно быть поведение во время драфта
return draft_choice
def battle(self, data: dict) -> UserOutput:
state = State.from_json(data)
user_output = UserOutput()
# тут должно быть поведение во время боя
return user_output
def main(self):
while True:
line_in = input()
data = json.loads(line_in)
if 'PlayerId' in data:
result = self.draft(data)
else:
result = self.battle(data)
line_out = json.dumps(result,
default=JSONCapability.to_json,
ensure_ascii=False)
print(line_out)
if __name__ == '__main__':
Game().main()