diff options
author | kwolekr <kwolekr@minetest.net> | 2013-06-26 17:19:39 -0400 |
---|---|---|
committer | kwolekr <kwolekr@minetest.net> | 2013-06-27 22:35:35 -0400 |
commit | 8aa930f28e69f3518831500022988ca2a4b6985d (patch) | |
tree | 5699c9b0cd8eaa54f4fadf8dcd7d0ba98a9c3c74 /src/script | |
parent | 2c0b51795e6fa6747d881f1871c89830abb6e6e8 (diff) | |
download | minetest-8aa930f28e69f3518831500022988ca2a4b6985d.tar.gz minetest-8aa930f28e69f3518831500022988ca2a4b6985d.tar.bz2 minetest-8aa930f28e69f3518831500022988ca2a4b6985d.zip |
Add minetest.get_mapgen_object to API
Diffstat (limited to 'src/script')
-rw-r--r-- | src/script/lua_api/l_env.cpp | 108 | ||||
-rw-r--r-- | src/script/lua_api/l_env.h | 8 | ||||
-rw-r--r-- | src/script/lua_api/l_item.cpp | 2 | ||||
-rw-r--r-- | src/script/lua_api/l_vmanip.cpp | 10 | ||||
-rw-r--r-- | src/script/lua_api/l_vmanip.h | 2 |
5 files changed, 124 insertions, 6 deletions
diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp index 6f663646c..b5c40977e 100644 --- a/src/script/lua_api/l_env.cpp +++ b/src/script/lua_api/l_env.cpp @@ -35,11 +35,27 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "lua_api/l_noise.h" #include "treegen.h" #include "pathfinder.h" +#include "emerge.h" +#include "mapgen_v7.h" #define GET_ENV_PTR ServerEnvironment* env = \ dynamic_cast<ServerEnvironment*>(getEnv(L)); \ if( env == NULL) return 0 + +struct EnumString ModApiEnvMod::es_MapgenObject[] = +{ + {MGOBJ_VMANIP, "voxelmanip"}, + {MGOBJ_HEIGHTMAP, "heightmap"}, + {MGOBJ_BIOMEMAP, "biomemap"}, + {MGOBJ_HEATMAP, "heatmap"}, + {MGOBJ_HUMIDMAP, "humiditymap"}, + {0, NULL}, +}; + + +/////////////////////////////////////////////////////////////////////////////// + void LuaABM::trigger(ServerEnvironment *env, v3s16 p, MapNode n, u32 active_object_count, u32 active_object_count_wider) @@ -541,14 +557,101 @@ int ModApiEnvMod::l_get_voxel_manip(lua_State *L) GET_ENV_PTR; Map *map = &(env->getMap()); - LuaVoxelManip *vm = new LuaVoxelManip(map); + LuaVoxelManip *o = new LuaVoxelManip(map); - *(void **)(lua_newuserdata(L, sizeof(void *))) = vm; + *(void **)(lua_newuserdata(L, sizeof(void *))) = o; luaL_getmetatable(L, "VoxelManip"); lua_setmetatable(L, -2); return 1; } +// minetest.get_mapgen_object(objectname) +// returns the requested object used during map generation +int ModApiEnvMod::l_get_mapgen_object(lua_State *L) +{ + const char *mgobjstr = lua_tostring(L, 1); + + int mgobjint; + if (!string_to_enum(es_MapgenObject, mgobjint, mgobjstr ? mgobjstr : "")) + return 0; + + enum MapgenObject mgobj = (MapgenObject)mgobjint; + + EmergeManager *emerge = getServer(L)->getEmergeManager(); + Mapgen *mg = emerge->getCurrentMapgen(); + + size_t maplen = mg->csize.X * mg->csize.Z; + + int nargs = 1; + + switch (mgobj) { + case MGOBJ_VMANIP: { + ManualMapVoxelManipulator *vm = mg->vm; + + // VoxelManip object + LuaVoxelManip *o = new LuaVoxelManip(vm, false); + *(void **)(lua_newuserdata(L, sizeof(void *))) = o; + luaL_getmetatable(L, "VoxelManip"); + lua_setmetatable(L, -2); + + // VoxelManip data + int volume = vm->m_area.getVolume(); + lua_newtable(L); + for (int i = 0; i != volume; i++) { + lua_Number cid = vm->m_data[i].getContent(); + lua_pushnumber(L, cid); + lua_rawseti(L, -2, i + 1); + } + + // emerged min pos + push_v3s16(L, vm->m_area.MinEdge); + + // emerged max pos + push_v3s16(L, vm->m_area.MaxEdge); + + nargs = 4; + + break; } + case MGOBJ_HEIGHTMAP: { + if (!mg->heightmap) + return 0; + + lua_newtable(L); + for (size_t i = 0; i != maplen; i++) { + lua_pushinteger(L, mg->heightmap[i]); + lua_rawseti(L, -2, i + 1); + } + break; } + case MGOBJ_BIOMEMAP: { + if (!mg->heightmap) + return 0; + + lua_newtable(L); + for (size_t i = 0; i != maplen; i++) { + lua_pushinteger(L, mg->biomemap[i]); + lua_rawseti(L, -2, i + 1); + } + break; } + case MGOBJ_HEATMAP: { // Mapgen V7 specific objects + case MGOBJ_HUMIDMAP: + MapgenV7 *mgv7 = (MapgenV7 *)mg; + + float *arr = (mgobj == MGOBJ_HEATMAP) ? + mgv7->noise_heat->result : mgv7->noise_humidity->result; + if (!arr) + return 0; + + lua_newtable(L); + for (size_t i = 0; i != maplen; i++) { + lua_pushnumber(L, arr[i]); + lua_rawseti(L, -2, i + 1); + } + break; } + } + + return nargs; +} + // minetest.clear_objects() // clear all objects in the environment int ModApiEnvMod::l_clear_objects(lua_State *L) @@ -695,6 +798,7 @@ bool ModApiEnvMod::Initialize(lua_State *L,int top) retval &= API_FCT(get_perlin); retval &= API_FCT(get_perlin_map); retval &= API_FCT(get_voxel_manip); + retval &= API_FCT(get_mapgen_object); retval &= API_FCT(clear_objects); retval &= API_FCT(spawn_tree); retval &= API_FCT(find_path); diff --git a/src/script/lua_api/l_env.h b/src/script/lua_api/l_env.h index 24700d27c..8f769e350 100644 --- a/src/script/lua_api/l_env.h +++ b/src/script/lua_api/l_env.h @@ -113,6 +113,10 @@ private: // minetest.get_voxel_manip() // returns world-specific voxel manipulator static int l_get_voxel_manip(lua_State *L); + + // minetest.get_mapgen_object(objectname) + // returns the requested object used during map generation + static int l_get_mapgen_object(lua_State *L); // minetest.clear_objects() // clear all objects in the environment @@ -127,7 +131,9 @@ private: // minetest.find_path(pos1, pos2, searchdistance, // max_jump, max_drop, algorithm) -> table containing path static int l_find_path(lua_State *L); - + + static struct EnumString es_MapgenObject[]; + public: bool Initialize(lua_State *L, int top); }; diff --git a/src/script/lua_api/l_item.cpp b/src/script/lua_api/l_item.cpp index c69562dda..4069c61ce 100644 --- a/src/script/lua_api/l_item.cpp +++ b/src/script/lua_api/l_item.cpp @@ -466,7 +466,7 @@ int ModApiItemMod::l_get_content_id(lua_State *L) INodeDefManager *ndef = STACK_TO_SERVER(L)->getNodeDefManager(); content_t c = ndef->getId(name); - lua_pushnumber(L, c); + lua_pushinteger(L, c); return 1; /* number of results */ } diff --git a/src/script/lua_api/l_vmanip.cpp b/src/script/lua_api/l_vmanip.cpp index e0397dfce..6743f40f9 100644 --- a/src/script/lua_api/l_vmanip.cpp +++ b/src/script/lua_api/l_vmanip.cpp @@ -33,7 +33,8 @@ with this program; if not, write to the Free Software Foundation, Inc., int LuaVoxelManip::gc_object(lua_State *L) { LuaVoxelManip *o = *(LuaVoxelManip **)(lua_touserdata(L, 1)); - delete o; + if (o->do_gc) + delete o; return 0; } @@ -82,7 +83,6 @@ int LuaVoxelManip::l_write_chunk(lua_State *L) vm->m_data[i].setContent(c); lua_pop(L, 1); - } vm->blitBackAll(&o->modified_blocks); @@ -184,6 +184,12 @@ int LuaVoxelManip::l_update_map(lua_State *L) return 0; } +LuaVoxelManip::LuaVoxelManip(ManualMapVoxelManipulator *mmvm, bool dogc) +{ + this->vm = mmvm; + this->do_gc = dogc; +} + LuaVoxelManip::LuaVoxelManip(Map *map) { vm = new ManualMapVoxelManipulator(map); diff --git a/src/script/lua_api/l_vmanip.h b/src/script/lua_api/l_vmanip.h index 568f7104e..5a57d6bfa 100644 --- a/src/script/lua_api/l_vmanip.h +++ b/src/script/lua_api/l_vmanip.h @@ -36,6 +36,7 @@ class LuaVoxelManip private: ManualMapVoxelManipulator *vm; std::map<v3s16, MapBlock *> modified_blocks; + bool do_gc; static const char className[]; static const luaL_reg methods[]; @@ -50,6 +51,7 @@ private: static int l_set_lighting(lua_State *L); public: + LuaVoxelManip(ManualMapVoxelManipulator *mmvm, bool dogc); LuaVoxelManip(Map *map); ~LuaVoxelManip(); |