bol/module/actor/actor.js
2022-07-07 13:33:31 +02:00

660 lines
20 KiB
JavaScript

import { BoLDefaultRoll } from "../controllers/bol-rolls.js";
import { BoLUtility } from "../system/bol-utility.js";
/**
* Extend the base Actor entity by defining a custom roll data structure which is ideal for the Simple system.
* @extends {Actor}
*/
export class BoLActor extends Actor {
/** @override */
prepareData() {
if (this.type === 'character') {
this.chartype = 'player'
this.villainy = false
}
if (this.type === 'encounter') {
this.chartype = 'tough'
this.villainy = true
}
super.prepareData()
}
/* -------------------------------------------- */
getCharType() {
if (this.type === 'character') {
return 'player'
}
return 'tough'
}
/* -------------------------------------------- */
getVillainy() {
if (this.type === 'character') {
return false
}
return true
}
/* -------------------------------------------- */
updateResourcesData() {
if (this.type == 'character') {
let newVitality = 10 + this.system.attributes.vigor.value + this.system.resources.hp.bonus
if (this.system.resources.hp.max != newVitality) {
this.update({ 'system.resources.hp.max': newVitality })
}
let newPower = 10 + this.system.attributes.mind.value + this.system.resources.power.bonus
if (this.system.resources.power.max != newPower) {
this.update({ 'system.resources.power.max': newPower })
}
}
}
/* -------------------------------------------- */
prepareDerivedData() {
super.prepareDerivedData()
this.updateResourcesData()
this.manageHealthState();
}
/* -------------------------------------------- */
get details() {
return this.system.details
}
get attributes() {
return Object.values(this.system.attributes)
}
get aptitudes() {
return Object.values(this.system.aptitudes)
}
/* -------------------------------------------- */
clearRoundModifiers() { // Process data/items that are finished at end of a round
let foList = this.fightoptions
for (let fo of foList) {
if (fo.system.properties.used) {
this.updateEmbeddedDocuments("Item", [{ _id: fo._id, 'system.properties.used': false }])
}
}
}
/* -------------------------------------------- */
get defenseValue() {
let defMod = 0
let fo = this.getActiveFightOption()
if (fo && fo.system.properties.fightoptiontype == "intrepid") {
defMod += -2
}
if (fo && fo.system.properties.fightoptiontype == "fulldefense") {
defMod += 2
}
if (fo && fo.system.properties.fightoptiontype == "twoweaponsdef" && !fo.system.properties.used) {
defMod += 1
this.updateEmbeddedDocuments("Item", [{ _id: fo._id, 'system.properties.used': true }])
}
if (fo && fo.system.properties.fightoptiontype == "defense") {
defMod += 1
}
if (fo && fo.system.properties.fightoptiontype == "attack") {
defMod += -1
}
return this.system.aptitudes.def.value + defMod
}
/* -------------------------------------------- */
getActiveFightOption() {
let it = this.items.find(i => i.type === "feature" && i.system.subtype === "fightoption" && i.system.properties.activated)
if (it) {
return duplicate(it)
}
return undefined
}
/* -------------------------------------------- */
incAttributeXP(key) {
let attr = duplicate(this.system.attributes[key])
if (attr) {
let nextXP = (attr.value == -1) ? 2 : attr.value + (attr.value + 1)
let xp = duplicate(this.system.xp)
if (xp.total - xp.spent >= nextXP) {
attr.value += 1
xp.spent += nextXP
this.update({ [`system.attributes.${key}`]: attr, [`system.xp`]: xp })
} else {
ui.notifications.warn("Pas assez de points d'expérience !")
}
}
}
/* -------------------------------------------- */
incAptitudeXP(key) {
let apt = duplicate(this.system.aptitudes[key])
if (apt) {
let nextXP = (apt.value == -1) ? 1 : apt.value + 2
let xp = duplicate(this.system.xp)
if (xp.total - xp.spent >= nextXP) {
apt.value += 1
xp.spent += nextXP
this.update({ [`system.aptitudes.${key}`]: apt, [`system.xp`]: xp })
} else {
ui.notifications.warn("Pas assez de points d'expérience !")
}
}
}
/* -------------------------------------------- */
incCareerXP(itemId) {
let career = this.items.get(itemId)
if (career) {
career = duplicate(career)
let nextXP = career.system.rank + 1
let xp = duplicate(this.system.xp)
if (xp.total - xp.spent >= nextXP) {
xp.spent += nextXP
this.update({ [`system.xp`]: xp })
this.updateEmbeddedDocuments('Item', [{ _id: career._id, 'system.rank': career.system.rank + 1 }])
} else {
ui.notifications.warn("Pas assez de points d'expérience !")
}
}
}
/* -------------------------------------------- */
async toggleFightOption(itemId) {
let fightOption = this.items.get(itemId)
let state
let updates = []
if (fightOption) {
fightOption = duplicate(fightOption)
if (fightOption.system.properties.activated) {
state = false
} else {
state = true
}
updates.push({ _id: fightOption._id, 'system.properties.activated': state }) // Update the selected one
await this.updateEmbeddedDocuments("Item", updates) // Apply all changes
// Then notify
ChatMessage.create({
alias: this.name,
whisper: BoLUtility.getWhisperRecipientsAndGMs(this.name),
content: await renderTemplate('systems/bol/templates/chat/chat-activate-fight-option.hbs', { name: this.name, img: fightOption.img, foName: fightOption.name, state: state })
})
}
}
/*-------------------------------------------- */
get armorMalusValue() { // used for Fight Options
for (let armor of this.armors) {
if (armor.system.properties.armorQuality.includes("light")) {
return 1
}
if (armor.system.properties.armorQuality.includes("medium")) {
return 2
}
if (armor.system.properties.armorQuality.includes("heavy")) {
return 3
}
}
return 0
}
get resources() {
return Object.values(this.system.resources)
}
get boons() {
return duplicate(this.items.filter(i => i.type === "feature" && i.system.subtype === "boon") || []);
}
get flaws() {
return duplicate(this.items.filter(i => i.type === "feature" && i.system.subtype === "flaw") || []);
}
get careers() {
return duplicate( this.items.filter(i => i.type === "feature" && i.system.subtype === "career") || [])
}
get origins() {
return this.items.filter(i => i.type === "feature" && i.system.subtype === "origin");
}
get races() {
return this.items.filter(i => i.type === "feature" && i.system.subtype === "race");
}
get languages() {
return this.items.filter(i => i.type === "feature" && i.system.subtype === "language")
}
get fightoptions() {
return this.items.filter(i => i.type === "feature" && i.system.subtype === "fightoption")
}
get godsfaith() {
return this.items.filter(i => i.type === "feature" && i.system.subtype === "godsfaith")
}
get features() {
return this.items.filter(i => i.type === "feature")
}
get equipment() {
return this.items.filter(i => i.type === "item")
}
get equipmentCreature() {
return this.items.filter(i => i.type === "item" && i.system.category === "equipment" && (( i.system.subtype === "weapon" && i.system.properties.natural === true) || (i.system.subtype === "armor")) )
}
get armors() {
return this.items.filter(i => i.type === "item" && i.system.category === "equipment" && i.system.subtype === "armor");
}
get helms() {
return this.items.filter(i => i.type === "item" && i.system.category === "equipment" && i.system.subtype === "helm");
}
get shields() {
return this.items.filter(i => i.type === "item" && i.system.category === "equipment" && i.system.subtype === "shield");
}
get weapons() {
return this.items.filter(i => i.type === "item" && i.system.category === "equipment" && i.system.subtype === "weapon");
}
get protections() {
return this.armors.concat(this.helms).concat(this.shields)
}
get spells() {
return this.items.filter(i => i.type === "item" && i.system.category === "spell");
}
get alchemy() {
return this.items.filter(i => i.type === "item" && i.system.category === "alchemy");
}
get melee() {
return this.weapons.filter(i => i.system.properties.melee === true);
}
get natural() {
return this.weapons.filter(i => i.system.properties.natural === true);
}
get ranged() {
return this.weapons.filter(i => i.system.properties.ranged === true);
}
get containers() {
return this.items.filter(i => i.type === "item" && i.system.category === "equipment" && i.system.subtype === "container");
}
get treasure() {
return this.items.filter(i => i.type === "item" && i.system.category === "equipment" && i.system.subtype === "currency");
}
get vehicles() {
return this.items.filter(i => i.type === "item" && i.system.category === "vehicle");
}
get ammos() {
return this.items.filter(i => i.type === "item" && i.system.category === "equipment" && i.system.subtype === "ammunition");
}
get misc() {
return this.items.filter(i => i.type === "item" && i.system.category === "equipment" && (i.system.subtype === "other" || i.system.subtype === "container" || i.system.subtype === "scroll" || i.system.subtype === "jewel"));
}
get bonusBoons() {
let boons = this.items.filter(i => i.type === "feature" && i.system.subtype === "boon" && i.system.properties.isbonusdice)
return duplicate( boons || [])
}
get malusFlaws() {
return duplicate( this.items.filter(i => i.type === "feature" && i.system.subtype === "flaw" && i.system.properties.ismalusdice) || []);
}
isSorcerer() {
if (this.careers.find(item => item.system.properties.sorcerer == true))
return true
return false
}
isAlchemist() {
if (this.careers.find(item => item.system.properties.alchemist == true))
return true
return false
}
isPriest() {
if (this.careers.find(item => item.system.properties.priest == true))
return true
return false
}
/*-------------------------------------------- */
getPPCostArmor() {
let armors = this.armors
let ppCostArmor = 0
for (let armor of armors) {
if (armor.system.worn) {
ppCostArmor += Number(armor.system.properties.modifiers.powercost) || 0
}
}
return ppCostArmor
}
/*-------------------------------------------- */
getArmorAgiMalus() {
let malusAgi = 0
for (let armor of this.protections) {
if (armor.system.worn) {
malusAgi += Number(armor.system.properties.modifiers.agility) || 0
}
}
return malusAgi
}
/*-------------------------------------------- */
getArmorInitMalus() {
let malusInit = 0
for (let armor of this.protections) {
if (armor.system.worn) {
malusInit += Number(armor.system.properties.modifiers.init) || 0
}
}
return malusInit
}
/*-------------------------------------------- */
spendPowerPoint(ppCost) {
let newPP = this.system.resources.power.value - ppCost
newPP = (newPP < 0) ? 0 : newPP
this.update({ 'data.resources.power.value': newPP })
return newPP
}
/*-------------------------------------------- */
resetAlchemyStatus(alchemyId) {
let alchemy = this.items.get(alchemyId)
if (alchemy) {
this.updateEmbeddedDocuments('Item', [{ _id: alchemy.id, 'data.properties.pccurrent': 0 }])
}
}
/*-------------------------------------------- */
async spendAlchemyPoint(alchemyId, pcCost) {
let alchemy = this.items.get(alchemyId)
if (alchemy) {
pcCost = Number(pcCost) ?? 0
if (this.system.resources.alchemypoints.value >= pcCost) {
let newPC = this.system.resources.alchemypoints.value - pcCost
newPC = (newPC < 0) ? 0 : newPC
this.update({ 'data.resources.alchemypoints.value': newPC })
newPC = alchemy.system.properties.pccurrent + pcCost
await this.updateEmbeddedDocuments('Item', [{ _id: alchemy.id, 'data.properties.pccurrent': newPC }])
} else {
ui.notifications.warn("Plus assez de Points de Création !")
}
}
}
/*-------------------------------------------- */
getAlchemistBonus() {
let sorcerer = this.careers.find(item => item.system.properties.alchemist == true)
if (sorcerer) {
return sorcerer.system.rank
}
return 0;
}
/*-------------------------------------------- */
getSorcererBonus() {
let sorcerer = this.careers.find(item => item.system.properties.sorcerer == true)
if (sorcerer) {
return sorcerer.system.rank
}
return 0;
}
/*-------------------------------------------- */
heroReroll() {
if (this.type == 'character') {
return this.system.resources.hero.value > 0;
} else {
if (this.system.type == 'adversary') {
return this.system.resources.hero.value > 0;
}
}
return false
}
getResourcesFromType() {
let resources = {};
if (this.type == 'encounter') {
resources['hp'] = this.system.resources.hp;
if (this.system.type != 'base') {
resources['faith'] = this.system.resources.faith
resources['power'] = this.system.resources.power
}
if (this.system.type == 'adversary') {
resources['hero'] = duplicate(this.system.resources.hero)
resources['hero'].label = "BOL.resources.villainy"
}
} else {
resources = this.system.resources;
}
return resources
}
buildFeatures() {
return {
"careers": {
"label": "BOL.featureCategory.careers",
"ranked": true,
"items": this.careers
},
"origins": {
"label": "BOL.featureCategory.origins",
"ranked": false,
"items": this.origins
},
"races": {
"label": "BOL.featureCategory.races",
"ranked": false,
"items": this.races
},
"boons": {
"label": "BOL.featureCategory.boons",
"ranked": false,
"items": this.boons
},
"flaws": {
"label": "BOL.featureCategory.flaws",
"ranked": false,
"items": this.flaws
},
"languages": {
"label": "BOL.featureCategory.languages",
"ranked": false,
"items": this.languages
},
"fightoptions": {
"label": "BOL.featureCategory.fightoptions",
"ranked": false,
"items": this.fightoptions
},
"godsfaith": {
"label": "BOL.featureSubtypes.gods",
"ranked": false,
"items": this.godsfaith
}
}
}
buildCombat() {
return {
"melee": {
"label": "BOL.combatCategory.melee",
"weapon": true,
"protection": false,
"blocking": false,
"ranged": false,
"options": false,
"items": this.melee
},
"ranged": {
"label": "BOL.combatCategory.ranged",
"weapon": true,
"protection": false,
"blocking": false,
"ranged": true,
"options": false,
"items": this.ranged
},
"protections": {
"label": "BOL.combatCategory.protections",
"weapon": false,
"protection": true,
"blocking": false,
"ranged": false,
"options": false,
"items": this.protections
},
"shields": {
"label": "BOL.combatCategory.shields",
"weapon": false,
"protection": false,
"blocking": true,
"ranged": false,
"options": false,
"items": this.shields
},
"fightoptions": {
"label": "BOL.combatCategory.fightOptions",
"weapon": false,
"protection": false,
"blocking": false,
"ranged": false,
"options": true,
"items": this.fightoptions
}
}
}
buildCombatCreature() {
return {
"natural": {
"label": "BOL.combatCategory.natural",
"weapon": true,
"protection": false,
"blocking": false,
"ranged": false,
"options": false,
"items": this.natural
},
"protections": {
"label": "BOL.combatCategory.protections",
"weapon": false,
"protection": true,
"blocking": false,
"ranged": false,
"options": false,
"items": this.protections
},
}
}
/*-------------------------------------------- */
buildRollList() {
let rolls = []
for (let key in this.system.attributes) {
let attr = this.system.attributes[key]
rolls.push({ key: key, value: attr.value, name: attr.label, type: "attribute" })
}
for (let key in this.system.aptitudes) {
if (key != "def") {
let apt = this.system.aptitudes[key]
rolls.push({ key: key, value: apt.value, name: apt.label, type: "aptitude" })
}
}
return rolls
}
/*-------------------------------------------- */
buildListeActions() {
return this.melee.concat(this.ranged).concat(this.natural)
}
/*-------------------------------------------- */
async manageHealthState() {
let hpID = "lastHP" + this.id
let lastHP = await this.getFlag("world", hpID)
if (lastHP != this.system.resources.hp.value && game.user.isGM ) { // Only GM sends this
await this.setFlag("world", hpID, this.system.resources.hp.value)
if (this.system.resources.hp.value <= 0) {
ChatMessage.create({
alias: this.name,
whisper: BoLUtility.getWhisperRecipientsAndGMs(this.name),
content: await renderTemplate('systems/bol/templates/chat/chat-vitality-zero.hbs', { name: this.name, img: this.img, hp: this.system.resources.hp.value })
})
}
}
}
/*-------------------------------------------- */
registerInit(initScore, isCritical, isFumble) {
this.update({ 'system.combat.lastinit': initScore, 'system.combat.iscritical': isCritical, 'system.combat.isfumble': isFumble })
}
/*-------------------------------------------- */
getLastInitData() {
return this.system.combat
}
/*-------------------------------------------- */
async subHeroPoints(nb) {
let newHeroP = this.system.resources.hero.value - nb;
newHeroP = (newHeroP < 0) ? 0 : newHeroP;
await this.update({ 'system.resources.hero.value': newHeroP });
}
/*-------------------------------------------- */
async sufferDamage(damage) {
let newHP = this.system.resources.hp.value - damage
await this.update({ 'system.resources.hp.value': newHP })
}
/* -------------------------------------------- */
getArmorFormula() {
let protectWorn = this.protections.filter(item => item.system.worn)
let formula = ""
for (let protect of protectWorn) {
if (protect.system.subtype == 'helm') {
formula += "+1"
} else if (protect.system.subtype == 'armor') {
if (BoLUtility.getRollArmor()) {
if (!protect.system.properties.soak.formula || protect.system.properties.soak.formula == "") {
ui.notifications.warn(`L'armure ${protect.name} n'a pas de formule pour la protection !`)
} else {
formula += "+" + " max(" + protect.system.properties.soak.formula +",0)"
}
} else {
if (protect.system.properties.soak.value == undefined) {
ui.notifications.warn(`L'armure ${protect.name} n'a pas de valeur fixe pour la protection !`)
} else {
formula += "+ " + protect.system.properties.soak.value
}
}
}
}
console.log("Protect Formula", formula)
return (formula == "") ? "0" : formula;
}
/* -------------------------------------------- */
rollProtection(itemId) {
let armor = duplicate(this.items.get(itemId))
if (armor) {
let armorFormula = "max("+armor.system.properties.soak.formula + ", 0)"
let rollArmor = new Roll(armorFormula)
rollArmor.roll({ async: false }).toMessage()
}
}
/* -------------------------------------------- */
rollWeaponDamage(itemId) {
let weapon = duplicate(this.items.get(itemId))
if (weapon) {
let r = new BoLDefaultRoll({ id: randomID(16), isSuccess: true, mode: "weapon", weapon: weapon, actorId: this.id, actor: this })
r.setSuccess(true)
r.rollDamage()
}
}
/* -------------------------------------------- */
toggleEquipItem(item) {
const equipable = item.system.properties.equipable;
if (equipable) {
let itemData = duplicate(item);
itemData.system.worn = !itemData.system.worn;
return item.update(itemData);
}
}
}