Skip to content

Commit

Permalink
fix: Roll initiative for creatures added to encounters (close #81)
Browse files Browse the repository at this point in the history
  • Loading branch information
valentine195 committed Aug 16, 2023
1 parent e489766 commit fefe218
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 38 deletions.
1 change: 0 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ export default class InitiativeTracker extends Plugin {
dice = dice.replace(`%mod${i + 1}%`, `${modifier[i]}`);
}
}
console.log("🚀 ~ file: main.ts:87 ~ dice:", dice);
const roller = this.getRoller(dice);
const initiative = roller.rollSync();
if (isNaN(initiative)) return defaultIfNoResult;
Expand Down
91 changes: 54 additions & 37 deletions src/tracker/stores/tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import type {
import type { StackRoller } from "obsidian-overload";
import { OVERFLOW_TYPE, getRpgSystem } from "src/utils";
import type Logger from "../../logger/logger";
import type { DifficultyLevel, DifficultyThreshold } from "src/utils/rpg-system";
import type {
DifficultyLevel,
DifficultyThreshold
} from "src/utils/rpg-system";

type HPUpdate = {
saved: boolean;
Expand Down Expand Up @@ -307,8 +310,7 @@ function createTracker() {
const creature = creatures.find((c) => c.name == name);
if (creature) {
if (!isNaN(Number(change.hp))) {
creature.hp =
change.hp;
creature.hp = change.hp;
}
if (change.max) {
creature.current_max = Math.max(
Expand Down Expand Up @@ -625,6 +627,13 @@ function createTracker() {
roller.rollSync();
}
}

for (let creature of items) {
if (creature.static) continue;
creature.initiative = plugin.getInitiativeValue(
creature.modifier
);
}
creatures.push(...items);
_logger?.log(
_logger?.join(items.map((c) => c.name)),
Expand Down Expand Up @@ -791,42 +800,50 @@ function createTracker() {
updateState: () => update((c) => c),

difficulty: (plugin: InitiativeTracker) =>
derived<Writable<Creature[]>, {difficulty: DifficultyLevel, thresholds: DifficultyThreshold[]}>(
creatures,
(values) => {
const players: number[] = [];
const creatureMap = new Map<Creature, number>();
const rpgSystem = getRpgSystem(plugin);

for (const creature of values) {
if (!creature.enabled) continue;
if (creature.friendly) continue;
if (creature.player && creature.level) {
players.push(creature.level);
continue;
}
const stats = {
name: creature.name,
display: creature.display,
ac: creature.ac,
hp: creature.hp,
modifier: creature.modifier,
xp: creature.xp,
hidden: creature.hidden
};
const existing = [...creatureMap].find(([c]) => equivalent(c, stats));
if (!existing) {
creatureMap.set(creature, 1);
continue;
}
creatureMap.set(existing[0], existing[1] + 1);
derived<
Writable<Creature[]>,
{
difficulty: DifficultyLevel;
thresholds: DifficultyThreshold[];
}
>(creatures, (values) => {
const players: number[] = [];
const creatureMap = new Map<Creature, number>();
const rpgSystem = getRpgSystem(plugin);

for (const creature of values) {
if (!creature.enabled) continue;
if (creature.friendly) continue;
if (creature.player && creature.level) {
players.push(creature.level);
continue;
}
return {
difficulty: rpgSystem.getEncounterDifficulty(creatureMap, players),
thresholds: rpgSystem.getDifficultyThresholds(players)
};
const stats = {
name: creature.name,
display: creature.display,
ac: creature.ac,
hp: creature.hp,
modifier: creature.modifier,
xp: creature.xp,
hidden: creature.hidden
};
const existing = [...creatureMap].find(([c]) =>
equivalent(c, stats)
);
if (!existing) {
creatureMap.set(creature, 1);
continue;
}
creatureMap.set(existing[0], existing[1] + 1);
}
)
return {
difficulty: rpgSystem.getEncounterDifficulty(
creatureMap,
players
),
thresholds: rpgSystem.getDifficultyThresholds(players)
};
})
};
}

Expand Down

0 comments on commit fefe218

Please sign in to comment.