I have a grid of objects like this:
1
+
F
1 is equal to the constant 1, F flickers between 0 and 1 each tick,
and + is equal to the sum of the 1 and the F.
I want to update these values as described above. The 1 won't change, the F will change from 0 to 1 and vice-versa, and the + will change to the sum.
I could just do a loop:
for node in nodes {
if(node.type == '+') {
node.value = node.input1.value + node.input2.value;
}
if(node.type == '1') { ... }
if(node.type == 'F') { ... }
}
but the problem is that this will depend on the order of iteration. If the F
is iterated on first, then it will be set to 1 from an initial value of 0, then the + would equal 1 + 1, i.e. 2. But if the + was first, the result would be 1 + 0 = 0 and the NEXT tick would be 1 + 1 = 2.
I want to do this simultaneously. So I thought I could have a map of new values:
global map = new HashMap<Node, double>();
...
map.clear();
for node in nodes {
if(node.type == '1') {}
if(node.type == 'F') {
map.set(node, node.value == 1 ? 0 : 1);
}
if(node.type == '+') {
map.set(node, node.input1.value + node.input2.value);
}
}
map.forEach(k, v -> k.value = v);
but this is probably inefficient as all values have to be looped over and entries in the map must be created for each node (node count is expected to be alot, around 10,000).
How do I do this? I probably need a list of "update required" nodes but I'm not sure what to do exactly, especially to do it simultaneously.
Examples:
1
\
== + === + === + == (flickers between 6 and 7 starting at 6 due to F flickering)
/ / /
F 3 2
// Nodes can be created by the player
1 => + <= 2
|
|
(equals 3)
// Player creates a node:
1 => + <= 2
|
|
(factorial) ===> (equals 6)
There can be multiple of these connected nodes at a time:
1 => + <= 2
|
|
(equals 3)
2 => + <= 3
|
|
(equals 5)
// If the player chooses, they can connect these 2 together:
1 => + <= 2
|
|
-===========
|
2 => + <= 3 |
| + =====> (equals 8)
| |
-===========
Nodes can be inputs to themselves, or have no inputs (in which case the inputs default to 0):
=====> (starts at 1 in the first tick, then keeps incrementing.)
|
1 => + <==
| |
=====