extends RefCounted ## Derived power conditions for the Runtime-owned facility network. ## Does not change durability, operator policy, dependency or suspension status. ## A missing consumer denotes a facility with no network consumption; the reserve ## still gates it. Runtime must register every positive-demand consumer first. static func apply(facilities: FacilityService, network: ManaNetworkService, reserve: int, minute: int) -> Dictionary: if facilities == null or network == null or reserve < 0 or minute < 0: return {&"ok": false, &"code": &"INVALID_POWER_CONTEXT"} var changed: Array[StringName] = [] for facility: FacilityInstance in facilities.get_instances(): var node := network.get_node(facility.instance_id) var supplied := node.is_empty() or int(node.get(&"state", ManaNetworkService.ConsumerState.NO_POWER)) in [ManaNetworkService.ConsumerState.ACTIVE, ManaNetworkService.ConsumerState.DEGRADED] # Preserve the existing construction-to-first-evaluation contract. An # unevaluated node is not an observed outage; reserve zero still wins. if not node.is_empty() and &"awaiting_evaluation" in node.get(&"state_reasons", []): supplied = facility.power_available var available := reserve > 0 and supplied var reasons: Array[StringName] = facility.degraded_reasons.duplicate() reasons.erase(&"MANA_BROWNOUT") if reserve > 0 and not node.is_empty() and int(node.get(&"state", -1)) == ManaNetworkService.ConsumerState.DEGRADED: reasons.append(&"MANA_BROWNOUT") if available == facility.power_available and reasons == facility.degraded_reasons: continue var result := facilities.set_operational_status(facility.instance_id, {&"power_available": available, &"degraded_reasons": reasons}, minute) if not result.is_success(): return {&"ok": false, &"code": &"POWER_PROJECTION_FAILED", &"facility_id": facility.instance_id} changed.append(facility.instance_id) return {&"ok": true, &"depleted": reserve == 0, &"changed_facilities": changed}