Skip to content

Commit

Permalink
Schematics: Reorganize (de)serialization and add Lua serialization API
Browse files Browse the repository at this point in the history
  • Loading branch information
kwolekr committed Apr 13, 2015
1 parent 39fd4da commit b2a89c0
Show file tree
Hide file tree
Showing 5 changed files with 254 additions and 89 deletions.
9 changes: 9 additions & 0 deletions doc/lua_api.txt
Expand Up @@ -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
Expand Down
218 changes: 137 additions & 81 deletions src/mg_schematic.cpp
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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;

//// Write header
{
ss << "schematic = {" << std::endl;
ss << "\tsize = "
<< "{x=" << size.X
<< ", y=" << size.Y
<< ", z=" << size.Z
<< "}," << std::endl;
}

for (u32 i = 0; i != nodecount; i++) {
content_t id;
content_t c = nodes[i].getContent();
//// Write y-slice probabilities
{
ss << "\tyslice_prob = {" << std::endl;

std::map<content_t, content_t>::const_iterator it = nodeidmap.find(c);
if (it == nodeidmap.end()) {
id = numids;
numids++;
for (u16 y = 0; y != size.Y; y++) {
ss << "\t\t{"
<< "ypos=" << y
<< ", prob=" << (u16)slice_probs[y]
<< "}," << std::endl;
}

usednodes->push_back(c);
nodeidmap.insert(std::make_pair(c, id));
} else {
id = it->second;
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;
}
}
nodes[i].setContent(id);

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());
}


Expand Down Expand Up @@ -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);
}
}
48 changes: 46 additions & 2 deletions src/mg_schematic.h
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -68,14 +103,23 @@ class Schematic : public ObjDef, public NodeResolver {

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 {
Expand Down

0 comments on commit b2a89c0

Please sign in to comment.