Adding up #'s in a var from a script

Posted by Chaosmstr on Thu 06 Aug 2015 10:27 PM — 4 posts, 17,432 views.

#0
I found code in this thread:

http://mushclient.com/forum/bbshowpost.php?id=7669

titled "Matching a trigger multiple times on the same line" and I got it to work on my trigger text in that it shows me on each line what I'm looking for.

The code for the trigger and script is:

<triggers>
  <trigger
   enabled="y"
   expand_variables="y"
   keep_evaluating="y"
   match="(int:|wis:|dex:|str:|chr:|con:|luc:|dmg:|hr:|da:|ma:|svs:)(\D*\d+)"
   name="additup"
   regexp="y"
   repeat="y"
   script="attrib_trigger_script"
   send_to="12"
   sequence="100"
  >
  </trigger>
</triggers>


function attrib_trigger_script (name, line, wildcards)
 
  -- function to handle individual matches
  local function one_attrib (m, t)
  --  print ("count", t [1])
  --  print ("attrib",  t [2])
	SetVariable (t [1], t [2])
  end -- one_herb 

  local re = rex.new (GetTriggerInfo (name, 1))  -- get the match text, make into a regexp
  re:gmatch (line, one_attrib)  -- match repeatedly, call function

end -- attrib_trigger_script  



What I want to do it take the triggered name (t [1]) and set a variable by that name (int, wis, dex...) and add the # from (t [2]) to the variable.

So, when I look at myself and see the gear I'm wearing it will add up all the int or dex or dmg in each variable so I can then show a total.

For instance:

<used as light>     (Moderate magic) (Glowing) a war banner [gm|ac:-2 svs:-2 1#]
<worn on finger>    (Moderate magic) the radiant zircon ring [m|AC 0 hr:3 2#]
<worn on body>      (Weak magic) a dragon scale shirt [m|AC 12 ma:5 16#]
<worn on head>      (Moderate magic) an Atheist's sensibilities [m|AC 6 int:3 int:3 1#]
<worn on legs>      (Moderate magic) a pair of heavy cloth commoner's pants [mAN|AC 5 str:3 2#]
<worn on feet>      (Moderate magic) a pair of heavy cloth slippers [m|AC 5 dmg:2 con:2 1#]
<worn on hands>     (Potent magic) a pair of quilted cloth formal gloves [m|AC 6 wis:5 1#]
<worn on arms>      (Moderate magic) a pair of leather sleeves [m|AC 4 wis:2 wis:2 2#]
<worn about body>   (Moderate magic) the Crest of Cormac [!m|AC 6 dmg:2 svs:-3 5#]
<worn about waist>  (Moderate magic) (Woven) a nine-stranded embroidered belt [m|AC 4 dmg:3 1#]
<worn around wrist> (Moderate magic) the grimy adamantite bracelet [m|AC 0 int:1 hr:2 0#]
<worn around wrist> (Moderate magic) a storm-blue mithril wristguard [m|AC 0 dmg:1 str:3 3#]
<wielded>           (Powerful magic) a steel short sword [mAEAN|7-11 hr:5 dmg:4 5#]
<held>              (Moderate magic) a black bone staff [!mAGAN|svs:-4 5#]
<worn with pride>   (Weak magic) a Crest bearing the Midgaard Coat of Arms [m|svs:-1 3#]
<worn with pride>   a cloth baldric, draped over the shoulder


would allow me to print the totals with an alias.

"You are wearing int=9, con=4, svs=-8, dmg=9..."
Amended on Thu 06 Aug 2015 10:30 PM by Chaosmstr
Australia Forum Administrator #1
Well, I would change the script to:


function attrib_trigger_script (name, line, wildcards)
  
  -- function to handle individual matches
  local function one_attrib (m, t)
    local name = "attrib_" .. string.gsub (t [1], ":", "")
    local value = tonumber (t [2])
    SetVariable (name, tonumber (GetVariable (name) or 0) + value)
  end -- one_herb 

  local re = rex.new (GetTriggerInfo (name, 1))  -- get the match text, make into a regexp
  re:gmatch (line, one_attrib)  -- match repeatedly, call function
  
end -- attrib_trigger_script  


Now it should add to the relevant variables. But you can't handle the totals there, because this is called for every line.

So I think you need another function:


function start_of_equip_list (name, line, wildcards)

  for k, v in pairs (GetVariableList()) do 
    if string.match (k, "^attrib_") then
      DeleteVariable (k)
    end -- one of the attrib_* variables
  end

end -- start_of_equip_list


And another trigger:


<triggers>
  <trigger
   enabled="y"
   match="You are using:"
   script="start_of_equip_list"
   sequence="100"
  >
  </trigger>
</triggers>


What that will do is delete all the variables starting with "attrib_" in the variable list (effectively zeroing them) when it sees a line with:


You are using:


Modify that to be exactly what appears at the start of your equipment list.

Now, you can make an alias to print all the variables, for example:


function show_equip_list (name, line, wildcards)

  for k, v in pairs (GetVariableList()) do 
    if string.match (k, "^attrib_") then
      print (k, v)
    end -- one of the attrib_* variables
  end

end -- show_equip_list



<aliases>
  <alias
   script="show_equip_list"
   match="show attribs"
   enabled="y"
   sequence="100"
  >
  </alias>
</aliases>
#2
Works like a charm.
Again, you are the hero of many, Nick.

Where do I send donations? :)
Australia Forum Administrator #3
There's a donate button on this page if you are keen:

http://www.gammon.com.au/mushclient/mushclient.htm

:)