ATtempt to fix init rolls

This commit is contained in:
2026-04-07 20:43:15 +02:00
parent 3ad5681539
commit 1bf88bac06
33 changed files with 134 additions and 124 deletions

View File

@@ -317,6 +317,7 @@ Hooks.on(hookName, (message, html, data) => {
const attackKey = button.data("attack-key")
let attackerId = button.data("attacker-id")
const defenderId = button.data("defender-id")
const defenderTokenId = button.data("defender-token-id") || null
const damageType = button.data("damage-type")
const damageFormula = button.data("damage-formula")
const damageModifier = button.data("damage-modifier")
@@ -331,7 +332,7 @@ Hooks.on(hookName, (message, html, data) => {
// Pour les boutons de résultat de combat (monster damage)
if (damageType === "monster" && attackKey) {
await actor.system.prepareMonsterRoll("monster-damage", attackKey, undefined, undefined, undefined, defenderId)
await actor.system.prepareMonsterRoll("monster-damage", attackKey, undefined, undefined, undefined, defenderId, defenderTokenId)
return
}
@@ -350,7 +351,7 @@ Hooks.on(hookName, (message, html, data) => {
// Lancer les dégâts avec la bonne méthode
const rollType = damageType === "small" ? "weapon-damage-small" : "weapon-damage-medium"
await actor.prepareRoll(rollType, weaponId, undefined, defenderId)
await actor.prepareRoll(rollType, weaponId, undefined, defenderId, defenderTokenId)
})
// Masquer les boutons de dommages dans les messages de résultat de combat si l'utilisateur n'est pas l'attaquant
@@ -409,7 +410,7 @@ Hooks.on("createChatMessage", async (message) => {
return
}
const { attackerId, attackRoll, attackerName, defenderName, attackWeaponId, attackRollType, attackRollKey, defenderId } = attackData
const { attackerId, attackRoll, attackerName, defenderName, attackWeaponId, attackRollType, attackRollKey, defenderId, defenderTokenId } = attackData
let defenseRoll = message.rolls[0]?.options?.rollTotal || message.rolls[0]?.total || 0
console.log("Processing defense:", { attackRoll, defenseRoll, attackerId, defenderId })
@@ -487,6 +488,7 @@ Hooks.on("createChatMessage", async (message) => {
attackRollKey,
defenderName,
defenderId,
defenderTokenId,
defenseRoll
})
} else {
@@ -512,7 +514,7 @@ Hooks.on("createChatMessage", async (message) => {
// 1. Si l'attaquant a un propriétaire joueur, seul ce joueur applique
// 2. Si l'attaquant n'a que le MJ comme propriétaire (monstre), seul le MJ applique
const attackerOwners = attacker ? game.users.filter(u =>
!u.isGM && attacker.testUserPermission(u, "OWNER")
u.active && !u.isGM && attacker.testUserPermission(u, "OWNER")
) : []
let shouldApplyDamage = false
@@ -552,11 +554,13 @@ Hooks.on("createChatMessage", async (message) => {
const armorDR = defender.computeDamageReduction() || 0
const finalDamage = Math.max(0, damageTotal - armorDR)
// For unlinked tokens (default for monsters), we need the specific token actor, not the base
// world actor — otherwise applyDamage would modify the base actor and affect every unlinked
// copy of that monster. Prefer the combatant actor, fall back to canvas scan.
// Prefer the token ID stored in roll options (set at attack time when the exact token is known).
// For unlinked tokens (default for monsters), this ensures we target the right instance even
// when multiple unlinked copies of the same monster type are in combat.
const rollDefenderTokenId = message.rolls[0]?.options?.defenderTokenId
const defenderCombatant = game.combat?.combatants?.find(c => c.actorId === defender.id)
const defenderTokenId = defenderCombatant?.token?.id
const defenderTokenId = rollDefenderTokenId
?? defenderCombatant?.token?.id
?? canvas.tokens?.placeables?.find(t => t.actor?.id === defender.id)?.id
?? null
@@ -564,7 +568,11 @@ Hooks.on("createChatMessage", async (message) => {
// route the HP update to the GM via socket. The confirmation message is still created here
// since all users can create chat messages.
if (defender.isOwner) {
const tokenActor = defenderCombatant?.actor ?? defender
// Resolve the token actor: prefer lookup by token ID (exact match for unlinked monsters),
// fall back to combatant actor, then base world actor.
const tokenActor = (defenderTokenId
? canvas.tokens?.placeables?.find(t => t.id === defenderTokenId)?.actor
: defenderCombatant?.actor) ?? defender
await tokenActor.applyDamage(-finalDamage)
} else {
game.socket.emit(`system.${SYSTEM.id}`, { type: "applyDamage", actorId: defender.id, tokenId: defenderTokenId, damage: -finalDamage })