Schematics: Reorganize (de)serialization and add Lua serialization API
parent
39fd4da7a0
commit
b2a89c04b2
|
@ -2170,6 +2170,15 @@ These functions return the leftover itemstack.
|
|||
* `force_placement` is a boolean indicating whether nodes other than `air` and
|
||||
`ignore` are replaced by the schematic
|
||||
|
||||
* `minetest.serialize_schematic(schematic, format, use_comments)`
|
||||
* Return the serialized schematic specified by schematic (see: Schematic specifier)
|
||||
* in the `format` of either "mts" or "lua".
|
||||
* "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
|
||||
* If `use_comments` is true, the Lua code generated will have (X, Z) position comments
|
||||
* for every X row generated in the schematic data for easier reading. This parameter
|
||||
* is ignored if `format` is not "lua".
|
||||
|
||||
### Misc.
|
||||
* `minetest.get_connected_players()`: returns list of `ObjectRefs`
|
||||
* `minetest.hash_node_position({x=,y=,z=})`: returns an 48-bit integer
|
||||
|
|
|
@ -198,71 +198,59 @@ void Schematic::placeStructure(Map *map, v3s16 p, u32 flags, Rotation rot,
|
|||
}
|
||||
|
||||
|
||||
bool Schematic::loadSchematicFromFile(const char *filename, INodeDefManager *ndef,
|
||||
StringMap *replace_names)
|
||||
bool Schematic::deserializeFromMts(std::istream *is,
|
||||
INodeDefManager *ndef, std::vector<std::string> *names)
|
||||
{
|
||||
std::istream &ss = *is;
|
||||
content_t cignore = CONTENT_IGNORE;
|
||||
bool have_cignore = false;
|
||||
|
||||
std::ifstream is(filename, std::ios_base::binary);
|
||||
if (!is.good()) {
|
||||
errorstream << "loadSchematicFile: unable to open file '"
|
||||
<< filename << "'" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
u32 signature = readU32(is);
|
||||
u32 signature = readU32(ss);
|
||||
if (signature != MTSCHEM_FILE_SIGNATURE) {
|
||||
errorstream << "loadSchematicFile: invalid schematic "
|
||||
errorstream << "Schematic::deserializeFromMts: invalid schematic "
|
||||
"file" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
u16 version = readU16(is);
|
||||
u16 version = readU16(ss);
|
||||
if (version > MTSCHEM_FILE_VER_HIGHEST_READ) {
|
||||
errorstream << "loadSchematicFile: unsupported schematic "
|
||||
errorstream << "Schematic::deserializeFromMts: unsupported schematic "
|
||||
"file version" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
size = readV3S16(is);
|
||||
size = readV3S16(ss);
|
||||
|
||||
delete []slice_probs;
|
||||
slice_probs = new u8[size.Y];
|
||||
for (int y = 0; y != size.Y; y++)
|
||||
slice_probs[y] = (version >= 3) ? readU8(is) : MTSCHEM_PROB_ALWAYS;
|
||||
slice_probs[y] = (version >= 3) ? readU8(ss) : MTSCHEM_PROB_ALWAYS;
|
||||
|
||||
NodeResolveInfo *nri = new NodeResolveInfo(this);
|
||||
|
||||
u16 nidmapcount = readU16(is);
|
||||
u16 nidmapcount = readU16(ss);
|
||||
for (int i = 0; i != nidmapcount; i++) {
|
||||
std::string name = deSerializeString(is);
|
||||
std::string name = deSerializeString(ss);
|
||||
|
||||
// Instances of "ignore" from ver 1 are converted to air (and instances
|
||||
// are fixed to have MTSCHEM_PROB_NEVER later on).
|
||||
if (name == "ignore") {
|
||||
name = "air";
|
||||
cignore = i;
|
||||
have_cignore = true;
|
||||
}
|
||||
|
||||
std::map<std::string, std::string>::iterator it;
|
||||
it = replace_names->find(name);
|
||||
if (it != replace_names->end())
|
||||
name = it->second;
|
||||
|
||||
nri->nodenames.push_back(name);
|
||||
names->push_back(name);
|
||||
}
|
||||
|
||||
nri->nodelistinfo.push_back(NodeListInfo(nidmapcount, CONTENT_AIR));
|
||||
ndef->pendNodeResolve(nri);
|
||||
|
||||
size_t nodecount = size.X * size.Y * size.Z;
|
||||
|
||||
delete []schemdata;
|
||||
schemdata = new MapNode[nodecount];
|
||||
|
||||
MapNode::deSerializeBulk(is, SER_FMT_VER_HIGHEST_READ, schemdata,
|
||||
MapNode::deSerializeBulk(ss, SER_FMT_VER_HIGHEST_READ, schemdata,
|
||||
nodecount, 2, 2, true);
|
||||
|
||||
if (version == 1) { // fix up the probability values
|
||||
// fix any probability values for nodes that were ignore
|
||||
if (version == 1) {
|
||||
for (size_t i = 0; i != nodecount; i++) {
|
||||
if (schemdata[i].param1 == 0)
|
||||
schemdata[i].param1 = MTSCHEM_PROB_ALWAYS;
|
||||
|
@ -275,39 +263,9 @@ bool Schematic::loadSchematicFromFile(const char *filename, INodeDefManager *nde
|
|||
}
|
||||
|
||||
|
||||
/*
|
||||
Minetest Schematic File Format
|
||||
|
||||
All values are stored in big-endian byte order.
|
||||
[u32] signature: 'MTSM'
|
||||
[u16] version: 3
|
||||
[u16] size X
|
||||
[u16] size Y
|
||||
[u16] size Z
|
||||
For each Y:
|
||||
[u8] slice probability value
|
||||
[Name-ID table] Name ID Mapping Table
|
||||
[u16] name-id count
|
||||
For each name-id mapping:
|
||||
[u16] name length
|
||||
[u8[]] name
|
||||
ZLib deflated {
|
||||
For each node in schematic: (for z, y, x)
|
||||
[u16] content
|
||||
For each node in schematic:
|
||||
[u8] probability of occurance (param1)
|
||||
For each node in schematic:
|
||||
[u8] param2
|
||||
}
|
||||
|
||||
Version changes:
|
||||
1 - Initial version
|
||||
2 - Fixed messy never/always place; 0 probability is now never, 0xFF is always
|
||||
3 - Added y-slice probabilities; this allows for variable height structures
|
||||
*/
|
||||
void Schematic::saveSchematicToFile(const char *filename, INodeDefManager *ndef)
|
||||
bool Schematic::serializeToMts(std::ostream *os, INodeDefManager *ndef)
|
||||
{
|
||||
std::ostringstream ss(std::ios_base::binary);
|
||||
std::ostream &ss = *os;
|
||||
|
||||
writeU32(ss, MTSCHEM_FILE_SIGNATURE); // signature
|
||||
writeU16(ss, MTSCHEM_FILE_VER_HIGHEST_WRITE); // version
|
||||
|
@ -326,35 +284,108 @@ void Schematic::saveSchematicToFile(const char *filename, INodeDefManager *ndef)
|
|||
ss << serializeString(ndef->get(usednodes[i]).name); // node names
|
||||
|
||||
// compressed bulk node data
|
||||
MapNode::serializeBulk(ss, SER_FMT_VER_HIGHEST_WRITE, schemdata,
|
||||
nodecount, 2, 2, true);
|
||||
MapNode::serializeBulk(ss, SER_FMT_VER_HIGHEST_WRITE,
|
||||
schemdata, nodecount, 2, 2, true);
|
||||
|
||||
fs::safeWriteToFile(filename, ss.str());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void build_nnlist_and_update_ids(MapNode *nodes, u32 nodecount,
|
||||
std::vector<content_t> *usednodes)
|
||||
bool Schematic::serializeToLua(std::ostream *os,
|
||||
INodeDefManager *ndef, bool use_comments)
|
||||
{
|
||||
std::map<content_t, content_t> nodeidmap;
|
||||
content_t numids = 0;
|
||||
std::ostream &ss = *os;
|
||||
|
||||
for (u32 i = 0; i != nodecount; i++) {
|
||||
content_t id;
|
||||
content_t c = nodes[i].getContent();
|
||||
|
||||
std::map<content_t, content_t>::const_iterator it = nodeidmap.find(c);
|
||||
if (it == nodeidmap.end()) {
|
||||
id = numids;
|
||||
numids++;
|
||||
|
||||
usednodes->push_back(c);
|
||||
nodeidmap.insert(std::make_pair(c, id));
|
||||
} else {
|
||||
id = it->second;
|
||||
//// Write header
|
||||
{
|
||||
ss << "schematic = {" << std::endl;
|
||||
ss << "\tsize = "
|
||||
<< "{x=" << size.X
|
||||
<< ", y=" << size.Y
|
||||
<< ", z=" << size.Z
|
||||
<< "}," << std::endl;
|
||||
}
|
||||
nodes[i].setContent(id);
|
||||
|
||||
//// Write y-slice probabilities
|
||||
{
|
||||
ss << "\tyslice_prob = {" << std::endl;
|
||||
|
||||
for (u16 y = 0; y != size.Y; y++) {
|
||||
ss << "\t\t{"
|
||||
<< "ypos=" << y
|
||||
<< ", prob=" << (u16)slice_probs[y]
|
||||
<< "}," << std::endl;
|
||||
}
|
||||
|
||||
ss << "\t}," << std::endl;
|
||||
}
|
||||
|
||||
//// Write node data
|
||||
{
|
||||
ss << "\tdata = {" << std::endl;
|
||||
|
||||
u32 i = 0;
|
||||
for (u16 z = 0; z != size.Z; z++)
|
||||
for (u16 y = 0; y != size.Y; y++) {
|
||||
if (use_comments) {
|
||||
ss << std::endl
|
||||
<< "\t\t-- z=" << z
|
||||
<< ", y=" << y << std::endl;
|
||||
}
|
||||
|
||||
for (u16 x = 0; x != size.X; x++, i++) {
|
||||
ss << "\t\t{"
|
||||
<< "name=\"" << ndef->get(schemdata[i]).name
|
||||
<< "\", param1=" << (u16)schemdata[i].param1
|
||||
<< ", param2=" << (u16)schemdata[i].param2
|
||||
<< "}," << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
ss << "\t}," << std::endl;
|
||||
}
|
||||
|
||||
ss << "}" << std::endl;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool Schematic::loadSchematicFromFile(const char *filename,
|
||||
INodeDefManager *ndef, StringMap *replace_names)
|
||||
{
|
||||
std::ifstream is(filename, std::ios_base::binary);
|
||||
if (!is.good()) {
|
||||
errorstream << "Schematic::loadSchematicFile: unable to open file '"
|
||||
<< filename << "'" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<std::string> names;
|
||||
if (!deserializeFromMts(&is, ndef, &names))
|
||||
return false;
|
||||
|
||||
NodeResolveInfo *nri = new NodeResolveInfo(this);
|
||||
for (size_t i = 0; i != names.size(); i++) {
|
||||
if (replace_names) {
|
||||
StringMap::iterator it = replace_names->find(names[i]);
|
||||
if (it != replace_names->end())
|
||||
names[i] = it->second;
|
||||
}
|
||||
nri->nodenames.push_back(names[i]);
|
||||
}
|
||||
nri->nodelistinfo.push_back(NodeListInfo(names.size(), CONTENT_AIR));
|
||||
ndef->pendNodeResolve(nri);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool Schematic::saveSchematicToFile(const char *filename, INodeDefManager *ndef)
|
||||
{
|
||||
std::ostringstream os(std::ios_base::binary);
|
||||
serializeToMts(&os, ndef);
|
||||
return fs::safeWriteToFile(filename, os.str());
|
||||
}
|
||||
|
||||
|
||||
|
@ -411,3 +442,28 @@ void Schematic::applyProbabilities(v3s16 p0,
|
|||
slice_probs[y] = (*splist)[i].second;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void build_nnlist_and_update_ids(MapNode *nodes, u32 nodecount,
|
||||
std::vector<content_t> *usednodes)
|
||||
{
|
||||
std::map<content_t, content_t> nodeidmap;
|
||||
content_t numids = 0;
|
||||
|
||||
for (u32 i = 0; i != nodecount; i++) {
|
||||
content_t id;
|
||||
content_t c = nodes[i].getContent();
|
||||
|
||||
std::map<content_t, content_t>::const_iterator it = nodeidmap.find(c);
|
||||
if (it == nodeidmap.end()) {
|
||||
id = numids;
|
||||
numids++;
|
||||
|
||||
usednodes->push_back(c);
|
||||
nodeidmap.insert(std::make_pair(c, id));
|
||||
} else {
|
||||
id = it->second;
|
||||
}
|
||||
nodes[i].setContent(id);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,10 +30,40 @@ class MMVManip;
|
|||
class PseudoRandom;
|
||||
class NodeResolver;
|
||||
|
||||
/*
|
||||
Minetest Schematic File Format
|
||||
|
||||
All values are stored in big-endian byte order.
|
||||
[u32] signature: 'MTSM'
|
||||
[u16] version: 3
|
||||
[u16] size X
|
||||
[u16] size Y
|
||||
[u16] size Z
|
||||
For each Y:
|
||||
[u8] slice probability value
|
||||
[Name-ID table] Name ID Mapping Table
|
||||
[u16] name-id count
|
||||
For each name-id mapping:
|
||||
[u16] name length
|
||||
[u8[]] name
|
||||
ZLib deflated {
|
||||
For each node in schematic: (for z, y, x)
|
||||
[u16] content
|
||||
For each node in schematic:
|
||||
[u8] probability of occurance (param1)
|
||||
For each node in schematic:
|
||||
[u8] param2
|
||||
}
|
||||
|
||||
Version changes:
|
||||
1 - Initial version
|
||||
2 - Fixed messy never/always place; 0 probability is now never, 0xFF is always
|
||||
3 - Added y-slice probabilities; this allows for variable height structures
|
||||
*/
|
||||
|
||||
/////////////////// Schematic flags
|
||||
#define SCHEM_CIDS_UPDATED 0x08
|
||||
|
||||
|
||||
#define MTSCHEM_FILE_SIGNATURE 0x4d54534d // 'MTSM'
|
||||
#define MTSCHEM_FILE_VER_HIGHEST_READ 3
|
||||
#define MTSCHEM_FILE_VER_HIGHEST_WRITE 3
|
||||
|
@ -46,6 +76,11 @@ enum SchematicType
|
|||
SCHEMATIC_NORMAL,
|
||||
};
|
||||
|
||||
enum SchematicFormatType {
|
||||
SCHEM_FMT_HANDLE,
|
||||
SCHEM_FMT_MTS,
|
||||
SCHEM_FMT_LUA,
|
||||
};
|
||||
|
||||
class Schematic : public ObjDef, public NodeResolver {
|
||||
public:
|
||||
|
@ -68,14 +103,23 @@ public:
|
|||
|
||||
bool loadSchematicFromFile(const char *filename, INodeDefManager *ndef,
|
||||
StringMap *replace_names);
|
||||
void saveSchematicToFile(const char *filename, INodeDefManager *ndef);
|
||||
bool saveSchematicToFile(const char *filename, INodeDefManager *ndef);
|
||||
bool getSchematicFromMap(Map *map, v3s16 p1, v3s16 p2);
|
||||
|
||||
bool deserializeFromMts(std::istream *is, INodeDefManager *ndef,
|
||||
std::vector<std::string> *names);
|
||||
bool serializeToMts(std::ostream *os, INodeDefManager *ndef);
|
||||
bool serializeToLua(std::ostream *os,
|
||||
INodeDefManager *ndef, bool use_comments);
|
||||
|
||||
|
||||
void placeStructure(Map *map, v3s16 p, u32 flags,
|
||||
Rotation rot, bool force_placement, INodeDefManager *nef);
|
||||
void applyProbabilities(v3s16 p0,
|
||||
std::vector<std::pair<v3s16, u8> > *plist,
|
||||
std::vector<std::pair<s16, u8> > *splist);
|
||||
|
||||
std::string getAsLuaTable(INodeDefManager *ndef, bool use_comments);
|
||||
};
|
||||
|
||||
class SchematicManager : public ObjDefManager {
|
||||
|
|
|
@ -36,7 +36,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
#include "settings.h"
|
||||
#include "log.h"
|
||||
|
||||
|
||||
struct EnumString ModApiMapgen::es_BiomeTerrainType[] =
|
||||
{
|
||||
{BIOME_NORMAL, "normal"},
|
||||
|
@ -85,6 +84,13 @@ struct EnumString ModApiMapgen::es_Rotation[] =
|
|||
{0, NULL},
|
||||
};
|
||||
|
||||
struct EnumString ModApiMapgen::es_SchematicFormatType[] =
|
||||
{
|
||||
{SCHEM_FMT_HANDLE, "handle"},
|
||||
{SCHEM_FMT_MTS, "mts"},
|
||||
{SCHEM_FMT_LUA, "lua"},
|
||||
{0, NULL},
|
||||
};
|
||||
|
||||
ObjDef *get_objdef(lua_State *L, int index, ObjDefManager *objmgr);
|
||||
|
||||
|
@ -329,9 +335,11 @@ Schematic *read_schematic_def(lua_State *L, int index,
|
|||
param2 = !lua_isnil(L, -1) ? lua_tonumber(L, -1) : 0;
|
||||
lua_pop(L, 1);
|
||||
|
||||
if (replace_names) {
|
||||
StringMap::iterator it = replace_names->find(name);
|
||||
if (it != replace_names->end())
|
||||
name = it->second;
|
||||
}
|
||||
|
||||
schemdata[i] = MapNode(ndef, name, param1, param2);
|
||||
|
||||
|
@ -1067,8 +1075,9 @@ int ModApiMapgen::l_place_schematic(lua_State *L)
|
|||
|
||||
//// Read rotation
|
||||
int rot = ROTATE_0;
|
||||
if (lua_isstring(L, 3))
|
||||
string_to_enum(es_Rotation, rot, std::string(lua_tostring(L, 3)));
|
||||
const char *enumstr = lua_tostring(L, 3);
|
||||
if (enumstr)
|
||||
string_to_enum(es_Rotation, rot, std::string(enumstr));
|
||||
|
||||
//// Read force placement
|
||||
bool force_placement = true;
|
||||
|
@ -1094,6 +1103,48 @@ int ModApiMapgen::l_place_schematic(lua_State *L)
|
|||
return 1;
|
||||
}
|
||||
|
||||
// serialize_schematic(schematic, format, use_comments)
|
||||
int ModApiMapgen::l_serialize_schematic(lua_State *L)
|
||||
{
|
||||
SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr;
|
||||
INodeDefManager *ndef = getServer(L)->getNodeDefManager();
|
||||
|
||||
//// Read schematic
|
||||
Schematic *schem = get_or_load_schematic(L, 1, schemmgr, NULL);
|
||||
if (!schem) {
|
||||
errorstream << "serialize_schematic: failed to get schematic" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
//// Read format of definition to save as
|
||||
int schem_format = SCHEM_FMT_MTS;
|
||||
const char *enumstr = lua_tostring(L, 2);
|
||||
if (enumstr)
|
||||
string_to_enum(es_SchematicFormatType, schem_format, std::string(enumstr));
|
||||
|
||||
//// Read use_comments
|
||||
bool use_comments = false;
|
||||
if (lua_isboolean(L, 3))
|
||||
use_comments = lua_toboolean(L, 3);
|
||||
|
||||
//// Serialize to binary string
|
||||
std::ostringstream os(std::ios_base::binary);
|
||||
switch (schem_format) {
|
||||
case SCHEM_FMT_MTS:
|
||||
schem->serializeToMts(&os, ndef);
|
||||
break;
|
||||
case SCHEM_FMT_LUA:
|
||||
schem->serializeToLua(&os, ndef, use_comments);
|
||||
break;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::string ser = os.str();
|
||||
lua_pushlstring(L, ser.c_str(), ser.length());
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
void ModApiMapgen::Initialize(lua_State *L, int top)
|
||||
{
|
||||
|
@ -1118,4 +1169,5 @@ void ModApiMapgen::Initialize(lua_State *L, int top)
|
|||
API_FCT(generate_decorations);
|
||||
API_FCT(create_schematic);
|
||||
API_FCT(place_schematic);
|
||||
API_FCT(serialize_schematic);
|
||||
}
|
||||
|
|
|
@ -84,6 +84,9 @@ private:
|
|||
// place_schematic(p, schematic, rotation, replacement)
|
||||
static int l_place_schematic(lua_State *L);
|
||||
|
||||
// serialize_schematic(schematic, format, use_comments)
|
||||
static int l_serialize_schematic(lua_State *L);
|
||||
|
||||
public:
|
||||
static void Initialize(lua_State *L, int top);
|
||||
|
||||
|
@ -92,6 +95,7 @@ public:
|
|||
static struct EnumString es_MapgenObject[];
|
||||
static struct EnumString es_OreType[];
|
||||
static struct EnumString es_Rotation[];
|
||||
static struct EnumString es_SchematicFormatType[];
|
||||
};
|
||||
|
||||
#endif /* L_MAPGEN_H_ */
|
||||
|
|
Loading…
Reference in New Issue