Moved ammo to its own pseudo property

Triggers now work on ammo
#amo now works as well
This commit is contained in:
Thaum Rystra
2023-09-20 14:03:37 +02:00
parent 3a3deca867
commit d3c533dfa1
6 changed files with 94 additions and 43 deletions

View File

@@ -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,

View File

@@ -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) {

View File

@@ -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);
}

View File

@@ -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({

View File

@@ -23,6 +23,7 @@ const timingOptions = {
const actionPropertyTypeOptions = {
action: 'Action',
ammo: 'Ammo used',
adjustment: 'Attribute damage',
branch: 'Branch',
buff: 'Buff',

View File

@@ -84,12 +84,30 @@
<form-section
name="Behavior"
>
<smart-switch
label="Show increment button"
:value="model.showIncrement"
:error-messages="errors.showIncrement"
@change="change('showIncrement', ...arguments)"
/>
<v-row dense>
<v-col
cols="12"
md="6"
>
<smart-switch
label="Show increment button"
:value="model.showIncrement"
:error-messages="errors.showIncrement"
@change="change('showIncrement', ...arguments)"
/>
</v-col>
<v-col
cols="12"
md="6"
>
<smart-switch
label="Don't show in log"
:value="model.silent"
:error-messages="errors.silent"
@change="change('silent', ...arguments)"
/>
</v-col>
</v-row>
</form-section>
<form-section
name="Attunement"