1
0
mirror of https://github.com/moparisthebest/minetest synced 2024-08-13 16:53:49 -04:00

Schematics: Add indent-with-space option for schematic Lua table serialization

This commit is contained in:
kwolekr 2015-05-07 02:54:30 -04:00
parent 656575b59d
commit b246812455
4 changed files with 22 additions and 13 deletions

View File

@ -2178,8 +2178,10 @@ These functions return the leftover itemstack.
* "mts" - a string containing the binary MTS data used in the MTS file format * "mts" - a string containing the binary MTS data used in the MTS file format
* "lua" - a string containing Lua code representing the schematic in table format * "lua" - a string containing Lua code representing the schematic in table format
* `options` is a table containing the following optional parameters: * `options` is a table containing the following optional parameters:
* If `use_comments` is true and `format` is "lua", the Lua code generated will have (X, Z) * If `lua_use_comments` is true and `format` is "lua", the Lua code generated will have (X, Z)
* position comments for every X row generated in the schematic data for easier reading. * position comments for every X row generated in the schematic data for easier reading.
* If `lua_num_indent_spaces` is a nonzero number and `format` is "lua", the Lua code generated
* will use that number of spaces as indentation instead of a tab character.
### Misc. ### Misc.
* `minetest.get_connected_players()`: returns list of `ObjectRefs` * `minetest.get_connected_players()`: returns list of `ObjectRefs`

View File

@ -309,14 +309,18 @@ bool Schematic::serializeToMts(std::ostream *os,
bool Schematic::serializeToLua(std::ostream *os, bool Schematic::serializeToLua(std::ostream *os,
const std::vector<std::string> &names, bool use_comments) const std::vector<std::string> &names, bool use_comments, u32 indent_spaces)
{ {
std::ostream &ss = *os; std::ostream &ss = *os;
std::string indent("\t");
if (indent_spaces > 0)
indent.assign(indent_spaces, ' ');
//// Write header //// Write header
{ {
ss << "schematic = {" << std::endl; ss << "schematic = {" << std::endl;
ss << "\tsize = " ss << indent << "size = "
<< "{x=" << size.X << "{x=" << size.X
<< ", y=" << size.Y << ", y=" << size.Y
<< ", z=" << size.Z << ", z=" << size.Z
@ -325,33 +329,34 @@ bool Schematic::serializeToLua(std::ostream *os,
//// Write y-slice probabilities //// Write y-slice probabilities
{ {
ss << "\tyslice_prob = {" << std::endl; ss << indent << "yslice_prob = {" << std::endl;
for (u16 y = 0; y != size.Y; y++) { for (u16 y = 0; y != size.Y; y++) {
ss << "\t\t{" ss << indent << indent << "{"
<< "ypos=" << y << "ypos=" << y
<< ", prob=" << (u16)slice_probs[y] << ", prob=" << (u16)slice_probs[y]
<< "}," << std::endl; << "}," << std::endl;
} }
ss << "\t}," << std::endl; ss << indent << "}," << std::endl;
} }
//// Write node data //// Write node data
{ {
ss << "\tdata = {" << std::endl; ss << indent << "data = {" << std::endl;
u32 i = 0; u32 i = 0;
for (u16 z = 0; z != size.Z; z++) for (u16 z = 0; z != size.Z; z++)
for (u16 y = 0; y != size.Y; y++) { for (u16 y = 0; y != size.Y; y++) {
if (use_comments) { if (use_comments) {
ss << std::endl ss << std::endl
<< "\t\t-- z=" << z << indent << indent
<< "-- z=" << z
<< ", y=" << y << std::endl; << ", y=" << y << std::endl;
} }
for (u16 x = 0; x != size.X; x++, i++) { for (u16 x = 0; x != size.X; x++, i++) {
ss << "\t\t{" ss << indent << indent << "{"
<< "name=\"" << names[schemdata[i].getContent()] << "name=\"" << names[schemdata[i].getContent()]
<< "\", param1=" << (u16)schemdata[i].param1 << "\", param1=" << (u16)schemdata[i].param1
<< ", param2=" << (u16)schemdata[i].param2 << ", param2=" << (u16)schemdata[i].param2
@ -359,7 +364,7 @@ bool Schematic::serializeToLua(std::ostream *os,
} }
} }
ss << "\t}," << std::endl; ss << indent << "}," << std::endl;
} }
ss << "}" << std::endl; ss << "}" << std::endl;

View File

@ -110,7 +110,7 @@ public:
bool deserializeFromMts(std::istream *is, std::vector<std::string> *names); bool deserializeFromMts(std::istream *is, std::vector<std::string> *names);
bool serializeToMts(std::ostream *os, const std::vector<std::string> &names); bool serializeToMts(std::ostream *os, const std::vector<std::string> &names);
bool serializeToLua(std::ostream *os, const std::vector<std::string> &names, bool serializeToLua(std::ostream *os, const std::vector<std::string> &names,
bool use_comments); bool use_comments, u32 indent_spaces);
void placeStructure(Map *map, v3s16 p, u32 flags, void placeStructure(Map *map, v3s16 p, u32 flags,
Rotation rot, bool force_placement); Rotation rot, bool force_placement);

View File

@ -1134,7 +1134,8 @@ int ModApiMapgen::l_serialize_schematic(lua_State *L)
SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr; SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr;
//// Read options //// Read options
bool use_comments = getboolfield_default(L, 3, "use_lua_comments", false); bool use_comments = getboolfield_default(L, 3, "lua_use_comments", false);
u32 indent_spaces = getintfield_default(L, 3, "lua_num_indent_spaces", 0);
//// Get schematic //// Get schematic
bool was_loaded = false; bool was_loaded = false;
@ -1161,7 +1162,8 @@ int ModApiMapgen::l_serialize_schematic(lua_State *L)
schem->serializeToMts(&os, schem->m_nodenames); schem->serializeToMts(&os, schem->m_nodenames);
break; break;
case SCHEM_FMT_LUA: case SCHEM_FMT_LUA:
schem->serializeToLua(&os, schem->m_nodenames, use_comments); schem->serializeToLua(&os, schem->m_nodenames,
use_comments, indent_spaces);
break; break;
default: default:
return 0; return 0;