In my game the map is represented by connected nodes, each node has a number of connected nodes. The nodes represent a system in which players can build structures and move units about. If you're familiar with Sins of a Solar Empire the game map is very similar.
I want each node to be able to produce power and share it with all connected nodes.
For example if A, B, C & D are all connected and produce 100 power units, then each system
should have 400 power units available. If node B builds a structure that consumes 100 power units then A, B, C & D should then have 300 power units available.
I've been working on this system all day and haven't been able to get it working quite the way I want.
My current implementation is to first recurse through each nodes's connected node adding up the power, I keep a list of closed nodes so it doesn't loop, it's quite similar to A* actually.
Pseudo code:
All nodes start with the properties
node.power = 0
node.basePower = 100 // could be different for each node.
node.initialPower = node.basePower
-
function propagatePower( node, initialPower, closedNodes )
node.power += initialPower
add( closedNodes, node )
connectedNodes = connected_nodes_except_from( closedNodes )
foreach node in connectedNodes do
propagatePower( node, initialPower, closedNodes )
end
end
After this I iterate through all power consumers.
foreach consumer in consumers do
node = consumer.parentNode
if node.power >= consumer.powerConsumption then
consumer.powerConsumed += consumer.powerConsumption
node.producedPower -= consumer.powerConsumption
end
end
Then I adjust the initial power for the next propagation cycle.
foreach node in nodes do
node.initialPower = node.basePower - node.producedPower
node.displayPower = node.power // for rendering the power.
node.power = 0
end
This seemed to work at first but then I came into a problem.
Say two nodes A & B produce 100Pu each, it's shared so both A & B have 200Pu.
I then make two structures that consume 80Pu each on A (160Pu).
Then the nodes power is adjusted to basePower - producedPower (100-160 = -60).
Nodes are propagated, both nodes now have 40Pu (A: -60 + B: 100 = 40).
Which is correct because they started with 200Pu - 160Pu = 40Pu.
However now node.power >= consumer.powerConsumption is false.
Whats worse is it's false for any structure that uses more that 40Pu, so the whole system goes down.
I could deduct from consumer.powerConsumption but what do I do if power is reduced elsewhere? I don't have the correct data to perform the necessary checks.
It's late so I'm probably not thinking straight but I thought to ask on here to see if anyone has any other implementations, better or worse I'd be interested to know.