File: claimed_stars.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 (45 lines) | stat: -rw-r--r-- 1,463 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
import freeOrionAIInterface as fo
from collections import defaultdict
from collections.abc import Mapping

import AIstate
from common.fo_typing import SystemId
from freeorion_tools.caching import cache_for_current_turn


def has_claimed_star(*stars: "fo.starType") -> bool:
    """
    Return `True` if at least one star of that type is claimed.
    """
    return bool(set(_get_claimed_stars()).intersection(stars))


def is_system_star_claimed(system: "fo.system"):
    return system.systemID in _get_claimed_stars()[system.starType]


def count_claimed_stars(star_type: "fo.starType") -> int:
    """
    Count claimed starts of specified type.
    """
    return len(_get_claimed_stars()[star_type])


@cache_for_current_turn
def _get_claimed_stars() -> Mapping["fo.starType", set[SystemId]]:
    """
    Return dictionary of star type: list of colonised and planned to be colonized systems.
    Start type converted to int because `cache_by_turn` store its value in savegame
    and boost objects are not serializable.
    """
    claimed_stars = defaultdict(set)

    claimed_stars.update({int(s_type): set(AIstate.empireStars[s_type]) for s_type in AIstate.empireStars})

    universe = fo.getUniverse()
    for sys_id in set(AIstate.colonyTargetedSystemIDs + AIstate.outpostTargetedSystemIDs):
        t_sys = universe.getSystem(sys_id)
        if not t_sys:
            continue
        claimed_stars[t_sys.starType].add(sys_id)
    return claimed_stars