Skip to content

Commit 2f170a6

Browse files
committedSep 12, 2014
Simplify and optimize schematic replacements
1 parent b8ba631 commit 2f170a6

File tree

2 files changed

+29
-29
lines changed

2 files changed

+29
-29
lines changed
 

‎doc/lua_api.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1719,7 +1719,7 @@ minetest.place_schematic(pos, schematic, rotation, replacements, force_placement
17191719
^ Place the schematic specified by schematic (see: Schematic specifier) at pos.
17201720
^ Rotation can be "0", "90", "180", "270", or "random".
17211721
^ If the rotation parameter is omitted, the schematic is not rotated.
1722-
^ replacements = {{"oldname", "convert_to"}, ...}
1722+
^ replacements = {["old_name"] = "convert_to", ...}
17231723
^ force_placement is a boolean indicating whether nodes other than air and
17241724
^ ignore are replaced by the schematic
17251725

@@ -2634,7 +2634,7 @@ Decoration definition (register_decoration)
26342634
},
26352635
},
26362636
^ See 'Schematic specifier' for details.
2637-
replacements = {{"oldname", "convert_to"}, ...},
2637+
replacements = {["oldname"] = "convert_to", ...},
26382638
flags = "place_center_x, place_center_z",
26392639
^ Flags for schematic decorations. See 'Schematic attributes'.
26402640
rotation = "90" -- rotate schematic 90 degrees on placement

‎src/script/lua_api/l_mapgen.cpp

+27-27
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,31 @@ struct EnumString ModApiMapgen::es_Rotation[] =
7878
};
7979

8080

81+
static void read_schematic_replacements(lua_State *L, DecoSchematic *dschem, int index)
82+
{
83+
lua_pushnil(L);
84+
while (lua_next(L, index)) {
85+
// key at index -2 and value at index -1
86+
std::string replace_from;
87+
std::string replace_to;
88+
if (lua_istable(L, -1)) { // Old {{"x", "y"}, ...} format
89+
lua_rawgeti(L, -1, 1);
90+
replace_from = lua_tostring(L, -1);
91+
lua_pop(L, 1);
92+
lua_rawgeti(L, -1, 2);
93+
replace_to = lua_tostring(L, -1);
94+
lua_pop(L, 1);
95+
} else { // New {x = "y", ...} format
96+
replace_from = lua_tostring(L, -2);
97+
replace_to = lua_tostring(L, -1);
98+
}
99+
dschem->replacements[replace_from] = replace_to;
100+
// removes value, keeps key for next iteration
101+
lua_pop(L, 1);
102+
}
103+
}
104+
105+
81106
// get_mapgen_object(objectname)
82107
// returns the requested object used during map generation
83108
int ModApiMapgen::l_get_mapgen_object(lua_State *L)
@@ -414,20 +439,7 @@ int ModApiMapgen::l_register_decoration(lua_State *L)
414439

415440
lua_getfield(L, index, "replacements");
416441
if (lua_istable(L, -1)) {
417-
int i = lua_gettop(L);
418-
lua_pushnil(L);
419-
while (lua_next(L, i) != 0) {
420-
// key at index -2 and value at index -1
421-
lua_rawgeti(L, -1, 1);
422-
std::string replace_from = lua_tostring(L, -1);
423-
lua_pop(L, 1);
424-
lua_rawgeti(L, -1, 2);
425-
std::string replace_to = lua_tostring(L, -1);
426-
lua_pop(L, 1);
427-
dschem->replacements[replace_from] = replace_to;
428-
// removes value, keeps key for next iteration
429-
lua_pop(L, 1);
430-
}
442+
read_schematic_replacements(L, dschem, lua_gettop(L));
431443
}
432444
lua_pop(L, 1);
433445

@@ -602,19 +614,7 @@ int ModApiMapgen::l_place_schematic(lua_State *L)
602614
dschem.rotation = (Rotation)rot;
603615

604616
if (lua_istable(L, 4)) {
605-
lua_pushnil(L);
606-
while (lua_next(L, 4) != 0) {
607-
// key at index -2 and value at index -1
608-
lua_rawgeti(L, -1, 1);
609-
std::string replace_from = lua_tostring(L, -1);
610-
lua_pop(L, 1);
611-
lua_rawgeti(L, -1, 2);
612-
std::string replace_to = lua_tostring(L, -1);
613-
lua_pop(L, 1);
614-
dschem.replacements[replace_from] = replace_to;
615-
// removes value, keeps key for next iteration
616-
lua_pop(L, 1);
617-
}
617+
read_schematic_replacements(L, &dschem, 4);
618618
}
619619

620620
bool force_placement = true;

0 commit comments

Comments
 (0)
Please sign in to comment.