Files
vtt_work/wg-greyknights/scripts/macros/gk-loadout-manager.js
T

64 lines
2.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// GK Loadout Manager (module)
(async () => {
const NAMES = {
archetype: "Grey Knight (Archetype)",
ability: "Aegis of Titan (Ability)",
nemesis: "Nemesis Force Halberd",
storm: "Grey Knight Armour Integrated Storm Bolter",
aegis: "Aegis Power Armour",
term: "Terminator Aegis (Grey Knights)"
};
const FOLDER = "Grey Knight Pack";
const actor = canvas.tokens.controlled[0]?.actor ?? game.user.character;
if (!actor) return ui.notifications.warn("Zaznacz token albo przypisz postać.");
const mode = await Dialog.prompt({
title: "Grey Knight Loadout Manager",
content: `<p>Wybierz lub zmień loadout:</p>
<label><input type="radio" name="m" value="incognito"> Incognito (bez Aegis)</label><br/>
<label><input type="radio" name="m" value="war" checked> War-Plate (Aegis Power Armour)</label><br/>
<label><input type="radio" name="m" value="term"> Terminator (Terminator Aegis)</label>`,
label: "OK",
callback: html => html.find('input[name="m"]:checked').val()
});
if (!mode) return;
const findWorldItem = (name) => {
const inFolder = game.items.filter(i => i.name === name && i.folder?.name === FOLDER);
if (inFolder.length) return inFolder[0];
return game.items.getName(name) ?? null;
};
const ensureOnActor = async (name) => {
const have = actor.items.getName(name);
if (have) return have;
const worldIt = findWorldItem(name);
if (!worldIt) { ui.notifications.warn(`Brak w Items Directory: ${name} — użyj importera modułu.`); return null; }
const created = await actor.createEmbeddedDocuments("Item", [worldIt.toObject()]);
return created[0];
};
for (const key of Object.keys(NAMES)) await ensureOnActor(NAMES[key]);
const setEquipped = async (name, on) => { const it = actor.items.getName(name); if (it) await it.update({"system.equipped": !!on}); };
await setEquipped(NAMES.aegis, false);
await setEquipped(NAMES.term, false);
await setEquipped(NAMES.storm, false);
await setEquipped(NAMES.nemesis, false);
if (mode === "war") {
await setEquipped(NAMES.aegis, true);
await setEquipped(NAMES.storm, true);
await setEquipped(NAMES.nemesis, true);
} else if (mode === "term") {
await setEquipped(NAMES.term, true);
await setEquipped(NAMES.storm, true);
await setEquipped(NAMES.nemesis, true);
}
const anyAegisEquipped = !!actor.items.find(i => ["armour","armor"].includes(i.type) && /Aegis/.test(i.name) && i.system?.equipped);
const abilItem = actor.items.getName(NAMES.ability);
if (abilItem) for (const ef of abilItem.effects ?? []) if (ef.name?.includes("Aegis Synergy")) await ef.update({ disabled: !anyAegisEquipped });
ui.notifications.info(`Loadout ustawiony: ${mode.toUpperCase()}.`);
})();