@seroh/roll
Version:
An RPG dice-rolling library with a variety of built-in roll mechanics.
61 lines • 2.28 kB
JavaScript
import { Randomizer } from "./Randomizer";
var HistoryEntry;
(function (HistoryEntry) {
HistoryEntry[HistoryEntry["HIGH"] = 0] = "HIGH";
HistoryEntry[HistoryEntry["LOW"] = 1] = "LOW";
HistoryEntry[HistoryEntry["NEUTRAL"] = 2] = "NEUTRAL";
})(HistoryEntry || (HistoryEntry = {}));
export class KarmicRandomizer extends Randomizer {
static HistoryEntry = HistoryEntry;
historyQueue = [];
historyLimit;
highRollThreshold;
lowRollThreshold;
biasFactor;
constructor({ highRollThreshold, lowRollThreshold, biasFactor, historyLimit } = {}) {
//validate high roll / low roll
super();
this.historyLimit = historyLimit || 10;
this.highRollThreshold = highRollThreshold || 0.8;
this.lowRollThreshold = lowRollThreshold || 0.2;
this.biasFactor = biasFactor || 0.2;
this.historyLimit = historyLimit || 10;
}
generator() {
const rawRoll = this.generateRawRoll();
const biasedRoll = this.applyBias(this.biasFactor, rawRoll);
const rollType = this.evaluateRoll(biasedRoll);
// Update history and maintain the limit
this.historyQueue.push(rollType);
if (this.historyQueue.length > this.historyLimit) {
this.historyQueue.shift();
}
return biasedRoll;
}
applyBias(biasFactor, rawRoll) {
if (this.recentLowRolls > this.recentHighRolls) {
return Math.min(1, rawRoll + biasFactor); // Skew slightly toward high
}
else if (this.recentHighRolls > this.recentLowRolls) {
return Math.max(0, rawRoll - biasFactor); // Skew slightly toward low
}
return rawRoll;
}
get recentHighRolls() {
return this.historyQueue.filter((entry) => entry === HistoryEntry.HIGH).length;
}
get recentLowRolls() {
return this.historyQueue.filter((entry) => entry === HistoryEntry.LOW).length;
}
evaluateRoll(rawRoll) {
if (rawRoll >= this.highRollThreshold)
return HistoryEntry.HIGH;
if (rawRoll <= this.lowRollThreshold)
return HistoryEntry.LOW;
return HistoryEntry.NEUTRAL;
}
generateRawRoll() {
return Math.random();
}
}
//# sourceMappingURL=KarmicRandomizer.js.map