Skip to content

Commit

Permalink
add lua libs for lua map generator, also add bindings for mt rng
Browse files Browse the repository at this point in the history
Tested to work with this test scenario, and the mainline defaults:

     [label]
         x = {X}
         y = {Y}
         text = {STRING}
     [/label]
[multiplayer]
    id=lua_map_gen
    name= _ "Lua Map Gen Test Scenario"
    description= _ "test test test of lua map gen"
    map_generation="lua"
    [generator]
        id="test"
	config_name="Test Lua Map Generator"
	create_map = << local rng = Rng:create()

			print(rng:draw())
			print(rng:draw())
			print(rng:draw())

			local w = 50
		        local h = 40

			map=""
			for y=1,h do
			  local r = rng:draw() % 2
			  for x=1,w do
                            if x == 10 and y == 10 then
                              map = map .. " 1 "
 			    end
                            if x == (w-10) and y == (h-10) then
                              map = map .. " 2 "
 			    end
			    if ((x + y) % 2) == r then
			      map = map .. "Gg"
			    else
			      map = map .. "Md"
			    end
                            if x ~= w then
                              map = map .. ","
                            end
                          end
                          map = map .. "\n"
                        end
                        return map
                     >>
    [/generator]

    id = foo
    random_start_time=yes

    {DEFAULT_SCHEDULE}
    [event]
        name=prestart
        {LABEL 25 20 ("Lua map generator")}
    [/event]

    [side]
         [ai]
             villages_per_scout=8
         [/ai]
         id=RBY_Side1
         side=1
         save_id=RBY_Side1
         persistent=yes
         color=red
         team_name=Red
         user_team_name= _ "teamname^Red"
         controller=human
         canrecruit=yes
         shroud=no
         fog=no
         gold=1000000
    [/side]
    [side]
         [ai]
             villages_per_scout=8
         [/ai]
         id=RBY_Side2
         side=2
         save_id=RBY_Side2
         persistent=yes
         color=blue
         team_name=Blue
         user_team_name= _ "teamname^Blue"
         controller=human
         canrecruit=yes
         shroud=no
         fog=no
         gold=1000000
    [/side]
[/multiplayer]
  • Loading branch information
cbeck88 committed Nov 5, 2014
1 parent 2da4c91 commit ce99658
Showing 1 changed file with 118 additions and 0 deletions.
118 changes: 118 additions & 0 deletions src/generators/lua_map_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include "lua/lua.h"
#include "lua/lualib.h"

#include "mt_rng.hpp"

#ifdef DEBUG_LUA
#include "scripting/debug_lua.hpp"
#endif
Expand All @@ -30,6 +32,120 @@

#include <boost/foreach.hpp>


// Add compiler directive suppressing unused variable warning
#if defined(__GNUC__) || defined(__clang__) || defined(__MINGW32__)
#define ATTR_UNUSED( x ) __attribute__((unused)) x
#else
#define ATTR_UNUSED( x ) x
#endif

// Begin lua rng bindings

using rand_rng::mt_rng;

static const char * Rng = "Rng";

static int impl_rng_create(lua_State* L);
static int impl_rng_destroy(lua_State* L);
static int impl_rng_seed(lua_State* L);
static int impl_rng_draw(lua_State* L);

static void initialize_lua_state(lua_State * L)
{
// Open safe libraries.
// Debug and OS are not, but most of their functions will be disabled below.
static const luaL_Reg safe_libs[] = {
{ "", luaopen_base },
{ "table", luaopen_table },
{ "string", luaopen_string },
{ "math", luaopen_math },
{ "debug", luaopen_debug },
{ "os", luaopen_os },
{ NULL, NULL }
};
for (luaL_Reg const *lib = safe_libs; lib->func; ++lib)
{
luaL_requiref(L, lib->name, lib->func, 1);
lua_pop(L, 1); /* remove lib */
}

// Disable functions from os which we don't want.
lua_getglobal(L, "os");
lua_pushnil(L);
while(lua_next(L, -2) != 0) {
lua_pop(L, 1);
char const* function = lua_tostring(L, -1);
if(strcmp(function, "clock") == 0 || strcmp(function, "date") == 0
|| strcmp(function, "time") == 0 || strcmp(function, "difftime") == 0) continue;
lua_pushnil(L);
lua_setfield(L, -3, function);
}
lua_pop(L, 1);

// Disable functions from debug which we don't want.
lua_getglobal(L, "debug");
lua_pushnil(L);
while(lua_next(L, -2) != 0) {
lua_pop(L, 1);
char const* function = lua_tostring(L, -1);
if(strcmp(function, "traceback") == 0) continue;
lua_pushnil(L);
lua_setfield(L, -3, function);
}
lua_pop(L, 1);

lua_settop(L, 0);

// Add mersenne twister rng wrapper

luaL_newmetatable(L, Rng);

static luaL_Reg const callbacks[] = {
{ "create", &impl_rng_create},
{ "__gc", &impl_rng_destroy},
{ "seed", &impl_rng_seed},
{ "draw", &impl_rng_draw},
{ NULL, NULL }
};
luaL_setfuncs(L, callbacks, 0);

lua_pushvalue(L, -1); //make a copy of this table, set it to be its own __index table
lua_setfield(L, -2, "__index");

lua_setglobal(L, Rng);
}

int impl_rng_create(lua_State* L)
{
mt_rng * ATTR_UNUSED(rng) = new ( lua_newuserdata(L, sizeof(mt_rng)) ) mt_rng();
luaL_setmetatable(L, Rng);

return 1;
}
int impl_rng_destroy(lua_State* L)
{
static_cast< mt_rng * >( lua_touserdata( L , 1 ) )->~mt_rng();
return 0;
}
int impl_rng_seed(lua_State* L)
{
mt_rng * rng = static_cast<mt_rng *>(luaL_checkudata(L, 1, Rng));
std::string seed = luaL_checkstring(L, 2);

rng->seed_random(seed);
return 0;
}
int impl_rng_draw(lua_State* L)
{
mt_rng * rng = static_cast<mt_rng *>(luaL_checkudata(L, 1, Rng));

lua_pushnumber(L, rng->get_next_random());
return 1;
}

// End Lua Rng bindings

lua_map_generator::lua_map_generator(const config & cfg)
: id_(cfg["id"])
, config_name_(cfg["config_name"])
Expand All @@ -45,6 +161,8 @@ lua_map_generator::lua_map_generator(const config & cfg)
throw mapgen_exception(msg);
}
}

initialize_lua_state(mState_);
}

lua_map_generator::~lua_map_generator()
Expand Down

0 comments on commit ce99658

Please sign in to comment.