-
Notifications
You must be signed in to change notification settings - Fork 0
/
world.py
567 lines (461 loc) · 21.2 KB
/
world.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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
from enum import Enum
from typing import Final, List, Tuple, Dict
from random import sample
from rich.text import Text as ColoredText
from rich.console import Console
from time import sleep
import os
import sys
"""
WORLD VERSION: 1.1.2
"""
class AppState(Enum):
WAITING_FOR_CONNECTION_B1 = object()
WAITING_FOR_CONNECTION_B2 = object()
WRITING_LAST_STATE = object()
WAITING_CMD_B1 = object()
WAITING_CMD_B2 = object()
class MatchResult(Enum):
DRAW = 0
WIN_PLAYER1 = 1
WIN_PLAYER2 = 2
class Vec:
def __init__(self, x: int | List[int] | Tuple[int, int], y: int = -1):
if type(x) in [list, tuple]:
self.x: int = x[0]
self.y: int = x[1]
else:
self.x: int = x
self.y: int = y
def __add__(self, cell2: 'Vec'):
return Vec(self.x + cell2.x, self.y + cell2.y)
def __sub__(self, cell2: 'Vec'):
return Vec(self.x - cell2.x, self.y - cell2.y)
def __str__(self):
return f"{self.x} {self.y}"
class Resource:
def __init__(self, position: Vec) -> None:
self.health: int = 3
self.position: Vec = position
self.hit_by: List[Player] = []
class Player:
def __init__(self, spawn_pos) -> None:
self.resources: int = 0
self.troops: List[Troop] = []
self.spawn_pos: Vec = spawn_pos
class Troop:
def __init__(self, troop_ID, owner: Player, position: Vec) -> None:
self.id: int = troop_ID
self.owner: Player = owner
self.position: Vec = position
self.health: int = 2
self.move_speed: int = 1
self.damage: int = 1
self.has_hit: bool = False
# add the troop to the player's troop list
owner.troops.append(self)
class Map:
def __init__(self, width: int, height: int):
self.width: int = width
self.height: int = height
self.map: List[List[None | Troop | Resource]] = [[None for _ in range(self.width)] for _ in range(self.height)]
def get_cell(self, pos: Vec) -> None | Troop | Resource:
true_cell = (self.height - pos.y - 1, pos.x)
return self.map[true_cell[0]][true_cell[1]]
def set_cell(self, pos: Vec, obj: None | Resource | Troop):
true_cell = (self.height - pos.y - 1, pos.x)
self.map[true_cell[0]][true_cell[1]] = obj
def print_formatted(self, player1, player2):
texts = ColoredText()
for row in self.map:
for cell in row:
if type(cell) is Resource:
texts.append("o ", style="green")
if type(cell) is Troop and cell.owner is player1:
texts.append("X ", style="blue")
if type(cell) is Troop and cell.owner is player2:
texts.append("X ", style="red")
if cell is None:
texts.append("- ", style="white")
texts.append("\n")
console.print(texts)
def copy(self):
m = Map(self.width, self.height)
m.map = [[cell for cell in row] for row in self.map]
return m
def is_outside(self, pos: Vec) -> bool:
return pos.x < 0 or pos.x >= self.width or pos.y < 0 or pos.y >= self.height
def debug_log(text, map_to_show: Map, player1, player2):
if not debug:
return
# clear terminal and write map
os.system('cls' if os.name == 'nt' else 'clear')
map_to_show.print_formatted(player1, player2)
console.print(text)
sleep(sleep_time)
def movement_str_to_vec(dir_):
match dir_:
case "up":
return 0, 1
case "down":
return 0, -1
case "left":
return -1, 0
case "right":
return 1, 0
case _:
return 0, 0
def get_troop_from_id(troop_ID, player_) -> Troop | None:
for troop in player_.troops:
if troop.id == troop_ID:
return troop
return None
def compute_movement(cmds_move, next_frame, actions_logging):
# remove invalid movements
for i in range(len(cmds_move))[::-1]:
troop: Troop
movement: Vec
amount: int
troop, movement, amount = cmds_move[i]
if troop is None: # I'm trying to move a troop I don't have control of (it doesn't exist, or it's the enemy's troop)
cmds_move.pop(i)
continue
# remove duped move commands
valid_cmds: List[Tuple[Troop | None, Vec, int]] = []
ids_found: List[int] = []
for troop, vec, amount in cmds_move:
if troop.id not in ids_found:
ids_found.append(troop.id)
valid_cmds.append((troop, vec, amount))
cmds_move = valid_cmds
moved_troops: Dict[Troop, Vec] = {}
while len(cmds_move) > 0:
for i in range(len(cmds_move))[::-1]: # NOTE: reverse order, so we can edit the list while working on it
troop: Troop
movement: Vec
amount: int
troop, movement, amount = cmds_move[i]
# clamp in [0, move_speed]
amount = max(0, min(amount, troop.move_speed))
if amount <= 0:
cmds_move.pop(i)
continue
# work here
target_pos: Vec = troop.position + movement
# trying to get off map, stop and finish command
if next_frame.is_outside(target_pos):
cmds_move.pop(i)
continue
target_cell = next_frame.get_cell(target_pos)
# getting into a position filled by a resource = stop movement
if type(target_cell) == Resource:
cmds_move.pop(i)
continue
# trying to move in an occupied position
if type(target_cell) == Troop:
cmds_move.pop(i)
continue
# move by 1 cell
next_frame.set_cell(troop.position, None)
next_frame.set_cell(target_pos, troop)
troop.position = target_pos
cmds_move[i] = (troop, movement, amount - 1)
# add moved amount to temp list, used for logging
if troop not in moved_troops:
moved_troops[troop] = troop.position - movement
# logging
for troop, start_position in moved_troops.items():
for k in actions_logging:
if k == troop.owner:
actions_logging[k].append(f"action troop ally {troop.id} {start_position} move {troop.position}")
else:
actions_logging[k].append(f"action troop enemy {troop.id} {start_position} move {troop.position}")
del moved_troops
def compute_actions(cmds_action, next_frame, actions_logging):
resources_hit: List[Resource] = []
for troop, movement in cmds_action:
if troop is None:
continue
target_pos = troop.position + movement
target_cell = next_frame.get_cell(target_pos)
if not troop.has_hit:
troop.has_hit = True
if type(target_cell) is Troop:
target_cell.health -= troop.damage
# destroy troop
if target_cell.health <= 0:
next_frame.set_cell(target_pos, None)
target_cell.owner.troops.remove(target_cell)
for k in actions_logging:
if k == troop.owner:
actions_logging[k].append(f"action troop ally {troop.id} {troop.position} attack {target_pos}")
else:
actions_logging[k].append(f"action troop enemy {troop.id} {troop.position} attack {target_pos}")
elif type(target_cell) is Resource:
target_cell.health -= troop.damage
resources_hit.append(target_cell)
# whoever destroys the resource cell, get the resources (health, for now)
target_cell.hit_by.append(troop.owner)
for k in actions_logging:
if k == troop.owner:
actions_logging[k].append(f"action troop ally {troop.id} {troop.position} harvest {target_pos}")
else:
actions_logging[k].append(f"action troop enemy {troop.id} {troop.position} harvest {target_pos}")
# resets hit action of troops
for troop, movement in cmds_action:
troop.has_hit = False
for resource in resources_hit:
if resource.health <= 0:
destroyed_by = list(set(resource.hit_by))
next_frame.set_cell(resource.position, None)
for owner in destroyed_by:
owner.resources += round(gain_per_resource / len(destroyed_by), 2)
else:
resource.hit_by.clear()
# count resources in map
resources_count = 0
for row in next_frame.map:
for cell in row:
if type(cell) == Resource:
resources_count += 1
if resources_count < max_resources:
empty_spots: List[Vec] = []
for x in range(map_width):
for y in range(map_height):
pos: Vec = Vec(x, y)
if next_frame.get_cell(pos) is None:
empty_spots.append(pos)
chosen_spots: List[Vec] = sample(empty_spots, max_resources - resources_count)
for pos in chosen_spots:
next_frame.set_cell(pos, Resource(pos))
def compute_powerup(cmds_powerup, actions_logging):
for troop, powerID in cmds_powerup:
if troop is None:
continue
if troop.owner.resources < troop_powerup_cost: # not enough to buy an upgrade
continue
match powerID:
case "health":
troop.health += 1
case "speed":
troop.move_speed += 1
case "damage":
troop.damage += 1
troop.owner.resources -= troop_powerup_cost
for k in actions_logging:
if k == troop.owner:
actions_logging[k].append(f"action troop ally {troop.id} {troop.position} powerup {powerID}")
else:
actions_logging[k].append(f"action troop enemy {troop.id} {troop.position} powerup {powerID}")
def compute_create(cmds_create, next_frame, troop_id, actions_logging):
for player in cmds_create:
# check if the player has enough resources and the cell is not blocked
if player.resources >= troop_creation_cost and next_frame.get_cell(player.spawn_pos) is None and len(player.troops) < max_troops:
troop_id += 1
next_frame.set_cell(player.spawn_pos, Troop(troop_id, player, player.spawn_pos))
player.resources -= troop_creation_cost
for k in actions_logging:
if k == player:
actions_logging[k].append(f"action troop ally {troop_id} spawn")
else:
actions_logging[k].append(f"action troop enemy {troop_id} spawn")
return troop_id
def play_game():
print("match started...")
current_state = AppState.WAITING_FOR_CONNECTION_B1
world_map: Map = Map(map_width, map_height)
is_match_ended = False
match_result = None
player1: Player = Player(b1_spawn)
player2: Player = Player(b2_spawn)
b1_cmds: List[str] = []
b2_cmds: List[str] = []
turn = 0
troop_id = -1
# clean channels (or create them if they don't exist)
with open(channel1, "w") as f:
f.write("spawn_pos 2 2\nmatch_start")
with open(channel2, "w") as f:
f.write("spawn_pos 8 8\nmatch_start")
while current_state == AppState.WAITING_FOR_CONNECTION_B1:
with open(channel1, "r") as f:
data = f.read()
if "ready" in data:
debug_log("b1 is ready", world_map, player1, player2)
# gen the player and the first troop
troop_id += 1
world_map.set_cell(b1_spawn, Troop(troop_id, player1, b1_spawn))
current_state = AppState.WAITING_FOR_CONNECTION_B2
else:
debug_log("waiting for b1", world_map, player1, player2)
while current_state == AppState.WAITING_FOR_CONNECTION_B2:
with open(channel2, "r") as f:
data = f.read()
if "ready" in data:
debug_log("b2 is ready", world_map, player1, player2)
# gen the player and the first troop
troop_id += 1
world_map.set_cell(b2_spawn, Troop(troop_id, player2, b2_spawn))
current_state = AppState.WRITING_LAST_STATE
else:
debug_log("waiting for b2", world_map, player1, player2)
while turn < max_turns and not is_match_ended:
match current_state:
case AppState.WRITING_LAST_STATE:
# work on every command
all_cmds: List[Tuple[str, Player]] = [(x, player2) for x in b2_cmds] + [(x, player1) for x in b1_cmds]
actions_logging: Dict[Player, List[str]] = {player1: [], player2: []}
cmds_move: List[Tuple[Troop | None, Vec, int]] = []
cmds_action: List[Tuple[Troop | None, Vec]] = []
cmds_powerup: List[Tuple[Troop | None, int]] = []
cmds_create: List[Player] = []
# filter only useful commands
for cmd, player in all_cmds:
match cmd.split(" "):
case ["move", troopID, ("up" | "down" | "left" | "right") as direction, amount] if troopID.isnumeric() and amount.isnumeric():
cmds_move.append((get_troop_from_id(int(troopID), player), Vec(movement_str_to_vec(direction)), int(amount)))
case ["action", troopID, ("up" | "down" | "left" | "right") as direction] if troopID.isnumeric():
cmds_action.append((get_troop_from_id(int(troopID), player), Vec(movement_str_to_vec(direction))))
case ["powerup", troopID, ("health" | "speed" | "damage") as powerID] if troopID.isnumeric():
cmds_powerup.append((get_troop_from_id(int(troopID), player), powerID))
case ["create"] if player.resources >= troop_creation_cost:
cmds_create.append(player)
# copy to have edits
next_frame: Map = world_map.copy()
# compute all the troops actions in order
compute_movement(cmds_move, next_frame, actions_logging)
compute_actions(cmds_action, next_frame, actions_logging)
compute_powerup(cmds_powerup, actions_logging)
troop_id = compute_create(cmds_create, next_frame, troop_id, actions_logging)
# paste edits into map
world_map = next_frame.copy()
debug_log("COMMANDS COMPLETED: NEW FRAME", world_map, player1, player2)
# collect resources info
resources_txt = ""
for row in world_map.map:
for cell in row:
if type(cell) == Resource:
resource: Resource = cell
resources_txt += f"resource {resource.position.x} {resource.position.y} {resource.health} {gain_per_resource}\n"
# send messages to troops
with open(channel1, "w") as f:
troops_actions = ""
for troop in player1.troops:
troops_actions += f"troop ally {troop.id} {troop.position} {troop.health} {troop.move_speed} {troop.damage}\n"
for troop in player2.troops:
troops_actions += f"troop enemy {troop.id} {troop.position} {troop.health} {troop.move_speed} {troop.damage}\n"
event_actions = '\n'.join(actions_logging[player1])
f.write(f"turn {turn} {round(float(player1.resources), 2)}\n{troops_actions}{resources_txt}{event_actions}\nworld_done")
with open(channel2, "w") as f:
troops_actions = ""
for troop in player2.troops:
troops_actions += f"troop ally {troop.id} {troop.position} {troop.health} {troop.move_speed} {troop.damage}\n"
for troop in player1.troops:
troops_actions += f"troop enemy {troop.id} {troop.position} {troop.health} {troop.move_speed} {troop.damage}\n"
event_actions = '\n'.join(actions_logging[player2])
f.write(f"turn {turn} {round(float(player2.resources), 2)}\n{troops_actions}{resources_txt}{event_actions}\nworld_done")
if len(player1.troops) == 0 and len(player2.troops) == 0:
print(f"Ended at turn {turn}!")
print("Draw!")
is_match_ended = True
match_result = MatchResult.DRAW
elif len(player2.troops) == 0:
print(f"Ended at turn {turn}!")
print("Player 1 Win!")
is_match_ended = True
match_result = MatchResult.WIN_PLAYER1
elif len(player1.troops) == 0:
print(f"Ended at turn {turn}!")
print("Player 2 Win!")
is_match_ended = True
match_result = MatchResult.WIN_PLAYER2
# visualization
b1_cmds.clear() # clear for next round
b2_cmds.clear() # clear for next round
turn += 1
current_state = AppState.WAITING_CMD_B1
if not debug:
print(f"TURN: {str(turn).rjust(4, ' ')}/1000")
case AppState.WAITING_CMD_B1:
with open(channel1, "r") as f:
data = f.read()
if "end_turn" in data:
for cmd in data.split("\n"):
if cmd == "end_turn":
break
b1_cmds.append(cmd)
debug_log(f"b1 commands list: {b1_cmds}", world_map, player1, player2)
current_state = AppState.WAITING_CMD_B2
else:
debug_log("waiting for b1 cmd", world_map, player1, player2)
case AppState.WAITING_CMD_B2:
with open(channel2, "r") as f:
data = f.read()
if "end_turn" in data:
for cmd in data.split("\n"):
if cmd == "end_turn":
break
b2_cmds.append(cmd)
debug_log(f"b2 commands list: {b2_cmds}", world_map, player1, player2)
current_state = AppState.WRITING_LAST_STATE
else:
debug_log("waiting for b2 cmd", world_map, player1, player2)
# cases when reaching turn 1000
if not is_match_ended:
print(f"Ended at turn {turn}!")
if player1.resources > player2.resources:
print("Player 1 Win!")
match_result = MatchResult.WIN_PLAYER1
elif player2.resources > player1.resources:
print("Player 2 Win!")
match_result = MatchResult.WIN_PLAYER2
else:
print("Draw!")
match_result = MatchResult.DRAW
print(f"Player 1 Resources: {player1.resources}")
print(f"Player 2 Resources: {player2.resources}")
with open(channel1, "w") as f:
f.write("end_match")
with open(channel2, "w") as f:
f.write("end_match")
with open("result.txt", "w") as f:
f.write(f"game_ended\n{match_result.name}")
if __name__ == '__main__':
# CONFIGS
console: Final[Console] = Console()
b1_spawn: Final[Vec] = Vec(2, 2)
b2_spawn: Final[Vec] = Vec(8, 8)
map_width: Final[int] = 11
map_height: Final[int] = 11
max_troops: Final[int] = 10
troop_creation_cost: Final[int] = 5
troop_powerup_cost: Final[int] = 3
max_resources: Final[int] = 10
gain_per_resource: Final[int] = 6
max_turns: Final[int] = 1000
channel1: Final[str] = "channel1.txt"
channel2: Final[str] = "channel2.txt"
# debug stuff
sleep_time: float
debug: bool
def is_float(val) -> bool:
try:
float(val)
return True
except ValueError:
return False
# get bots names from command line arguments
match sys.argv:
case [_, "True", debug_speed] if is_float(debug_speed):
print(f"received debug command: True, {debug_speed}")
debug = True
sleep_time = float(debug_speed)
case [_, "False", debug_speed] if is_float(debug_speed):
print(f"received debug command: False, {debug_speed}")
debug = False
sleep_time = float(debug_speed)
case _:
print(f"no command received (or received incorrectly), playing in debug mode")
debug = True
sleep_time = 0.3
play_game()