| Message |
As for converting that script to Lua, this is what I did:
- Copied and pasted the whole thing into one of MUSHclient's notepad windows.
- I fixed the capitalization of function names, which Lua requires to be correct, by using the Search menu -> Global Replace (Ctrl+Shift+G), as described in the forum post "How to fix capitalization of function names in scripts":
http://www.gammon.com.au/forum/?id=6211
That is:
Find Pattern: [%a%d]+
Replacement: f
Call function: checked
Script:
t1 = utils.functionlist ()
t2 = {}
-- keys are lower-case functions, values are original functions
table.foreach (t1, function (k, v) t2 [string.lower (v)] = v end )
function f (str)
return t2 [string.lower (str)] or str
end -- function f
This changed things like world.getvariable to world.GetVariable
- I fixed up "if" statements by doing a global replace of "end if" to "end -- if"
- The word "world." is a bit redundant, so I changed "world." to nothing. So in other words:
becomes:
- As Onoitsu2 pointed out, generally speaking Lua function calls have to be in brackets. In particular you were doing a lot of SetVariable without the brackets. So I used the Search menu -> Global Replace to do another big cleanup:
Find Pattern: SetVariable%s*(.+)
Replacement: SetVariable (%1)
Call function: NOT checked
This changes, for example:
SetVariable "truelock", "1"
to:
SetVariable ("truelock", "1")
- To define local variables Lua uses "local" rather than "dim" so I changed:
to
- Lua uses == to compare for equality, not =, so another fixup using Search menu -> Global Replace was required:
Find Pattern: GetVariable%s*%(%s*(.-)%s*%)%s*=%s*(%d+)
Replacement: GetVariable (%1) == "%2"
Call function: NOT checked
After running this lines like this:
if world.GetVariable ("erebound")=0 and world.getvariable ("hasshield")=0 then
changed to:
if GetVariable ("erebound") == "0" and GetVariable ("hasshield") == "0" then
Note also I quoted the number you are comparing to, as Lua considers "0" and 0 to be different. One is a string and one is a number.
- I wanted to make the long "if" statements a bit easier to read, so another Search menu -> Global Replace was used to break the long if statements after the "and", like this:
Find Pattern: and
Replacement: and\n
Call function: NOT checked
Escape sequences: checked
The find pattern is actually: (space)and(space)
The replace pattern is actually: (space)and\n(space)(space)(space)
I mention that because the spaces are hard to see.
This adds a line break after each "and".
- For neatness I also added a blank line after each if:
Find Pattern: end %-%- if
Replacement: end -- if\n
Call function: NOT checked
Escape sequences: checked
- I also wanted to indent the inside of an "if" like this:
Find Pattern: ^Execute
Replacement: Execute
Call function: NOT checked
(That is 6 spaces before "Execute" in the replacement box).
Ditto for the words "SetVariable" and "Send".
A couple of manual cleanups of spacing, and this is the finished result:
if GetVariable ("erebound") == "0" and
GetVariable ("hasshield") == "0" then
Execute "ds"
end -- if
if GetVariable ("erebound") == "1" and
GetVariable ("hasshield") == "1" then
Execute "rs"
end -- if
if GetVariable ("erebound") == "1" and
GetVariable ("hasshield") == "0" then
Execute "rs"
end -- if
if GetVariable ("erebound") == "0" and
GetVariable ("hasshield") == "1" then
Execute "rs"
end -- if
SetVariable ("health", "%1")
SetVariable ("mana", "%2")
local curhp
local curmp
curhp=%1
curmp=%2
if curhp < 2800 and
GetVariable ("moss") == "1" and
GetVariable ("dead") == "0" and
GetVariable ("stun") == "0" and
GetVariable ("sleeping") == "0" and
GetVariable ("anorexia") == "0" then
Execute "emo"
SetVariable ("moss", "0")
end -- if
if curhp < 3300 and
GetVariable ("elixir") == "1" and
GetVariable ("dead") == "0" and
GetVariable ("stun") == "0" and
GetVariable ("sleeping") == "0" and
GetVariable ("belonephobia") == "0" and
GetVariable ("addiction") == "0" then
Execute "sh"
SetVariable ("elixir", "0")
end -- if
if curmp < 2500 and
curhp > 2900 and
GetVariable ("elixir") == "1" and
GetVariable ("dead") == "0" and
GetVariable ("stun") == "0" and
GetVariable ("sleeping") == "0" and
GetVariable ("belonephobia") == "0" and
GetVariable ("addiction") == "0" then
Execute "sm"
SetVariable ("elixir", "0")
end -- if
if GetVariable ("illusion") == "1" then
Execute "resettemps"
end -- if
Execute "setaff"
if GetVariable ("indifference") == "1" and
GetVariable ("salve") == "1" and
GetVariable ("dead") == "0" and
GetVariable ("slickness") == "0" and
GetVariable ("sleeping") == "0" and
GetVariable ("stun") == "0" and
GetVariable ("aeon") == "0" and
GetVariable ("illusion") == "0" then
Send "press oculi"
SetVariable ("salve", "0")
end -- if
if GetVariable ("stupidity") == "1" and
GetVariable ("dead") == "0" and
GetVariable ("illusion") == "0" and
GetVariable ("herb") == "1" and
GetVariable ("sleeping") == "0" and
GetVariable ("belonephobia") == "0" and
GetVariable ("aeon") == "0" and
GetVariable ("indifference") == "0" and
GetVariable ("stun") == "0" then
Send "outc 2 liver slice"
Send "eat liver slice"
Send "inc liver slice"
end -- if
if GetVariable ("paralysis") == "1" and
GetVariable ("dead") == "0" and
GetVariable ("illusion") == "0" and
GetVariable ("stupidity") == "0" and
GetVariable ("herb") == "1" and
GetVariable ("sleeping") == "0" and
GetVariable ("belonephobia") == "0" and
GetVariable ("aeon") == "0" and
GetVariable ("indifference") == "0" and
GetVariable ("stun") == "0" then
Send "outc lung slice"
Send "eat lung slice"
SetVariable ("herb", "0")
end -- if
if GetVariable ("limp_veins") == "1" and
GetVariable ("dead") == "0" and
GetVariable ("balance") == "1" and
GetVariable ("aeon") == "0" then
Send "Fitness"
SetVariable ("balance", "0")
SetVariable ("fitness", "1")
end -- if
if GetVariable ("slickness") == "1" and
GetVariable ("stun") == "0" and
GetVariable ("dead") == "0" and
GetVariable ("limp_veins") == "0" and
GetVariable ("sleeping") == "0" and
GetVariable ("illusion") == "0" then
Execute "ia"
end -- if
if GetVariable ("belonephobia") == "1" and
GetVariable ("salve") == "1" and
GetVariable ("dead") == "0" and
GetVariable ("slickness") == "0" and
GetVariable ("sleeping") == "0" and
GetVariable ("stun") == "0" and
GetVariable ("aeon") == "0" and
GetVariable ("illusion") == "0" then
Send "press oculi on torso"
SetVariable ("salve", "0")
end -- if
if GetVariable ("fsleep") == "1" and
GetVariable ("illusion") == "0" and
GetVariable ("sleeping") == "1" and
GetVariable ("aeon") == "0" then
Execute "wake"
else
if GetVariable ("insomnia") == "0" and
GetVariable ("fsleep") == "0" and
GetVariable ("balance") == "1" and
GetVariable ("equilibrium") == "1" and
GetVariable ("sleeping") == "0" and
GetVariable ("fighting") == "1" then
Send "insomnia"
end -- if
end -- if
if GetVariable ("limp_veins") == "1" and
GetVariable ("slickness") == "1" and
GetVariable ("belonephobia") == "1" and
GetVariable ("paralysis") == "1" and
GetVariable ("dead") == "0" and
GetVariable ("stun") == "0" and
GetVariable ("sleeping") == "0" and
GetVariable ("aeon") == "0" and
GetVariable ("illusion") == "0" and
GetVariable ("balance") == "1" then
Send "fitness"
Execute "ia"
else
if GetVariable ("limp_veins") == "1" and
GetVariable ("slickness") == "1" and
GetVariable ("belonephobia") == "1" and
GetVariable ("paralysis") == "1" and
GetVariable ("confusion") == "1" and
GetVariable ("frozen") == "1" then
SetVariable ("truelock", "1")
end -- if
end -- if
if GetVariable ("broken_larm") == "1" and
GetVariable ("broken_rarm") == "1" and
GetVariable ("stun") == "0" and
GetVariable ("illusion") == "0" and
GetVariable ("dead") == "0" and
GetVariable ("sleeping") == "0" and
GetVariable ("aeon") == "0" and
GetVariable ("equilibrium") == "1" then
Execute "restore"
end -- if
if GetVariable ("stun") == "0" and
GetVariable ("illusion") == "0" and
GetVariable ("dead") == "0" and
GetVariable ("herb") == "1" and
GetVariable ("sleeping") == "0" and
GetVariable ("belonephobia") == "0" and
GetVariable ("aeon") == "0" and
GetVariable ("indifference") == "0" then
SetVariable ("ccslice", "1")
else
SetVariable ("ccslice", "0")
end -- if
if GetVariable ("salve") == "1" and
GetVariable ("dead") == "0" and
GetVariable ("slickness") == "0" and
GetVariable ("sleeping") == "0" and
GetVariable ("stun") == "0" and
GetVariable ("aeon") == "0" and
GetVariable ("illusion") == "0" then
SetVariable ("ccpoultice", "1")
else
SetVariable ("ccpoultice", "0")
end -- if
if GetVariable ("stun") == "0" and
GetVariable ("dead") == "0" and
GetVariable ("moss") == "1" and
GetVariable ("sleeping") == "0" and
GetVariable ("anorexia") == "0" and
GetVariable ("aeon") == "0" and
GetVariable ("illusion") == "0" then
SetVariable ("ccmoss", "1")
else
SetVariable ("ccmoss", "0")
end -- if
if GetVariable ("stun") == "0" and
GetVariable ("dead") == "0" and
GetVariable ("limp_veins") == "0" and
GetVariable ("sleeping") == "0" and
GetVariable ("illusion") == "0" then
SetVariable ("ccsyringe", "1")
else
SetVariable ("ccsyringe", "0")
end -- if
if GetVariable ("freedom") == "0" and
GetVariable ("dead") == "0" and
GetVariable ("stun") == "0" and
GetVariable ("writhe") == "0" and
GetVariable ("sleeping") == "0" and
GetVariable ("illusion") == "0" then
Execute "CureWrithe"
end -- if
if GetVariable ("possessed") == "1" and
GetVariable ("stun") == "0" and
GetVariable ("frozen") == "0" and
GetVariable ("dead") == "0" and
GetVariable ("balance") == "1" and
GetVariable ("sleeping") == "0" and
GetVariable ("aeon") == "0" and
GetVariable ("illusion") == "0" then
Send "lose soulmaster"
end -- if
if GetVariable ("stun") == "0" and
GetVariable ("sleeping") == "0" and
GetVariable ("frozen") == "0" and
GetVariable ("dead") == "0" and
GetVariable ("fear") == "1" and
GetVariable ("aeon") == "0" and
GetVariable ("illusion") == "0" then
Send "Compose"
end -- if
if GetVariable ("prone") == "1" and
GetVariable ("stun") == "0" and
GetVariable ("dead") == "0" and
GetVariable ("sleeping") == "0" and
GetVariable ("aeon") == "0" and
GetVariable ("balance") == "1" and
GetVariable ("broken_lleg") == "0" and
GetVariable ("broken_rleg") == "0" and
GetVariable ("paralysis") == "0" and
GetVariable ("freedom") == "1" and
GetVariable ("illusion") == "0" then
Send "stand"
end -- if
if GetVariable ("freedom") == "1" and
GetVariable ("dead") == "0" and
GetVariable ("stun") == "0" and
GetVariable ("writhe") == "0" and
GetVariable ("sleeping") == "0" and
GetVariable ("aeon") == "0" and
GetVariable ("illusion") == "0" then
Execute "CureAll"
end -- if
I hope that gives the general flavour of the "before" and "after". If you have hundreds of similar aliases you could write a script that effectively automated those steps. If you only have a dozen it shouldn't take too long to do them by hand. You could copy and paste them all into one notepad file (separated by hyphens or something) and do each step on the lot as a batch.
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | top |
|