File: colony_builders.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 (78 lines) | stat: -rw-r--r-- 2,474 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
from collections.abc import Mapping, Sequence
from typing import Union

import AIDependencies
from common.fo_typing import PlanetId, SpeciesName
from empire.survey_lock import survey_universe_lock
from freeorion_tools import tech_is_complete, tech_soon_available
from freeorion_tools.caching import cache_for_current_turn


def set_colony_builders(species_name: SpeciesName, yards: Sequence[PlanetId]):
    """
    Add planet where you can build colonies for species.

    Warning! Temporal coupling.
    All calls of this function should be done before using of this information.
    """
    empire_colonizers = _get_colony_builders()
    empire_colonizers.setdefault(species_name, []).extend(yards)


@survey_universe_lock
def can_build_colony_for_species(species_name: Union[SpeciesName, str]):
    return species_name in get_colony_builders()


@survey_universe_lock
def get_colony_builder_locations(species_name: SpeciesName) -> list[PlanetId]:
    return get_colony_builders()[species_name]


@survey_universe_lock
def can_build_only_sly_colonies():
    """
    Return true if empire could build only SP_SLY colonies.

    This could be possible only on early stage, when no other races are conquered
    and techs like EXOBOTS are not invented.

    This race has poor supply and live only on gas giants.
    """
    return list(get_colony_builders()) == ["SP_SLY"]


@survey_universe_lock
def get_colony_builders() -> Mapping[SpeciesName, list[PlanetId]]:
    """
    Return map from the species to list of the planet where you could build a colony ship with it.
    """
    return _get_colony_builders()


@cache_for_current_turn
def get_extra_colony_builders() -> list[str]:
    """
    Returns species the empire can build without having a colony, i.e. Exobots, if (almost) researched, plus
    extinct species that has been enabled.
    """
    ret = []
    if tech_soon_available(AIDependencies.EXOBOT_TECH_NAME, 1):
        ret.append("SP_EXOBOT")
    for spec_name in AIDependencies.EXTINCT_SPECIES:
        if tech_is_complete("TECH_COL_" + spec_name):
            ret.append("SP_" + spec_name)
    return ret


@cache_for_current_turn
def _get_colony_builders() -> dict[SpeciesName, list[PlanetId]]:
    """
    Return mutable state.
    """
    colony_build_locations = {}

    # get it into colonizer list even if no colony yet
    for species in get_extra_colony_builders():
        colony_build_locations[species] = []
    return colony_build_locations