-
Notifications
You must be signed in to change notification settings - Fork 4
/
unit_role_manager.py
444 lines (374 loc) · 13.8 KB
/
unit_role_manager.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
"""Manage assigning/removing of roles and getting units by role.
"""
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Set, Union
from sc2.ids.unit_typeid import UnitTypeId as UnitID
from sc2.unit import Unit
from sc2.units import Units
from ares.consts import (
UNIT_TYPES_WITH_NO_ROLE,
ManagerName,
ManagerRequestType,
UnitRole,
)
from ares.managers.manager import Manager
from ares.managers.manager_mediator import IManagerMediator, ManagerMediator
if TYPE_CHECKING:
from ares import AresBot
class UnitRoleManager(Manager, IManagerMediator):
"""Assign and remove roles as well as organize units by role.
Other Managers should call this Manager's functions rather than assigning roles
themselves.
"""
LOCUSTS: Set[UnitID] = {UnitID.LOCUSTMP, UnitID.LOCUSTMPFLYING}
SQUAD_ROLES: Set[UnitRole] = {UnitRole.ATTACKING}
ZERG_STATIC_DEFENCE: Set[UnitID] = {UnitID.SPINECRAWLER, UnitID.SPORECRAWLER}
def __init__(
self,
ai: "AresBot",
config: Dict,
mediator: ManagerMediator,
) -> None:
"""Set up the manager.
Parameters
----------
ai :
Bot object that will be running the game
config :
Dictionary with the data from the configuration file
mediator :
ManagerMediator used for getting information from other managers.
Returns
-------
"""
super(UnitRoleManager, self).__init__(ai, config, mediator)
self.unit_role_dict: Dict[str, Set[int]] = {
role.name: set() for role in UnitRole
}
self.tag_to_role_dict: Dict[int, str] = {}
self.manager_requests_dict = {
ManagerRequestType.ASSIGN_ROLE: lambda kwargs: self.assign_role(**kwargs),
ManagerRequestType.BATCH_ASSIGN_ROLE: lambda kwargs: self.batch_assign_role(
**kwargs
),
ManagerRequestType.CLEAR_ROLE: lambda kwargs: self.clear_role(**kwargs),
ManagerRequestType.GET_ALL_FROM_ROLES_EXCEPT: lambda kwargs: (
self.get_all_from_roles_except(**kwargs)
),
ManagerRequestType.GET_UNIT_ROLE_DICT: lambda kwargs: self.unit_role_dict,
ManagerRequestType.GET_UNITS_FROM_ROLE: lambda kwargs: (
self.get_units_from_role(**kwargs)
),
ManagerRequestType.GET_UNITS_FROM_ROLES: lambda kwargs: (
self.get_units_from_roles(**kwargs)
),
ManagerRequestType.SWITCH_ROLES: lambda kwargs: self.switch_roles(**kwargs),
}
self.all_assigned_tags: Set[int] = set()
def manager_request(
self,
receiver: ManagerName,
request: ManagerRequestType,
reason: str = None,
**kwargs,
) -> Any:
"""Enables ManagerRequests to this Manager.
Parameters
----------
receiver :
The Manager the request is being sent to.
request :
The Manager that made the request
reason :
Why the Manager has made the request
kwargs :
If the ManagerRequest is calling a function, that function's keyword
arguments go here.
Returns
-------
Any
"""
return self.manager_requests_dict[request](kwargs)
async def update(self, iteration: int) -> None:
"""Update Overseer role in realtime.
Notes
-----
This is just a workaround for a realtime bug and is generally unused.
Parameters
----------
iteration :
The game iteration.
Returns
-------
"""
# This is a workaround to realtime overseers keeping their SCOUTING role from
# overlord
# TODO: Fix the root cause of this bug and remove this `if realtime` block
# TODO: This was an Eris fix, commented out for now to be investigated later
# if self.ai.realtime and iteration % 8 == 0:
# overseers: Units = self.manager_mediator.get_units_from_role(
# role=UnitRole.SCOUTING, unit_type=UnitID.OVERSEER
# )
# # Bots should assign appropriately once overseers have defending role
# for overseer in overseers:
# self.assign_role(overseer.tag, UnitRole.DEFENDING)
pass
def get_assigned_units(self) -> None:
"""Create set of all tags that have been assigned to a role.
Returns
-------
"""
assigned_tags_list: List[int] = []
for role in self.unit_role_dict:
assigned_tags_list += self.unit_role_dict[role]
self.all_assigned_tags = set(assigned_tags_list)
def catch_unit(self, unit: Unit) -> None:
"""Check if unit is unassigned and give it a role if necessary.
Parameters
----------
unit :
Unit that needs a role.
Returns
-------
"""
if unit.type_id in UNIT_TYPES_WITH_NO_ROLE:
return
if unit.tag not in self.all_assigned_tags:
if unit.type_id == self.ai.worker_type:
self.assign_role(unit.tag, UnitRole.GATHERING)
def assign_role(
self, tag: int, role: UnitRole, remove_from_squad: bool = True
) -> None:
"""Assign a unit a role.
Parameters
----------
tag :
Tag of the unit to be assigned.
role :
What role the unit should have.
remove_from_squad :
Search for this unit in UnitSquads and remove it.
Default=True prevents unexpected bugs when using
UnitSquads
Returns
-------
"""
self.clear_role(tag)
self.unit_role_dict[role.name].add(tag)
self.tag_to_role_dict[tag] = role.name
if remove_from_squad:
self.manager_mediator.remove_tag_from_squads(tag=tag)
def batch_assign_role(
self, tags: Union[List[int], Set[int]], role: UnitRole
) -> None:
"""Assign a given role to a List of unit tags.
Notes
-----
Nothing more than a for loop, provided for convenience.
Parameters
----------
tags :
Tags of the units to assign to a role.
role :
The role the units should be assigned to.
Returns
-------
"""
for tag in tags:
self.assign_role(tag, role)
def clear_role(self, tag: int) -> None:
"""Clear a unit's role.
Parameters
----------
tag :
Tag of the unit to clear the role of.
Returns
-------
"""
for role in self.unit_role_dict:
if tag in self.unit_role_dict[role]:
self.unit_role_dict[role].remove(tag)
def batch_clear_role(self, tags: Set[int]) -> None:
"""Clear the roles of a given List of unit tags.
Notes
-----
Nothing more than a for loop, provided for convenience.
Parameters
----------
tags :
Tags of the units to clear the roles of.
Returns
-------
"""
for tag in tags:
self.clear_role(tag)
def get_units_from_role(
self,
role: UnitRole,
unit_type: Optional[Union[UnitID, Set[UnitID]]] = None,
restrict_to: Optional[Units] = None,
) -> Units:
"""Get a Units object containing units with a given role.
If a UnitID or set of UnitIDs are given, it will only return units of those
types, otherwise it will return all units with the role. If `restrict_to` is
specified, it will only retrieve units from that object.
Parameters
----------
role :
Role to get units from.
unit_type :
Type(s) of units that should be returned. If omitted, all units with the
role will be returned.
restrict_to :
If supplied, only take Units with the given role and type if they also exist
here.
Returns
-------
Units :
Units with the given role.
"""
if unit_type:
if isinstance(unit_type, UnitID):
# single unit type, use the single type and role function
return Units(
self.get_single_type_from_single_role(unit_type, role, restrict_to),
self.ai,
)
else:
# will crash if not an iterable but we should be careful with typing
# anyway
retrieved_units: List[Unit] = []
for type_id in unit_type:
retrieved_units.extend(
self.get_single_type_from_single_role(
type_id, role, restrict_to
)
)
return Units(retrieved_units, self.ai)
else:
# get every unit with the role
if restrict_to:
tags_to_get: Set[int] = (
self.unit_role_dict[role.name] & restrict_to.tags
)
else:
tags_to_get: Set[int] = self.unit_role_dict[role.name]
# get the List[Unit] from UnitCacheManager and return as Units
return Units(
self.manager_mediator.manager_request(
ManagerName.UNIT_CACHE_MANAGER,
ManagerRequestType.GET_UNITS_FROM_TAGS,
tags=tags_to_get,
),
self.ai,
)
def get_units_from_roles(
self,
roles: Set[UnitRole],
unit_type: Union[None, UnitID, Set[UnitID]] = None,
) -> Units:
"""Get the units matching `unit_type` from the given roles.
Parameters
----------
roles :
Roles to get units from.
unit_type :
Type(s) of units that should be returned. If omitted, all units with the
role will be returned.
Returns
-------
Units :
Units with the given roles.
"""
retrieved_units = Units([], self.ai)
for role in roles:
retrieved_units.extend(self.get_units_from_role(role, unit_type))
return retrieved_units
def switch_roles(self, from_role: UnitRole, to_role: UnitRole) -> None:
"""Give all units in a role a different role.
Parameters
----------
from_role :
Role the units currently have.
to_role :
Role to assign to the units.
Returns
-------
"""
self.batch_assign_role(self.get_units_from_role(from_role).tags, to_role)
def get_all_from_roles_except(
self, roles: Set[UnitRole], excluded: Set[UnitID]
) -> Units:
"""Get all units from the given roles except for unit types in excluded.
Parameters
----------
roles :
Roles to get units from.
excluded :
Unit types that should not be included.
Returns
-------
Units :
Units matching the role that are not of an excluded type.
"""
role_tags: List[int] = []
valid_tags: List[int] = []
# get a list of the tags of the units in the given roles
for role in roles:
role_tags.extend(self.unit_role_dict[role.name])
# convert to a set for faster lookup
role_set: Set[int] = set(role_tags)
own_army_dict: Dict = self.manager_mediator.manager_request(
ManagerName.UNIT_CACHE_MANAGER, ManagerRequestType.GET_CACHED_OWN_ARMY_DICT
)
# get the tags of all units that aren't of the excluded types
for unit_type in own_army_dict:
if unit_type not in excluded:
valid_tags.extend(own_army_dict[unit_type].tags)
shared_tags: List[int] = [tag for tag in valid_tags if tag in role_set]
return Units(
self.manager_mediator.manager_request(
ManagerName.UNIT_CACHE_MANAGER,
ManagerRequestType.GET_UNITS_FROM_TAGS,
tags=shared_tags,
),
self.ai,
)
def get_single_type_from_single_role(
self, unit_type: UnitID, role: UnitRole, restrict_to: Optional[Units] = None
) -> List[Unit]:
"""Get all units of a given type that have a specified role.
If restrict_to is Units, this will only get the units of the specified type and
role that are also in restrict_to.
Parameters
----------
unit_type :
Type of unit to retrieve.
role :
Role the units should have.
restrict_to :
If supplied, only take Units with the given role and type if they also exist
here.
Returns
-------
List[Unit] :
Units matching the given type with the given role.
"""
# get set of tags of units with the role
unit_with_role_tags: set[int] = self.unit_role_dict[role.name]
# get the tags of units of the type
own_cached_army_dict = self.manager_mediator.get_own_army_dict
units_of_type_tags: set[int] = {u.tag for u in own_cached_army_dict[unit_type]}
# take the intersection of the sets to get the shared tags
# this will be the units of the specified type with the specified role
if not restrict_to:
shared_tags: set[int] = unit_with_role_tags & units_of_type_tags
else:
shared_tags: set[int] = (
unit_with_role_tags & units_of_type_tags & restrict_to.tags
)
# get the List[Unit] from UnitCacheManager
return self.manager_mediator.manager_request(
ManagerName.UNIT_CACHE_MANAGER,
ManagerRequestType.GET_UNITS_FROM_TAGS,
tags=shared_tags,
)