Advertisement
Guest User

Untitled

a guest
Jun 21st, 2017
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.30 KB | None | 0 0
  1. --
  2. -- Please see the license.html file included with this distribution for
  3. -- attribution and copyright information.
  4. --
  5.  
  6. local TOKEN_MAX_EFFECTS = 6;
  7. local TOKEN_EFFECT_WIDTH = 12;
  8. local TOKEN_EFFECT_MARGIN = 2;
  9. local TOKEN_EFFECT_OFFSETX = 6;
  10. local TOKEN_EFFECT_OFFSETY = -6;
  11. local TOKEN_HEALTH_MINBAR = 14;
  12. local TOKEN_HEALTH_WIDTH = 20;
  13.  
  14. function onInit()
  15.     DB.addHandler("combattracker.list.*.hp", "onUpdate", updateHealth);
  16.     DB.addHandler("combattracker.list.*.hptemp", "onUpdate", updateHealth);
  17.     DB.addHandler("combattracker.list.*.wounds", "onUpdate", updateHealth);
  18.  
  19.     DB.addHandler("combattracker.list.*.effects", "onChildUpdate", updateEffectsList);
  20.     DB.addHandler("combattracker.list.*.effects.*.isactive", "onAdd", updateEffects);
  21.     DB.addHandler("combattracker.list.*.effects.*.isactive", "onUpdate", updateEffects);
  22.     DB.addHandler("combattracker.list.*.effects.*.isgmonly", "onAdd", updateEffects);
  23.     DB.addHandler("combattracker.list.*.effects.*.isgmonly", "onUpdate", updateEffects);
  24.     DB.addHandler("combattracker.list.*.effects.*.label", "onAdd", updateEffects);
  25.     DB.addHandler("combattracker.list.*.effects.*.label", "onUpdate", updateEffects);
  26.  
  27.     DB.addHandler("options.TNPCE", "onUpdate", TokenManager.onOptionChanged);
  28.     DB.addHandler("options.TNPCH", "onUpdate", TokenManager.onOptionChanged);
  29.     DB.addHandler("options.TPCE", "onUpdate", TokenManager.onOptionChanged);
  30.     DB.addHandler("options.TPCH", "onUpdate", TokenManager.onOptionChanged);
  31.     DB.addHandler("options.WNDC", "onUpdate", TokenManager.onOptionChanged);
  32. end
  33.  
  34. function onScaleChanged(tokenCT, nodeCT)
  35.     updateHealthBarScale(tokenCT, nodeCT);
  36.     updateEffectsHelper(tokenCT, nodeCT);
  37. end
  38.  
  39. function onHover(tokenCT, nodeCT, bOver)
  40.     local sFaction = DB.getValue(nodeCT, "friendfoe", "");
  41.  
  42.     local sOptEffects, sOptHealth;
  43.     if sFaction == "friend" then
  44.         sOptEffects = OptionsManager.getOption("TPCE");
  45.         sOptHealth = OptionsManager.getOption("TPCH");
  46.     else
  47.         sOptEffects = OptionsManager.getOption("TNPCE");
  48.         sOptHealth = OptionsManager.getOption("TNPCH");
  49.     end
  50.        
  51.     local aWidgets = {};
  52.     if sOptHealth == "barhover" then
  53.         aWidgets["healthbar"] = tokenCT.findWidget("healthbar");
  54.     elseif sOptHealth == "dothover" then
  55.         aWidgets["healthdot"] = tokenCT.findWidget("healthdot");
  56.     end
  57.     if sOptEffects == "hover" or sOptEffects == "markhover" then
  58.         for i = 1, TOKEN_MAX_EFFECTS do
  59.             aWidgets["effect" .. i] = tokenCT.findWidget("effect" .. i);
  60.         end
  61.     end
  62.  
  63.     for _, vWidget in pairs(aWidgets) do
  64.         vWidget.setVisible(bOver);
  65.     end
  66. end
  67.  
  68. function updateAttributesHelper(tokenCT, nodeCT)
  69.     updateHealthHelper(tokenCT, nodeCT);
  70.     updateEffectsHelper(tokenCT, nodeCT);
  71. end
  72.  
  73. function updateTooltip(tokenCT, nodeCT)
  74.     local sOptTNAM = OptionsManager.getOption("TNAM");
  75.     local sOptTH, sOptTE;
  76.     if DB.getValue(nodeCT, "friendfoe", "") == "friend" then
  77.         sOptTE = OptionsManager.getOption("TPCE");
  78.         sOptTH = OptionsManager.getOption("TPCH");
  79.     else
  80.         sOptTE = OptionsManager.getOption("TNPCE");
  81.         sOptTH = OptionsManager.getOption("TNPCH");
  82.     end
  83.        
  84.     local aTooltip = {};
  85.     if sOptTNAM == "tooltip" then
  86.         table.insert(aTooltip, DB.getValue(nodeCT, "name", ""));
  87.     end
  88.     if sOptTH == "tooltip" then
  89.         local sStatus;
  90.         _, sStatus = ActorManager2.getPercentWounded2("ct", nodeCT);
  91.         table.insert(aTooltip, sStatus);
  92.     end
  93.     if sOptTE == "tooltip" then
  94.         local aCondList = getConditionIconList(nodeCT, true);
  95.         for _,v in ipairs(aCondList) do
  96.             table.insert(aTooltip, v.sLabel);
  97.         end
  98.     end
  99.    
  100.     tokenCT.setName(table.concat(aTooltip, "\r"));
  101. end
  102.  
  103. function updateFaction(tokenCT, nodeCT)
  104.     updateHealthHelper(tokenCT, nodeCT);
  105.     updateEffectsHelper(tokenCT, nodeCT);
  106. end
  107.  
  108. function updateHealth(nodeField)
  109.     local nodeCT = nodeField.getParent();
  110.     local tokenCT = CombatManager.getTokenFromCT(nodeCT);
  111.     if tokenCT then
  112.         updateHealthHelper(tokenCT, nodeCT);
  113.         updateTooltip(tokenCT, nodeCT);
  114.     end
  115. end
  116.  
  117. function updateHealthHelper(tokenCT, nodeCT)
  118.     local sOptTH;
  119.     if DB.getValue(nodeCT, "friendfoe", "") == "friend" then
  120.         sOptTH = OptionsManager.getOption("TPCH");
  121.     else
  122.         sOptTH = OptionsManager.getOption("TNPCH");
  123.     end
  124.    
  125.     local aWidgets = getWidgetList(tokenCT, "health");
  126.    
  127.     if sOptTH == "off" or sOptTH == "tooltip" then
  128.         for _, vWidget in pairs(aWidgets) do
  129.             vWidget.destroy();
  130.         end
  131.     else
  132.         local sColor, nPercentWounded, sStatus = ActorManager2.getWoundBarColor("ct", nodeCT);
  133.        
  134.         if sOptTH == "bar" or sOptTH == "barhover" then
  135.             local w, h = tokenCT.getSize();
  136.        
  137.             if h >= TOKEN_HEALTH_MINBAR then
  138.                 local widgetHealthBar = aWidgets["healthbar"];
  139.                 if not widgetHealthBar then
  140.                     widgetHealthBar = tokenCT.addBitmapWidget("healthbar");
  141.                     widgetHealthBar.sendToBack();
  142.                     widgetHealthBar.setName("healthbar");
  143.                 end
  144.                 if widgetHealthBar then
  145.                     widgetHealthBar.setColor(sColor);
  146.                     widgetHealthBar.setTooltipText(sStatus);
  147.                     widgetHealthBar.setVisible(sOptTH == "bar");
  148.                 end
  149.             end
  150.             updateHealthBarScale(tokenCT, nodeCT);
  151.            
  152.             if aWidgets["healthdot"] then
  153.                 aWidgets["healthdot"].destroy();
  154.             end
  155.         elseif sOptTH == "dot" or sOptTH == "dothover" then
  156.             local widgetHealthDot = aWidgets["healthdot"];
  157.             if not widgetHealthDot then
  158.                 widgetHealthDot = tokenCT.addBitmapWidget("healthdot");
  159.                 widgetHealthDot.setPosition("bottomright", -4, -6);
  160.                 widgetHealthDot.setName("healthdot");
  161.             end
  162.             if widgetHealthDot then
  163.                 widgetHealthDot.setColor(sColor);
  164.                 widgetHealthDot.setTooltipText(sStatus);
  165.                 widgetHealthDot.setVisible(sOptTH == "dot");
  166.             end
  167.  
  168.             if aWidgets["healthbar"] then
  169.                 aWidgets["healthbar"].destroy();
  170.             end
  171.         end
  172.     end
  173.     -- new stuff, adds indicator for "DEAD" on the token. -celestian
  174.     local nPercentHealth = ActorManager2.getPercentWounded2("ct", nodeCT);
  175.     local widgetDeathIndicator = tokenCT.findWidget("deathindicator");
  176.     local nWidth, nHeight = tokenCT.getSize();
  177.     local sName = DB.getValue(nodeCT,"name","Unknown");
  178.     if not widgetDeathIndicator then
  179.         widgetDeathIndicator = tokenCT.addBitmapWidget("token_dead");
  180.         widgetDeathIndicator.setBitmap("token_dead");
  181.         widgetDeathIndicator.setName("deathindicator");
  182.         widgetDeathIndicator.setTooltipText(sName .. " has fallen, as if dead.");
  183.         widgetDeathIndicator.setSize(nWidth-20, nHeight-20);
  184.     end
  185.     -- nPercentHealth is the percent of damage, 1 = 100% or more so dead
  186.     widgetDeathIndicator.setVisible(nPercentHealth>=1);
  187.     -- end new stuff
  188. end
  189.  
  190. function updateHealthBarScale(tokenCT, nodeCT)
  191.     local widgetHealthBar = tokenCT.findWidget("healthbar");
  192.     if widgetHealthBar then
  193.         local nPercentWounded = ActorManager2.getPercentWounded2("ct", nodeCT);
  194.        
  195.         local w, h = tokenCT.getSize();
  196.         h = h + 4;
  197.  
  198.         widgetHealthBar.setSize();
  199.         local barw, barh = widgetHealthBar.getSize();
  200.        
  201.         -- Resize bar to match health percentage, but preserve bulb portion of bar graphic
  202.         if h >= TOKEN_HEALTH_MINBAR then
  203.             barh = (math.max(1.0 - nPercentWounded, 0) * (math.min(h, barh) - TOKEN_HEALTH_MINBAR)) + TOKEN_HEALTH_MINBAR;
  204.         else
  205.             barh = TOKEN_HEALTH_MINBAR;
  206.         end
  207.  
  208.         widgetHealthBar.setSize(barw, barh, "bottom");
  209.         widgetHealthBar.setPosition("bottomright", -4, -(barh / 2) + 4);
  210.     end
  211. end
  212.  
  213. function updateEffects(nodeEffectField)
  214.     local nodeEffect = nodeEffectField.getChild("..");
  215.     local nodeCT = nodeEffect.getChild("...");
  216.     local tokenCT = CombatManager.getTokenFromCT(nodeCT);
  217.     if tokenCT then
  218.         updateEffectsHelper(tokenCT, nodeCT);
  219.         updateTooltip(tokenCT, nodeCT);
  220.     end
  221. end
  222.  
  223. function updateEffectsList(nodeEffectsList, bListChanged)
  224.     if bListChanged then
  225.         local nodeCT = nodeEffectsList.getParent();
  226.         local tokenCT = CombatManager.getTokenFromCT(nodeCT);
  227.         if tokenCT then
  228.             updateEffectsHelper(tokenCT, nodeCT);
  229.             updateTooltip(tokenCT, nodeCT);
  230.         end
  231.     end
  232. end
  233.  
  234. function updateEffectsHelper(tokenCT, nodeCT)
  235.     local sOptTE;
  236.     if DB.getValue(nodeCT, "friendfoe", "") == "friend" then
  237.         sOptTE = OptionsManager.getOption("TPCE");
  238.     else
  239.         sOptTE = OptionsManager.getOption("TNPCE");
  240.     end
  241.  
  242.     local aWidgets = getWidgetList(tokenCT, "effect");
  243.    
  244.     if sOptTE == "off" or sOptTE == "tooltip" then
  245.         for _, vWidget in pairs(aWidgets) do
  246.             vWidget.destroy();
  247.         end
  248.     elseif sOptTE == "mark" or sOptTE == "markhover" then
  249.         local bWidgetsVisible = (sOptTE == "mark");
  250.        
  251.         local aTooltip = {};
  252.         local aCondList = getConditionIconList(nodeCT);
  253.         for _,v in ipairs(aCondList) do
  254.             table.insert(aTooltip, v.sLabel);
  255.         end
  256.        
  257.         if #aTooltip > 0 then
  258.             local w = aWidgets["effect1"];
  259.             if not w then
  260.                 w = tokenCT.addBitmapWidget();
  261.                 w.setPosition("bottomleft", TOKEN_EFFECT_OFFSETX, TOKEN_EFFECT_OFFSETY);
  262.                 w.setName("effect1");
  263.             end
  264.             if w then
  265.                 w.setBitmap("cond_generic");
  266.                 w.setVisible(bWidgetsVisible);
  267.                 w.setTooltipText(table.concat(aTooltip, "\r"));
  268.             end
  269.             for i = 2, TOKEN_MAX_EFFECTS do
  270.                 local w = aWidgets["effect" .. i];
  271.                 if w then
  272.                     w.destroy();
  273.                 end
  274.             end
  275.         else
  276.             for i = 1, TOKEN_MAX_EFFECTS do
  277.                 local w = aWidgets["effect" .. i];
  278.                 if w then
  279.                     w.destroy();
  280.                 end
  281.             end
  282.         end
  283.     else
  284.         local bWidgetsVisible = (sOptTE == "on");
  285.        
  286.         local aCondList = getConditionIconList(nodeCT);
  287.         local nConds = #aCondList;
  288.        
  289.         local wToken, hToken = tokenCT.getSize();
  290.         local nMaxToken = math.floor(((wToken - TOKEN_HEALTH_WIDTH - TOKEN_EFFECT_MARGIN) / (TOKEN_EFFECT_WIDTH + TOKEN_EFFECT_MARGIN)) + 0.5);
  291.         if nMaxToken < 1 then
  292.             nMaxToken = 1;
  293.         end
  294.         local nMaxShown = math.min(nMaxToken, TOKEN_MAX_EFFECTS);
  295.        
  296.         local i = 1;
  297.         local nMaxLoop = math.min(nConds, nMaxShown);
  298.         while i <= nMaxLoop do
  299.             local w = aWidgets["effect" .. i];
  300.             if not w then
  301.                 w = tokenCT.addBitmapWidget();
  302.                 w.setPosition("bottomleft", TOKEN_EFFECT_OFFSETX + ((TOKEN_EFFECT_WIDTH + TOKEN_EFFECT_MARGIN) * (i - 1)), TOKEN_EFFECT_OFFSETY);
  303.                 w.setName("effect" .. i);
  304.             end
  305.             if w then
  306.                 if i == nMaxLoop and nConds > nMaxLoop then
  307.                     w.setBitmap("cond_more");
  308.                     local aTooltip = {};
  309.                     for j = i, nConds do
  310.                         table.insert(aTooltip, aCondList[j].sLabel);
  311.                     end
  312.                     w.setTooltipText(table.concat(aTooltip, "\r"));
  313.                 else
  314.                     w.setBitmap(aCondList[i].sIcon);
  315.                     w.setTooltipText(aCondList[i].sText);
  316.                 end
  317.                 w.setVisible(bWidgetsVisible);
  318.             end
  319.             i = i + 1;
  320.         end
  321.         while i <= TOKEN_MAX_EFFECTS do
  322.             local w = aWidgets["effect" .. i];
  323.             if w then
  324.                 w.destroy();
  325.             end
  326.             i = i + 1;
  327.         end
  328.     end
  329. end
  330.  
  331. function getConditionIconList(nodeCT, bSkipGMOnly)
  332.     local aIconList = {};
  333.  
  334.     local rActor = ActorManager.getActorFromCT(nodeCT);
  335.    
  336.     -- Iterate through effects
  337.     local aSorted = {};
  338.     for _,nodeChild in pairs(DB.getChildren(nodeCT, "effects")) do
  339.         table.insert(aSorted, nodeChild);
  340.     end
  341.     table.sort(aSorted, function (a, b) return a.getName() < b.getName() end);
  342.  
  343.     for k,v in pairs(aSorted) do
  344.         if DB.getValue(v, "isactive", 0) == 1 then
  345.             if (not bSkipGMOnly and User.isHost()) or (DB.getValue(v, "isgmonly", 0) == 0) then
  346.                 local sLabel = DB.getValue(v, "label", "");
  347.                
  348.                 local sEffect = nil;
  349.                 local bSame = true;
  350.                 local sLastIcon = nil;
  351.  
  352.                 local aEffectComps = EffectManager.parseEffect(sLabel);
  353.                 for kComp,vComp in ipairs(aEffectComps) do
  354.                     -- CHECK CONDITIONALS
  355.                     if vComp.type == "IF" then
  356.                         if not EffectManager.checkConditional(rActor, v, vComp.remainder) then
  357.                             break;
  358.                         end
  359.                     elseif vComp.type == "IFT" then
  360.                         -- Do nothing
  361.                    
  362.                     else
  363.                         local sNewIcon = nil;
  364.                        
  365.                         -- CHECK FOR A BONUS OR PENALTY
  366.                         local sComp = vComp.type;
  367.                         if StringManager.contains(DataCommon.bonuscomps, sComp) then
  368.                             if #(vComp.dice) > 0 or vComp.mod > 0 then
  369.                                 sNewIcon = "cond_bonus";
  370.                             elseif vComp.mod < 0 then
  371.                                 sNewIcon = "cond_penalty";
  372.                             else
  373.                                 sNewIcon = "cond_generic";
  374.                             end
  375.                    
  376.                         -- CHECK FOR OTHER VISIBLE EFFECT TYPES
  377.                         else
  378.                             sNewIcon = DataCommon.othercomps[sComp];
  379.                         end
  380.                    
  381.                         -- CHECK FOR A CONDITION
  382.                         if not sNewIcon then
  383.                             sComp = vComp.original:lower();
  384.                             sNewIcon = DataCommon.condcomps[sComp];
  385.                         end
  386.                        
  387.                         if sNewIcon then
  388.                             if bSame then
  389.                                 if sLastIcon and sLastIcon ~= sNewIcon then
  390.                                     bSame = false;
  391.                                 end
  392.                                 sLastIcon = sNewIcon;
  393.                             end
  394.                         else
  395.                             if kComp == 1 then
  396.                                 sEffect = vComp.original;
  397.                             end
  398.                         end
  399.                     end
  400.                 end
  401.                
  402.                 if #aEffectComps > 0 then
  403.                     local sFinalIcon;
  404.                     if bSame and sLastIcon then
  405.                         sFinalIcon = sLastIcon;
  406.                     else
  407.                         sFinalIcon = "cond_generic";
  408.                     end
  409.                    
  410.                     local sFinalLabel;
  411.                     if sEffect then
  412.                         sFinalLabel = sEffect;
  413.                     else
  414.                         sFinalLabel = sLabel;
  415.                     end
  416.                    
  417.                     table.insert(aIconList, { sText = sFinalLabel, sIcon = sFinalIcon, sLabel = sLabel } );
  418.                 end
  419.             end
  420.         end
  421.     end
  422.    
  423.     return aIconList;
  424. end
  425.  
  426. function getWidgetList(tokenCT, sSubset)
  427.     local aWidgets = {};
  428.  
  429.     local w = nil;
  430.     if not sSubset or sSubset == "health" then
  431.         for _, vName in pairs({"healthbar", "healthdot"}) do
  432.             w = tokenCT.findWidget(vName);
  433.             if w then
  434.                 aWidgets[vName] = w;
  435.             end
  436.         end
  437.     end
  438.     if not sSubset or sSubset == "effect" then
  439.         for i = 1, TOKEN_MAX_EFFECTS do
  440.             w = tokenCT.findWidget("effect" .. i);
  441.             if w then
  442.                 aWidgets["effect" .. i] = w;
  443.             end
  444.         end
  445.     end
  446.    
  447.     return aWidgets;
  448. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement