-
Notifications
You must be signed in to change notification settings - Fork 1
/
Model.py
424 lines (359 loc) · 10.2 KB
/
Model.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
from enum import Enum
from typing import *
import random
from datetime import datetime
class Ant:
antType: int
antTeam: int
currentResource: "Resource"
currentX: int
currentY: int
health: int
visibleMap: "Map"
attackDistance: int
viewDistance: int
# near by attacks
attacks: List["Attack"]
def __init__(
self,
ant_type: int,
ant_team: int,
currentResource: "Resource",
currentX: int,
currentY: int,
health: int,
visibleMap: "Map",
attackDistance: int,
viewDistance: int,
attacks: List["Attack"],
):
self.antType = ant_type
self.antTeam = ant_team
self.currentX = currentX
self.currentY = currentY
self.currentResource = currentResource
self.visibleMap = visibleMap
self.health = health
self.attackDistance = attackDistance
self.viewDistance = viewDistance
self.attacks = attacks
@classmethod
def createAntXY(cls, ant_type: int, ant_team: int, currentX: int, currentY: int):
return cls(ant_type, ant_team, None, currentX, currentY, -1, None, -1, -1, [])
@classmethod
def createCurrentAnt(
cls,
ant_type: int,
ant_team: int,
currentState: "CurrentState",
visibleMap: "Map",
attackDistance: int,
viewDistance: int,
):
return cls(
ant_type,
ant_team,
Resource(
currentState.current_resource_type, currentState.current_resource_value
),
currentState.current_x,
currentState.current_y,
currentState.health,
visibleMap,
attackDistance,
viewDistance,
currentState.attacks,
)
def getMapRelativeCell(self, x, y):
return self.visibleMap.getRelativeCell(x, y)
def getNeightbourCell(self, x, y):
return self.getMapRelativeCell(x, y)
def getLocationCell(self):
return self.getNeightbourCell(0, 0)
class Map:
cells: List[List["Cell"]]
width: int
height: int
antCurrentX: int
antCurrentY: int
def __init__(
self,
cells: List["Cell"],
width: int,
height: int,
currentX: int,
currentY: int,
):
super().__init__()
self.cells = cells
self.width = width
self.height = height
self.antCurrentX = currentX
self.antCurrentY = currentY
def getRelativeCell(self, dx: int, dy: int):
x = (self.antCurrentX + dx) % self.width
y = (self.antCurrentY + dy) % self.height
if x < 0:
x += self.width
if y < 0:
y += self.height
return self.cells[x][y]
class Cell:
x = 0
y = 0
type = 0
resource_value = 0
resource_type = 0
ants = []
def __init__(self, x, y, type, resource_value, resource_type):
self.x = x
self.y = y
self.type = type
self.ants = []
self.resource_value = resource_value
self.resource_type = resource_type
class Resource:
type: int
value: int
def __init__(self, type: int, value: int):
super().__init__()
self.value = value
self.type = type
class Message:
def __init__(self, text: str, turn: int):
self.text = text
self.turn = turn
class GameConfig:
map_width: int = 0
map_height: int = 0
ant_type: int = 0
base_x: int = -1
base_y: int = -1
health_kargar: int = 0
health_sarbaaz: int = 0
attack_distance: int = 0
view_distance: int = 0
generate_kargar: int = 0
generate_sarbaz: int = 0
generate_sarbaaz: int = 0
rate_death_resource: int = 0
def __init__(self, message):
self.__dict__ = message
class CurrentState:
around_cells: List["Cell"] = []
chat_box: List["Message"] = []
current_x: int = -1
current_y: int = -1
current_resource_type: int = None
current_resource_value: int = 0
health: int = 0
attacks: List["Attack"] = []
def __init__(self, message):
self.__dict__ = message
cells = []
for cel in self.around_cells:
cell_tmp = Cell(
cel["cell_x"],
cel["cell_y"],
cel["cell_type"],
cel["resource_value"],
cel["resource_type"],
)
cell_tmp.ants = cel["ants"]
cells.append(cell_tmp)
self.around_cells = cells
attacks = []
for attack in self.attacks:
attacks.append(Attack(attack))
self.attacks = attacks
def getVisibleCells(self, height, width):
cells = [[None for i in range(height)] for j in range(width)]
if self.around_cells == None:
return cells
for clientCell in self.around_cells:
cell = Cell(
clientCell.x,
clientCell.y,
clientCell.type,
clientCell.resource_value,
clientCell.resource_type,
)
for clientAnt in clientCell.ants:
cell.ants.append(
Ant.createAntXY(
clientAnt["ant_type"],
clientAnt["ant_team"],
clientCell.x,
clientCell.y,
)
)
cells[cell.x][cell.y] = cell
return cells
class Attack:
attacker_row: int
attacker_col: int
defender_row: int
defender_col: int
is_attacker_enemy: bool
def __init__(self, message):
self.__dict__ = message
class Game:
ant: "Ant"
chatBox: "ChatBox"
antType: int
mapWidth: int
mapHeight: int
baseX: int
baseY: int
healthKargar: int
healthSarbaaz: int
attackDistance: int
viewDistance: int
generateKargar: int
rateDeathResource: int
generateSarbaaz: int
def __init__(self):
super().__init__()
self.ant = None
self.chatBox = None
self.mapWidth = None
self.mapHeight = None
self.baseX = None
self.baseY = None
self.healthKargar = None
self.healthSarbaaz = None
self.attackDistance = None
self.viewDistance = None
self.generateKargar = None
self.generateSarbaaz = None
self.rateDeathResource = None
def initGameConfig(self, gameConfig: "GameConfig"):
self.antType = gameConfig.ant_type
self.mapWidth = gameConfig.map_width
self.mapHeight = gameConfig.map_height
self.baseX = gameConfig.base_x
self.baseY = gameConfig.base_y
self.healthKargar = gameConfig.health_kargar
self.healthSarbaaz = gameConfig.health_sarbaaz
self.attackDistance = gameConfig.attack_distance
self.viewDistance = gameConfig.view_distance
self.generateKargar = gameConfig.generate_kargar
self.generateSarbaaz = gameConfig.generate_sarbaaz
self.rateDeathResource = gameConfig.rate_death_resource
def setCurrentState(self, currentState: "CurrentState"):
self.chatBox = ChatBox(currentState.chat_box)
self.ant = self.initialAntState(currentState)
def initialAntState(self, currentState: "CurrentState"):
cells = currentState.getVisibleCells(self.mapHeight, self.mapWidth)
my_map = Map(
cells,
self.mapWidth,
self.mapHeight,
currentState.current_x,
currentState.current_y,
)
return Ant.createCurrentAnt(
self.antType,
AntTeam.ALLIED.value,
currentState,
my_map,
self.attackDistance,
self.viewDistance,
)
class ChatBox:
allChats: List["Chat"]
def __init__(self, allChats):
super().__init__()
chats = []
for chat in allChats:
chats.append(Chat(chat["text"], chat["turn"]))
self.allChats = chats
class Chat:
text: str
turn: int
def __init__(self, text, turn):
super().__init__()
self.text = text
self.turn = turn
class AntType(Enum):
SARBAAZ = 0
KARGAR = 1
@staticmethod
def get_value(string: str):
if string == "SARBAAZ":
return AntType.SARBAAZ
if string == "KARGAR":
return AntType.KARGAR
return None
class Direction(Enum):
CENTER = 0
RIGHT = 1
UP = 2
LEFT = 3
DOWN = 4
@staticmethod
def get_value(string: str):
if string == "CENTER":
return Direction.CENTER.value
if string == "RIGHT":
return Direction.RIGHT.value
if string == "UP":
return Direction.UP.value
if string == "LEFT":
return Direction.LEFT.value
if string == "DOWN":
return Direction.DOWN.value
return None
@staticmethod
def get_random_direction():
random.seed(datetime.now())
return random.randint(1, 4)
@staticmethod
def get_string(dir_val: int):
if dir_val is None:
return "NONE"
return Direction(dir_val).name
class CellType(Enum):
BASE = 0
EMPTY = 1
WALL = 2
TRAP = 3
SWAMP = 4
@staticmethod
def get_value(string: str):
if string == "BASE":
return CellType.BASE.value
if string == "EMPTY":
return CellType.EMPTY.value
if string == "WALL":
return CellType.WALL.value
return None
class ResourceType(Enum):
BREAD = 0
GRASS = 1
@staticmethod
def get_value(string: str):
if string == "BREAD":
return ResourceType.BREAD.value
if string == "GRASS":
return ResourceType.GRASS.value
return None
class ServerConstants:
KEY_INFO = "info"
KEY_TURN = "turn"
KEY_TYPE = "type"
# CONFIG_KEY_IP = "ip"
# CONFIG_KEY_PORT = "port"
CONFIG_KEY_TOKEN = "token"
MESSAGE_TYPE_INIT = "3"
MESSAGE_TYPE_TURN = "4"
MESSAGE_TYPE_KILL = "7"
MESSAGE_TYPE_DUMMY = "8"
class AntTeam(Enum):
ENEMY = 1
ALLIED = 0
class ServerMessage:
def __init__(self, type, turn, info):
self.type = type
self.info = info
self.turn = turn