1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
import os
from collections.abc import Generator
def get_code_location(generator: Generator) -> str:
if generator.gi_yieldfrom:
return get_code_location(generator.gi_yieldfrom)
return f"{os.path.basename(generator.gi_code.co_filename)}:{generator.gi_frame.f_lineno}"
def get_item_with_location(generator: Generator) -> Generator:
for x in generator:
yield get_code_location(generator), x
def handle_interfaces_mismatch():
"""
Handle cases when AI and universe generation interfaces mismatch.
"""
try:
import freeorion as fo
universe = fo.get_universe()
empire_of_first_ai = fo.get_empire(2)
galaxy_data = fo.get_galaxy_setup_data()
return fo, universe, empire_of_first_ai, galaxy_data
except ImportError:
pass
try:
import freeOrionAIInterface as fo
universe = fo.getUniverse()
empire_of_first_ai = fo.getEmpire(2)
galaxy_data = fo.getGalaxySetupData()
return fo, universe, empire_of_first_ai, galaxy_data
except ImportError:
pass
def get_common_instances() -> Generator:
"""
Get instances for objects that are the same to AI and universe generation interfaces.
"""
fo, universe, empire_of_first_ai, galaxy_data = handle_interfaces_mismatch()
yield universe
yield galaxy_data
planet = universe.getPlanet(empire_of_first_ai.capitalID)
yield planet
yield universe.getSystem(planet.systemID)
tech = fo.getTech("SHP_WEAPON_2_1")
yield tech
yield tech.unlockedItems[0]
yield fo.getGameRules()
ship_hull = fo.getShipHull("SH_XENTRONIUM")
yield ship_hull
yield fo.getSpecies("SP_CRAY")
fleets_int_vector = universe.fleetIDs
fleet = universe.getFleet(list(fleets_int_vector)[0])
yield fleet
fields_ids = universe.fieldIDs
field = universe.getField(fields_ids[0])
yield field
yield fo.getFieldType("FLD_ION_STORM")
yield fo.getBuildingType("BLD_SHIPYARD_BASE")
yield fo.getShipPart("SR_WEAPON_1_1")
yield fo.getSpecial("MODERATE_TECH_NATIVES_SPECIAL")
yield fo.getShipHull("SH_XENTRONIUM")
ship = universe.getShip(list(universe.shipIDs)[0])
yield ship
design = fo.getShipDesign(ship.designID)
yield design
yield fo.diplomaticMessage(1, 2, fo.diplomaticMessageType.acceptPeaceProposal)
yield empire_of_first_ai
yield empire_of_first_ai.productionQueue
yield empire_of_first_ai.researchQueue
planet = universe.getPlanet(empire_of_first_ai.capitalID)
yield planet
meter = planet.getMeter(fo.meterType.population)
yield meter
building = list(planet.buildingIDs)[0]
yield universe.getBuilding(building)
yield fo.getPolicy("PLC_LIBERTY")
common_classes_to_exclude = {
"popCenter", # parent class, it's not possible to get instance
"resourceCenter", # parent class, it's not possible to get instance
"diplomaticStatusUpdate", # this item is not used in generate orders/universe
"universeObject", # parent class, it's not possible to get instance
}
classes_to_exclude_from_universe = {
"productionQueueElement", # not applicable
"researchQueueElement", # not applicable
*common_classes_to_exclude,
}
classes_to_exclude_from_ai = {
*common_classes_to_exclude,
}
|