From 2fd3d5202051e03303ac2b8e76976a7c4c8477f3 Mon Sep 17 00:00:00 2001 From: kwolekr Date: Sun, 7 Dec 2014 21:57:12 -0500 Subject: Add flags and lacunarity as new noise parameters Add 'absolute value' option to noise map functions Extend persistence modulation to 3D noise Extend 'eased' option to noise2d_perlin* functions Some noise.cpp formatting fixups --- src/script/common/c_content.cpp | 22 ++++++++++------- src/script/common/c_content.h | 4 ++-- src/script/lua_api/l_env.cpp | 6 ++--- src/script/lua_api/l_mapgen.cpp | 9 ++++--- src/script/lua_api/l_noise.cpp | 53 +++++++++++++++++++++++++++++++---------- src/script/lua_api/l_noise.h | 4 ++-- 6 files changed, 67 insertions(+), 31 deletions(-) (limited to 'src/script') diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index 799251bcf..1c78f139f 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -975,11 +975,11 @@ void luaentity_get(lua_State *L, u16 id) } /******************************************************************************/ -NoiseParams *read_noiseparams(lua_State *L, int index) +NoiseParams *get_noiseparams(lua_State *L, int index) { NoiseParams *np = new NoiseParams; - if (!read_noiseparams_nc(L, index, np)) { + if (!read_noiseparams(L, index, np)) { delete np; np = NULL; } @@ -987,7 +987,7 @@ NoiseParams *read_noiseparams(lua_State *L, int index) return np; } -bool read_noiseparams_nc(lua_State *L, int index, NoiseParams *np) +bool read_noiseparams(lua_State *L, int index, NoiseParams *np) { if (index < 0) index = lua_gettop(L) + 1 + index; @@ -995,12 +995,16 @@ bool read_noiseparams_nc(lua_State *L, int index, NoiseParams *np) if (!lua_istable(L, index)) return false; - np->offset = getfloatfield_default(L, index, "offset", 0.0); - np->scale = getfloatfield_default(L, index, "scale", 0.0); - np->persist = getfloatfield_default(L, index, "persist", 0.0); - np->seed = getintfield_default(L, index, "seed", 0); - np->octaves = getintfield_default(L, index, "octaves", 0); - np->eased = getboolfield_default(L, index, "eased", false); + np->offset = getfloatfield_default(L, index, "offset", 0.0); + np->scale = getfloatfield_default(L, index, "scale", 0.0); + np->persist = getfloatfield_default(L, index, "persist", 0.0); + np->lacunarity = getfloatfield_default(L, index, "lacunarity", 2.0); + np->seed = getintfield_default(L, index, "seed", 0); + np->octaves = getintfield_default(L, index, "octaves", 0); + + u32 flags = 0, flagmask = 0; + np->flags = getflagsfield(L, index, "flags", flagdesc_noiseparams, + &flags, &flagmask) ? flags : NOISE_FLAG_DEFAULTS; lua_getfield(L, index, "spread"); np->spread = read_v3f(L, -1); diff --git a/src/script/common/c_content.h b/src/script/common/c_content.h index 5b4dff2bd..02e3e29fa 100644 --- a/src/script/common/c_content.h +++ b/src/script/common/c_content.h @@ -147,9 +147,9 @@ bool string_to_enum (const EnumString *spec, int &result, const std::string &str); -NoiseParams* read_noiseparams (lua_State *L, int index); +NoiseParams* get_noiseparams (lua_State *L, int index); -bool read_noiseparams_nc (lua_State *L, int index, +bool read_noiseparams (lua_State *L, int index, NoiseParams *np); bool get_schematic (lua_State *L, int index, Schematic *schem, diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp index 130b15c0b..8f1f851d7 100644 --- a/src/script/lua_api/l_env.cpp +++ b/src/script/lua_api/l_env.cpp @@ -608,13 +608,13 @@ int ModApiEnvMod::l_get_perlin_map(lua_State *L) { GET_ENV_PTR; - NoiseParams *np = read_noiseparams(L, 1); - if (!np) + NoiseParams np; + if (!read_noiseparams(L, 1, &np)) return 0; v3s16 size = read_v3s16(L, 2); int seed = (int)(env->getServerMap().getSeed()); - LuaPerlinNoiseMap *n = new LuaPerlinNoiseMap(np, seed, size); + LuaPerlinNoiseMap *n = new LuaPerlinNoiseMap(&np, seed, size); *(void **)(lua_newuserdata(L, sizeof(void *))) = n; luaL_getmetatable(L, "PerlinNoiseMap"); lua_setmetatable(L, -2); diff --git a/src/script/lua_api/l_mapgen.cpp b/src/script/lua_api/l_mapgen.cpp index 5f9266c2b..78cf389e0 100644 --- a/src/script/lua_api/l_mapgen.cpp +++ b/src/script/lua_api/l_mapgen.cpp @@ -274,7 +274,10 @@ int ModApiMapgen::l_set_noiseparam_defaults(lua_State *L) lua_pushnil(L); while (lua_next(L, 1)) { - if (read_noiseparams_nc(L, -1, &np)) { + if (read_noiseparams(L, -1, &np)) { + /// TODO(hmmmm): Update this for newer noiseparam formats + /// Right now this is safe because serializeStructToString() won't + /// touch memory outside of what the format string specifies if (!serializeStructToString(&val, NOISEPARAMS_FMT_STR, &np)) continue; if (!lua_isstring(L, -2)) @@ -406,7 +409,7 @@ int ModApiMapgen::l_register_decoration(lua_State *L) //// Get NoiseParams to define how decoration is placed lua_getfield(L, index, "noise_params"); - deco->np = read_noiseparams(L, -1); + deco->np = get_noiseparams(L, -1); lua_pop(L, 1); //// Get biomes associated with this decoration (if any) @@ -556,7 +559,7 @@ int ModApiMapgen::l_register_ore(lua_State *L) getflagsfield(L, index, "flags", flagdesc_ore, &ore->flags, NULL); lua_getfield(L, index, "noise_params"); - ore->np = read_noiseparams(L, -1); + ore->np = get_noiseparams(L, -1); lua_pop(L, 1); u32 id = oremgr->add(ore); diff --git a/src/script/lua_api/l_noise.cpp b/src/script/lua_api/l_noise.cpp index 96ed93643..2a57df0f6 100644 --- a/src/script/lua_api/l_noise.cpp +++ b/src/script/lua_api/l_noise.cpp @@ -31,6 +31,7 @@ int LuaPerlinNoise::gc_object(lua_State *L) return 0; } + int LuaPerlinNoise::l_get2d(lua_State *L) { NO_MAP_LOCK_REQUIRED; @@ -40,6 +41,8 @@ int LuaPerlinNoise::l_get2d(lua_State *L) lua_pushnumber(L, val); return 1; } + + int LuaPerlinNoise::l_get3d(lua_State *L) { NO_MAP_LOCK_REQUIRED; @@ -60,10 +63,12 @@ LuaPerlinNoise::LuaPerlinNoise(int a_seed, int a_octaves, float a_persistence, { } + LuaPerlinNoise::~LuaPerlinNoise() { } + // LuaPerlinNoise(seed, octaves, persistence, scale) // Creates an LuaPerlinNoise and leaves it on top of stack int LuaPerlinNoise::create_object(lua_State *L) @@ -80,15 +85,18 @@ int LuaPerlinNoise::create_object(lua_State *L) return 1; } + LuaPerlinNoise* LuaPerlinNoise::checkobject(lua_State *L, int narg) { NO_MAP_LOCK_REQUIRED; luaL_checktype(L, narg, LUA_TUSERDATA); void *ud = luaL_checkudata(L, narg, className); - if(!ud) luaL_typerror(L, narg, className); + if (!ud) + luaL_typerror(L, narg, className); return *(LuaPerlinNoise**)ud; // unbox pointer } + void LuaPerlinNoise::Register(lua_State *L) { lua_newtable(L); @@ -117,6 +125,7 @@ void LuaPerlinNoise::Register(lua_State *L) lua_register(L, className, create_object); } + const char LuaPerlinNoise::className[] = "PerlinNoise"; const luaL_reg LuaPerlinNoise::methods[] = { luamethod(LuaPerlinNoise, get2d), @@ -124,11 +133,11 @@ const luaL_reg LuaPerlinNoise::methods[] = { {0,0} }; + /* PerlinNoiseMap */ - int LuaPerlinNoiseMap::gc_object(lua_State *L) { LuaPerlinNoiseMap *o = *(LuaPerlinNoiseMap **)(lua_touserdata(L, 1)); @@ -136,6 +145,7 @@ int LuaPerlinNoiseMap::gc_object(lua_State *L) return 0; } + int LuaPerlinNoiseMap::l_get2dMap(lua_State *L) { NO_MAP_LOCK_REQUIRED; @@ -160,6 +170,7 @@ int LuaPerlinNoiseMap::l_get2dMap(lua_State *L) return 1; } + int LuaPerlinNoiseMap::l_get2dMap_flat(lua_State *L) { NO_MAP_LOCK_REQUIRED; @@ -181,6 +192,7 @@ int LuaPerlinNoiseMap::l_get2dMap_flat(lua_State *L) return 1; } + int LuaPerlinNoiseMap::l_get3dMap(lua_State *L) { NO_MAP_LOCK_REQUIRED; @@ -190,7 +202,7 @@ int LuaPerlinNoiseMap::l_get3dMap(lua_State *L) v3f p = read_v3f(L, 2); Noise *n = o->noise; - n->perlinMap3D(p.X, p.Y, p.Z, n->np->eased); + n->perlinMap3D(p.X, p.Y, p.Z); lua_newtable(L); for (int z = 0; z != n->sz; z++) { @@ -208,6 +220,7 @@ int LuaPerlinNoiseMap::l_get3dMap(lua_State *L) return 1; } + int LuaPerlinNoiseMap::l_get3dMap_flat(lua_State *L) { NO_MAP_LOCK_REQUIRED; @@ -216,7 +229,7 @@ int LuaPerlinNoiseMap::l_get3dMap_flat(lua_State *L) v3f p = read_v3f(L, 2); Noise *n = o->noise; - n->perlinMap3D(p.X, p.Y, p.Z, n->np->eased); + n->perlinMap3D(p.X, p.Y, p.Z); int maplen = n->sx * n->sy * n->sz; @@ -230,37 +243,42 @@ int LuaPerlinNoiseMap::l_get3dMap_flat(lua_State *L) return 1; } -LuaPerlinNoiseMap::LuaPerlinNoiseMap(NoiseParams *np, int seed, v3s16 size) { + +LuaPerlinNoiseMap::LuaPerlinNoiseMap(NoiseParams *np, int seed, v3s16 size) +{ + memcpy(&m_noise_params, np, sizeof(m_noise_params)); try { - noise = new Noise(np, seed, size.X, size.Y, size.Z); + noise = new Noise(&m_noise_params, seed, size.X, size.Y, size.Z); } catch (InvalidNoiseParamsException &e) { throw LuaError(e.what()); } } + LuaPerlinNoiseMap::~LuaPerlinNoiseMap() { - delete noise->np; delete noise; } + // LuaPerlinNoiseMap(np, size) // Creates an LuaPerlinNoiseMap and leaves it on top of stack int LuaPerlinNoiseMap::create_object(lua_State *L) { - NoiseParams *np = read_noiseparams(L, 1); - if (!np) + NoiseParams np; + if (!read_noiseparams(L, 1, &np)) return 0; v3s16 size = read_v3s16(L, 2); - LuaPerlinNoiseMap *o = new LuaPerlinNoiseMap(np, 0, size); + LuaPerlinNoiseMap *o = new LuaPerlinNoiseMap(&np, 0, size); *(void **)(lua_newuserdata(L, sizeof(void *))) = o; luaL_getmetatable(L, className); lua_setmetatable(L, -2); return 1; } -LuaPerlinNoiseMap* LuaPerlinNoiseMap::checkobject(lua_State *L, int narg) + +LuaPerlinNoiseMap *LuaPerlinNoiseMap::checkobject(lua_State *L, int narg) { luaL_checktype(L, narg, LUA_TUSERDATA); @@ -271,6 +289,7 @@ LuaPerlinNoiseMap* LuaPerlinNoiseMap::checkobject(lua_State *L, int narg) return *(LuaPerlinNoiseMap **)ud; // unbox pointer } + void LuaPerlinNoiseMap::Register(lua_State *L) { lua_newtable(L); @@ -299,6 +318,7 @@ void LuaPerlinNoiseMap::Register(lua_State *L) lua_register(L, className, create_object); } + const char LuaPerlinNoiseMap::className[] = "PerlinNoiseMap"; const luaL_reg LuaPerlinNoiseMap::methods[] = { luamethod(LuaPerlinNoiseMap, get2dMap), @@ -320,6 +340,7 @@ int LuaPseudoRandom::gc_object(lua_State *L) return 0; } + // next(self, min=0, max=32767) -> get next value int LuaPseudoRandom::l_next(lua_State *L) { @@ -354,19 +375,23 @@ LuaPseudoRandom::LuaPseudoRandom(int seed): { } + LuaPseudoRandom::~LuaPseudoRandom() { } + const PseudoRandom& LuaPseudoRandom::getItem() const { return m_pseudo; } + PseudoRandom& LuaPseudoRandom::getItem() { return m_pseudo; } + // LuaPseudoRandom(seed) // Creates an LuaPseudoRandom and leaves it on top of stack int LuaPseudoRandom::create_object(lua_State *L) @@ -379,14 +404,17 @@ int LuaPseudoRandom::create_object(lua_State *L) return 1; } + LuaPseudoRandom* LuaPseudoRandom::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); + if (!ud) + luaL_typerror(L, narg, className); return *(LuaPseudoRandom**)ud; // unbox pointer } + void LuaPseudoRandom::Register(lua_State *L) { lua_newtable(L); @@ -415,6 +443,7 @@ void LuaPseudoRandom::Register(lua_State *L) lua_register(L, className, create_object); } + const char LuaPseudoRandom::className[] = "PseudoRandom"; const luaL_reg LuaPseudoRandom::methods[] = { luamethod(LuaPseudoRandom, next), diff --git a/src/script/lua_api/l_noise.h b/src/script/lua_api/l_noise.h index 65a927882..6f6173fc2 100644 --- a/src/script/lua_api/l_noise.h +++ b/src/script/lua_api/l_noise.h @@ -54,7 +54,7 @@ public: // Creates an LuaPerlinNoise and leaves it on top of stack static int create_object(lua_State *L); - static LuaPerlinNoise* checkobject(lua_State *L, int narg); + static LuaPerlinNoise *checkobject(lua_State *L, int narg); static void Register(lua_State *L); }; @@ -63,7 +63,7 @@ public: LuaPerlinNoiseMap */ class LuaPerlinNoiseMap : public ModApiBase { -private: + NoiseParams m_noise_params; Noise *noise; static const char className[]; static const luaL_reg methods[]; -- cgit v1.2.3