Full SAN management

This commit is contained in:
2025-06-15 23:02:36 +02:00
parent 9e4d76298c
commit 5ba88a1ae5
27 changed files with 433 additions and 41 deletions

View File

@ -78,6 +78,7 @@ export default class CthulhuEternalProtagonistSheet extends CthulhuEternalActorS
context.enrichedDescription = await foundry.applications.ux.TextEditor.implementation.enrichHTML(this.document.system.description, { async: true })
context.enrichedNotes = await foundry.applications.ux.TextEditor.implementation.enrichHTML(this.document.system.notes, { async: true })
context.isGM = game.user.isGM
context.tooltipsCharacteristic = {
str: game.i18n.localize("CTHULHUETERNAL.Characteristic.Str"),

View File

@ -568,7 +568,7 @@ export default class CthulhuEternalRoll extends Roll {
{ rollMode: rollMode },
)
console.log("Roll to message", this.options, this.options.rollData, this.options.rollItem)
// Manage the skill evolution if the roll is a failure
let rollData = this.options.rollData || this.options
let rollItem = this.options.rollItem
if (rollData.resultType.includes("failure") && rollItem.type === "skill") {
@ -594,6 +594,20 @@ export default class CthulhuEternalRoll extends Roll {
}
}
// If the roll is a SAN roll, we propose to select the SAN loss
if (rollData.rollType === "san") {
let msgData = {
rollItem: rollItem,
rollData: rollData
}
let msg = await foundry.applications.handlebars.renderTemplate("systems/fvtt-cthulhu-eternal/templates/chat-san-request.hbs", msgData)
let chatMsg = await ChatMessage.create({
user: game.user.id,
content: msg,
speaker: ChatMessage.getSpeaker({ actor: rollData.actor })
}, { rollMode: rollData.rollMode, create: true })
await chatMsg.setFlag("fvtt-cthulhu-eternal", "rollData", rollData)
}
}
}

View File

@ -14,7 +14,8 @@ export default class CthulhuEternalProtagonist extends foundry.abstract.TypeData
const characteristicField = (label) => {
const schema = {
value: new fields.NumberField({ ...requiredInteger, initial: 3, min: 0 }),
feature: new fields.StringField({ required: true, nullable: false, initial: "" })
feature: new fields.StringField({ required: true, nullable: false, initial: "" }),
max: new fields.NumberField({ ...requiredInteger, initial: 3, min: 0 })
}
return new fields.SchemaField(schema, { label })
}
@ -47,6 +48,7 @@ export default class CthulhuEternalProtagonist extends foundry.abstract.TypeData
violence: new fields.ArrayField(new fields.BooleanField(), { required: true, initial: [false, false, false], min: 3, max: 3 }),
helplessness: new fields.ArrayField(new fields.BooleanField(), { required: true, initial: [false, false, false], min: 3, max: 3 }),
breakingPoint: new fields.NumberField({ ...requiredInteger, initial: 0, min: 0 }),
breakingPointReached: new fields.BooleanField({ required: true, initial: false }),
insanity: new fields.StringField({ required: true, nullable: false, initial: "none", choices: SYSTEM.INSANITY }),
})
@ -132,6 +134,15 @@ export default class CthulhuEternalProtagonist extends foundry.abstract.TypeData
updates[`system.damageBonus`] = dmgBonus
}
// BP (Breaking Point) management
if (!this.san.breakingPointReached && this.san.value <= this.san.breakingPoint) {
updates[`system.san.breakingPointReached`] = true
ChatMessage.create({
content: `<p>${game.i18n.format("CTHULHUETERNAL.Label.breakingPointReached", { bp: this.san.breakingPoint, san: this.san.value })}</p>`,
speaker: ChatMessage.getSpeaker({ actor: this.parent })
})
}
// Unconsciousness management
if (!this.hp.unconscious && this.hp.value <= 2) {
updates[`system.hp.unconscious`] = true
@ -183,6 +194,108 @@ export default class CthulhuEternalProtagonist extends foundry.abstract.TypeData
}
}
async applySANConsequences(rollData) {
// If sanType is "non", do nothing
if (rollData.sanType === "none") {
return
}
let msgData = {
sanType: rollData.sanType,
sanLoss: rollData.sanLoss,
actorId: this.parent.id,
actorName: this.parent.name,
adaptedToHelplessness: this.biodata.adaptedToHelplessness,
adaptedToViolence: this.biodata.adaptedToViolence,
... rollData
}
let updates = {}
let template = ""
// Manage temporary insanity
if (rollData.sanLoss >= 5) {
rollData.resetMsg = false
if (rollData.sanType === "violence" && !this.biodata.adaptedToViolence) {
updates[`system.san.violence`] = [false, false, false]
rollData.resetMsg = "CTHULHUETERNAL.Label.sanViolenceReset"
}
if (rollData.sanType === "helplessness" && !this.biodata.adaptedToHelplessness) {
updates[`system.san.helplessness`] = [false, false, false]
rollData.resetMsg = "CTHULHUETERNAL.Label.sanHelplessnessReset"
}
template = "systems/fvtt-cthulhu-eternal/templates/chat-san-temp-insanity.hbs"
} else if (rollData.sanLoss === 0) { // Manage if sanLoss is 0
rollData.resetMsg = false
if (rollData.sanType === "violence" && !this.biodata.adaptedToViolence) {
updates[`system.san.violence`] = [false, false, false]
rollData.resetMsg = "CTHULHUETERNAL.Label.sanViolenceReset"
}
if (rollData.sanType === "helplessness" && !this.biodata.adaptedToHelplessness) {
updates[`system.san.helplessness`] = [false, false, false]
rollData.resetMsg = "CTHULHUETERNAL.Label.sanHelplessnessReset"
}
template = "systems/fvtt-cthulhu-eternal/templates/chat-san-loss-0.hbs"
} else if (rollData.sanType === "violence" ) {
// Set the first false element of the violence array to true
let violence = this.san.violence.slice()
let index = violence.findIndex(v => !v)
if (index !== -1) {
violence[index] = true
updates[`system.san.violence`] = violence
}
template = "systems/fvtt-cthulhu-eternal/templates/chat-san-loss-1-4.hbs"
// Check if all violence elements are true, if so, set adaptedToViolence to true
if (violence.every(v => v)) {
updates[`system.biodata.adaptedToViolence`] = true
updates[`system.san.violence`] = [false, false, false]
msgData.adaptedToViolence = true
}
} else if (rollData.sanType === "helplessness" ) {
// If sanType is "helplessness" and adapted to helplessness, set the first false element of the helplessness array to true
let helplessness = this.san.helplessness.slice()
let index = helplessness.findIndex(h => !h)
if (index !== -1) {
helplessness[index] = true
updates[`system.san.helplessness`] = helplessness
}
template = "systems/fvtt-cthulhu-eternal/templates/chat-san-loss-1-4.hbs"
// Check if all helplessness elements are true, if so, set adaptedToHelplessness to true
if (helplessness.every(h => h)) {
updates[`system.biodata.adaptedToHelplessness`] = true
updates[`system.san.helplessness`] = [false, false, false]
msgData.adaptedToHelplessness = true
}
}
let content = await foundry.applications.handlebars.renderTemplate(template, msgData)
let msg = await ChatMessage.create({
content: content,
speaker: ChatMessage.getSpeaker({ actor: this.parent })
})
msg.setFlag("fvtt-cthulhu-eternal", "rollData", msgData)
if (Object.keys(updates).length > 0) {
this.parent.update(updates)
}
}
async modifySAN(rollData) {
let updates = {}
let san = Math.max(Math.min(this.san.value + rollData.sanLoss, this.san.max), 0)
if (this.san.value !== san) {
updates[`system.san.value`] = san
const content = await foundry.applications.handlebars.renderTemplate("systems/fvtt-cthulhu-eternal/templates/chat-san-type-request.hbs", rollData)
let msg = await ChatMessage.create({
content: content,
speaker: ChatMessage.getSpeaker({ actor: this.parent })
})
msg.setFlag("fvtt-cthulhu-eternal", "rollData", rollData)
}
if (Object.keys(updates).length > 0) {
this.parent.update(updates)
}
}
isStunned() {
return this.hp.stunned
}
@ -215,6 +328,7 @@ export default class CthulhuEternalProtagonist extends foundry.abstract.TypeData
let bp = Math.max(this.san.value - this.characteristics.pow.value, 0)
if (this.san.breakingPoint !== bp) {
updates[`system.san.breakingPoint`] = bp
updates[`system.san.breakingPointReached`] = false // Reset breaking point reached
}
if (Object.keys(updates).length > 0) {
this.parent.update(updates)

View File

@ -180,6 +180,55 @@ export default class CthulhuEternalUtils {
});
}
static async applySANType(rollMessage, event) {
let rollData = rollMessage.getFlag("fvtt-cthulhu-eternal", "rollData")
if (!rollData) {
ui.notifications.error(game.i18n.localize("CTHULHUETERNAL.Notifications.noRollDataFound"))
return
}
let actor = game.actors.get(rollData.actorId)
if (!actor) {
ui.notifications.error(game.i18n.localize("CTHULHUETERNAL.Notifications.noActorFound"))
return
}
let sanType = event.currentTarget.dataset.sanType;
if (!sanType) {
ui.notifications.error(game.i18n.localize("CTHULHUETERNAL.Notifications.noSanTypeFound"))
return
}
// If the sanType is "none", we don't apply any SAN processing
if (sanType === "none") {
ui.notifications.info(game.i18n.localize("CTHULHUETERNAL.Notifications.noSanLossApplied"))
return
}
rollData.sanType = sanType
await actor.system.applySANConsequences(rollData)
// Delete the roll message
await rollMessage.delete()
}
static async applySANLoss(rollMessage, event) {
let rollData = rollMessage.getFlag("fvtt-cthulhu-eternal", "rollData")
if (!rollData) {
ui.notifications.error(game.i18n.localize("CTHULHUETERNAL.Notifications.noRollDataFound"))
return
}
let actor = game.actors.get(rollData.actorId)
if (!actor) {
ui.notifications.error(game.i18n.localize("CTHULHUETERNAL.Notifications.noActorFound"))
return
}
// Get the san loss from the event : data-san-value
let sanLoss = event.currentTarget.dataset.sanValue
let r = new Roll(sanLoss.toString())
await r.evaluate()
rollData.sanLoss = -r.total
await actor.system.modifySAN(rollData)
// Delete the roll message
await rollMessage.delete()
}
static async healingRoll(rollMessage) {
let rollData = rollMessage.rolls[0]?.options?.rollData
let healingFormula = rollData.rollItem.system.healingFormula