Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | 1x 1x 1x 3x 69x 3x 50x 1x 3x 3x 3x 3x 89x 89x 89x 134x 134x 134x 52x 82x 82x 26x 56x 1x 2x 2x 80x 2x 54x 54x 2x 52x 94x 52x 46x 46x 6x 2x 10x 14x | import { cartesianProduct, Fraction, roll, WithOdds } from "./math";
import {
AttackDie,
AttackResult,
CampaignResult,
DefenseDie,
DefenseResult,
toAttackResult,
toDefenseResult,
} from "./oath";
import { replace } from "./utils";
/**
* Lists all possible results of an attack roll.
*
* @param {number} diceNumber
* Number of attack (red) dice to roll.
* @returns {WithOdds<AttackResult>[]}
* All possible results of rolling this number of attack dice.
*/
function rollAttack(diceNumber: number): WithOdds<AttackResult>[] {
return roll(
AttackDie,
diceNumber,
toAttackResult,
(a, b) => a.attack === b.attack && a.kill === b.kill
);
}
/**
* Lists all possible results of defense roll.
*
* @param {number} diceNumber
* Number of defense (blue) dice to roll.
* @returns {WithOdds<DefenseResult>[]}
* All possible results of rolling this number of defense dice.
*/
function rollDefense(diceNumber: number): WithOdds<DefenseResult>[] {
return roll(
DefenseDie,
diceNumber,
toDefenseResult,
(a, b) => a.defense === b.defense
);
}
/**
* Lists all possible results of a campaign in Oath along with their odds.
*
* @param {number} attackDice
* Number of attack (red) dice to roll.
* @param {number} defenseDice
* Number of defense (blue) dice to roll.
* @returns {CampaignResultWithOdds[]}
* All possible results for this campaign along with their odds.
*/
export function rollCampaign(
attackDice: number,
defenseDice: number
): WithOdds<CampaignResult>[] {
const attackRoll = rollAttack(attackDice);
const defenseRoll = rollDefense(defenseDice);
const product = cartesianProduct<any>(attackRoll, defenseRoll);
return product.map((pair) =>
merge(pair as [WithOdds<AttackResult>, WithOdds<DefenseResult>])
);
}
function merge(
attackDefensePair: [WithOdds<AttackResult>, WithOdds<DefenseResult>]
): WithOdds<CampaignResult> {
const [attack, defense] = attackDefensePair;
return {
oddsOfValue: attack.oddsOfValue.multiply(defense.oddsOfValue),
value: { ...attack.value, ...defense.value },
};
}
/**
* Type to hold information about a campaign success.
*/
export interface CampaignSuccess {
/** Remaining warbands in attacking force after warbands have been killed
* from the dice roll and sacrificed.
*/
remainingWarbands: number;
/** Odds of this particular campaign success. */
odds: Fraction;
}
/**
* Computes the remaining warbands in attacking force by applying the kills
* from the dice roll and sacrificing the number of warbands needed to succeed.
* If the result is negative, it means by this campaign can't be a success (not
* enough warbands to sacrifice).
*
* @param {CampaignResult} result
* The campaign result.
* @param {number} attackingForceWarbands
* The number of warbands in attacking force.
* @param {number} defendingForceWarbands
* The number of warbands in defending force.
* @returns {number | null}
* The remaining warbands or null if the campaign is a loss.
*/
function remainingWarbandsAfterSacrifice(
result: CampaignResult,
attackingForceWarbands: number,
defendingForceWarbands: number
): number | null {
const warbandsAfterKill = attackingForceWarbands - result.kill;
const attackVsDefense =
result.attack - result.defense - defendingForceWarbands;
if (attackVsDefense > 0) {
return warbandsAfterKill;
} else {
const warbandsToSacrifice = 1 - attackVsDefense;
if (warbandsToSacrifice > warbandsAfterKill) {
return null;
} else {
return warbandsAfterKill - warbandsToSacrifice;
}
}
}
export function computeCampaignSuccess(
attackDice: number,
attackingForceWarbands: number,
defenseDice: number,
defendingForceWarbands: number
): CampaignSuccess[] {
const possibleResults = rollCampaign(attackDice, defenseDice);
const successes = possibleResults.filter(
(result) =>
remainingWarbandsAfterSacrifice(
result.value,
attackingForceWarbands,
defendingForceWarbands
) !== null
);
const groupedByRemainingWarbands: CampaignSuccess[] = successes.reduce(
(acc: CampaignSuccess[], result: WithOdds<CampaignResult>) => {
const remainingWarbands = remainingWarbandsAfterSacrifice(
result.value,
attackingForceWarbands,
defendingForceWarbands
);
if (acc.length === 0) {
return [
{
odds: result.oddsOfValue,
remainingWarbands,
},
];
} else {
// Find an equivalent set
const matchingIndex = acc.findIndex(
(item) => item.remainingWarbands === remainingWarbands
);
if (matchingIndex > -1) {
// Update the existing set by adding the odds
const updated = {
odds: acc[matchingIndex].odds.add(result.oddsOfValue),
remainingWarbands: acc[matchingIndex].remainingWarbands,
};
return replace(acc, matchingIndex, updated);
} else {
// Add the set
return [
...acc,
{
odds: result.oddsOfValue,
remainingWarbands,
},
];
}
}
},
[]
);
return groupedByRemainingWarbands.sort((a, b) =>
a.remainingWarbands < b.remainingWarbands
? -1
: a.remainingWarbands > b.remainingWarbands
? 1
: 0
);
}
export { Fraction } from "./math";
|