sc2nachos.extensions.upgrade_inference
Upgrade inference: the enemy's upgrades read off what only an upgrade can cause.
1"""Upgrade inference: the enemy's upgrades read off what only an upgrade can cause.""" 2 3from sc2nachos.extensions.upgrade_inference._events import EnemyUpgradeAssumedEvent 4from sc2nachos.extensions.upgrade_inference._upgrade_inference import UpgradeInference 5 6__all__ = ["EnemyUpgradeAssumedEvent", "UpgradeInference"]
18class EnemyUpgradeAssumedEvent(AnyUpgradeEvent): 19 """Upgrade inference has added an upgrade to `Enemy.upgrades`, which did not hold it: one it read off what only the 20 upgrade can cause, or off how fast the enemy's units move. Not for one the bot assumes, nor for a level NachOS 21 reads off the enemy's units. One the bot forgot comes again once it is shown again. 22 23 It comes at once, while upgrade inference handles `TurnStartEvent`, so before any of the bot's handlers of it. 24 Several of one observation come in id order, those read off movement after the rest. 25 """
Upgrade inference has added an upgrade to Enemy.upgrades, which did not hold it: one it read off what only the
upgrade can cause, or off how fast the enemy's units move. Not for one the bot assumes, nor for a level NachOS
reads off the enemy's units. One the bot forgot comes again once it is shown again.
It comes at once, while upgrade inference handles TurnStartEvent, so before any of the bot's handlers of it.
Several of one observation come in id order, those read off movement after the rest.
62@final 63class UpgradeInference: 64 """Upgrade inference: the enemy's upgrades read off what only an upgrade can cause, and added to 65 `Enemy.upgrades` with `Enemy.assume_upgrades`, beyond the attack, armor and shield levels NachOS reads there 66 itself. 67 68 Build it once the game has started, in the bot's own object an api is given as `extensions`: 69 70 ```python 71 from sc2nachos import Api 72 from sc2nachos.extensions.upgrade_inference import UpgradeInference 73 74 75 class Extensions: 76 def __init__(self, api: Api) -> None: 77 self.upgrade_inference = UpgradeInference(api) 78 79 80 api = Api(extensions=Extensions) 81 ``` 82 83 It reads each turn's observation on its `TurnStartEvent`, at `EventPriority.BEFORE`, before any handler of the 84 bot's, so every handler sees what it read; not the one the game ended on, which gets no turn: 85 86 - An enemy unit of a type nothing makes without an upgrade, in sight or remembered: a burrowed unit shows Burrow, 87 but a lurker, a widow mine or a creep tumor, which burrow without it, and a warp gate shows Warp Gate. 88 - On a visible enemy unit, a buff only an upgrade gives: Stimpack, Charge, the hydralisk's lunge, and the banshee's 89 and the ghost's cloak; and a marine with more than 45 health shows Combat Shield. 90 - On a visible unit of this player's, the slow of Concussive Shells and Interference Matrix. 91 - An enemy storm, Psionic Storm. 92 - A unit the enemy holds with a neural parasite shows Neural Parasite, and nothing of the player it was taken from. 93 One on another side than when first seen shows nothing by its type. 94 - With `include_speed_upgrades`, as by default, how fast its units move shows ten speed upgrades: Metabolic Boost, 95 Centrifugal Hooks, Glial Reconstitution, Muscular Augments, Anabolic Synthesis, Pneumatized Carapace, Hyperflight 96 Rotors, Gravitic Boosters, Gravitic Drive and Flux Vanes. Over windows of `speed_window`, 22 steps by default, on 97 one clock, it measures each visible enemy unit of a type one of them speeds up that was in sight throughout, and 98 on the side it was first seen on: one that moved at its speed with the upgrade at every observation of a window, 99 within `speed_tolerance` of the difference between its speeds without the upgrade and with it, 5% by default, 100 shows the upgrade; a Zerg ground unit at its speeds on creep where it stood on creep. None shows one while 101 wearing a buff that changes its speed, as Time Warp does, or where the upgrade adds less than 10% to its speed, as 102 Anabolic Synthesis does on creep. The rule was checked on games observed at every step, over windows of 22 steps 103 within 5%: a bot that observes fewer sees fewer moves in a window, and so tells less well a unit that moved 104 steadily from one that stopped and started again. 105 106 Upgrades are never lost, so what it reads stays. `Enemy.forget_upgrades` takes one back until it is shown again. In 107 a game with no other player it reads nothing. 108 109 Each upgrade it adds to `Enemy.upgrades` comes as an `EnemyUpgradeAssumedEvent`, an `AnyUpgradeEvent`, which `only` 110 selects by the upgrade. It comes at once, as it reads, so before any of the bot's handlers of `TurnStartEvent`. 111 112 Raises `TypeError` unless `speed_window` is a `GameTime`, `ValueError` unless it is longer than 0 and 113 `speed_tolerance` lies above 0 and below 0.5, and `NotPlayingError` if built before the game starts. A buff the 114 curated ids leave out, on any visible unit, raises `UncuratedIdError` from its handler, as reading `Unit.buffs` 115 does. 116 """ 117 118 __slots__ = ("_api",) 119 120 def __init__( 121 self, 122 api: Api[object], 123 *, 124 include_speed_upgrades: bool = True, 125 speed_window: GameTime = _DEFAULT_WINDOW, 126 speed_tolerance: float = _DEFAULT_TOLERANCE, 127 ) -> None: 128 """Upgrade inference for the enemy of `api`'s game, from its first turn, reading the speed upgrades too if 129 `include_speed_upgrades`, over windows of `speed_window`, within `speed_tolerance`.""" 130 check_the_speed_arguments(window=speed_window, tolerance=speed_tolerance) 131 self._api = api 132 try: 133 _ = api.enemy 134 except NoEnemyError: 135 return 136 api.on(TurnStartEvent, priority=EventPriority.BEFORE)(self._assume_the_upgrades_shown) 137 if include_speed_upgrades: 138 SpeedUpgradesFromMovement(api, window=speed_window, tolerance=speed_tolerance) 139 140 def _assume_the_upgrades_shown(self, _: TurnStartEvent) -> None: 141 """Assume the enemy has the upgrades the last observation shows, handing out an event for each new one.""" 142 api = self._api 143 assume_and_emit_events(api, self._enemy_upgrades_shown(api.units, api.effects)) 144 145 @classmethod 146 def _enemy_upgrades_shown(cls, units: Iterable[Unit[Any]], effects: Iterable[Effect]) -> frozenset[UpgradeId]: 147 """The enemy's upgrades shown by its units and effects and by the buffs on this player's units, beyond the 148 levels its units report.""" 149 shown: set[UpgradeId] = set() 150 for unit in units: 151 if unit.alliance is Alliance.ENEMY: 152 shown |= cls._owner_upgrades_shown_by_unit(unit) 153 elif unit.alliance is Alliance.OWN: 154 shown |= cls._opponent_upgrades_shown_by_unit(unit) 155 for effect in effects: 156 if effect.alliance is Alliance.ENEMY: 157 shown |= cls._owner_upgrades_shown_by_effect(effect) 158 return frozenset(shown) 159 160 @classmethod 161 def _owner_upgrades_shown_by_unit(cls, unit: Unit[Any]) -> frozenset[UpgradeId]: 162 """The upgrades `unit` shows its owner has: by its type, and while visible, by its buffs and its health. 163 164 A unit under a neural parasite shows only that its controller has Neural Parasite: its buffs and type belong to 165 the player it was taken from. One on another side than when first seen shows nothing by its type, even out of 166 sight. 167 """ 168 shown = cls._upgrades_its_type_needs(unit) if unit.alliance is unit.first_alliance else _NO_UPGRADES 169 if unit.visibility is not Visibility.VISIBLE: 170 return shown 171 buffs = unit.buffs 172 if BuffId.INFESTOR_NEURAL_PARASITE in buffs: 173 return frozenset({UpgradeId.NEURAL_PARASITE}) 174 worn = {_BUFF_EVIDENCE_OF_OWNER[buff] for buff in buffs if buff in _BUFF_EVIDENCE_OF_OWNER} 175 if (health := _HEALTH_EVIDENCE.get(unit.type_id)) is not None and unit.health_max > health[0]: 176 worn.add(health[1]) 177 return shown | worn if worn else shown 178 179 @classmethod 180 def _upgrades_its_type_needs(cls, unit: Unit[Any]) -> frozenset[UpgradeId]: 181 """The upgrades nothing makes a unit of `unit`'s type without: those its type's `tech_requirements` name, as a 182 zergling needs Burrow to burrow, and those of `_UNIT_TYPE_EVIDENCE`.""" 183 needed = unit.type_data.tech_requirements.upgrades 184 only = _UNIT_TYPE_EVIDENCE.get(unit.type_id) 185 return needed if only is None else needed | {only} 186 187 @classmethod 188 def _opponent_upgrades_shown_by_unit(cls, unit: Unit[Any]) -> frozenset[UpgradeId]: 189 """The upgrades the buffs on `unit` show its owner's opponent has. Nothing for a unit not visible.""" 190 if unit.visibility is not Visibility.VISIBLE: 191 return _NO_UPGRADES 192 return frozenset(_BUFF_EVIDENCE_OF_OPPONENT[buff] for buff in unit.buffs if buff in _BUFF_EVIDENCE_OF_OPPONENT) 193 194 @classmethod 195 def _owner_upgrades_shown_by_effect(cls, effect: Effect) -> frozenset[UpgradeId]: 196 """The upgrades `effect` shows its owner has.""" 197 upgrade = _EFFECT_EVIDENCE.get(effect.id) 198 return _NO_UPGRADES if upgrade is None else frozenset({upgrade})
Upgrade inference: the enemy's upgrades read off what only an upgrade can cause, and added to
Enemy.upgrades with Enemy.assume_upgrades, beyond the attack, armor and shield levels NachOS reads there
itself.
Build it once the game has started, in the bot's own object an api is given as extensions:
from sc2nachos import Api
from sc2nachos.extensions.upgrade_inference import UpgradeInference
class Extensions:
def __init__(self, api: Api) -> None:
self.upgrade_inference = UpgradeInference(api)
api = Api(extensions=Extensions)
It reads each turn's observation on its TurnStartEvent, at EventPriority.BEFORE, before any handler of the
bot's, so every handler sees what it read; not the one the game ended on, which gets no turn:
- An enemy unit of a type nothing makes without an upgrade, in sight or remembered: a burrowed unit shows Burrow, but a lurker, a widow mine or a creep tumor, which burrow without it, and a warp gate shows Warp Gate.
- On a visible enemy unit, a buff only an upgrade gives: Stimpack, Charge, the hydralisk's lunge, and the banshee's and the ghost's cloak; and a marine with more than 45 health shows Combat Shield.
- On a visible unit of this player's, the slow of Concussive Shells and Interference Matrix.
- An enemy storm, Psionic Storm.
- A unit the enemy holds with a neural parasite shows Neural Parasite, and nothing of the player it was taken from. One on another side than when first seen shows nothing by its type.
- With
include_speed_upgrades, as by default, how fast its units move shows ten speed upgrades: Metabolic Boost, Centrifugal Hooks, Glial Reconstitution, Muscular Augments, Anabolic Synthesis, Pneumatized Carapace, Hyperflight Rotors, Gravitic Boosters, Gravitic Drive and Flux Vanes. Over windows ofspeed_window, 22 steps by default, on one clock, it measures each visible enemy unit of a type one of them speeds up that was in sight throughout, and on the side it was first seen on: one that moved at its speed with the upgrade at every observation of a window, withinspeed_toleranceof the difference between its speeds without the upgrade and with it, 5% by default, shows the upgrade; a Zerg ground unit at its speeds on creep where it stood on creep. None shows one while wearing a buff that changes its speed, as Time Warp does, or where the upgrade adds less than 10% to its speed, as Anabolic Synthesis does on creep. The rule was checked on games observed at every step, over windows of 22 steps within 5%: a bot that observes fewer sees fewer moves in a window, and so tells less well a unit that moved steadily from one that stopped and started again.
Upgrades are never lost, so what it reads stays. Enemy.forget_upgrades takes one back until it is shown again. In
a game with no other player it reads nothing.
Each upgrade it adds to Enemy.upgrades comes as an EnemyUpgradeAssumedEvent, an AnyUpgradeEvent, which only
selects by the upgrade. It comes at once, as it reads, so before any of the bot's handlers of TurnStartEvent.
Raises TypeError unless speed_window is a GameTime, ValueError unless it is longer than 0 and
speed_tolerance lies above 0 and below 0.5, and NotPlayingError if built before the game starts. A buff the
curated ids leave out, on any visible unit, raises UncuratedIdError from its handler, as reading Unit.buffs
does.
120 def __init__( 121 self, 122 api: Api[object], 123 *, 124 include_speed_upgrades: bool = True, 125 speed_window: GameTime = _DEFAULT_WINDOW, 126 speed_tolerance: float = _DEFAULT_TOLERANCE, 127 ) -> None: 128 """Upgrade inference for the enemy of `api`'s game, from its first turn, reading the speed upgrades too if 129 `include_speed_upgrades`, over windows of `speed_window`, within `speed_tolerance`.""" 130 check_the_speed_arguments(window=speed_window, tolerance=speed_tolerance) 131 self._api = api 132 try: 133 _ = api.enemy 134 except NoEnemyError: 135 return 136 api.on(TurnStartEvent, priority=EventPriority.BEFORE)(self._assume_the_upgrades_shown) 137 if include_speed_upgrades: 138 SpeedUpgradesFromMovement(api, window=speed_window, tolerance=speed_tolerance)
Upgrade inference for the enemy of api's game, from its first turn, reading the speed upgrades too if
include_speed_upgrades, over windows of speed_window, within speed_tolerance.