Server side scripting with LUA 4.0

Started by Led, April 28, 2012, 05:54:28 AM

Previous topic - Next topic
April 28, 2012, 05:54:28 AM Last Edit: August 10, 2012, 07:55:26 PM by Buckler
Sleepkiller and I have been testing out what we can do with LUA commands.

SWBF uses LUA 4.0
http://www.lua.org/manual/4.0/manual.html
see section 6.4 for file write commands




I inserted some code into bes2a.lua to make a file write:



writeto("anotherfile.txt")
write (IMP)



This command creates a file called anotherfile.txt and write the value of IMP into it.

Combined with the IF THEN ELSE IF END statement structure, we have a way of modifying server side settings without players having to ever leave the server.

Stay tuned for more.

These lines also work--writing the value of PI and a random number:



writeto("anotherfile.txt")
write (IMP)
write(" ")
write(PI)
write(" ")
j = random()
write(j)


makes this:

2 3.141592741012573 0.04034546762704849




When modifying the mission lua, the file writes to the Gamedata folder.




I'm not sure how to create a new line yet.


Randomization will allow us to invoke various server side mods on the same server without have to restart the server.  For example, we can play fairsides one time, then regular sides the next time.



Quote from: Abraham Lincoln. on November 04, 1971, 12:34:40 PM
Don't believe everything you read on the internet

based on Sleepkillers IF THEN ELSE END construct, here is a way to create a random number and make a selection based on the random number:

(random number is from 1 to 10)



k=random(10);
writeto("anotherfile.txt")

if k == 1 then
write (k)
else
j = random()
write(j)
write(" ")
write (k)
write (" ")
write(PI)
end




From the LUA manual:

Quote
The functions random and randomseed are interfaces to the simple random generator functions rand and srand, provided by ANSI C. (No guarantees can be given for their statistical properties.) The function random, when called without arguments, returns a pseudo-random real number in the range [0,1). When called with a number n, random returns a pseudo-random integer in the range [1,n]. When called with two arguments, l and u, random returns a pseudo-random integer in the range [l,u].

Quote from: Abraham Lincoln. on November 04, 1971, 12:34:40 PM
Don't believe everything you read on the internet

April 29, 2012, 07:09:57 AM #2 Last Edit: April 29, 2012, 07:12:45 AM by Buckler
SK, the are the system commands.  The date command sounds like it would be useful to you.

I want to get the execute command to work.



6.5 - System Facilities

clock ()

Returns an approximation of the amount of CPU time used by the program, in seconds.

date ([format])

Returns a string containing date and time formatted according to the given string format, following the same rules of the ANSI C function strftime. When called without arguments, it returns a reasonable date and time representation that depends on the host system and on the current locale.

execute (command)

This function is equivalent to the C function system. It passes command to be executed by an operating system shell. It returns a status code, which is system-dependent.

exit (["code])  I put the " in there to keep BBCODE from interpreting this as a bbcode command.

Calls the C function exit, with an optional code, to terminate the program. The default value for code is the success code.

getenv (varname)

Returns the value of the process environment variable varname, or nil if the variable is not defined.

setlocale (locale [, category])

This function is an interface to the ANSI C function setlocale. locale is a string specifying a locale; category is an optional string describing which category to change: "all", "collate", "ctype", "monetary", "numeric", or "time"; the default category is "all". The function returns the name of the new locale, or nil if the request cannot be honored.
Quote from: Abraham Lincoln. on November 04, 1971, 12:34:40 PM
Don't believe everything you read on the internet

Mm, I'll have to see if I can set it up to do what I want.

EDIT: the date command that is.