def canTransition(self): if self.allowedTransitions > 0: LOG.debug("transition IS allowed") return True else: LOG.debug("transition not allowed") return False
def make_worker(self): if self.minerals >= 50 and self.supply >= 1: for base in self.bases: if base.makeWorker(): self.supply -= 1 self.minerals -= CONFIG["worker"]["mincost"] LOG.debug("making a worker") return True return False
def transfer_to_minerals(self): for base in self.bases: # if there is at least 1 worker in a geyser if base.builtGeysers > 0: for g in base.geysers: if g > 0: LOG.debug("transfer to minerals in progress...") return base.transferGasToMins() return False
def transfer_to_gas(self): for base in self.bases: # if the geysers aren't all occupied if base.builtGeysers > 0: for g in base.geysers: if g < 3: LOG.debug("transfer to gas in progress...") return base.transferMinsToGas() return False
def build_geyser(self): for base in self.bases: # check all bases if self.race == Race.ZERG: if base.builtGeysers < base.amtGeysers and self.minerals >= 25 and base.workersOnMinerals >= 1: if base.sendWorkerToBuildGeyser(): self.minerals -= CONFIG["extractor"]["mincost"] self.supply += 1 # this will free up supply a second or two faster than actual - this could lead to issues later with unit production. LOG.debug("building zerg extractor") return True return False else: if base.builtGeysers < base.amtGeysers and self.minerals >= 75: if base.sendWorkerToBuildGeyser(): self.minerals -= CONFIG["assimilator"]["mincost"] LOG.debug("building terran/protoss geyser") return True return False return False
def build_supply(self): if self.race == Race.ZERG and self.minerals >= CONFIG["overlord"][ "mincost"]: for base in self.bases: if base.build_supply(): self.minerals -= CONFIG["overlord"]["mincost"] return True elif self.race == Race.TERRAN and self.minerals >= CONFIG[ "supplydepot"]["mincost"]: for base in self.bases: if base.build_supply(): self.minerals -= CONFIG["supplydepot"]["mincost"] return True elif self.race == Race.PROTOSS and self.minerals >= CONFIG["pylon"][ "mincost"]: for base in self.bases: if base.build_supply(): self.minerals -= CONFIG["pylon"]["mincost"] LOG.debug("building supply failed") return False
def run_simulation(output, gamestate, target): if not gamestate.remaining_ticks: output.append(gamestate.player.build_order) return output if target == None: for action in gamestate.possible_actions: # this will reach into GameState and run the function directly in its Player class. gamestate_copy = deepcopy(gamestate) action_name = str(action.__name__) action_function = getattr(gamestate_copy.player, action_name) # this has to be a copy or we don't branch correctly. if action_function(): # print(action.__name__) LOG.debug(action_name) if LOG.level == logging.DEBUG: gamestate_copy.player.build_order.append({ "completed_action": action_name, "gamestate": gamestate_copy.debug_gamestate(), "player": gamestate_copy.debug_player(), "bases": gamestate_copy.debug_bases() }) else: # if NOT in debug mode, we get only practical information gamestate_copy.player.build_order.append({ "completed_action": action.__name__, "gamestate": gamestate_copy.debug_bases() }) # regardless of logging level, we recurse normally. gamestate_copy.remaining_ticks -= 1 gamestate_copy.tick() run_simulation(output, gamestate_copy, None) else: # if the action fails if LOG.level == logging.DEBUG: gamestate_copy.player.build_order.append({ "target": action_name, "gamestate": gamestate_copy.debug_gamestate(), "player": gamestate_copy.debug_player(), "bases": gamestate_copy.debug_bases() }) else: # if NOT in debug mode, we get only practical information gamestate_copy.player.build_order.append({ "target": action.__name__, "gamestate": gamestate_copy.debug_bases() }) gamestate_copy.remaining_ticks -= 1 gamestate_copy.tick() run_simulation(output, gamestate_copy, action) else: gamestate_copy = deepcopy(gamestate) action_name = str(target.__name__) action_function = getattr(gamestate_copy.player, action_name) # this has to be a copy or we don't branch correctly. if action_function( ): # run the target function. If successful, proceed here. if LOG.level == logging.DEBUG: gamestate_copy.player.build_order.append({ "completed_action": action_name, "gamestate": gamestate_copy.debug_gamestate(), "player": gamestate_copy.debug_player(), "bases": gamestate_copy.debug_bases() }) else: # if NOT in debug mode, we get only practical information gamestate_copy.player.build_order.append({ "completed_action": action_name, "gamestate": gamestate_copy.debug_bases() }) gamestate_copy.remaining_ticks -= 1 gamestate_copy.tick() # because the action worked this time, we set it back to None. run_simulation(output, gamestate_copy, None) else: # if the target fails, keep it set as the target and recurse until it is possible. if LOG.level == logging.DEBUG: gamestate_copy.player.build_order.append({ "target": action_name, "gamestate": gamestate_copy.debug_gamestate(), "player": gamestate_copy.debug_player(), "bases": gamestate_copy.debug_bases() }) else: # if NOT in debug mode, we get only practical information gamestate_copy.player.build_order.append({ "target": action_name, "gamestate": gamestate_copy.debug_bases() }) gamestate_copy.remaining_ticks -= 1 gamestate_copy.tick() run_simulation(output, gamestate_copy, target)
run_simulation(output, gamestate_copy, target) # ============================================================== # # SECTION: Main # # ============================================================== # if __name__ == "__main__": goal_units = ["zergling", "zergling", "zergling", "zergling"] max_ticks = 16 output = [] player = Player( Race.ZERG, minerals=50, gas=0, goal_units=goal_units, current_units=[], buildings=[], bases=[Base(12, Race.ZERG, "normal", "normal", 2, False, True)], build_order=[], supply=3, required_tech=get_all_required_tech(goal_units) + goal_units, remaining_ticks=max_ticks) gamestate = GameState(remaining_ticks=max_ticks, player=player) # store results in output simulation = run_simulation(output, gamestate, None) # print(simulation) # print(output) # print(len(output)) LOG.info(json.dumps(output))