2021-03-13 17:43:48 +01:00
|
|
|
export default function evaluateCheck(
|
|
|
|
dice: number[],
|
|
|
|
checkTargetNumber: number,
|
|
|
|
{
|
|
|
|
maximumCoupResult = 1,
|
|
|
|
minimumFumbleResult = 20,
|
|
|
|
canFumble = true,
|
|
|
|
}: { maximumCoupResult?: number; minimumFumbleResult?: number; canFumble?: boolean } = {},
|
2021-03-13 18:53:26 +01:00
|
|
|
): SubCheckResult[] {
|
2021-03-13 17:43:48 +01:00
|
|
|
const diceWithSubChecks = assignSubChecksToDice(dice, checkTargetNumber, {
|
|
|
|
maximumCoupResult: maximumCoupResult,
|
|
|
|
});
|
|
|
|
return evaluateDiceWithSubChecks(diceWithSubChecks, {
|
|
|
|
maximumCoupResult: maximumCoupResult,
|
|
|
|
minimumFumbleResult: minimumFumbleResult,
|
|
|
|
canFumble: canFumble,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-13 18:43:23 +01:00
|
|
|
interface DieWithSubCheck {
|
|
|
|
result: number;
|
|
|
|
checkTargetNumber: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
function assignSubChecksToDice(
|
2021-03-13 02:27:09 +01:00
|
|
|
dice: number[],
|
|
|
|
checkTargetNumber: number,
|
|
|
|
{
|
|
|
|
maximumCoupResult = 1,
|
|
|
|
}: {
|
|
|
|
maximumCoupResult?: number;
|
|
|
|
} = {},
|
2021-03-13 17:43:48 +01:00
|
|
|
): DieWithSubCheck[] {
|
|
|
|
const requiredNumberOfDice = getRequiredNumberOfDice(checkTargetNumber);
|
|
|
|
|
2021-03-13 02:27:09 +01:00
|
|
|
if (dice.length !== requiredNumberOfDice || requiredNumberOfDice < 1) {
|
|
|
|
throw new Error(game.i18n.localize("DS4.ErrorInvalidNumberOfDice")); // TODO: add i18n
|
|
|
|
}
|
|
|
|
|
|
|
|
const checkTargetNumberForLastSubCheck = checkTargetNumber - 20 * (requiredNumberOfDice - 1);
|
|
|
|
|
|
|
|
const indexOfSmallestNonCoup = findIndexOfSmallestNonCoup(dice, maximumCoupResult);
|
|
|
|
const indexOfFirstCoup = dice.findIndex((die) => die <= maximumCoupResult);
|
|
|
|
const indexForLastSubCheck = shouldUseCoupForLastSubCheck(
|
|
|
|
indexOfSmallestNonCoup,
|
|
|
|
indexOfFirstCoup,
|
|
|
|
dice,
|
|
|
|
checkTargetNumberForLastSubCheck,
|
|
|
|
)
|
|
|
|
? indexOfFirstCoup
|
|
|
|
: indexOfSmallestNonCoup;
|
|
|
|
|
|
|
|
return dice.map((die, index) => ({
|
|
|
|
result: die,
|
|
|
|
checkTargetNumber: index === indexForLastSubCheck ? checkTargetNumberForLastSubCheck : 20,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
function findIndexOfSmallestNonCoup(dice: number[], maximumCoupResult: number): number {
|
|
|
|
return dice
|
|
|
|
.map((die, index) => [die, index])
|
|
|
|
.filter((indexedDie) => indexedDie[0] > maximumCoupResult)
|
|
|
|
.reduce(
|
|
|
|
(smallestIndexedDie, indexedDie) =>
|
|
|
|
indexedDie[0] < smallestIndexedDie[0] ? indexedDie : smallestIndexedDie,
|
|
|
|
[Infinity, -1],
|
|
|
|
)[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
function shouldUseCoupForLastSubCheck(
|
|
|
|
indexOfSmallestNonCoup: number,
|
|
|
|
indexOfFirstCoup: number,
|
|
|
|
dice: number[],
|
|
|
|
checkTargetNumberForLastSubCheck: number,
|
|
|
|
) {
|
|
|
|
return (
|
|
|
|
indexOfFirstCoup !== -1 &&
|
|
|
|
(indexOfSmallestNonCoup === -1 ||
|
|
|
|
(dice[indexOfSmallestNonCoup] > checkTargetNumberForLastSubCheck &&
|
|
|
|
dice[indexOfSmallestNonCoup] + checkTargetNumberForLastSubCheck > 20))
|
|
|
|
);
|
|
|
|
}
|
2021-03-13 17:43:48 +01:00
|
|
|
|
2021-03-13 18:53:26 +01:00
|
|
|
interface SubCheckResult extends DieWithSubCheck, DiceTerm.Result {
|
2021-03-13 17:43:48 +01:00
|
|
|
success?: boolean;
|
|
|
|
failure?: boolean;
|
|
|
|
count?: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
function evaluateDiceWithSubChecks(
|
|
|
|
results: DieWithSubCheck[],
|
|
|
|
{
|
|
|
|
maximumCoupResult,
|
|
|
|
minimumFumbleResult,
|
|
|
|
canFumble,
|
|
|
|
}: { maximumCoupResult: number; minimumFumbleResult: number; canFumble: boolean },
|
2021-03-13 18:53:26 +01:00
|
|
|
): SubCheckResult[] {
|
2021-03-13 17:43:48 +01:00
|
|
|
return results.map((dieWithSubCheck, index) => {
|
2021-03-13 18:53:26 +01:00
|
|
|
const result: SubCheckResult = {
|
2021-03-13 17:43:48 +01:00
|
|
|
...dieWithSubCheck,
|
|
|
|
active: dieWithSubCheck.result <= dieWithSubCheck.checkTargetNumber,
|
|
|
|
discarded: dieWithSubCheck.result > dieWithSubCheck.checkTargetNumber,
|
|
|
|
};
|
|
|
|
if (result.result <= maximumCoupResult) {
|
|
|
|
result.success = true;
|
|
|
|
result.count = result.checkTargetNumber;
|
2021-03-13 20:58:59 +01:00
|
|
|
result.active = true;
|
|
|
|
result.discarded = false;
|
2021-03-13 17:43:48 +01:00
|
|
|
}
|
|
|
|
if (index === 0 && canFumble && result.result >= minimumFumbleResult) result.failure = true;
|
|
|
|
return result;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getRequiredNumberOfDice(checkTargetNumber: number): number {
|
|
|
|
return Math.ceil(checkTargetNumber / 20);
|
|
|
|
}
|