Skip to content

Commit 45589fa

Browse files
committedAug 3, 2013
Add replacements to schematics
1 parent 5e433fa commit 45589fa

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed
 

‎doc/lua_api.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -1439,10 +1439,11 @@ minetest.create_schematic(p1, p2, probability_list, filename)
14391439
^ If probability_list is nil, no probabilities are applied.
14401440
^ Saves schematic in the Minetest Schematic format to filename.
14411441

1442-
minetest.place_schematic(pos, schematic, rotation)
1442+
minetest.place_schematic(pos, schematic, rotation, replacements)
14431443
^ Place the schematic specified by schematic (see: Schematic specifier) at pos.
14441444
^ Rotation can be "0", "90", "180", "270", or "random".
14451445
^ If the rotation parameter is omitted, the schematic is not rotated.
1446+
^ replacements = {{"oldname", "convert_to"}, ...}
14461447

14471448
Random:
14481449
minetest.get_connected_players() -> list of ObjectRefs
@@ -2129,6 +2130,7 @@ Ore definition (register_ore)
21292130
ore_type = "scatter", -- See "Ore types"
21302131
ore = "default:stone_with_coal",
21312132
wherein = "default:stone",
2133+
^ a list of nodenames is supported too
21322134
clust_scarcity = 8*8*8,
21332135
^ Ore has a 1 out of clust_scarcity chance of spawning in a node
21342136
^ This value should be *MUCH* higher than your intuition might tell you!
@@ -2196,6 +2198,7 @@ Decoration definition (register_decoration)
21962198
}
21972199
},
21982200
^ See 'Schematic specifier' for details.
2201+
replacements = {{"oldname", "convert_to"}, ...},
21992202
flags = "place_center_x, place_center_z",
22002203
^ Flags for schematic decorations. See 'Schematic attributes'.
22012204
rotation = "90" --rotate schematic 90 degrees on placement

‎src/mapgen.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,12 @@ void DecoSchematic::resolveNodeNames(INodeDefManager *ndef) {
506506
}
507507

508508
for (size_t i = 0; i != node_names->size(); i++) {
509-
content_t c = ndef->getId(node_names->at(i));
509+
std::string name = node_names->at(i);
510+
std::map<std::string, std::string>::iterator it;
511+
it = replacements.find(name);
512+
if (it != replacements.end())
513+
name = it->second;
514+
content_t c = ndef->getId(name);
510515
if (c == CONTENT_IGNORE) {
511516
errorstream << "DecoSchematic::resolveNodeNames: node '"
512517
<< node_names->at(i) << "' not defined" << std::endl;

‎src/mapgen.h

+1
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ class DecoSchematic : public Decoration {
269269

270270
std::vector<std::string> *node_names;
271271
std::vector<content_t> c_nodes;
272+
std::map<std::string, std::string> replacements;
272273

273274
u32 flags;
274275
Rotation rotation;

‎src/script/lua_api/luaapi.cpp

+38-2
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,26 @@ int ModApiBasic::l_register_decoration(lua_State *L)
808808
dschem->flags = getflagsfield(L, index, "flags", flagdesc_deco_schematic);
809809
dschem->rotation = (Rotation)getenumfield(L, index,
810810
"rotation", es_Rotation, ROTATE_0);
811-
811+
812+
lua_getfield(L, index, "replacements");
813+
if (lua_istable(L, -1)) {
814+
int i = lua_gettop(L);
815+
lua_pushnil(L);
816+
while (lua_next(L, i) != 0) {
817+
// key at index -2 and value at index -1
818+
lua_rawgeti(L, -1, 1);
819+
std::string replace_from = lua_tostring(L, -1);
820+
lua_pop(L, 1);
821+
lua_rawgeti(L, -1, 2);
822+
std::string replace_to = lua_tostring(L, -1);
823+
lua_pop(L, 1);
824+
dschem->replacements[replace_from] = replace_to;
825+
// removes value, keeps key for next iteration
826+
lua_pop(L, 1);
827+
}
828+
}
829+
lua_pop(L, 1);
830+
812831
lua_getfield(L, index, "schematic");
813832
if (!read_schematic(L, -1, dschem, getServer(L))) {
814833
delete dschem;
@@ -888,7 +907,7 @@ int ModApiBasic::l_create_schematic(lua_State *L)
888907
}
889908

890909

891-
// place_schematic(p, schematic, rotation)
910+
// place_schematic(p, schematic, rotation, replacement)
892911
int ModApiBasic::l_place_schematic(lua_State *L)
893912
{
894913
DecoSchematic dschem;
@@ -906,6 +925,23 @@ int ModApiBasic::l_place_schematic(lua_State *L)
906925

907926
dschem.rotation = rot;
908927

928+
if (lua_istable(L, 4)) {
929+
int index = 4;
930+
lua_pushnil(L);
931+
while (lua_next(L, index) != 0) {
932+
// key at index -2 and value at index -1
933+
lua_rawgeti(L, -1, 1);
934+
std::string replace_from = lua_tostring(L, -1);
935+
lua_pop(L, 1);
936+
lua_rawgeti(L, -1, 2);
937+
std::string replace_to = lua_tostring(L, -1);
938+
lua_pop(L, 1);
939+
dschem.replacements[replace_from] = replace_to;
940+
// removes value, keeps key for next iteration
941+
lua_pop(L, 1);
942+
}
943+
}
944+
909945
if (!dschem.filename.empty()) {
910946
if (!dschem.loadSchematicFile()) {
911947
errorstream << "place_schematic: failed to load schematic file '"

0 commit comments

Comments
 (0)
Please sign in to comment.