BurnySc2 / python-sc2

A StarCraft II bot api client library for Python 3
MIT License
491 stars 157 forks source link

Stop workers from moving #197

Open zeroN1 opened 5 months ago

zeroN1 commented 5 months ago

I have a simple bot script where I am trying to issue a stop command to workers to stop and do nothing. I have the following script:

import sc2
import sc2.action
from sc2.bot_ai import BotAI
import sc2.constants
import sc2.game_data
import sc2.ids
from sc2.player import Bot, Computer
from sc2.ids.unit_typeid import UnitTypeId
import os

import sc2.unit
import sc2.units

os.environ["SC2PATH"] = "C:\\sc2\\StarCraft II"

def stop_all_units(bot: BotAI):
    '''
        Stop all units by cancelling all active and pending orders
    '''
    #u: sc2.unit.Unit = bot.units[0]
    #o: sc2.unit.UnitOrder = u.orders[0]
    #a: sc2.unit.AbilityId = o.ability
    print("Stopping all workers")
    for worker in bot.workers:
        if worker:
            print(f"{type(worker.hold_position)} {type(worker.stop)}")
            worker.stop()

class SimpleBot(BotAI):
    async def on_step(self, iteration):
        global HasInformedUnitsInfo
        #print_info(bot=self, iteration=iteration)
        #if not HasInformedUnitsInfo:
        stop_all_units(self)
            #HasInformedUnitsInfo = True

sc2.run_game(
    sc2.maps.get("AcropolisLE"),
    [Bot(sc2.Race.Zerg, SimpleBot()), Computer(sc2.Race.Zerg, sc2.Difficulty.Hard)],
    realtime=True
)

The above is the code I have. On every iteration, stop_all_units is executed and I can see the printed statements on the command prompt. However, once the game starts, the workers start gathering minerals and don't stop. I have also tried hold_position() on the worker objects without success and also tried worker.move(bot.start_location.position) to move them to a spot and stop gathering.

None of the above worked for me. Are there any other ways to do this or did I miss something? I am new to SC2 and maybe what I am trying makes no sense. Some pointers will be highly appreciated.