Learning LUA in anticipation for NS2?
WhiteZero
That Guy Join Date: 2004-06-24 Member: 29511Members, Constellation
<div class="IPBDescription">Roll call!</div>I've always had an interest in programming but never got that deep into it (mostly a bit of VisualBasic in highschool). But now I'm committing myself to spend at least an hour per day studying C++ and LUA. Sense the two languages go hand-in-hand and I have a couple C++ books lying around, I figured I'd start learning both.
Has anyone else started to try to learn LUA in preparation for NS2 modding? I'm sure there are a few people here that already know some LUA after messing with GMOD. <img src="style_emoticons/<#EMO_DIR#>/biggrin-fix.gif" style="vertical-align:middle" emoid=":D" border="0" alt="biggrin-fix.gif" />
Has anyone else started to try to learn LUA in preparation for NS2 modding? I'm sure there are a few people here that already know some LUA after messing with GMOD. <img src="style_emoticons/<#EMO_DIR#>/biggrin-fix.gif" style="vertical-align:middle" emoid=":D" border="0" alt="biggrin-fix.gif" />
Comments
I have almost no experience with LUA, but from what I can tell it looks nothing like the C++ I am familiar with.
1. Get a game you know has some support for Lua scripting. G-Mod, as you say, is one. WoW is another. Vendetta Online is yet another.
2. Most integrated Lua systems will be just slightly different than the next, as they would have in-house modifications, and restrictions, etc. (you don't want a Lua script to be able to access the operating system of the client for example)
3. Once you've learned the basic syntax <a href="http://www.lua.org/manual/5.1/" target="_blank">(reading assignment)</a> its probably a good idea to move on to Lua in its own seperate environment. You can do so by building the Lua interpreter/compiler from its source or download the binaries directly. Some downloads (particularly Lua for Windows) will come with some nice "modules" that will allow you to create interfaces, connect to a database, etc. Links can be found <a href="http://www.lua.org/download.html" target="_blank">here</a>
4. Find some small projects (<a href="http://projecteuler.net/" target="_blank">Project Euler</a> anyone?) that would present some sort of challenge for you and try to implement it with Lua. I've done some pretty neat stuff myself and there is no task Lua can't accomplish (almost).
5. Pray that the devs made Object Oriented programing with Lua friendlier than it is out of the box.
A note about its syntax: If you're familiar at all with Java/C++/Python, you'll pick up on the syntax fairly immediately. Lua does away with braces however and might look more like Basic. Its a snap once you start working with it though.
<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->function MyFunction(a,b,c)
print(a,b,c)
end
if(Test ~= nil ) then
print("Yay!")
end
myArray = {}
myArray["Associative Arrays Are Pwnage!"] = "omgzor? wow!"
print(myArray["Associative Arrays Are Pwnage!"])
--> "omgzor? wow!"<!--c2--></div><!--ec2-->
<b>ALL CREDIT FOR THIS GOES TO DALIN!</b>
And for those that are sort of interested in LUA but don't know where to start ("WHERE IS THE COMPILER?!?"), you can start doing some Euler projects on the LUA:Demo site.
Euler problems (these you try to solve with LUA):
<a href="http://projecteuler.net/index.php?section=problems" target="_blank">http://projecteuler.net/index.php?section=problems</a>
LUA:Demo allows you to run simple LUA code in your browser:
<a href="http://www.lua.org/cgi-bin/demo" target="_blank">http://www.lua.org/cgi-bin/demo</a>
Just to have a stab at it I did the first Euler problem (I am not an experienced programmer)
<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->-- Euler project
-- Problem 1
a = 3
b = 5
sum = 0
top_a = math.floor(1000/a)
top_b = math.floor(1000/b)
for i=1,top_a,1 do sum = sum + i*a end
for i=1,top_b,1 do sum = sum + i*b end
io.write("Finding the sum of all the multiples of 3 or 5 below 1000\n")
io.write("The highest multiple for the number ", a ,", without going over 1000 is: ", top_a,"\n")
io.write("The highest multiple for the number ", b ,", without going over 1000 is: ", top_b,"\n\n")
io.write("The sum of all multiples of 3 and 5 below 1000 is: ", sum)<!--c2--></div><!--ec2-->
Executed on LUA:Demo you get:
<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->Finding the sum of all the multiples of 3 or 5 below 1000
The highest multiple for the number 3, without going over 1000 is: 333
The highest multiple for the number 5, without going over 1000 is: 200
The sum of all multiples of 3 and 5 below 1000 is: 267333<!--c2--></div><!--ec2-->
Would be nice to get a forum up for LUA learners soon, it's more fun to learn together <img src="style_emoticons/<#EMO_DIR#>/wink-fix.gif" style="vertical-align:middle" emoid=";)" border="0" alt="wink-fix.gif" />
To use the interpreter, you can either run the lua5.1.exe directly from the command prompt, after which it will run like the Lua Demo on the Lua site, or you can pass it a filename from the command prompt. This will then execute the interpreter and have it interpret the code inside MyLuaProgram.lua. Its probably best to use a text editor that supports the command-line so you can press a key, execute the lua interpreter, and capture the "output" into the text editor you're using. I prefer <a href="http://www.contexteditor.org/" target="_blank">ConTEXT</a> for this, but dozens of text editors have that very same feature.
<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->>lua5.1.exe C:\MyLuaProgram.lua<!--c2--></div><!--ec2-->
A very nice attempt Strepto, but there is a small problem with your code. I believe the problem lies when you do the summation of the numbers. The question is "Find the sum of all numbers below 1000 that are multiples of either 3 or 5." and your code assumes that these are exclusive cases. For example, 12 is not a multiple of 5 but it is of 3. If we take a look at 15, we find that it is a multiple of 3 and 5.
Your code first sums up the numbers that are multiples of 3, (like 15) and then the numbers that are multiples of 5 (like 15). So, while it would be correct if a single number did not have another divisor, but since there are multiple numbers that 3 and 5 both go into evenly, then the final count you get includes these numbers twice.
Here is my solution (now I didn't know io.write existed, heh, but print = io.write):
<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->--Program that finds the sum of all multiples of 3 or 5 from 1 to 1000.
sum = 0
for i = 1, 999 do
--MODULUS
--result = a - b*floor(a/b)
if( ((i - 3*math.floor(i/3)) == 0) or ((i-5*math.floor(i/5)) == 0) ) then
sum = sum + i
end
end
print("Sum: " .. sum)<!--c2--></div><!--ec2-->
The correct answer is: 233168.
"result = a - b*math.floor(i/3)" finds the result of a modulus operation (which I don't think exists in Lua as part of the math library) that is, it finds the remainder of one number divided by another. I think it goes about it in another way though, but if i is a multiple of 3, then "3*math.floor(i/3)" will return i and "i-i" is 0 so we know there is no remainders.
The if statement itself is pretty simple. If (i - result of modulus check for multiples of 3) is 0 or if (i - result of modulus check for multiples of 5) is 0 then we add i to the sum. This will catch all numbers that are multiples of 3 or 5 and add them once.
Hope that helps.
I also suggested there be a Lua forum, but the idea didn't stick. That was a little early in the game then, but perhaps now would be an appropriate time. Then again, there could always just be a running Lua thread for Project Euler <img src="style_emoticons/<#EMO_DIR#>/biggrin-fix.gif" style="vertical-align:middle" emoid=":D" border="0" alt="biggrin-fix.gif" />
I have almost no experience with LUA, but from what I can tell it looks nothing like the C++ I am familiar with.<!--QuoteEnd--></div><!--QuoteEEnd-->
From the LUA manual:
"Lua is implemented as a library, written in clean C (that is, in the common subset of ANSI C and C++)."
So very true, guess I shoulda thought about that >.<
Was trying to figure out a way to calculate the sum without doing the check if a number is the multiple of both 3 and 5 every single time you add to the sum. Which I believe should make the code a little faster?
Fixed my code, I also didn't take into account that the multiples should be BELOW 1000 (200*5=1000), which further made my calculation erronous. Really wanted to avoid calling functions in the loops. I have no idea if the execution time is lower or not, but perhaps you can tell me that <img src="style_emoticons/<#EMO_DIR#>/wink-fix.gif" style="vertical-align:middle" emoid=";)" border="0" alt="wink-fix.gif" />
<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->a = 3
b = 5
c = a*b
sum = 0
top_a = math.floor(999/a)
top_b = math.floor(999/b)
top_c = math.floor(999/c)
for i=1,top_a,1 do sum = sum + i*a end
for i=1,top_b,1 do sum = sum + i*b end
for i=1,top_c,1 do sum = sum - i*c end<!--c2--></div><!--ec2-->
Sum is now correctly calculated to 233168
Anyways, this forum is NS2 General Discussion, and Euler problems aren't really NS2 <img src="style_emoticons/<#EMO_DIR#>/smile-fix.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile-fix.gif" />
Hopefully we'll see an appropriate forum for LUA in the future.
"Lua is implemented as a library, written in clean C (that is, in the common subset of ANSI C and C++)."<!--QuoteEnd--></div><!--QuoteEEnd-->
Simply because LUA is implemented in C doesn't mean the LUA language itself is anything like C.
The java virtual machine is also written in C, but that doesn't mean the language is similar. Hell, many languages are implemented in C at their core: Java, Perl, php, etc... Inherently, the entire purpose of these languages is to be different than C.
The java virtual machine is also written in C, but that doesn't mean the language is similar. Hell, many languages are implemented in C at their core: Java, Perl, php, etc... Inherently, the entire purpose of these languages is to be different than C.<!--QuoteEnd--></div><!--QuoteEEnd-->
LUA has a C (C++) API, and thats all I was really meaning. I guess I should have said "They can go hand-in-hand". Don't look too much into it.
Anyway, yeah Strepto and Dalin, I appreciate your interest in the topic, but please keep the LUA tutorials to the LUA community. Lets keep this on-topic with NS2.
-----------------------
--Program that finds the sum of all multiples of 3 or 5 from 1 to 1000.
n = math.floor(1000/3)
m = math.floor(1000/5)
p = math.floor(1000/15)
sum = 3*n*(n+1)/2 + 5*m*(m+1)/2 - 15*p*(p+1)/2
-----------------------
and the correct answer is actually 234168. Your programs fail to compensate for the fact that 1000 is also a multiple of 5.
<b>Competitive Player Database</b>
A database of registered players, their steam ID, location, and team; designed to facilitate organized pugs, scrims, and matches. Optimistically, expanding into an NS2 league. Accompanied by a plugin that allows servers to easily verify player identity.
<b>Ingame Pug System</b>
A server plugin that allows players to sign up for pugs in game, from across many public servers. Once enough players have signed up they will automatically be transferred to a private server. Only players registered with the organized player database will be allowed to signup.
In order to accomplish these things socket level communication will be required. This all of course depends on how UWE takes things themselves and what tools they offer us, but the premise is obvious.
Let me know if you are interested in helping is such projects.
Maybe by the time we get our hands on NS2 I will have learned LUA and I can do my own MOD. I think I'd like to MOD in a MvM mode. <img src="style_emoticons/<#EMO_DIR#>/biggrin-fix.gif" style="vertical-align:middle" emoid=":D" border="0" alt="biggrin-fix.gif" />
That being said, I would like to propose an exercise.
You are tasked with solving any Sudoku.
The program input will be a string representation of any sudoku, the program output is expected to be the same sudoku in a similar format, with no missing numbers.
Allow me to elaborate; A sudoku:
2 x x 3 6 7 5 x x
5 x x 8 x x x 6 x
3 x x 4 5 x 7 x x
x 9 x 5 3 x 4 x x
x 8 x x x x x 7 x
x x 3 x 7 4 x 5 x
x x 1 x 2 6 x x 5
x 3 x x x 5 x x 7
x x 2 7 8 3 x x 1
Can be written in a string: (I am terribly sorry about the formatting. Something feels the need to break lines at inappropriate places. I'll break manually and mark the spot with a \)
"2,,,3,6,7,5,,,5,,,8,,,,6,,3,,,4,5,,7,,,,9,,5,3,,4,,,,8,,,,,,7,,,,3,,7,4,,5\
,,,,1,,2,6,,,5,,3,,,,5,,,7,,,2,7,8,3,,,1"
This is done by simply writing one row after another, starting with the top one, and writing nothing between commas when a space is empty.
Your task is to write a program that, when fed this sudoku, solves it. Like so, for instance:
$ lua ./sudoku.lua "2,,,3,6,7,5,,,5,,,8,,,,6,,3\
>,,,4,5,,7,,,,9,,5,3,,4,,,,8,,,,,,7,,,,3,,7,4,,5,,,,1\
>,,2,6,,,5,,3,,,,5,,,7,,,2,7,8,3,,,1"
Solved!
2,1,9,3,6,7,5,8,4,5,7,4,8,9,2,1,6,3,3,6,8,4,5,1,7,2,9,6,9,\
7,5,3,8,4,1,2,4,8,5,2,1,9,3,7,6,1,2,3,6,7,4,9,5,8,7,4,1,9,\
2,6,8,3,5,8,3,6,1,4,5,2,9,7,9,5,2,7,8,3,6,4,1
Or something like that. The solution (of course) translates into:
2 1 9 3 6 7 5 8 4
5 7 4 8 9 2 1 6 3
3 6 8 4 5 1 7 2 9
6 9 7 5 3 8 4 1 2
4 8 5 2 1 9 3 7 6
1 2 3 6 7 4 9 5 8
7 4 1 9 2 6 8 3 5
8 3 6 1 4 5 2 9 7
9 5 2 7 8 3 6 4 1
I'm pretty sure I stole this exercise from somewhere, but I can't seem to remember where. What do you think? Would you rather be doing euler problems? Too much? Too little?
Sounds good. But can we please keep it to the Off-Topic forum and this thread NS2-centric? Thanks!
I'm going to request a MOD move Private and JAmazon's posts to a new thread in Off-Topic just to keep my thread on track...
<!--quoteo(post=1711166:date=Jun 8 2009, 12:58 PM:name=TinCan)--><div class='quotetop'>QUOTE (TinCan @ Jun 8 2009, 12:58 PM) <a href="index.php?act=findpost&pid=1711166"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec-->I plan to ... hack ...<!--QuoteEnd--></div><!--QuoteEEnd-->
:O
lol j/k
That being said, I would like to propose an exercise.
You are tasked with solving any Sudoku.
The program input will be a string representation of any sudoku, the program output is expected to be the same sudoku in a similar format, with no missing numbers.
Allow me to elaborate; A sudoku:
2 x x 3 6 7 5 x x
5 x x 8 x x x 6 x
3 x x 4 5 x 7 x x
x 9 x 5 3 x 4 x x
x 8 x x x x x 7 x
x x 3 x 7 4 x 5 x
x x 1 x 2 6 x x 5
x 3 x x x 5 x x 7
x x 2 7 8 3 x x 1
Can be written in a string: (I am terribly sorry about the formatting. Something feels the need to break lines at inappropriate places. I'll break manually and mark the spot with a \)
"2,,,3,6,7,5,,,5,,,8,,,,6,,3,,,4,5,,7,,,,9,,5,3,,4,,,,8,,,,,,7,,,,3,,7,4,,5\
,,,,1,,2,6,,,5,,3,,,,5,,,7,,,2,7,8,3,,,1"
This is done by simply writing one row after another, starting with the top one, and writing nothing between commas when a space is empty.
Your task is to write a program that, when fed this sudoku, solves it. Like so, for instance:
$ lua ./sudoku.lua "2,,,3,6,7,5,,,5,,,8,,,,6,,3\
>,,,4,5,,7,,,,9,,5,3,,4,,,,8,,,,,,7,,,,3,,7,4,,5,,,,1\
>,,2,6,,,5,,3,,,,5,,,7,,,2,7,8,3,,,1"
Solved!
2,1,9,3,6,7,5,8,4,5,7,4,8,9,2,1,6,3,3,6,8,4,5,1,7,2,9,6,9,\
7,5,3,8,4,1,2,4,8,5,2,1,9,3,7,6,1,2,3,6,7,4,9,5,8,7,4,1,9,\
2,6,8,3,5,8,3,6,1,4,5,2,9,7,9,5,2,7,8,3,6,4,1
Or something like that. The solution (of course) translates into:
2 1 9 3 6 7 5 8 4
5 7 4 8 9 2 1 6 3
3 6 8 4 5 1 7 2 9
6 9 7 5 3 8 4 1 2
4 8 5 2 1 9 3 7 6
1 2 3 6 7 4 9 5 8
7 4 1 9 2 6 8 3 5
8 3 6 1 4 5 2 9 7
9 5 2 7 8 3 6 4 1
I'm pretty sure I stole this exercise from somewhere, but I can't seem to remember where. What do you think? Would you rather be doing euler problems? Too much? Too little?<!--QuoteEnd--></div><!--QuoteEEnd-->
Isn't sudoku NP-Complete? I seem to remember hearing somewhere that it is. While this doesn't seem terribly hard to code, it could take a very long time to run <img src="style_emoticons/<#EMO_DIR#>/biggrin-fix.gif" style="vertical-align:middle" emoid=":D" border="0" alt="biggrin-fix.gif" />
Also, feel free to use the LUA section of the site in my signature if you want to post these somewhere <img src="style_emoticons/<#EMO_DIR#>/smile-fix.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile-fix.gif" />
Actually I've been into mods for a long time. Often spending more time with mods that the actual game that the mod was made for. Most obvious example being NS but also Desert Combat and Red Orchestra are big example of that. So for a while now with all the LUA talk going on in the NS2 blog I've been becoming more and more interested in looking into it.
Actually the off-topic discussion (as you refer to it) in this thread has gotten me a bit more excited about learning LUA for NS2 and I'll definetly keep an eye out for more "Learning LUA" threads.
Good topic idea.
As for LUA... Try <a href="http://www.zachtronicsindustries.com/pivot/entry.php?id=18" target="_blank">this Game</a>. It may help to Understand the Language.
examples:
<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->a,b = b,a<!--c2--></div><!--ec2-->Yup, that works for swapping values
<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->local oldprint = print -- Store current print function as oldprint
function print(s) -- Redefine print function
if s == "foo" then
oldprint("bar")
else
oldprint(s)
end
end<!--c2--></div><!--ec2-->Yup, thats right. dont like how LUA's core functions work? You can re-write them yourself.
And finally
<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->function makeaddfunc(x)
-- Return a new function that adds x to the argument
return function(y)
return x + y
end
end
plustwo = makeaddfunc(2)
print(plustwo(5)) -- Prints 7<!--c2--></div><!--ec2-->This ones just strange.
The more I learn about lua: the more I'm convinced that LUA was used to program skynet. <img src="style_emoticons/<#EMO_DIR#>/tounge.gif" style="vertical-align:middle" emoid=":p" border="0" alt="tounge.gif" />
examples:
<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->a,b = b,a<!--c2--></div><!--ec2-->Yup, that works for swapping values
<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->local oldprint = print -- Store current print function as oldprint
function print(s) -- Redefine print function
if s == "foo" then
oldprint("bar")
else
oldprint(s)
end
end<!--c2--></div><!--ec2-->Yup, thats right. dont like how LUA's core functions work? You can re-write them yourself.
And finally
<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->function makeaddfunc(x)
-- Return a new function that adds x to the argument
return function(y)
return x + y
end
end
plustwo = makeaddfunc(2)
print(plustwo(5)) -- Prints 7<!--c2--></div><!--ec2-->This ones just strange.
The more I learn about lua: the more I'm convinced that LUA was used to program skynet. <img src="style_emoticons/<#EMO_DIR#>/tounge.gif" style="vertical-align:middle" emoid=":p" border="0" alt="tounge.gif" /><!--QuoteEnd--></div><!--QuoteEEnd-->
Those aren't terribly odd as far as the more popular language now go.. I can only speak for python, but at least the first and third will work in it. The second may, I've never needed to do anything like it. I decided to learn LUA I would start writing the basics of an admin plugin... That keeps me interested moreso then doing things like the project euler challenges. Anyone have comments/suggestions on it? Code is available <a href="http://modns2.net/index.php?topic=3.msg5#new" target="_blank">here</a>.