lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I have been working on a sandbox environment for lua, aimed at programming competitions in the form of bots. An easy example would be two bots playing Gomoku (which i have implemented sample bots for). In order to provide fairness and prevent cheating I have a system where each bot has its own state, with limited memory. A master state can then control slave states by calling slave functions and providing certain functions. In order to limit the processing time of the bots (and to make it fair) I'm using hooks to set a maximum amount of operations (and removing problematic functions such as string matching, where the processing time isn't constant).

Now, I want my bots to get an incentive to try to run as fast as possible in most cases, and for that I need to see how many operations it actually used.
Currently I have my own build of Lua, but I would rather use the default Lua if possible, so I propose that the following patch be included in Lua.

The patch is quite small:

diff -r -u lua-5.1.1/src/ldebug.c lua-5.1.1-hookcount/src/ldebug.c
--- lua-5.1.1/src/ldebug.c    2005-12-22 17:19:56.000000000 +0100
+++ lua-5.1.1-hookcount/src/ldebug.c    2006-09-10 17:55: 05.000000000 +0200
@@ -80,6 +80,10 @@
   return L->basehookcount;
 }
 
+LUA_API int lua_gethookcountremaining (lua_State *L) {
+  return L->hookcount;
+}
+
 
 LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
   int status;
diff -r -u lua-5.1.1/src/lua.h lua-5.1.1-hookcount/src/lua.h
--- lua-5.1.1/src/lua.h    2006-06-02 17:34:00.000000000 +0200
+++ lua-5.1.1-hookcount/src/lua.h    2006-09-10 17:55:05.000000000 +0200
@@ -338,6 +338,7 @@
 LUA_API lua_Hook lua_gethook (lua_State *L);
 LUA_API int lua_gethookmask (lua_State *L);
 LUA_API int lua_gethookcount (lua_State *L);
+LUA_API int lua_gethookcountremaining (lua_State *L);
 
 
 struct lua_Debug {