From d3c533dfa11291efd31765f0f60f4868eedaadd2 Mon Sep 17 00:00:00 2001
From: Thaum Rystra <9525416+ThaumRystra@users.noreply.github.com>
Date: Wed, 20 Sep 2023 14:03:37 +0200
Subject: [PATCH] Moved ammo to its own pseudo property Triggers now work on
ammo #amo now works as well
---
.../api/engine/actions/applyProperty.js | 2 +
.../applyPropertyByType/applyAction.js | 57 +++++++------------
.../applyPropertyByType/applyItemAsAmmo.js | 42 ++++++++++++++
app/imports/api/properties/Items.js | 5 ++
app/imports/api/properties/Triggers.js | 1 +
.../client/ui/properties/forms/ItemForm.vue | 30 ++++++++--
6 files changed, 94 insertions(+), 43 deletions(-)
create mode 100644 app/imports/api/engine/actions/applyPropertyByType/applyItemAsAmmo.js
diff --git a/app/imports/api/engine/actions/applyProperty.js b/app/imports/api/engine/actions/applyProperty.js
index 18f35d32..b8e809d8 100644
--- a/app/imports/api/engine/actions/applyProperty.js
+++ b/app/imports/api/engine/actions/applyProperty.js
@@ -1,4 +1,5 @@
import action from './applyPropertyByType/applyAction.js';
+import ammo from './applyPropertyByType/applyItemAsAmmo.js'
import adjustment from './applyPropertyByType/applyAdjustment.js';
import branch from './applyPropertyByType/applyBranch.js';
import buff from './applyPropertyByType/applyBuff.js';
@@ -12,6 +13,7 @@ import toggle from './applyPropertyByType/applyToggle.js';
const applyPropertyByType = {
action,
+ ammo,
adjustment,
branch,
buff,
diff --git a/app/imports/api/engine/actions/applyPropertyByType/applyAction.js b/app/imports/api/engine/actions/applyPropertyByType/applyAction.js
index 4cdb7d69..b44c1b44 100644
--- a/app/imports/api/engine/actions/applyPropertyByType/applyAction.js
+++ b/app/imports/api/engine/actions/applyPropertyByType/applyAction.js
@@ -4,13 +4,10 @@ import rollDice from '/imports/parser/rollDice.js';
import applyProperty from '../applyProperty.js';
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
import applyChildren from '/imports/api/engine/actions/applyPropertyByType/shared/applyChildren.js';
-import { adjustQuantityWork } from '/imports/api/creature/creatureProperties/methods/adjustQuantity.js';
import { damagePropertyWork } from '/imports/api/creature/creatureProperties/methods/damageProperty.js';
import numberToSignedString from '/imports/api/utility/numberToSignedString.js';
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers.js';
import { resetProperties } from '/imports/api/creature/creatures/methods/restCreature.js';
-import { getPropertyDecendants } from '/imports/api/engine/loadCreatures.js';
-import { nodeArrayToTree } from '/imports/api/parenting/nodesToTree.js';
export default function applyAction(node, actionContext) {
applyNodeTriggers(node, 'before', actionContext);
@@ -210,10 +207,9 @@ function spendResources(prop, actionContext) {
return true;
}
// Items
- let itemQuantityAdjustments = [];
let spendLog = [];
let gainLog = [];
- let ammoChildren = [];
+ const ammoToApply = [];
try {
prop.resources.itemsConsumed.forEach(itemConsumed => {
recalculateCalculation(itemConsumed.quantity, actionContext);
@@ -228,11 +224,6 @@ function spendResources(prop, actionContext) {
!itemConsumed?.quantity?.value ||
!isFinite(itemConsumed.quantity.value)
) return;
- itemQuantityAdjustments.push({
- property: item,
- operation: 'increment',
- value: itemConsumed.quantity.value,
- });
let logName = item.name;
if (itemConsumed.quantity.value > 1 || itemConsumed.quantity.value < -1) {
logName = item.plural || logName;
@@ -242,15 +233,20 @@ function spendResources(prop, actionContext) {
} else if (itemConsumed.quantity.value < 0) {
gainLog.push(logName + ': ' + -itemConsumed.quantity.value);
}
- ammoChildren.push(...getItemChildren(item, actionContext, prop));
- /* Disabled this for now. Applying an item as ammo should be its own "applyPropertyByType"
- * which will set #item correctly before applying the item's children
- * and make triggers compatible in the future
-
- // simulate the increment and add the item to the action scope
- item.quantity -= itemConsumed.quantity.value;
- actionContext.scope['~ammo'] = item;
- */
+ // So long as the item isn't an ancestor of the current prop apply it
+ // If it was an ancestor this would be an infinite loop
+ if (!hasAncestorRelationship(item, prop)) {
+ ammoToApply.push({
+ node: {
+ ...item,
+ // Use ammo pseudo-type
+ type: 'ammo',
+ // Store the adjustment to be applied
+ adjustment: itemConsumed.quantity.value,
+ },
+ children: []
+ });
+ }
});
} catch (e) {
actionContext.addLog({
@@ -261,9 +257,6 @@ function spendResources(prop, actionContext) {
return true;
}
// No more errors should be thrown after this line
- // Now that we have confirmed that there are no errors, do actual work
- //Items
- itemQuantityAdjustments.forEach(adjustQuantityWork);
// Use uses
if (prop.usesLeft) {
@@ -303,6 +296,11 @@ function spendResources(prop, actionContext) {
}
});
+ // Apply the ammo children
+ ammoToApply.forEach(node => {
+ applyProperty(node, actionContext);
+ });
+
// Log all the spending
if (gainLog.length && !prop.silent) actionContext.addLog({
name: 'Gained',
@@ -314,21 +312,6 @@ function spendResources(prop, actionContext) {
value: spendLog.join('\n'),
inline: true,
});
-
- // Apply the ammo children
- ammoChildren.forEach(prop => {
- applyProperty(prop, actionContext);
- });
-}
-
-function getItemChildren(item, actionContext, prop) {
- // Skip if the prop or the item are ancestors of one another, otherwise infinite loop
- if (hasAncestorRelationship(item, prop)) return [];
- // Get the item children
- const itemProperties = getPropertyDecendants(actionContext.creature._id, item._id);
- // Tree them up
- const propertyForest = nodeArrayToTree(itemProperties);
- return propertyForest
}
function hasAncestorRelationship(a, b) {
diff --git a/app/imports/api/engine/actions/applyPropertyByType/applyItemAsAmmo.js b/app/imports/api/engine/actions/applyPropertyByType/applyItemAsAmmo.js
new file mode 100644
index 00000000..3bc3b24e
--- /dev/null
+++ b/app/imports/api/engine/actions/applyPropertyByType/applyItemAsAmmo.js
@@ -0,0 +1,42 @@
+import { getPropertyDecendants } from '/imports/api/engine/loadCreatures.js';
+import applyProperty from '../applyProperty.js';
+import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers.js';
+import { nodeArrayToTree } from '/imports/api/parenting/nodesToTree.js';
+import { adjustQuantityWork } from '/imports/api/creature/creatureProperties/methods/adjustQuantity.js';
+
+export default function applyItemAsAmmo(node, actionContext) {
+ // The item node should come without children, since it is not part of the original action tree
+ const prop = node.node;
+ // Get all the item's descendant properties
+ const properties = getPropertyDecendants(actionContext.creature._id, prop._id);
+ properties.sort((a, b) => a.order - b.order);
+ const propertyForest = nodeArrayToTree(properties);
+
+ // Apply the item
+ applyNodeTriggers(node, 'before', actionContext);
+
+ // Do the quantity adjustment
+ const itemProp = { ...prop, type: 'item' };
+ delete itemProp.adjustment;
+ adjustQuantityWork({
+ property: itemProp,
+ operation: 'increment',
+ value: prop.adjustment,
+ });
+
+ // Simulate the change to quantity
+ prop.quantity -= prop.adjustment;
+
+ // Log the item name as a heading if it's not silent and has child properties to apply
+ if (!prop.silent && propertyForest.length) {
+ actionContext.addLog({
+ name: prop.name || 'Ammo',
+ inline: false,
+ });
+ }
+ applyNodeTriggers(node, 'after', actionContext);
+
+ // Apply the item's children
+ propertyForest.forEach(node => applyProperty(node, actionContext));
+ applyNodeTriggers(node, 'afterChildren', actionContext);
+}
diff --git a/app/imports/api/properties/Items.js b/app/imports/api/properties/Items.js
index a328cfcf..b4bb7319 100644
--- a/app/imports/api/properties/Items.js
+++ b/app/imports/api/properties/Items.js
@@ -55,6 +55,11 @@ const ItemSchema = createPropertySchema({
type: Boolean,
defaultValue: false,
},
+ // Prevent the property from showing up in the log
+ silent: {
+ type: Boolean,
+ optional: true,
+ },
});
let ComputedOnlyItemSchema = createPropertySchema({
diff --git a/app/imports/api/properties/Triggers.js b/app/imports/api/properties/Triggers.js
index 250d6bda..09d777e2 100644
--- a/app/imports/api/properties/Triggers.js
+++ b/app/imports/api/properties/Triggers.js
@@ -23,6 +23,7 @@ const timingOptions = {
const actionPropertyTypeOptions = {
action: 'Action',
+ ammo: 'Ammo used',
adjustment: 'Attribute damage',
branch: 'Branch',
buff: 'Buff',
diff --git a/app/imports/client/ui/properties/forms/ItemForm.vue b/app/imports/client/ui/properties/forms/ItemForm.vue
index 3bbebdd4..394ccec0 100644
--- a/app/imports/client/ui/properties/forms/ItemForm.vue
+++ b/app/imports/client/ui/properties/forms/ItemForm.vue
@@ -84,12 +84,30 @@
-
+
+
+
+
+
+
+
+