Skip to content
DrInfy edited this page Apr 26, 2020 · 6 revisions

The micro for sharpy can be extended in two ways. You can either replace the defaults or create a new temporarily used micro for certain component(s).

Replacing Default Unit Micro for Unit Type

Default micro can be found in knowledge.combat_manager.default_rules. All Micro classes should inherit MicroStep and because MicroStep is a Component it requires await component.start(knowledge). It is highly recommended that all default unit micros are initiated during KnowledgeBot.configure_managers.

class MyLingBot(KnowledgeBot):
    def __init__(self):
        super().__init__("My Ling Bot")

    def configure_managers(self) -> Optional[List[ManagerBase]]:
        rules = self.knowledge.combat_manager.default_rules
        rules.unit_micros[UnitTypeId.ZERGLING] = MyLingMicro()
        return None  # No new managers

    async def create_plan(self) -> BuildOrder:
        return BuildOrder(
            # TODO
        )        

Using Custom Micro in an Act

In case you need a special micro in a certain act (i.e. zergling runby, oracle harass or medivac drop), it's possible by creating a new MicroRules class and using that.

class MyLingMicro(MicroStep):
    def group_solve_combat(self, units: Units, current_command: Action) -> Action:
        pass # Your implementation here
        
    def unit_solve_combat(self, unit: Unit, current_command: Action) -> Action:
        pass # Your implementation here


class SuicideLings(ActBase):
    def __init__(self, adepts_to_start: int = 2):
        self.micro = MicroRules()
        self.micro.load_default_methods()
        self.micro.generic_micro = MyLingMicro(False)
        # Following would work for a specific unit type:
        # self.micro.unit_micros[UnitTypeId.ZERGLING]
        super().__init__()
        
    async def start(self, knowledge: "Knowledge"):
        await super().start(knowledge)
        # MicroRules is also a Component and needs start
        await self.micro.start(knowledge)
        
    async def execute(self) -> bool:
        target = self.knowledge.enemy_start_location
        for unit in self.cache.own(UnitTypeId.ZERGLING):
            self.combat.add_unit(unit)
        
        self.combat.execute(target, MoveType.Assault, rules=self.micro)

Replacing Default Methods

Default micro helper methods are implemented in DefaultMicroMethods. In order to replace them, set new ones to your MicroRules.

def focus_fire(
        step: MicroStep, 
        unit: Unit, 
        current_command: Action, 
        prio: Optional[Dict[UnitTypeId, int]]
    ) -> Action:
    enemies = step.cache.enemy_in_range(unit.position, lookup)
    best_distance = 0
    best_target = None
    
    for enemy in enemies:  # type: Unit
        d = unit.distance_to(enemy)
        if best_target is None or d < best_distance:
            # Blindly pick closest as best target
            # Does not consider air / ground units or weapon types
            best_distance = d
            best_target = enemy
    
    if best_target:
        return Action(best_target, True)

    return current_command
        

class FocusFireClosestBot(KnowledgeBot):
    def __init__(self):
        super().__init__("My Ling Bot")

    def configure_managers(self) -> Optional[List[ManagerBase]]:
        self.knowledge.combat_manager.default_rules.focus_fire_func = focus_fire
        return None  # No new managers
        
    async def create_plan(self) -> BuildOrder:
        return BuildOrder(
            # TODO
        )
Clone this wiki locally