-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtech.py
117 lines (91 loc) · 4.25 KB
/
tech.py
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
from __future__ import annotations
from openciv.engine.managers.base import BaseManager
from collections import OrderedDict
from openciv.gameplay.tech import Tech
from typing import List
from openciv.engine.mixins.callbacks import CallbacksMixin
from openciv.engine.additions.pyload import PyLoad
class TechManager(BaseManager, CallbacksMixin):
def __init__(self, technology_folders: List = [], *args, **kwargs):
BaseManager.__init__(self, *args, **kwargs)
CallbacksMixin.__init__(self)
self.queue: OrderedDict[int, Tech] = OrderedDict()
self.registered_techs: List[Tech] = []
self._needed_science: int = 1
self._current_science_pool: int = 0
self._tech_tree = None
self.declare_events()
self.process_folders(technology_folders)
@property
def needed_science(self) -> int:
return self._needed_science
@needed_science.setter
def needed_science(self, value: int):
self._needed_science = value
@property
def current_science(self) -> int:
return self._current_science_pool
@current_science.setter
def current_science(self, value: int):
self._current_science_pool = value
def log(self):
parent: "GameManager" = self.parent() # noqa F821
return parent.log()
def declare_events(self):
self._declare_event("on_tech_researched")
self._declare_event("on_tech_queue_updated")
self._declare_event("on_tech_added")
self._declare_event("on_tech_reorder")
self._declare_event("on_science_added")
self._declare_event("on_science_removed")
def process_folders(self, folders: List):
def process_folder(folder):
classes = PyLoad.load_classes(folder)
for _class in classes:
if not isinstance(_class, Tech):
self.log().gameplay.warning(f"Class is not a tech: {_class}")
continue
self.log().gameplay.debug(f"Adding tech: {_class}")
self.add_tech(_class)
for folder in folders:
process_folder(folder)
def add_tech_to_queue(self, tech: Tech) -> None:
self.queue[len(self.queue)] = tech
self.trigger_callback("on_tech_queue_updated")
def reorder(self) -> None:
self.queue = OrderedDict((i, tech) for i, tech in enumerate(self.queue.values()))
self.trigger_callback("on_tech_reorder")
def do_first_queue(self, tech: Tech) -> None:
self.queue = OrderedDict([(0, tech)] + [(i + 1, t) for i, t in enumerate(self.queue.values())])
self.trigger_callback("on_tech_queue_updated")
def delete_tech(self, tech: Tech) -> None:
self.registered_techs.remove(tech)
def add_tech(self, tech: Tech) -> None:
self.registered_techs.append(tech)
self.trigger_callback("on_tech_added")
def current_tech(self) -> Tech | None:
return self.queue[0] if self.queue else None
def next_tech(self) -> Tech | None:
return self.queue[1] if len(self.queue) > 1 else None
def process_queue(self) -> None:
if self.queue:
self.queue.pop(0)
self.reorder()
self.trigger_callback("on_tech_queue_updated")
def complete_research(self, auto_complete_tech: bool = True) -> None:
self._current_science_pool -= self._needed_science
self.process_queue()
self.trigger_callback("on_tech_researched")
# We check if we can still complete more for example if the player gets a large amount of science it could complete 1 or more techs.
if auto_complete_tech and self._current_science_pool >= self._needed_science:
self.complete_research()
def add_science(self, science: int, auto_complete_tech: bool = True) -> None:
self._current_science_pool += science
self.trigger_callback("on_science_added")
while auto_complete_tech and self._current_science_pool >= self._needed_science and self.queue:
self.complete_research(auto_complete_tech=auto_complete_tech)
def remove_science(self, science: int):
self._current_science_pool -= science
if self._current_science_pool < 0:
self._current_science_pool = 0
self.trigger_callback("on_science_removed")