mrkwnzl / cyphersystem-foundryvtt

The Cypher System for Foundry VTT
Other
21 stars 14 forks source link

Don't update all stats for recursions/tags if no values defined. #309

Closed farling42 closed 1 year ago

farling42 commented 1 year ago

In order for the active effects to handle the recursion and tags, it would be nice if the code didn't set all the pool values when all the "Stat Modifiers" configured for the recursions and tags are left at 0 value (or leave the values unchanged).

tagging-engine-computation.js:

export async function changeTagStats(actor, statChanges) {
  if (actor?.actorLink == false) {
    actor = actor.actor;
  }

  let pool = actor.system.pools;

  let oldMightModifier = (!actor.getFlag("cyphersystem", "tagMightModifier")) ? 0 : actor.getFlag("cyphersystem", "tagMightModifier");
  let oldSpeedModifier = (!actor.getFlag("cyphersystem", "tagSpeedModifier")) ? 0 : actor.getFlag("cyphersystem", "tagSpeedModifier");
  let oldIntellectModifier = (!actor.getFlag("cyphersystem", "tagIntellectModifier")) ? 0 : actor.getFlag("cyphersystem", "tagIntellectModifier");
  let oldMightEdgeModifier = (!actor.getFlag("cyphersystem", "tagMightEdgeModifier")) ? 0 : actor.getFlag("cyphersystem", "tagMightEdgeModifier");
  let oldSpeedEdgeModifier = (!actor.getFlag("cyphersystem", "tagSpeedEdgeModifier")) ? 0 : actor.getFlag("cyphersystem", "tagSpeedEdgeModifier");
  let oldIntellectEdgeModifier = (!actor.getFlag("cyphersystem", "tagIntellectEdgeModifier")) ? 0 : actor.getFlag("cyphersystem", "tagIntellectEdgeModifier");

  if (oldMightModifier == statChanges.mightModifier &&
    oldSpeedModifier == statChanges.speedModifier &&
    oldIntellectModifier == statChanges.intellectModifier &&
    oldMightEdgeModifier == statChanges.mightEdgeModifier &&
    oldSpeedEdgeModifier == statChanges.speedEdgeModifier &&
    oldIntellectEdgeModifier == statChanges.intellectEdgeModifier)
    return;

  await actor.update({
    "system.pools.might.value": pool.might.value + statChanges.mightModifier - oldMightModifier,
    "system.pools.might.max": pool.might.max + statChanges.mightModifier - oldMightModifier,
    "system.pools.speed.value": pool.speed.value + statChanges.speedModifier - oldSpeedModifier,
    "system.pools.speed.max": pool.speed.max + statChanges.speedModifier - oldSpeedModifier,
    "system.pools.intellect.value": pool.intellect.value + statChanges.intellectModifier - oldIntellectModifier,
    "system.pools.intellect.max": pool.intellect.max + statChanges.intellectModifier - oldIntellectModifier,
    "system.pools.might.edge": pool.might.edge + statChanges.mightEdgeModifier - oldMightEdgeModifier,
    "system.pools.speed.edge": pool.speed.edge + statChanges.speedEdgeModifier - oldSpeedEdgeModifier,
    "system.pools.intellect.edge": pool.intellect.edge + statChanges.intellectEdgeModifier - oldIntellectEdgeModifier,
    "flags.cyphersystem.tagMightModifier": statChanges.mightModifier,
    "flags.cyphersystem.tagSpeedModifier": statChanges.speedModifier,
    "flags.cyphersystem.tagIntellectModifier": statChanges.intellectModifier,
    "flags.cyphersystem.tagMightEdgeModifier": statChanges.mightEdgeModifier,
    "flags.cyphersystem.tagSpeedEdgeModifier": statChanges.speedEdgeModifier,
    "flags.cyphersystem.tagIntellectEdgeModifier": statChanges.intellectEdgeModifier
  });
}

AND in macros.js (here it is still necessary to set the "flags.cyphersystem.recursion", but not the pool values.

export async function changeRecursionStats(actor, recursion, mightModifier, mightEdgeModifier, speedModifier, speedEdgeModifier, intellectModifier, intellectEdgeModifier) {
  let pool = actor.system.pools;

  let oldMightModifier = (!actor.getFlag("cyphersystem", "recursionMightModifier")) ? 0 : actor.getFlag("cyphersystem", "recursionMightModifier");
  let oldSpeedModifier = (!actor.getFlag("cyphersystem", "recursionSpeedModifier")) ? 0 : actor.getFlag("cyphersystem", "recursionSpeedModifier");
  let oldIntellectModifier = (!actor.getFlag("cyphersystem", "recursionIntellectModifier")) ? 0 : actor.getFlag("cyphersystem", "recursionIntellectModifier");
  let oldMightEdgeModifier = (!actor.getFlag("cyphersystem", "recursionMightEdgeModifier")) ? 0 : actor.getFlag("cyphersystem", "recursionMightEdgeModifier");
  let oldSpeedEdgeModifier = (!actor.getFlag("cyphersystem", "recursionSpeedEdgeModifier")) ? 0 : actor.getFlag("cyphersystem", "recursionSpeedEdgeModifier");
  let oldIntellectEdgeModifier = (!actor.getFlag("cyphersystem", "recursionIntellectEdgeModifier")) ? 0 : actor.getFlag("cyphersystem", "recursionIntellectEdgeModifier");

  if (oldMightModifier == mightModifier &&
    oldSpeedModifier == speedModifier &&
    oldIntellectModifier == intellectModifier &&
    oldMightEdgeModifier == mightEdgeModifier &&
    oldSpeedEdgeModifier == speedEdgeModifier &&
    oldIntellectEdgeModifier == intellectEdgeModifier)
    await actor.update({"flags.cyphersystem.recursion": recursion})
  else
  await actor.update({
    "flags.cyphersystem.recursion": recursion,
    "system.pools.might.value": pool.might.value + mightModifier - oldMightModifier,
    "system.pools.might.max": pool.might.max + mightModifier - oldMightModifier,
    "system.pools.speed.value": pool.speed.value + speedModifier - oldSpeedModifier,
    "system.pools.speed.max": pool.speed.max + speedModifier - oldSpeedModifier,
    "system.pools.intellect.value": pool.intellect.value + intellectModifier - oldIntellectModifier,
    "system.pools.intellect.max": pool.intellect.max + intellectModifier - oldIntellectModifier,
    "system.pools.might.edge": pool.might.edge + mightEdgeModifier - oldMightEdgeModifier,
    "system.pools.speed.edge": pool.speed.edge + speedEdgeModifier - oldSpeedEdgeModifier,
    "system.pools.intellect.edge": pool.intellect.edge + intellectEdgeModifier - oldIntellectEdgeModifier,
    "flags.cyphersystem.recursionMightModifier": mightModifier,
    "flags.cyphersystem.recursionSpeedModifier": speedModifier,
    "flags.cyphersystem.recursionIntellectModifier": intellectModifier,
    "flags.cyphersystem.recursionMightEdgeModifier": mightEdgeModifier,
    "flags.cyphersystem.recursionSpeedEdgeModifier": speedEdgeModifier,
    "flags.cyphersystem.recursionIntellectEdgeModifier": intellectEdgeModifier
  });
}
mrkwnzl commented 1 year ago

I don't understand the issue. What is this for and what problem does it solve? Can you elaborate?

In order for the active effects to handle the recursion and tags

Active effects aren't supposed to handle tags and recursions. Those are handled by the system.

farling42 commented 1 year ago

If an active effect is on an Item which is linked to being only in a single recursion, then that item gets archived when in the other recursion. If that item has an effect tied to it which modifies stats or pools (for whatever reason), then the updates aren't handled gracefully because of the above routines reading the pool values when they have possibly been changed by an effect already on the actor.

I've found an alternative solution which is that at the start of the above two routines, you can set

let pool = actor._source.system.pools;

rather than

let pool = actor.system.pools;

This will read the values from the actor from the _source block which are the values for those pools without any effects having been applied to them.

mrkwnzl commented 1 year ago

I see, thanks for clarifying. Done in the current develop branch. After a quick test, this doesn’t affect the functionality without the Active Effects module, so it’s a good solution.