local fontWidth, fontHeight = calcFontSize( fontSize )
local main = ui_main * width / fontWidth -- here ui_main is percentage of width actual game content display, this calculation is the number of characters in the main window (unsure if there's a faster way to do it)
local main_offset = 16 - string.len(name)
local str = string.format( "%s%"..main_offset.."s ", name, "::" )
I've forgotten, like, all of my lua and other scripting skills.
Is there a primer on... 1. Syntax for sending commands and using embedded variables and so-on. 2. Structure of if statements in lua. 3. The syntax for setting variables, including default variables. 4. A good summary of how to use tables. 5. Regex!!
Is there a primer on... 1. Syntax for sending commands and using embedded variables and so-on. 2. Structure of if statements in lua. 3. The syntax for setting variables, including default variables. 4. A good summary of how to use tables. 5. Regex!!
Tables can be dictionary (key-value) or list (indexed)
Key-Value example
my_table = {
Iriaen = "awesome",
["Iniar"] = "scrub",
}
my_table["Iriaen"] yields "awesome", (as does my_table.Iriaen)
my_table[1] yields nothing
Indexed table,
my_table = {
"Iriaen",
"Iniar",
}
my_table[1] yields "Iriaen"
my_table[2] yields "Iniar"
Sending commands
============
send("rt Iriaen is awesome")
or
local str, target = "rt ", "Iriaen"
str = str..target.." is awesome"
send(str)
yields the exact same thing
Concatenation:
The above is a sample of concatenation.
Most wordy variables are string.
stringn = "Apple juice is sweeter than orange juice"
can be constituted like so,
juice_one = "Apple"
juice_two = "orange"
stringn = juice_one.." juice is sweeter than "..juice_two.." juice"
Handy tip,
function p_send( arg )
if pause == 1 then
-- do nothing
else
send( arg )
end
end
creates a function p_send("stuff") that lets you pause it, then simply use send("stuff") for things you don't wish to ever pause
The IF statement
===========
if true then execute else do_other end
if target == "Iriaen" then send("kick Iriaen") end
Useful comparators
auto_redef = true, (or even auto_redef = 1)
if auto_redef then send("touch owl") end
will work.
if type(target) == "string" then ...
if target_hp > 100 then ...
if table.contains( my_table, target ) then ... or syntaxially,
if my_table:contains( target ) then...
Iterators
=====
For key-value tables, we loop like this;
for k, v in pairs(my_table) do
display(tostring(k))
display(v)
end
For indexed tables, we loop like this;
for i=1, #my_table do
echo( i..": "..my_table[i] )
if i > 5 then break end
end
Regex
====
My two/three favourite capture patterns
(.*) <- everything
(\w+) <- single word
(\d+) <- digit
remember if you capture (\d+), you'll want to do,
tonumber( thecapture )
before doing
if thecapture > 100 then ...
as it -can- sometimes run as a string (well, it shouldn't but don't let it f you over)
to use the captures, do..
example:
pattern: ^(\w+) kisses (\w+) gently on the lips\.$
matches[1] will be:
Hastati kisses Svorai gently on the lips.
matches[2] will be:
"Hastati"
matches[3] will be:
"Svorai"
For a multiline capture;
Pattern:
^(\w+) kisses (\w+) gently on the lips\.
^(\w+) slaps (\w+) with some vim and vigour\.
multimatches[2][3] will match the fourth (\w+), that is if the second line 'Svorai slaps Iniar with some vim and vigour', then multimatches[2][3] == "Iniar"
Iriaen here (this is going to be my new character, avatar could use some work), I want to say thank you very much Iniar! I remember that I used to get by doing some fairly complex things in mudlet or IMTS despite knowing only a small number of core scripting techniques, so your posts are exactly the kind of thing I was looking for. I'm sure I will be referring back to these posts of yours many times ^:)^
One of my favorite things is table.contains (table_name, variable) for checking certain lines.
Line: ^(\w+) raises a (.+) high above (?:his|her) head.$
Script: if not table.contains(allies, matches[2]) then send("sweep "..matches[2]) end
You people and your complicated and unnecessary lua meta functions.
if (not allies[variable]) then is so much easier, and doesn't require a for loop.
That's useful, to do something like that in stock lua you'd need to write a nested for loop.
It does make me curious though - in what possible situation in Imperian would your code be so disorganized that you would need to search through multiple nested tables for a variable?
Why on earth would you want so many nested tables? Why on earth would you want to traverse every element of every table searching for something?
"On the battlefield I am a god. I love war. The steel, the smell, the corpses. I wish there were more. On the first day I drove the Northmen back alone at the ford. Alone! On the second I carried the bridge! Me! Yesterday I climbed the Heroes! I love war! I… I wish it wasn’t over."
Is there a better way to do something like:
if affliction == ("anorexia" or "clumsiness" or "stupidity") then send("something") end
So I don't have to do that in separate clauses, such as affliction == "anorexia" or affliction == "clumsiness", since the prior doesn't work it seems.
Is there a better way to do something like:
if affliction == ("anorexia" or "clumsiness" or "stupidity") then send("something") end
So I don't have to do that in separate clauses, such as affliction == "anorexia" or affliction == "clumsiness", since the prior doesn't work it seems.
Hey all, just wanted to say I bought a clan and am inviting anyone who wishes to join it for mudlet related questions/talk. You can hit me up in game if you want an invite, and like I said, everyone is welcome.
Comments
string.match(gmcp.Char.Status.level, "^%d+")
Seems to capture the 'level' how would I go about capturing the XP of what's in that GMCP??
string.lpad = function(str, len, char)
if char == nil then char = ' ' end
return str .. string.rep(char, len - #str)
end
width, height = getMainWindowSize()
local fontSize = 11
local fontWidth, fontHeight = calcFontSize( fontSize )
local main = ui_main * width / fontWidth -- here ui_main is percentage of width actual game content display, this calculation is the number of characters in the main window (unsure if there's a faster way to do it)
local main_offset = 16 - string.len(name)
local str = string.format( "%s%"..main_offset.."s ", name, "::" )
Is there a primer on...
1. Syntax for sending commands and using embedded variables and so-on.
2. Structure of if statements in lua.
3. The syntax for setting variables, including default variables.
4. A good summary of how to use tables.
5. Regex!!
Thanks guys!
http://www.mudlet.org/asciidoc/manual.html
http://www.regular-expressions.info/tutorial.html
Lots of information gathered here.
1. Syntax for sending commands and using embedded variables and so-on.
2. Structure of if statements in lua.
3. The syntax for setting variables, including default variables.
4. A good summary of how to use tables.
5. Regex!!
matches[2] = string.gsub( matches[2], "+n", "\n" )
feedTriggers("\n" .. matches[2] .. "\n")
echo("\n")
Invoked like so;
`echo A pleasant warmth soothes your body and mind.
Secondly, this piece of code lets you run Lua code from the command line;
Alias:
^lua (.*)$
Code:
local f,e = loadstring("return "..matches[2])
if not f then
f,e = assert(loadstring(matches[2]))
end
local r = f()
if r ~= nil then display(r) end
To invoke it, type into your command line;
lua local str = "Iriaen is awesome" display(str) local tablen = {"Apples","Oranges"} display(tablen)
will run the code as you have it written
Line: ^(\w+) raises a (.+) high above (?:his|her) head.$
Script: if not table.contains(allies, matches[2]) then send("sweep "..matches[2]) end
"On the battlefield I am a god. I love war. The steel, the smell, the corpses. I wish there were more. On the first day I drove the Northmen back alone at the ford. Alone! On the second I carried the bridge! Me! Yesterday I climbed the Heroes! I love war! I… I wish it wasn’t over."
If table.contains( exclude, affliction ) then
-- do awesome
end
Did you try telling them to turn it off and back on again?