This repository has been archived on 2020-09-30. You can view files and clone it, but cannot push or open issues or pull requests.
odyssey-old/Actors/Systems/Electricity/PowerNetwork.gd
2020-07-09 16:50:34 +02:00

83 lines
2.1 KiB
GDScript

extends Node
class_name PowerNetwork
const DEBUG = false
var nodes = []
var sockets = []
var total_source = 0
var total_usage = 0
var debugColor = Color.cyan
func _ready():
name = "PowerNetwork"
debugColor = Color.from_hsv(randf(), 0.8, 0.8)
func add_node(node) -> void:
nodes.append(node)
if "connections" in node:
sockets.append(node)
func remove_node(node) -> void:
var node_idx = nodes.find(node)
if node_idx >= 0:
nodes.remove(node_idx)
var sock_idx = sockets.find(node)
if sock_idx >= 0:
sockets.remove(sock_idx)
# Do other splitting here
node.network = null
func join(network) -> void:
for node in network.nodes:
nodes.append(node)
node.network = self
for socket in network.sockets:
sockets.append(socket)
socket.network = self
# Do other merging here
network.queue_free()
func _physics_process(_delta: float) -> void:
# Recalculate power availability and usage
total_source = 0
total_usage = 0
var sources = []
var sinks = []
# Calculate totals
for socket in sockets:
for connection in socket.connections:
var manager = connection as PowerManager
match socket.flow:
ElectricSocket.Flow.SINK:
total_usage += manager.power_usage
sinks.append(manager)
ElectricSocket.Flow.SOURCE:
total_source += manager.power_source
sources.append(manager)
ElectricSocket.Flow.BIDIRECTIONAL:
total_usage += manager.power_usage
total_source += manager.power_source
sinks.append(manager)
sources.append(manager)
# Update manager stats
var available_supply = total_source
for sink in sinks:
# Check if item can be powered this cycle
if sink.power_usage > 0:
if available_supply > sink.power_usage:
available_supply -= sink.power_usage
sink.powered = true
else:
sink.powered = false
# Update available power to sinks
for sink in sinks:
sink.available = available_supply
# Check how much we actually need to drain from sources
var remaining_drain = total_source - available_supply
for source in sources:
var source_load = source.power_source / total_source * remaining_drain
source.power_load = source_load