File: expansion_plans_implementation.py

package info (click to toggle)
freeorion 0.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 194,940 kB
  • sloc: cpp: 186,508; python: 40,969; ansic: 1,164; xml: 719; makefile: 32; sh: 7
file content (64 lines) | stat: -rw-r--r-- 2,897 bytes parent folder | download
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
import freeOrionAIInterface as fo
from collections import OrderedDict
from collections import OrderedDict as odict
from collections.abc import Iterable
from itertools import chain
from logging import debug

from aistate_interface import get_aistate
from common.fo_typing import PlanetId, SpeciesName
from EnumsAI import MissionType
from expansion_plans import expansion_plans_interface
from FleetUtilsAI import get_targeted_planet_ids


def initialise_expansion_plans():
    expansion_plans_interface._the_planner = ExpansionPlanner()


class ExpansionPlanner:
    @staticmethod
    def colonies_targeted() -> frozenset[PlanetId]:
        colony_ship_pids = get_targeted_planet_ids(fo.getUniverse().planetIDs, MissionType.COLONISATION)
        colony_building_pids = [e.locationID for e in fo.getEmpire().productionQueue if e.name.startswith("BLD_COL_")]
        debug(f"colonies_targeted: colony_ship_pids={colony_ship_pids}, colony_building_pids={colony_building_pids}")
        return frozenset(chain(colony_ship_pids, colony_building_pids))

    @staticmethod
    def outposts_targeted() -> frozenset[PlanetId]:
        outpost_ship_pids = get_targeted_planet_ids(fo.getUniverse().planetIDs, MissionType.OUTPOST)
        outpost_base_pids = get_targeted_planet_ids(fo.getUniverse().planetIDs, MissionType.ORBITAL_OUTPOST)
        debug(f"outpost_targeted: outpost_ship_pids={outpost_ship_pids}, outpost_base_pids={outpost_base_pids}")
        return frozenset(chain(outpost_ship_pids, outpost_base_pids))

    @staticmethod
    def get_colonisable_planet_ids(include_targeted: bool = False) -> OrderedDict[PlanetId, tuple[float, SpeciesName]]:
        if include_targeted:
            return get_aistate().colonisablePlanetIDs
        else:
            return odict(
                (pid, values)
                for (pid, values) in get_aistate().colonisablePlanetIDs.items()
                if pid not in expansion_plans_interface.colonies_targeted()  # interface function is cached
            )

    @staticmethod
    def get_colonisable_outpost_ids(include_targeted: bool = False) -> OrderedDict[PlanetId, tuple[float, SpeciesName]]:
        if include_targeted:
            return get_aistate().colonisableOutpostIDs
        else:
            return odict(
                (pid, values)
                for (pid, values) in get_aistate().colonisableOutpostIDs.items()
                if pid not in expansion_plans_interface.outposts_targeted()  # interface function is cached
            )

    @staticmethod
    def update_colonisable_planet_ids(new_list: Iterable) -> None:
        get_aistate().colonisablePlanetIDs.clear()
        get_aistate().colonisablePlanetIDs.update(new_list)

    @staticmethod
    def update_colonisable_outpost_ids(new_list: Iterable) -> None:
        get_aistate().colonisableOutpostIDs.clear()
        get_aistate().colonisableOutpostIDs.update(new_list)