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 _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 _help_system(self): tool.doDanger(self.data, self.client, self.db) pirate_influenced_systems = tool.findInfluence(self.data, self.client, self.db, Rules.pirateInfluenceRange, self.data.pirateSystems) one_fighter_mp = self.player.shipDesigns[self.designs["fighter"]].combatPwr for system_id in set(self.data.endangeredSystems) - set(pirate_influenced_systems): mil_pwr, shipQuantity = self.data.endangeredSystems[system_id] mil_pwr = -mil_pwr if system_id in self.data.myMPPerSystem: mil_pwr += self.data.myMPPerSystem[system_id] if mil_pwr < 0: system = self.db[system_id] nearest = tool.findNearest(self.db, system, self.data.mySystems, 99999, 20)[1:] for temp_id in nearest: if temp_id in self.data.myMPPerSystem: temp_mp = self.data.myMPPerSystem[temp_id] else: temp_mp = 0 self.data.myMPPerSystem[temp_id] = 0 if temp_id in self.data.endangeredSystems: a, b = self.data.endangeredSystems[temp_id] temp_mp -= a * 1.5 if temp_mp <= 0: continue orig = temp_mp = min(-mil_pwr, temp_mp) * 1.25 # this is just prototype, working only with Fighters quantity = int(math.ceil(temp_mp / float(one_fighter_mp))) if quantity == 0: continue ships_left, mil_pwrSend = tool.orderFromSystem(self.data, self.client, self.db, {self.designs["fighter"]:quantity}, temp_id, Const.FLACTION_MOVE, system_id, None) mil_pwr += mil_pwrSend self.data.myMPPerSystem[temp_id] -= mil_pwrSend if mil_pwr > 0: break
def _followup_attack(self, fleet): max_range = tool.subfleetMaxRange(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 _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.subfleetMaxRange(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 _return_strays(self): for fleetID in self.data.idleFleets: fleet = self.db[fleetID] # fleets orbiting in systems not belonging to the self.player if fleet.orbiting and fleet.orbiting not in self.data.mySystems: nearest = tool.findNearest(self.db, fleet, self.data.mySystems, 99999, 1) if len(nearest): targetID = nearest[0] tool.orderFleet(self.client, self.db, fleetID, Const.FLACTION_MOVE, targetID, None)
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 _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.subfleetMaxRange(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 _return_strays(self): for fleetID in self.data.idleFleets: fleet = self.db[fleetID] # fleets orbiting in systems not belonging to the self.player if fleet.orbiting and fleet.orbiting not in self.data.mySystems: nearest = tool.findNearest(self.db, fleet, self.data.mySystems, 99999, 1) if len(nearest): targetID = nearest[0] tool.orderFleet(self.client, self.db, fleetID, Const.FLACTION_MOVE, targetID, 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 _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.subfleetMaxRange(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.subfleetMaxRange(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
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.subfleetMaxRange(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 _help_system(self): tool.doDanger(self.data, self.client, self.db) pirate_influenced_systems = tool.findInfluence( self.data, self.client, self.db, Rules.pirateInfluenceRange, self.data.pirateSystems) one_fighter_mp = self.player.shipDesigns[ self.designs["fighter"]].combatPwr for system_id in set( self.data.endangeredSystems) - set(pirate_influenced_systems): mil_pwr, shipQuantity = self.data.endangeredSystems[system_id] mil_pwr = -mil_pwr if system_id in self.data.myMPPerSystem: mil_pwr += self.data.myMPPerSystem[system_id] if mil_pwr < 0: system = self.db[system_id] nearest = tool.findNearest(self.db, system, self.data.mySystems, 99999, 20)[1:] for temp_id in nearest: if temp_id in self.data.myMPPerSystem: temp_mp = self.data.myMPPerSystem[temp_id] else: temp_mp = 0 self.data.myMPPerSystem[temp_id] = 0 if temp_id in self.data.endangeredSystems: a, b = self.data.endangeredSystems[temp_id] temp_mp -= a * 1.5 if temp_mp <= 0: continue orig = temp_mp = min(-mil_pwr, temp_mp) * 1.25 # this is just prototype, working only with Fighters quantity = int(math.ceil(temp_mp / float(one_fighter_mp))) if quantity == 0: continue ships_left, mil_pwrSend = tool.orderFromSystem( self.data, self.client, self.db, {self.designs["fighter"]: quantity}, temp_id, Const.FLACTION_MOVE, system_id, None) mil_pwr += mil_pwrSend self.data.myMPPerSystem[temp_id] -= mil_pwrSend if mil_pwr > 0: break
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
def _return_fleet(self, fleet): nearest_sys_ids = tool.findNearest(self.db, fleet, self.data.mySystems) 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)
def _return_fleet(self, fleet): nearest_sys_ids = tool.findNearest(self.db, fleet, self.data.mySystems) 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)