Complex math within an alias

Posted by Aralia Faal on Wed 10 Oct 2012 04:45 AM — 10 posts, 37,388 views.

#0
Alright so I want to make an alias that takes two variables, @strength and @constitution, and adds them together then divides that by 10 then divides that by 2. Once the math is done it would SendImmediate (train %1 ##). Where ## is the outcome of the math.

I tried doing this on my own, starting with:

bc.digits (20)
n = bc.number (((@strength + @constitution) / 10) / 2)
print(n)

Witch worked so I changed print(n) to SendImeediate(n) which also worked. But the moment I tried adding anything before the n within the ( ) it failed.

Could I please get some help?
Australia Forum Administrator #1
Please show the whole thing:

Template:copying
For advice on how to copy aliases, timers or triggers from within MUSHclient, and paste them into a forum message, please see Copying XML.
#2
The capture trigger:


<aliases>
  <alias
   match="train *"
   enabled="y"
   expand_variables="y"
   send_to="12"
   sequence="100"
  >
  <send>bc.digits (20)
n = bc.number (((@strength + @constitution) / 10) / 2)
SendImmediate(n)</send>
  </alias>
</aliases>


The Variable:


<variables>
  <variable name="Constitution">578</variable>
</variables>

<variables>
  <variable name="Strength">496</variable>
</variables>
Amended on Wed 10 Oct 2012 01:47 PM by Aralia Faal
Australia Forum Administrator #3
Why are you using bc? That is for, like, hundreds of digits of precision.
USA Global Moderator #4
Quote:
the moment I tried adding anything before the n within the ( ) it failed.

Please show what you tried doing that failed.
#5
Nick Gammon said:

Why are you using bc? That is for, like, hundreds of digits of precision.


Because I'm not all that good with the functions and it was the first thing I found that began to work for what I wanted.

Fiendish said:

Quote:
the moment I tried adding anything before the n within the ( ) it failed.

Please show what you tried doing that failed.


I first started with what I wanted it to be:

<aliases>
  <alias
   match="train *"
   enabled="y"
   expand_variables="y"
   send_to="12"
   sequence="100"
  >
  <send>bc.digits (20)
n = bc.number (((@strength + @constitution) / 10) / 2)
SendImmediate(train %1 (n))</send>
  </alias>
</aliases>


I got the error:

Error number: 0
Event: Compile error
Description: [string "Alias: "]:3: ')' expected near 'str'
Called by: Immediate execution

I removed the %1 for now, using str, to see if I could get the "n" to work. But it just sends "train str (n)" exactly as it is.
Australia Forum Administrator #6
It would be more like:


Send ("train %1 " .. n)


One is a literal, one is a variable.





bc.digits (20)
n = bc.number (((@strength + @constitution) / 10) / 2)


can be just:


n = ((@strength + @constitution) / 10) / 2)
#7
Awsome! Can always count on you.

Is there a way to make it round up to the next whole number?
Australia Forum Administrator #8
http://www.gammon.com.au/modules

Inside commas.lua is a round function.

eg.


require "commas"

n = round (n)

#9
Thanks. You are a big help, as always.