[Mudlet] Code Snippets

2

Comments

  • Tysandr said:


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


    "All we have to decide is what to do with the time that is given to us."

  • That could be part of the mapper script, seems pretty handy to have
  • That is really cool script, Thanks!

    (Party): Mezghar says, "Stop."
  • Vadimuses said:
    That could be part of the mapper script, seems pretty handy to have
    It is now!
  • Have no idea where your function is getting called from? Find out with:

    echo(debug.traceback())

  • 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. :(
    "All we have to decide is what to do with the time that is given to us."

  • The Lua manual is just about the only language manual I've ever read and it's the most useful one
  • edited February 2018
    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)<br><br>-- or:<br><br>echo(string.format("target is: %s, current health: %s, maxhealth: %s", target, currenthealth, maxhealth))

  • When you've filled up your screen debugging your system, clear it all with:

    lua clearWindow()

  • 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...
  • All of the following are valid ways to call a function with one argument to it. Take your pick:

    <code>myfunction ("some value")<br>myfunction ('some value')<br>myfunction"some value"
    myfunction'some value'
    myfunction "some value"
    myfunction 'some value'myfunction("some value")<br>myfunction('some value')<br>


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

  • Vadimuses said:
    All of the following are valid ways to call a function with one argument to it. Take your pick:

    <code>myfunction ("some value")<br>myfunction ('some value')<br>myfunction"some value"
    myfunction'some value'
    myfunction "some value"
    myfunction 'some value'myfunction("some value")<br>myfunction('some value')<br>


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

    @Zulah :)
  • If you really want to mess with people, pick a function call syntax at random every time you call a function. 
  • 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 :(
  • Austere said:
    Vadimuses said:
    All of the following are valid ways to call a function with one argument to it. Take your pick:
    <code>myfunction ("some value")<br>
    <br>
    Or stick with what you do. You've got choices!
    @Zulah :)
    @Austere just because you CAN do something doesn't mean you should though!
  • 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:
    <code>myfunction ("some value")<br>
    <br>
    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.
  • 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.


  • 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

  • It is opinionated so people always have the same style and don't need to fiddle with settings.
  • 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.
    </code>function toEncrypt(tocode, num)<br>   return tocode:gsub("%a", function(x)<br>      local start = (x:lower() == x and string.byte('a') or string.byte('A'))<br>      local c = x:byte() - start
          c = c + num<br>      c = c%26
          c = c + start<br>      return string.char(c)
       end)<br>end
    <br>function toDecrypt(tocode, num)
       return toEncrypt(tocode, num)<br>end<br></pre>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.<br><br>The functions above: Encrypt is a right shift, whereas Decrypt functions as a left shift.<br>So for instance, if we were to do:<br><pre class="CodeBlock"><code>local msg = "Pyori is the greatest"<br>toEncrypt(msg, 7)<br>toDecrypt(msg, 7)<br>
    The former would return: Wfvyp pz aol nylhalza
    The latter would return: Irhkb bl max zkxtmxlm

    Whereas if we did:
    Wfvyp pz aol nylhalza"
    toEncrypt(decrypt, 7)<br>toDecrypt(encrypt, 7)
    local decrypt = "Irhkb bl max zkxtmxlm"<br>local encrypt = "
    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.



  • 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.


    "All we have to decide is what to do with the time that is given to us."

  • 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. 


  • 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

  • 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.

  • this is what it looks like for me for reference

  • 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.
  • edited April 2018
    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.
    ^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.")
    Pattern:
    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.


  • Love these. They help me learn to code a little more. Keep em coming!
    Give us -real- shop logs! Not another misinterpretation of features we ask for, turned into something that either doesn't help at all, or doesn't remotely resemble what we wanted to begin with.

    Thanks!

    Current position of some of the playerbase, instead of expressing a desire to fix problems:

    Vhaynna: "Honest question - if you don't like Achaea or the current admin, why do you even bother playing?"


Sign In or Register to comment.