-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from draconas1/dev
Auto set actor to status bloodied, dying or dead based on HP.
- Loading branch information
Showing
9 changed files
with
240 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import DnD4eTools from "./4e-tools.js"; | ||
|
||
export function registerConfigs() { | ||
game.settings.register(DnD4eTools.ID, DnD4eTools.SETTINGS.CREATE_IN_ENCOUNTER_FOLDERS, { | ||
name: `TOOLS4E.settings.${DnD4eTools.SETTINGS.CREATE_IN_ENCOUNTER_FOLDERS}.Name`, | ||
default: true, | ||
type: Boolean, | ||
scope: 'client', | ||
config: true, | ||
hint: `TOOLS4E.settings.${DnD4eTools.SETTINGS.CREATE_IN_ENCOUNTER_FOLDERS}.Hint` | ||
}); | ||
|
||
game.settings.register(DnD4eTools.ID, DnD4eTools.SETTINGS.DO_NOT_DUPLICATE, { | ||
name: `TOOLS4E.settings.${DnD4eTools.SETTINGS.DO_NOT_DUPLICATE}.Name`, | ||
default: true, | ||
type: Boolean, | ||
scope: 'client', | ||
config: true, | ||
hint: `TOOLS4E.settings.${DnD4eTools.SETTINGS.DO_NOT_DUPLICATE}.Hint` | ||
}); | ||
|
||
game.settings.register(DnD4eTools.ID, DnD4eTools.SETTINGS.DO_NOT_DUPLICATE_IN_FOLDER, { | ||
name: `TOOLS4E.settings.${DnD4eTools.SETTINGS.DO_NOT_DUPLICATE_IN_FOLDER}.Name`, | ||
default: true, | ||
type: Boolean, | ||
scope: 'client', | ||
config: true, | ||
hint: `TOOLS4E.settings.${DnD4eTools.SETTINGS.DO_NOT_DUPLICATE_IN_FOLDER}.Hint` | ||
}); | ||
|
||
game.settings.register(DnD4eTools.ID, DnD4eTools.SETTINGS.BLOODIED_ICON, { | ||
name: `TOOLS4E.settings.${DnD4eTools.SETTINGS.BLOODIED_ICON}.Name`, | ||
default: true, | ||
type: Boolean, | ||
scope: 'client', | ||
config: true, | ||
hint: `TOOLS4E.settings.${DnD4eTools.SETTINGS.BLOODIED_ICON}.Hint` | ||
}); | ||
game.settings.register(DnD4eTools.ID, DnD4eTools.SETTINGS.DEAD_ICON, { | ||
name: `TOOLS4E.settings.${DnD4eTools.SETTINGS.DEAD_ICON}.Name`, | ||
default: true, | ||
type: Boolean, | ||
scope: 'client', | ||
config: true, | ||
hint: `TOOLS4E.settings.${DnD4eTools.SETTINGS.DEAD_ICON}.Hint` | ||
}); | ||
game.settings.register(DnD4eTools.ID, DnD4eTools.SETTINGS.CHANGE_DEAD_ICON, { | ||
name: `TOOLS4E.settings.${DnD4eTools.SETTINGS.CHANGE_DEAD_ICON}.Name`, | ||
default: false, | ||
type: Boolean, | ||
scope: 'client', | ||
config: true, | ||
hint: `TOOLS4E.settings.${DnD4eTools.SETTINGS.CHANGE_DEAD_ICON}.Hint`, | ||
onChange: () => DnD4eTools.setDeadIcon() | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import DnD4eTools from "../4e-tools.js"; | ||
|
||
const dead = "dead" | ||
const dying = "dying" | ||
const bloodied = "bloodied" | ||
|
||
export async function setBloodiedDeadOnHPChange(actor, change, options, userId) { | ||
// only fire this for the user that changed the hp in the first place | ||
if (userId !== game.user.id) { return;} | ||
// only fire if the HP changed | ||
if(change.data?.attributes?.hp?.hasOwnProperty('value')) { | ||
DnD4eTools.log(false, "Firing bloodied update for " + game.users.get(userId).data.name) | ||
const newHP = change.data.attributes.hp.value | ||
const maxHP = actor.data.data.attributes.hp.max | ||
const bloodiedHP = maxHP / 2 | ||
if (!maxHP) { | ||
DnD4eTools.log(false, "Could not get actor max HP") | ||
return; | ||
} | ||
DnD4eTools.log(false, newHP + "/" + maxHP) | ||
|
||
// remove any of bloodied, dying or dead as a batch or else we get weird concurrent update issues | ||
const statusIds = findEffectIds(bloodied, actor).concat(findEffectIds(dying, actor)).concat(findEffectIds(dead, actor)) | ||
await actor.deleteEmbeddedDocuments("ActiveEffect", statusIds) | ||
|
||
if (newHP <= bloodiedHP && newHP > 0 && game.settings.get(DnD4eTools.ID, DnD4eTools.SETTINGS.BLOODIED_ICON)) { | ||
applyEffectIfNotPresent(bloodied, actor) | ||
} | ||
if (newHP <= 0 && game.settings.get(DnD4eTools.ID, DnD4eTools.SETTINGS.DEAD_ICON)) { | ||
if (actor.data.type === 'NPC') { | ||
DnD4eTools.log(false, "NPC Dead!") | ||
applyEffectIfNotPresent(dead, actor) | ||
defeatInCombat(actor) | ||
} | ||
else { | ||
if (newHP <= 0 - bloodiedHP) { | ||
DnD4eTools.log(false, "PC Dead!") | ||
applyEffectIfNotPresent(dead, actor) | ||
defeatInCombat(actor) | ||
} | ||
else { | ||
DnD4eTools.log(false, "PC Dying!") | ||
applyEffectIfNotPresent(dying, actor) | ||
defeatInCombat(actor, false) | ||
} | ||
} | ||
} | ||
if (newHP > 0 && game.settings.get(DnD4eTools.ID, DnD4eTools.SETTINGS.DEAD_ICON)) { | ||
defeatInCombat(actor, false) | ||
} | ||
} | ||
|
||
function defeatInCombat(actor, defeated = true) { | ||
const activeCombat = game.combat | ||
// if we are not runnign a combat, stop now | ||
if (!activeCombat) { return } | ||
const updates = [] | ||
for (const token of actor.getActiveTokens()) { | ||
const matchingCombatants = activeCombat.combatants?.contents?.filter(c => c.data.tokenId === token.id) | ||
if (!matchingCombatants || matchingCombatants.length === 0) { | ||
continue | ||
} | ||
const tokenUpdates = matchingCombatants.map(combatant => { | ||
return { | ||
_id: combatant.id, | ||
defeated: defeated | ||
} | ||
}) | ||
|
||
tokenUpdates.forEach(update => updates.push(update)) | ||
} | ||
|
||
if (!updates.length) { return } | ||
DnD4eTools.log(false, updates) | ||
activeCombat.updateEmbeddedDocuments("Combatant", updates) | ||
} | ||
|
||
function applyEffectIfNotPresent(statusToCheck, actor) { | ||
const bloodiedEffect = actor.effects.find(x => x.data.flags.core?.statusId === statusToCheck) | ||
if (bloodiedEffect) { | ||
DnD4eTools.log(false, `Actor already has ${statusToCheck}, not reapplying`) | ||
return | ||
} | ||
else { | ||
DnD4eTools.log(false, `Actor does not have status ${statusToCheck}. Applying it`) | ||
} | ||
|
||
const status = CONFIG.statusEffects.find(x => x.id === statusToCheck) | ||
const effect = { | ||
...status, | ||
"label" : game.i18n.localize(status.label), | ||
"flags": { | ||
"core": { | ||
"statusId": statusToCheck, | ||
"overlay": true | ||
} | ||
} | ||
} | ||
delete effect.id | ||
|
||
ActiveEffect.create(effect, { parent : actor }) | ||
} | ||
|
||
function findEffectIds(statusToCheck, actor) { | ||
return actor.effects.filter(effect => effect.data.flags.core?.statusId === statusToCheck).map(effect => effect.id) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.