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

203 lines
7.8 KiB
JavaScript
Raw Permalink Normal View History

2021-02-03 15:33:07 +01:00
import { SoSCardDeck } from "./sos-card-deck.js";
import { SoSUtility } from "./sos-utility.js";
import { SoSFlipDialog } from "./sos-flip-dialog.js";
import { SoSDialogCombatActions } from "./sos-dialog-combat-actions.js";
/* -------------------------------------------- */
export class SoSCombat extends Combat {
2022-07-17 20:53:25 +02:00
2021-02-03 15:33:07 +01:00
/* -------------------------------------------- */
2021-02-04 22:55:57 +01:00
requestActions() {
2022-07-17 20:53:25 +02:00
if (game.user.isGM && !this.actionsRequested) {
console.log("REQUEST ACTIONS !!!")
2021-02-04 22:55:57 +01:00
this.actionsRequested = true;
this.phaseSetup = {}; // Reset each new round/update
2022-07-17 20:53:25 +02:00
for (let combatant of this.combatants) {
this.setInitiative(combatant.id, -1); // Reset init
2024-06-07 11:26:46 +02:00
let uniq = foundry.utils.randomID(16)
2022-07-13 08:11:00 +02:00
const name = combatant.actor ? combatant.actor.name : combatant.name;
2023-05-25 16:22:02 +02:00
if (combatant.players && combatant.players[0] ) {
2021-02-04 08:38:59 +01:00
// A player controls this combatant -> message !
2022-07-17 20:53:25 +02:00
ChatMessage.create({
content: `New round ! Click on the button below to declare the actions of ${name} for round ${this.round} !<br>
<a class='chat-card-button' id='button-declare-actions' data-uniq-id='${uniq}' data-combatant-id='${combatant.id}' data-combat-id='${this.id}' data-round='${this.round}'>Declare actions</a>`,
whisper: [combatant.players[0].id]
});
2021-02-04 08:38:59 +01:00
} else {
2022-07-17 20:53:25 +02:00
ChatMessage.create({
content: `New round ! Click on the button below to declare the actions of ${name} for round ${this.round} !<br>
<a class='chat-card-button' id='button-declare-actions' data-uniq-id='${uniq}' data-combatant-id='${combatant.id}' data-combat-id='${this.id}' data-round='${this.round}'>Declare actions</a>`,
whisper: ChatMessage.getWhisperRecipients("GM"),
});
2021-02-04 08:38:59 +01:00
}
}
2021-02-03 15:33:07 +01:00
}
2021-02-04 22:55:57 +01:00
}
/* -------------------------------------------- */
async nextRound() {
this.actionsRequested = false;
2021-02-03 15:33:07 +01:00
super.nextRound();
}
2021-02-04 22:55:57 +01:00
/* -------------------------------------------- */
gotoNextTurn() {
this.phaseNumber -= 1;
2022-07-17 20:53:25 +02:00
if (this.phaseNumber <= 0) {
2021-02-15 23:21:53 +01:00
this.applyConsequences();
2021-02-04 22:55:57 +01:00
this.nextRound(); // Auto-switch to next round
2022-07-17 20:53:25 +02:00
} else {
2021-02-04 22:55:57 +01:00
this.nextTurn();
}
}
/* -------------------------------------------- */
async nextTurn() {
2022-07-17 20:53:25 +02:00
console.log("Going to phase !", this.phaseNumber);
2021-02-04 22:55:57 +01:00
// Get all actions for this phase
let phaseIndex = this.phaseNumber - 1;
let actionList = [];
let actionMsg = `<h4>Actions for phase ${this.phaseNumber}</h4>`;
2022-07-17 20:53:25 +02:00
for (let combatantId in this.phaseSetup) {
2021-02-04 22:55:57 +01:00
let actionData = this.phaseSetup[combatantId];
2022-07-17 20:53:25 +02:00
if (actionData.phaseArray[phaseIndex].name != 'No Action') {
let combatant = this.combatants.find(comb => comb._id == actionData.combatantId);
const name = combatant.actor ? combatant.actor.name : combatant.name;
actionList.push({
combatant: combatant,
action: actionData.phaseArray[phaseIndex],
isDone: false
});
actionMsg += `<br>${name} is going to : ${actionData.phaseArray[phaseIndex].name}`;
2021-02-04 22:55:57 +01:00
}
}
2022-07-17 20:53:25 +02:00
if (actionList.length == 0) {
2021-02-04 22:55:57 +01:00
actionMsg += "<br>No actions for the phase !";
this.gotoNextTurn();
}
// Display a nice message
2022-07-17 20:53:25 +02:00
ChatMessage.create({ content: actionMsg });
2021-02-04 22:55:57 +01:00
// Now push specific messages
2022-07-17 20:53:25 +02:00
for (let action of actionList) {
2024-06-07 11:26:46 +02:00
let uniq = foundry.utils.randomID(16);
2021-02-04 22:55:57 +01:00
action.uniqId = uniq; // Easy tracking with chat messages
2022-07-17 20:53:25 +02:00
const name = action.combatant.actor ? action.combatant.actor.name : action.combatant.name;
if (action.combatant.players[0]) {
2021-02-04 22:55:57 +01:00
// A player controls this combatant -> message !
2022-07-17 20:53:25 +02:00
ChatMessage.create({
content: `Phase ${this.phaseNumber} ! ${name} must perform a <strong>${action.action.name}</strong> action.
2021-02-04 22:55:57 +01:00
When done, click on the button below to close the action.
2022-07-17 20:53:25 +02:00
<a class='chat-card-button' id='button-end-action' data-uniq-id='${uniq}' data-combatant-id='${action.combatant.id}' data-combat-id='${this.id}' data-round='${this.round}'>Action is done !</a>`,
whisper: [action.combatant.players[0].id]
});
2021-02-04 22:55:57 +01:00
} else {
2022-07-17 20:53:25 +02:00
ChatMessage.create({
content: `Phase ${this.phaseNumber} ! ${name} must perform a <strong>${action.action.name}</strong> action.<br>
2021-02-04 22:55:57 +01:00
When done, click on the button below to close the action.
2022-07-17 20:53:25 +02:00
<a class='chat-card-button' id='button-end-action' data-uniq-id='${uniq}' data-combatant-id='${action.combatant.id}' data-combat-id='${this.id}' data-round='${this.round}'>Action is done !</a>`,
whisper: ChatMessage.getWhisperRecipients("GM")
});
2021-02-04 22:55:57 +01:00
}
}
// Save for easy access
this.currentActions = actionList;
}
2021-02-15 23:21:53 +01:00
/* -------------------------------------------- */
2022-07-17 20:53:25 +02:00
applyConsequences() {
if (game.user.isGM) {
for (let combatant of this.combatants) {
if (!combatant.actor) continue; // Can't check tokens without assigned actors, Maybe print chat message about bleeding happening so that the GM can manually track this?
2022-07-17 20:53:25 +02:00
let bleeding = combatant.actor.items.find(item => item.type == 'consequence' && item.name == 'Bleeding');
combatant.actor.applyConsequenceWound(bleeding.system.severity, "bleeding");
2021-02-15 23:21:53 +01:00
}
}
}
2021-02-04 22:55:57 +01:00
/* -------------------------------------------- */
2022-07-17 20:53:25 +02:00
closeAction(uniqId) {
2021-02-17 22:38:12 +01:00
// Delete message !
2022-07-17 20:53:25 +02:00
const toDelete = game.messages.filter(it => it.content.includes(uniqId));
toDelete.forEach(it => it.delete())
2021-02-17 22:38:12 +01:00
2022-07-17 20:53:25 +02:00
let action = this.currentActions.find(_action => _action.uniqId == uniqId);
2021-02-04 22:55:57 +01:00
if (action) {
action.isDone = true;
2022-07-17 20:53:25 +02:00
let filtered = this.currentActions.filter(_action => action.isDone);
if (filtered.length == this.currentActions.length) { // All actions closed !
2021-02-04 22:55:57 +01:00
console.log("Going next turn !!!");
this.gotoNextTurn();
}
}
}
2021-02-04 08:38:59 +01:00
/* -------------------------------------------- */
2022-07-17 20:53:25 +02:00
getPhaseRank(actionConf) {
for (let i = 2; i >= 0; i--) {
2021-02-04 08:38:59 +01:00
let action = actionConf.phaseArray[i];
if (action.name != "No Action") {
2022-07-17 20:53:25 +02:00
return i + 1;
2021-02-04 08:38:59 +01:00
}
}
return 0;
}
2021-02-11 00:07:13 +01:00
/* -------------------------------------------- */
2022-07-17 20:53:25 +02:00
getAPFromActor(actorId) {
for (let combatant of this.combatants) {
2021-02-11 00:07:13 +01:00
//console.log(combatant);
2022-07-17 20:53:25 +02:00
if (combatant.actor.id == actorId) {
let phase = this.phaseSetup[combatant.id];
2021-02-11 00:07:13 +01:00
return phase.remainingAP;
}
}
return 0;
}
2021-02-16 22:04:59 +01:00
/* -------------------------------------------- */
2022-07-17 20:53:25 +02:00
decreaseAPFromActor(actorId) {
for (let combatant of this.combatants) {
2021-02-16 22:04:59 +01:00
//console.log(combatant);
2022-07-17 20:53:25 +02:00
if (combatant.actor.id == actorId) {
let phase = this.phaseSetup[combatant.id];
2021-02-16 22:04:59 +01:00
phase.remainingAP -= 1;
2022-07-17 20:53:25 +02:00
if (phase.remainingAP < 0) phase.remainingAP = 0;
2021-02-16 22:04:59 +01:00
}
}
}
2022-07-17 20:53:25 +02:00
2021-02-03 15:33:07 +01:00
/* -------------------------------------------- */
2021-02-04 22:55:57 +01:00
async setupActorActions(actionConf) {
2021-02-17 22:38:12 +01:00
console.log("Setting combat for phase : ", actionConf, actionConf.uniqId);
// Delete message !
2022-07-17 20:53:25 +02:00
const toDelete = game.messages.filter(it => it.content.includes(actionConf.uniqId));
2021-02-17 22:38:12 +01:00
toDelete.forEach(it => it.delete());
2021-02-04 08:38:59 +01:00
2022-07-17 20:53:25 +02:00
if (!this.phaseSetup) this.phaseSetup = {}; // Opportunistic init
2021-02-03 15:33:07 +01:00
// Keep track
2021-02-04 22:55:57 +01:00
this.phaseSetup[actionConf.combatantId] = actionConf;
2022-07-17 20:53:25 +02:00
console.log(this.combatants);
2021-02-04 22:55:57 +01:00
//let combatant = this.combatants.find( comb => comb._id == actionConf.combatantId);
2022-07-17 20:53:25 +02:00
await this.setInitiative(actionConf.combatantId, this.getPhaseRank(actionConf));
2021-02-04 08:38:59 +01:00
let actionsDone = true
2022-07-17 20:53:25 +02:00
for (let combatant of this.combatants) {
if (combatant.initiative == -1) actionsDone = false;
2021-02-04 08:38:59 +01:00
}
2022-07-17 20:53:25 +02:00
if (actionsDone) {
2021-02-04 22:55:57 +01:00
this.actionsRequested = false;
2022-07-17 20:53:25 +02:00
ChatMessage.create({
content: `Action declaration has been completed ! Now proceeding with actions.`,
whisper: ChatMessage.getWhisperRecipients("GM")
})
2021-02-04 22:55:57 +01:00
this.phaseNumber = 3;
this.nextTurn();
2021-02-04 08:38:59 +01:00
}
2021-02-03 15:33:07 +01:00
}
}