forked from harrischristiansen/generals-bot
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathArmy.py
More file actions
147 lines (122 loc) · 4.6 KB
/
Army.py
File metadata and controls
147 lines (122 loc) · 4.6 KB
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
from __future__ import annotations
import typing
import logbook
import SearchUtils
from Path import Path
from base.client.tile import Tile
class Army(object):
start = 'A'
end = 'z'
curLetter = start
@staticmethod
def get_letter():
ch = Army.curLetter
if ord(ch) + 1 > ord(Army.end):
Army.curLetter = Army.start
else:
Army.curLetter = chr(ord(ch) + 1)
while Army.curLetter in ['[', '\\', ']', '^', '_', '`']:
Army.curLetter = chr(ord(Army.curLetter) + 1)
return ch
def __init__(self, tile: Tile, name: str | None = None):
self.tile: Tile = tile
self.path: Path = Path()
self.player: int = tile.player
self.visible: bool = tile.visible
self.value: int = 0
"""Always the value of the tile, minus one. For some reason."""
self.update_tile(tile)
self.expectedPaths: typing.List[Path] = []
self.entangledArmies: typing.List[Army] = []
self.name: str = name
if not name:
self.name = Army.get_letter()
self.entangledValue = None
self.scrapped = False
self.last_moved_turn: int = 0
self.last_seen_turn: int = 0
# # for debugging
# @property
# def last_seen_turn(self) -> int:
# return self._last_seen_turn
#
# @last_seen_turn.setter
# def last_seen_turn(self, value: int):
# self._last_seen_turn = value
def update_tile(self, tile):
if self.path.tail is None or self.path.tail.tile != tile:
self.path.add_next(tile)
if self.tile != tile:
self.tile = tile
self.update()
def update(self):
if self.tile.visible:
self.value = self.tile.army - 1
self.entangledValue = None
self.visible = self.tile.visible
def get_split_for_fog(self, fogTiles: typing.List[Tile]) -> typing.List[Army]:
split = []
if self.entangledValue is None:
self.entangledValue = self.value
for tile in fogTiles:
splitArmy = self.clone()
splitArmy.entangledValue = self.value
if self.entangledValue is not None:
splitArmy.entangledValue = self.entangledValue
split.append(splitArmy)
# entangle the armies
for existingEntangled in self.entangledArmies:
existingEntangled.entangledArmies.extend(split)
for splitBoi in split:
splitBoi.entangledArmies.extend(SearchUtils.where(split, lambda army: army != splitBoi))
logbook.info(f"for army {str(self)} set self as scrapped because splitting for fog")
self.scrapped = True
return split
def clone(self):
newDude = Army(self.tile, self.name)
if self.path is not None:
newDude.path = self.path.clone()
newDude.player = self.player
newDude.visible = self.visible
newDude.value = self.value
newDude.last_moved_turn = self.last_moved_turn
newDude.last_seen_turn = self.last_seen_turn
for path in self.expectedPaths:
clone = path.clone()
if clone is None:
raise "What the fuck?"
newDude.expectedPaths.append(clone)
newDude.entangledArmies = list(self.entangledArmies)
newDude.scrapped = self.scrapped
return newDude
def toString(self):
return f"[{self.name} {self.tile} p{self.player} v{self.value}{' scr' if self.scrapped else ''}]"
def __str__(self):
return self.toString()
def __repr__(self):
return self.toString()
def include_path(self, path: Path):
if path is None:
return
foundMatch = False
for existingPath in self.expectedPaths:
if existingPath is None:
self.expectedPaths.remove(existingPath)
continue
pathNode = path.start
existingPathNode = existingPath.start
isPathMatch = True
while pathNode is not None and existingPathNode is not None:
if pathNode.tile != existingPathNode.tile:
isPathMatch = False
break
pathNode = pathNode.next
existingPathNode = existingPathNode.next
if pathNode is not None or existingPathNode is not None:
# then one was longer than the other, also false
isPathMatch = False
if isPathMatch:
foundMatch = True
break
if not foundMatch and path is not None:
self.expectedPaths.append(path)