Moved ammo to its own pseudo property
Triggers now work on ammo #amo now works as well
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import action from './applyPropertyByType/applyAction.js';
|
import action from './applyPropertyByType/applyAction.js';
|
||||||
|
import ammo from './applyPropertyByType/applyItemAsAmmo.js'
|
||||||
import adjustment from './applyPropertyByType/applyAdjustment.js';
|
import adjustment from './applyPropertyByType/applyAdjustment.js';
|
||||||
import branch from './applyPropertyByType/applyBranch.js';
|
import branch from './applyPropertyByType/applyBranch.js';
|
||||||
import buff from './applyPropertyByType/applyBuff.js';
|
import buff from './applyPropertyByType/applyBuff.js';
|
||||||
@@ -12,6 +13,7 @@ import toggle from './applyPropertyByType/applyToggle.js';
|
|||||||
|
|
||||||
const applyPropertyByType = {
|
const applyPropertyByType = {
|
||||||
action,
|
action,
|
||||||
|
ammo,
|
||||||
adjustment,
|
adjustment,
|
||||||
branch,
|
branch,
|
||||||
buff,
|
buff,
|
||||||
|
|||||||
@@ -4,13 +4,10 @@ import rollDice from '/imports/parser/rollDice.js';
|
|||||||
import applyProperty from '../applyProperty.js';
|
import applyProperty from '../applyProperty.js';
|
||||||
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
||||||
import applyChildren from '/imports/api/engine/actions/applyPropertyByType/shared/applyChildren.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 { damagePropertyWork } from '/imports/api/creature/creatureProperties/methods/damageProperty.js';
|
||||||
import numberToSignedString from '/imports/api/utility/numberToSignedString.js';
|
import numberToSignedString from '/imports/api/utility/numberToSignedString.js';
|
||||||
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers.js';
|
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers.js';
|
||||||
import { resetProperties } from '/imports/api/creature/creatures/methods/restCreature.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) {
|
export default function applyAction(node, actionContext) {
|
||||||
applyNodeTriggers(node, 'before', actionContext);
|
applyNodeTriggers(node, 'before', actionContext);
|
||||||
@@ -210,10 +207,9 @@ function spendResources(prop, actionContext) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
// Items
|
// Items
|
||||||
let itemQuantityAdjustments = [];
|
|
||||||
let spendLog = [];
|
let spendLog = [];
|
||||||
let gainLog = [];
|
let gainLog = [];
|
||||||
let ammoChildren = [];
|
const ammoToApply = [];
|
||||||
try {
|
try {
|
||||||
prop.resources.itemsConsumed.forEach(itemConsumed => {
|
prop.resources.itemsConsumed.forEach(itemConsumed => {
|
||||||
recalculateCalculation(itemConsumed.quantity, actionContext);
|
recalculateCalculation(itemConsumed.quantity, actionContext);
|
||||||
@@ -228,11 +224,6 @@ function spendResources(prop, actionContext) {
|
|||||||
!itemConsumed?.quantity?.value ||
|
!itemConsumed?.quantity?.value ||
|
||||||
!isFinite(itemConsumed.quantity.value)
|
!isFinite(itemConsumed.quantity.value)
|
||||||
) return;
|
) return;
|
||||||
itemQuantityAdjustments.push({
|
|
||||||
property: item,
|
|
||||||
operation: 'increment',
|
|
||||||
value: itemConsumed.quantity.value,
|
|
||||||
});
|
|
||||||
let logName = item.name;
|
let logName = item.name;
|
||||||
if (itemConsumed.quantity.value > 1 || itemConsumed.quantity.value < -1) {
|
if (itemConsumed.quantity.value > 1 || itemConsumed.quantity.value < -1) {
|
||||||
logName = item.plural || logName;
|
logName = item.plural || logName;
|
||||||
@@ -242,15 +233,20 @@ function spendResources(prop, actionContext) {
|
|||||||
} else if (itemConsumed.quantity.value < 0) {
|
} else if (itemConsumed.quantity.value < 0) {
|
||||||
gainLog.push(logName + ': ' + -itemConsumed.quantity.value);
|
gainLog.push(logName + ': ' + -itemConsumed.quantity.value);
|
||||||
}
|
}
|
||||||
ammoChildren.push(...getItemChildren(item, actionContext, prop));
|
// So long as the item isn't an ancestor of the current prop apply it
|
||||||
/* Disabled this for now. Applying an item as ammo should be its own "applyPropertyByType"
|
// If it was an ancestor this would be an infinite loop
|
||||||
* which will set #item correctly before applying the item's children
|
if (!hasAncestorRelationship(item, prop)) {
|
||||||
* and make triggers compatible in the future
|
ammoToApply.push({
|
||||||
|
node: {
|
||||||
// simulate the increment and add the item to the action scope
|
...item,
|
||||||
item.quantity -= itemConsumed.quantity.value;
|
// Use ammo pseudo-type
|
||||||
actionContext.scope['~ammo'] = item;
|
type: 'ammo',
|
||||||
*/
|
// Store the adjustment to be applied
|
||||||
|
adjustment: itemConsumed.quantity.value,
|
||||||
|
},
|
||||||
|
children: []
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
actionContext.addLog({
|
actionContext.addLog({
|
||||||
@@ -261,9 +257,6 @@ function spendResources(prop, actionContext) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
// No more errors should be thrown after this line
|
// 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
|
// Use uses
|
||||||
if (prop.usesLeft) {
|
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
|
// Log all the spending
|
||||||
if (gainLog.length && !prop.silent) actionContext.addLog({
|
if (gainLog.length && !prop.silent) actionContext.addLog({
|
||||||
name: 'Gained',
|
name: 'Gained',
|
||||||
@@ -314,21 +312,6 @@ function spendResources(prop, actionContext) {
|
|||||||
value: spendLog.join('\n'),
|
value: spendLog.join('\n'),
|
||||||
inline: true,
|
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) {
|
function hasAncestorRelationship(a, b) {
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
@@ -55,6 +55,11 @@ const ItemSchema = createPropertySchema({
|
|||||||
type: Boolean,
|
type: Boolean,
|
||||||
defaultValue: false,
|
defaultValue: false,
|
||||||
},
|
},
|
||||||
|
// Prevent the property from showing up in the log
|
||||||
|
silent: {
|
||||||
|
type: Boolean,
|
||||||
|
optional: true,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
let ComputedOnlyItemSchema = createPropertySchema({
|
let ComputedOnlyItemSchema = createPropertySchema({
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ const timingOptions = {
|
|||||||
|
|
||||||
const actionPropertyTypeOptions = {
|
const actionPropertyTypeOptions = {
|
||||||
action: 'Action',
|
action: 'Action',
|
||||||
|
ammo: 'Ammo used',
|
||||||
adjustment: 'Attribute damage',
|
adjustment: 'Attribute damage',
|
||||||
branch: 'Branch',
|
branch: 'Branch',
|
||||||
buff: 'Buff',
|
buff: 'Buff',
|
||||||
|
|||||||
@@ -84,12 +84,30 @@
|
|||||||
<form-section
|
<form-section
|
||||||
name="Behavior"
|
name="Behavior"
|
||||||
>
|
>
|
||||||
<smart-switch
|
<v-row dense>
|
||||||
label="Show increment button"
|
<v-col
|
||||||
:value="model.showIncrement"
|
cols="12"
|
||||||
:error-messages="errors.showIncrement"
|
md="6"
|
||||||
@change="change('showIncrement', ...arguments)"
|
>
|
||||||
/>
|
<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>
|
||||||
<form-section
|
<form-section
|
||||||
name="Attunement"
|
name="Attunement"
|
||||||
|
|||||||
Reference in New Issue
Block a user