tickwerk/src/systems/ds4.ts

32 lines
1.1 KiB
TypeScript
Raw Normal View History

// SPDX-FileCopyrightText: 2022 Johannes Loher
//
// SPDX-License-Identifier: MIT
2022-05-16 02:08:27 +02:00
import { packageId } from '../constants';
import type { TickwerkCombatant } from '../data/documents/combatant';
export const registerDS4SpecificFunctionality = () => {
if (CONFIG.tickwerk === undefined) CONFIG.tickwerk = {};
2022-05-16 03:46:53 +02:00
foundry.utils.mergeObject(CONFIG.tickwerk, { getTiebreaker, getInitiativeFormula });
2022-05-16 02:08:27 +02:00
};
2022-05-16 03:46:53 +02:00
const getTiebreaker = async (combatant: TickwerkCombatant, combatants: TickwerkCombatant[]): Promise<number> => {
2022-05-16 02:08:27 +02:00
if (combatants.length === 0) return 0;
2022-05-16 03:46:53 +02:00
const tiebreakers = combatants.map((combatant) => {
2022-05-16 02:08:27 +02:00
return (
2022-05-16 03:46:53 +02:00
(combatant._newTiebreaker !== undefined
? combatant._newTiebreaker
: combatant.getFlag(packageId, 'tiebreaker')) ?? 0
2022-05-16 02:08:27 +02:00
);
});
2022-05-16 03:46:53 +02:00
return Math.max(...tiebreakers) + 1;
2022-05-16 02:08:27 +02:00
};
const getInitiativeFormula = (combatant: TickwerkCombatant) => {
const started = combatant.combat?.started ?? false;
if (!started) return '-@combatValues.initiative.total';
const tickValue = combatant.combat?.round ?? 0;
return `max(${tickValue} + 10 - @combatValues.initiative.total, ${tickValue})`;
};