Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initiative ties resolution setting #295

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions src/settings/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import {
DEFAULT_UNDEFINED,
EDIT,
HP,
INITIATIVE
INITIATIVE,
OVERFLOW_TYPE,
RESOLVE_TIES
} from "../utils";
import { RpgSystemSetting, getRpgSystem } from "../utils/rpg-system";
import type { Party } from "./settings.types";
Expand Down Expand Up @@ -209,10 +211,10 @@ export default class InitiativeTrackerSettings extends PluginSettingTab {
"Set what happens to healing which goes above creatures' max HP threshold."
)
.addDropdown((d) => {
d.addOption("ignore", "Ignore");
d.addOption("temp", "Add to temp HP");
d.addOption("current", "Add to current HP");
d.setValue(this.plugin.data.hpOverflow ?? "ignore");
d.addOption(OVERFLOW_TYPE.ignore, "Ignore");
d.addOption(OVERFLOW_TYPE.temp, "Add to temp HP");
d.addOption(OVERFLOW_TYPE.current, "Add to current HP");
d.setValue(this.plugin.data.hpOverflow ?? OVERFLOW_TYPE.ignore);
d.onChange(async (v) => {
this.plugin.data.hpOverflow = v;
this.plugin.saveSettings();
Expand Down Expand Up @@ -329,6 +331,21 @@ export default class InitiativeTrackerSettings extends PluginSettingTab {
this.display();
});
});
new Setting(additionalContainer)
.setName("Resolve Initiative Ties")
.setDesc(
"Define what happens if two creatures have the same initiative."
)
.addDropdown((d) => {
d.addOption(RESOLVE_TIES.playerFirst, "Player first");
d.addOption(RESOLVE_TIES.npcFirst, "NPC first");
d.addOption(RESOLVE_TIES.random, "Random");
d.setValue(this.plugin.data.resolveTies ?? RESOLVE_TIES.playerFirst);
d.onChange(async (v) => {
this.plugin.data.resolveTies = v;
this.plugin.saveSettings();
});
});
}
private _displayPlayers(additionalContainer: HTMLDetailsElement) {
additionalContainer.empty();
Expand Down
1 change: 1 addition & 0 deletions src/settings/settings.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface InitiativeTrackerData {
warnedAboutImports: boolean;
logging: boolean;
logFolder: string;
resolveTies: string;
useLegacy: boolean;
diplayPlayerHPValues: boolean;
rollHP: boolean;
Expand Down
37 changes: 35 additions & 2 deletions src/tracker/stores/tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type { InitiativeTrackerData } from "src/settings/settings.types";
import type { InitiativeViewState } from "../view.types";
import {
OVERFLOW_TYPE,
RESOLVE_TIES,
RollPlayerInitiativeBehavior,
getRpgSystem
} from "src/utils";
Expand Down Expand Up @@ -95,7 +96,7 @@ function createTracker() {
return data.descending;
});
let _settings: InitiativeTrackerData | null;

const condensed = derived(creatures, (values) => {
if (_settings?.condense) {
values.forEach((creature, _, arr) => {
Expand All @@ -116,9 +117,40 @@ function createTracker() {
const ordered = derived([condensed, data], ([values, data]) => {
const sort = [...values];
sort.sort((a, b) => {
return data.descending
/* Order creatures in this order:
1. By initiative
2. By manual order (drag & drop)
3. According to the resolveTies setting */
if (a.initiative != b.initiative) {
return data.descending
? b.initiative - a.initiative
: a.initiative - b.initiative;
}

if (
a.manualOrder !== null && a.manualOrder !== undefined &&
b.manualOrder !== null && b.manualOrder !== undefined &&
a.manualOrder !== b.manualOrder
) {
const aOrder = a.manualOrder || 0;
const bOrder = b.manualOrder || 0;
return aOrder - bOrder;
}

switch (_settings.resolveTies) {
case RESOLVE_TIES.random:
return Math.random() < 0.5 ? 1 : -1;
case RESOLVE_TIES.playerFirst:
case RESOLVE_TIES.npcFirst:
const aPlayer = a.player ? 1 : 0;
const bPlayer = b.player ? 1 : 0;
if (_settings.resolveTies == RESOLVE_TIES.playerFirst) {
return bPlayer - aPlayer
} else {
return aPlayer - bPlayer
}
}

});
current_order = sort;
return sort;
Expand Down Expand Up @@ -336,6 +368,7 @@ function createTracker() {
creature.modifier
);
}
creature.manualOrder = null;
}
return creatures;
}
Expand Down
5 changes: 4 additions & 1 deletion src/tracker/ui/creatures/Table.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@
tracker.logNewInitiative(dropped.creature);
}
items = e.detail.items;
$tracker = [...items.map(({ creature }) => creature)];
$tracker = [...items.map(({ creature }, i) => {
creature.manualOrder = i;
return creature;
})];
}

const diceIcon = (node: HTMLElement) => {
Expand Down
21 changes: 14 additions & 7 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ export enum RollPlayerInitiativeBehavior {
SetToZero
}

export const OVERFLOW_TYPE: { [key: string]: string } = {
ignore: "ignore",
current: "current",
temp: "temp"
};

export const RESOLVE_TIES: { [key: string]: string } = {
playerFirst: "playerFirst",
npcFirst: "npcFirst",
random: "random",
};

export const DEFAULT_SETTINGS: InitiativeTrackerData = {
players: [],
parties: [],
Expand Down Expand Up @@ -54,11 +66,12 @@ export const DEFAULT_SETTINGS: InitiativeTrackerData = {
player: true,
builder: true
},
hpOverflow: "ignore",
hpOverflow: OVERFLOW_TYPE.ignore,
additiveTemp: false,
rpgSystem: "dnd5e",
logging: false,
logFolder: "/",
resolveTies: RESOLVE_TIES.playerFirst,
useLegacy: false,
diplayPlayerHPValues: true,
rollHP: false,
Expand All @@ -71,12 +84,6 @@ export const DEFAULT_SETTINGS: InitiativeTrackerData = {
rollPlayerInitiatives: RollPlayerInitiativeBehavior.Always
};

export const OVERFLOW_TYPE: { [key: string]: string } = {
ignore: "ignore",
current: "current",
temp: "temp"
};

export const DECIMAL_TO_VULGAR_FRACTION: Record<string, string> = {
0.125: "⅛",
0.25: "¼",
Expand Down
1 change: 1 addition & 0 deletions src/utils/creature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export class Creature {
status: Set<Condition> = new Set();
marker: string;
initiative: number;
manualOrder: number;
static: boolean = false;
source: string | string[];
id: string;
Expand Down