Skip to content

Commit

Permalink
fix: Creatures with HP specified will no longer roll HP
Browse files Browse the repository at this point in the history
  • Loading branch information
valentine195 committed Aug 25, 2023
1 parent dca507c commit 8790517
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ export interface HomebrewCreature {
friendly?: boolean;
active?: boolean;
static?: boolean;
rollHP?: boolean;
"statblock-link"?: string;
}

Expand Down
21 changes: 14 additions & 7 deletions src/encounter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ interface CreatureStats {
hidden: boolean;
friendly?: boolean;
static?: boolean;
rollHP?: boolean;
}

export const equivalent = (
Expand Down Expand Up @@ -87,9 +88,9 @@ export class EncounterParser {
const players: string[] = this.parsePlayers(params);
const hide = this.parseHide(params);
const rawMonsters = params.creatures ?? [];
const rollHP = params.rollHP;

let creatures = await this.parseRawCreatures(rawMonsters);
const rollHP = params.rollHP ?? this.plugin.data.rollHP;
let creatures = await this.parseRawCreatures(rawMonsters, rollHP);

const xp = params.xp ?? null;
const playerLevels = players
Expand Down Expand Up @@ -156,12 +157,12 @@ export class EncounterParser {
}
return Array.from(new Set(playersToReturn));
}
async parseRawCreatures(rawMonsters: RawCreatureArray) {
async parseRawCreatures(rawMonsters: RawCreatureArray, rollHP: boolean) {
const creatureMap: Map<Creature, number | string> = new Map();
if (rawMonsters && Array.isArray(rawMonsters)) {
for (const raw of rawMonsters) {
const { creature, number = 1 } =
this.parseRawCreature(raw) ?? {};
this.parseRawCreature(raw, rollHP) ?? {};
if (!creature) continue;

const stats = {
Expand All @@ -172,7 +173,8 @@ export class EncounterParser {
modifier: creature.modifier,
xp: creature.xp,
hidden: creature.hidden,
friendly: creature.friendly
friendly: creature.friendly,
rollHP: creature.rollHP
};
const existing = [...creatureMap].find(([c]) =>
equivalent(c, stats)
Expand All @@ -195,7 +197,7 @@ export class EncounterParser {
}
return creatureMap;
}
parseRawCreature(raw: RawCreature) {
parseRawCreature(raw: RawCreature, globalRollHP: boolean) {
if (!raw) return {};
let monster: string | string[] | Record<string, any>,
number: string | number = 1;
Expand Down Expand Up @@ -233,7 +235,8 @@ export class EncounterParser {
mod: number,
xp: number,
hidden: boolean = false,
friendly: boolean = false;
friendly: boolean = false,
rollHP: boolean = globalRollHP;

if (typeof monster == "string") {
if (monster.match(/,\s+hidden/)) {
Expand All @@ -254,6 +257,9 @@ export class EncounterParser {
.split(/,\s?/)
.slice(1)
.map((v) => (isNaN(Number(v)) ? null : Number(v)));
if (hp) {
rollHP = false;
}
} else if (Array.isArray(monster)) {
if (typeof monster[0] == "string") {
//Hobgoblin, Jim
Expand Down Expand Up @@ -303,6 +309,7 @@ export class EncounterParser {
creature.xp = xp ?? creature.xp;
creature.hidden = hidden ?? creature.hidden;
creature.friendly = friendly ?? creature.friendly;
creature.rollHP = rollHP ?? globalRollHP ?? creature.rollHP;

return { creature, number };
}
Expand Down
22 changes: 17 additions & 5 deletions src/tracker/stores/tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,9 @@ function createTracker() {
) =>
updateAndSave((creatures) => {
if (plugin.canUseDiceRoller && roll) {
for (const creature of items) {
setCreatureHP(items, plugin);
/* for (const creature of items) {
if (!creature.rollHP) continue;
if (!creature?.hit_dice?.length) continue;
let roller = plugin.getRoller(
creature.hit_dice
Expand All @@ -632,7 +634,7 @@ function createTracker() {
creature.max =
creature.current_max =
roller.rollSync();
}
} */
}

for (let creature of items) {
Expand Down Expand Up @@ -701,8 +703,9 @@ function createTracker() {
plugin.canUseDiceRoller &&
(state?.rollHP ?? plugin.data.rollHP)
) {
for (const creature of creatures) {
if (creature.hit_dice?.length) {
setCreatureHP(creatures, plugin);
/* for (const creature of creatures) {
if (creature.rollHP && creature.hit_dice?.length) {
let roller = plugin.getRoller(
creature.hit_dice
) as StackRoller;
Expand All @@ -711,7 +714,7 @@ function createTracker() {
creature.current_max =
roller.rollSync();
}
}
} */
}

if (state?.logFile) {
Expand Down Expand Up @@ -855,3 +858,12 @@ function createTracker() {
}

export const tracker = createTracker();

function setCreatureHP(creatures: Creature[], plugin: InitiativeTracker) {
for (const creature of creatures) {
if (!creature.rollHP) continue;
if (!creature.hit_dice?.length) continue;
let roller = plugin.getRoller(creature.hit_dice) as StackRoller;
creature.hp = creature.max = creature.current_max = roller.rollSync();
}
}
7 changes: 6 additions & 1 deletion src/utils/creature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class Creature {
modifier: number | number[];
hp: number;
hit_dice?: string;
rollHP?: boolean;
temp: number;
ac: number | string;
current_ac: number | string;
Expand Down Expand Up @@ -74,6 +75,8 @@ export class Creature {
this.level = creature.level;
this.player = creature.player;

this.rollHP = creature.rollHP;

this.marker = creature.marker;

this.hp = this.max;
Expand Down Expand Up @@ -151,6 +154,7 @@ export class Creature {
yield this.hidden;
yield this.hit_dice;
yield this.current_ac;
yield this.rollHP;
}

static new(creature: Creature) {
Expand Down Expand Up @@ -230,7 +234,8 @@ export class Creature {
hidden: this.hidden,
friendly: this.friendly,
"statblock-link": this["statblock-link"],
hit_dice: this.hit_dice
hit_dice: this.hit_dice,
rollHP: this.rollHP
};
}

Expand Down

0 comments on commit 8790517

Please sign in to comment.