Skip to content

Mudlet Scripting

13468918

Comments

  • p, li { white-space: pre-wrap; }

    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??

  • IniarIniar Australia
    string.match(gmcp.Char.Status.level, "%d+%.%d+%%")

    works, kinda

    this: string.match(gmcp.Char.Status.level, "[.%d]+%%")
    wit beyond measure is a Sidhe's greatest treasure
  • thanks, I figured that part out but wanted it just the number and not the % on the end, :P Thank tho
  • Does anyone happen to be good with 'Alignment' and formatting, Im having issues and would love help if anyone is? Design UI stuff is my weak point :(
  • IniarIniar Australia
    edited February 2014
    image


    Code needs cleaning but lazy/should go do work. :(
    wit beyond measure is a Sidhe's greatest treasure
  • MathiausMathiaus Pennsylvania
    I use a lot of string.len(variable length) and string.rep(repetition with spaces) within functions to line stuff up.
    image
  • IniarIniar Australia
    these snippets avoids a lot hard coding lengths

    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, "::" )

    wit beyond measure is a Sidhe's greatest treasure
  • edited February 2014
    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!!

    Thanks guys!
    Drill baby drill because luck is a skill.
  • IniarIniar Australia
    edited February 2014
    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!!

    Variables
    ======
    Global assignment is simply done as such:
    my_variable = "Iriaen"
    For a dynamically named variable, do as such:
    targeted_limb = "right leg"
    _G[targeted_limb.."_count"] = _G[targeted_limb.."_count"] + 1
    Alternatively, I believe you can do (but always hazy on the details):
    assert([["right_leg_count" = 3]])
    One practice I find useful is doing this:
    my_taint = my_taint or 0
    This ensures every time I modify my script, I don't re-assign my_taint to 0.
    Avoid assigning variable names like 'table', 'string' that are used by the global table.
    _G is the global table (if it wasn't clear already).

    Tables
    ====
    this_table = {}
    this_table = {
    pet = 49130,
    ["right leg"] = 3,
    gurrenlagaan = "legendary",
    (don't ignore the commas)
    table.insert( this_table, "apple" )
    table.remove( this_table, 1 ) <- removes element 1
    table.remove( this_table, table.index_of( this_table, "apple" ) )

    this can be syntaxially shortened to:
    this_table:remove( 1 )

    You can retrieve values by:
    short = my_table.target
    Table assignment and retrieval syntax is funny;
    my_table.target (is exactly the same as) 
    my_table["target"] (but)

    my_table["target"] (is not the same as) 
    my_table[target]

    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"

    Hope that helps.
    wit beyond measure is a Sidhe's greatest treasure
  • IniarIniar Australia
    Two of the most useful pieces of code:

    This next alias lets you test trigger;
    Alias: 
    `echo (.*)$

    Code:

    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

    wit beyond measure is a Sidhe's greatest treasure
  • 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 ^:)^
  • IniarIniar Australia
    Excited to see your code!
    wit beyond measure is a Sidhe's greatest treasure
  • @Iniar, I'm not sure how I feel about your examples. :(
  • IniarIniar Australia
    Me neither. :(
    wit beyond measure is a Sidhe's greatest treasure
  • MathiausMathiaus Pennsylvania
    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
    image
  • you know table.isMember works better then contaains right?
  • Mathiaus said:
    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.
  • IniarIniar Australia
    edited February 2014
    That's generally faster except when say:

    tablen = {
       open = { "source" },
       afflictions = { "numbness", "asthma" },
       likely_cured = { "hemotoxin", "metrazol" },
       giga = { meter = { "earthquake" }, "orange" },
    }

    you can do table.contains( tablen, "hemotoxin" ) or even table.contains( tablen, "earthquake" )

    and it will traverse all elements of the parent table, even keys

    so even table.contains( tablen, "meter" ) would return true

    E:Also, my _G table doesn't contain a table.isMember, Nyoka.
    wit beyond measure is a Sidhe's greatest treasure
  • 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."

  • IniarIniar Australia
    edited February 2014
    Was just trying to think of a practical example and most things are readily handled with a static declaration! :P

    I guess the only instance of a useful table.contains in my scripts is:

    if table.contains( gmcp.Char.Skills.Groups, "Shamanism" ) then...

    So searching through an indexed table (of tables) would be handy.

    E: Also, my code is like my brain -> disorganized!
    wit beyond measure is a Sidhe's greatest treasure
  • MathiausMathiaus Pennsylvania
    Both get the job done, either or.
    image
  • MathiausMathiaus Pennsylvania
    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.
    image
  • MathiausMathiaus Pennsylvania
    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.
    image
  • IniarIniar Australia
    edited March 2014
    Local exclude = { "anorexia", "clumsiness", "stupidity" }

    If table.contains( exclude, affliction ) then
    -- do awesome
    end
    wit beyond measure is a Sidhe's greatest treasure
  • 7 hours of trying to code something in Mudlet and I have been defeated.
    image
  • IniarIniar Australia
    Sorry Saitou, cousins crashed the house so was busy :(
    wit beyond measure is a Sidhe's greatest treasure

  • Iniar said:
    Sorry Saitou, cousins crashed the house so was busy :(
    Did you try telling them to turn it off and back on again?
  • 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.
Sign In or Register to comment.