Add math, string, and table libraries to lua

Case number:845813-987796
Topic:Game: Tools
Opened by:Tlaloc
Status:Closed
Type:Suggestion
Opened on:Thursday, June 3, 2010 - 09:35
Last modified:Sunday, November 11, 2012 - 10:01

Can you add the math, string, and table libraries to lua. These should be a safe extensions to lua that can't cause any damage. My scripts could use things such as math.abs, math.floor, math.random, and string.format functions. If I had to have just one, it would be the math library.

(Thu, 06/03/2010 - 09:35  |  21 comments)


Joined: 09/18/2009
Groups: SETI.Germany

Oh, yes.
We already created a thread dealing with this some time ago, so it is good to remind of this suggestion.

Joined: 06/06/2008
Groups: None

Wish I could vote this up many times. Time to get people on the voting wagon. The lack of tables drives me nuts... lua is a tables based language! No math library in a math based environment is crazy! Strings would help in so many ways...

Joined: 06/06/2008
Groups: None

I've been watching the vote value on this.

Its sad.

Its not about how much this suggestion would help. Anyone that knows anything about lua knows this is infinitely invaluable. Unfortunately, there are so many people voting down since it further 'hurts' the by-hand die-hards.

Brick's picture
User offline. Last seen 11 weeks 6 days ago. Offline
Joined: 07/15/2008
Groups: Beta Folders

Perhaps it's a priorities thing. There are perhaps a half dozen or less script writers who will benefit from this enhancement ( or even understand the request!), while there are many feedbacks of all sorts that affect gameplay and product usage for the entire community. Which is more important to the general users? Which should the project focus their limited resources on?

I'd rather have someone address the rude children in chat issue than an obscure script thing, or maybe the perpetual client crashing problem, or any of the basic connection issues people have.

beta_helix's picture
User offline. Last seen 5 days 5 hours ago. Offline
Joined: 05/09/2008
Groups: None
Status: Open » Open

it is definitely a priorities thing as we are trying to resolve all the bugs first, and then our the first thing on our list is the school-chat-issue.

Thanks for your patience!

Joined: 08/06/2010
Groups: Contenders

How do I vote?

Brick's picture
User offline. Last seen 11 weeks 6 days ago. Offline
Joined: 07/15/2008
Groups: Beta Folders

The little blue up and down arrows, right under the column heading that says "Vote"

Tlaloc's picture
User offline. Last seen 9 weeks 5 days ago. Offline
Joined: 08/04/2008
Groups: Mojo Risin'

There are four categories of foldit players:

1) Writers of scripts...these write the scripts that make the whole world sing
2) Tweakers of scripts...these modify the parameters in existing scripts to optimize differently
3) Users of scripts...execute scripts that others write without modification
4) The die hard manual players that refuse to run any scripts...I don't think that these actually exist, much like the ROUS in the Princess Bride

So, while the number in category 1 is relatively low, the people in categories 1 through 3 would benefit from additional Lua features.

Joined: 09/18/2009
Groups: SETI.Germany

The easiest thing would be allowing all standard libraries, but before a script is executed, it can be searched for functions/commands which might be harmful.

Joined: 06/06/2008
Groups: None

Crash, that's not how embedded lua works. You don't add the libraries and then search for and remove commands. That's just silly. You just don't expose system-changing commands in the first place.

These 3 libraries, by default, do not have any effects on the user's system.

They only affect the scripts.

As per the class thing, I wrote the NPHS teacher, principal, asst superintendent, and superintendent, and got back 2 responses, saying it'd be dealt with. My logs have been clean of the two responsible since.

Joined: 08/06/2010
Groups: Contenders

Thanks FW for the personal touch on dealing with the school kid barnyard language.

I'm still trying to get into the head of the foldit programmers. I'm thinking they have a good reason for limitting script writers access though I'm not sure I completely understand it.

I'm wondering if there is sufficient information in the segment scores to calculate the baseline with a strong enough math library.

I was trying some experiments with locals inside tables. LUA manual says

The statement

local function f () body end

translates to

local f; f = function () body end

not to

local f = function () body end

I cannot do:

c={local f}

What I was hoping to do is create friendly variables and functions and explore the possibility of stable locals. It appears these concepts don't exist in LUA but they certainly don't exist in Foldit LUA.

The only other reference to "self" in the manual is:

The colon syntax is used for defining methods, that is, functions that have an implicit extra parameter self. Thus, the statement

function t.a.b.c:f (params) body end

is syntactic sugar for

t.a.b.c.f = function (self, params) body end

but it doesn't say if self references the function or the identifier. My concept of methods have them attached to object, that is var.tostring() would be a method referencing var's value not the function. LUA tables are a bit like a collection that could represent a class but not quite. One would have to practice safe programming rather than enforce it because we can declare persistant data without exposing it.

This brings me back to why the programmers might have disabled certain functions. If I could use the the ipairs function and had access to the core table I could walk the table and find the baseline. I'm not sure there's much science in copying the basline on top of one's structure. I started to disassemble foldit.exe and find the access points to the functions but decided against it. (It seems like too much work anyway.)

Joined: 06/06/2008
Groups: None

Allowing ipairs doesn't open up the core tables. Creating a Lua to C function to make the core tables visible does.

The reason I'm advocating the addition of the tables library is it allows for far cleaner lua. Lua is a tables based language. By that, you make a function, and its an entry in the global table. Even in your example, you show t.a.b.c, a function technically nested deep in a table. (For those who know lua well, yes yes, semantics... but its a good laymans summary for scripters that aren't in tune with finer details.)

On the upside, I found a (far slower than having it written in C and exposed as a function) implementation of unpack. With enough effort, many of the functions can be rewritten in lua anyway. The downside is speed. Having them written such that they are optimized machine code to begin with, and being exposed functions would make them faster. Look at tlaloc's math library... its most of math, but written in lua. Which has to be read in, compiled for use, then ran. Being compiled at run time means its not getting optimizations from an in depth compiler. The run time compile lua performs is pretty basic. Don't even get me started on his random. Hoping to find a better way to do it.

The strings library works well with the math library for allowing all sorts of neat math tricks. Something that fold.it is based on. Between the string, math, and tables libraries, there's no IO, its all safe to the user, and it opens up a world of easier programming for scripters, which improves everything for all users except those who are die hard by handers with no scripts in their cookbook.

Very very few functions in those 3 libraries can't be implemented in lua, its just that writing them in lua causes a huge performance hit.

Joined: 08/06/2010
Groups: Contenders

OK, I found unpack as well,

function unpack (t, i)
i = i or 1
if t[i] ~= nil then
return t[i], unpack(t, i + 1)
end
end

That's only useful for integer indexes.

I guess I could "register" methods in a subordinate table with a fixed name.
I'll look at a for each loop using unpack tonight.

Joined: 06/06/2008
Groups: None

The primary purpose for unpack is to handle the ... argument with ease.

It would be good for, say, passing an array of segments, and doing work on them mathematically. Its also useful for basically overloading functions. Honestly, its the least of the things I'd like to see included simply because its so easily redone in the most basic lua.

Other things, like string manipulation or the math library CAN be done, but not easily, and its far more processor intensive. For instance, look at tlaloc's reimplementation of math.*

(I've been working on my own revamp of the math library, and looking to find a better means to create random numbers)

Joined: 09/16/2010
Groups: None

This is a great suggestion...

I could think of a dozen uses for a table in a few seconds.

Also, just last week I was frustrated with the 'Break' function being missing.
Oh how nice it would be to break out of loops. (i guess i am showing my scripting skill in asking for a 'Hack command' ... i know workarounds could be coded, but you want to make this easy for us to do. Break is easy )

The implementation of these libraries into foldit looks like it has been well thought out

http://foldit.wikia.com/wiki/Lua_Standard_Libraries

It just needs to be implemented.

Please implement these features.

Joined: 08/06/2010
Groups: Contenders

I was growing tired of waiting so I'm adding my own lua implementations as I find time. Please add your own or improved versions of existing implementations. The sort was dearly missed. I didn't know I could pass a function definition as a parameter until I tried it. The sort is an insertion sort. This is faster than a bubble sort but not much more complex. I'd like to add random, and will, but I don't want to add a complex random routine. I may just include the one from tlaloc's scripts. Alternatively, I may include an even simpler one. How good does a random routine need to be for foldit?

Anyway, just include the functions you want in your scripts after adding an empty table defiition for the particuar library.

math={}
table={}
function math.max(x,...)
(etc)

I'll add gary.fuse this evening. It will be a generalized version of bluefuse where one can pass a table of clashing values or not and possibly some other parameters (for instance a list of segments).

Feel free to add comments as well but use a different color font if you want me to see it quickly. I'm not going to work on the string functions because they are of little use in foldit and would be very inefficient since I would have to atomize the strings first by building a string and comparing it against the string passed.

I also have a surpize function I'll implement since I can't get recipe output into a file for loading back into a recipe.

Please, everyone, share your implementations you think others would like to include in their scripts.

Tlaloc's picture
User offline. Last seen 9 weeks 5 days ago. Offline
Joined: 08/04/2008
Groups: Mojo Risin'
Status: Open » Closed

Lua math, string, table, parts of os and the base library are implemented and will show in main soon.

ipatrol's picture
User offline. Last seen 6 years 7 weeks ago. Offline
Joined: 12/04/2010
Groups: None
Status: Closed » Open
Type: Suggestion » Suggestion

I still don't see it after 4 months, where the he-eleven is it? Trapped in the same paranoid fear that has kept Foldit closed source?

Joined: 06/17/2010
Status: Open » Open

It is part of LUA V2.
It is available in dev preview.

beta_helix's picture
User offline. Last seen 5 days 5 hours ago. Offline
Joined: 05/09/2008
Groups: None
Status: Open » Closed

this has been implemented into LUA V2

Tlaloc's picture
User offline. Last seen 9 weeks 5 days ago. Offline
Joined: 08/04/2008
Groups: Mojo Risin'
Status: Closed » Closed

Done

Sitemap

Developed by: UW Center for Game Science, UW Institute for Protein Design, Northeastern University, Vanderbilt University Meiler Lab, UC Davis
Supported by: DARPA, NSF, NIH, HHMI, Amazon, Microsoft, Adobe, RosettaCommons