This repository has been archived on 2024-05-29. You can view files and clone it, but cannot push or open issues or pull requests.
fvtt-warhero/modules/warhero-actor.js

870 lines
31 KiB
JavaScript
Raw Normal View History

2023-01-04 21:24:56 +01:00
/* -------------------------------------------- */
2023-01-04 22:09:09 +01:00
import { WarheroUtility } from "./warhero-utility.js";
import { WarheroRollDialog } from "./warhero-roll-dialog.js";
2023-01-04 21:24:56 +01:00
/* -------------------------------------------- */
const coverBonusTable = { "nocover": 0, "lightcover": 2, "heavycover": 4, "entrenchedcover": 6 };
const statThreatLevel = ["agi", "str", "phy", "com", "def", "per"]
const __subkey2title = {
"melee-dmg": "Melee Damage", "melee-atk": "Melee Attack", "ranged-atk": "Ranged Attack",
"ranged-dmg": "Ranged Damage", "dmg-res": "Damare Resistance"
}
/* -------------------------------------------- */
/* -------------------------------------------- */
/**
* Extend the base Actor entity by defining a custom roll data structure which is ideal for the Simple system.
* @extends {Actor}
*/
2023-01-04 22:09:09 +01:00
export class WarheroActor extends Actor {
2023-01-04 21:24:56 +01:00
/* -------------------------------------------- */
/**
* Override the create() function to provide additional SoS functionality.
*
* This overrided create() function adds initial items
* Namely: Basic skills, money,
*
* @param {Object} data Barebones actor data which this function adds onto.
* @param {Object} options (Unused) Additional options which customize the creation workflow.
*
*/
static async create(data, options) {
// Case of compendium global import
if (data instanceof Array) {
return super.create(data, options);
}
// If the created actor has items (only applicable to duplicated actors) bypass the new actor creation logic
if (data.items) {
let actor = super.create(data, options);
return actor;
}
if (data.type == 'character') {
2023-01-04 22:09:09 +01:00
const skills = await WarheroUtility.loadCompendium("fvtt-warhero.skills");
2023-01-04 21:24:56 +01:00
data.items = skills.map(i => i.toObject())
}
if (data.type == 'npc') {
}
return super.create(data, options);
}
/* -------------------------------------------- */
prepareBaseData() {
}
/* -------------------------------------------- */
async prepareData() {
super.prepareData();
}
/* -------------------------------------------- */
computeHitPoints() {
if (this.type == "character") {
}
}
/* -------------------------------------------- */
prepareDerivedData() {
if (this.type == 'character' || game.user.isGM) {
this.computeHitPoints()
}
super.prepareDerivedData();
}
/* -------------------------------------------- */
_preUpdate(changed, options, user) {
super._preUpdate(changed, options, user);
}
/* -------------------------------------------- */
getEncumbranceCapacity() {
return 1;
}
/* -------------------------------------------- */
getMoneys() {
let comp = this.items.filter(item => item.type == 'money');
2023-01-04 22:09:09 +01:00
WarheroUtility.sortArrayObjectsByName(comp)
2023-01-04 21:24:56 +01:00
return comp;
}
/* -------------------------------------------- */
getFeats() {
let comp = duplicate(this.items.filter(item => item.type == 'feat') || []);
2023-01-04 22:09:09 +01:00
WarheroUtility.sortArrayObjectsByName(comp)
2023-01-04 21:24:56 +01:00
return comp;
}
/* -------------------------------------------- */
getFeatsWithDie() {
let comp = duplicate(this.items.filter(item => item.type == 'feat' && item.system.isfeatdie) || []);
2023-01-04 22:09:09 +01:00
WarheroUtility.sortArrayObjectsByName(comp)
2023-01-04 21:24:56 +01:00
return comp;
}
getFeatsWithSL() {
let comp = duplicate(this.items.filter(item => item.type == 'feat' && item.system.issl) || []);
2023-01-04 22:09:09 +01:00
WarheroUtility.sortArrayObjectsByName(comp)
2023-01-04 21:24:56 +01:00
return comp;
}
/* -------------------------------------------- */
getLore() {
let comp = duplicate(this.items.filter(item => item.type == 'spell') || []);
2023-01-04 22:09:09 +01:00
WarheroUtility.sortArrayObjectsByName(comp)
2023-01-04 21:24:56 +01:00
return comp;
}
getEquippedWeapons() {
let comp = duplicate(this.items.filter(item => item.type == 'weapon' && item.system.equipped) || []);
2023-01-04 22:09:09 +01:00
WarheroUtility.sortArrayObjectsByName(comp)
2023-01-04 21:24:56 +01:00
return comp;
}
/* -------------------------------------------- */
2023-03-20 21:01:00 +01:00
getEquippedArmors() {
let comp = duplicate(this.items.filter(item => item.type == 'armor' && item.system.slotlocation == 'armor') || []);
WarheroUtility.sortArrayObjectsByName(comp)
return comp;
}
2023-01-04 21:24:56 +01:00
getArmors() {
let comp = duplicate(this.items.filter(item => item.type == 'armor') || []);
2023-01-04 22:09:09 +01:00
WarheroUtility.sortArrayObjectsByName(comp)
2023-01-04 21:24:56 +01:00
return comp;
}
2023-01-29 16:56:09 +01:00
getPowers() {
let comp = duplicate(this.items.filter(item => item.type == 'power') || []);
WarheroUtility.sortArrayObjectsByName(comp)
return comp;
2023-01-04 21:24:56 +01:00
}
2023-02-08 21:19:53 +01:00
sortPowers() {
let schools = {}
2023-02-18 23:19:10 +01:00
for (let power of this.items) {
2023-02-08 21:19:53 +01:00
if (power.type == "power") {
power = duplicate(power)
let school = schools[power.system.magicschool] || []
school.push(power)
WarheroUtility.sortArrayObjectsByNameAndLevel(school)
schools[power.system.magicschool] = school
}
}
return schools
}
2023-03-26 08:57:44 +02:00
getAllItems() {
let comp = duplicate(this.items || []);
WarheroUtility.sortArrayObjectsByName(comp)
return comp;
}
2023-01-04 21:24:56 +01:00
/* -------------------------------------------- */
2023-03-20 21:01:00 +01:00
getEquippedShields() {
let comp = duplicate(this.items.filter(item => item.type == 'shield' && item.system.slotlocation == "shield") || []);
WarheroUtility.sortArrayObjectsByName(comp)
return comp;
}
2023-01-04 21:24:56 +01:00
getShields() {
let comp = duplicate(this.items.filter(item => item.type == 'shield') || []);
2023-01-04 22:09:09 +01:00
WarheroUtility.sortArrayObjectsByName(comp)
2023-01-04 21:24:56 +01:00
return comp;
}
/* -------------------------------------------- */
getRace() {
let race = this.items.filter(item => item.type == 'race')
return race[0] ?? [];
}
/* -------------------------------------------- */
2023-03-24 13:29:19 +01:00
getMainClass() {
let classWH = this.items.find(item => item.type == 'class' && !item.system.issecondary)
return classWH
}
getSecondaryClass() {
let classWH = this.items.find(item => item.type == 'class' && item.system.issecondary)
return classWH
2023-01-29 16:56:09 +01:00
}
2023-03-09 13:53:19 +01:00
getClasses() {
let comp = duplicate(this.items.filter(item => item.type == "class") || []);
WarheroUtility.sortArrayObjectsByName(comp)
return comp;
}
2023-01-29 16:56:09 +01:00
/* -------------------------------------------- */
2023-01-04 21:24:56 +01:00
checkAndPrepareEquipment(item) {
}
/* -------------------------------------------- */
checkAndPrepareEquipments(listItem) {
for (let item of listItem) {
this.checkAndPrepareEquipment(item)
}
return listItem
}
2023-01-29 16:56:09 +01:00
/* -------------------------------------------- */
2023-02-21 17:53:00 +01:00
computeTotalMoney() {
let nbMoney = 0
2023-03-10 18:32:13 +01:00
this.items.forEach(it => { if (it.type == 'money') { nbMoney += it.system.quantity } })
2023-02-21 17:53:00 +01:00
return nbMoney
2023-03-10 18:32:13 +01:00
}
2023-02-21 17:53:00 +01:00
/* -------------------------------------------- */
2023-03-10 18:32:13 +01:00
buildPartySlots() {
2023-01-29 16:56:09 +01:00
let containers = {}
2023-03-10 18:32:13 +01:00
for (let slotName in game.system.warhero.config.partySlotNames) {
let slotDef = game.system.warhero.config.partySlotNames[slotName]
2023-01-29 16:56:09 +01:00
containers[slotName] = duplicate(slotDef)
2023-03-10 18:32:13 +01:00
containers[slotName].content = this.items.filter(it => (it.type == 'money' || it.type == 'weapon' || it.type == 'armor' || it.type == 'shield' || it.type == 'equipment') )
2023-01-29 16:56:09 +01:00
let slotUsed = 0
2023-03-10 18:32:13 +01:00
for (let item of containers[slotName].content) {
2023-02-18 23:19:10 +01:00
let q = (item.system.quantity) ? item.system.quantity : 1
2023-03-10 18:32:13 +01:00
containers[slotName].nbslots += (item.system.providedslot ?? 0) * q
if (item.type == "money") {
2023-02-20 18:38:00 +01:00
slotUsed += Math.ceil(item.system.quantity / 1000)
} else {
2023-03-10 18:32:13 +01:00
slotUsed += item.system.slotused * q
2023-02-20 18:38:00 +01:00
}
2023-01-29 16:56:09 +01:00
}
2023-02-08 19:38:09 +01:00
slotUsed = Math.ceil(slotUsed)
2023-01-29 16:56:09 +01:00
containers[slotName].slotUsed = slotUsed
}
return containers
}
2023-03-10 18:32:13 +01:00
/* -------------------------------------------- */
buildBodySlot() {
let containers = {}
for (let slotName in game.system.warhero.config.slotNames) {
let slotDef = game.system.warhero.config.slotNames[slotName]
if (!slotDef.container) {
containers[slotName] = duplicate(slotDef)
containers[slotName].content = this.items.filter(it => (it.type == 'money' || it.type == 'weapon' || it.type == 'armor' || it.type == 'shield' || it.type == 'equipment')
&& it.system.slotlocation == slotName)
let slotUsed = 0
for (let item of containers[slotName].content) {
let q = (item.system.quantity) ? item.system.quantity : 1
containers[slotName].nbslots += (item.system.providedslot ?? 0) * q
if (item.type == "money") {
slotUsed += Math.ceil(item.system.quantity / 1000)
} else {
slotUsed += item.system.slotused * q
}
}
slotUsed = Math.ceil(slotUsed)
containers[slotName].slotUsed = slotUsed
}
}
return containers
}
/* -------------------------------------------- */
buildEquipmentsSlot() {
let containers = {}
for (let slotName in game.system.warhero.config.slotNames) {
let slotDef = game.system.warhero.config.slotNames[slotName]
if (slotDef.container) {
containers[slotName] = duplicate(slotDef)
containers[slotName].content = this.items.filter(it => (it.type == 'money' || it.type == 'weapon' || it.type == 'armor' || it.type == 'shield' || it.type == 'equipment')
&& it.system.slotlocation == slotName)
let slotUsed = 0
for (let item of containers[slotName].content) {
let q = (item.system.quantity) ? item.system.quantity : 1
containers[slotName].nbslots += (item.system.providedslot ?? 0) * q
if (item.type == "money") {
slotUsed += Math.ceil(item.system.quantity / 1000)
} else {
slotUsed += item.system.slotused * q
}
}
slotUsed = Math.ceil(slotUsed)
containers[slotName].slotUsed = slotUsed
}
}
return containers
}
2023-01-04 21:24:56 +01:00
/* -------------------------------------------- */
getConditions() {
let comp = duplicate(this.items.filter(item => item.type == 'condition') || []);
2023-01-04 22:09:09 +01:00
WarheroUtility.sortArrayObjectsByName(comp)
2023-01-04 21:24:56 +01:00
return comp;
}
/* -------------------------------------------- */
2023-01-29 18:32:30 +01:00
prepareWeapon(weapon) {
let formula = weapon.system.damage
2023-02-08 21:19:53 +01:00
if (weapon.system.weapontype == "long" || weapon.system.weapontype == "short") {
2023-01-29 18:32:30 +01:00
formula += "+" + this.system.statistics.str.value
}
if (weapon.system.weapontype == "twohanded") {
2023-02-18 23:19:10 +01:00
formula += "+" + Math.floor(this.system.statistics.str.value * 1.5)
2023-01-29 18:32:30 +01:00
}
2023-02-08 21:19:53 +01:00
if (weapon.system.weapontype == "polearm") {
2023-02-18 23:19:10 +01:00
formula += "+" + Math.floor(this.system.statistics.str.value * 1)
weapon.damageFormula2Hands = weapon.system.damage2hands + "+" + Math.floor(this.system.statistics.str.value * 1.5)
2023-02-08 21:19:53 +01:00
}
2023-03-24 13:29:19 +01:00
if (weapon.system.weapontype == "throwing" || weapon.system.weapontype == "shooting") {
formula += "+"+this.system.secondary.rangeddamagebonus.value
} else if ( weapon.system.weapontype != "special" ) {
formula += "+"+this.system.secondary.meleedamagebonus.value
if (weapon.damageFormula2Hands) {
weapon.damageFormula2Hands += "+"+this.system.secondary.meleedamagebonus.value
}
}
2023-01-29 18:32:30 +01:00
weapon.damageFormula = formula
}
/* -------------------------------------------- */
2023-03-20 21:01:00 +01:00
getEquippedWeapons() {
let comp = duplicate(this.items.filter(item => item.type == 'weapon' && (item.system.slotlocation == "weapon1" || item.system.slotlocation == "weapon2") ) || []);
for (let weapon of comp) {
this.prepareWeapon(weapon)
}
WarheroUtility.sortArrayObjectsByName(comp)
return comp;
}
2023-01-04 21:24:56 +01:00
getWeapons() {
let comp = duplicate(this.items.filter(item => item.type == 'weapon') || []);
2023-01-29 18:32:30 +01:00
for (let weapon of comp) {
this.prepareWeapon(weapon)
}
2023-01-04 22:09:09 +01:00
WarheroUtility.sortArrayObjectsByName(comp)
2023-01-04 21:24:56 +01:00
return comp;
}
/* -------------------------------------------- */
2023-02-07 15:18:00 +01:00
getConditions() {
let comp = duplicate(this.items.filter(item => item.type == 'condition') || []);
WarheroUtility.sortArrayObjectsByName(comp)
return comp;
}
/* -------------------------------------------- */
2023-01-04 21:24:56 +01:00
getItemById(id) {
let item = this.items.find(item => item.id == id);
if (item) {
item = duplicate(item)
}
return item;
}
/* -------------------------------------------- */
2023-02-06 07:37:39 +01:00
getLanguages() {
let comp = this.items.filter(it => it.type == "language")
WarheroUtility.sortArrayObjectsByName(comp)
return comp
}
/* -------------------------------------------- */
getNormalSkills() {
2023-03-09 13:53:19 +01:00
let comp = this.items.filter(it => it.type == "skill" && !it.system.classskill && !it.system.raceskill)
2023-02-06 07:37:39 +01:00
WarheroUtility.sortArrayObjectsByName(comp)
return comp
}
2023-02-27 22:04:03 +01:00
getRaceSkills() {
let comp = this.items.filter(it => it.type == "skill" && it.system.raceskill)
WarheroUtility.sortArrayObjectsByName(comp)
return comp
}
2023-02-06 07:37:39 +01:00
getClassSkills() {
let comp = this.items.filter(it => it.type == "skill" && it.system.classskill)
WarheroUtility.sortArrayObjectsByName(comp)
return comp
}
2023-01-04 21:24:56 +01:00
getSkills() {
let comp = duplicate(this.items.filter(item => item.type == 'skill') || [])
2023-01-04 22:09:09 +01:00
WarheroUtility.sortArrayObjectsByName(comp)
2023-01-04 21:24:56 +01:00
return comp
}
/* -------------------------------------------- */
getRelevantAbility(statKey) {
let comp = duplicate(this.items.filter(item => item.type == 'skill' && item.system.ability == ability) || []);
return comp;
}
/* -------------------------------------------- */
async equipItem(itemId) {
let item = this.items.find(item => item.id == itemId)
if (item && item.system) {
if (item.type == "armor") {
let armor = this.items.find(item => item.id != itemId && item.type == "armor" && item.system.equipped)
if (armor) {
ui.notifications.warn("You already have an armor equipped!")
return
}
}
if (item.type == "shield") {
let shield = this.items.find(item => item.id != itemId && item.type == "shield" && item.system.equipped)
if (shield) {
ui.notifications.warn("You already have a shield equipped!")
return
}
}
let update = { _id: item.id, "system.equipped": !item.system.equipped };
await this.updateEmbeddedDocuments('Item', [update]); // Updates one EmbeddedEntity
}
}
/* -------------------------------------------- */
compareName(a, b) {
if (a.name < b.name) {
return -1;
}
if (a.name > b.name) {
return 1;
}
return 0;
}
/* ------------------------------------------- */
getEquipments() {
2023-03-10 18:32:13 +01:00
return this.items.filter(item => item.type == 'shield' || item.type == 'armor' || item.type == "weapon" || item.type == "equipment" || item.type == "potion" || item.type == "poison" || item.type == "trap" || item.type == "classitem");
2023-02-27 22:04:03 +01:00
}
getCompetencyItems() {
return duplicate(this.items.filter(item => item.type == "competency") || [])
2023-01-04 21:24:56 +01:00
}
/* ------------------------------------------- */
getEquipmentsOnly() {
return duplicate(this.items.filter(item => item.type == "equipment") || [])
}
/* ------------------------------------------- */
getSaveRoll() {
return {
reflex: {
"label": "Reflex Save",
2023-01-04 22:09:09 +01:00
"img": "systems/fvtt-warhero/images/icons/saves/reflex_save.webp",
2023-01-04 21:24:56 +01:00
"value": this.system.abilities.agi.value + this.system.abilities.wit.value
},
fortitude: {
"label": "Fortitude Save",
2023-01-04 22:09:09 +01:00
"img": "systems/fvtt-warhero/images/icons/saves/fortitude_save.webp",
2023-01-04 21:24:56 +01:00
"value": this.system.abilities.str.value + this.system.abilities.con.value
},
willpower: {
"label": "Willpower Save",
2023-01-04 22:09:09 +01:00
"img": "systems/fvtt-warhero/images/icons/saves/will_save.webp",
2023-01-04 21:24:56 +01:00
"value": this.system.abilities.int.value + this.system.abilities.cha.value
}
}
}
/* ------------------------------------------- */
async buildContainerTree() {
let equipments = duplicate(this.items.filter(item => item.type == "equipment") || [])
for (let equip1 of equipments) {
if (equip1.system.iscontainer) {
equip1.system.contents = []
equip1.system.contentsEnc = 0
for (let equip2 of equipments) {
if (equip1._id != equip2.id && equip2.system.containerid == equip1.id) {
equip1.system.contents.push(equip2)
let q = equip2.system.quantity ?? 1
equip1.system.contentsEnc += q * equip2.system.weight
}
}
}
}
// Compute whole enc
let enc = 0
for (let item of equipments) {
2023-01-04 22:09:09 +01:00
//item.data.idrDice = WarheroUtility.getDiceFromLevel(Number(item.data.idr))
2023-01-04 21:24:56 +01:00
if (item.system.equipped) {
if (item.system.iscontainer) {
enc += item.system.contentsEnc
} else if (item.system.containerid == "") {
let q = item.system.quantity ?? 1
enc += q * item.system.weight
}
}
}
for (let item of this.items) { // Process items/shields/armors
if ((item.type == "weapon" || item.type == "shield" || item.type == "armor") && item.system.equipped) {
let q = item.system.quantity ?? 1
enc += q * item.system.weight
}
}
// Store local values
this.encCurrent = enc
this.containersTree = equipments.filter(item => item.system.containerid == "") // Returns the root of equipements without container
}
/* -------------------------------------------- */
async rollArmor(rollData) {
let armor = this.getEquippedArmor()
if (armor) {
}
return { armor: "none" }
}
/* -------------------------------------------- */
async incDecHP(formula) {
2023-02-18 23:19:10 +01:00
let dmgRoll = new Roll(formula + "[warhero-orange]").roll({ async: false })
2023-01-04 22:09:09 +01:00
await WarheroUtility.showDiceSoNice(dmgRoll, game.settings.get("core", "rollMode"))
2023-01-04 21:24:56 +01:00
let hp = duplicate(this.system.secondary.hp)
hp.value = Number(hp.value) + Number(dmgRoll.total)
this.update({ 'system.secondary.hp': hp })
return Number(dmgRoll.total)
}
2023-02-07 15:18:00 +01:00
/* -------------------------------------------- */
2023-02-14 15:45:59 +01:00
updateCompetency(competency, obj, labelTab) {
2023-02-18 23:19:10 +01:00
for (let key in obj) {
2023-02-07 15:18:00 +01:00
if (obj[key]) {
2023-02-14 15:45:59 +01:00
//console.log("Parsing", key) //game.system.warhero.config.weaponTypes[key].label)
competency[key] = { enabled: true, label: labelTab[key].label }
2023-02-07 15:18:00 +01:00
}
}
}
getCompetency() {
let myRace = this.getRace()
2023-03-24 13:29:19 +01:00
let myClass1 = this.getMainClass()
let myClass2 = this.getSecondaryClass()
2023-02-18 23:19:10 +01:00
let competency = { weapons: {}, armors: {}, shields: {} }
if (myRace.system) {
2023-02-14 15:45:59 +01:00
this.updateCompetency(competency.weapons, myRace.system.weapons, game.system.warhero.config.weaponTypes)
this.updateCompetency(competency.armors, myRace.system.armors, game.system.warhero.config.armorTypes)
this.updateCompetency(competency.shields, myRace.system.shields, game.system.warhero.config.shieldTypes)
2023-02-18 23:19:10 +01:00
}
2023-03-24 13:29:19 +01:00
if (myClass1 && myClass1.system) {
this.updateCompetency(competency.weapons, myClass1.system.weapons, game.system.warhero.config.weaponTypes)
this.updateCompetency(competency.armors, myClass1.system.armors, game.system.warhero.config.armorTypes)
this.updateCompetency(competency.shields, myClass1.system.shields, game.system.warhero.config.shieldTypes)
}
if (myClass2 && myClass2.system) {
this.updateCompetency(competency.weapons, myClass2.system.weapons, game.system.warhero.config.weaponTypes)
this.updateCompetency(competency.armors, myClass2.system.armors, game.system.warhero.config.armorTypes)
this.updateCompetency(competency.shields, myClass2.system.shields, game.system.warhero.config.shieldTypes)
2023-02-07 15:18:00 +01:00
}
return competency
}
2023-01-04 21:24:56 +01:00
/* -------------------------------------------- */
getAbility(abilKey) {
return this.system.abilities[abilKey];
}
/* -------------------------------------------- */
async addObjectToContainer(itemId, containerId) {
let container = this.items.find(item => item.id == containerId && item.system.iscontainer)
let object = this.items.find(item => item.id == itemId)
if (container) {
if (object.system.iscontainer) {
ui.notifications.warn("Only 1 level of container allowed")
return
}
let alreadyInside = this.items.filter(item => item.system.containerid && item.system.containerid == containerId);
if (alreadyInside.length >= container.system.containercapacity) {
ui.notifications.warn("Container is already full !")
return
} else {
await this.updateEmbeddedDocuments("Item", [{ _id: object.id, 'system.containerid': containerId }])
}
} else if (object && object.system.containerid) { // remove from container
console.log("Removeing: ", object)
await this.updateEmbeddedDocuments("Item", [{ _id: object.id, 'system.containerid': "" }]);
}
}
/* -------------------------------------------- */
async preprocessItem(event, item, onDrop = false) {
let dropID = $(event.target).parents(".item").attr("data-item-id") // Only relevant if container drop
let objectID = item.id || item._id
this.addObjectToContainer(objectID, dropID)
return true
}
/* -------------------------------------------- */
async equipGear(equipmentId) {
let item = this.items.find(item => item.id == equipmentId);
if (item && item.system) {
let update = { _id: item.id, "system.equipped": !item.system.equipped };
await this.updateEmbeddedDocuments('Item', [update]); // Updates one EmbeddedEntity
}
}
/* -------------------------------------------- */
2023-02-20 10:16:17 +01:00
async getInitiativeScore(combatId, combatantId) {
2023-03-10 18:32:13 +01:00
let roll = new Roll("1d20+" + this.system.attributes.ini.value).roll({ async: false })
2023-02-20 10:16:17 +01:00
await WarheroUtility.showDiceSoNice(roll, game.settings.get("core", "rollMode"))
return roll.total
2023-01-04 21:24:56 +01:00
}
/* -------------------------------------------- */
getSubActors() {
let subActors = [];
for (let id of this.system.subactors) {
subActors.push(duplicate(game.actors.get(id)))
}
return subActors;
}
/* -------------------------------------------- */
async addSubActor(subActorId) {
let subActors = duplicate(this.system.subactors);
subActors.push(subActorId);
await this.update({ 'system.subactors': subActors });
}
/* -------------------------------------------- */
async delSubActor(subActorId) {
let newArray = [];
for (let id of this.system.subactors) {
if (id != subActorId) {
newArray.push(id);
}
}
await this.update({ 'system.subactors': newArray });
}
/* -------------------------------------------- */
syncRoll(rollData) {
this.lastRollId = rollData.rollId;
2023-01-04 22:09:09 +01:00
WarheroUtility.saveRollData(rollData);
2023-01-04 21:24:56 +01:00
}
/* -------------------------------------------- */
getOneSkill(skillId) {
let skill = this.items.find(item => item.type == 'skill' && item.id == skillId)
if (skill) {
skill = duplicate(skill);
}
return skill;
}
/* -------------------------------------------- */
async deleteAllItemsByType(itemType) {
let items = this.items.filter(item => item.type == itemType);
await this.deleteEmbeddedDocuments('Item', items);
}
/* -------------------------------------------- */
async addItemWithoutDuplicate(newItem) {
let item = this.items.find(item => item.type == newItem.type && item.name.toLowerCase() == newItem.name.toLowerCase())
if (!item) {
await this.createEmbeddedDocuments('Item', [newItem]);
}
}
/* -------------------------------------------- */
async incrementSkillExp(skillId, inc) {
let skill = this.items.get(skillId)
if (skill) {
await this.updateEmbeddedDocuments('Item', [{ _id: skill.id, 'system.exp': skill.system.exp + inc }])
let chatData = {
user: game.user.id,
rollMode: game.settings.get("core", "rollMode"),
whisper: [game.user.id].concat(ChatMessage.getWhisperRecipients('GM')),
content: `<div>${this.name} has gained 1 exp in the skill ${skill.name} (exp = ${skill.system.exp})</div`
}
ChatMessage.create(chatData)
if (skill.system.exp >= 25) {
await this.updateEmbeddedDocuments('Item', [{ _id: skill.id, 'system.exp': 0, 'system.explevel': skill.system.explevel + 1 }])
let chatData = {
user: game.user.id,
rollMode: game.settings.get("core", "rollMode"),
whisper: [game.user.id].concat(ChatMessage.getWhisperRecipients('GM')),
content: `<div>${this.name} has gained 1 exp SL in the skill ${skill.name} (new exp SL : ${skill.system.explevel}) !</div`
}
ChatMessage.create(chatData)
}
}
}
2023-02-20 10:16:17 +01:00
/* -------------------------------------------- */
async incDecSkillUse(skillId, value) {
let skill = this.items.get(skillId)
if (skill) {
let newUse = skill.system.currentuse + value
if (newUse > skill.system.maxuse) {
ui.notifications.warn(game.i18n.localize("WH.notif.skillmaxuse"))
return
}
newUse = Math.max(newUse, 0)
2023-03-10 18:32:13 +01:00
this.updateEmbeddedDocuments('Item', [{ _id: skill.id, 'system.currentuse': newUse }])
2023-02-20 10:16:17 +01:00
}
}
2023-01-04 21:24:56 +01:00
/* -------------------------------------------- */
async incDecQuantity(objetId, incDec = 0) {
let objetQ = this.items.get(objetId)
if (objetQ) {
let newQ = objetQ.system.quantity + incDec
if (newQ >= 0) {
const updated = await this.updateEmbeddedDocuments('Item', [{ _id: objetQ.id, 'system.quantity': newQ }]) // pdates one EmbeddedEntity
}
}
}
/* -------------------------------------------- */
async incDecAmmo(objetId, incDec = 0) {
let objetQ = this.items.get(objetId)
if (objetQ) {
let newQ = objetQ.system.ammocurrent + incDec;
if (newQ >= 0 && newQ <= objetQ.system.ammomax) {
const updated = await this.updateEmbeddedDocuments('Item', [{ _id: objetQ.id, 'system.ammocurrent': newQ }]); // pdates one EmbeddedEntity
}
}
}
/* -------------------------------------------- */
isForcedAdvantage() {
return this.items.find(cond => cond.type == "condition" && cond.system.advantage)
}
isForcedDisadvantage() {
return this.items.find(cond => cond.type == "condition" && cond.system.disadvantage)
}
isForcedRollAdvantage() {
return this.items.find(cond => cond.type == "condition" && cond.system.rolladvantage)
}
isForcedRollDisadvantage() {
return this.items.find(cond => cond.type == "condition" && cond.system.rolldisadvantage)
}
isNoAdvantage() {
return this.items.find(cond => cond.type == "condition" && cond.system.noadvantage)
}
isNoAction() {
return this.items.find(cond => cond.type == "condition" && cond.system.noaction)
}
isAttackDisadvantage() {
return this.items.find(cond => cond.type == "condition" && cond.system.attackdisadvantage)
}
isDefenseDisadvantage() {
return this.items.find(cond => cond.type == "condition" && cond.system.defensedisadvantage)
}
isAttackerAdvantage() {
return this.items.find(cond => cond.type == "condition" && cond.system.targetadvantage)
}
2023-02-06 07:37:39 +01:00
/* -------------------------------------------- */
setLevel() {
let xp = this.system.secondary.xp.value
2023-02-18 23:19:10 +01:00
this.system.secondary.xp.level = 1 + Math.floor(xp / 10)
2023-02-06 07:37:39 +01:00
}
/* -------------------------------------------- */
computeDRTotal() {
2023-03-22 13:38:49 +01:00
let armors = this.items.filter(it => it.type == "armor" && it.system.slotlocation == 'armor')
2023-02-06 07:37:39 +01:00
let dr = 0
for (let armor of armors) {
dr += armor.system.damagereduction
}
2023-02-18 23:19:10 +01:00
this.system.secondary.drbonustotal.value = this.system.secondary.drbonus.value + dr
2023-02-06 07:37:39 +01:00
}
/* -------------------------------------------- */
computeParryBonusTotal() {
2023-03-22 13:38:49 +01:00
let shields = this.items.filter(it => it.type == "shield" && it.system.slotlocation == 'shield')
2023-02-06 07:37:39 +01:00
let parry = 0
for (let shield of shields) {
parry += shield.system.parrybonus
}
2023-02-18 23:19:10 +01:00
this.system.secondary.parrybonustotal.value = this.system.secondary.parrybonus.value + parry
2023-02-06 07:37:39 +01:00
}
/* -------------------------------------------- */
computeBonusLanguages() {
this.system.secondary.nblanguage.value = Math.floor(this.system.statistics.min.value / 2)
}
2023-01-29 18:32:30 +01:00
/* -------------------------------------------- */
2023-02-20 10:16:17 +01:00
spentMana(spentValue) {
let mana = duplicate(this.system.attributes.mana)
if (Number(spentValue) > mana.value) {
2023-01-29 18:32:30 +01:00
ui.notifications.warn("Not enough Mana points !")
return false
}
2023-02-20 10:16:17 +01:00
mana.value -= Number(spentValue)
this.update({ 'system.attributes.mana': mana })
2023-01-29 18:32:30 +01:00
return true
2023-02-18 23:19:10 +01:00
}
2023-03-10 18:32:13 +01:00
2023-02-27 22:04:03 +01:00
/* -------------------------------------------- */
incrementUse(rollData) {
let stat = duplicate(this.system[rollData.mode][rollData.statKey])
stat.nbuse++
2023-03-10 18:32:13 +01:00
this.update({ [`system.${rollData.mode}.${rollData.statKey}`]: stat })
2023-02-27 22:04:03 +01:00
}
2023-01-04 21:24:56 +01:00
/* -------------------------------------------- */
2023-01-29 16:56:09 +01:00
getCommonRollData() {
2023-01-04 22:09:09 +01:00
let rollData = WarheroUtility.getBasicRollData()
2023-01-04 21:24:56 +01:00
rollData.alias = this.name
rollData.actorImg = this.img
rollData.actorId = this.id
rollData.img = this.img
console.log("ROLLDATA", rollData)
return rollData
}
/* -------------------------------------------- */
2023-01-29 16:56:09 +01:00
rollFromType(rollType, rollKey) {
let stat = duplicate(this.system[rollType][rollKey])
let rollData = this.getCommonRollData()
rollData.mode = rollType
2023-02-27 22:04:03 +01:00
rollData.statKey = rollKey
2023-01-29 16:56:09 +01:00
rollData.stat = stat
2023-03-10 18:32:13 +01:00
if (stat && stat.stat) {
2023-02-20 10:16:17 +01:00
rollData.statBonus = duplicate(this.system.statistics[stat.stat])
}
2023-03-10 18:32:13 +01:00
if (stat.hasuse && stat.nbuse >= stat.maxuse) {
2023-02-27 22:04:03 +01:00
ui.notifications.warn(game.i18n.localize("WH.notif.toomanyuses"))
return
}
2023-02-18 23:19:10 +01:00
if (rollKey == "parrybonustotal") {
2023-02-06 07:37:39 +01:00
WarheroUtility.rollParry(rollData)
return
}
2023-01-29 16:56:09 +01:00
this.startRoll(rollData)
2023-01-04 21:24:56 +01:00
}
2023-02-06 07:37:39 +01:00
/* -------------------------------------------- */
rollSaveFromType(rollType, rollKey) {
let stat = duplicate(this.system[rollType][rollKey])
let rollData = this.getCommonRollData()
rollData.mode = "save"
rollData.stat = stat
this.startRoll(rollData)
}
2023-02-18 23:19:10 +01:00
2023-01-04 21:24:56 +01:00
/* -------------------------------------------- */
rollWeapon(weaponId) {
let weapon = this.items.get(weaponId)
if (weapon) {
weapon = duplicate(weapon)
2023-01-29 18:32:30 +01:00
let rollData = this.getCommonRollData()
rollData.mode = "weapon"
2023-02-18 23:19:10 +01:00
if (weapon.system.weapontype === "shooting" || weapon.system.weapontype === "throwing") {
2023-02-06 07:37:39 +01:00
rollData.stat = duplicate(this.system.attributes.txcr)
} else {
rollData.stat = duplicate(this.system.attributes.txcm)
}
2023-02-20 10:16:17 +01:00
rollData.usemWeaponMalus = false
rollData.mWeaponMalus = this.system.secondary.malusmultiweapon.value
2023-01-29 18:32:30 +01:00
rollData.weapon = weapon
rollData.img = weapon.img
this.startRoll(rollData)
2023-01-04 21:24:56 +01:00
}
}
2023-01-29 18:32:30 +01:00
/* -------------------------------------------- */
2023-02-08 21:19:53 +01:00
rollDamage(weaponId, is2hands = false) {
2023-01-29 18:32:30 +01:00
let weapon = this.items.get(weaponId)
if (weapon) {
weapon = duplicate(weapon)
this.prepareWeapon(weapon)
let rollData = this.getCommonRollData()
rollData.mode = "damage"
rollData.weapon = weapon
2023-02-08 21:19:53 +01:00
rollData.is2hands = is2hands
2023-01-29 18:32:30 +01:00
rollData.img = weapon.img
this.startRoll(rollData)
}
}
/* -------------------------------------------- */
rollPower(powerId) {
let power = this.items.get(powerId)
if (power) {
power = duplicate(power)
let rollData = this.getCommonRollData()
rollData.mode = "power"
rollData.power = power
2023-02-20 10:16:17 +01:00
rollData.powerLevel = Number(power.system.level)
2023-01-29 18:32:30 +01:00
rollData.img = power.img
rollData.hasBM = false
this.startRoll(rollData)
}
}
2023-02-18 23:19:10 +01:00
2023-01-04 21:24:56 +01:00
/* -------------------------------------------- */
async startRoll(rollData) {
this.syncRoll(rollData)
2023-01-04 22:09:09 +01:00
let rollDialog = await WarheroRollDialog.create(this, rollData)
2023-01-04 21:24:56 +01:00
rollDialog.render(true)
}
}