foundryvtt-shadows-over-sol/module/sos-utility.js

404 lines
15 KiB
JavaScript
Raw Normal View History

2024-06-07 11:26:46 +02:00
/* -------------------------------------------- */
2021-02-11 00:07:13 +01:00
import { SoSCombat } from "./sos-combat.js";
2021-02-03 15:33:07 +01:00
import { SoSDialogCombatActions } from "./sos-dialog-combat-actions.js";
2021-01-17 22:09:01 +01:00
2024-06-07 11:26:46 +02:00
/* -------------------------------------------- */
const severity2malus = { "none": 0, "light": -1, "moderate": -2, "severe": -3, "critical": -4 };
/* -------------------------------------------- */
const severity2bonus = { "none": 0, "light": 1, "moderate": 2, "severe": 3, "critical": 4 };
2021-02-09 23:32:55 +01:00
2024-06-07 11:26:46 +02:00
/* -------------------------------------------- */
export class SoSUtility {
/* -------------------------------------------- */
2021-01-17 22:09:01 +01:00
static async preloadHandlebarsTemplates() {
2024-06-07 11:26:46 +02:00
2021-01-17 22:09:01 +01:00
const templatePaths = [
2021-01-18 17:46:39 +01:00
'systems/foundryvtt-shadows-over-sol/templates/actor-sheet.html',
'systems/foundryvtt-shadows-over-sol/templates/editor-notes-gm.html',
'systems/foundryvtt-shadows-over-sol/templates/stat-option-list.html',
2021-01-21 17:16:01 +01:00
'systems/foundryvtt-shadows-over-sol/templates/stat-name-list.html',
2021-01-19 22:54:53 +01:00
'systems/foundryvtt-shadows-over-sol/templates/item-sheet.html',
2021-01-21 17:16:01 +01:00
'systems/foundryvtt-shadows-over-sol/templates/item-geneline-sheet.html',
'systems/foundryvtt-shadows-over-sol/templates/item-subculture-sheet.html',
2021-02-09 23:32:55 +01:00
'systems/foundryvtt-shadows-over-sol/templates/item-weapon-sheet.html',
'systems/foundryvtt-shadows-over-sol/templates/item-commongear-sheet.html',
2021-01-21 17:16:01 +01:00
'systems/foundryvtt-shadows-over-sol/templates/dialog-flip.html'
2021-01-17 22:09:01 +01:00
]
2024-06-07 11:26:46 +02:00
return loadTemplates(templatePaths);
2021-01-21 17:16:01 +01:00
}
2021-01-31 17:23:14 +01:00
/* -------------------------------------------- */
2024-06-07 11:26:46 +02:00
static ready() {
Handlebars.registerHelper('select', function (selected, options) {
const escapedValue = RegExp.escape(Handlebars.escapeExpression(selected));
const rgx = new RegExp(' value=[\"\']' + escapedValue + '[\"\']');
const html = options.fn(this);
return html.replace(rgx, "$& selected");
});
}
/* -------------------------------------------- */
static fillRange(start, end) {
2021-01-21 17:16:01 +01:00
return Array(end - start + 1).fill().map((item, index) => start + index);
2021-01-17 22:09:01 +01:00
}
2024-06-07 11:26:46 +02:00
2021-02-03 15:33:07 +01:00
/* -------------------------------------------- */
2024-06-07 11:26:46 +02:00
static onSocketMesssage(msg) {
if (!game.user.isGM) return; // Only GM
2021-02-16 22:04:59 +01:00
2024-06-07 11:26:46 +02:00
if (msg.name == 'msg_declare_actions') {
let combat = game.combats.get(msg.data.combatId); // Get the associated combat
combat.setupActorActions(msg.data);
2021-02-04 22:55:57 +01:00
} else if (msg.name == 'msg_close_action') {
2024-06-07 11:26:46 +02:00
game.combat.closeAction(msg.data.uniqId);
2021-02-15 23:21:53 +01:00
} else if (msg.name == 'msg_request_defense') {
2024-06-07 11:26:46 +02:00
SoSUtility.applyDamage(msg.data);
2021-02-16 22:04:59 +01:00
} else if (msg.name == 'msg_reaction_cover') {
2024-06-07 11:26:46 +02:00
SoSUtility.reactionCover(msg.data.uniqId);
2021-02-16 22:04:59 +01:00
} else if (msg.name == 'msg_reaction_melee') {
2024-06-07 11:26:46 +02:00
SoSUtility.reactionMelee(msg.data.uniqId);
2021-02-16 22:04:59 +01:00
} else if (msg.name == 'msg_reaction_hit') {
2024-06-07 11:26:46 +02:00
SoSUtility.reactionHit(msg.data.uniqId);
2021-02-03 15:33:07 +01:00
}
}
2021-05-22 23:20:23 +02:00
/* -------------------------------------------- */
static async loadCompendiumData(compendium) {
const pack = game.packs.get(compendium);
return await pack?.getDocuments() ?? [];
}
2024-06-07 11:26:46 +02:00
2021-05-22 23:20:23 +02:00
/* -------------------------------------------- */
static async loadCompendium(compendium, filter = item => true) {
let compendiumData = await SoSUtility.loadCompendiumData(compendium);
return compendiumData.filter(filter);
}
2024-06-07 11:26:46 +02:00
2021-01-31 17:23:14 +01:00
/* -------------------------------------------- */
static async loadCompendiumNames(compendium) {
const pack = game.packs.get(compendium);
let competences;
await pack.getIndex().then(index => competences = index);
return competences;
}
2021-02-04 22:55:57 +01:00
/* -------------------------------------------- */
static updateCombat(combat, round, diff, id) {
combat.requestActions();
}
2021-02-03 15:33:07 +01:00
/* -------------------------------------------- */
2024-06-07 11:26:46 +02:00
static async openDeclareActions(event) {
2021-02-03 15:33:07 +01:00
event.preventDefault();
let round = event.currentTarget.attributes['data-round'].value;
2021-02-04 08:38:59 +01:00
let combatantId = event.currentTarget.attributes['data-combatant-id'].value;
2021-02-03 15:33:07 +01:00
let combatId = event.currentTarget.attributes['data-combat-id'].value;
2021-02-04 08:38:59 +01:00
let uniqId = event.currentTarget.attributes['data-uniq-id'].value;
2024-06-07 11:26:46 +02:00
let d = await SoSDialogCombatActions.create(combatId, combatantId, round, uniqId);
d.render(true);
2021-02-03 15:33:07 +01:00
}
2021-02-09 23:32:55 +01:00
/* -------------------------------------------- */
static getConsequenceMalus(severity) {
return severity2malus[severity] ?? 0;
}
2021-02-16 23:01:42 +01:00
/* -------------------------------------------- */
static getConsequenceBonus(severity) {
return severity2bonus[severity] ?? 0;
}
2024-06-07 11:26:46 +02:00
2021-02-09 23:32:55 +01:00
/* -------------------------------------------- */
2024-06-07 11:26:46 +02:00
static computeEncumbrance(items) {
let trappings = items.filter(item => item.type == 'gear' || item.type == 'armor' || item.type == 'weapon');
2021-02-09 23:32:55 +01:00
let sumEnc = 0;
for (let object of trappings) {
2024-06-07 11:26:46 +02:00
if ((!object.system.worn) && (!object.system.neg) && (!object.system.software) && (!object.system.implant) && (!object.system.containerid || object.system.containerid == "")) {
2021-02-09 23:32:55 +01:00
sumEnc += (object.big > 0) ? object.big : 1;
}
}
return sumEnc;
}
2021-02-04 22:55:57 +01:00
/* -------------------------------------------- */
static closeAction(event) {
let uniqId = event.currentTarget.attributes['data-uniq-id'].value;
2021-02-17 22:38:12 +01:00
2024-06-07 11:26:46 +02:00
if (game.user.isGM) {
game.combat.closeAction(uniqId);
2021-02-04 22:55:57 +01:00
} else {
2021-02-16 22:04:59 +01:00
game.socket.emit("system.foundryvtt-shadows-over-sol", {
2024-06-07 11:26:46 +02:00
name: "msg_close_action", data: { uniqId: uniqId }
});
2021-02-04 22:55:57 +01:00
}
}
2021-02-03 15:33:07 +01:00
/* -------------------------------------------- */
static async registerChatCallbacks(html) {
html.on("click", '#button-declare-actions', event => {
2024-06-07 11:26:46 +02:00
SoSUtility.openDeclareActions(event);
2021-02-03 15:33:07 +01:00
});
2021-02-04 22:55:57 +01:00
html.on("click", '#button-end-action', event => {
2024-06-07 11:26:46 +02:00
SoSUtility.closeAction(event);
});
2021-02-16 22:04:59 +01:00
html.on("click", '#button-reaction-cover', event => {
let uniqId = event.currentTarget.attributes['data-uniq-id'].value;
2024-06-07 11:26:46 +02:00
if (game.user.isGM) {
SoSUtility.reactionCover(uniqId);
2021-02-16 22:04:59 +01:00
} else {
2024-06-07 11:26:46 +02:00
game.socket.emit("system.foundryvtt-shadows-over-sol", { name: "msg_reaction_cover", data: { uniqId: uniqId } });
2021-02-16 22:04:59 +01:00
}
2024-06-07 11:26:46 +02:00
});
2021-02-16 22:04:59 +01:00
html.on("click", '#button-reaction-melee', event => {
let uniqId = event.currentTarget.attributes['data-uniq-id'].value;
2024-06-07 11:26:46 +02:00
if (game.user.isGM) {
SoSUtility.reactionMelee(uniqId);
2021-02-16 22:04:59 +01:00
} else {
2024-06-07 11:26:46 +02:00
game.socket.emit("system.foundryvtt-shadows-over-sol", { name: "msg_reaction_melee", data: { uniqId: uniqId } });
2021-02-16 22:04:59 +01:00
}
2024-06-07 11:26:46 +02:00
});
2021-02-16 22:04:59 +01:00
html.on("click", '#button-reaction-hit', event => {
let uniqId = event.currentTarget.attributes['data-uniq-id'].value;
2024-06-07 11:26:46 +02:00
if (game.user.isGM) {
SoSUtility.reactionHit(uniqId);
2021-02-16 22:04:59 +01:00
} else {
2024-06-07 11:26:46 +02:00
game.socket.emit("system.foundryvtt-shadows-over-sol", { name: "msg_reaction_hit", data: { uniqId: uniqId } });
2021-02-16 22:04:59 +01:00
}
2024-06-07 11:26:46 +02:00
});
2021-02-03 15:33:07 +01:00
}
2021-02-11 00:07:13 +01:00
/* -------------------------------------------- */
static getTarget() {
if (game.user.targets && game.user.targets.size == 1) {
for (let target of game.user.targets) {
return target;
}
}
return undefined;
}
2021-02-16 22:04:59 +01:00
/* -------------------------------------------- */
2024-06-07 11:26:46 +02:00
static increaseConsequenceSeverity(severity) {
if (severity == 'none') return 'light';
if (severity == 'light') return 'moderate';
if (severity == 'moderate') return 'severe';
if (severity == 'severe') return 'critical';
2021-02-16 22:04:59 +01:00
return 'critical';
}
/* -------------------------------------------- */
2024-06-07 11:26:46 +02:00
static getConsequenceSeverityLevel(severity) {
if (severity == 'none') return 0;
if (severity == 'light') return 1;
if (severity == 'moderate') return 2;
if (severity == 'severe') return 3;
if (severity == 'critical') return 4;
2021-02-16 22:04:59 +01:00
return 0;
}
2024-06-07 11:26:46 +02:00
2021-02-11 00:07:13 +01:00
/* -------------------------------------------- */
2024-06-07 11:26:46 +02:00
static increaseSeverity(severity) {
if (severity == 'L') return 'M';
if (severity == 'M') return 'S';
if (severity == 'S') return 'C';
if (severity == 'C') return 'F';
2021-02-11 00:07:13 +01:00
}
/* -------------------------------------------- */
2024-06-07 11:26:46 +02:00
static decreaseSeverity(severity) {
if (severity == 'C') return 'S';
if (severity == 'S') return 'M';
if (severity == 'M') return 'L';
if (severity == 'L') return 'N';
2021-02-11 00:07:13 +01:00
}
/* -------------------------------------------- */
2024-06-07 11:26:46 +02:00
static getSeverityLevel(severity) {
if (severity == 'C') return 4;
if (severity == 'S') return 3;
if (severity == 'M') return 2;
if (severity == 'L') return 1;
2021-02-11 00:07:13 +01:00
return 0;
}
2021-02-22 20:32:12 +01:00
/* -------------------------------------------- */
static async confirmDelete(actorSheet, li) {
let itemId = li.data("item-id");
let objet = actorSheet.actor.items.find(item => item._id == itemId);
let msgTxt = "<p>Are you sure to delete this item ?";
let buttons = {
delete: {
2024-06-07 11:26:46 +02:00
icon: '<i class="fas fa-check"></i>',
label: "Yes, delete it",
callback: () => {
console.log("Delete : ", itemId);
actorSheet.actor.deleteEmbeddedDocuments("Item", [itemId]);
li.slideUp(200, () => actorSheet.render(false));
2021-02-22 20:32:12 +01:00
}
2024-06-07 11:26:46 +02:00
},
cancel: {
icon: '<i class="fas fa-times"></i>',
label: "Cancel"
2021-02-22 20:32:12 +01:00
}
}
2024-06-07 11:26:46 +02:00
msgTxt += "</p>";
let d = new Dialog({
title: "Confirm deletion",
content: msgTxt,
buttons: buttons,
default: "cancel"
});
d.render(true);
}
2021-02-11 00:07:13 +01:00
/* -------------------------------------------- */
2024-06-07 11:26:46 +02:00
static async applyDamage(flipData) {
2021-02-16 22:04:59 +01:00
if (!this.registry) this.registry = {};
2024-06-07 11:26:46 +02:00
if (flipData.isReaction) { // Check again resut in case of reaction !
2021-02-16 22:04:59 +01:00
flipData.magnitude = flipData.finalScore - flipData.tn; // Update magnitude
2024-06-07 11:26:46 +02:00
if (flipData.magnitude < 0) {
let html = await renderTemplate('systems/foundryvtt-shadows-over-sol/templates/chat-reaction-result.html', flipData);
ChatMessage.create({ content: html });
2021-02-16 22:04:59 +01:00
return;
}
}
2024-06-07 11:26:46 +02:00
2021-03-13 22:33:24 +01:00
// DR management
2024-06-07 11:26:46 +02:00
let armor = flipData.target.actor.system.items.find(item => item.type == 'armor' && item.system.worn);
2023-05-25 16:22:02 +02:00
flipData.armorDR = armor ? armor.system.dr : 0;
2024-06-07 11:26:46 +02:00
flipData.armorGel = armor ? armor.system.gel : 0;
2023-05-25 16:22:02 +02:00
flipData.armorReflect = armor ? armor.system.reflect : 0;
let dr = flipData.target.actor.system.scores.dr.value + flipData.armorDR;
if (flipData.weapon.system.category == 'ballistic') {
2024-06-07 11:26:46 +02:00
dr += flipData.armorGel;
2021-03-13 22:33:24 +01:00
}
2023-05-25 16:22:02 +02:00
if (flipData.weapon.system.category == 'laser') {
2024-06-07 11:26:46 +02:00
dr += flipData.armorReflect;
2021-03-13 22:33:24 +01:00
}
2021-02-16 22:04:59 +01:00
2023-05-25 16:22:02 +02:00
let shock = flipData.target.actor.system.scores.shock.value || 1;
let defenseCritical = flipData.target.actor.system.scores.defense.critical;
2021-02-11 00:07:13 +01:00
flipData.damageStatus = 'apply_damage';
flipData.targetShock = shock;
2024-06-07 11:26:46 +02:00
flipData.targetDR = dr;
2021-02-11 00:07:13 +01:00
flipData.targetCritical = defenseCritical;
// DR management
2024-06-07 11:26:46 +02:00
if (flipData.damageValue < dr) {
2021-02-11 00:07:13 +01:00
if (flipData.damageValue < dr / 2) {
flipData.damageStatus = "no_damage";
2021-03-13 22:33:24 +01:00
flipData.damageReason = "Damage are lesser than DR/2";
2021-02-11 00:07:13 +01:00
} else {
2024-06-07 11:26:46 +02:00
flipData.damageSeverity = this.decreaseSeverity(flipData.damageSeverity);
if (flipData.damageSeverity == 'N') {
2021-02-11 00:07:13 +01:00
flipData.damageStatus = "no_damage";
2021-03-13 22:33:24 +01:00
flipData.damageReason = "Severity decreased to nothing";
2024-06-07 11:26:46 +02:00
}
2021-02-11 00:07:13 +01:00
}
}
2024-06-07 11:26:46 +02:00
2021-02-11 00:07:13 +01:00
// Shock management
flipData.woundsList = [];
2021-03-13 22:33:24 +01:00
flipData.nbStun = 0;
2024-06-07 11:26:46 +02:00
if (flipData.weapon.stun) { // Stun weapon case
if (flipData.damageValue >= shock) {
2021-03-13 22:33:24 +01:00
flipData.nbStun = Math.floor(flipData.damageValue / shock);
}
} else {
2024-06-07 11:26:46 +02:00
if (flipData.damageValue >= shock) {
2021-03-13 22:33:24 +01:00
let incSeverity = Math.floor(flipData.damageValue / shock);
2024-06-07 11:26:46 +02:00
for (let i = 0; i < incSeverity; i++) {
if (flipData.damageSeverity == 'C') {
flipData.woundsList.push(flipData.damageSeverity);
2021-03-13 22:33:24 +01:00
flipData.damageSeverity = 'L';
} else {
flipData.nbStun++;
2024-06-07 11:26:46 +02:00
flipData.damageSeverity = this.increaseSeverity(flipData.damageSeverity);
2021-03-13 22:33:24 +01:00
flipData.damageReason = "Severity increased";
}
2021-02-11 00:07:13 +01:00
}
}
}
2024-06-07 11:26:46 +02:00
flipData.woundsList.push(flipData.damageSeverity);
2021-02-11 00:07:13 +01:00
flipData.nbWounds = flipData.woundsList.length;
// Critical management
2024-06-07 11:26:46 +02:00
flipData.isCritical = (flipData.cardTotal >= defenseCritical);
2021-02-11 00:07:13 +01:00
2024-06-07 11:26:46 +02:00
let html = await renderTemplate('systems/foundryvtt-shadows-over-sol/templates/chat-damage-target.html', flipData);
ChatMessage.create({ content: html });
2021-02-11 00:07:13 +01:00
// Is target able to dodge ??
2024-06-07 11:26:46 +02:00
let defender = game.actors.get(flipData.target.actor._id);
flipData.coverConsequence = defender.items.find(item => item.type == 'consequence' && item.name == 'Cover');
flipData.APavailable = game.combat.getAPFromActor(defender._id);
2021-02-11 00:07:13 +01:00
console.log("FLIPDATE : ", flipData);
2024-06-07 11:26:46 +02:00
if (!flipData.isReaction && flipData.APavailable > 0) {
if ((flipData.weapon.system.category == 'melee') || ((flipData.weapon.system.category == 'laser' || flipData.weapon.system.category == 'ballistic') &&
flipData.coverConsequence.system.severity != 'none')) {
flipData.coverSeverityLevel = this.getConsequenceSeverityLevel(flipData.coverConsequence.system.severity) * 2;
flipData.coverSeverityFlag = (flipData.coverSeverityLevel > 0);
flipData.isMelee = (flipData.weapon.system.category == 'melee');
let melee = defender.items.find(item => item.type == 'skill' && item.name == 'Melee');
2023-05-25 16:22:02 +02:00
flipData.defenderMelee = melee.system.value;
2024-06-07 11:26:46 +02:00
flipData.uniqId = foundry.utils.randomID(16);
2021-02-16 22:04:59 +01:00
this.registry[flipData.uniqId] = flipData;
2024-06-07 11:26:46 +02:00
let html = await renderTemplate('systems/foundryvtt-shadows-over-sol/templates/chat-damage-request-dodge.html', flipData);
ChatMessage.create({ content: html, whisper: ChatMessage.getWhisperRecipients(flipData.target.actor.name).concat(ChatMessage.getWhisperRecipients("GM")) });
2021-02-15 23:21:53 +01:00
return; // Wait message response
2021-02-11 00:07:13 +01:00
}
}
2021-02-16 22:04:59 +01:00
flipData.isReaction = false;
2024-06-07 11:26:46 +02:00
this.takeWounds(flipData);
2021-02-11 00:07:13 +01:00
}
2021-02-15 23:21:53 +01:00
2024-06-07 11:26:46 +02:00
/* -------------------------------------------- */
static reactionCover(uniqId) {
2021-02-16 22:04:59 +01:00
let flipData = this.registry[uniqId];
flipData.tn += flipData.coverSeverityLevel;
flipData.isReaction = true;
2024-06-07 11:26:46 +02:00
game.combat.decreaseAPFromActor(flipData.target.actor._id);
SoSUtility.applyDamage(flipData);
}
2021-02-16 22:04:59 +01:00
/* -------------------------------------------- */
2024-06-07 11:26:46 +02:00
static reactionMelee(uniqId) {
2021-02-16 22:04:59 +01:00
let flipData = this.registry[uniqId];
flipData.tn += flipData.defenderMelee;
flipData.isReaction = true;
2024-06-07 11:26:46 +02:00
game.combat.decreaseAPFromActor(flipData.target.actor._id);
SoSUtility.applyDamage(flipData);
2021-02-16 22:04:59 +01:00
}
/* -------------------------------------------- */
2024-06-07 11:26:46 +02:00
static reactionHit(uniqId) {
2021-02-16 22:04:59 +01:00
let flipData = this.registry[uniqId];
flipData.isReaction = true;
2024-06-07 11:26:46 +02:00
SoSUtility.takeWounds(flipData);
2021-02-16 22:04:59 +01:00
}
2024-06-07 11:26:46 +02:00
/* -------------------------------------------- */
static takeWounds(flipData) {
let defender = game.actors.get(flipData.target.actor._id);
defender.applyWounds(flipData);
2021-02-15 23:21:53 +01:00
}
2021-03-22 21:02:56 +01:00
/* -------------------------------------------- */
static async processItemDropEvent(actorSheet, event) {
let dragData = JSON.parse(event.dataTransfer.getData("text/plain"));
2022-07-13 08:11:00 +02:00
const item = fromUuidSync(dragData.uuid)
let dropId = $(event.target).parents(".item").attr("data-item-id"); // Only relevant if container drop
let objectId = item.id
console.log("ID", dragData, dropId, objectId)
if (dragData.type == 'Item' && dropId) {
2024-06-07 11:26:46 +02:00
actorSheet.actor.addObjectToContainer(objectId, dropId);
2021-03-22 21:02:56 +01:00
}
2022-07-13 08:11:00 +02:00
return true
2021-03-22 21:02:56 +01:00
}
2021-01-17 22:09:01 +01:00
}