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.
Entire forum
➜ MUSHclient
➜ Lua
➜ complex maths thing
Posting of new messages is disabled at present.
Refresh page
Pages: 1 2
Posted by
| Humiliation
(12 posts) Bio
|
Date
| Fri 13 Apr 2007 11:58 PM (UTC) Amended on Sat 14 Apr 2007 04:37 AM (UTC) by Humiliation
|
Message
| could someone help me with an alias/script i'm writing?
basically i want it to go:
@var1 ÷ (@var1 + @var2 + @var3 + @var4) x 300= @Avar1
Then the same for Var 2 3 and 4 and avar 2 3 and 4 respectively.
Thanks a bunch | Top |
|
Posted by
| Shaun Biggs
USA (644 posts) Bio
|
Date
| Reply #1 on Sat 14 Apr 2007 01:20 AM (UTC) |
Message
| That really depends on what you want the alias to be phrased as, what the variables are, how they are stored, and how many of them you want.
The easy way if you will always have 4 variables and you are just triggering on "foo * * * *" would be this:
temp = 300 / ( "%1" + "%2" + "%3" + "%4" )
print( "%1" * temp )
print( "%2" * temp )
print( "%3" * temp )
print( "%4" * temp )
|
It is much easier to fight for one's ideals than to live up to them. | Top |
|
Posted by
| Humiliation
(12 posts) Bio
|
Date
| Reply #2 on Sat 14 Apr 2007 04:52 AM (UTC) Amended on Sat 14 Apr 2007 05:27 AM (UTC) by Humiliation
|
Message
| How do you do it so instead of just printing it it puts it in a variable? I want to then use those variables.
And just to make it even more complicated i forgot to add after multiplying it by 300 if it is above 75 we minus 75 from it then minus that number from 75 and if it is below we minus it from 75 then add 75. And then round it to the nearest whole number. If that makes sense...
And thanks for the response | Top |
|
Posted by
| Shaun Biggs
USA (644 posts) Bio
|
Date
| Reply #3 on Sat 14 Apr 2007 06:00 AM (UTC) |
Message
| The variables part is easy.
temp = "%1" * foo
SetVariable("var1", temp )
It's retrieved later with GetVariable( "var1" )
Quote: And just to make it even more complicated i forgot to add after multiplying it by 300 if it is above 75 we minus 75 from it then minus that number from 75 and if it is below we minus it from 75 then add 75. And then round it to the nearest whole number. If that makes sense...
It might make sense if I tried to pull it apart more than I'm willing to do in the middle of the night after a few drinks. But the easier thing to do is to show you if statements. Scripting with Lua is very easy, and there are some nice tutorials here: http://lua-users.org/wiki/TutorialDirectory
if temp > 75 then
temp = 75 - temp
else
temp = 75 - temp + 75
end
temp = math.floor(temp + 5)
I think that's roughly what you were getting at. I'm still really confused about the 75 bit. The actual math you can probably handle after reading the tutorial. I can't seem to find a round function, so I just rounded down after adding a half. |
It is much easier to fight for one's ideals than to live up to them. | Top |
|
Posted by
| Nick Gammon
Australia (23,122 posts) Bio
Forum Administrator |
Date
| Reply #4 on Sat 14 Apr 2007 06:15 AM (UTC) |
Message
|
Quote:
temp = math.floor(temp + 5)
He means to add 0.5 - but that is quite a good way of rounding:
temp = math.floor(temp + 0.5)
Quote:
if it is below we minus it from 75 then add 75
...
temp = 75 - temp + 75
Yes, this looks a bit strange. It is identical mathematically to:
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Nick Gammon
Australia (23,122 posts) Bio
Forum Administrator |
Date
| Reply #5 on Sat 14 Apr 2007 06:54 AM (UTC) |
Message
| This actually raises an interesting point. How do you round negative numbers? For example, does -1.5 round to -2 or -1?
This simple function actually rounds it to -1:
function round (x)
return math.floor (x + 0.5)
end -- round
This slightly more complex round function rounds to the nearest number with "upwards" being to the absolute value.
function round (x)
if x >= 0 then
return math.floor (x + 0.5)
else
return math.ceil (x - 0.5)
end
end -- round
It might be a moot point, especially if your numbers are always positive, but it is an interesting question.
The second function rounds -1.5 to -2. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Humiliation
(12 posts) Bio
|
Date
| Reply #6 on Sat 14 Apr 2007 07:31 AM (UTC) |
Message
| Thank you so very much. I'm actually really impressed at how powerful lua - and mushclient - is.
Thanks again | Top |
|
Posted by
| ReallyCurious
USA (50 posts) Bio
|
Date
| Reply #7 on Sat 14 Apr 2007 07:41 AM (UTC) |
Message
| you should be impressed. this client rules. | Top |
|
Posted by
| Humiliation
(12 posts) Bio
|
Date
| Reply #8 on Sat 14 Apr 2007 08:45 AM (UTC) |
Message
| uhh got this error
[string "Alias: "]:2: attempt to perform arithmetic on global `foo' (a nil value)
stack traceback:
[string "Alias: "]:2: in main chunk
the alias is
<aliases>
<alias
match="foo * * * *"
enabled="y"
expand_variables="y"
send_to="12"
sequence="100"
>
<send>temp = 300 / ( "%1" + "%2" + "%3" + "%4" )
temp = "%1" * foo
SetVariable("var1", temp )
temp = "%2" * foo
SetVariable("var2", temp )
temp = "%3" * foo
SetVariable("var3", temp )
temp = "%4" * foo
SetVariable("var4", temp )
</send>
</alias>
</aliases>
If you could give me thw hole thing in one block it would make things easier for me. Thanks again | Top |
|
Posted by
| Humiliation
(12 posts) Bio
|
Date
| Reply #9 on Sat 14 Apr 2007 08:52 AM (UTC) |
Message
| Thank you so very much. I'm actually really impressed at how powerful lua - and mushclient - is.
Thanks again | Top |
|
Posted by
| ReallyCurious
USA (50 posts) Bio
|
Date
| Reply #10 on Sat 14 Apr 2007 09:15 AM (UTC) |
Message
| copy/paste an example from your mud of what you are trying to alias. | Top |
|
Posted by
| Humiliation
(12 posts) Bio
|
Date
| Reply #11 on Sat 14 Apr 2007 09:20 AM (UTC) Amended on Sat 14 Apr 2007 10:16 AM (UTC) by Humiliation
|
Message
| i cleared it all up but its super messy. that's beside the point but. Now var1 2 and 3 are getting negatives (which shouldn't be happening). Here it is...
<aliases>
<alias
match="foo"
enabled="y"
expand_variables="y"
send_to="12"
sequence="100"
>
<send>temp = 300 / ( "@offensive_skill" + "@daring_skill" + "@attack_skill" + "@Aim_skill" )
temp = "@offensive_skill" * temp
if temp > 75 then
temp = 75 - temp
else
temp = 150 - temp
end
temp = math.floor(temp + 0.5)
SetVariable("var1", temp )
temp = 300 / ( "@offensive_skill" + "@daring_skill" + "@attack_skill" + "@Aim_skill" )
temp = "@daring_skill" * temp
if temp > 75 then
temp = 75 - temp
else
temp = 150 - temp
end
temp = math.floor(temp + 0.5)
SetVariable("var2", temp )
temp = 300 / ( "@offensive_skill" + "@daring_skill" + "@attack_skill" + "@Aim_skill" )
temp = "@attack_skill" * temp
if temp > 75 then
temp = 75 - temp
else
temp = 150 - temp
end
temp = math.floor(temp + 0.5)
SetVariable("var3", temp )
temp = 300 / ( "@offensive_skill" + "@daring_skill" + "@attack_skill" + "@Aim_skill" )
temp = "@aim_skill" * temp
if temp > 75 then
temp = 75 - temp
else
temp = 150 - temp
end
temp = math.floor(temp + 0.5)
SetVariable("var4", temp )
</send>
</alias>
</aliases>
@offensive_skill = 41
@daring_skill = 35
@attack_skill = 37
@aim_skill = 20
The results i got were
@var1 = -17
@var2 = -4
@var3 = -8
@var4 = 105 | Top |
|
Posted by
| ReallyCurious
USA (50 posts) Bio
|
Date
| Reply #12 on Sat 14 Apr 2007 09:25 AM (UTC) |
Message
| Also.
I, as well, just started to learn LUA(2weeks so far) and here's some tips that I found helped me out...
Get familiar with regular expresions(regex). I printed out 2 pages worth of regex symbols and taped them to my wall. What used to be weird symbols and strange-looking words now start to make sense. The sooner you take to memory these things the faster you'll learn -> the faster you'll understand.
Be very specific with asking about what you want. Give exact examples of the output from your mud and what you want to do. In the last 2 weeks of my posting here on the forums 90% of the replies I got were from Nick and Shaun. They aren't here all the time so make sure you phrase your questions as simple as possible so that anyone else who happens to peruse your thread might want to help out.
Hope this helps, and good luck.
cheers
| Top |
|
Posted by
| Nick Gammon
Australia (23,122 posts) Bio
Forum Administrator |
Date
| Reply #13 on Sat 14 Apr 2007 10:49 AM (UTC) Amended on Sat 14 Apr 2007 10:50 AM (UTC) by Nick Gammon
|
Message
|
Quote:
Now var1 2 and 3 are getting negatives (which shouldn't be happening). Here it is...
...
temp = "@offensive_skill" * temp
if temp > 75 then
temp = 75 - temp
else
temp = 150 - temp
end
First, I wouldn't put "@offensive_skill" in quotes. It's a number, right? So if offensive_skill is 41, then you are saying:
temp = "41" * temp
You don't really multiply strings, you multiply numbers, so it should read:
temp = 41 * temp
I think Lua has converted it to a number for you, but you should be more precise with your use of strings and numbers.
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Nick Gammon
Australia (23,122 posts) Bio
Forum Administrator |
Date
| Reply #14 on Sat 14 Apr 2007 11:03 AM (UTC) |
Message
| As for the negative problem, printing intermediate results can help. I substituted your numbers to see what would happen:
temp = 300 / ( "41" + "35" + "37" + "20" )
print (temp) --> 2.2556390977444
temp = "41" * temp
print (temp) --> 92.481203007519
if temp > 75 then
temp = 75 - temp --> 75 - 92.481203007519 = -17.481203007519
else
temp = 150 - temp
end
print (temp) --> -17.481203007519
temp = math.floor(temp + 0.5)
print (temp) --> -17
It is doing what you told it to, the question is, what do you really want it to do? |
- 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.
60,280 views.
This is page 1, subject is 2 pages long: 1 2
Posting of new messages is disabled at present.
Refresh page
top