3 Copyright (C) 2014-2018 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4 Copyright (C) 2015-2018 paramat
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "mg_schematic.h"
30 #include "util/numeric.h"
31 #include "util/serialize.h"
32 #include "serialization.h"
34 #include "voxelalgorithms.h"
36 ///////////////////////////////////////////////////////////////////////////////
39 SchematicManager::SchematicManager(Server *server) :
40 ObjDefManager(server, OBJDEF_SCHEMATIC),
46 SchematicManager *SchematicManager::clone() const
48 auto mgr = new SchematicManager();
50 ObjDefManager::cloneTo(mgr);
55 void SchematicManager::clear()
57 EmergeManager *emerge = m_server->getEmergeManager();
59 // Remove all dangling references in Decorations
60 DecorationManager *decomgr = emerge->getWritableDecorationManager();
61 for (size_t i = 0; i != decomgr->getNumObjects(); i++) {
62 Decoration *deco = (Decoration *)decomgr->getRaw(i);
65 DecoSchematic *dschem = dynamic_cast<DecoSchematic *>(deco);
67 dschem->schematic = NULL;
68 } catch (const std::bad_cast &) {
72 ObjDefManager::clear();
76 ///////////////////////////////////////////////////////////////////////////////
79 Schematic::Schematic()
83 Schematic::~Schematic()
89 ObjDef *Schematic::clone() const
91 auto def = new Schematic();
93 NodeResolver::cloneTo(def);
95 def->c_nodes = c_nodes;
98 FATAL_ERROR_IF(!schemdata, "Schematic can only be cloned after loading");
99 u32 nodecount = size.X * size.Y * size.Z;
100 def->schemdata = new MapNode[nodecount];
101 memcpy(def->schemdata, schemdata, sizeof(MapNode) * nodecount);
102 def->slice_probs = new u8[size.Y];
103 memcpy(def->slice_probs, slice_probs, sizeof(u8) * size.Y);
109 void Schematic::resolveNodeNames()
111 getIdsFromNrBacklog(&c_nodes, true, CONTENT_AIR);
113 size_t bufsize = size.X * size.Y * size.Z;
114 for (size_t i = 0; i != bufsize; i++) {
115 content_t c_original = schemdata[i].getContent();
116 content_t c_new = c_nodes[c_original];
117 schemdata[i].setContent(c_new);
122 void Schematic::blitToVManip(MMVManip *vm, v3s16 p, Rotation rot, bool force_place)
124 assert(schemdata && slice_probs);
125 sanity_check(m_ndef != NULL);
128 int ystride = size.X;
129 int zstride = size.X * size.Y;
135 int i_start, i_step_x, i_step_z;
144 i_start = zstride * (sz - 1) + sx - 1;
149 i_start = zstride * (sz - 1);
161 for (s16 y = 0; y != sy; y++) {
162 if ((slice_probs[y] != MTSCHEM_PROB_ALWAYS) &&
163 (slice_probs[y] <= myrand_range(1, MTSCHEM_PROB_ALWAYS)))
166 for (s16 z = 0; z != sz; z++) {
167 u32 i = z * i_step_z + y * ystride + i_start;
168 for (s16 x = 0; x != sx; x++, i += i_step_x) {
169 v3s16 pos(p.X + x, y_map, p.Z + z);
170 if (!vm->m_area.contains(pos))
173 if (schemdata[i].getContent() == CONTENT_IGNORE)
176 u8 placement_prob = schemdata[i].param1 & MTSCHEM_PROB_MASK;
177 bool force_place_node = schemdata[i].param1 & MTSCHEM_FORCE_PLACE;
179 if (placement_prob == MTSCHEM_PROB_NEVER)
182 u32 vi = vm->m_area.index(pos);
183 if (!force_place && !force_place_node) {
184 content_t c = vm->m_data[vi].getContent();
185 if (c != CONTENT_AIR && c != CONTENT_IGNORE)
189 if ((placement_prob != MTSCHEM_PROB_ALWAYS) &&
190 (placement_prob <= myrand_range(1, MTSCHEM_PROB_ALWAYS)))
193 vm->m_data[vi] = schemdata[i];
194 vm->m_data[vi].param1 = 0;
197 vm->m_data[vi].rotateAlongYAxis(m_ndef, rot);
205 bool Schematic::placeOnVManip(MMVManip *vm, v3s16 p, u32 flags,
206 Rotation rot, bool force_place)
209 assert(schemdata && slice_probs);
210 sanity_check(m_ndef != NULL);
212 //// Determine effective rotation and effective schematic dimensions
213 if (rot == ROTATE_RAND)
214 rot = (Rotation)myrand_range(ROTATE_0, ROTATE_270);
216 v3s16 s = (rot == ROTATE_90 || rot == ROTATE_270) ?
217 v3s16(size.Z, size.Y, size.X) : size;
219 //// Adjust placement position if necessary
220 if (flags & DECO_PLACE_CENTER_X)
221 p.X -= (s.X - 1) / 2;
222 if (flags & DECO_PLACE_CENTER_Y)
223 p.Y -= (s.Y - 1) / 2;
224 if (flags & DECO_PLACE_CENTER_Z)
225 p.Z -= (s.Z - 1) / 2;
227 blitToVManip(vm, p, rot, force_place);
229 return vm->m_area.contains(VoxelArea(p, p + s - v3s16(1, 1, 1)));
232 void Schematic::placeOnMap(ServerMap *map, v3s16 p, u32 flags,
233 Rotation rot, bool force_place)
235 std::map<v3s16, MapBlock *> lighting_modified_blocks;
236 std::map<v3s16, MapBlock *> modified_blocks;
237 std::map<v3s16, MapBlock *>::iterator it;
240 assert(schemdata != NULL);
241 sanity_check(m_ndef != NULL);
243 //// Determine effective rotation and effective schematic dimensions
244 if (rot == ROTATE_RAND)
245 rot = (Rotation)myrand_range(ROTATE_0, ROTATE_270);
247 v3s16 s = (rot == ROTATE_90 || rot == ROTATE_270) ?
248 v3s16(size.Z, size.Y, size.X) : size;
250 //// Adjust placement position if necessary
251 if (flags & DECO_PLACE_CENTER_X)
252 p.X -= (s.X - 1) / 2;
253 if (flags & DECO_PLACE_CENTER_Y)
254 p.Y -= (s.Y - 1) / 2;
255 if (flags & DECO_PLACE_CENTER_Z)
256 p.Z -= (s.Z - 1) / 2;
258 //// Create VManip for effected area, emerge our area, modify area
259 //// inside VManip, then blit back.
260 v3s16 bp1 = getNodeBlockPos(p);
261 v3s16 bp2 = getNodeBlockPos(p + s - v3s16(1, 1, 1));
264 vm.initialEmerge(bp1, bp2);
266 blitToVManip(&vm, p, rot, force_place);
268 voxalgo::blit_back_with_light(map, &vm, &modified_blocks);
270 //// Carry out post-map-modification actions
272 //// Create & dispatch map modification events to observers
274 event.type = MEET_OTHER;
275 for (it = modified_blocks.begin(); it != modified_blocks.end(); ++it)
276 event.modified_blocks.insert(it->first);
278 map->dispatchEvent(event);
282 bool Schematic::deserializeFromMts(std::istream *is,
283 std::vector<std::string> *names)
285 std::istream &ss = *is;
286 content_t cignore = CONTENT_IGNORE;
287 bool have_cignore = false;
290 u32 signature = readU32(ss);
291 if (signature != MTSCHEM_FILE_SIGNATURE) {
292 errorstream << __FUNCTION__ << ": invalid schematic "
298 u16 version = readU16(ss);
299 if (version > MTSCHEM_FILE_VER_HIGHEST_READ) {
300 errorstream << __FUNCTION__ << ": unsupported schematic "
301 "file version" << std::endl;
306 size = readV3S16(ss);
308 //// Read Y-slice probability values
309 delete []slice_probs;
310 slice_probs = new u8[size.Y];
311 for (int y = 0; y != size.Y; y++)
312 slice_probs[y] = (version >= 3) ? readU8(ss) : MTSCHEM_PROB_ALWAYS_OLD;
315 u16 nidmapcount = readU16(ss);
316 for (int i = 0; i != nidmapcount; i++) {
317 std::string name = deSerializeString(ss);
319 // Instances of "ignore" from v1 are converted to air (and instances
320 // are fixed to have MTSCHEM_PROB_NEVER later on).
321 if (name == "ignore") {
327 names->push_back(name);
331 size_t nodecount = size.X * size.Y * size.Z;
334 schemdata = new MapNode[nodecount];
336 MapNode::deSerializeBulk(ss, SER_FMT_VER_HIGHEST_READ, schemdata,
337 nodecount, 2, 2, true);
339 // Fix probability values for nodes that were ignore; removed in v2
341 for (size_t i = 0; i != nodecount; i++) {
342 if (schemdata[i].param1 == 0)
343 schemdata[i].param1 = MTSCHEM_PROB_ALWAYS_OLD;
344 if (have_cignore && schemdata[i].getContent() == cignore)
345 schemdata[i].param1 = MTSCHEM_PROB_NEVER;
349 // Fix probability values for probability range truncation introduced in v4
351 for (s16 y = 0; y != size.Y; y++)
352 slice_probs[y] >>= 1;
353 for (size_t i = 0; i != nodecount; i++)
354 schemdata[i].param1 >>= 1;
361 bool Schematic::serializeToMts(std::ostream *os,
362 const std::vector<std::string> &names)
364 std::ostream &ss = *os;
366 writeU32(ss, MTSCHEM_FILE_SIGNATURE); // signature
367 writeU16(ss, MTSCHEM_FILE_VER_HIGHEST_WRITE); // version
368 writeV3S16(ss, size); // schematic size
370 for (int y = 0; y != size.Y; y++) // Y slice probabilities
371 writeU8(ss, slice_probs[y]);
373 writeU16(ss, names.size()); // name count
374 for (size_t i = 0; i != names.size(); i++)
375 ss << serializeString(names[i]); // node names
377 // compressed bulk node data
378 MapNode::serializeBulk(ss, SER_FMT_VER_HIGHEST_WRITE,
379 schemdata, size.X * size.Y * size.Z, 2, 2, true);
385 bool Schematic::serializeToLua(std::ostream *os,
386 const std::vector<std::string> &names, bool use_comments, u32 indent_spaces)
388 std::ostream &ss = *os;
390 std::string indent("\t");
391 if (indent_spaces > 0)
392 indent.assign(indent_spaces, ' ');
396 ss << "schematic = {" << std::endl;
397 ss << indent << "size = "
401 << "}," << std::endl;
404 //// Write y-slice probabilities
406 ss << indent << "yslice_prob = {" << std::endl;
408 for (u16 y = 0; y != size.Y; y++) {
409 u8 probability = slice_probs[y] & MTSCHEM_PROB_MASK;
411 ss << indent << indent << "{"
413 << ", prob=" << (u16)probability * 2
414 << "}," << std::endl;
417 ss << indent << "}," << std::endl;
422 ss << indent << "data = {" << std::endl;
425 for (u16 z = 0; z != size.Z; z++)
426 for (u16 y = 0; y != size.Y; y++) {
431 << ", y=" << y << std::endl;
434 for (u16 x = 0; x != size.X; x++, i++) {
435 u8 probability = schemdata[i].param1 & MTSCHEM_PROB_MASK;
436 bool force_place = schemdata[i].param1 & MTSCHEM_FORCE_PLACE;
438 ss << indent << indent << "{"
439 << "name=\"" << names[schemdata[i].getContent()]
440 << "\", prob=" << (u16)probability * 2
441 << ", param2=" << (u16)schemdata[i].param2;
444 ss << ", force_place=true";
446 ss << "}," << std::endl;
450 ss << indent << "}," << std::endl;
453 ss << "}" << std::endl;
459 bool Schematic::loadSchematicFromFile(const std::string &filename,
460 const NodeDefManager *ndef, StringMap *replace_names)
462 std::ifstream is(filename.c_str(), std::ios_base::binary);
464 errorstream << __FUNCTION__ << ": unable to open file '"
465 << filename << "'" << std::endl;
469 size_t origsize = m_nodenames.size();
470 if (!deserializeFromMts(&is, &m_nodenames))
473 m_nnlistsizes.push_back(m_nodenames.size() - origsize);
478 for (size_t i = origsize; i < m_nodenames.size(); i++) {
479 std::string &node_name = m_nodenames[i];
480 StringMap::iterator it = replace_names->find(node_name);
481 if (it != replace_names->end())
482 node_name = it->second;
487 ndef->pendNodeResolve(this);
493 bool Schematic::saveSchematicToFile(const std::string &filename,
494 const NodeDefManager *ndef)
496 MapNode *orig_schemdata = schemdata;
497 std::vector<std::string> ndef_nodenames;
498 std::vector<std::string> *names;
500 if (m_resolve_done && ndef == NULL)
504 names = &ndef_nodenames;
506 u32 volume = size.X * size.Y * size.Z;
507 schemdata = new MapNode[volume];
508 for (u32 i = 0; i != volume; i++)
509 schemdata[i] = orig_schemdata[i];
511 generate_nodelist_and_update_ids(schemdata, volume, names, ndef);
512 } else { // otherwise, use the names we have on hand in the list
513 names = &m_nodenames;
516 std::ostringstream os(std::ios_base::binary);
517 bool status = serializeToMts(&os, *names);
521 schemdata = orig_schemdata;
527 return fs::safeWriteToFile(filename, os.str());
531 bool Schematic::getSchematicFromMap(Map *map, v3s16 p1, v3s16 p2)
533 MMVManip *vm = new MMVManip(map);
535 v3s16 bp1 = getNodeBlockPos(p1);
536 v3s16 bp2 = getNodeBlockPos(p2);
537 vm->initialEmerge(bp1, bp2);
541 slice_probs = new u8[size.Y];
542 for (s16 y = 0; y != size.Y; y++)
543 slice_probs[y] = MTSCHEM_PROB_ALWAYS;
545 schemdata = new MapNode[size.X * size.Y * size.Z];
548 for (s16 z = p1.Z; z <= p2.Z; z++)
549 for (s16 y = p1.Y; y <= p2.Y; y++) {
550 u32 vi = vm->m_area.index(p1.X, y, z);
551 for (s16 x = p1.X; x <= p2.X; x++, i++, vi++) {
552 schemdata[i] = vm->m_data[vi];
553 schemdata[i].param1 = MTSCHEM_PROB_ALWAYS;
562 void Schematic::applyProbabilities(v3s16 p0,
563 std::vector<std::pair<v3s16, u8> > *plist,
564 std::vector<std::pair<s16, u8> > *splist)
566 for (size_t i = 0; i != plist->size(); i++) {
567 v3s16 p = (*plist)[i].first - p0;
568 int index = p.Z * (size.Y * size.X) + p.Y * size.X + p.X;
569 if (index < size.Z * size.Y * size.X) {
570 u8 prob = (*plist)[i].second;
571 schemdata[index].param1 = prob;
573 // trim unnecessary node names from schematic
574 if (prob == MTSCHEM_PROB_NEVER)
575 schemdata[index].setContent(CONTENT_AIR);
579 for (size_t i = 0; i != splist->size(); i++) {
580 s16 y = (*splist)[i].first - p0.Y;
581 slice_probs[y] = (*splist)[i].second;
586 void generate_nodelist_and_update_ids(MapNode *nodes, size_t nodecount,
587 std::vector<std::string> *usednodes, const NodeDefManager *ndef)
589 std::unordered_map<content_t, content_t> nodeidmap;
590 content_t numids = 0;
592 for (size_t i = 0; i != nodecount; i++) {
594 content_t c = nodes[i].getContent();
596 std::unordered_map<content_t, content_t>::const_iterator it = nodeidmap.find(c);
597 if (it == nodeidmap.end()) {
601 usednodes->push_back(ndef->get(c).name);
602 nodeidmap.insert(std::make_pair(c, id));
606 nodes[i].setContent(id);