// GK – Loadout Manager (attach from Items Directory, set loadout, wire Synergy)
(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 WORLD_FOLDER_NAME = "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: `
Wybierz lub zmień loadout:
`,
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 === WORLD_FOLDER_NAME);
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}`); 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()}.`);
})();