Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- on("ready", () => {
- let waitingForAdvantage = false;
- let waitingForPerceptionType = false;
- let selectedToken = null;
- let isAdvantage = false;
- let tokens = [];
- on("chat:message", (msg) => {
- if (msg.type === "api" && msg.content === "!stealth") {
- selectedToken = msg.selected ? getObj("graphic", msg.selected[0]._id) : null;
- if (!selectedToken) {
- sendChat("StealthMaster", "/w gm <div style='color: red;'><b>Please select a token to perform a stealth check.</b></div>");
- return;
- }
- const pageID = Campaign().get("playerpageid");
- tokens = findObjs({
- _type: "graphic",
- _subtype: "token",
- _pageid: pageID
- }).filter(token => token.get("represents") && token.id !== selectedToken.id);
- if (tokens.length === 0) {
- sendChat("StealthMaster", "/w gm <div style='color: red;'><b>No tokens with perception found on the current map.</b></div>");
- return;
- }
- waitingForAdvantage = true;
- sendChat("StealthMaster", "/w gm <div style='border: 1px solid #888; padding: 10px; background-color: #f4f4f4;'><b>Roll Stealth with Advantage?</b><br><br>[Yes](!stealth-adv-yes) | [No](!stealth-adv-no)</div>");
- } else if (waitingForAdvantage && (msg.content === "!stealth-adv-yes" || msg.content === "!stealth-adv-no")) {
- waitingForAdvantage = false;
- isAdvantage = (msg.content === "!stealth-adv-yes");
- waitingForPerceptionType = true;
- sendChat("StealthMaster", "/w gm <div style='border: 1px solid #888; padding: 10px; background-color: #f4f4f4;'><b>Choose Perception Type:</b><br><br>[Passive Perception](!stealth-perception-passive) | [Active Perception](!stealth-perception-active)</div>");
- } else if (waitingForPerceptionType && (msg.content === "!stealth-perception-passive" || msg.content === "!stealth-perception-active")) {
- waitingForPerceptionType = false;
- const usePassive = (msg.content === "!stealth-perception-passive");
- const stealthBonus = parseInt(getAttrByName(selectedToken.get("represents"), "stealth_bonus"), 10) || 0;
- const stealthRoll = isAdvantage
- ? Math.max(randomInteger(20), randomInteger(20)) + stealthBonus
- : randomInteger(20) + stealthBonus;
- let stealthMessage = `<div style='border: 1px solid #888; padding: 10px; background-color: #f4f4f4;'>`;
- stealthMessage += `<div style='color: darkblue; font-weight: bold;'><u>Stealth Check</u></div>`;
- stealthMessage += `<div><b>${selectedToken.get("name")}</b> rolls <b>${stealthRoll}</b> for Stealth.</div>`;
- stealthMessage += `</div>`;
- sendChat("StealthMaster", `/w gm ${stealthMessage}`); // Whisper stealth roll to GM
- let detectionMessage = `<div style='border: 1px solid #888; padding: 10px; background-color: #f4f4f4;'>`;
- detectionMessage += `<div style='color: darkblue; font-weight: bold;'><u>Detection Results:</u></div>`;
- let processedTokens = new Set();
- tokens.forEach(token => {
- const characterID = token.get("represents");
- const character = getObj("character", characterID);
- // Check if the token is a PC by confirming a specific attribute or flag (for this example, assuming they have "npc" set to 0 for PCs)
- const isPC = (parseInt(getAttrByName(characterID, "npc"), 10) || 0) === 0;
- if (character && isPC && !processedTokens.has(characterID)) {
- const perceptionValue = usePassive
- ? parseInt(getAttrByName(characterID, "passive_wisdom"), 10) || 10
- : randomInteger(20) + (parseInt(getAttrByName(characterID, "wisdom_mod"), 10) || 0);
- const detectionResult = stealthRoll > perceptionValue
- ? "<span style='color: red;'>Fails to Notice</span>"
- : "<span style='color: green;'>Notices</span>";
- detectionMessage += `<div><b>${token.get("name")}</b>: ${detectionResult} (${usePassive ? "Passive" : "Active"} Perception: ${perceptionValue})</div>`;
- processedTokens.add(characterID);
- }
- });
- detectionMessage += `</div>`;
- sendChat("StealthMaster", `${detectionMessage}`); // Send detection results to general chat
- }
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement