def current(self, observations: Observations) -> SmartOrder: counter = BuildingCounter() if not self.current_order.done(observations) and not isinstance( self.current_order, NoOrder): return self.current_order elif self.expected_refineries == 1 and self.expected_refineries > counter.refineries_count( observations): self.current_order = BuildRefinery(self.location, 1) elif self.expected_refineries == 2 and 0 == counter.refineries_count( observations): self.current_order = BuildRefinery(self.location, 1) elif self.expected_refineries == 2 and 1 == counter.refineries_count( observations): self.current_order = BuildRefinery(self.location, 2) elif self.missing_barracks(observations, counter): self.current_order = BuildBarrack(self.location) elif self.missing_supply_depot(observations, counter): self.current_order = BuildSupplyDepot(self.location) elif self.expected_factories > counter.factories_count(observations): self.current_order = BuildFactory(self.location) elif self.expected_techlab_barrack > counter.techlab_barracks_count( observations): self.current_order = BuildTechLabBarrack(self.location, 1) elif self.expected_reactor_barrack > counter.reactor_barracks_count( observations): self.current_order = BuildReactorBarrack(self.location, 2) else: self.current_order = NoOrder() return self.current_order
def order(self, observations: Observations, step_index: int)-> Order: if observations.last(): self.qlearn.learn(str(self.previous_state), self.previous_action, observations.reward(), 'terminal') QLearningTableStorage().save(self.qlearn, self.agent_name) self.previous_action = None self.previous_state = None self.previous_order = None return NoOrder() elif observations.first(): self.location = Location(observations) self.smart_actions = SmartActions(self.location) self.qlearn = QLearningTable(actions=list(range(len(self.smart_actions.all())))) QLearningTableStorage().load(self.qlearn, self.agent_name) self.control_group_order = PrepareSCVControlGroupsOrder(self.location) if not self.control_group_order.done(observations): return self.control_group_order elif not self.previous_order or self.previous_order.done(observations): current_state = StateBuilder().build_state(self.location, observations) if self.previous_action is not None: self.qlearn.learn(str(self.previous_state), self.previous_action, 0, str(current_state)) rl_action = self.qlearn.choose_action(str(current_state)) self.previous_state = current_state self.previous_action = rl_action self.previous_order = self.smart_actions.order(rl_action) return self.previous_order
def order(self, observations: Observations, step_index: int) -> Order: if not self.current_order and self._can_play(step_index): if self.idle_scv_to_mineral.doable(observations): if self.idle_scv_to_mineral.done(observations): self.idle_scv_to_mineral = SendIdleSCVToMineral( self.base_location) self.current_order = self.idle_scv_to_mineral elif self.fill_refinery_one_order.doable( observations ) and not self.fill_refinery_one_order.done(observations): self.current_order = self.fill_refinery_one_order self._plan_to_build_scv_mineral_harvesters(3) elif self.fill_refinery_two_order.doable( observations ) and not self.fill_refinery_two_order.done(observations): self.current_order = self.fill_refinery_two_order self._plan_to_build_scv_mineral_harvesters(3) elif self._has_scv_to_build(observations): self.current_order = self._scv_to_build_order(observations) elif self.current_order and self.current_order.done(observations): self._update_last_played_step(step_index) self.current_order = None return CenterCameraOnCommandCenter(self.base_location) if self.current_order: return self.current_order return NoOrder()
def order(self, observations: Observations, step_index: int) -> Order: if self.enemy_detector.race_detected(): return NoOrder() elif self.camera_order: self.enemy_detector.detect_race(observations) self.camera_order = None #print(self.enemy_detector.race()) return CenterCameraOnCommandCenter(self.location) elif self._see_enemy_on_minimap(observations): enemy_y, enemy_x = self._enemy_position_on_minimap(observations) self.camera_order = MoveCameraOnMinimapTarget( self.location, enemy_x[0], enemy_y[0]) return self.camera_order return NoOrder()
def _extra_supply_depots(self, observations: Observations) -> Order: counter = BuildingCounter() expectMore = 8 > counter.supply_depots_count(observations) supplyAlmostFull = observations.player().food_cap( ) - observations.player().food_used() <= 2 if expectMore and supplyAlmostFull: return BuildSupplyDepot(self.location) else: return NoOrder()
def __init__(self, location: Location): self.location = location self.current_order = BuildSupplyDepot( self.location) # as a Terran, you need to start by this self.expected_supply_depot = 8 # 2 last can block a vcs against minerals when playing bottom down self.expected_barracks = 4 self.expected_refineries = 1 self.expected_factories = 1 # second one is not buildable when playing bottom down self.expected_techlab_barrack = 1 self.expected_reactor_barrack = 1
def order(self, action_id: str) -> Order: smart_action, x, y = self._split_action(action_id) if smart_action == ACTION_BUILD_MARINE: return BuildMarine(self.location) elif smart_action == ACTION_BUILD_MARAUDER: return BuildMarauder(self.location) elif smart_action == ACTION_ATTACK: return QLearningAttack(self.location, int(x), int(y)) elif smart_action == ACTION_DO_NOTHING: return NoOrder() else: raise Exception('The smart action ' + smart_action + " is unknown")
def order(self, observations: Observations, step_index: int) -> Order: if self.build_orders.finished(observations): return self._extra_supply_depots(observations) elif self.current_order and self.current_order.done(observations): self.current_order = None return CenterCameraOnCommandCenter(self.location) elif self.current_order and not self.current_order.done(observations): return self.current_order elif not self.current_order: order = self.build_orders.current(observations) if order.doable(observations): self.current_order = order return self.current_order return NoOrder()
def order(self, action_id: str) -> Order: smart_action, x, y = self._split_action(action_id) if smart_action == ACTION_BUILD_BARRACKS: max_barracks = 2 return BuildBarrack(self.location, max_barracks) elif smart_action == ACTION_BUILD_SUPPLY_DEPOT: max_supplies = 2 return BuildSupplyDepot(self.location, max_supplies) elif smart_action == ACTION_BUILD_MARINE: return BuildMarine(self.location) elif smart_action == ACTION_ATTACK: return DumbAttack(self.location, int(x), int(y)) elif smart_action == ACTION_DO_NOTHING: return NoOrder() else: raise Exception('The smart action ' + smart_action + " is unknown")
def order(self, observations: Observations, step_index: int) -> Order: if observations.last(): self.qlearn.learn(str(self.previous_state), self.previous_action, observations.reward(), 'terminal') QLearningTableStorage().save(self.qlearn, self.agent_name) self.previous_action = None self.previous_state = None self.previous_order = None return NoOrder() if not self.previous_order or self.previous_order.done(observations): current_state = StateBuilder().build_state(self.location, observations, self.enemy_detector) if self.previous_action is not None: self.qlearn.learn(str(self.previous_state), self.previous_action, 0, str(current_state)) rl_action = self.qlearn.choose_action(str(current_state)) self.previous_state = current_state self.previous_action = rl_action self.previous_order = self.smart_actions.order(rl_action) return self.previous_order
class BuildOrder: def __init__(self, location: Location): self.location = location self.current_order = BuildSupplyDepot( self.location) # as a Terran, you need to start by this self.expected_supply_depot = 8 # 2 last can block a vcs against minerals when playing bottom down self.expected_barracks = 4 self.expected_refineries = 1 self.expected_factories = 1 # second one is not buildable when playing bottom down self.expected_techlab_barrack = 1 self.expected_reactor_barrack = 1 def current(self, observations: Observations) -> SmartOrder: counter = BuildingCounter() if not self.current_order.done(observations) and not isinstance( self.current_order, NoOrder): return self.current_order elif self.expected_refineries == 1 and self.expected_refineries > counter.refineries_count( observations): self.current_order = BuildRefinery(self.location, 1) elif self.expected_refineries == 2 and 0 == counter.refineries_count( observations): self.current_order = BuildRefinery(self.location, 1) elif self.expected_refineries == 2 and 1 == counter.refineries_count( observations): self.current_order = BuildRefinery(self.location, 2) elif self.missing_barracks(observations, counter): self.current_order = BuildBarrack(self.location) elif self.missing_supply_depot(observations, counter): self.current_order = BuildSupplyDepot(self.location) elif self.expected_factories > counter.factories_count(observations): self.current_order = BuildFactory(self.location) elif self.expected_techlab_barrack > counter.techlab_barracks_count( observations): self.current_order = BuildTechLabBarrack(self.location, 1) elif self.expected_reactor_barrack > counter.reactor_barracks_count( observations): self.current_order = BuildReactorBarrack(self.location, 2) else: self.current_order = NoOrder() return self.current_order def missing_supply_depot(self, observations: Observations, counter: BuildingCounter) -> bool: expectMore = self.expected_supply_depot > counter.supply_depots_count( observations) supplyAlmostFull = observations.player().food_cap( ) - observations.player().food_used() <= 4 buildingOne = isinstance(self.current_order, BuildSupplyDepot) # but still build several in a row, needs to detect if a supply building is in progress return expectMore and supplyAlmostFull and not buildingOne def missing_barracks(self, observations: Observations, counter: BuildingCounter) -> bool: expectMore = self.expected_barracks > counter.barracks_count( observations) buildingOne = isinstance(self.current_order, BuildBarrack) return expectMore and not buildingOne def finished(self, observations: Observations) -> bool: counter = BuildingCounter() supply_ok = counter.supply_depots_count( observations) == self.expected_supply_depot barracks_ok = counter.barracks_count( observations) == self.expected_barracks refineries_ok = counter.refineries_count( observations) == self.expected_refineries factories_ok = counter.factories_count( observations) == self.expected_factories techlab_barracks_ok = counter.techlab_barracks_count( observations) == self.expected_techlab_barrack return supply_ok and barracks_ok and refineries_ok and factories_ok and techlab_barracks_ok