fvtt-hero-system-6/modules/hero6-actor.js

826 lines
29 KiB
JavaScript
Raw Normal View History

2022-12-15 21:44:23 +01:00
/* -------------------------------------------- */
import { Hero6Utility } from "./hero6-utility.js";
import { Hero6RollDialog } from "./hero6-roll-dialog.js";
2023-05-07 14:03:14 +02:00
import { Hero6LiftDice } from "./hero6-lift-dice.js";
2022-12-15 21:44:23 +01:00
/* -------------------------------------------- */
2023-08-09 18:05:16 +02:00
const __speed2Segments = [[0], [7], [6, 12], [4, 8, 12], [3, 6, 9, 12], [3, 5, 8, 10, 12], [2, 4, 6, 8, 10, 12],
2023-05-07 14:03:14 +02:00
[2, 4, 6, 7, 9, 11, 12], [2, 3, 5, 6, 8, 9, 11, 12], [2, 3, 4, 6, 7, 8, 10, 11, 12], [2, 3, 4, 5, 6, 8, 9, 10, 11, 12],
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]]
2022-12-15 21:44:23 +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 Hero6Actor 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;
}
2023-08-09 18:05:16 +02:00
if (data.type == 'character' || data.type == 'minion') {
const maneuvers = await Hero6Utility.loadCompendium("fvtt-hero-system-6.maneuvers")
let maneuversObj = maneuvers.map(i => i.toObject())
2023-08-09 18:05:16 +02:00
data.items = maneuversObj.filter(m => m.system.isstock)
2022-12-15 21:44:23 +01:00
}
return super.create(data, options);
}
/* -------------------------------------------- */
prepareBaseData() {
}
/* -------------------------------------------- */
async prepareData() {
super.prepareData();
}
/* -------------------------------------------- */
computeDerivatedData() {
2023-08-09 18:05:16 +02:00
let newSTREND = this.computeSTREND()
if (newSTREND != this.system.characteristics.str.strend) {
this.update({ 'system.characteristics.str.strend': newSTREND })
2022-12-15 21:44:23 +01:00
}
}
2023-08-07 23:28:04 +02:00
/* -------------------------------------------- */
performMigration() {
// Fix OCV/OMCV rollable
if (!this.system.characteristics.ocv.hasroll) {
this.update({ 'system.characteristics.ocv.hasroll': true, 'system.characteristics.omcv.hasroll': true })
}
}
/* -------------------------------------------- */
2023-03-22 22:43:30 +01:00
computeDicesValue() {
2023-04-06 16:57:19 +02:00
this.system.biodata.presenceattack = Hero6Utility.getDerivatedDiceFormulas(this.system.characteristics.pre.value)
2023-05-07 14:03:14 +02:00
this.system.characteristics.str.strdice = Hero6LiftDice.getLiftDice(this.system.characteristics.str.value)
this.system.characteristics.str.lift = Hero6LiftDice.getLift(this.system.characteristics.str.value)
2023-03-22 22:43:30 +01:00
}
2022-12-15 21:44:23 +01:00
/* -------------------------------------------- */
prepareDerivedData() {
if (this.type == 'character' || game.user.isGM) {
this.system.encCapacity = this.getEncumbranceCapacity()
this.buildContainerTree()
this.computeDerivatedData()
2023-03-22 22:43:30 +01:00
this.computeDicesValue()
2022-12-15 21:44:23 +01:00
}
super.prepareDerivedData();
}
/* -------------------------------------------- */
_preUpdate(changed, options, user) {
super._preUpdate(changed, options, user);
}
/* -------------------------------------------- */
getEncumbranceCapacity() {
let numLift = this.system.characteristics.str.lift.match(/\d*\s/g)
if (numLift && numLift[0] && Number(numLift[0])) {
return numLift[0] / 2
}
2022-12-15 21:44:23 +01:00
}
/* -------------------------------------------- */
getMoneys() {
2023-03-14 19:23:19 +01:00
let comp = this.items.filter(item => item.type == 'currency');
2022-12-15 21:44:23 +01:00
Hero6Utility.sortArrayObjectsByName(comp)
return comp;
}
getEquippedWeapons() {
let comp = duplicate(this.items.filter(item => item.type == 'weapon' && item.system.equipped) || []);
Hero6Utility.sortArrayObjectsByName(comp)
return comp;
}
/* -------------------------------------------- */
getArmors() {
let comp = duplicate(this.items.filter(item => item.type == 'armor') || []);
Hero6Utility.sortArrayObjectsByName(comp)
return comp;
}
getEquippedArmor() {
let comp = this.items.find(item => item.type == 'armor' && item.system.equipped)
if (comp) {
return duplicate(comp)
}
return undefined
}
/* -------------------------------------------- */
getShields() {
let comp = duplicate(this.items.filter(item => item.type == 'shield') || []);
Hero6Utility.sortArrayObjectsByName(comp)
return comp;
}
getEquippedShield() {
let comp = this.items.find(item => item.type == 'shield' && item.system.equipped)
if (comp) {
return duplicate(comp)
}
return undefined
}
/* -------------------------------------------- */
getRace() {
let race = this.items.filter(item => item.type == 'race')
return race[0] ?? [];
}
/* -------------------------------------------- */
checkAndPrepareEquipment(item) {
}
/* -------------------------------------------- */
checkAndPrepareEquipments(listItem) {
for (let item of listItem) {
this.checkAndPrepareEquipment(item)
}
return listItem
}
/* -------------------------------------------- */
getItemById(id) {
let item = this.items.find(item => item.id == id);
if (item) {
item = duplicate(item)
}
return item;
}
2023-08-09 18:05:16 +02:00
/* -------------------------------------------- */
async _preCreate(data, options, user) {
await super._preCreate(data, options, user);
// Configure prototype token settings
const prototypeToken = {};
if (this.type === "character") Object.assign(prototypeToken, {
sight: { enabled: true }, actorLink: true, disposition: CONST.TOKEN_DISPOSITIONS.FRIENDLY
});
this.updateSource({ prototypeToken });
}
2023-03-14 19:23:19 +01:00
/* -------------------------------------------- */
prepareSkill(skill) {
skill.roll = 0
skill.charac = "N/A"
2023-05-07 14:03:14 +02:00
skill.system.skilltype = skill.system.skilltype.toLowerCase()
2023-03-14 19:23:19 +01:00
if (skill.system.skillfamiliarity) {
skill.roll = 8;
} else if (skill.system.skillprofiency) {
skill.roll = 10;
} else if (skill.system.skilltype == "agility") {
2023-05-07 14:03:14 +02:00
skill.charac = "dex"
2023-03-14 19:23:19 +01:00
let charac = duplicate(this.system.characteristics.dex)
this.prepareCharacValues(charac)
skill.roll = charac.roll
} else if (skill.system.skilltype == "interaction") {
2023-05-07 14:03:14 +02:00
skill.charac = "pre"
2023-03-14 19:23:19 +01:00
let charac = duplicate(this.system.characteristics.pre)
this.prepareCharacValues(charac)
skill.roll = charac.roll
} else if (skill.system.skilltype == "intellect") {
2023-05-07 14:03:14 +02:00
skill.charac = "int"
2023-03-14 19:23:19 +01:00
let charac = duplicate(this.system.characteristics.int)
this.prepareCharacValues(charac)
skill.roll = charac.roll
} else if (skill.system.skilltype == "background") {
skill.roll = 11
} else if (skill.system.skilltype == "custom") {
if (skill.system.characteristic == "manual") {
skill.roll = skill.system.base
} else {
2023-05-07 14:03:14 +02:00
skill.charac = (skill.system.characteristic == "") ? "str" : skill.system.characteristic
2023-03-14 19:23:19 +01:00
let charac = duplicate(this.system.characteristics[skill.system.characteristic])
this.prepareCharacValues(charac)
skill.roll = charac.roll
}
}
2023-05-07 14:03:14 +02:00
console.log("SILL", skill)
2023-03-14 19:23:19 +01:00
if (skill.system.levels > 0) {
skill.roll += skill.system.levels
}
}
2023-08-09 18:05:16 +02:00
2023-08-07 23:28:04 +02:00
/* -------------------------------------------- */
prepareManeuver(maneuver) {
2023-08-09 18:05:16 +02:00
2023-08-07 23:28:04 +02:00
maneuver.roll = 11 + this.system.characteristics.ocv.value
2023-08-09 18:05:16 +02:00
if (Number(maneuver.system.ocv)) {
2023-08-07 23:28:04 +02:00
maneuver.roll += Number(maneuver.system.ocv)
} else {
maneuver.noOCV = true
}
}
2022-12-15 21:44:23 +01:00
/* -------------------------------------------- */
getSkills() {
let comp = duplicate(this.items.filter(item => item.type == 'skill') || [])
2023-03-14 19:23:19 +01:00
for (let skill of comp) {
this.prepareSkill(skill)
}
2022-12-15 21:44:23 +01:00
Hero6Utility.sortArrayObjectsByName(comp)
return comp
}
2023-02-14 15:34:37 +01:00
getPerks() {
let comp = duplicate(this.items.filter(item => item.type == 'perk') || [])
Hero6Utility.sortArrayObjectsByName(comp)
return comp
}
2023-02-21 20:22:08 +01:00
async getPowers() {
2023-02-14 15:34:37 +01:00
let comp = duplicate(this.items.filter(item => item.type == 'power') || [])
2023-03-14 19:23:19 +01:00
for (let c of comp) {
c.enrichDescription = c.name + "<br>" + await TextEditor.enrichHTML(c.system.description, { async: true })
2023-05-07 14:03:14 +02:00
c.enrichNotes = c.name + "<br>" + await TextEditor.enrichHTML(c.system.notes, { async: true })
2023-02-21 20:22:08 +01:00
}
2023-02-14 15:34:37 +01:00
Hero6Utility.sortArrayObjectsByName(comp)
return comp
}
getTalents() {
let comp = duplicate(this.items.filter(item => item.type == 'talent') || [])
Hero6Utility.sortArrayObjectsByName(comp)
return comp
}
getComplications() {
let comp = duplicate(this.items.filter(item => item.type == 'complication') || [])
Hero6Utility.sortArrayObjectsByName(comp)
return comp
}
2022-12-15 21:44:23 +01:00
/* -------------------------------------------- */
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;
}
2023-03-21 14:01:27 +01:00
getManeuvers() {
2023-04-06 16:57:19 +02:00
let maneuvers = {
2023-03-21 20:43:41 +01:00
general: this.items.filter(item => item.type == "maneuver" && item.system.maneuvertype == "general"),
offensive: this.items.filter(item => item.type == "maneuver" && item.system.maneuvertype == "offensive"),
defensive: this.items.filter(item => item.type == "maneuver" && item.system.maneuvertype == "defensive")
}
2023-04-06 16:57:19 +02:00
Hero6Utility.sortArrayObjectsByName(maneuvers.general)
Hero6Utility.sortArrayObjectsByName(maneuvers.offensive)
2023-03-24 10:17:20 +01:00
Hero6Utility.sortArrayObjectsByName(maneuvers.defensive)
return maneuvers
}
2023-08-15 22:12:02 +02:00
getAllManeuvers() {
let maneuvers = this.items.filter(item => item.type == "maneuver")
Hero6Utility.sortArrayObjectsByName(maneuvers)
return maneuvers
}
2023-03-24 10:17:20 +01:00
getNonStockManeuvers() {
let maneuvers = this.items.filter(item => item.type == "maneuver" && !item.system.isstock)
Hero6Utility.sortArrayObjectsByName(maneuvers)
2023-03-21 20:43:41 +01:00
return maneuvers
2023-03-21 14:01:27 +01:00
}
2022-12-15 21:44:23 +01:00
getEquipments() {
2023-03-24 10:17:20 +01:00
let list = this.items.filter(item => item.type == "equipment" && item.system.subtype == "equipment");
2023-04-06 16:57:19 +02:00
Hero6Utility.sortArrayObjectsByName(list)
2023-03-24 10:17:20 +01:00
return list
2023-02-21 20:22:08 +01:00
}
2023-08-22 23:30:31 +02:00
getMoneys() {
let list = this.items.filter(item => item.type == "equipment" && item.system.subtype == "money");
Hero6Utility.sortArrayObjectsByName(list)
return list
}
2023-02-21 20:22:08 +01:00
getWeapons() {
2023-03-24 10:17:20 +01:00
let list = this.items.filter(item => item.type == "equipment" && item.system.subtype == "weapon");
2023-04-06 16:57:19 +02:00
Hero6Utility.sortArrayObjectsByName(list)
2023-03-24 10:17:20 +01:00
return list
2023-02-21 20:22:08 +01:00
}
getArmors() {
2023-03-24 10:17:20 +01:00
let list = this.items.filter(item => item.type == "equipment" && item.system.subtype == "armor");
2023-04-06 16:57:19 +02:00
Hero6Utility.sortArrayObjectsByName(list)
2023-03-24 10:17:20 +01:00
return list
2023-02-21 20:22:08 +01:00
}
getShields() {
2023-03-24 10:17:20 +01:00
let list = this.items.filter(item => item.type == "equipment" && item.system.subtype == "shield");
2023-04-06 16:57:19 +02:00
Hero6Utility.sortArrayObjectsByName(list)
2023-03-24 10:17:20 +01:00
return list
2022-12-15 21:44:23 +01:00
}
2023-03-21 14:01:27 +01:00
getEquipmentsMoneys() {
2023-03-24 10:17:20 +01:00
let list = duplicate(this.items.filter(item => item.type == "equipment" && (item.system.subtype == "equipment" || item.system.subtype == "money")) || [])
2023-04-06 16:57:19 +02:00
Hero6Utility.sortArrayObjectsByName(list)
2023-03-24 10:17:20 +01:00
return list
2023-03-21 14:01:27 +01:00
}
2022-12-15 21:44:23 +01:00
getEquipmentsOnly() {
2023-03-24 10:17:20 +01:00
let list = duplicate(this.items.filter(item => item.type == "equipment" && item.system.subtype == "equipment") || [])
2023-04-06 16:57:19 +02:00
Hero6Utility.sortArrayObjectsByName(list)
2023-03-24 10:17:20 +01:00
return list
2022-12-15 21:44:23 +01:00
}
/* ------------------------------------------- */
buildContainerTree() {
let equipments = duplicate(this.items.filter(item => item.type == "equipment") || []);
2022-12-15 21:44:23 +01:00
let enc = 0
2023-06-30 13:14:40 +02:00
let value = 0
for (let equip1 of equipments) {
if (Number(equip1.system.weight) && Number(equip1.system.quantity)) {
enc += equip1.system.weight * equip1.system.quantity
2022-12-15 21:44:23 +01:00
}
2023-06-30 13:14:40 +02:00
if (Number(equip1.system.value) && Number(equip1.system.quantity)) {
value += equip1.system.value * equip1.system.quantity
}
2022-12-15 21:44:23 +01:00
}
// Store local values
this.encCurrent = enc
2023-06-30 13:14:40 +02:00
this.totalValue = value
2022-12-15 21:44:23 +01:00
}
/* -------------------------------------------- */
async incDecHP(formula) {
2023-03-14 19:23:19 +01:00
let dmgRoll = new Roll(formula + "[dark-starsorange]").roll({ async: false })
2022-12-15 21:44:23 +01:00
await Hero6Utility.showDiceSoNice(dmgRoll, game.settings.get("core", "rollMode"))
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)
}
/* -------------------------------------------- */
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?.system?.containerid) { // remove from container
2022-12-15 21:44:23 +01:00
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?.system) {
2022-12-15 21:44:23 +01:00
let update = { _id: item.id, "system.equipped": !item.system.equipped };
await this.updateEmbeddedDocuments('Item', [update]); // Updates one EmbeddedEntity
}
}
2023-06-30 21:11:29 +02:00
/* -------------------------------------------- */
async cleanCombat() {
await this.setFlag("world", "hold-action", false)
2023-08-09 18:05:16 +02:00
await this.setFlag("world", "abort-action", { state: false, count: 0 })
2023-06-30 21:11:29 +02:00
}
async holdAction() {
2023-07-16 14:33:26 +02:00
await this.disableAbortAction()
2023-06-30 21:11:29 +02:00
if (this.getFlag("world", "hold-action")) {
await this.setFlag("world", "hold-action", false)
2023-07-16 14:33:26 +02:00
//game.combat.holdAction(this.id, false)
game.combat.forceHold(this, false)
2023-07-07 15:35:24 +02:00
return false
2023-06-30 21:11:29 +02:00
} else {
await this.setFlag("world", "hold-action", true)
2023-07-16 14:33:26 +02:00
//game.combat.holdAction(this.id, false)
game.combat.forceHold(this, true)
2023-07-07 15:35:24 +02:00
return true
2023-06-30 21:11:29 +02:00
}
}
2023-07-06 13:10:41 +02:00
async disableHoldAction() {
await this.setFlag("world", "hold-action", false)
}
2023-06-30 22:20:44 +02:00
async disableAbortAction() {
await this.setFlag("world", "abort-action", { state: false, count: 0 })
}
2023-06-30 21:11:29 +02:00
async abortAction() {
2023-07-16 14:33:26 +02:00
await this.disableHoldAction()
2023-06-30 22:20:44 +02:00
let abort = this.getFlag("world", "abort-action")
2023-07-06 13:10:41 +02:00
if (abort.state) {
2023-06-30 22:20:44 +02:00
await this.setFlag("world", "abort-action", { state: false, count: 0 })
2023-07-16 14:33:26 +02:00
game.combat.forceAbort(this, false)
//game.combat.abortAction(this.id, false)
2023-06-30 21:11:29 +02:00
} else {
2023-06-30 22:20:44 +02:00
await this.setFlag("world", "abort-action", { state: true, count: 0 })
2023-07-16 14:33:26 +02:00
game.combat.forceAbort(this, true)
//game.combat.abortAction(this.id, true)
2023-06-30 21:11:29 +02:00
}
}
2023-07-24 13:35:35 +02:00
async incAbortActionCount() {
let abort = this.getFlag("world", "abort-action")
if (abort.state) {
abort.count++
await this.setFlag("world", "abort-action", abort)
if (abort.count == 2) {
return true
}
}
return false
}
2023-06-30 21:11:29 +02:00
getHoldAction() {
return this.getFlag("world", "hold-action")
}
2023-06-30 22:20:44 +02:00
getAbortAction() {
let abort = this.getFlag("world", "abort-action")
return abort?.state || false
2023-06-30 21:11:29 +02:00
}
2023-04-06 16:57:19 +02:00
2022-12-15 21:44:23 +01:00
/* -------------------------------------------- */
2023-05-07 14:03:14 +02:00
hasPhase(segmentNumber) {
let index = Math.min(Math.max(this.system.characteristics.spd.value, 1), 12) // Security bounds
let phases = __speed2Segments[index]
2023-07-06 13:10:41 +02:00
console.log("index", segmentNumber, index, phases, phases.includes(segmentNumber), __speed2Segments)
2023-04-06 16:57:19 +02:00
return phases.includes(segmentNumber)
}
/* -------------------------------------------- */
getSegments() {
2023-07-06 13:10:41 +02:00
let index = Math.min(Math.max(this.system.characteristics.spd.value, 1), 12) // Security bounds
//console.log("INDEX", index, __speed2Segments[index])
2023-04-06 16:57:19 +02:00
return __speed2Segments[index]
}
2023-07-06 13:10:41 +02:00
getPhasesString() {
let index = Math.min(Math.max(this.system.characteristics.spd.value, 1), 12) // Security bounds
return __speed2Segments[index].toString()
}
2023-04-06 16:57:19 +02:00
/* -------------------------------------------- */
2023-07-16 14:33:26 +02:00
getBaseInit(turn) {
2023-08-09 18:05:16 +02:00
if (turn != this.turn) {
2023-07-16 14:33:26 +02:00
let r = new Roll("1d6").roll({ async: false })
2023-08-07 23:28:04 +02:00
this.currentInit = Number(this.system.characteristics.dex.initiative) + Number(((r.total / 10).toFixed(2)))
2023-07-16 14:33:26 +02:00
this.turn = turn
}
return this.currentInit
2022-12-15 21:44:23 +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 });
}
/* -------------------------------------------- */
2023-03-14 19:23:19 +01:00
prepareCharacValues(charac) {
2023-08-09 18:05:16 +02:00
if (charac.label == "OCV" || charac.label == "OMCV" ) {
charac.total = charac.value
charac.roll = 11 + charac.value
} else {
charac.total = charac.value
charac.roll = 9 + Math.round((charac.value) / 5)
}
2023-02-01 12:16:04 +01:00
}
prepareCharac() {
let characs = duplicate(this.system.characteristics)
2023-03-14 19:23:19 +01:00
for (let key in characs) {
2023-05-07 14:03:14 +02:00
let ch = characs[key]
this.prepareCharacValues(ch)
if (key == "str") {
ch.lift = Hero6LiftDice.getLift(ch.value)
ch.liftDice = Hero6LiftDice.getLiftDice(ch.value)
}
2023-07-06 13:10:41 +02:00
if (key == "spd") {
ch.phasesString = this.getPhasesString()
}
2023-08-09 18:05:16 +02:00
if (key == "pre") {
2023-07-06 13:10:41 +02:00
ch.presenceattack = duplicate(this.system.biodata.presenceattack)
}
2023-02-01 12:16:04 +01:00
}
return characs
2022-12-15 21:44:23 +01:00
}
/* -------------------------------------------- */
getOneSkill(skillId) {
let skill = this.items.find(item => item.type == 'skill' && item.id == skillId)
if (skill) {
skill = duplicate(skill);
}
return skill;
}
/* -------------------------------------------- */
computeSTREND() {
let newSTREND = 0
switch (this.system.characteristics.str.strendmode) {
case "str20":
newSTREND = Math.floor(this.system.characteristics.str.value / 20)
break;
case "str10":
newSTREND = Math.floor(this.system.characteristics.str.value / 10)
break;
case "str5":
newSTREND = Math.floor(this.system.characteristics.str.value / 5)
break;
}
return newSTREND
}
2022-12-15 21:44:23 +01:00
/* -------------------------------------------- */
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 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
}
}
}
/* -------------------------------------------- */
2023-02-01 12:16:04 +01:00
getCommonRollData(chKey = undefined) {
2022-12-15 21:44:23 +01:00
let rollData = Hero6Utility.getBasicRollData()
rollData.alias = this.name
rollData.actorImg = this.img
rollData.actorId = this.id
rollData.img = this.img
2023-02-01 12:16:04 +01:00
if (chKey) {
rollData.charac = duplicate(this.system.characteristics[chKey])
this.prepareCharacValues(rollData.charac)
}
2022-12-15 21:44:23 +01:00
if (rollData.defenderTokenId) {
let defenderToken = game.canvas.tokens.get(rollData.defenderTokenId)
let defender = defenderToken.actor
// Distance management
let token = this.token
if (!token) {
let tokens = this.getActiveTokens()
token = tokens[0]
}
if (token) {
const ray = new Ray(token.object?.center || token.center, defenderToken.center)
rollData.tokensDistance = canvas.grid.measureDistances([{ ray }], { gridSpaces: false })[0] / canvas.grid.grid.options.dimensions.distance
} else {
ui.notifications.info("No token connected to this actor, unable to compute distance.")
return
}
if (defender) {
rollData.forceAdvantage = defender.isAttackerAdvantage()
rollData.advantageFromTarget = true
}
}
console.log("ROLLDATA", rollData)
return rollData
}
/* -------------------------------------------- */
rollPerception() {
let rollData = this.getCommonRollData("int")
rollData.isPerception = true
rollData.charac.roll = Number(rollData.charac.perceptionroll)
rollData.mode = "perception"
if (rollData.target) {
ui.notifications.warn("You are targetting a token with a skill : please use a Weapon instead.")
return
}
this.startRoll(rollData)
}
2022-12-15 21:44:23 +01:00
/* -------------------------------------------- */
2023-02-01 12:16:04 +01:00
rollCharac(chKey) {
let rollData = this.getCommonRollData(chKey)
rollData.mode = "charac"
2022-12-15 21:44:23 +01:00
if (rollData.target) {
ui.notifications.warn("You are targetting a token with a skill : please use a Weapon instead.")
return
}
2023-02-01 12:16:04 +01:00
this.startRoll(rollData)
2022-12-15 21:44:23 +01:00
}
2023-02-14 15:34:37 +01:00
/* -------------------------------------------- */
rollItem(itemId) {
2023-03-14 19:23:19 +01:00
let item = this.items.get(itemId)
2023-02-14 15:34:37 +01:00
let rollData = this.getCommonRollData()
rollData.mode = "item"
rollData.item = duplicate(item)
2023-03-21 14:01:27 +01:00
if (item.type == "skill") {
2023-03-14 19:23:19 +01:00
this.prepareSkill(rollData.item)
}
2023-08-07 23:28:04 +02:00
if (item.type == "maneuver") {
this.prepareManeuver(rollData.item)
}
2023-02-14 15:34:37 +01:00
this.startRoll(rollData)
}
2023-05-07 14:03:14 +02:00
/* -------------------------------------------- */
async rollDamage(itemId) {
let item = this.items.get(itemId)
let rollData = this.getCommonRollData()
rollData.mode = "damage"
rollData.item = duplicate(item)
2023-06-30 13:14:40 +02:00
rollData.title = item.name
2023-05-07 14:03:14 +02:00
rollData.diceFormula = Hero6Utility.convertRollHeroSyntax(item.system.damage)
let myRoll = new Roll(rollData.diceFormula).roll({ async: false })
await Hero6Utility.showDiceSoNice(myRoll, game.settings.get("core", "rollMode"))
2023-05-07 14:03:14 +02:00
rollData.roll = myRoll
rollData.result = myRoll.total
rollData.bodyValue = Hero6Utility.computeBodyValue(myRoll)
2022-12-15 21:44:23 +01:00
2023-06-30 13:14:40 +02:00
let msgFlavor = await renderTemplate(`systems/fvtt-hero-system-6/templates/chat/chat-damage-result.hbs`, rollData)
let msg = await rollData.roll.toMessage({
user: game.user.id,
rollMode: game.settings.get("core", "rollMode"),
flavor: msgFlavor
2023-05-07 14:03:14 +02:00
})
2023-06-30 13:14:40 +02:00
rollData.roll = duplicate(rollData.roll) // Convert to object
2023-05-07 14:03:14 +02:00
msg.setFlag("world", "rolldata", rollData)
console.log("Rolldata result", rollData)
}
/* -------------------------------------------- */
async rollLiftDice() {
let rollData = this.getCommonRollData()
rollData.mode = "lift-dice"
rollData.diceFormula = Hero6Utility.convertRollHeroSyntax(Hero6LiftDice.getLiftDice(this.system.characteristics.str.value))
2023-05-07 14:03:14 +02:00
let myRoll = new Roll(rollData.diceFormula).roll({ async: false })
await Hero6Utility.showDiceSoNice(myRoll, game.settings.get("core", "rollMode"))
rollData.bodyValue = Hero6Utility.computeBodyValue(myRoll)
2023-05-07 14:03:14 +02:00
rollData.result = myRoll.total
2023-06-30 17:21:11 +02:00
rollData.roll = duplicate(myRoll)
2023-05-07 14:03:14 +02:00
2023-06-30 17:21:11 +02:00
let msgFlavor = await renderTemplate(`systems/fvtt-hero-system-6/templates/chat/chat-lift-dice-result.hbs`, rollData)
let msg = await myRoll.toMessage({
user: game.user.id,
rollMode: game.settings.get("core", "rollMode"),
flavor: msgFlavor
2023-05-07 14:03:14 +02:00
})
msg.setFlag("world", "rolldata", rollData)
console.log("Rolldata result", rollData)
}
2023-08-07 23:28:04 +02:00
/* -------------------------------------------- */
rollManeuver(maneuverId) {
let skill = this.items.get(skillId)
if (skill) {
if (skill.system.islore && skill.system.level == 0) {
ui.notifications.warn("You can't use Lore Skills with a SL of 0.")
return
}
skill = duplicate(skill)
Hero6Utility.updateSkill(skill)
let abilityKey = skill.system.ability
let rollData = this.getCommonRollData(abilityKey)
rollData.mode = "skill"
rollData.skill = skill
rollData.img = skill.img
if (rollData.target) {
ui.notifications.warn("You are targetting a token with a skill : please use a Weapon instead.")
return
}
this.startRoll(rollData)
}
}
2022-12-15 21:44:23 +01:00
/* -------------------------------------------- */
rollSkill(skillId) {
let skill = this.items.get(skillId)
if (skill) {
if (skill.system.islore && skill.system.level == 0) {
ui.notifications.warn("You can't use Lore Skills with a SL of 0.")
return
}
skill = duplicate(skill)
Hero6Utility.updateSkill(skill)
let abilityKey = skill.system.ability
let rollData = this.getCommonRollData(abilityKey)
rollData.mode = "skill"
rollData.skill = skill
rollData.img = skill.img
if (rollData.target) {
ui.notifications.warn("You are targetting a token with a skill : please use a Weapon instead.")
return
}
this.startRoll(rollData)
}
}
/* -------------------------------------------- */
rollWeapon(weaponId) {
let weapon = this.items.get(weaponId)
if (weapon) {
weapon = duplicate(weapon)
let skill = this.items.find(item => item.name.toLowerCase() == weapon.system.skill.toLowerCase())
if (skill) {
skill = duplicate(skill)
Hero6Utility.updateSkill(skill)
let abilityKey = skill.system.ability
let rollData = this.getCommonRollData(abilityKey)
rollData.mode = "weapon"
rollData.skill = skill
rollData.weapon = weapon
rollData.img = weapon.img
if (!rollData.forceDisadvantage) { // This is an attack, check if disadvantaged
rollData.forceDisadvantage = this.isAttackDisadvantage()
}
/*if (rollData.weapon.system.isranged && rollData.tokensDistance > Hero6Utility.getWeaponMaxRange(rollData.weapon) ) {
ui.notifications.warn(`Your target is out of range of your weapon (max: ${Hero6Utility.getWeaponMaxRange(rollData.weapon)} - current : ${rollData.tokensDistance})` )
return
}*/
this.startRoll(rollData)
} else {
ui.notifications.warn("Unable to find the relevant skill for weapon " + weapon.name)
}
}
}
/* -------------------------------------------- */
async startRoll(rollData) {
let rollDialog = await Hero6RollDialog.create(this, rollData)
rollDialog.render(true)
}
}