More engine rewrite progress, starting to get messy again

This commit is contained in:
Stefan Zermatten
2021-09-13 16:12:04 +02:00
parent b877a8b45f
commit 5c84836238
38 changed files with 614 additions and 262 deletions

View File

@@ -1,5 +1,6 @@
import evaluateCalculation from '/imports/api/creature/computation/newEngine/computeComputation/evaluateCalculation.js';
import computeVariable from '/imports/api/creature/computation/newEngine/computeComputation/computeVariable.js';
import computeCalculations from '/imports/api/creature/computation/newEngine/computeComputation/computeCalculations.js';
import computeToggles from '/imports/api/creature/computation/newEngine/computeComputation/computeToggles.js';
import computeByType from '/imports/api/creature/computation/newEngine/computeComputation/computeByType.js';
export default function computeCreatureComputation(computation){
const stack = [];
@@ -16,20 +17,16 @@ export default function computeCreatureComputation(computation){
while (stack.length){
let top = stack[stack.length - 1];
if (top.visited){
// The object has already
// The object has already been computed, skip
stack.pop();
} else if (top.visitedChildren){
// Compute the top object of the stack
compute(graph, top.node, scope);
// If the node holds a variable, store it in the scope
if (!top.node.data?.type){
scope[top.node.id] = top.node.data;
}
// Mark the object as visited and remove from stack
top.visited = true;
stack.pop();
} else {
// Push children to graph
// Push dependencies to graph to be computed first
pushDependenciesToStack(top.node.id, graph, stack);
top.visitedChildren = true;
}
@@ -37,32 +34,20 @@ export default function computeCreatureComputation(computation){
}
function compute(graph, node, scope){
// Get the property
let prop = node.data;
// evaluate all the calculations
if (prop?._computationDetails?.calculations){
prop._computationDetails.calculations.forEach(calcObj => {
evaluateCalculation(calcObj, scope)
});
}
// Determine the prop's active status by its toggles
computeToggles(node);
computeCalculations(node, scope);
// Compute the property by type
let typeCompute = propTypeComputations[prop?.type || '_variable'];
typeCompute?.(graph, node);
computeByType[node.data?.type || '_variable']?.(graph, node, scope);
}
var propTypeComputations = {
'_variable': computeVariable,
};
function pushDependenciesToStack(nodeId, graph, stack){
graph.forEachLinkedNode(
nodeId,
(linkedNode, link) => {
// Ignore inventory links, they can't cause dependency loops
// and are already fully computed when they are created
if (link.data === 'inventory') return;
// Ignore inventory links, they are already fully computed when they are
// created
if (link.data === 'inventory' || link.data === 'classLevel') return;
stack.push({
node: linkedNode,
visited: false,