Converting zMUD math to MUSH math

Posted by WHD on Thu 19 Nov 2009 11:16 PM — 3 posts, 18,389 views.

#0
How would I go about converting this zMUD script sample to MUSH?

#ALIAS moss {#math moss {%1 *40};#show @moss}

The way this particular line works is you enter the amount of moss you want to price and it does the math. So if you entered "moss 5" it would output "200" in the output window.

I plan on converting a zMUD calculator script to MUSH, and there are 34 lines just like this one that would need to be converted, plus a line that would total the 34 results and another that would clear all the inputs (resetting all the price totals back to 0).

Thanks for your help!
USA #1
A literal translation would look like this (assuming Lua as the selected scripting language):


<aliases>
  <alias
   match="moss *"
   enabled="y"
   send_to="12"
   ignore_case="y"
   sequence="100"
  >
  <send>SetVariable("moss", %1 * 40)
Note(GetVariable("moss"))</send>
  </alias>
</aliases>


(This is in the XML format, just copy it and click the Paste button on the Aliases dialog to load it.)

A more 'native' translation might look like this, though:


<aliases>
  <alias
   match="moss *"
   enabled="y"
   send_to="12"
   ignore_case="y"
   sequence="100"
  >
  <send>-- temporarily store the value as a local
local num = tonumber("%1")

if num then
  -- moss is likely defined elsewhere as a global
  moss = %1 * 40
  Note(moss)
else
  Note("Must be a number.")
end</send>
  </alias>
</aliases>


Here I added some error checking, and used Lua variables rather than MUSHclient's variable storage, because MUSH variables can only contain strings, plus they're relatively more efficient. I could have used a regular expression for the alias and limited the capture to only numbers, too.
Amended on Thu 19 Nov 2009 11:53 PM by Twisol
#2
I'd do this and maybe store the prices for items in MUSH Variables.

This trigger is catching a line that has the price of moss on it and then storing it for later.
<triggers>
  <trigger
   enabled="y"
   keep_evaluating="y"
   match="The price of moss is: *"
   send_to="12"
   sequence="100"
  >
<send>
moss_price = tonumber("%1")
</send>
  </trigger>
</triggers>

This alias is for moss. It uses the price stored in moss_price to work out the cost and display the info then store it for later.
<aliases>
  <alias
   match="moss *"
   enabled="y"
   expand_variables="y"
   send_to="12"
   sequence="100"
  >
<send>

--make the table for the first time
if not totals then
    totals = {}
end

--make sure we have the price
if not moss_price then
    Note("no moss_price")
    return
end

--do the math
quant = tonumber("%1")
moss_total = quant * moss_price

--add this item to the totals table
totals.moss = moss_total

--now print out our moss info

--make a nice formated output string
output_string = string.format("%i x moss at %i will cost %i",  quant, moss_price, moss_total)

--print the formated string to the command window
Note(output_string)

</send>
  </alias>
</aliases>

Now I can get the totals with this alias when ever I want.
<aliases>
  <alias
   match="totals"
   enabled="y"
   expand_variables="y"
   send_to="12"
   sequence="100"
  >
<send>

--make a var to hold the total
total = 0

--loop the totals table adding it up
for key, value in pairs(totals) do
    total = total + value
    Note(key.." total: "..value) --print each total
end
Note("The total cost is: "..total)
</send>
  </alias>
</aliases>


I'd then follow the info bellow and make a plugin for it all.
Template:post=9617
Please see the forum thread: http://gammon.com.au/forum/?id=9617.