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

363 lines
14 KiB
JavaScript
Raw Normal View History

2021-02-03 15:33:07 +01: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
2021-02-03 15:33:07 +01:00
/* -------------------------------------------- */
2021-02-09 23:32:55 +01:00
const severity2malus = { "none": 0, "light": -1, "moderate": -2, "severe": -3, "critical": -4};
2021-02-16 23:01:42 +01:00
/* -------------------------------------------- */
const severity2bonus = { "none": 0, "light": 1, "moderate": 2, "severe": 3, "critical": 4};
2021-02-09 23:32:55 +01:00
/* -------------------------------------------- */
2021-02-16 22:09:56 +01:00
export class SoSUtility extends Entity {
2021-01-18 16:11:27 +01:00
/* -------------------------------------------- */
2021-01-17 22:09:01 +01:00
static async preloadHandlebarsTemplates() {
2021-01-18 17:46:39 +01: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
]
2021-01-21 17:16:01 +01:00
return loadTemplates(templatePaths);
}
2021-01-31 17:23:14 +01:00
/* -------------------------------------------- */
2021-01-21 17:16:01 +01:00
static fillRange (start, end) {
return Array(end - start + 1).fill().map((item, index) => start + index);
2021-01-17 22:09:01 +01:00
}
2021-01-31 17:23:14 +01:00
2021-02-03 15:33:07 +01:00
/* -------------------------------------------- */
2021-02-17 22:38:12 +01:00
static onSocketMesssage( msg ) {
2021-02-16 22:04:59 +01:00
if( !game.user.isGM ) return; // Only GM
2021-02-03 15:33:07 +01:00
if (msg.name == 'msg_declare_actions' ) {
2021-02-16 22:04:59 +01:00
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') {
2021-02-16 22:04:59 +01:00
game.combat.closeAction( msg.data.uniqId );
2021-02-15 23:21:53 +01:00
} else if (msg.name == 'msg_request_defense') {
2021-02-16 22:04:59 +01:00
SoSUtility.applyDamage( msg.data );
} else if (msg.name == 'msg_reaction_cover') {
SoSUtility.reactionCover( msg.data.uniqId );
} else if (msg.name == 'msg_reaction_melee') {
SoSUtility.reactionMelee( msg.data.uniqId );
} else if (msg.name == 'msg_reaction_hit') {
SoSUtility.reactionHit( msg.data.uniqId );
2021-02-03 15:33:07 +01: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;
}
/* -------------------------------------------- */
static async loadCompendium(compendium, filter = item => true) {
let compendiumItems = await SoSUtility.loadCompendiumNames(compendium);
const pack = game.packs.get(compendium);
let list = [];
for (let compendiumItem of compendiumItems) {
await pack.getEntity(compendiumItem._id).then(it => {
const item = it.data;
if (filter(item)) {
list.push(item);
}
});
};
return list;
}
2021-02-04 22:55:57 +01:00
/* -------------------------------------------- */
static updateCombat(combat, round, diff, id) {
combat.requestActions();
}
2021-02-03 15:33:07 +01:00
/* -------------------------------------------- */
static async openDeclareActions( event) {
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;
let d = await SoSDialogCombatActions.create( combatId, combatantId, round, uniqId );
2021-02-03 15:33:07 +01:00
d.render(true);
}
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;
}
2021-02-09 23:32:55 +01:00
/* -------------------------------------------- */
static computeEncumbrance( items) {
let trappings = items.filter( item => item.type == 'gear' || item.type == 'armor' || item.type == 'weapon' );
let sumEnc = 0;
for (let object of trappings) {
if ( (!object.data.worn) && (!object.data.neg) && (!object.data.software) && (!object.data.implant) && (!object.data.containerid || object.data.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
2021-02-04 22:55:57 +01:00
if ( game.user.isGM ) {
game.combat.closeAction( uniqId );
} else {
2021-02-16 22:04:59 +01:00
game.socket.emit("system.foundryvtt-shadows-over-sol", {
2021-02-04 22:55:57 +01:00
name: "msg_close_action", data: { uniqId: uniqId} } );
}
}
2021-02-03 15:33:07 +01:00
/* -------------------------------------------- */
static async registerChatCallbacks(html) {
html.on("click", '#button-declare-actions', event => {
SoSUtility.openDeclareActions( event );
});
2021-02-04 22:55:57 +01:00
html.on("click", '#button-end-action', event => {
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;
if ( game.user.isGM ) {
SoSUtility.reactionCover( uniqId );
} else {
game.socket.emit("system.foundryvtt-shadows-over-sol", { name: "msg_reaction_cover", data: { uniqId: uniqId} } );
}
});
html.on("click", '#button-reaction-melee', event => {
let uniqId = event.currentTarget.attributes['data-uniq-id'].value;
if ( game.user.isGM ) {
SoSUtility.reactionMelee( uniqId );
} else {
game.socket.emit("system.foundryvtt-shadows-over-sol", { name: "msg_reaction_melee", data: { uniqId: uniqId} } );
}
});
html.on("click", '#button-reaction-hit', event => {
let uniqId = event.currentTarget.attributes['data-uniq-id'].value;
if ( game.user.isGM ) {
SoSUtility.reactionHit( uniqId );
} else {
game.socket.emit("system.foundryvtt-shadows-over-sol", { name: "msg_reaction_hit", data: { uniqId: uniqId} } );
}
});
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
/* -------------------------------------------- */
static increaseConsequenceSeverity( severity ) {
if ( severity == 'none') return 'light';
if ( severity == 'light') return 'moderate';
if ( severity == 'moderate') return 'severe';
if ( severity == 'severe') return 'critical';
return 'critical';
}
/* -------------------------------------------- */
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;
return 0;
}
2021-02-15 23:21:53 +01:00
2021-02-11 00:07:13 +01:00
/* -------------------------------------------- */
static increaseSeverity( severity ) {
if ( severity == 'L') return 'M';
if ( severity == 'M') return 'S';
if ( severity == 'S') return 'C';
if ( severity == 'C') return 'F';
}
/* -------------------------------------------- */
static decreaseSeverity( severity ) {
if ( severity == 'C') return 'S';
if ( severity == 'S') return 'M';
if ( severity == 'M') return 'L';
if ( severity == 'L') return 'N';
}
/* -------------------------------------------- */
static getSeverityLevel( severity) {
if ( severity == 'C') return 4;
if ( severity == 'S') return 3;
if ( severity == 'M') return 2;
if ( severity == 'L') return 1;
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: {
icon: '<i class="fas fa-check"></i>',
label: "Yes, delete it",
callback: () => {
console.log("Delete : ", itemId);
actorSheet.actor.deleteOwnedItem(itemId);
li.slideUp(200, () => actorSheet.render(false));
}
},
cancel: {
icon: '<i class="fas fa-times"></i>',
label: "Cancel"
}
}
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
/* -------------------------------------------- */
static async applyDamage( flipData ) {
2021-02-16 22:04:59 +01:00
if (!this.registry) this.registry = {};
2021-02-16 22:09:56 +01: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
if ( flipData.magnitude < 0 ) {
let html = await renderTemplate('systems/foundryvtt-shadows-over-sol/templates/chat-reaction-result.html', flipData );
ChatMessage.create( { content: html });
return;
}
}
2021-02-11 00:07:13 +01:00
let dr = flipData.target.actor.data.data.scores.dr.value;
let shock = flipData.target.actor.data.data.scores.shock.value || 1;
2021-02-11 00:07:13 +01:00
let defenseCritical = flipData.target.actor.data.data.scores.defense.critical;
flipData.damageStatus = 'apply_damage';
flipData.targetShock = shock;
flipData.targetDR = dr;
flipData.targetCritical = defenseCritical;
// DR management
if ( flipData.damageValue < dr) {
if (flipData.damageValue < dr / 2) {
flipData.damageStatus = "no_damage";
// TODO : No damage !
} else {
flipData.damageSeverity = this.decreaseSeverity(flipData.damageSeverity );
if ( flipData.damageSeverity == 'N') {
flipData.damageStatus = "no_damage";
}
}
}
// Shock management
flipData.woundsList = [];
if ( flipData.damageValue >= shock) {
let incSeverity = Math.floor(flipData.damageValue / shock);
for (let i=0; i<incSeverity; i++) {
if ( flipData.damageSeverity == 'C') {
flipData.woundsList.push( flipData.damageSeverity );
flipData.damageSeverity = 'L';
} else {
flipData.damageSeverity = this.increaseSeverity( flipData.damageSeverity );
}
}
}
flipData.woundsList.push( flipData.damageSeverity );
flipData.nbWounds = flipData.woundsList.length;
// Critical management
flipData.isCritical = ( flipData.cardTotal >= defenseCritical);
let html = await renderTemplate('systems/foundryvtt-shadows-over-sol/templates/chat-damage-target.html', flipData );
ChatMessage.create( { content: html });
// Is target able to dodge ??
let defender = game.actors.get( flipData.target.actor._id);
flipData.coverConsequence = defender.data.items.find( item => item.type == 'consequence' && item.name == 'Cover');
flipData.APavailable = game.combat.getAPFromActor( defender.data._id );
console.log("FLIPDATE : ", flipData);
2021-02-16 22:04:59 +01:00
if ( !flipData.isReaction && flipData.APavailable > 0) {
2021-02-11 00:07:13 +01:00
if ( (flipData.weapon.data.category == 'melee' ) || ( (flipData.weapon.data.category == 'laser' || flipData.weapon.data.category == 'ballistic') &&
flipData.coverConsequence.data.severity != 'none') ) {
2021-02-16 22:04:59 +01:00
flipData.coverSeverityLevel = this.getConsequenceSeverityLevel( flipData.coverConsequence.data.severity ) * 2;
2021-02-11 00:07:13 +01:00
flipData.coverSeverityFlag = (flipData.coverSeverityLevel > 0);
flipData.isMelee = (flipData.weapon.data.category == 'melee' );
let melee = defender.data.items.find( item => item.type == 'skill' && item.name == 'Melee');
flipData.defenderMelee = melee.data.value;
2021-02-16 22:04:59 +01:00
flipData.uniqId = randomID(16);
this.registry[flipData.uniqId] = flipData;
2021-02-11 00:07:13 +01:00
let html = await renderTemplate('systems/foundryvtt-shadows-over-sol/templates/chat-damage-request-dodge.html', flipData );
2021-02-17 22:38:12 +01:00
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;
2021-02-15 23:21:53 +01:00
this.takeWounds( flipData);
2021-02-11 00:07:13 +01:00
}
2021-02-15 23:21:53 +01:00
2021-02-16 22:04:59 +01:00
/* -------------------------------------------- */
static reactionCover( uniqId) {
let flipData = this.registry[uniqId];
flipData.tn += flipData.coverSeverityLevel;
flipData.isReaction = true;
game.combat.decreaseAPFromActor( flipData.target.actor._id );
SoSUtility.applyDamage( flipData);
}
/* -------------------------------------------- */
static reactionMelee( uniqId) {
let flipData = this.registry[uniqId];
flipData.tn += flipData.defenderMelee;
flipData.isReaction = true;
game.combat.decreaseAPFromActor( flipData.target.actor._id );
SoSUtility.applyDamage( flipData);
}
/* -------------------------------------------- */
static reactionHit( uniqId) {
let flipData = this.registry[uniqId];
flipData.isReaction = true;
SoSUtility.takeWounds( flipData);
}
2021-02-15 23:21:53 +01:00
/* -------------------------------------------- */
static takeWounds( flipData ) {
let defender = game.actors.get( flipData.target.actor._id);
defender.applyWounds( flipData );
}
2021-01-17 22:09:01 +01:00
}