diff options
author | ShadowNinja <shadowninja@minetest.net> | 2014-09-11 22:25:06 -0400 |
---|---|---|
committer | ShadowNinja <shadowninja@minetest.net> | 2014-09-11 22:25:06 -0400 |
commit | 2f170a63c6df9bff7fcf819226f5937a902bf335 (patch) | |
tree | 1c3e5a3a57ab62ef5b9581e5ff2b013745889b1a /src/script/lua_api | |
parent | b8ba6318d6ed9edd59455674d39ff1d5018393df (diff) | |
download | minetest-2f170a63c6df9bff7fcf819226f5937a902bf335.tar.gz minetest-2f170a63c6df9bff7fcf819226f5937a902bf335.tar.bz2 minetest-2f170a63c6df9bff7fcf819226f5937a902bf335.zip |
Simplify and optimize schematic replacements
Diffstat (limited to 'src/script/lua_api')
-rw-r--r-- | src/script/lua_api/l_mapgen.cpp | 54 |
1 files changed, 27 insertions, 27 deletions
diff --git a/src/script/lua_api/l_mapgen.cpp b/src/script/lua_api/l_mapgen.cpp index 287faade9..c6d41050b 100644 --- a/src/script/lua_api/l_mapgen.cpp +++ b/src/script/lua_api/l_mapgen.cpp @@ -78,6 +78,31 @@ struct EnumString ModApiMapgen::es_Rotation[] = }; +static void read_schematic_replacements(lua_State *L, DecoSchematic *dschem, int index) +{ + lua_pushnil(L); + while (lua_next(L, index)) { + // key at index -2 and value at index -1 + std::string replace_from; + std::string replace_to; + if (lua_istable(L, -1)) { // Old {{"x", "y"}, ...} format + lua_rawgeti(L, -1, 1); + replace_from = lua_tostring(L, -1); + lua_pop(L, 1); + lua_rawgeti(L, -1, 2); + replace_to = lua_tostring(L, -1); + lua_pop(L, 1); + } else { // New {x = "y", ...} format + replace_from = lua_tostring(L, -2); + replace_to = lua_tostring(L, -1); + } + dschem->replacements[replace_from] = replace_to; + // removes value, keeps key for next iteration + lua_pop(L, 1); + } +} + + // get_mapgen_object(objectname) // returns the requested object used during map generation int ModApiMapgen::l_get_mapgen_object(lua_State *L) @@ -414,20 +439,7 @@ int ModApiMapgen::l_register_decoration(lua_State *L) lua_getfield(L, index, "replacements"); if (lua_istable(L, -1)) { - int i = lua_gettop(L); - lua_pushnil(L); - while (lua_next(L, i) != 0) { - // key at index -2 and value at index -1 - lua_rawgeti(L, -1, 1); - std::string replace_from = lua_tostring(L, -1); - lua_pop(L, 1); - lua_rawgeti(L, -1, 2); - std::string replace_to = lua_tostring(L, -1); - lua_pop(L, 1); - dschem->replacements[replace_from] = replace_to; - // removes value, keeps key for next iteration - lua_pop(L, 1); - } + read_schematic_replacements(L, dschem, lua_gettop(L)); } lua_pop(L, 1); @@ -602,19 +614,7 @@ int ModApiMapgen::l_place_schematic(lua_State *L) dschem.rotation = (Rotation)rot; if (lua_istable(L, 4)) { - lua_pushnil(L); - while (lua_next(L, 4) != 0) { - // key at index -2 and value at index -1 - lua_rawgeti(L, -1, 1); - std::string replace_from = lua_tostring(L, -1); - lua_pop(L, 1); - lua_rawgeti(L, -1, 2); - std::string replace_to = lua_tostring(L, -1); - lua_pop(L, 1); - dschem.replacements[replace_from] = replace_to; - // removes value, keeps key for next iteration - lua_pop(L, 1); - } + read_schematic_replacements(L, &dschem, 4); } bool force_placement = true; |