Exemple #1
0
def _print_empire_species_roster():
    """Print empire species roster in table format to log."""
    grade_map = {"ULTIMATE": "+++", "GREAT": "++", "GOOD": "+", "AVERAGE": "o", "BAD": "-", "NO": "---"}

    grade_tags = [
        Tags.INDUSTRY,
        Tags.RESEARCH,
        Tags.POPULATION,
        Tags.SUPPLY,
        Tags.WEAPONS,
        Tags.ATTACKTROOPS,
    ]

    grade_tags_names = {
        Tags.INDUSTRY: "Ind.",
        Tags.RESEARCH: "Res.",
        Tags.POPULATION: "Pop.",
        Tags.SUPPLY: "Supply",
        Tags.WEAPONS: "Pilot",
        Tags.ATTACKTROOPS: "Troop",
    }

    species_table = Table(
        Text("species"),
        Sequence("PIDs"),
        Bool("Colonize"),
        Text("Shipyards"),
        *[Text(grade_tags_names[v]) for v in grade_tags],
        Sequence("Tags"),
        table_name="Empire species roster Turn %d" % fo.currentTurn(),
    )
    for species_name, planet_ids in get_empire_planets_by_species().items():
        species_tags = fo.getSpecies(species_name).tags
        number_of_shipyards = len(get_ship_builder_locations(species_name))
        species_table.add_row(
            species_name,
            planet_ids,
            can_build_colony_for_species(species_name),
            number_of_shipyards,
            *[grade_map.get(get_species_tag_grade(species_name, tag).upper(), "o") for tag in grade_tags],
            [tag for tag in species_tags if not any(s in tag for s in grade_tags) and "PEDIA" not in tag],
        )
    species_table.print_table(info)
Exemple #2
0
    def is_valid(self) -> bool:
        """
        Check the colonization plan for validity, i.e. if it could be executed in the future.

        The plan is valid if it is possible to outpust the target planet
        and if the planet envisioned to build the outpost bases can still do so.
        """
        universe = fo.getUniverse()

        # make sure target is valid
        target = universe.getPlanet(self.target)
        if target is None or (not target.unowned) or target.speciesName:
            return False

        # make sure source is valid
        source = universe.getPlanet(self.source)
        if not (source and source.ownedBy(fo.empireID()) and can_build_colony_for_species(source.speciesName)):
            return False

        # appears to be valid
        return True
Exemple #3
0
def get_colony_fleets():
    """examines known planets, collects various colonization data, to be later used to send colony fleets"""
    universe = fo.getUniverse()
    empire = fo.getEmpire()

    colonization_timer.start("Identify Existing colony/outpost targets")
    colony_targeted_planet_ids = FleetUtilsAI.get_targeted_planet_ids(universe.planetIDs, MissionType.COLONISATION)
    all_colony_targeted_system_ids = PlanetUtilsAI.get_systems(colony_targeted_planet_ids)
    colony_fleet_ids = FleetUtilsAI.get_empire_fleet_ids_by_role(MissionType.COLONISATION)
    num_colony_fleets = len(FleetUtilsAI.extract_fleet_ids_without_mission_types(colony_fleet_ids))

    outpost_targeted_planet_ids = FleetUtilsAI.get_targeted_planet_ids(universe.planetIDs, MissionType.OUTPOST)
    outpost_targeted_planet_ids.extend(
        FleetUtilsAI.get_targeted_planet_ids(universe.planetIDs, MissionType.ORBITAL_OUTPOST)
    )
    all_outpost_targeted_system_ids = PlanetUtilsAI.get_systems(outpost_targeted_planet_ids)
    outpost_fleet_ids = FleetUtilsAI.get_empire_fleet_ids_by_role(MissionType.OUTPOST)
    num_outpost_fleets = len(FleetUtilsAI.extract_fleet_ids_without_mission_types(outpost_fleet_ids))

    debug("Colony Targeted SystemIDs: %s" % all_colony_targeted_system_ids)
    debug("Colony Targeted PlanetIDs: %s" % colony_targeted_planet_ids)
    debug(colony_fleet_ids and "Colony Fleet IDs: %s" % colony_fleet_ids or "Available Colony Fleets: 0")
    debug("Colony Fleets Without Missions: %s" % num_colony_fleets)
    debug("")
    debug("Outpost Targeted SystemIDs: %s" % all_outpost_targeted_system_ids)
    debug("Outpost Targeted PlanetIDs: %s" % outpost_targeted_planet_ids)
    debug(outpost_fleet_ids and "Outpost Fleet IDs: %s" % outpost_fleet_ids or "Available Outpost Fleets: 0")
    debug("Outpost Fleets Without Missions: %s" % num_outpost_fleets)
    debug("")

    # export targeted systems for other AI modules
    AIstate.colonyTargetedSystemIDs = all_colony_targeted_system_ids
    AIstate.outpostTargetedSystemIDs = all_outpost_targeted_system_ids

    colonization_timer.start("Identify colony base targets")
    # keys are sets of ints; data is doubles
    available_pp = {tuple(el.key()): el.data() for el in empire.planetsWithAvailablePP}

    avail_pp_by_sys = {}
    for p_set in available_pp:
        avail_pp_by_sys.update([(sys_id, available_pp[p_set]) for sys_id in set(PlanetUtilsAI.get_systems(p_set))])

    evaluated_colony_planet_ids = list(
        get_unowned_empty_planets().union(get_empire_outposts()) - set(colony_targeted_planet_ids)
    )  # places for possible colonyBase

    aistate = get_aistate()
    outpost_base_manager = aistate.orbital_colonization_manager

    for pid in evaluated_colony_planet_ids:  # TODO: reorganize
        planet = universe.getPlanet(pid)
        if not planet:
            continue
        sys_id = planet.systemID
        for pid2 in get_colonized_planets_in_system(sys_id):
            planet2 = universe.getPlanet(pid2)
            if not (planet2 and can_build_colony_for_species(planet2.speciesName)):
                continue
            if planet.unowned:
                outpost_base_manager.create_new_plan(pid, pid2)

    colonization_timer.start("Initiate outpost base construction")

    reserved_outpost_base_targets = outpost_base_manager.get_targets()
    debug("Current qualifyingOutpostBaseTargets: %s" % reserved_outpost_base_targets)
    outpost_base_manager.build_bases()
    colonization_timer.start("Evaluate Primary Colony Opportunities")

    evaluated_outpost_planet_ids = list(
        get_unowned_empty_planets()
        - set(outpost_targeted_planet_ids)
        - set(colony_targeted_planet_ids)
        - set(reserved_outpost_base_targets)
    )

    evaluated_colony_planets = assign_colonisation_values(evaluated_colony_planet_ids, MissionType.COLONISATION, None)
    colonization_timer.stop("Evaluate %d Primary Colony Opportunities" % (len(evaluated_colony_planet_ids)))
    colonization_timer.start("Evaluate All Colony Opportunities")
    _all_colony_opportunities.clear()
    _all_colony_opportunities.update(
        assign_colonisation_values(evaluated_colony_planet_ids, MissionType.COLONISATION, None, [], True)
    )
    colonization_timer.start("Evaluate Outpost Opportunities")

    sorted_planets = list(evaluated_colony_planets.items())
    sorted_planets.sort(key=itemgetter(1), reverse=True)

    _print_colony_candidate_table(sorted_planets, show_detail=False)

    sorted_planets = [(planet_id, score[:2]) for planet_id, score in sorted_planets if score[0] > 0]
    # export planets for other AI modules
    aistate.colonisablePlanetIDs.clear()
    aistate.colonisablePlanetIDs.update(sorted_planets)

    evaluated_outpost_planets = assign_colonisation_values(evaluated_outpost_planet_ids, MissionType.OUTPOST, None)
    # if outposted planet would be in supply range, let outpost value be best of outpost value or colonization value
    for pid in set(evaluated_outpost_planets).intersection(evaluated_colony_planets):
        if get_planet_supply(pid, -99) >= 0:
            evaluated_outpost_planets[pid] = (
                max(evaluated_colony_planets[pid][0], evaluated_outpost_planets[pid][0]),
                "",
            )

    colonization_timer.stop()

    sorted_outposts = list(evaluated_outpost_planets.items())
    sorted_outposts.sort(key=itemgetter(1), reverse=True)

    _print_outpost_candidate_table(sorted_outposts)

    sorted_outposts = [(planet_id, score[:2]) for planet_id, score in sorted_outposts if score[0] > 0]
    # export outposts for other AI modules
    aistate.colonisableOutpostIDs.clear()
    aistate.colonisableOutpostIDs.update(sorted_outposts)
    colonization_timer.stop_print_and_clear()
Exemple #4
0
def _calculate_colonisation_priority():
    """Calculates the demand for colony ships by colonisable planets."""
    global allottedColonyTargets
    aistate = get_aistate()
    enemies_sighted = aistate.misc.get("enemies_sighted", {})
    galaxy_is_sparse = ColonisationAI.galaxy_is_sparse()
    total_pp = fo.getEmpire().productionPoints
    num_colonies = get_number_of_colonies()
    colony_growth_barrier = aistate.character.max_number_colonies()
    if num_colonies > colony_growth_barrier:
        return 0.0
    colony_cost, turns_to_build = ColonisationAI.colony_pod_cost_turns()

    mil_prio = aistate.get_priority(PriorityType.PRODUCTION_MILITARY)
    allotted_portion = [[[0.6, 0.8], [0.3, 0.4]],
                        [[0.8, 0.9],
                         [0.4, 0.6]]][galaxy_is_sparse][any(enemies_sighted)]
    allotted_portion = aistate.character.preferred_colonization_portion(
        allotted_portion)
    # if ( get_aistate().get_priority(AIPriorityType.PRIORITY_PRODUCTION_COLONISATION)
    # > 2 * get_aistate().get_priority(AIPriorityType.PRIORITY_PRODUCTION_MILITARY)):
    # allotted_portion *= 1.5
    if mil_prio < 100:
        allotted_portion *= 2
    elif mil_prio < 200:
        allotted_portion *= 1.5
    elif fo.currentTurn() > 100:
        allotted_portion *= 0.75**(num_colonies / 10.0)
    # allottedColonyTargets = 1+ int(fo.currentTurn()/50)
    allottedColonyTargets = 1 + int(
        total_pp * turns_to_build * allotted_portion / colony_cost)
    outpost_prio = aistate.get_priority(PriorityType.PRODUCTION_OUTPOST)

    # if have no SP_SLY, and have any outposts to build, don't build colony ships TODO: make more complex assessment
    if not can_build_colony_for_species("SP_SLY") and outpost_prio > 0:
        return 0.0
    min_score = ColonisationAI.MINIMUM_COLONY_SCORE
    minimal_top = min_score + 2  # one more than the conditional floor set by ColonisationAI.revise_threat_factor()
    minimal_opportunities = [
        species_name
        for (_, (score, species_name)) in aistate.colonisablePlanetIDs.items()
        if min_score < score <= minimal_top
    ]
    decent_opportunities = [
        species_name
        for (_, (score, species_name)) in aistate.colonisablePlanetIDs.items()
        if score > minimal_top
    ]
    minimal_planet_factor = 0.2  # count them for something, but not much
    num_colonisable_planet_ids = len(
        decent_opportunities
    ) + minimal_planet_factor * len(minimal_opportunities)
    if num_colonisable_planet_ids == 0:
        return 1

    colony_ship_ids = FleetUtilsAI.get_empire_fleet_ids_by_role(
        MissionType.COLONISATION)
    num_colony_ships = len(
        FleetUtilsAI.extract_fleet_ids_without_mission_types(colony_ship_ids))
    colonisation_priority = (
        60 * (1.0 + num_colonisable_planet_ids - num_colony_ships) /
        (num_colonisable_planet_ids + 1))

    if can_build_only_sly_colonies():
        colonisation_priority *= 2
    elif can_build_colony_for_species("SP_SLY"):
        colony_opportunities = minimal_opportunities + decent_opportunities
        colonisation_priority *= (1.0 + colony_opportunities.count("SP_SLY")
                                  ) / len(colony_opportunities)

    if colonisation_priority < 1:
        return 0
    return colonisation_priority