Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to "verify" your details, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ MUSHclient ➜ Lua ➜ Converting Python Code to Lua

Converting Python Code to Lua

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Sanichi   (11 posts)  Bio
Date Sun 13 Nov 2022 06:43 PM (UTC)
Message
I have a particular code I'm trying to get working that I've favored in MUD I've always played, but it seems impossible to get Python working.

Is this code possible to be functional in Lua?

https://gist.githubusercontent.com/castercerberus/f4a4bb543ce005d9b2cf/raw/6c506085e211c9fb264c3e581d413007a0ff540a/gistfile1.txt
Top

Posted by Nick Gammon   Australia  (23,070 posts)  Bio   Forum Administrator
Date Reply #1 on Sun 13 Nov 2022 11:13 PM (UTC)

Amended on Mon 14 Nov 2022 07:59 AM (UTC) by Nick Gammon

Message
Yes, it's possible. Virtually anything is possible.

As an example, take your code here:


 def SkillImprove(TriggerName, trig_line, wildcards):

       #Concatonate all the variables passed by the MUSHclient
       vName = ''.join([wildcards[x] for x in range(len(wildcards)-1)])

       #Replace invalid characters with other characters to store as variables
       vName = vName.replace(' ', '_').replace('\'', '').replace('-', '_').replace('#', '_')

       #If the variable doesn't currently have a value give it
       #the value of 1, else increment it by 1
       if not world.GetVariable(vName):
          vValue = 1
       else:
          vValue = int(world.GetVariable(vName)) + 1

       #Display in the command window the improvement message
       #with the corresponding number of improvements added in
       world.SetVariable(vName, str(vValue))
       world.ColourTell('#00FFFF', '', '* You think your ' + str(vName))
       world.ColourTell('#FFFFFF', '', ' [')
       world.ColourTell('#FF0000', '', str(vValue))
       world.ColourTell('#FFFFFF', '', '] ')
       world.ColourNote('#00FFFF', '', 'skill has improved. *' )



Would look roughly like this:


 function SkillImprove(TriggerName, trig_line, wildcards)

   --Concatonate all the variables passed by the MUSHclient
   vName = table.concat (wildcards, '')

   --Replace invalid characters with other characters to store as variables
   vName = vName:gsub(' ', '_'):gsub('\'', ''):gsub('-', '_'):gsub('#', '_')

   --If the variable doesn't currently have a value give it
   --the value of 1, else increment it by 1
   if not GetVariable(vName) then
      vValue = 1
   else
      vValue = tonumber(GetVariable(vName)) + 1
   end -- if
   
   --Display in the command window the improvement message
   --with the corresponding number of improvements added in
   SetVariable(vName, vValue)
   ColourTell('#00FFFF', '', '* You think your ' .. vName)
   ColourTell('#FFFFFF', '', ' [')
   ColourTell('#FF0000', '', vValue)
   ColourTell('#FFFFFF', '', '] ')
   ColourNote('#00FFFF', '', 'skill has improved. *' )

end -- function SkillImprove



  • In Lua functions are defined with "function" not "def".

  • Functions end with "end".

  • "If" statements need a "then" rather than a colon, and end with a "end".

  • You don't need "world." in front of world (MUSHclient) functions.

  • Lua uses "elseif" rather than "elif".

  • Comments start with "--" rather than "#"

  • Concatenation of strings is done with ".." rather than "+".


I'll leave you to handle the rest.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,070 posts)  Bio   Forum Administrator
Date Reply #2 on Mon 14 Nov 2022 08:01 AM (UTC)

Amended on Wed 16 Nov 2022 06:59 AM (UTC) by Nick Gammon

Message
You could simplify:


   vName = vName:gsub(' ', '_'):gsub('\'', ''):gsub('-', '_'):gsub('#', '_')


To:



   vName = vName:gsub("[ '%-#]", '_')


The gsub function takes regular expressions Lua patterns as an argument, and the things inside square brackets are a "set".

[EDIT]

Lua patterns are conceptually similar to regular expressions (you can capture things inside brackets, and match on sets of things, like numbers or letters) but are somewhat simplified.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Fiendish   USA  (2,533 posts)  Bio   Global Moderator
Date Reply #3 on Tue 15 Nov 2022 09:32 PM (UTC)
Message
Quote:
The gsub function takes regular expressions as an argument


Well, Lua patterns ( https://www.lua.org/manual/5.3/manual.html#6.4.1 )

Though if you want a version of gsub that uses regular expressions, I have that here https://github.com/fiendish/funny_little_Lua_things/blob/master/pcre_gsub_without_gsub.lua

https://github.com/fiendish/aardwolfclientpackage
Top

Posted by Sanichi   (11 posts)  Bio
Date Reply #4 on Wed 16 Nov 2022 12:28 AM (UTC)
Message
This is perfect! I'll be quite honest though, I'm lost on applying the code. I didn't write the original script, so I'm having issues looking at it and applying the modifications.

I'm more on the user end when it comes to scripts.
Top

Posted by Nick Gammon   Australia  (23,070 posts)  Bio   Forum Administrator
Date Reply #5 on Wed 16 Nov 2022 07:01 AM (UTC)
Message
Fiendish said:

Well, Lua patterns ...


Quite right, I tend to simplify my description of Lua patterns as "regular expressions" because they are better known.

I've amended my post to be a little clearer about that.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Sanichi   (11 posts)  Bio
Date Reply #6 on Fri 18 Nov 2022 01:04 AM (UTC)
Message
This is what I have so far, still not working because of issues with comments it seems, but I don't know what I did wrong.

https://github.com/several-wolves/mushclient/blob/main/counting
Top

Posted by Nick Gammon   Australia  (23,070 posts)  Bio   Forum Administrator
Date Reply #7 on Fri 18 Nov 2022 05:21 AM (UTC)

Amended on Fri 18 Nov 2022 05:30 AM (UTC) by Nick Gammon

Message
I suggested:


   if not GetVariable(vName) then
      vValue = 1
   else
      vValue = tonumber(GetVariable(vName)) + 1
   end -- if


You have:


       if not GetVariable(vName):
          vValue = 1
       else
          vValue = int(GetVariable(vName)) .. 1


There are four differences to what I have.


  • You didn't change ":" to "then"
  • You changed "+" to ".." for some reason
  • You didn't put an "end" in for the "if"
  • You didn't change "int" to "tonumber"


On the line with "vName.replace" on it you ignored my suggestions completely.




These remarks apply to most of your code. There are many "if" or "elseif" statements you have there still with a colon on the end of the line, and no "end" at the end of the "if".

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Sanichi   (11 posts)  Bio
Date Reply #8 on Fri 18 Nov 2022 03:35 PM (UTC)
Message
Nick Gammon said:

I suggested:


   if not GetVariable(vName) then
      vValue = 1
   else
      vValue = tonumber(GetVariable(vName)) + 1
   end -- if


You have:


       if not GetVariable(vName):
          vValue = 1
       else
          vValue = int(GetVariable(vName)) .. 1


There are four differences to what I have.


*You didn't change ":" to "then"
*You changed "+" to ".." for some reason
*You didn't put an "end" in for the "if"
*You didn't change "int" to "tonumber"


On the line with "vName.replace" on it you ignored my suggestions completely.

-----

These remarks apply to most of your code. There are many "if" or "elseif" statements you have there still with a colon on the end of the line, and no "end" at the end of the "if".

I've updated the code as I comprehend it, but I'm not quite sure I completely understand how to apply if-then statements nor how to apply the ends completely. I've added thens on all colon ending if/elseif statments.

I'm not quite sure how to apply the end -- if.

https://github.com/several-wolves/mushclient/blob/main/counting
Top

Posted by AdInfinitum   (74 posts)  Bio
Date Reply #9 on Fri 18 Nov 2022 05:02 PM (UTC)

Amended on Fri 18 Nov 2022 08:11 PM (UTC) by AdInfinitum

Message
For every 'if' you have in a code, it has to have a corresponding 'end' to indicate the end of the 'if' block. For instance:


if (foo == bar) then
   print("foobar")
end -- This ends 'if (foo == bar)'


Indentation is your friend here to ensure you match the 'end' with its corresponding 'if'. If it's easier, think of 'if' as being the opening (, and the 'end' as being the closing ).

Edit to add:

'if' blocks operate in the manner of: if-then-else(if)-end. You do not want to terminate the 'if' block unless you have completed all conditional statements with 'else' or 'elseif'.
Top

Posted by Fiendish   USA  (2,533 posts)  Bio   Global Moderator
Date Reply #10 on Fri 18 Nov 2022 10:59 PM (UTC)

Amended on Fri 18 Nov 2022 11:04 PM (UTC) by Fiendish

Message
Sanichi said:

I'm not quite sure I completely understand how to apply if-then statements nor how to apply the ends completely.


This is solvable by reading the Lua programming guide. https://www.lua.org/pil/4.3.1.html
It's the first hit if you google "lua if then".

https://github.com/fiendish/aardwolfclientpackage
Top

Posted by Nick Gammon   Australia  (23,070 posts)  Bio   Forum Administrator
Date Reply #11 on Sat 19 Nov 2022 06:48 AM (UTC)
Message
I'm not a huge fan of stat rollers. You can program them to give you a "really good" character, but at what cost? Depending on what you program it to do, it could hit the server with days of high-volume traffic, all with the aim of getting "slightly better" characters than the others have.

Meanwhile the server goes down because its traffic hits its monthly limit.

Part of the blame here is for MUD game designers who lazily use a random number generator to give you your stats and ask if that is OK. They didn't allow for people retrying a million times to get a better character.

They could work around this by disallowing more than (say) 10 attempts from a particular IP address before not allowing any more.

Alternatively you could do what a lot of games do and give you a pool of stats, and let you choose how to distribute them. Since this pool is not random, there is no point in retrying to get a better pool.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


11,281 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.