Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ General ➜ Adding up #'s in a var from a script

Adding up #'s in a var from a script

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Chaosmstr   (21 posts)  Bio
Date Thu 06 Aug 2015 10:27 PM (UTC)

Amended on Thu 06 Aug 2015 10:30 PM (UTC) by Chaosmstr

Message
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..."
Top

Posted by Nick Gammon   Australia  (23,165 posts)  Bio   Forum Administrator
Date Reply #1 on Sat 08 Aug 2015 04:24 AM (UTC)
Message
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>

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Chaosmstr   (21 posts)  Bio
Date Reply #2 on Mon 10 Aug 2015 07:49 PM (UTC)
Message
Works like a charm.
Again, you are the hero of many, Nick.

Where do I send donations? :)
Top

Posted by Nick Gammon   Australia  (23,165 posts)  Bio   Forum Administrator
Date Reply #3 on Mon 10 Aug 2015 08:05 PM (UTC)
Message
There's a donate button on this page if you are keen:

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

:)

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


15,077 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.