generated from League-of-Foundry-Developers/FoundryVTT-Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
acd3134
commit ce2532f
Showing
1 changed file
with
46 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,63 @@ | ||
import { eldenRingNounVerbed } from "./helpers/animation/text/fromSoftwareText.js"; | ||
import { getSetting } from "./helpers/misc.js"; | ||
|
||
/** | ||
* Handles pre-deletion actions for combat encounters. | ||
* @param {Object} encounter - The combat encounter object. | ||
* @param {*} _changed - Unused parameter. | ||
* @param {*} _userid - Unused parameter. | ||
*/ | ||
export async function preDeleteCombat(encounter, _changed, _userid) { | ||
// Only proceed if the user is a GM | ||
if (!game.user.isGM) return; | ||
|
||
const xpNeeded = getSetting('from-software.noun-verbed.text'); | ||
|
||
// If xpNeeded is 0, trigger the animation and exit | ||
if (xpNeeded === 0) { | ||
await eldenRingNounVerbed(); | ||
return; | ||
} | ||
|
||
// Extract combatants from the encounter | ||
const combatants = encounter.combatants.map(c => c?.token); | ||
const enemyLevels = combatants.filter(t => t?.disposition === CONST.TOKEN_DISPOSITIONS.HOSTILE).map(t => t?.actor?.level ?? 0); | ||
const hazardLevels = combatants.filter(t => t?.actor?.type === 'hazard').map(t => t?.actor?.level ?? 0); | ||
|
||
// Calculate enemy and hazard levels | ||
const enemyLevels = getActorLevels(combatants, t => t?.disposition === CONST.TOKEN_DISPOSITIONS.HOSTILE); | ||
const hazardLevels = getActorLevels(combatants, t => t?.actor?.type === 'hazard'); | ||
|
||
// Get party members involved in the combat | ||
const partyMemberIDs = game.actors?.party?.members?.map(a => a.uuid) ?? []; | ||
const partyCombatMembers = combatants.filter(t => partyMemberIDs.includes(t?.actor?.uuid)); | ||
const partyCombatLevel = Math.round(partyCombatMembers.map(p => p?.actor?.level ?? 0).reduce((a, b) => a + b) / partyCombatMembers.length); | ||
const xp = game.pf2e.gm.calculateXP(partyCombatLevel, partyCombatMembers.length, enemyLevels, hazardLevels, {}) | ||
|
||
// Calculate average party level | ||
const partyCombatLevel = calculateAverageLevel(partyCombatMembers); | ||
|
||
// Calculate XP | ||
const xp = game.pf2e.gm.calculateXP(partyCombatLevel, partyCombatMembers.length, enemyLevels, hazardLevels, {}); | ||
|
||
// Trigger animation if conditions are met | ||
if (getSetting('from-software.noun-verbed.enable') && xp >= xpNeeded) { | ||
await eldenRingNounVerbed(); | ||
} | ||
} | ||
|
||
/** | ||
* Extracts actor levels based on a filter condition. | ||
* @param {Array} combatants - Array of combatants. | ||
* @param {Function} filterCondition - Condition to filter combatants. | ||
* @returns {Array} Array of actor levels. | ||
*/ | ||
function getActorLevels(combatants, filterCondition) { | ||
return combatants.filter(filterCondition).map(t => t?.actor?.level ?? 0); | ||
} | ||
|
||
/** | ||
* Calculates the average level of party members. | ||
* @param {Array} partyMembers - Array of party members. | ||
* @returns {number} Average level rounded to the nearest integer. | ||
*/ | ||
function calculateAverageLevel(partyMembers) { | ||
const totalLevel = partyMembers.reduce((sum, p) => sum + (p?.actor?.level ?? 0), 0); | ||
return Math.round(totalLevel / partyMembers.length); | ||
} |