Guest User

Untitled

a guest
Nov 22nd, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.55 KB | None | 0 0
  1. // SET CONFIG FIRST, THEN CHANGE "RUN" TO TRUE
  2.  
  3. // Create a button to roll HP for a selected NPC token. Requires TokenMod
  4. var doHP = false,
  5.  
  6. // If you're done with all the config, set this to true and save the script.
  7. // Be careful and make sure you're 100 percent ready. This script is going
  8. // to make macro buttons for EVERY CHARACTER YOU HAVE. It will take a moment.
  9. // These actions are not easily undone. Consider copying your campaign
  10. // to test it first.
  11. run = false,
  12.  
  13. // END CONFIG
  14. CharAbilities = {}
  15. const getRepeatingSectionAttrs = function (charid, prefix)
  16. {
  17. const repeatingAttrs = {},
  18. regExp = new RegExp(`^${prefix}_(-[-A-Za-z0-9]+?|\\d+)_`);
  19. let repOrder;
  20. // Get attributes
  21. findObjs({
  22. _type: 'attribute',
  23. _characterid: charid
  24. }).forEach(o => {
  25. const attrName = o.get('name');
  26. if (attrName.search(regExp) === 0) repeatingAttrs[attrName] = o;
  27. else if (attrName === `_reporder_${prefix}`) repOrder = o.get('current').split(',');
  28. });
  29. if (!repOrder) repOrder = [];
  30. // Get list of repeating row ids by prefix from repeatingAttrs
  31. const unorderedIds = [...new Set(Object.keys(repeatingAttrs)
  32. .map(n => n.match(regExp))
  33. .filter(x => !!x)
  34. .map(a => a[1]))];
  35. const repRowIds = [...new Set(repOrder.filter(x => unorderedIds.includes(x)).concat(unorderedIds))];
  36. return [repRowIds, repeatingAttrs];
  37. },
  38. removeSplenAbils = function()
  39. {
  40. var abilities = filterObjs(function(o)
  41. {
  42. if(o.get("_type") == "ability" && o.get("description") === "SplenRepeatingAbil")
  43. {
  44. log("Removing ability '" + o.get("name") + "' from " + o.get("_characterid"))
  45. o.remove()
  46. }
  47. })
  48. }
  49. var wasAnythingCreated = false
  50. const createAbil = function(n, act, char)
  51. {
  52. var makeAction = true
  53. if(CharAbilities[char.id][n]) makeAction = false
  54. createObj("ability", {
  55. name: n,
  56. action: act,
  57. istokenaction: makeAction,
  58. characterid: char.id,
  59. description: "SplenRepeatingAbil"
  60. })
  61. wasAnythingCreated = true
  62. CharAbilities[char.id][n] = true
  63. log("Created ability button '" + n + "' for character '" + char.get("name") + "'")
  64. },
  65. abilExists = function(a, char) {return findObjs({action: a, _type: "ability", _characterid: char.id}).length},
  66. process = function()
  67. {
  68. removeSplenAbils()
  69. CharAbilities = {}
  70. var prefixes =
  71. [
  72. "npcaction",
  73. "npcaction-l",
  74. "attack",
  75. "spell-npc",
  76. "spell-cantrip"
  77. ],
  78. suffixes = [],
  79. names = []
  80. for (f = 1; f < 15; f++)
  81. {
  82. if(f<3) names.push("_name")
  83. else if(f<4) names.push("_atkname")
  84. else names.push("_spellname")
  85.  
  86. if(f<4) suffixes.push("rollbase")
  87. else if(f<6) suffixes.push("rollcontent")
  88. else
  89. {
  90. prefixes.push("spell-" + String(f - 5))
  91. suffixes.push("rollcontent")
  92. }
  93. }
  94.  
  95. log("Finding repeating actions for characters...")
  96. var chars = findObjs({_type: "character"})
  97.  
  98. var makeAbils = function(o)
  99. {
  100. CharAbilities[o.id] = CharAbilities[o.id] || {}
  101. var isNPC = parseInt(getAttrByName(o.id, "npc"))
  102.  
  103. //Handle utility buttons
  104. if(isNPC && doHP)
  105. {
  106. var a = "!token-mod --set bar3|[[@{npc_hpformula}]]"
  107. if(!abilExists(a,o)) createAbil(".HP", a, o)
  108. }
  109. a = "@{wtype}&{template:simple} {{rname=^{init-u}}} {{mod=@{initiative_bonus}}} {{r1=[[@{initiative_style}+@{initiative_bonus}@{pbd_safe}[INIT] &{tracker}]]}} {{normal=1}} @{charname_output}"
  110. if(isNPC) a = "@{wtype}&{template:npc} @{npc_name_flag} {{rname=^{init}}} {{mod=[[[[@{initiative_bonus}]][DEX]]]}} {{r1=[[@{d20}+[[@{initiative_bonus}]][DEX] &{tracker}]]}} {{normal=1}} {{type=Initiative}}"
  111. if(!abilExists(a,o))
  112. {
  113. if(isNPC) createAbil(".INIT", a, o)
  114. else createAbil(".INIT", a, o)
  115. }
  116. a = "!SplenSave ?{Which attribute?|STR|DEX|CON|INT|WIS|CHA}"
  117. if(!abilExists(a,o)) createAbil(".SAVE", a, o)
  118. a = "!SplenDoor"
  119. if(!abilExists(a,o)) createAbil(".DOOR", a, o)
  120.  
  121. //Handle repeating actions
  122. _.each(prefixes, function(p,k)
  123. {
  124. RepActions = getRepeatingSectionAttrs(o.id,"repeating_" + p)
  125. _.each(RepActions[0], function(g)
  126. {
  127. if(RepActions[0].length)
  128. {
  129. var a = "@{repeating_" + p + "_" + g + "_" + suffixes[k] + "}"
  130. var j = RepActions[1]["repeating_" + p + "_" + g + names[k]].get("current")
  131. if(j === "") j = p + "_" + g
  132. createAbil(j, a, o)
  133. }
  134. })
  135. })
  136.  
  137. }
  138. _.each(chars, function(o)
  139. {
  140. _.delay(makeAbils, 50, o)
  141. })
  142. }
  143.  
  144. on('ready',function()
  145. {
  146. if(run) process()
  147. })
  148.  
  149. var getAttr = function(a)
  150. {
  151. return {
  152. "STR" : "strength",
  153. "DEX" : "dexterity",
  154. "CON" : "constitution",
  155. "INT" : "intelligence",
  156. "WIS" : "wisdom",
  157. "CHA" : "charisma"
  158. }[a]
  159. }
  160.  
  161. on("chat:message", function(msg)
  162. {
  163. if(msg.type == "api")
  164. {
  165. if(msg.content.indexOf("!SplenSave ") !== -1 && msg.selected)
  166. {
  167. var a = msg.content.replace("!SplenSave ", "")
  168. var b = getAttr(a)
  169. var char = getObj(msg.selected[0]._type, msg.selected[0]._id)
  170. var c = getObj("character", char.get("represents")).get("name")
  171. var isNPC = parseInt(getAttrByName(char.get("represents"), "npc"))
  172. if(isNPC) sendChat(msg.who, "@{" + c + "|wtype}&{template:npc} @{" + c + "|npc_name_flag} {{rname=^{" + b + "}}} {{mod=[[[[@{" + c + "|" + b + "_mod}]]]]}} {{r1=[[@{" + c + "|d20}+[[@{" + c + "|" + b + "_mod}]]]]}} @{" + c + "|rtype}+[[@{" + c + "|" + b + "_mod}]]]]}} {{type=Ability}}")
  173. else sendChat(msg.who, "@{" + c + "|wtype}&{template:simple} {{rname=^{" + b + "-save-u}}} {{mod=@{" + c + "|" + b + "_save_bonus}}} {{r1=[[@{" + c + "|d20}+@{" + c + "|" + b + "_save_bonus}@{" + c + "|pbd_safe}]]}} @{" + c + "|rtype}+@{" + c + "|" + b + "_save_bonus}@{" + c + "|pbd_safe}]]}} @{" + c + "|global_save_mod} @{" + c + "|charname_output}")
  174. }
  175. else if(msg.content.indexOf("!SplenFlushAbils") !== -1)
  176. {
  177. var isGM = playerIsGM(msg.playerid)
  178. if(isGM)
  179. {
  180. removeSplenAbils()
  181. sendChat("System", "/w " + msg.who + "All repeating ability buttons have been removed.")
  182. }
  183. else sendChat("System", "/w " + msg.who + "You must be a GM to use that command.")
  184. }
  185. }
  186.  
  187. })
Add Comment
Please, Sign In to add comment