File: _planet_state.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 (126 lines) | stat: -rw-r--r-- 4,248 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
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
122
123
124
125
126
import freeOrionAIInterface as fo
from collections.abc import Mapping
from typing import Callable

import AIDependencies
from common.fo_typing import PlanetId, SpeciesName, SystemId
from freeorion_tools import ReadOnlyDict
from freeorion_tools.caching import cache_for_current_turn


class PlanetInfo:
    def __init__(self, pid: PlanetId):
        universe = fo.getUniverse()
        planet = universe.getPlanet(pid)
        self.pid: PlanetId = planet.id
        self.species_name = planet.speciesName
        self.owner = planet.owner
        self.system_id: SystemId = planet.systemID

    def is_colonized(self) -> bool:
        return self.owner == fo.empireID() and self.species_name

    def is_owned_by_empire(self) -> bool:
        return self.owner == fo.empireID()

    def has_outpost(self) -> bool:
        return self.owner == fo.empireID() and not self.species_name

    def is_not_owned(self) -> bool:
        return self.owner == AIDependencies.INVALID_ID and not self.species_name


@cache_for_current_turn
def _get_planets_info() -> Mapping[PlanetId, PlanetInfo]:
    universe = fo.getUniverse()
    planets = (universe.getPlanet(pid) for pid in universe.planetIDs)
    return {planet.id: PlanetInfo(planet.id) for planet in planets}


def _get_system_planets_map(planet_filter: Callable[[PlanetInfo], bool]) -> Mapping[SystemId, tuple[PlanetId]]:
    result = {}
    for planet_info in (planet_info for planet_info in _get_planets_info().values() if planet_filter(planet_info)):
        result.setdefault(planet_info.system_id, []).append(planet_info.pid)
    return ReadOnlyDict({k: tuple(v) for k, v in result.items()})


@cache_for_current_turn
def get_owned_planets() -> Mapping[SystemId, tuple[PlanetId]]:
    """
    Return map from system id to list of planet ids with colony or outpost.
    """
    return _get_system_planets_map(PlanetInfo.is_owned_by_empire)


@cache_for_current_turn
def get_colonized_planets() -> Mapping[SystemId, tuple[PlanetId]]:
    """
    Return map from system id to list of planet ids with colony only.
    """
    return _get_system_planets_map(PlanetInfo.is_colonized)


def get_owned_planets_in_system(sys_id: SystemId) -> tuple[PlanetId]:
    """
    Return list of planet ids with colony or outpost in the system.
    """
    return get_owned_planets().get(sys_id, ())


def get_colonized_planets_in_system(sys_id: SystemId) -> tuple[PlanetId]:
    """
    Return list of planets with colony in the system.
    """
    return get_colonized_planets().get(sys_id, ())


@cache_for_current_turn
def get_inhabited_planets() -> frozenset[PlanetId]:
    """
    Return frozenset of empire planet ids with species.
    """
    return frozenset(planet_info.pid for planet_info in _get_planets_info().values() if planet_info.is_colonized())


@cache_for_current_turn
def get_empire_outposts() -> tuple[PlanetId]:
    all_planets = _get_planets_info().values()
    return tuple(planet_info.pid for planet_info in all_planets if planet_info.has_outpost())


@cache_for_current_turn
def get_all_empire_planets() -> tuple[PlanetId]:
    all_planets = _get_planets_info().values()
    return tuple(planet_info.pid for planet_info in all_planets if planet_info.is_owned_by_empire())


@cache_for_current_turn
def get_empire_planets_by_species() -> Mapping[SpeciesName, list[PlanetId]]:
    """
    Return dict for empire from species to list of planet ids.
    """
    result = {}
    colonized = (planet_info for planet_info in _get_planets_info().values() if planet_info.is_colonized())
    for planet in colonized:
        result.setdefault(planet.species_name, []).append(planet.pid)
    return result


def get_unowned_empty_planets() -> set[PlanetId]:
    """
    Return the set of planets that are not owned by any player and have no natives.
    """
    return {planet_info.pid for planet_info in _get_planets_info().values() if planet_info.is_not_owned()}


def get_number_of_colonies() -> int:
    return len(get_inhabited_planets())


def get_empire_planets_with_species(species_name: str) -> tuple[PlanetId]:
    """
    Return empire planet ids with species.
    """
    if not species_name:
        return tuple()
    return tuple(get_empire_planets_by_species().get(species_name, []))