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 | class PlayerManager(AoE2Object):
"""Manager of everything player related."""
# Todo: Implement a DE version separate of this.
# I'll be dealing with this IF support for other game versions will ever happen.
_link_list = [
RetrieverObjectLink("_player_count", "FileHeader", "player_count"),
RetrieverObjectLinkGroup("DataHeader", group=[
RetrieverObjectLink("_tribe_names", link="tribe_names"),
RetrieverObjectLink("_string_table_player_names", link="string_table_player_names"),
RetrieverObjectLink("_metadata", link="player_data_1", process_as_object=PlayerMetaData),
RetrieverObjectLink("_lock_civilizations", link="per_player_lock_civilization"),
RetrieverObjectLink("_lock_personalities", link="per_player_lock_personality", support=Support(since=1.53)),
]),
RetrieverObjectLink("_resources", "PlayerDataTwo", "resources", process_as_object=PlayerResources),
RetrieverObjectLinkGroup("Diplomacy", group=[
RetrieverObjectLink("_diplomacy", link="per_player_diplomacy", process_as_object=PlayerDiplomacy),
RetrieverObjectLink("_allied_victories", link="per_player_allied_victory"),
]),
RetrieverObjectLinkGroup("Options", group=[
*[
RetrieverObjectLink(f"_disabled_{type_}_ids_player_{i}", link=f"disabled_{type_}_ids_player_{i}")
for type_ in ["tech", "building", "unit"] for i in range(1, 9)
],
RetrieverObjectLink("_starting_ages", link="per_player_starting_age"),
RetrieverObjectLink("_base_priorities", link="per_player_base_priority"),
]),
RetrieverObjectLink("_pop_caps", "Map", "per_player_population_cap", support=Support(since=1.44)),
RetrieverObjectLink("_initial_player_views", "Map", "initial_player_views", support=Support(since=1.40), process_as_object=PlayerInitialView),
RetrieverObjectLinkGroup("Units", group=[
RetrieverObjectLink("_player_data_4", link="player_data_4", process_as_object=PlayerDataFour),
RetrieverObjectLink("_player_data_3", link="player_data_3", process_as_object=PlayerDataThree),
]),
]
def __init__(
self,
_player_count: int,
_tribe_names: List[str],
_string_table_player_names: List[int],
_metadata: List[PlayerMetaData],
_lock_civilizations: List[int],
_lock_personalities: List[int],
_resources: List[PlayerResources],
_diplomacy: List[PlayerDiplomacy],
_allied_victories: List[int],
_starting_ages: List[int],
_base_priorities: List[int],
_pop_caps: List[int],
_initial_player_views: List[PlayerInitialView],
_player_data_4: List[PlayerDataFour],
_player_data_3: List[PlayerDataThree],
**kwargs
):
super().__init__(**kwargs)
disables = {}
for type_ in ['tech', 'building', 'unit']:
disables[type_] = [kwargs[f'_disabled_{type_}_ids_player_{p}'] for p in range(1, 9)]
gaia_first_params = {
'initial_player_view_x': [ipv.location_x for ipv in _initial_player_views or []],
'initial_player_view_y': [ipv.location_y for ipv in _initial_player_views or []],
}
no_gaia_params = {
'population_cap': [int(pd.population_limit) for pd in _player_data_4],
'tribe_name': _tribe_names,
'string_table_name_id': _string_table_player_names,
'base_priority': _base_priorities,
'allied_victory': _allied_victories,
'disabled_techs': disables['tech'],
'disabled_buildings': disables['building'],
'disabled_units': disables['unit'],
'initial_camera_x': [pd.initial_camera_x for pd in _player_data_3],
'initial_camera_y': [pd.initial_camera_y for pd in _player_data_3],
'diplomacy': [d.diplomacy_stance for d in _diplomacy],
}
gaia_last_params = {
'starting_age': _starting_ages,
'lock_civ': _lock_civilizations,
'lock_personality': _lock_personalities,
'food': [r.food for r in _resources],
'wood': [r.wood for r in _resources],
'gold': [r.gold for r in _resources],
'stone': [r.stone for r in _resources],
'color': [r.color for r in _resources],
'active': [r.active for r in _metadata],
'human': [r.human for r in _metadata],
'civilization': [r.civilization for r in _metadata],
'architecture_set': [r.architecture_set for r in _metadata],
}
param_sets = [(no_gaia_params, None), (gaia_first_params, True), (gaia_last_params, False)]
player_attributes: Dict[int, Dict] = {i: {'player_id': PlayerId(i)} for i in range(9)}
for param_set, gaia_first in param_sets:
for key, lst in param_set.items():
# If a property is not supported, fill it with Nones and skip it
if lst is None or isinstance(lst, list) and len(lst) == 0:
_spread_player_attributes(player_attributes, key, [None] * 16, gaia_first)
continue
_spread_player_attributes(player_attributes, key, lst, gaia_first)
self.players = [Player(**player_attributes[p]) for p in PlayerId.all()]
@property
def active_players(self) -> int:
"""The amount of players that are active within the scenario"""
return len([player for player in self.players if player.active])
@active_players.setter
def active_players(self, value: int):
if not 1 <= value <= 8:
raise ValueError("Active players value has to be between 1 and 8")
for player_id in PlayerId.all(exclude_gaia=True):
setattr(self.players[player_id], '_active', player_id <= value)
@property
def players(self) -> List[Player]:
"""Returns all player objects"""
return self._players
@players.setter
def players(self, value: List[Player]) -> None:
"""Sets player objects"""
self._players = UuidList(self._uuid, value)
def set_default_starting_resources(self, players: List[PlayerId] = None) -> None:
"""
Sets the default starting resources for all players
Warning: Does NOT take civilizations into account
This does not take the current selected civ of this player into account. For example, a player with the
Chinese civ selected will still be set to 200 food. Generally speaking, it's recommended to not use this for
competitive, normal play. You can select `low` resources in the lobby menu to get 'normal' resources for
every civ.
Args:
players: A list of players, defaults to all players (incl GAIA) when left out
"""
if players is None:
players = PlayerId.all()
for player in players:
self.players[player].food = 200
self.players[player].wood = 200
self.players[player].gold = 100
self.players[player].stone = 200
def set_diplomacy_teams(self, *args: List[PlayerId | int], diplomacy: DiplomacyState = DiplomacyState.ALLY) \
-> None:
"""
Sets all players in list allied with all others in the same list.
Args:
*args: List(s) with player IDs that'll be set to the given diplomacy value
diplomacy: The diplomacy to set the teams to. Defaults to ally.
Examples:
To set diplomacy like a 4v4 in ranked. Two teams of 4 with alternating IDs.
set_diplomacy_teams([1,3,5,7], [2,4,6,8], diplomacy=DiplomacyState.ALLY)
"""
for team in args:
for player in team:
if player == PlayerId.GAIA:
raise ValueError("Gaia cannot be in a team")
self.players[player].set_player_diplomacy([p for p in team if p != player], diplomacy)
# ###############################################################################################
# ################################# Functions for reconstruction ################################
# ###############################################################################################
def __getattribute__(self, name: str) -> Any:
if name.startswith('_disabled_'):
type_ = name.split('_')[2]
return getattr(self.players[int(name[-1])], f"disabled_{type_}s")
return super().__getattribute__(name)
@property
def _player_count(self):
"""Returns number of active players to be stored in the FileHeader"""
return self.active_players
@property
def _allied_victories(self):
"""Returns the allied victory of all players"""
return self._player_attributes_to_list("allied_victory", None, default=0, fill_empty=8)
@property
def _starting_ages(self):
"""Returns the starting age of all players"""
return self._player_attributes_to_list("starting_age", False, default=2, fill_empty=7)
@property
def _lock_civilizations(self):
"""Returns the civ lock bool of all players"""
return self._player_attributes_to_list("lock_civ", False, default=0, fill_empty=7)
@property
def _lock_personalities(self):
"""Returns the civ lock bool of all players"""
return self._player_attributes_to_list("lock_personality", False, default=0, fill_empty=7)
@property
def _pop_caps(self):
"""Returns the population cap of all players"""
return self._player_attributes_to_list("population_cap", None, default=200, fill_empty=8)
@property
def _diplomacy(self):
"""Returns the diplomacy of all players"""
diplomacies = self._player_attributes_to_list("diplomacy", None)
player_diplomacies = UuidList(self._uuid, [
PlayerDiplomacy(diplomacy_stance=diplomacies[i]) for i in range(8)
])
player_diplomacies.extend([
PlayerDiplomacy(diplomacy_stance=[3] * 16) for _ in range(8)
])
return player_diplomacies
@property
def _player_data_4(self):
"""Returns the resource duplicates for all players"""
population_limit = self._player_attributes_to_list("population_cap", None, default=200)
food = self._player_attributes_to_list("food", None, default=0)
wood = self._player_attributes_to_list("wood", None, default=0)
gold = self._player_attributes_to_list("gold", None, default=0)
stone = self._player_attributes_to_list("stone", None, default=0)
return UuidList(self._uuid, [
PlayerDataFour(
population_limit=float(population_limit[i]),
food_duplicate=float(food[i]),
wood_duplicate=float(wood[i]),
gold_duplicate=float(gold[i]),
stone_duplicate=float(stone[i]),
) for i in range(8)
])
@property
def _player_data_3(self) -> List[PlayerDataThree]:
"""Returns the resource objects for all players"""
original_map: Dict[int, str] = {0: 'ally', 1: 'neutral', 3: 'enemy'}
mappings: Dict[str, Dict[str, int]] = {
'diplomacy_for_interaction': {'self': 0, 'ally': 0, 'neutral': 1, 'enemy': 3, 'gaia': 3},
'diplomacy_for_ai_system': {'self': 1, 'ally': 2, 'neutral': 3, 'enemy': 4, 'gaia': 0},
}
initial_camera_x = self._player_attributes_to_list("initial_camera_x", None, default=72)
initial_camera_y = self._player_attributes_to_list("initial_camera_y", None, default=72)
aok_allied_victory = self._player_attributes_to_list("allied_victory", None, default=0)
color = self._player_attributes_to_list("color", False, default=1)
diplomacies: List[List[int]] = self._player_attributes_to_list("diplomacy", None)
other_diplomacies: Dict[str, List[List[int]]] = {}
for player in range(8):
diplomacy = diplomacies[player][:8]
for key, mapping in mappings.items():
lst = other_diplomacies.setdefault(key, [])
temp_lst = [mapping['gaia']] + [mapping[original_map[n]] for n in diplomacy]
temp_lst[player + 1] = mapping['self']
lst.append(temp_lst)
return UuidList(self._uuid, [
PlayerDataThree(
initial_camera_x[i],
initial_camera_y[i],
aok_allied_victory[i],
other_diplomacies['diplomacy_for_interaction'][i],
other_diplomacies['diplomacy_for_ai_system'][i],
color[i],
) for i in range(len(initial_camera_x))
])
@property
def _resources(self) -> List[PlayerResources]:
"""Returns the resource objects for all players"""
food = self._player_attributes_to_list("food", False, default=0, fill_empty=7)
wood = self._player_attributes_to_list("wood", False, default=0, fill_empty=7)
gold = self._player_attributes_to_list("gold", False, default=0, fill_empty=7)
stone = self._player_attributes_to_list("stone", False, default=0, fill_empty=7)
color = self._player_attributes_to_list("color", False, default=0)
color.extend(range(9, 16))
return UuidList(self._uuid, [
PlayerResources(food[i], wood[i], gold[i], stone[i], color[i]) for i in range(len(food))
])
@property
def _metadata(self) -> List[PlayerMetaData]:
"""Returns the metadata objects for all players"""
active = self._player_attributes_to_list("active", False, default=0, fill_empty=7)
human = self._player_attributes_to_list("human", False, default=1, fill_empty=7)
civilization = self._player_attributes_to_list("civilization", False, default=Civilization.RANDOM, fill_empty=7)
architecture_set = self._player_attributes_to_list("architecture_set", False, default=Civilization.RANDOM, fill_empty=7)
return UuidList(self._uuid, [
PlayerMetaData(active[i], human[i], civilization[i], architecture_set[i]) for i in range(len(active))
])
@property
def _base_priorities(self) -> List[int]:
"""Returns the base priorities of all players"""
return self._player_attributes_to_list("base_priority", None, default=0)
@property
def _tribe_names(self) -> List[str]:
"""Returns the tribe names of all players"""
return self._player_attributes_to_list("tribe_name", None, default="", fill_empty=8)
@property
def _string_table_player_names(self) -> List[int]:
"""Returns the string table player names of all players"""
return self._player_attributes_to_list("string_table_name_id", None, default=-2, fill_empty=8)
@property
def _initial_player_views(self):
x = self._player_attributes_to_list("initial_player_view_x", gaia_first=True, default=-1, fill_empty=7)
y = self._player_attributes_to_list("initial_player_view_y", gaia_first=True, default=-1, fill_empty=7)
return UuidList(self._uuid, [PlayerInitialView(x[i], y[i]) for i in range(len(x))])
def _player_attributes_to_list(
self,
attribute: str,
gaia_first: bool | None = True,
default: str | int = 0,
fill_empty: int = 0
) -> List[Any]:
"""
The list to store in the scenario structure with values from all players.
Args:
attribute: The attribute to get from the players
gaia_first: If the list has gaia first, last or not at all
default: The default value to fill the empty fields and what to end to an 16 field list
fill_empty: How many empty elements have to be filled with the default value
Returns:
The list of values
"""
players = _player_list(gaia_first)
default_list = [default] * fill_empty
values = []
for p in players:
try:
v = getattr(self.players[p], attribute)
if v is None and gaia_first is None:
v = default
except UnsupportedAttributeError:
v = None
values.append(v)
return values + default_list
|