Converting Python Code to Lua

Posted by Sanichi on Sun 13 Nov 2022 06:43 PM — 12 posts, 25,698 views.

#0
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
Australia Forum Administrator #1
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.
Amended on Mon 14 Nov 2022 07:59 AM by Nick Gammon
Australia Forum Administrator #2
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.
Amended on Wed 16 Nov 2022 06:59 AM by Nick Gammon
USA Global Moderator #3
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
#4
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.
Australia Forum Administrator #5
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.
#6
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
Australia Forum Administrator #7
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".
Amended on Fri 18 Nov 2022 05:30 AM by Nick Gammon
#8
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
#9
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'.
Amended on Fri 18 Nov 2022 08:11 PM by AdInfinitum
USA Global Moderator #10
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".
Amended on Fri 18 Nov 2022 11:04 PM by Fiendish
Australia Forum Administrator #11
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.