fvtt-pegasus-rpg/modules/pegasus-actor.js

889 lines
30 KiB
JavaScript
Raw Normal View History

2021-12-02 07:38:59 +01:00
/* -------------------------------------------- */
import { PegasusUtility } from "./pegasus-utility.js";
import { PegasusRollDialog } from "./pegasus-roll-dialog.js";
/* -------------------------------------------- */
2022-01-14 14:49:16 +01:00
const coverBonusTable = { "nocover": 0, "lightcover": 2, "heavycover": 4, "entrenchedcover": 6 };
2021-12-02 07:38:59 +01:00
/* -------------------------------------------- */
/* -------------------------------------------- */
/**
* Extend the base Actor entity by defining a custom roll data structure which is ideal for the Simple system.
* @extends {Actor}
*/
export class PegasusActor extends Actor {
/* -------------------------------------------- */
/**
* 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;
}
2022-01-14 14:49:16 +01:00
if (data.type == 'character') {
2021-12-02 07:38:59 +01:00
const skills = await PegasusUtility.loadCompendium("fvtt-weapons-of-the-gods.skills");
data.items = skills.map(i => i.toObject());
}
2022-01-14 14:49:16 +01:00
if (data.type == 'npc') {
2021-12-02 07:38:59 +01:00
}
return super.create(data, options);
}
2022-01-14 14:49:16 +01:00
2021-12-02 07:38:59 +01:00
/* -------------------------------------------- */
prepareBaseData() {
}
/* -------------------------------------------- */
async prepareData() {
super.prepareData();
}
/* -------------------------------------------- */
prepareDerivedData() {
if (this.type == 'character') {
let h = 0;
let updates = [];
2022-01-14 14:49:16 +01:00
2021-12-02 07:38:59 +01:00
for (let key in this.data.data.statistics) {
let attr = this.data.data.statistics[key];
}
/*if ( h != this.data.data.secondary.health.max) {
this.data.data.secondary.health.max = h;
updates.push( {'data.secondary.health.max': h} );
}*/
2022-01-14 14:49:16 +01:00
if (updates.length > 0) {
this.update(updates);
2021-12-02 07:38:59 +01:00
}
2022-01-11 23:35:23 +01:00
this.computeNRGHealth();
2021-12-02 07:38:59 +01:00
}
super.prepareDerivedData();
}
/* -------------------------------------------- */
_preUpdate(changed, options, user) {
super._preUpdate(changed, options, user);
}
2021-12-03 18:31:43 +01:00
/* -------------------------------------------- */
getActivePerks() {
2022-01-14 14:49:16 +01:00
let perks = this.data.items.filter(item => item.type == 'perk' && item.data.data.active);
2021-12-03 18:31:43 +01:00
return perks;
2022-01-14 14:49:16 +01:00
}
2021-12-02 07:38:59 +01:00
/* -------------------------------------------- */
2022-01-06 18:22:05 +01:00
getAbilities() {
2022-01-14 14:49:16 +01:00
let ab = this.data.items.filter(item => item.type == 'ability');
2022-01-06 18:22:05 +01:00
return ab;
}
/* -------------------------------------------- */
2021-12-02 07:38:59 +01:00
getPerks() {
2022-01-14 14:49:16 +01:00
let comp = this.data.items.filter(item => item.type == 'perk');
2021-12-02 07:38:59 +01:00
return comp;
}
/* -------------------------------------------- */
2022-01-12 16:25:55 +01:00
getEffects() {
2022-01-14 14:49:16 +01:00
let comp = this.data.items.filter(item => item.type == 'effect');
2022-01-12 16:25:55 +01:00
return comp;
}
/* -------------------------------------------- */
2021-12-02 07:38:59 +01:00
getPowers() {
2022-01-14 14:49:16 +01:00
let comp = this.data.items.filter(item => item.type == 'power');
2021-12-02 07:38:59 +01:00
return comp;
}
/* -------------------------------------------- */
2022-01-22 21:49:34 +01:00
getMoneys() {
let comp = this.data.items.filter(item => item.type == 'money');
return comp;
}
/* -------------------------------------------- */
2021-12-02 07:38:59 +01:00
getArmors() {
2022-01-14 18:20:15 +01:00
let comp = duplicate(this.data.items.filter(item => item.type == 'armor') || []);
2021-12-02 07:38:59 +01:00
return comp;
}
/* -------------------------------------------- */
getShields() {
2022-01-14 14:49:16 +01:00
let comp = this.data.items.filter(item => item.type == 'shield');
2021-12-02 07:38:59 +01:00
return comp;
2022-01-14 14:49:16 +01:00
}
2022-01-08 18:28:01 +01:00
getRace() {
2022-01-14 14:49:16 +01:00
let race = this.data.items.filter(item => item.type == 'race');
return race[0] ?? [];
2022-01-08 18:28:01 +01:00
}
getRole() {
2022-01-14 14:49:16 +01:00
let role = this.data.items.filter(item => item.type == 'role');
return role[0] ?? [];
2021-12-02 07:38:59 +01:00
}
2022-01-14 18:20:15 +01:00
/* -------------------------------------------- */
checkAndPrepareArmor(armor) {
armor.data.resistanceDice = PegasusUtility.getDiceFromLevel(armor.data.resistance);
}
/* -------------------------------------------- */
checkAndPrepareArmors(armors) {
for (let item of armors) {
this.checkAndPrepareArmor(item);
}
return armors;
}
2022-01-14 14:49:16 +01:00
2021-12-02 07:38:59 +01:00
/* -------------------------------------------- */
2022-01-14 14:49:16 +01:00
checkAndPrepareWeapon(weapon) {
weapon.data.damageDice = PegasusUtility.getDiceFromLevel(weapon.data.damage);
2021-12-02 07:38:59 +01:00
}
/* -------------------------------------------- */
checkAndPrepareWeapons(weapons) {
2022-01-14 14:49:16 +01:00
for (let item of weapons) {
2021-12-02 07:38:59 +01:00
this.checkAndPrepareWeapon(item);
}
return weapons;
}
/* -------------------------------------------- */
getWeapons() {
2022-01-14 14:49:16 +01:00
let comp = duplicate(this.data.items.filter(item => item.type == 'weapon') || []);
2021-12-02 07:38:59 +01:00
return comp;
}
2022-01-11 23:35:23 +01:00
/* -------------------------------------------- */
2022-01-14 14:49:16 +01:00
getItemById(id) {
let item = this.data.items.find(item => item.id == id);
if (item) {
2022-01-11 23:35:23 +01:00
item = duplicate(item)
if (item.type == 'specialisation') {
item.data.dice = PegasusUtility.getDiceFromLevel(item.data.level);
}
}
2022-01-14 14:49:16 +01:00
return item;
2022-01-11 23:35:23 +01:00
}
2021-12-02 07:38:59 +01:00
/* -------------------------------------------- */
getSpecs() {
2022-01-14 14:49:16 +01:00
let comp = duplicate(this.data.items.filter(item => item.type == 'specialisation') || []);
2021-12-02 20:18:21 +01:00
for (let c of comp) {
c.data.dice = PegasusUtility.getDiceFromLevel(c.data.level);
}
2021-12-02 07:38:59 +01:00
return comp;
}
2022-01-11 23:35:23 +01:00
/* -------------------------------------------- */
2022-01-14 14:49:16 +01:00
getRelevantSpec(statKey) {
let comp = duplicate(this.data.items.filter(item => item.type == 'specialisation' && item.data.data.statistic == statKey) || []);
2022-01-11 23:35:23 +01:00
for (let c of comp) {
c.data.dice = PegasusUtility.getDiceFromLevel(c.data.level);
}
return comp;
}
2022-01-14 14:49:16 +01:00
2021-12-03 18:31:43 +01:00
/* -------------------------------------------- */
2022-01-14 14:49:16 +01:00
async activatePerk(perkId) {
let item = this.data.items.find(item => item.id == perkId);
2021-12-03 18:31:43 +01:00
if (item && item.data.data) {
let update = { _id: item.id, "data.active": !item.data.data.active };
await this.updateEmbeddedDocuments('Item', [update]); // Updates one EmbeddedEntity
}
}
2022-01-25 09:14:32 +01:00
2022-01-16 16:12:15 +01:00
/* -------------------------------------------- */
async activatePower(itemId) {
let item = this.data.items.find(item => item.id == itemId);
if (item && item.data.data) {
let update = { _id: item.id, "data.activated": !item.data.data.activated };
await this.updateEmbeddedDocuments('Item', [update]); // Updates one EmbeddedEntity
}
}
2021-12-03 18:31:43 +01:00
2021-12-02 07:38:59 +01:00
/* -------------------------------------------- */
2022-01-14 14:49:16 +01:00
async equipItem(itemId) {
let item = this.data.items.find(item => item.id == itemId);
2021-12-02 07:38:59 +01:00
if (item && item.data.data) {
let update = { _id: item.id, "data.equipped": !item.data.data.equipped };
await this.updateEmbeddedDocuments('Item', [update]); // Updates one EmbeddedEntity
}
}
/* -------------------------------------------- */
2022-01-14 14:49:16 +01:00
compareName(a, b) {
if (a.name < b.name) {
2021-12-02 07:38:59 +01:00
return -1;
}
2022-01-14 14:49:16 +01:00
if (a.name > b.name) {
2021-12-02 07:38:59 +01:00
return 1;
}
return 0;
}
/* ------------------------------------------- */
getEquipments() {
2022-01-14 14:49:16 +01:00
return this.data.items.filter(item => item.type == 'shield' || item.type == 'armor' || item.type == "weapon" || item.type == "equipment");
2021-12-02 07:38:59 +01:00
}
2022-01-14 14:49:16 +01:00
2021-12-02 07:38:59 +01:00
/* -------------------------------------------- */
getActiveEffects(matching = it => true) {
let array = Array.from(this.getEmbeddedCollection("ActiveEffect").values());
return Array.from(this.getEmbeddedCollection("ActiveEffect").values()).filter(it => matching(it));
}
/* -------------------------------------------- */
getEffectByLabel(label) {
return this.getActiveEffects().find(it => it.data.label == label);
}
/* -------------------------------------------- */
getEffectById(id) {
return this.getActiveEffects().find(it => it.id == id);
}
/* -------------------------------------------- */
2022-01-14 14:49:16 +01:00
getAttribute(attrKey) {
2021-12-02 07:38:59 +01:00
return this.data.data.attributes[attrKey];
}
/* -------------------------------------------- */
2022-01-14 14:49:16 +01:00
async equipGear(equipmentId) {
let item = this.data.items.find(item => item.id == equipmentId);
2021-12-02 07:38:59 +01:00
if (item && item.data.data) {
let update = { _id: item.id, "data.equipped": !item.data.data.equipped };
await this.updateEmbeddedDocuments('Item', [update]); // Updates one EmbeddedEntity
}
}
/* -------------------------------------------- */
2022-01-14 14:49:16 +01:00
getInitiativeScore() {
if (this.type == 'character') {
2021-12-02 07:38:59 +01:00
// TODO
2022-01-14 14:49:16 +01:00
}
2021-12-02 07:38:59 +01:00
return 0.0;
}
/* -------------------------------------------- */
2022-01-14 14:49:16 +01:00
getArmorModifier() {
2021-12-02 07:38:59 +01:00
let armors = this.getArmors();
let modifier = 0;
for (let armor of armors) {
if (armor.data.data.equipped) {
if (armor.data.data.type == 'light') modifier += 5;
if (armor.data.data.type == 'medium') modifier += 10;
if (armor.data.data.type == 'heavy') modifier += 15;
}
}
return modifier;
}
/* -------------------------------------------- */
2022-01-14 14:49:16 +01:00
async applyDamageLoss(damage) {
2021-12-02 07:38:59 +01:00
let chatData = {
user: game.user.id,
2022-01-14 14:49:16 +01:00
alias: this.name,
2021-12-02 07:38:59 +01:00
rollMode: game.settings.get("core", "rollMode"),
2022-01-14 14:49:16 +01:00
whisper: [game.user.id].concat(ChatMessage.getWhisperRecipients('GM')),
};
2021-12-02 07:38:59 +01:00
//console.log("Apply damage chat", chatData );
2022-01-14 14:49:16 +01:00
if (damage > 0) {
2021-12-02 07:38:59 +01:00
let health = duplicate(this.data.data.secondary.health);
health.value -= damage;
2022-01-14 14:49:16 +01:00
if (health.value < 0) health.value = 0;
this.update({ "data.secondary.health.value": health.value });
chatData.content = `${this.name} looses ${damage} health. New health value is : ${health.value}`;
2021-12-02 07:38:59 +01:00
} else {
2022-01-14 14:49:16 +01:00
chatData.content = `No health loss for ${this.name} !`;
2021-12-02 07:38:59 +01:00
}
2022-01-14 14:49:16 +01:00
await ChatMessage.create(chatData);
2021-12-02 07:38:59 +01:00
}
/* -------------------------------------------- */
2022-01-14 14:49:16 +01:00
processNoDefense(attackRollData) {
2021-12-02 07:38:59 +01:00
let defenseRollData = {
2022-01-14 14:49:16 +01:00
mode: "nodefense",
2021-12-02 07:38:59 +01:00
finalScore: 0,
2022-01-14 14:49:16 +01:00
defenderName: this.name,
attackerName: attackRollData.alias,
2021-12-02 07:38:59 +01:00
armorModifier: this.getArmorModifier(),
actorId: this.id,
alias: this.name,
result: 0,
}
2022-01-14 14:49:16 +01:00
this.syncRoll(defenseRollData);
this.processDefenseResult(defenseRollData, attackRollData);
2021-12-02 07:38:59 +01:00
}
/* -------------------------------------------- */
async processApplyDamage(defenseRollData, attackRollData) { // Processed by the defender actor
2022-01-14 14:49:16 +01:00
if (attackRollData && attackRollData) {
2021-12-02 07:38:59 +01:00
let result = attackRollData.finalScore;
2022-01-14 14:49:16 +01:00
defenseRollData.damageDices = WotGUtility.getDamageDice(result);
2021-12-02 07:38:59 +01:00
defenseRollData.damageRoll = await this.rollDamage(defenseRollData);
chatData.damages = true;
chatData.damageDices = defenseRollData.damageDices;
2022-01-14 14:49:16 +01:00
WotGUtility.createChatWithRollMode(this.name, {
2021-12-02 07:38:59 +01:00
content: await renderTemplate(`systems/fvtt-weapons-of-the-gods/templates/chat-opposed-damages.html`, chatData)
});
2022-01-14 14:49:16 +01:00
}
2021-12-02 07:38:59 +01:00
}
/* -------------------------------------------- */
async processDefenseResult(defenseRollData, attackRollData) { // Processed by the defenser
2022-01-14 14:49:16 +01:00
if (defenseRollData && attackRollData) {
2021-12-02 07:38:59 +01:00
let result = attackRollData.finalScore - defenseRollData.finalScore;
2022-01-14 14:49:16 +01:00
defenseRollData.defenderName = this.name,
defenseRollData.attackerName = attackRollData.alias
defenseRollData.result = result
2021-12-02 07:38:59 +01:00
defenseRollData.damages = false
defenseRollData.damageDices = 0;
2022-01-14 14:49:16 +01:00
if (result > 0) {
defenseRollData.damageDices = WotGUtility.getDamageDice(result);
2021-12-02 07:38:59 +01:00
defenseRollData.damageRoll = await this.rollDamage(defenseRollData, attackRollData);
defenseRollData.damages = true;
defenseRollData.finalDamage = defenseRollData.damageRoll.total;
2022-01-14 14:49:16 +01:00
WotGUtility.updateRollData(defenseRollData);
2021-12-02 07:38:59 +01:00
console.log("DAMAGE ROLL OBJECT", defenseRollData);
2022-01-14 14:49:16 +01:00
WotGUtility.createChatWithRollMode(this.name, {
2021-12-02 07:38:59 +01:00
content: await renderTemplate(`systems/fvtt-weapons-of-the-gods/templates/chat-opposed-damage.html`, defenseRollData)
});
} else {
2022-01-14 14:49:16 +01:00
WotGUtility.updateRollData(defenseRollData);
WotGUtility.createChatWithRollMode(this.name, {
2021-12-02 07:38:59 +01:00
content: await renderTemplate(`systems/fvtt-weapons-of-the-gods/templates/chat-opposed-fail.html`, defenseRollData)
});
}
}
}
/* -------------------------------------------- */
2022-01-14 14:49:16 +01:00
async rollDamage(defenseRollData, attackRollData) {
2021-12-02 07:38:59 +01:00
let weaponDamage = 0;
if (attackRollData.weapon?.data?.damage) {
weaponDamage = Number(attackRollData.weapon.data.damage);
}
2022-01-14 14:49:16 +01:00
let formula = defenseRollData.damageDices + "d10+" + defenseRollData.armorModifier + "+" + weaponDamage;
2021-12-02 07:38:59 +01:00
console.log("ROLL : ", formula);
2022-01-14 14:49:16 +01:00
let myRoll = new Roll(formula).roll({ async: false });
await WotGUtility.showDiceSoNice(myRoll, game.settings.get("core", "rollMode"));
2021-12-02 07:38:59 +01:00
return myRoll;
}
/* -------------------------------------------- */
getSubActors() {
let subActors = [];
for (let id of this.data.data.subactors) {
subActors.push(duplicate(game.actors.get(id)));
}
return subActors;
}
/* -------------------------------------------- */
2022-01-14 14:49:16 +01:00
async addSubActor(subActorId) {
let subActors = duplicate(this.data.data.subactors);
subActors.push(subActorId);
await this.update({ 'data.subactors': subActors });
2021-12-02 07:38:59 +01:00
}
/* -------------------------------------------- */
2022-01-14 14:49:16 +01:00
async delSubActor(subActorId) {
2021-12-02 07:38:59 +01:00
let newArray = [];
for (let id of this.data.data.subactors) {
2022-01-14 14:49:16 +01:00
if (id != subActorId) {
newArray.push(id);
2021-12-02 07:38:59 +01:00
}
}
2022-01-14 14:49:16 +01:00
await this.update({ 'data.subactors': newArray });
2021-12-02 07:38:59 +01:00
}
/* -------------------------------------------- */
2022-01-14 14:49:16 +01:00
setDefenseMode(rollData) {
2021-12-02 07:38:59 +01:00
console.log("DEFENSE MODE IS SET FOR", this.name);
this.data.defenseRollData = rollData;
this.data.defenseDefenderId = rollData.defenderId;
this.data.defenseAttackerId = rollData.attackerId;
}
/* -------------------------------------------- */
2022-01-14 14:49:16 +01:00
clearDefenseMode() {
2021-12-02 07:38:59 +01:00
this.data.defenseDefenderId = undefined;
this.data.defenseAttackerId = undefined;
this.data.defenseRollData = undefined;
}
/* -------------------------------------------- */
2022-01-14 14:49:16 +01:00
syncRoll(rollData) {
2021-12-02 20:18:21 +01:00
let linkedRollId = PegasusUtility.getDefenseState(this.id);
2022-01-14 14:49:16 +01:00
if (linkedRollId) {
2021-12-02 07:38:59 +01:00
rollData.linkedRollId = linkedRollId;
}
this.lastRollId = rollData.rollId;
2022-01-14 14:49:16 +01:00
PegasusUtility.saveRollData(rollData);
2021-12-02 20:18:21 +01:00
}
/* -------------------------------------------- */
2022-01-14 14:49:16 +01:00
getStat(statKey) {
2022-01-12 16:25:55 +01:00
let stat
if (statKey == 'mr') {
stat = duplicate(this.data.data.mr);
} else {
stat = duplicate(this.data.data.statistics[statKey]);
}
2021-12-02 20:18:21 +01:00
stat.dice = PegasusUtility.getDiceFromLevel(stat.value);
return stat;
2021-12-02 07:38:59 +01:00
}
/* -------------------------------------------- */
2022-01-14 14:49:16 +01:00
getOneSpec(specId) {
let spec = this.data.items.find(item => item.type == 'specialisation' && item.id == specId);
2021-12-02 20:18:21 +01:00
if (spec) {
spec = duplicate(spec);
spec.data.dice = PegasusUtility.getDiceFromLevel(spec.data.level);
}
return spec;
}
2022-01-12 16:25:55 +01:00
/* -------------------------------------------- */
2022-01-14 14:49:16 +01:00
updatePerkRounds(itemId, roundValue) {
let item = this.items.get(itemId)
2022-01-12 16:25:55 +01:00
if (item) {
2022-01-14 14:49:16 +01:00
this.updateEmbeddedDocuments('Item', [{ _id: item.id, 'data.roundcount': roundValue }]);
2022-01-12 16:25:55 +01:00
}
}
2022-01-14 14:49:16 +01:00
/* -------------------------------------------- */
getCommonRollData() {
2022-01-11 23:35:23 +01:00
let rollData = {
2022-01-14 14:49:16 +01:00
rollId: randomID(16),
alias: this.name,
2022-01-11 23:35:23 +01:00
actorImg: this.img,
actorId: this.id,
img: this.img,
rollMode: game.settings.get("core", "rollMode"),
activePerks: duplicate(this.getActivePerks()),
optionsDiceList: PegasusUtility.getOptionsDiceList(),
2022-01-14 14:49:16 +01:00
bonusDicesLevel: 0,
2022-01-11 23:35:23 +01:00
hindranceDicesLevel: 0,
otherDicesLevel: 0,
}
return rollData
}
/* -------------------------------------------- */
2022-01-14 14:49:16 +01:00
async startRoll(rollData) {
this.syncRoll(rollData);
let rollDialog = await PegasusRollDialog.create(this, rollData);
2022-01-11 23:35:23 +01:00
console.log(rollDialog);
2022-01-14 14:49:16 +01:00
rollDialog.render(true);
2022-01-11 23:35:23 +01:00
}
2022-01-22 21:49:34 +01:00
/* -------------------------------------------- */
getShieldDice() {
2022-01-25 09:14:32 +01:00
let shields = this.data.items.filter(item => item.type == "shield" && item.data.data.equipped)
2022-01-22 21:49:34 +01:00
let def = 0
2022-01-25 09:14:32 +01:00
for (let sh of shields) {
2022-01-22 21:49:34 +01:00
def += sh.data.data.level
}
return def
}
2022-01-11 23:35:23 +01:00
/* -------------------------------------------- */
2022-01-25 09:14:32 +01:00
rollPool(statKey, useShield = false) {
2022-01-11 23:35:23 +01:00
let stat = this.getStat(statKey);
if (stat) {
let rollData = this.getCommonRollData()
2022-01-14 14:49:16 +01:00
rollData.mode = "stat"
rollData.specList = this.getRelevantSpec(statKey)
2022-01-11 23:35:23 +01:00
rollData.selectedSpec = "0"
2022-01-14 14:49:16 +01:00
rollData.stat = stat;
2022-01-25 09:14:32 +01:00
2022-01-22 21:49:34 +01:00
if (useShield) {
rollData.otherDicesLevel = this.getShieldDice()
}
2022-01-11 23:35:23 +01:00
this.startRoll(rollData);
2021-12-02 20:18:21 +01:00
} else {
ui.notifications.warn("Statistic not found !");
}
}
2022-01-11 23:35:23 +01:00
/* -------------------------------------------- */
2022-01-14 14:49:16 +01:00
rollUnarmedAttack() {
2022-01-11 23:35:23 +01:00
let stat = this.getStat('com');
if (stat) {
let rollData = this.getCommonRollData()
2022-01-14 14:49:16 +01:00
rollData.mode = "stat"
2022-01-11 23:35:23 +01:00
rollData.title = `Unarmed Attack`;
2022-01-14 14:49:16 +01:00
rollData.stat = stat;
2022-01-11 23:35:23 +01:00
rollData.damages = this.getStat('str');
this.startRoll(rollData);
} else {
ui.notifications.warn("Statistic not found !");
}
}
/* -------------------------------------------- */
2022-01-14 14:49:16 +01:00
rollStat(statKey) {
let stat = this.getStat(statKey);
2022-01-11 23:35:23 +01:00
if (stat) {
let rollData = this.getCommonRollData()
2022-01-16 16:18:22 +01:00
rollData.specList = this.getRelevantSpec(statKey)
2022-01-14 14:49:16 +01:00
rollData.mode = "stat"
2022-01-11 23:35:23 +01:00
rollData.title = `Stat ${stat.label}`;
2022-01-14 14:49:16 +01:00
rollData.stat = stat;
2021-12-02 20:18:21 +01:00
2022-01-11 23:35:23 +01:00
this.startRoll(rollData);
} else {
ui.notifications.warn("Statistic not found !");
}
}
2022-01-14 14:49:16 +01:00
2021-12-02 20:18:21 +01:00
/* -------------------------------------------- */
2022-01-14 14:49:16 +01:00
async rollSpec(specId) {
let spec = this.getOneSpec(specId)
2021-12-02 20:18:21 +01:00
if (spec) {
2022-01-14 14:49:16 +01:00
let rollData = this.getCommonRollData()
rollData.mode = "spec"
2022-01-11 23:35:23 +01:00
rollData.title = `Spec. : ${spec.name} `,
2022-01-14 14:49:16 +01:00
rollData.stat = this.getStat(spec.data.statistic)
2022-01-11 23:35:23 +01:00
rollData.spec = spec
2021-12-02 07:38:59 +01:00
2022-01-11 23:35:23 +01:00
this.startRoll(rollData);
2021-12-02 07:38:59 +01:00
} else {
2021-12-02 20:18:21 +01:00
ui.notifications.warn("Specialisation not found !");
2021-12-02 07:38:59 +01:00
}
}
2022-01-14 18:20:15 +01:00
/* -------------------------------------------- */
async rollMR() {
let mr = duplicate(this.data.data.mr);
if (mr) {
mr.dice = PegasusUtility.getDiceFromLevel(mr.value);
let rollData = this.getCommonRollData()
rollData.mode = "MR"
rollData.stat = mr
rollData.activePerks = duplicate(this.getActivePerks()),
2022-01-25 09:14:32 +01:00
rollData.specList = this.getRelevantSpec('mr'),
2022-01-14 18:20:15 +01:00
2022-01-25 09:14:32 +01:00
this.startRoll(rollData);
2022-01-14 18:20:15 +01:00
} else {
ui.notifications.warn("MR not found !");
}
}
2021-12-02 07:38:59 +01:00
/* -------------------------------------------- */
2022-01-14 14:49:16 +01:00
async deleteAllItemsByType(itemType) {
let items = this.data.items.filter(item => item.type == itemType);
await this.deleteEmbeddedDocuments('Item', items);
2022-01-07 20:40:40 +01:00
}
2022-01-08 10:49:08 +01:00
/* -------------------------------------------- */
2022-01-14 14:49:16 +01:00
async addItemWithoutDuplicate(newItem) {
let item = this.data.items.find(item => item.type == newItem.type && item.name.toLowerCase() == newItem.name.toLowerCase())
if (!item) {
await this.createEmbeddedDocuments('Item', [newItem]);
2022-01-08 10:49:08 +01:00
}
}
/* -------------------------------------------- */
2022-01-17 15:09:52 +01:00
async computeNRGHealth() {
2022-01-25 09:14:32 +01:00
if (this.isOwner || game.user.isGM) {
2022-01-19 21:25:59 +01:00
let updates = {}
let phyDiceValue = PegasusUtility.getDiceValue(this.data.data.statistics.phy.value) + this.data.data.secondary.health.bonus + this.data.data.statistics.phy.mod;
if (phyDiceValue != this.data.data.secondary.health.max) {
updates['data.secondary.health.max'] = phyDiceValue
updates['data.secondary.health.value'] = phyDiceValue
}
let mndDiceValue = PegasusUtility.getDiceValue(this.data.data.statistics.mnd.value) + this.data.data.secondary.delirium.bonus + this.data.data.statistics.mnd.mod;
if (mndDiceValue != this.data.data.secondary.delirium.max) {
updates['data.secondary.delirium.max'] = mndDiceValue
updates['data.secondary.delirium.value'] = mndDiceValue
}
2022-01-25 09:14:32 +01:00
let stlDiceValue = PegasusUtility.getDiceValue(this.data.data.statistics.stl.value) + this.data.data.secondary.stealthhealth.bonus + this.data.data.statistics.stl.mod;
2022-01-19 21:25:59 +01:00
if (stlDiceValue != this.data.data.secondary.stealthhealth.max) {
2022-01-25 09:14:32 +01:00
updates['data.secondary.stealthhealth.max'] = stlDiceValue
updates['data.secondary.stealthhealth.value'] = stlDiceValue
2022-01-19 21:25:59 +01:00
}
2022-01-25 09:14:32 +01:00
2022-01-19 21:25:59 +01:00
let socDiceValue = PegasusUtility.getDiceValue(this.data.data.statistics.soc.value) + this.data.data.secondary.socialhealth.bonus + this.data.data.statistics.soc.mod;
if (socDiceValue != this.data.data.secondary.socialhealth.max) {
updates['data.secondary.socialhealth.max'] = socDiceValue
2022-01-25 09:14:32 +01:00
updates['data.secondary.socialhealth.value'] = socDiceValue
2022-01-19 21:25:59 +01:00
}
2022-01-25 09:14:32 +01:00
2022-01-19 21:25:59 +01:00
let nrgValue = PegasusUtility.getDiceValue(this.data.data.statistics.foc.value) + this.data.data.nrg.mod + this.data.data.statistics.foc.mod;
if (nrgValue != this.data.data.nrg.max) {
updates['data.nrg.max'] = nrgValue
updates['data.nrg.value'] = nrgValue
}
2022-01-22 21:49:34 +01:00
nrgValue = PegasusUtility.getDiceValue(this.data.data.statistics.foc.value) + this.data.data.statistics.foc.mod;
2022-01-19 21:25:59 +01:00
if (nrgValue != this.data.data.combat.stunthreshold) {
2022-01-25 09:14:32 +01:00
updates['data.combat.stunthreshold'] = nrgValue
2022-01-19 21:25:59 +01:00
}
2022-01-25 09:14:32 +01:00
2022-01-22 21:49:34 +01:00
let momentum = PegasusUtility.getDiceValue(this.data.data.statistics.foc.value) + this.data.data.statistics.foc.mod
if (momentum != this.data.data.momentum.max) {
2022-01-25 09:14:32 +01:00
updates['data.momentum.value'] = 0
updates['data.momentum.max'] = momentum
}
let mrLevel = (this.data.data.statistics.agi.value + this.data.data.statistics.str.value) - this.data.data.statistics.phy.value
mrLevel = (mrLevel < 1) ? 1 : mrLevel;
if (mrLevel != this.data.data.mr.value) {
updates['data.mr.value'] = mrLevel
2022-01-19 21:25:59 +01:00
}
let race = this.getRace()
2022-01-25 09:14:32 +01:00
if (race && race.name && (race.name != this.data.data.biodata.racename)) {
2022-01-19 21:25:59 +01:00
updates['data.biodata.racename'] = race.name
}
let role = this.getRole()
2022-01-25 09:14:32 +01:00
if (role && role.name && (role.name != this.data.data.biodata.rolename)) {
2022-01-19 21:25:59 +01:00
updates['data.biodata.rolename'] = role.name
}
//console.log("UPD", updates, this.data.data.biodata)
await this.update(updates)
}
2022-01-08 10:49:08 +01:00
}
2022-01-07 20:40:40 +01:00
/* -------------------------------------------- */
2022-01-14 14:49:16 +01:00
async modStat(key, inc = 1) {
let stat = duplicate(this.data.data.statistics[key])
2022-01-07 20:40:40 +01:00
stat.mod += parseInt(inc)
2022-01-14 14:49:16 +01:00
await this.update({ [`data.statistics.${key}`]: stat })
2022-01-07 20:40:40 +01:00
}
2022-01-14 14:49:16 +01:00
2022-01-08 10:49:08 +01:00
/* -------------------------------------------- */
2022-01-14 14:49:16 +01:00
async valueStat(key, inc = 1) {
2022-01-08 10:49:08 +01:00
key = key.toLowerCase()
2022-01-14 14:49:16 +01:00
let stat = duplicate(this.data.data.statistics[key])
2022-01-08 10:49:08 +01:00
stat.value += parseInt(inc)
2022-01-14 14:49:16 +01:00
await this.update({ [`data.statistics.${key}`]: stat })
2022-01-08 10:49:08 +01:00
}
/* -------------------------------------------- */
2022-01-14 14:49:16 +01:00
async addIncSpec(spec, inc = 1) {
2022-01-08 10:49:08 +01:00
console.log("Using spec : ", spec, inc)
2022-01-14 14:49:16 +01:00
let specExist = this.data.items.find(item => item.type == 'specialisation' && item.name.toLowerCase() == spec.name.toLowerCase())
2022-01-08 10:49:08 +01:00
if (specExist) {
specExist = duplicate(specExist)
specExist.data.level += inc;
let update = { _id: specExist._id, "data.level": specExist.data.level };
2022-01-14 14:49:16 +01:00
await this.updateEmbeddedDocuments('Item', [update]);
2022-01-08 10:49:08 +01:00
} else {
spec.data.level += inc;
2022-01-14 14:49:16 +01:00
await this.createEmbeddedDocuments('Item', [spec]);
2022-01-08 10:49:08 +01:00
}
}
2022-01-11 23:35:23 +01:00
/* -------------------------------------------- */
2022-01-14 14:49:16 +01:00
applyAbility(ability, updates = []) {
if (ability.data.affectedstat != "notapplicable") {
let stat = duplicate(this.data.data.statistics[ability.data.affectedstat])
2022-01-11 23:35:23 +01:00
stat.value += parseInt(ability.data.statlevelincrease)
stat.mod += parseInt(ability.data.statmodifier)
updates[`data.statistics.${ability.data.affectedstat}`] = stat
2022-01-14 14:49:16 +01:00
}
2022-01-11 23:35:23 +01:00
}
2022-01-06 18:22:05 +01:00
/* -------------------------------------------- */
2022-01-14 14:49:16 +01:00
async applyRace(race) {
2022-01-17 15:09:52 +01:00
let updates = { 'data.biodata.racename': race.name }
2022-01-06 18:22:05 +01:00
let newItems = []
2022-01-14 14:49:16 +01:00
await this.deleteAllItemsByType('race')
2022-01-06 18:22:05 +01:00
newItems.push(race);
for (let ability of race.data.abilities) {
2022-01-11 23:35:23 +01:00
newItems.push(ability);
2022-01-14 14:49:16 +01:00
this.applyAbility(ability, updates)
2022-01-11 23:35:23 +01:00
}
2022-01-14 14:49:16 +01:00
if (race.data.powersgained) {
2022-01-11 23:35:23 +01:00
for (let power of race.data.powersgained) {
newItems.push(power);
2022-01-08 18:28:01 +01:00
}
2022-01-11 23:35:23 +01:00
}
2022-01-14 14:49:16 +01:00
if (race.data.specialisations) {
2022-01-11 23:35:23 +01:00
for (let spec of race.data.specialisations) {
newItems.push(spec);
2022-01-08 18:28:01 +01:00
}
2022-01-11 23:35:23 +01:00
}
2022-01-14 14:49:16 +01:00
if (race.data.attackgained) {
for (let weapon of race.data.attackgained) {
2022-01-11 23:35:23 +01:00
newItems.push(weapon);
2022-01-08 18:28:01 +01:00
}
2022-01-11 23:35:23 +01:00
}
2022-01-14 14:49:16 +01:00
if (race.data.armorgained) {
2022-01-11 23:35:23 +01:00
for (let armor of race.data.armorgained) {
newItems.push(armor);
2022-01-08 18:28:01 +01:00
}
2022-01-06 18:22:05 +01:00
}
2022-01-11 23:35:23 +01:00
2022-01-14 14:49:16 +01:00
await this.update(updates)
2022-01-06 18:22:05 +01:00
await this.createEmbeddedDocuments('Item', newItems)
console.log("Updates", updates, newItems)
console.log("Updated actor", this)
}
/* -------------------------------------------- */
2022-01-14 14:49:16 +01:00
getIncreaseStatValue(updates, statKey) {
2022-01-07 20:40:40 +01:00
let stat = duplicate(this.data.data.statistics[statKey])
stat.value += 1;
updates[`data.statistics.${statKey}`] = stat
}
/* -------------------------------------------- */
2022-01-14 14:49:16 +01:00
async applyRole(role) {
2022-01-06 18:22:05 +01:00
console.log("ROLE", role)
2022-01-07 20:40:40 +01:00
2022-01-17 15:09:52 +01:00
let updates = { 'data.biodata.rolename': role.name }
2022-01-07 20:40:40 +01:00
let newItems = []
2022-01-14 14:49:16 +01:00
await this.deleteAllItemsByType('role')
2022-01-07 20:40:40 +01:00
newItems.push(role);
2022-01-14 14:49:16 +01:00
this.getIncreaseStatValue(updates, role.data.statincrease1)
this.getIncreaseStatValue(updates, role.data.statincrease2)
2022-01-07 20:40:40 +01:00
2022-01-10 08:00:27 +01:00
//newItems = newItems.concat(duplicate(role.data.specialisationsplus1))
2022-01-08 10:49:08 +01:00
newItems = newItems.concat(duplicate(role.data.specialperk))
2022-01-14 14:49:16 +01:00
await this.update(updates)
2022-01-07 20:40:40 +01:00
await this.createEmbeddedDocuments('Item', newItems)
2022-01-06 18:22:05 +01:00
}
2022-01-14 14:49:16 +01:00
2021-12-02 07:38:59 +01:00
/* -------------------------------------------- */
2022-01-14 14:49:16 +01:00
async rollPower(powId) {
let power = this.data.items.find(item => item.type == 'power' && item.id == powId);
if (power) {
2021-12-02 07:38:59 +01:00
let rollData = {
2022-01-14 14:49:16 +01:00
mode: "power",
alias: this.name,
actorImg: this.img,
2021-12-02 07:38:59 +01:00
actorId: this.id,
2022-01-14 14:49:16 +01:00
img: power.img,
2021-12-02 07:38:59 +01:00
rollMode: game.settings.get("core", "rollMode"),
2022-01-14 14:49:16 +01:00
title: `Power ${power.name} `,
power: duplicate(power),
2021-12-03 18:31:43 +01:00
activePerks: duplicate(this.getActivePerks()),
optionsDiceList: PegasusUtility.getOptionsDiceList(),
2022-01-14 14:49:16 +01:00
bonusDicesLevel: 0,
2021-12-03 18:31:43 +01:00
hindranceDicesLevel: 0,
otherDicesLevel: 0,
2022-01-14 14:49:16 +01:00
}
2021-12-02 07:38:59 +01:00
this.updateWithTarget(rollData);
2022-01-14 14:49:16 +01:00
this.syncRoll(rollData);
let rollDialog = await PegasusRollDialog.create(this, rollData);
console.log(rollDialog);
rollDialog.render(true);
} else {
ui.notifications.warn("Technique not found !");
}
}
2022-01-14 18:20:15 +01:00
2022-01-14 14:49:16 +01:00
/* -------------------------------------------- */
updateWithTarget(rollData) {
let objectDefender
let target = PegasusUtility.getTarget();
if (!target) {
ui.notifications.warn("You are using a Weapon without a Target.");
} else {
let defenderActor = game.actors.get(target.data.actorId);
objectDefender = PegasusUtility.data(defenderActor);
objectDefender = mergeObject(objectDefender, target.data.actorData);
rollData.defender = objectDefender;
rollData.attackerId = this.id;
rollData.defenderId = objectDefender._id;
//console.log("ROLLDATA DEFENDER !!!", rollData);
}
}
2022-01-14 18:20:15 +01:00
/* -------------------------------------------- */
async rollArmor(armorId) {
let armor = this.data.items.get(armorId)
if (armor) {
let rollData = this.getCommonRollData()
armor = duplicate(armor);
this.checkAndPrepareArmor(armor);
rollData.mode = "armor"
rollData.img = armor.img
rollData.armor = armor
rollData.title = `Armor : ${armor.name}`
rollData.stat = this.getStat(armor.data.statistic)
rollData.specList = this.getRelevantSpec(armor.data.statistic)
rollData.activePerks = duplicate(this.getActivePerks())
rollData.isResistance = true;
rollData.otherDicesLevel = armor.data.resistance
//this.updateWithTarget(rollData);
this.startRoll(rollData);
} else {
ui.notifications.warn("Armor not found !", weaponId);
}
}
2022-01-14 14:49:16 +01:00
/* -------------------------------------------- */
2022-01-14 18:20:15 +01:00
async rollWeapon(weaponId, damage = false) {
2022-01-14 14:49:16 +01:00
let weapon = this.data.items.get(weaponId)
if (weapon) {
let rollData = this.getCommonRollData()
weapon = duplicate(weapon);
this.checkAndPrepareWeapon(weapon);
rollData.mode = "weapon"
rollData.img = weapon.img
rollData.weapon = weapon
rollData.title = `Weapon : ${weapon.name}`
rollData.specList = this.getRelevantSpec(weapon.data.statistic)
rollData.activePerks = duplicate(this.getActivePerks())
2022-01-14 18:20:15 +01:00
if (damage) {
2022-01-25 09:14:32 +01:00
rollData.stat = this.getStat(weapon.data.damagestatistic)
2022-01-14 14:49:16 +01:00
rollData.isDamage = true;
rollData.otherDicesLevel = weapon.data.damage
2022-01-18 13:36:27 +01:00
} else {
rollData.stat = this.getStat(weapon.data.statistic)
2022-01-14 14:49:16 +01:00
}
2021-12-02 07:38:59 +01:00
2022-01-14 14:49:16 +01:00
//this.updateWithTarget(rollData);
this.startRoll(rollData);
2021-12-02 07:38:59 +01:00
} else {
ui.notifications.warn("Weapon not found !", weaponId);
}
}
2022-01-14 18:20:15 +01:00
/* -------------------------------------------- */
async rollPower(powerId) {
let power = this.data.items.get(powerId)
if (power) {
let rollData = this.getCommonRollData()
power = duplicate(power);
rollData.mode = "power"
rollData.img = power.img
rollData.power = power
rollData.title = `Power : ${power.name}`
rollData.stat = this.getStat(power.data.statistic)
rollData.specList = this.getRelevantSpec(power.data.statistic)
rollData.activePerks = duplicate(this.getActivePerks())
this.startRoll(rollData);
} else {
ui.notifications.warn("Power not found !", powerId);
}
}
2022-01-25 09:14:32 +01:00
2021-12-02 07:38:59 +01:00
}