Advertisement
Guest User

StealthMaster

a guest
Oct 28th, 2024
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. on("ready", () => {
  2. let waitingForAdvantage = false;
  3. let waitingForPerceptionType = false;
  4. let selectedToken = null;
  5. let isAdvantage = false;
  6. let tokens = [];
  7.  
  8. on("chat:message", (msg) => {
  9. if (msg.type === "api" && msg.content === "!stealth") {
  10. selectedToken = msg.selected ? getObj("graphic", msg.selected[0]._id) : null;
  11.  
  12. if (!selectedToken) {
  13. sendChat("StealthMaster", "/w gm <div style='color: red;'><b>Please select a token to perform a stealth check.</b></div>");
  14. return;
  15. }
  16.  
  17. const pageID = Campaign().get("playerpageid");
  18. tokens = findObjs({
  19. _type: "graphic",
  20. _subtype: "token",
  21. _pageid: pageID
  22. }).filter(token => token.get("represents") && token.id !== selectedToken.id);
  23.  
  24. if (tokens.length === 0) {
  25. sendChat("StealthMaster", "/w gm <div style='color: red;'><b>No tokens with perception found on the current map.</b></div>");
  26. return;
  27. }
  28.  
  29. waitingForAdvantage = true;
  30. 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>");
  31. } else if (waitingForAdvantage && (msg.content === "!stealth-adv-yes" || msg.content === "!stealth-adv-no")) {
  32. waitingForAdvantage = false;
  33. isAdvantage = (msg.content === "!stealth-adv-yes");
  34. waitingForPerceptionType = true;
  35. 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>");
  36. } else if (waitingForPerceptionType && (msg.content === "!stealth-perception-passive" || msg.content === "!stealth-perception-active")) {
  37. waitingForPerceptionType = false;
  38. const usePassive = (msg.content === "!stealth-perception-passive");
  39.  
  40. const stealthBonus = parseInt(getAttrByName(selectedToken.get("represents"), "stealth_bonus"), 10) || 0;
  41. const stealthRoll = isAdvantage
  42. ? Math.max(randomInteger(20), randomInteger(20)) + stealthBonus
  43. : randomInteger(20) + stealthBonus;
  44.  
  45. let stealthMessage = `<div style='border: 1px solid #888; padding: 10px; background-color: #f4f4f4;'>`;
  46. stealthMessage += `<div style='color: darkblue; font-weight: bold;'><u>Stealth Check</u></div>`;
  47. stealthMessage += `<div><b>${selectedToken.get("name")}</b> rolls <b>${stealthRoll}</b> for Stealth.</div>`;
  48. stealthMessage += `</div>`;
  49.  
  50. sendChat("StealthMaster", `/w gm ${stealthMessage}`); // Whisper stealth roll to GM
  51.  
  52. let detectionMessage = `<div style='border: 1px solid #888; padding: 10px; background-color: #f4f4f4;'>`;
  53. detectionMessage += `<div style='color: darkblue; font-weight: bold;'><u>Detection Results:</u></div>`;
  54.  
  55. let processedTokens = new Set();
  56.  
  57. tokens.forEach(token => {
  58. const characterID = token.get("represents");
  59. const character = getObj("character", characterID);
  60.  
  61. // 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)
  62. const isPC = (parseInt(getAttrByName(characterID, "npc"), 10) || 0) === 0;
  63.  
  64. if (character && isPC && !processedTokens.has(characterID)) {
  65. const perceptionValue = usePassive
  66. ? parseInt(getAttrByName(characterID, "passive_wisdom"), 10) || 10
  67. : randomInteger(20) + (parseInt(getAttrByName(characterID, "wisdom_mod"), 10) || 0);
  68.  
  69. const detectionResult = stealthRoll > perceptionValue
  70. ? "<span style='color: red;'>Fails to Notice</span>"
  71. : "<span style='color: green;'>Notices</span>";
  72. detectionMessage += `<div><b>${token.get("name")}</b>: ${detectionResult} (${usePassive ? "Passive" : "Active"} Perception: ${perceptionValue})</div>`;
  73.  
  74. processedTokens.add(characterID);
  75. }
  76. });
  77.  
  78. detectionMessage += `</div>`;
  79. sendChat("StealthMaster", `${detectionMessage}`); // Send detection results to general chat
  80. }
  81. });
  82. });
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement