-
Notifications
You must be signed in to change notification settings - Fork 179
Add single unit unload example bot #108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
BurnySc2
wants to merge
2
commits into
develop
Choose a base branch
from
add_single_unit_unload_example
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+167
−19
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| import sys, os | ||
|
|
||
| sys.path.append(os.path.join(os.path.dirname(__file__), "../..")) | ||
|
|
||
| from typing import Union | ||
| from loguru import logger | ||
|
|
||
| import sc2 | ||
| from sc2 import Race, Difficulty | ||
| from sc2.ids.unit_typeid import UnitTypeId | ||
| from sc2.player import Bot, Computer | ||
| from sc2.unit import Unit | ||
| from sc2.units import Units | ||
|
|
||
| from s2clientprotocol import raw_pb2 as raw_pb | ||
| from s2clientprotocol import sc2api_pb2 as sc_pb | ||
| from s2clientprotocol import ui_pb2 as ui_pb | ||
|
|
||
|
|
||
| class SingleUnitUnloadBot(sc2.BotAI): | ||
| def __init__(self): | ||
| self.raw_affects_selection = True | ||
| self.enable_feature_layer = True | ||
|
|
||
| async def on_start(self): | ||
| self.client.game_step = 8 | ||
| self.load_unit_types = { | ||
| UnitTypeId.ZEALOT, | ||
| UnitTypeId.STALKER, | ||
| UnitTypeId.DARKTEMPLAR, | ||
| UnitTypeId.HIGHTEMPLAR, | ||
| } | ||
|
|
||
| async def unload_unit(self, transporter_unit: Unit, unload_unit: Union[int, Unit]): | ||
| assert isinstance(transporter_unit, Unit) | ||
| assert isinstance(unload_unit, (int, Unit)) | ||
| assert hasattr(self, "raw_affects_selection") and self.raw_affects_selection is True | ||
| assert hasattr(self, "enable_feature_layer") and self.enable_feature_layer is True | ||
| if isinstance(unload_unit, Unit): | ||
| unload_unit_tag = unload_unit.tag | ||
| else: | ||
| unload_unit_tag = unload_unit | ||
|
|
||
| # TODO Change unit.py passengers to return a List[Unit] instead of Set[Unit] ? Then I don't have to loop over '._proto' | ||
| unload_unit_index = next( | ||
| (index for index, unit in enumerate(transporter_unit._proto.passengers) if unit.tag == unload_unit_tag), | ||
| None | ||
| ) | ||
|
|
||
| if unload_unit_index is None: | ||
| logger.info(f"Unable to find unit {unload_unit} in transporter {transporter_unit}") | ||
| return | ||
|
|
||
| logger.info(f"Unloading unit at index: {unload_unit_index}") | ||
| await self.client._execute( | ||
| action=sc_pb.RequestAction( | ||
| actions=[ | ||
| sc_pb.Action( | ||
| action_raw=raw_pb.ActionRaw( | ||
| unit_command=raw_pb.ActionRawUnitCommand(ability_id=0, unit_tags=[transporter_unit.tag]) | ||
| ) | ||
| ), | ||
| sc_pb.Action( | ||
| action_ui=ui_pb.ActionUI( | ||
| cargo_panel=ui_pb.ActionCargoPanelUnload(unit_index=unload_unit_index) | ||
| ) | ||
| ), | ||
| ] | ||
| ) | ||
| ) | ||
|
|
||
| async def on_step(self, iteration): | ||
| # Spawn units | ||
| logger.info(f"Spawning units") | ||
| await self.client.debug_create_unit( | ||
| [ | ||
| [UnitTypeId.WARPPRISM, 1, self.game_info.map_center, 1], | ||
| [UnitTypeId.ZEALOT, 1, self.game_info.map_center, 1], | ||
| [UnitTypeId.STALKER, 1, self.game_info.map_center, 1], | ||
| [UnitTypeId.DARKTEMPLAR, 1, self.game_info.map_center, 1], | ||
| [UnitTypeId.HIGHTEMPLAR, 1, self.game_info.map_center, 1], | ||
| ] | ||
| ) | ||
| # Load units into prism | ||
| await self._advance_steps(50) | ||
| prism = self.units(UnitTypeId.WARPPRISM)[0] | ||
| my_zealot = self.units(UnitTypeId.ZEALOT)[0] | ||
| my_units = self.units(self.load_unit_types) | ||
| logger.info(f"Loading units into prism: {my_units}") | ||
| for unit in my_units: | ||
| unit.smart(prism) | ||
|
|
||
| # Unload single unit - here: zealot | ||
| await self._advance_steps(50) | ||
| assert self.units(self.load_unit_types).amount == 0 | ||
| prism: Unit = self.units(UnitTypeId.WARPPRISM)[0] | ||
| await self.unload_unit(prism, my_zealot) | ||
| # Also works: | ||
| # await self.unload_unit(prism, my_zealot.tag) | ||
|
|
||
| await self._advance_steps(50) | ||
| my_units = self.units(self.load_unit_types) | ||
| assert my_units.amount == 1, f"{my_units}" | ||
| my_zealots = self.units(UnitTypeId.ZEALOT) | ||
| assert my_zealots.amount == 1, f"{my_zealots}" | ||
| assert my_zealots[0].tag == my_zealot.tag | ||
|
|
||
| logger.info("Everything ran as expected. Terminating.") | ||
| await self.client.leave() | ||
|
|
||
|
|
||
| def main(): | ||
| sc2.run_game( | ||
| sc2.maps.get("2000AtmospheresAIE"), | ||
| [Bot(Race.Protoss, SingleUnitUnloadBot()), | ||
| Computer(Race.Terran, Difficulty.Medium)], | ||
| realtime=False, | ||
| save_replay_as="PvT.SC2Replay", | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a typo and should be "raw_affects_selection", right ?