Fixed content weightless carried containers weight

This commit is contained in:
Stefan Zermatten
2023-08-24 11:10:19 +02:00
parent b8387c5ab1
commit 7c2b7419d9
2 changed files with 26 additions and 26 deletions

View File

@@ -2,22 +2,22 @@
* Performs a depth first traversal of the character tree, summing the container * Performs a depth first traversal of the character tree, summing the container
* and inventory contents on the way up the tree * and inventory contents on the way up the tree
*/ */
export default function linkInventory(forest, dependencyGraph){ export default function linkInventory(forest, dependencyGraph) {
// The stack of properties to still navigate // The stack of properties to still navigate
const stack = [...forest]; const stack = [...forest];
// The current containers we are inside of // The current containers we are inside of
const containerStack = []; const containerStack = [];
while(stack.length){ while (stack.length) {
const top = stack[stack.length - 1]; const top = stack[stack.length - 1];
const prop = top.node; const prop = top.node;
if (prop._computationDetails.inventoryChildrenVisited){ if (prop._computationDetails.inventoryChildrenVisited) {
if (prop.type === 'container') containerStack.pop(); if (prop.type === 'container') containerStack.pop();
stack.pop(); stack.pop();
handleProp(prop, containerStack, dependencyGraph); handleProp(prop, containerStack, dependencyGraph);
} else { } else {
// Add all containers to the stack when we first visit them // Add all containers to the stack when we first visit them
if (prop.type === 'container'){ if (prop.type === 'container') {
containerStack.push(top.node); containerStack.push(top.node);
} }
// Push children onto the stack and mark this as children are visited // Push children onto the stack and mark this as children are visited
@@ -27,18 +27,18 @@ export default function linkInventory(forest, dependencyGraph){
} }
} }
function handleProp(prop, containerStack, dependencyGraph){ function handleProp(prop, containerStack, dependencyGraph) {
// Skip props that aren't part of the inventory // Skip props that aren't part of the inventory
if (prop.type !== 'item' && prop.type !== 'container') return; if (prop.type !== 'item' && prop.type !== 'container') return;
// Determine if this property is carried, items are carried by default // Determine if this property is carried, items are carried by default
let carried = prop.type === 'container' ? prop.carried : true; let carried = prop.type === 'container' ? prop.carried : true;
// Item-specific links // Item-specific links
if (prop.type === 'item'){ if (prop.type === 'item') {
if (prop.attuned){ if (prop.attuned) {
dependencyGraph.addLink('itemsAttuned', prop._id, 'attunedItem'); dependencyGraph.addLink('itemsAttuned', prop._id, 'attunedItem');
} }
if (prop.equipped){ if (prop.equipped) {
dependencyGraph.addLink('weightEquipment', prop._id, 'equippedItem'); dependencyGraph.addLink('weightEquipment', prop._id, 'equippedItem');
dependencyGraph.addLink('valueEquipment', prop._id, 'equippedItem'); dependencyGraph.addLink('valueEquipment', prop._id, 'equippedItem');
} }
@@ -47,14 +47,14 @@ function handleProp(prop, containerStack, dependencyGraph){
// Get the parent container // Get the parent container
const container = containerStack[containerStack.length - 1]; const container = containerStack[containerStack.length - 1];
if (container){ if (container) {
// The container depends on this prop for its contents data // The container depends on this prop for its contents data
dependencyGraph.addLink(container._id, prop._id, 'containerContents'); dependencyGraph.addLink(container._id, prop._id, 'containerContents');
} else { } else {
// There is no parent container, the character totals depend on this prop // There is no parent container, the character totals depend on this prop
dependencyGraph.addLink('weightTotal', prop._id, 'inventoryStats'); dependencyGraph.addLink('weightTotal', prop._id, 'inventoryStats');
dependencyGraph.addLink('valueTotal', prop._id, 'inventoryStats'); dependencyGraph.addLink('valueTotal', prop._id, 'inventoryStats');
if (carried){ if (carried) {
dependencyGraph.addLink('weightCarried', prop._id, 'inventoryStats'); dependencyGraph.addLink('weightCarried', prop._id, 'inventoryStats');
dependencyGraph.addLink('valueCarried', prop._id, 'inventoryStats'); dependencyGraph.addLink('valueCarried', prop._id, 'inventoryStats');
} }

View File

@@ -1,25 +1,25 @@
export default function aggregateInventory({node, linkedNode, link}){ export default function aggregateInventory({ node, linkedNode, link }) {
let linkedProp = linkedNode.data || {}; let linkedProp = linkedNode.data || {};
const prop = node.data; const prop = node.data;
switch (link.data){ switch (link.data) {
case 'attunedItem': case 'attunedItem':
prop.baseValue = (prop.baseValue || 0) + 1; prop.baseValue = (prop.baseValue || 0) + 1;
return; return;
case 'equippedItem': case 'equippedItem':
if (node.id === 'weightEquipment'){ if (node.id === 'weightEquipment') {
prop.baseValue = (prop.baseValue || 0) + weight(linkedProp); prop.baseValue = (prop.baseValue || 0) + weight(linkedProp);
} else if (node.id === 'valueEquipment'){ } else if (node.id === 'valueEquipment') {
prop.baseValue = (prop.baseValue || 0) + value(linkedProp); prop.baseValue = (prop.baseValue || 0) + value(linkedProp);
} }
return; return;
case 'containerContents': case 'containerContents':
// Add this property's weights and values to the container // Add this property's weights and values to the container
if (!prop.weightless){ if (!prop.weightless) {
prop.contentsWeight = (prop.contentsWeight || 0) + weight(linkedProp); prop.contentsWeight = (prop.contentsWeight || 0) + weight(linkedProp);
if (prop.carried){ if (prop.carried && !prop.contentsWeightless) {
prop.carriedWeight = (prop.carriedWeight || 0) + carriedWeight(linkedProp); prop.carriedWeight = (prop.carriedWeight || 0) + carriedWeight(linkedProp);
} }
} }
@@ -30,39 +30,39 @@ export default function aggregateInventory({node, linkedNode, link}){
return; return;
case 'inventoryStats': case 'inventoryStats':
if (node.id === 'weightTotal'){ if (node.id === 'weightTotal') {
prop.baseValue = (prop.baseValue || 0) + weight(linkedProp); prop.baseValue = (prop.baseValue || 0) + weight(linkedProp);
} else if (node.id === 'valueTotal'){ } else if (node.id === 'valueTotal') {
prop.baseValue = (prop.baseValue || 0) + value(linkedProp); prop.baseValue = (prop.baseValue || 0) + value(linkedProp);
} else if (node.id === 'weightCarried'){ } else if (node.id === 'weightCarried') {
prop.baseValue = (prop.baseValue || 0) + carriedWeight(linkedProp); prop.baseValue = (prop.baseValue || 0) + carriedWeight(linkedProp);
} else if (node.id === 'valueCarried'){ } else if (node.id === 'valueCarried') {
prop.baseValue = (prop.baseValue || 0) + carriedValue(linkedProp); prop.baseValue = (prop.baseValue || 0) + carriedValue(linkedProp);
} }
return; return;
} }
} }
function quantity(prop){ function quantity(prop) {
if (typeof prop.quantity === 'number'){ if (typeof prop.quantity === 'number') {
return prop.quantity; return prop.quantity;
} else { } else {
return 1; return 1;
} }
} }
function weight(prop){ function weight(prop) {
return (prop.weight || 0) * quantity(prop) + (prop.contentsWeight || 0); return (prop.weight || 0) * quantity(prop) + (prop.contentsWeight || 0);
} }
function carriedWeight(prop){ function carriedWeight(prop) {
return (prop.weight || 0) * quantity(prop) + (prop.carriedWeight || 0); return (prop.weight || 0) * quantity(prop) + (prop.carriedWeight || 0);
} }
function value (prop){ function value(prop) {
return (prop.value || 0) * quantity(prop) + (prop.contentsValue || 0); return (prop.value || 0) * quantity(prop) + (prop.contentsValue || 0);
} }
function carriedValue (prop){ function carriedValue(prop) {
return (prop.value || 0) * quantity(prop) + (prop.carriedValue || 0); return (prop.value || 0) * quantity(prop) + (prop.carriedValue || 0);
} }