Simplify and optimize schematic replacements
authorShadowNinja <shadowninja@minetest.net>
Fri, 12 Sep 2014 02:25:06 +0000 (22:25 -0400)
committerShadowNinja <shadowninja@minetest.net>
Fri, 12 Sep 2014 02:25:06 +0000 (22:25 -0400)
doc/lua_api.txt
src/script/lua_api/l_mapgen.cpp

index 5fd10d418ff480a1e53d2111c1cdc14e75fbe3fe..37477b60a30ca0bceb393d9eae920061e93ec07f 100644 (file)
@@ -1719,7 +1719,7 @@ minetest.place_schematic(pos, schematic, rotation, replacements, force_placement
 ^ Place the schematic specified by schematic (see: Schematic specifier) at pos.
 ^ Rotation can be "0", "90", "180", "270", or "random".
 ^ If the rotation parameter is omitted, the schematic is not rotated.
-^ replacements = {{"oldname", "convert_to"}, ...}
+^ replacements = {["old_name"] = "convert_to", ...}
 ^ force_placement is a boolean indicating whether nodes other than air and
 ^ ignore are replaced by the schematic
 
@@ -2634,7 +2634,7 @@ Decoration definition (register_decoration)
         },
     },
     ^ See 'Schematic specifier' for details.
-    replacements = {{"oldname", "convert_to"}, ...},
+    replacements = {["oldname"] = "convert_to", ...},
     flags = "place_center_x, place_center_z",
     ^ Flags for schematic decorations.  See 'Schematic attributes'.
     rotation = "90" -- rotate schematic 90 degrees on placement
index 287faade93e74ba2923a6da00bb1994f0dd8ea76..c6d41050be13d77c4ef5f05a2999dfd9a2bf2ff5 100644 (file)
@@ -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;