def _logistics_manager(self): for system_id in self.data.mySystems - self.data.myRelevantSystems: system = self.db[system_id] for fleet_id in set(system.fleets) & self.data.idleFleets: fleet = self.db[fleet_id] subfleet = tool.getSubfleet(fleet, {1:0, 4:0}, False) if len(subfleet): fleet_range = tool.subfleetMarange(self.client, self.db, {1:0, 4:0}, fleet_id) relevant_sys_id = tool.findNearest(self.db, system, self.data.myRelevantSystems, fleet_range) if relevant_sys_id: relevant_sys_id = relevant_sys_id[0] fleet, new_fleet, my_fleets = tool.orderPartFleet(self.client, self.db, {1:0, 4:0}, True, fleet_id, Const.FLACTION_MOVE, relevant_sys_id, None) self.data.idleFleets -= set([fleet_id]) else: min_dist = fleet_range min_dist_sys_id = None min_dist_rel = self.data.distanceToRelevance[system_id] for temp_id, dist in self.data.distanceToRelevance.items(): temp = self.db[temp_id] distance = math.hypot(temp.x - system.x, temp.y - system.y) if distance < min_dist and dist < min_dist_rel: min_dist = distance min_dist_sys_id = temp_id min_dist_rel = dist if min_dist_sys_id: fleet, new_fleet, my_fleets = tool.orderPartFleet(self.client, self.db, {1:0, 4:0}, True, fleet_id, Const.FLACTION_MOVE, min_dist_sys_id, None) self.data.idleFleets -= set([fleet_id])
def _colonize_free_systems(self, valid_systems, colony_design_id): should_repeat = False colony_fleets = self.data.myFleetsWithDesign.get( colony_design_id, set()) for fleet_id in copy.copy(colony_fleets & self.data.idleFleets): max_range = tool.subfleetMarange(self.client, self.db, {colony_design_id: 1}, fleet_id) nearest = tool.findNearest(self.db, self.db[fleet_id], valid_systems, max_range) if len(nearest) > 0: system_id = nearest[0] system = self.db[system_id] target_id = self._find_best_planet(system.planets) fleet, new_fleet, my_fleets = tool.orderPartFleet( self.client, self.db, {colony_design_id: 1}, True, fleet_id, Const.FLACTION_DEPLOY, target_id, colony_design_id) self.data.myFleetSheets[fleet_id][colony_design_id] -= 1 if self.data.myFleetSheets[fleet_id][colony_design_id] == 0: del self.data.myFleetSheets[fleet_id][colony_design_id] colony_fleets.remove(fleet_id) else: should_repeat = True self.data.freeSystems.remove(system_id) valid_systems.remove(system_id) return should_repeat
def _followup_attack(self, fleet): max_range = tool.subfleetMarange(self.client, self.db, None, fleet.oid) nearest_sys_ids = tool.findNearest(self.db, fleet, self.data.enemySystems & self.data.relevantSystems, max_range) if len(nearest_sys_ids): nearest_sys_id = nearest_sys_ids[0] tool.orderFleet(self.client, self.db, fleet.oid, Const.FLACTION_MOVE, nearest_sys_id, None) else: self._return_fleet(fleet)
def _retreat_fleet(self, fleet): max_range = tool.subfleetMarange(self.client, self.db, None, fleet.oid) nearest_sys_ids = tool.findNearest(self.db, fleet, self.data.mySystems) if len(nearest_sys_ids): nearest_sys_id = nearest_sys_ids[0] target = self.db[nearest_sys_id] distance = math.hypot(target.x-fleet.x, target.y-fleet.y) if distance >= max_range: tool.orderFleet(self.client, self.db, fleet.oid, Const.FLACTION_MOVE, nearest_sys_id, None)
def _commence_attack(self, fleet): sheet = tool.getFleetSheet(fleet) ships = {} ships[3] = min(sheet[1], sheet[2], sheet[3]) ships[1] = ships[2] = ships[3] ships[4] = 1 max_range = tool.subfleetMarange(self.client, self.db, ships, fleet.oid) nearest_sys_ids = tool.findNearest(self.db, fleet, self.data.enemySystems & self.data.relevantSystems, max_range * 0.45) if len(nearest_sys_ids): nearestSys = nearest_sys_ids[0] tool.orderPartFleet(self.client, self.db, ships, False, fleet.oid, Const.FLACTION_MOVE, nearestSys, None)
def _recall_to_upgrade(self, service_centers, outdated_fleets, obsolete_designs): for fleet in outdated_fleets: if fleet.orbiting in service_centers: continue subfleet_sheet = {}.fromkeys(obsolete_designs, 0) max_range = tool.subfleetMarange(self.client, self.db, subfleet_sheet, fleet.oid) nearest = tool.findNearest(self.db, fleet, service_centers, max_range) if not nearest: continue nearest_service = nearest[0] fleet, new_fleet, my_fleets = tool.orderPartFleet( self.client, self.db, subfleet_sheet, True, fleet.oid, Const.FLACTION_MOVE, nearest_service, None)
def _attack_manager(self): for fleet_id in self._get_attack_fleets(): fleet = self.db[fleet_id] # send the attack fleet, if in range sheet = tool.getFleetSheet(fleet) sowers = sheet[4] swarmers = min(sheet[1], math.ceil(sowers * 1.5)) max_range = 0.8 * tool.subfleetMarange(self.client, self.db, {1:swarmers, 4:sowers}, fleet_id) # four nearest systems are considered, with probability to be chosen based on order nearest = tool.findNearest(self.db, fleet, self.data.enemySystems, max_range, 4) if len(nearest): # range is adjusted to flatten probabilities a bit probability_map = map(lambda x: x ** 2, range(2 + len(nearest), 2, -1)) target = Utils.weightedRandom(nearest, probability_map) fleet, new_fleet, my_fleets = tool.orderPartFleet(self.client, self.db, {1:swarmers, 4:sowers}, True, fleet_id, Const.FLACTION_MOVE, target, None)
def _explore(self, explorer_design_id): should_repeat = False explorer_fleets = self.data.myFleetsWithDesign.get( explorer_design_id, set()) for fleet_id in copy.copy(explorer_fleets & self.data.idleFleets): max_range = tool.subfleetMarange(self.client, self.db, {explorer_design_id: 1}, fleet_id) nearest = tool.findNearest(self.db, self.db[fleet_id], self.data.unknownSystems, max_range) if len(nearest) > 0: system_id = nearest[0] # send the fleet fleet, new_fleet, my_fleets = tool.orderPartFleet( self.client, self.db, {explorer_design_id: 1}, True, fleet_id, Const.FLACTION_MOVE, system_id, None) self.data.myFleetSheets[fleet_id][explorer_design_id] -= 1 if self.data.myFleetSheets[fleet_id][explorer_design_id] == 0: del self.data.myFleetSheets[fleet_id][explorer_design_id] explorer_fleets.remove(fleet_id) else: should_repeat = True self.data.unknownSystems.remove(system_id) return should_repeat