Welcome to the Achaea Forums! Please be sure to read the Forum Rules.

[Mudlet] Code Snippets

2

Comments

  • TysandrTysandr Posts: 438Member ✭✭✭✭ - Eminent
    Tysandr said:


    Someone asked for the trigger pattern that I was asked to use, here it is:


    Nexus: Accents, Autotriggers, Dragon Talismans
    "As the child did... without the rope. Then fear will find you again."

  • VadimusesVadimuses Posts: 1,077Member @ - Epic Achaean
    That could be part of the mapper script, seems pretty handy to have
    CaelanTysandr
  • SobriquetSobriquet Posts: 1,980Member @@ - Legendary Achaean
    That is really cool script, Thanks!
    image
  • KeneanungKeneanung Posts: 584Member ✭✭✭✭ - Eminent
    Vadimuses said:
    That could be part of the mapper script, seems pretty handy to have
    It is now!
    VadimusesTysandrLaerinCailin
  • VadimusesVadimuses Posts: 1,077Member @ - Epic Achaean
    Have no idea where your function is getting called from? Find out with:

    echo(debug.traceback())

    RomTysandrKlendathu
  • TysandrTysandr Posts: 438Member ✭✭✭✭ - Eminent
    Vadimuses said:
    Have no idea where your function is getting called from? Find out with:

    echo(debug.traceback())

    Damn, wish I had known that one. :(
    Nexus: Accents, Autotriggers, Dragon Talismans
    "As the child did... without the rope. Then fear will find you again."

  • VadimusesVadimuses Posts: 1,077Member @ - Epic Achaean
    The Lua manual is just about the only language manual I've ever read and it's the most useful one
  • VadimusesVadimuses Posts: 1,077Member @ - Epic Achaean
    edited February 2
    This hasn't been mentioned on its own yet - there's another way of assembling text using string.format. It can help where things with .. get messy:


    echo("target is: "..target..", current health: "..currenthealth..", maxhealth: "..maxhealth)

    -- or:

    echo(string.format("target is: %s, current health: %s, maxhealth: %s", target, currenthealth, maxhealth))

    TysandrZulah
  • VadimusesVadimuses Posts: 1,077Member @ - Epic Achaean
    When you've filled up your screen debugging your system, clear it all with:

    lua clearWindow()

  • PyoriPyori Posts: 614Member ✭✭✭✭ - Eminent
    Vadimuses said:
    When you've filled up your screen debugging your system, clear it all with:

    lua clearWindow()


    ...............................................................................

    How did I not know you could do this on the buffer... Given how much I use it everywhere else you'd think I'd have tried...

    Tysandr
  • VadimusesVadimuses Posts: 1,077Member @ - Epic Achaean
    All of the following are valid ways to call a function with one argument to it. Take your pick:

    myfunction("some value")
    myfunction('some value')
    myfunction ("some value")
    myfunction ('some value')
    myfunction"some value"
    myfunction'some value'
    myfunction "some value"
    myfunction 'some value'


    Or stick with what you do. You've got choices!

    Tysandr
  • AustereAustere TennesseePosts: 2,187Member @@ - Legendary Achaean
    Vadimuses said:
    All of the following are valid ways to call a function with one argument to it. Take your pick:

    myfunction("some value")
    myfunction('some value')
    myfunction ("some value")
    myfunction ('some value')
    myfunction"some value"
    myfunction'some value'
    myfunction "some value"
    myfunction 'some value'


    Or stick with what you do. You've got choices!

    @Zulah :)
    Zulah
  • NazihkNazihk Posts: 965Member @ - Epic Achaean
    If you really want to mess with people, pick a function call syntax at random every time you call a function. 
  • PyoriPyori Posts: 614Member ✭✭✭✭ - Eminent
    Nazihk said:
    If you really want to mess with people, pick a function call syntax at random every time you call a function. 
    If you want people to never ask for your scripts, just do a dice roll every time you make a function and have it call one of the above at random. Like have one alias call myFunction"value", another one do myFunction2("value") and a third do myfunction 'value' and so on.

    Randomly alternate between tab, double space, triple space for indenting.

    For concatenation sometimes have spacing, other times don't. So like (var1 .. "+".. var2.. " - " .. var3 ), etc.

    Make the first half of your code have legible formatting, and following proper code practices. So like myFunction( value1, value2) -- if value2 > value 1 then .... Then halfway through your code start doing Myfunction( Value1, value2) etc etc.

    ...I could go on...

    I've been tempted to do it a number of times, to see how long it'd take people to notice... But I don't hate myself enough to go through with doing it :(

  • ZulahZulah Posts: 758Member @ - Epic Achaean
    Austere said:
    Vadimuses said:
    All of the following are valid ways to call a function with one argument to it. Take your pick:

    myfunction ("some value")

    Or stick with what you do. You've got choices!
    @Zulah :)
    @Austere just because you CAN do something doesn't mean you should though!
    Caelan
  • AntoniusAntonius Posts: 4,500Member @@ - Legendary Achaean
    Zulah said:
    Austere said:
    Vadimuses said:
    All of the following are valid ways to call a function with one argument to it. Take your pick:

    myfunction ("some value")

    Or stick with what you do. You've got choices!
    @Zulah :)
    @Austere just because you CAN do something doesn't mean you should though!
    This. One of the most important things when coding is to make the intention as clear as possible to the reader. By using the brackets for all of your function calls, you're making it clear to anybody else reading the code (including yourself months later) that the variable is meant to refer to a function and that you meant to call that function.

    As an example of what I mean, consider this code:
    local a, b = myvariable "some string"
    
    Did I mean to call the function that's referred to by "myvariable", passing in the string "some string" to it? Or do I have a minor syntax mistake, and I actually meant local a,b = myvariable, "some string" (i.e. assign the value of myvariable to a and the string "some string" to b)?

    The syntax is valid in both cases, but the meaning is completely different. If myvariable doesn't refer to a function (because I meant the latter - though I could have meant the latter even if myvariable did refer to a function), I get a runtime error when the code executes. If I come and look at this code six months later, there's an additional step in reasoning that myvariable should be a function with a single parameter and which returns multiple values, though I may want to go and confirm for myself again that I didn't mean to have the comma there.

    Not to mention that 1. you need to use brackets when you have a number of parameters that's not one, and 2. the bracketless syntax doesn't work in plenty of other languages. It just really doesn't seem like a good habit to get into.
  • CaledCaled Posts: 74Member ✭✭ - Stalwart
    Right-click a poorly formatted script, and select 'Format all'.

    I was dubious when I saw this feature in one of the mudlet updates, but it actually worked really nicely for me just now, when I was trying out a script from someone on this forum, that had no indentation.


    VadimusesCailinTysandr
  • BrenexBrenex Posts: 105Member ✭✭✭ - Distinguished
    That's neat, the only problem is it does remove spaces between code blocks which I feel can help improve readability if doing it correctly

  • KeneanungKeneanung Posts: 584Member ✭✭✭✭ - Eminent
    It is opinionated so people always have the same style and don't need to fiddle with settings.
  • PyoriPyori Posts: 614Member ✭✭✭✭ - Eminent
    I got bored and decided to make a Caesar Cipher for a different game; one of the quests was bugging me and I found out that's the kind of encryption it uses. I won't show the method I use, specifically, to decode things but I can give the functions with an explanation of how they work.
    function toEncrypt(tocode, num)
    return tocode:gsub("%a", function(x)
    local start = (x:lower() == x and string.byte('a') or string.byte('A'))
    local c = x:byte() - start c = c + num
    c = c%26 c = c + start
    return string.char(c) end)
    end
    function toDecrypt(tocode, num) return toEncrypt(tocode, num)
    end
    From wikipedia: It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be replaced by A, E would become B, and so on. With a right shift of 3, D would be replaced by G, E would become H, and so on.

    The functions above: Encrypt is a right shift, whereas Decrypt functions as a left shift.
    So for instance, if we were to do:
    local msg = "Pyori is the greatest"
    toEncrypt(msg, 7)
    toDecrypt(msg, 7)
    The former would return: Wfvyp pz aol nylhalza
    The latter would return: Irhkb bl max zkxtmxlm

    Whereas if we did:
    local decrypt = "Irhkb bl max zkxtmxlm"
    local encrypt = "
    Wfvyp pz aol nylhalza"
    toEncrypt(decrypt, 7)
    toDecrypt(encrypt, 7)

    They'd both return: Pyori is the greatest
    ---

    Enjoy! I'm not sure how useful it'll actually be for others, but it got usage for me. Initially I coded similar for a java project I had for an assessment, and converted it to work with Lua.




    TysandrSobriquet
  • TysandrTysandr Posts: 438Member ✭✭✭✭ - Eminent
    Not technically Mudlet, but it was very neat. Copied @Sena's code for this thread.
    Sena said:
    Here's a pattern I'd use to capture the price, item name, and shop name from directory:
    ^(\d+)(gp|cr|mc)\s+([a-z A-Z',()0-9&~"\.-]{34})\s+(.+)$
    That {34} means to match exactly 34 characters, no more and no less. I also added other characters to the range ( [a-z A-Z',()0-9&~"\.-] ) that can appear in item names because I like to be as specific as possible, but .{34} will work just as well.


    Nexus: Accents, Autotriggers, Dragon Talismans
    "As the child did... without the rope. Then fear will find you again."

    Cailin
  • CaledCaled Posts: 74Member ✭✭ - Stalwart
    Tysandr said:k
    Not technically Mudlet, but it was very neat. Copied @Sena's code for this thread.
    Sena said:
    Here's a pattern I'd use to capture the price, item name, and shop name from directory:
    ^(\d+)(gp|cr|mc)\s+([a-z A-Z',()0-9&~"\.-]{34})\s+(.+)$
    That {34} means to match exactly 34 characters, no more and no less. I also added other characters to the range ( [a-z A-Z',()0-9&~"\.-] ) that can appear in item names because I like to be as specific as possible, but .{34} will work just as well.


    I haven't got it working exactly how I want it yet, but I've been adding links to the directory output to walk to the shop, and display info about the price. Highlight the cheapest, show the per one cost if it's a group etc. I'd like to get it to 'wares theitem' on arrival as well but I haven't got that far.

    I'll post it when I'm eventually done with it - and tha ks to Sena that will be sooner than later. 


    BrenexCailinTysandr
  • BrenexBrenex Posts: 105Member ✭✭✭ - Distinguished
    Caled said:
    Tysandr said:k
    Not technically Mudlet, but it was very neat. Copied @Sena's code for this thread.
    Sena said:
    Here's a pattern I'd use to capture the price, item name, and shop name from directory:
    ^(\d+)(gp|cr|mc)\s+([a-z A-Z',()0-9&~"\.-]{34})\s+(.+)$
    That {34} means to match exactly 34 characters, no more and no less. I also added other characters to the range ( [a-z A-Z',()0-9&~"\.-] ) that can appear in item names because I like to be as specific as possible, but .{34} will work just as well.


    I haven't got it working exactly how I want it yet, but I've been adding links to the directory output to walk to the shop, and display info about the price. Highlight the cheapest, show the per one cost if it's a group etc. I'd like to get it to 'wares theitem' on arrival as well but I haven't got that far.

    I'll post it when I'm eventually done with it - and tha ks to Sena that will be sooner than later. 


    Walking to the store via directory is already included in the mudlet-mapper script

    Caled
  • CaledCaled Posts: 74Member ✭✭ - Stalwart
    I thought so too but it hasn't been coming up for me; I just assumed I had it confused with an addon script someone else had done. I'll try re-downloading the mapper script.
  • BrenexBrenex Posts: 105Member ✭✭✭ - Distinguished

    this is what it looks like for me for reference

  • CaledCaled Posts: 74Member ✭✭ - Stalwart
    Brenex said:

    this is what it looks like for me for reference
    Yeah I got it working. I'm guessing I copied the generic mapping script across to my newb's profile.
  • PyoriPyori Posts: 614Member ✭✭✭✭ - Eminent
    edited April 19
    I made this, so I wouldn't have to keep digging up the formula, when messing around with racial spec and stuff... You could technically use this to calculate mana as well. Would just have to add 10% to the base value (prior to bracelets) if your city has a pillar thingo that gives 10% mana.
    Pattern: ^hp (\d+) (\d+) (\d+)$
    Code:
    --Initialise variables; split lvl between <100 and >100, con, bracelet level.
    local lone, ltwo, c, b, hp = 0, 0, tonumber(matches[3]), tonumber(matches[4]), 0
    --If level is over 100, then split the variables above. if tonumber(matches[2]) > 100 then lone = 100 ltwo = tonumber(matches[2]) - 100 else lone = tonumber(matches[2]) ltwo = 0 end
    --Make bracelet a proper percentage to multiply by. b = ((100+b)/100)
    --Math the formula out. hp = (300 + (lone*((3*c)+4)) + (ltwo*((3*c)-16)))
    --Multiply by bracelet value. 1.05 for lvl1, 1.10 for 2, 1.15 for 3.
    hp = (hp*b)
    --Print the value! cecho("\n<green>Level of: <white>"..matches[2]..".") cecho("\n<green>With con: <white>"..c..".") cecho("\n<green>Bracelet: <white>"..matches[4].."%") cecho("\n<red>Equals : <green>"..hp.."hp.") cecho("\n<purple>+ Dcape : <SeaGreen>"..(hp*1.5).."hp.")
    Using is simple. Simply do 'hp <level> <con> <bracelets>'
    Example: hp 110 16 10 for lvl 110 with 16 con and lv 2 bracelets (5/10/15 are the bracelet values for 1/2/3 respectively)
    This would print out-
    Level of: 110.
    With con: 16.
    Bracelet: 10%
    Equals : 6402hp.
    + Dcape : 9603hp.



    TysandrCailinExelethril
  • IsmayIsmay Posts: 514Member ✭✭✭✭ - Eminent
    Love these. They help me learn to code a little more. Keep em coming!
    Tysandr
2
Sign In to Comment.