massiv bugfixes around getter methods and ThermalCap outpost handling

This commit is contained in:
2026-11-13 02:11:38 +01:00
parent e90b4d2097
commit 149e98c543
+48 -26
View File
@@ -41,7 +41,7 @@ class ThermalCapWrapper:
clock = get_component("clock") clock = get_component("clock")
default_threshold = 0.98 default_threshold = 0.98
def __init__(self, cap: ThermalCap | str, threshold: float = 0.99, outpost: OutpostComponent | OutpostRef | str | None = None): def __init__(self, cap: ThermalCap | str, threshold: float = 0.99, outposts: list[OutpostComponent] | OutpostComponent | OutpostRef | str | None = None):
"""init wrapper for `ThermalCap` """init wrapper for `ThermalCap`
Args: Args:
@@ -51,7 +51,7 @@ class ThermalCapWrapper:
""" """
self.cap = cap self.cap = cap
self.threshold = threshold self.threshold = threshold
self.outpost = outpost self.outposts = outposts
self.steam_out = cap.steam_out self.steam_out = cap.steam_out
def run(self): def run(self):
@@ -83,20 +83,24 @@ class ThermalCapWrapper:
"""fills the given `GasTank` until full""" """fills the given `GasTank` until full"""
if not isinstance(storage, GasTank): return if not isinstance(storage, GasTank): return
if storage.is_full(): if storage.is_full():
debug(f"{storage.id=} if full, disconnect.")
self.cap.set_throttle(0) self.cap.set_throttle(0)
self.steam_out.disconnect() self.steam_out.disconnect()
else: else:
debug(f"start filling {storage.id=}")
self.steam_out.connect(storage.id) self.steam_out.connect(storage.id)
self.cap.set_throttle(1.0) self.cap.set_throttle(1.0)
self.cap.set_relief(0) self.cap.set_relief(0)
while not storage.is_full() and self.cap.phase() == "active": while not storage.is_full() and self.cap.phase() == "active":
debug(f"wait until {storage.id=} is full")
sleep(1) sleep(1)
debug(f"{storage.id=} is full, disconnect.")
self.steam_out.disconnect() self.steam_out.disconnect()
def check_storage(self) -> None: def check_storage(self) -> None:
"""search a `GasTank` to fill""" """search a `GasTank` to fill"""
if self.cap.pressure() < 10: return if self.cap.pressure() < 0.1: return
gas_tanks = list() gas_tanks = list()
for outpost in self.outposts: for outpost in self.outposts:
gas_tanks.extend([ get_component(gt.id) for gt in outpost.buildings('gas_tank') ]) gas_tanks.extend([ get_component(gt.id) for gt in outpost.buildings('gas_tank') ])
@@ -108,6 +112,15 @@ class ThermalCapWrapper:
if tank.fill_pct() < 0.8: if tank.fill_pct() < 0.8:
self.fill_storage(tank) self.fill_storage(tank)
break break
def get_nearest_outpost(self):
"""get nearest outpost to `ThermalCap` vent"""
vent = self.cap.vent()
outpost = get_component(get_component("outpost_network").nearest(vent.x, vent.y).id)
if isinstance(outpost, OutpostComponent):
debug(f"nearest {outpost=}")
return outpost
else: return None
@property @property
def cap(self) -> ThermalCap: def cap(self) -> ThermalCap:
@@ -122,10 +135,13 @@ class ThermalCapWrapper:
@property @property
def threshold(self) -> float: def threshold(self) -> float:
if "__threshold" not in self.__dict__: return self.default_threshold try: raw = self.__threshold
if not isinstance(self.__threshold, (float, int)): return self.default_threshold except AttributeError: return self.default_threshold
if not 0 < self.__threshold < 1: return self.default_threshold
return self.__threshold if not isinstance(raw, (float, int)) or isinstance(raw, bool):
return self.default_threshold
if not 0 < raw < 1: return self.default_threshold
return raw
@threshold.setter @threshold.setter
def threshold(self, value: float): def threshold(self, value: float):
@@ -137,10 +153,11 @@ class ThermalCapWrapper:
@property @property
def outposts(self) -> list[OutpostComponent]: def outposts(self) -> list[OutpostComponent]:
if "__outpost" not in self.__dict__: return None try: raw = self.__outposts
if isinstance(self.__outposts, list): return [ o for o in self.__outposts if isinstance(o, OutpostComponent) ] except AttributeError: return [self.get_nearest_outpost()]
# TODO: default
return None if not isinstance(raw, list): return [self.get_nearest_outpost()]
return [o for o in raw if isinstance(o, OutpostComponent)]
@outposts.setter @outposts.setter
def outposts(self, value: list[OutpostComponent] | OutpostComponent | OutpostRef | str): def outposts(self, value: list[OutpostComponent] | OutpostComponent | OutpostRef | str):
@@ -151,9 +168,9 @@ class ThermalCapWrapper:
if isinstance(value, str): value = get_component(value) if isinstance(value, str): value = get_component(value)
if isinstance(value, OutpostRef): value = get_component(value.id) if isinstance(value, OutpostRef): value = get_component(value.id)
if not isinstance(value, OutpostComponent): if not isinstance(value, OutpostComponent):
# TODO: find deafult. powernet ? or nearest ? warn(f"no outpost for steam out found ({value=}, use default nearest")
warn(f"no outpost for steam out found ({value=}") self.__outposts = [ self.get_nearest_outpost() ]
self.__outposts = list() return
else: self.__outposts = [ value ] else: self.__outposts = [ value ]
class SteamTurbineWrapper: class SteamTurbineWrapper:
@@ -185,10 +202,10 @@ class SteamTurbineWrapper:
def check_health(self): def check_health(self):
"""check own function and notify if not in expected parameters""" """check own function and notify if not in expected parameters"""
if self.steam_in.level() < self.notify_threshold: if self.steam_in.level() < self.notify_threshold:
notify(f"{self.turbine.id}: steam level under {self.notify_threshold=}", "warn", 30) notify(f"{self.turbine.id}: steam level under {self.notify_threshold}", "warn", 30)
if self.turbine.is_stalled(): if self.turbine.is_stalled() or self.steam_in.level() == 0:
notify(f"{self.turbine.id}: stopped working or has low efficency ({self.turbine.efficiency()*100}%)", "error", 10) notify(f"{self.turbine.id}: stopped working or has low efficency ({self.turbine.efficiency()*100}%)", "error", 30)
sleep(10) sleep(30)
def fill_steam(self): def fill_steam(self):
"""fills steam if own steam under threshold""" """fills steam if own steam under threshold"""
@@ -236,7 +253,7 @@ class SteamTurbineWrapper:
else: return gastanks else: return gastanks
@property @property
def turbine(self) -> SteamTurbine: def turbine(self) -> SteamTurbine:
if isinstance(self.__turbine, SteamTurbine): return self.__turbine if isinstance(self.__turbine, SteamTurbine): return self.__turbine
raise TypeError(f"{self.__turbine=} ({type(self.__turbine)=} is not a SteamTurbine)") raise TypeError(f"{self.__turbine=} ({type(self.__turbine)=} is not a SteamTurbine)")
@@ -248,10 +265,13 @@ class SteamTurbineWrapper:
@property @property
def threshold(self) -> int: def threshold(self) -> int:
if not "__threshold" in self.__dict__: return self.default_threshold try: raw = self.__threshold
if not isinstance(self.__threshold, int): return self.default_threshold except AttributeError: return self.default_threshold
if not 0 < self.__threshold < 100: return self.default_threshold
return self.threshold if not isinstance(raw, int) or isinstance(raw, bool):
return self.default_threshold
if not 0 < raw < 100: return self.default_threshold
return raw
@threshold.setter @threshold.setter
def threshold(self, value: int): def threshold(self, value: int):
@@ -263,9 +283,11 @@ class SteamTurbineWrapper:
@property @property
def storages(self) -> list[GasTank]: def storages(self) -> list[GasTank]:
if not "__storages" in self.__dict__: return self.get_local_storages() try: raw = self.__storages
if isinstance(self.__storages, list): return [ s for s in self.__storages if isinstance(s, GasTank) ] except AttributeError: return self.get_local_storages()
return self.get_local_storages()
if not isinstance(raw, list): return self.get_local_storages()
return [s for s in raw if isinstance(s, GasTank)]
@storages.setter @storages.setter
def storages(self, value: list[GasTank] | GasTank | str | None): def storages(self, value: list[GasTank] | GasTank | str | None):