Merge branch 'develop' into feature-tabletop
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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);
|
||||
@@ -174,7 +171,11 @@ function rollAttack(attack, scope) {
|
||||
}
|
||||
|
||||
function applyCrits(value, scope) {
|
||||
const criticalHitTarget = scope['~criticalHitTarget']?.value || 20;
|
||||
let scopeCrit = scope['~criticalHitTarget']?.value;
|
||||
if (scopeCrit?.parseType === 'constant') {
|
||||
scopeCrit = scopeCrit.value;
|
||||
}
|
||||
const criticalHitTarget = scopeCrit || 20;
|
||||
let criticalHit = value >= criticalHitTarget;
|
||||
let criticalMiss;
|
||||
if (criticalHit) {
|
||||
@@ -206,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);
|
||||
@@ -224,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;
|
||||
@@ -238,7 +233,20 @@ function spendResources(prop, actionContext) {
|
||||
} else if (itemConsumed.quantity.value < 0) {
|
||||
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
|
||||
// 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({
|
||||
@@ -249,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) {
|
||||
@@ -291,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',
|
||||
@@ -302,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) {
|
||||
|
||||
@@ -10,6 +10,7 @@ import {
|
||||
} from '/imports/api/engine/loadCreatures.js';
|
||||
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers.js';
|
||||
import getEffectivePropTags from '/imports/api/engine/computation/utility/getEffectivePropTags.js';
|
||||
import applySavingThrow from '/imports/api/engine/actions/applyPropertyByType/applySavingThrow.js';
|
||||
|
||||
export default function applyDamage(node, actionContext) {
|
||||
applyNodeTriggers(node, 'before', actionContext);
|
||||
@@ -36,7 +37,7 @@ export default function applyDamage(node, actionContext) {
|
||||
const logName = prop.damageType === 'healing' ? 'Healing' : 'Damage';
|
||||
|
||||
// roll the dice only and store that string
|
||||
applyEffectsToCalculationParseNode(prop.amount, actionContext.log);
|
||||
applyEffectsToCalculationParseNode(prop.amount, actionContext);
|
||||
const { result: rolled } = resolve('roll', prop.amount.parseNode, scope, context);
|
||||
if (rolled.parseType !== 'constant') {
|
||||
logValue.push(toString(rolled));
|
||||
@@ -67,6 +68,7 @@ export default function applyDamage(node, actionContext) {
|
||||
|
||||
// Round the damage to a whole number
|
||||
damage = Math.floor(damage);
|
||||
scope['~damage'] = damage;
|
||||
|
||||
// Convert extra damage into the stored type
|
||||
if (prop.damageType === 'extra' && scope['~lastDamageType']?.value) {
|
||||
@@ -82,24 +84,74 @@ export default function applyDamage(node, actionContext) {
|
||||
prop.damageType +
|
||||
(prop.damageType !== 'healing' ? ' damage ' : '');
|
||||
|
||||
// If there is a save, calculate the save damage
|
||||
let damageOnSave, saveNode, saveRoll;
|
||||
if (prop.save) {
|
||||
if (prop.save.damageFunction?.calculation) {
|
||||
applyEffectsToCalculationParseNode(prop.save.damageFunction, actionContext);
|
||||
let { result: saveDamageRolled } = resolve('roll', prop.save.damageFunction.parseNode, scope, context);
|
||||
saveRoll = toString(saveDamageRolled);
|
||||
let { result: saveDamageResult } = resolve('reduce', saveDamageRolled, scope, context);
|
||||
// If we didn't end up with a constant of finite amount, give up
|
||||
if (reduced?.parseType !== 'constant' || !isFinite(reduced.value)) {
|
||||
return applyChildren(node, actionContext);
|
||||
}
|
||||
damageOnSave = +saveDamageResult.value;
|
||||
// Round the damage to a whole number
|
||||
damageOnSave = Math.floor(damageOnSave);
|
||||
} else {
|
||||
damageOnSave = Math.floor(damage / 2);
|
||||
}
|
||||
saveNode = {
|
||||
node: {
|
||||
...prop.save,
|
||||
name: prop.save.stat,
|
||||
silent: prop.silent,
|
||||
},
|
||||
children: [],
|
||||
}
|
||||
}
|
||||
|
||||
if (damageTargets && damageTargets.length) {
|
||||
// Iterate through all the targets
|
||||
damageTargets.forEach(target => {
|
||||
actionContext.target = [target];
|
||||
let damageToApply = damage;
|
||||
|
||||
// If there is a saving throw, apply that first
|
||||
if (prop.save) {
|
||||
applySavingThrow(saveNode, actionContext);
|
||||
if (scope['~saveSucceeded']?.value) {
|
||||
// Log the total damage
|
||||
logValue.push(toString(reduced));
|
||||
// Log the save damage
|
||||
const damageText = damageFunctionText(prop.save);
|
||||
if (damageText) {
|
||||
logValue.push(damageText);
|
||||
} else {
|
||||
logValue.push(
|
||||
'**Damage on successful save**',
|
||||
prop.save.damageFunction.calculation,
|
||||
saveRoll
|
||||
);
|
||||
}
|
||||
damageToApply = damageOnSave;
|
||||
}
|
||||
}
|
||||
|
||||
// Apply weaknesses/resistances/immunities
|
||||
damage = applyDamageMultipliers({
|
||||
damageToApply = applyDamageMultipliers({
|
||||
target,
|
||||
damage,
|
||||
damage: damageToApply,
|
||||
damageProp: prop,
|
||||
logValue
|
||||
});
|
||||
|
||||
actionContext.target = [target];
|
||||
// Deal the damage to the target
|
||||
let damageDealt = dealDamage({
|
||||
target,
|
||||
damageType: prop.damageType,
|
||||
amount: damage,
|
||||
amount: damageToApply,
|
||||
actionContext
|
||||
});
|
||||
|
||||
@@ -123,6 +175,10 @@ export default function applyDamage(node, actionContext) {
|
||||
} else {
|
||||
// There are no targets, just log the result
|
||||
logValue.push(`**${damage}** ${suffix}`);
|
||||
if (prop.save) {
|
||||
applySavingThrow(saveNode, actionContext);
|
||||
logValue.push(`**${damageOnSave}** ${suffix} on a successful save`);
|
||||
}
|
||||
}
|
||||
if (!prop.silent) actionContext.addLog({
|
||||
name: logName,
|
||||
@@ -132,6 +188,16 @@ export default function applyDamage(node, actionContext) {
|
||||
return applyChildren(node, actionContext);
|
||||
}
|
||||
|
||||
function damageFunctionText(save, scope, context, actionContext) {
|
||||
if (!save) return [];
|
||||
if (!save.damageFunction) {
|
||||
return '**Half damage on successful save**';
|
||||
}
|
||||
if (save.damageFunction.calculation == '0' || save.damageFunction.value === 0) {
|
||||
return '**No damage on successful save**'
|
||||
}
|
||||
}
|
||||
|
||||
function applyDamageMultipliers({ target, damage, damageProp, logValue }) {
|
||||
const damageType = damageProp?.damageType;
|
||||
if (!damageType) return damage;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user