Skip to content
Permalink
Browse files

PerlinNoise for Lua.

  • Loading branch information
inkelyad committed Mar 31, 2012
1 parent d5594e4 commit 81d254bd77bcc1dad8a616e2dcd809444b1802e0
Showing with 135 additions and 0 deletions.
  1. +135 −0 src/scriptapi.cpp
@@ -2876,6 +2876,116 @@ static void objectref_get_or_create(lua_State *L,
}
}


/*
PerlinNoise
*/

class LuaPerlinNoise
{
private:
int seed;
int octaves;
double persistence;
static const char className[];
static const luaL_reg methods[];

// Exported functions

// garbage collector
static int gc_object(lua_State *L)
{
LuaPerlinNoise *o = *(LuaPerlinNoise **)(lua_touserdata(L, 1));
delete o;
return 0;
}

static int l_get2d(lua_State *L)
{
LuaPerlinNoise *o = checkobject(L, 1);
v2f pos2d = read_v2f(L,2);
lua_Number val = noise2d_perlin(pos2d.X, pos2d.Y, o->seed, o->octaves, o->persistence);
lua_pushnumber(L, val);
return 1;
}
static int l_get3d(lua_State *L)
{
LuaPerlinNoise *o = checkobject(L, 1);
v3f pos3d = read_v3f(L,2);
lua_Number val = noise3d_perlin(pos3d.X, pos3d.Y, pos3d.Z, o->seed, o->octaves, o->persistence);
lua_pushnumber(L, val);
return 1;
}

public:
LuaPerlinNoise(int a_seed, int a_octaves, double a_persistence):
seed(a_seed),
octaves(a_octaves),
persistence(a_persistence)
{
}

~LuaPerlinNoise()
{
}

// LuaPerlinNoise(seed, octaves, persistence)
// Creates an LuaPerlinNoise and leaves it on top of stack
static int create_object(lua_State *L)
{
int seed = luaL_checkint(L, 1);
int octaves = luaL_checkint(L, 2);
double persistence = luaL_checknumber(L, 3);
LuaPerlinNoise *o = new LuaPerlinNoise(seed, octaves, persistence);
*(void **)(lua_newuserdata(L, sizeof(void *))) = o;
luaL_getmetatable(L, className);
lua_setmetatable(L, -2);
return 1;
}

static LuaPerlinNoise* checkobject(lua_State *L, int narg)
{
luaL_checktype(L, narg, LUA_TUSERDATA);
void *ud = luaL_checkudata(L, narg, className);
if(!ud) luaL_typerror(L, narg, className);
return *(LuaPerlinNoise**)ud; // unbox pointer
}

static void Register(lua_State *L)
{
lua_newtable(L);
int methodtable = lua_gettop(L);
luaL_newmetatable(L, className);
int metatable = lua_gettop(L);

lua_pushliteral(L, "__metatable");
lua_pushvalue(L, methodtable);
lua_settable(L, metatable); // hide metatable from Lua getmetatable()

lua_pushliteral(L, "__index");
lua_pushvalue(L, methodtable);
lua_settable(L, metatable);

lua_pushliteral(L, "__gc");
lua_pushcfunction(L, gc_object);
lua_settable(L, metatable);

lua_pop(L, 1); // drop metatable

luaL_openlib(L, 0, methods, 0); // fill methodtable
lua_pop(L, 1); // drop methodtable

// Can be created from Lua (PerlinNoise(seed, octaves, persistence)
lua_register(L, className, create_object);
}
};
const char LuaPerlinNoise::className[] = "PerlinNoise";
const luaL_reg LuaPerlinNoise::methods[] = {
method(LuaPerlinNoise, get2d),
method(LuaPerlinNoise, get3d),
{0,0}
};

/*
EnvRef
*/
@@ -3169,6 +3279,26 @@ class EnvRef
return 1;
}


// EnvRef:get_perlinnoise(seeddiff, octaves, persistence)
// returns world-specific PerlinNoise
static int l_get_perlinnoise(lua_State *L)
{
EnvRef *o = checkobject(L, 1);
ServerEnvironment *env = o->m_env;
if(env == NULL) return 0;

int seeddiff = luaL_checkint(L, 2);
int octaves = luaL_checkint(L, 3);
double persistence = luaL_checknumber(L, 4);

LuaPerlinNoise *n = new LuaPerlinNoise(seeddiff + int(env->getServerMap().getSeed()), octaves, persistence);
*(void **)(lua_newuserdata(L, sizeof(void *))) = n;
luaL_getmetatable(L, "PerlinNoise");
lua_setmetatable(L, -2);
return 1;
}

static int gc_object(lua_State *L) {
EnvRef *o = *(EnvRef **)(lua_touserdata(L, 1));
delete o;
@@ -3249,13 +3379,15 @@ const luaL_reg EnvRef::methods[] = {
method(EnvRef, get_objects_inside_radius),
method(EnvRef, set_timeofday),
method(EnvRef, get_timeofday),
method(EnvRef, get_perlinnoise),
{0,0}
};

/*
LuaPseudoRandom
*/


class LuaPseudoRandom
{
private:
@@ -3367,6 +3499,8 @@ const luaL_reg LuaPseudoRandom::methods[] = {
{0,0}
};



/*
LuaABM
*/
@@ -4057,6 +4191,7 @@ void scriptapi_export(lua_State *L, Server *server)
ObjectRef::Register(L);
EnvRef::Register(L);
LuaPseudoRandom::Register(L);
LuaPerlinNoise::Register(L);
}

bool scriptapi_loadmod(lua_State *L, const std::string &scriptpath,

0 comments on commit 81d254b

Please sign in to comment.
You can’t perform that action at this time.