3 Copyright (C) 2014-2016 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4 Copyright (C) 2015-2017 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 void SchematicManager::clear()
48 EmergeManager *emerge = m_server->getEmergeManager();
50 // Remove all dangling references in Decorations
51 DecorationManager *decomgr = emerge->decomgr;
52 for (size_t i = 0; i != decomgr->getNumObjects(); i++) {
53 Decoration *deco = (Decoration *)decomgr->getRaw(i);
56 DecoSchematic *dschem = dynamic_cast<DecoSchematic *>(deco);
58 dschem->schematic = NULL;
59 } catch (std::bad_cast) {
63 ObjDefManager::clear();
67 ///////////////////////////////////////////////////////////////////////////////
70 Schematic::Schematic()
75 Schematic::~Schematic()
82 void Schematic::resolveNodeNames()
84 getIdsFromNrBacklog(&c_nodes, true, CONTENT_AIR);
86 size_t bufsize = size.X * size.Y * size.Z;
87 for (size_t i = 0; i != bufsize; i++) {
88 content_t c_original = schemdata[i].getContent();
89 content_t c_new = c_nodes[c_original];
90 schemdata[i].setContent(c_new);
95 void Schematic::blitToVManip(MMVManip *vm, v3s16 p, Rotation rot, bool force_place)
97 sanity_check(m_ndef != NULL);
100 int ystride = size.X;
101 int zstride = size.X * size.Y;
107 int i_start, i_step_x, i_step_z;
116 i_start = zstride * (sz - 1) + sx - 1;
121 i_start = zstride * (sz - 1);
133 for (s16 y = 0; y != sy; y++) {
134 if ((slice_probs[y] != MTSCHEM_PROB_ALWAYS) &&
135 (slice_probs[y] <= myrand_range(1, MTSCHEM_PROB_ALWAYS)))
138 for (s16 z = 0; z != sz; z++) {
139 u32 i = z * i_step_z + y * ystride + i_start;
140 for (s16 x = 0; x != sx; x++, i += i_step_x) {
141 u32 vi = vm->m_area.index(p.X + x, y_map, p.Z + z);
142 if (!vm->m_area.contains(vi))
145 if (schemdata[i].getContent() == CONTENT_IGNORE)
148 u8 placement_prob = schemdata[i].param1 & MTSCHEM_PROB_MASK;
149 bool force_place_node = schemdata[i].param1 & MTSCHEM_FORCE_PLACE;
151 if (placement_prob == MTSCHEM_PROB_NEVER)
154 if (!force_place && !force_place_node) {
155 content_t c = vm->m_data[vi].getContent();
156 if (c != CONTENT_AIR && c != CONTENT_IGNORE)
160 if ((placement_prob != MTSCHEM_PROB_ALWAYS) &&
161 (placement_prob <= myrand_range(1, MTSCHEM_PROB_ALWAYS)))
164 vm->m_data[vi] = schemdata[i];
165 vm->m_data[vi].param1 = 0;
168 vm->m_data[vi].rotateAlongYAxis(m_ndef, rot);
176 bool Schematic::placeOnVManip(MMVManip *vm, v3s16 p, u32 flags,
177 Rotation rot, bool force_place)
180 assert(schemdata != NULL);
181 sanity_check(m_ndef != NULL);
183 //// Determine effective rotation and effective schematic dimensions
184 if (rot == ROTATE_RAND)
185 rot = (Rotation)myrand_range(ROTATE_0, ROTATE_270);
187 v3s16 s = (rot == ROTATE_90 || rot == ROTATE_270) ?
188 v3s16(size.Z, size.Y, size.X) : size;
190 //// Adjust placement position if necessary
191 if (flags & DECO_PLACE_CENTER_X)
192 p.X -= (s.X + 1) / 2;
193 if (flags & DECO_PLACE_CENTER_Y)
194 p.Y -= (s.Y + 1) / 2;
195 if (flags & DECO_PLACE_CENTER_Z)
196 p.Z -= (s.Z + 1) / 2;
198 blitToVManip(vm, p, rot, force_place);
200 return vm->m_area.contains(VoxelArea(p, p + s - v3s16(1,1,1)));
203 void Schematic::placeOnMap(ServerMap *map, v3s16 p, u32 flags,
204 Rotation rot, bool force_place)
206 std::map<v3s16, MapBlock *> lighting_modified_blocks;
207 std::map<v3s16, MapBlock *> modified_blocks;
208 std::map<v3s16, MapBlock *>::iterator it;
211 assert(schemdata != NULL);
212 sanity_check(m_ndef != NULL);
214 //// Determine effective rotation and effective schematic dimensions
215 if (rot == ROTATE_RAND)
216 rot = (Rotation)myrand_range(ROTATE_0, ROTATE_270);
218 v3s16 s = (rot == ROTATE_90 || rot == ROTATE_270) ?
219 v3s16(size.Z, size.Y, size.X) : size;
221 //// Adjust placement position if necessary
222 if (flags & DECO_PLACE_CENTER_X)
223 p.X -= (s.X + 1) / 2;
224 if (flags & DECO_PLACE_CENTER_Y)
225 p.Y -= (s.Y + 1) / 2;
226 if (flags & DECO_PLACE_CENTER_Z)
227 p.Z -= (s.Z + 1) / 2;
229 //// Create VManip for effected area, emerge our area, modify area
230 //// inside VManip, then blit back.
231 v3s16 bp1 = getNodeBlockPos(p);
232 v3s16 bp2 = getNodeBlockPos(p + s - v3s16(1,1,1));
235 vm.initialEmerge(bp1, bp2);
237 blitToVManip(&vm, p, rot, force_place);
239 voxalgo::blit_back_with_light(map, &vm, &modified_blocks);
241 //// Carry out post-map-modification actions
243 //// Create & dispatch map modification events to observers
245 event.type = MEET_OTHER;
246 for (it = modified_blocks.begin(); it != modified_blocks.end(); ++it)
247 event.modified_blocks.insert(it->first);
249 map->dispatchEvent(&event);
253 bool Schematic::deserializeFromMts(std::istream *is,
254 std::vector<std::string> *names)
256 std::istream &ss = *is;
257 content_t cignore = CONTENT_IGNORE;
258 bool have_cignore = false;
261 u32 signature = readU32(ss);
262 if (signature != MTSCHEM_FILE_SIGNATURE) {
263 errorstream << __FUNCTION__ << ": invalid schematic "
269 u16 version = readU16(ss);
270 if (version > MTSCHEM_FILE_VER_HIGHEST_READ) {
271 errorstream << __FUNCTION__ << ": unsupported schematic "
272 "file version" << std::endl;
277 size = readV3S16(ss);
279 //// Read Y-slice probability values
280 delete []slice_probs;
281 slice_probs = new u8[size.Y];
282 for (int y = 0; y != size.Y; y++)
283 slice_probs[y] = (version >= 3) ? readU8(ss) : MTSCHEM_PROB_ALWAYS_OLD;
286 u16 nidmapcount = readU16(ss);
287 for (int i = 0; i != nidmapcount; i++) {
288 std::string name = deSerializeString(ss);
290 // Instances of "ignore" from v1 are converted to air (and instances
291 // are fixed to have MTSCHEM_PROB_NEVER later on).
292 if (name == "ignore") {
298 names->push_back(name);
302 size_t nodecount = size.X * size.Y * size.Z;
305 schemdata = new MapNode[nodecount];
307 MapNode::deSerializeBulk(ss, SER_FMT_VER_HIGHEST_READ, schemdata,
308 nodecount, 2, 2, true);
310 // Fix probability values for nodes that were ignore; removed in v2
312 for (size_t i = 0; i != nodecount; i++) {
313 if (schemdata[i].param1 == 0)
314 schemdata[i].param1 = MTSCHEM_PROB_ALWAYS_OLD;
315 if (have_cignore && schemdata[i].getContent() == cignore)
316 schemdata[i].param1 = MTSCHEM_PROB_NEVER;
320 // Fix probability values for probability range truncation introduced in v4
322 for (s16 y = 0; y != size.Y; y++)
323 slice_probs[y] >>= 1;
324 for (size_t i = 0; i != nodecount; i++)
325 schemdata[i].param1 >>= 1;
332 bool Schematic::serializeToMts(std::ostream *os,
333 const std::vector<std::string> &names)
335 std::ostream &ss = *os;
337 writeU32(ss, MTSCHEM_FILE_SIGNATURE); // signature
338 writeU16(ss, MTSCHEM_FILE_VER_HIGHEST_WRITE); // version
339 writeV3S16(ss, size); // schematic size
341 for (int y = 0; y != size.Y; y++) // Y slice probabilities
342 writeU8(ss, slice_probs[y]);
344 writeU16(ss, names.size()); // name count
345 for (size_t i = 0; i != names.size(); i++)
346 ss << serializeString(names[i]); // node names
348 // compressed bulk node data
349 MapNode::serializeBulk(ss, SER_FMT_VER_HIGHEST_WRITE,
350 schemdata, size.X * size.Y * size.Z, 2, 2, true);
356 bool Schematic::serializeToLua(std::ostream *os,
357 const std::vector<std::string> &names, bool use_comments, u32 indent_spaces)
359 std::ostream &ss = *os;
361 std::string indent("\t");
362 if (indent_spaces > 0)
363 indent.assign(indent_spaces, ' ');
367 ss << "schematic = {" << std::endl;
368 ss << indent << "size = "
372 << "}," << std::endl;
375 //// Write y-slice probabilities
377 ss << indent << "yslice_prob = {" << std::endl;
379 for (u16 y = 0; y != size.Y; y++) {
380 u8 probability = slice_probs[y] & MTSCHEM_PROB_MASK;
382 ss << indent << indent << "{"
384 << ", prob=" << (u16)probability * 2
385 << "}," << std::endl;
388 ss << indent << "}," << std::endl;
393 ss << indent << "data = {" << std::endl;
396 for (u16 z = 0; z != size.Z; z++)
397 for (u16 y = 0; y != size.Y; y++) {
402 << ", y=" << y << std::endl;
405 for (u16 x = 0; x != size.X; x++, i++) {
406 u8 probability = schemdata[i].param1 & MTSCHEM_PROB_MASK;
407 bool force_place = schemdata[i].param1 & MTSCHEM_FORCE_PLACE;
409 ss << indent << indent << "{"
410 << "name=\"" << names[schemdata[i].getContent()]
411 << "\", prob=" << (u16)probability * 2
412 << ", param2=" << (u16)schemdata[i].param2;
415 ss << ", force_place=true";
417 ss << "}," << std::endl;
421 ss << indent << "}," << std::endl;
424 ss << "}" << std::endl;
430 bool Schematic::loadSchematicFromFile(const std::string &filename,
431 INodeDefManager *ndef, StringMap *replace_names)
433 std::ifstream is(filename.c_str(), std::ios_base::binary);
435 errorstream << __FUNCTION__ << ": unable to open file '"
436 << filename << "'" << std::endl;
440 size_t origsize = m_nodenames.size();
441 if (!deserializeFromMts(&is, &m_nodenames))
444 m_nnlistsizes.push_back(m_nodenames.size() - origsize);
449 for (size_t i = origsize; i < m_nodenames.size(); i++) {
450 std::string &node_name = m_nodenames[i];
451 StringMap::iterator it = replace_names->find(node_name);
452 if (it != replace_names->end())
453 node_name = it->second;
458 ndef->pendNodeResolve(this);
464 bool Schematic::saveSchematicToFile(const std::string &filename,
465 INodeDefManager *ndef)
467 MapNode *orig_schemdata = schemdata;
468 std::vector<std::string> ndef_nodenames;
469 std::vector<std::string> *names;
471 if (m_resolve_done && ndef == NULL)
475 names = &ndef_nodenames;
477 u32 volume = size.X * size.Y * size.Z;
478 schemdata = new MapNode[volume];
479 for (u32 i = 0; i != volume; i++)
480 schemdata[i] = orig_schemdata[i];
482 generate_nodelist_and_update_ids(schemdata, volume, names, ndef);
483 } else { // otherwise, use the names we have on hand in the list
484 names = &m_nodenames;
487 std::ostringstream os(std::ios_base::binary);
488 bool status = serializeToMts(&os, *names);
492 schemdata = orig_schemdata;
498 return fs::safeWriteToFile(filename, os.str());
502 bool Schematic::getSchematicFromMap(Map *map, v3s16 p1, v3s16 p2)
504 MMVManip *vm = new MMVManip(map);
506 v3s16 bp1 = getNodeBlockPos(p1);
507 v3s16 bp2 = getNodeBlockPos(p2);
508 vm->initialEmerge(bp1, bp2);
512 slice_probs = new u8[size.Y];
513 for (s16 y = 0; y != size.Y; y++)
514 slice_probs[y] = MTSCHEM_PROB_ALWAYS;
516 schemdata = new MapNode[size.X * size.Y * size.Z];
519 for (s16 z = p1.Z; z <= p2.Z; z++)
520 for (s16 y = p1.Y; y <= p2.Y; y++) {
521 u32 vi = vm->m_area.index(p1.X, y, z);
522 for (s16 x = p1.X; x <= p2.X; x++, i++, vi++) {
523 schemdata[i] = vm->m_data[vi];
524 schemdata[i].param1 = MTSCHEM_PROB_ALWAYS;
533 void Schematic::applyProbabilities(v3s16 p0,
534 std::vector<std::pair<v3s16, u8> > *plist,
535 std::vector<std::pair<s16, u8> > *splist)
537 for (size_t i = 0; i != plist->size(); i++) {
538 v3s16 p = (*plist)[i].first - p0;
539 int index = p.Z * (size.Y * size.X) + p.Y * size.X + p.X;
540 if (index < size.Z * size.Y * size.X) {
541 u8 prob = (*plist)[i].second;
542 schemdata[index].param1 = prob;
544 // trim unnecessary node names from schematic
545 if (prob == MTSCHEM_PROB_NEVER)
546 schemdata[index].setContent(CONTENT_AIR);
550 for (size_t i = 0; i != splist->size(); i++) {
551 s16 y = (*splist)[i].first - p0.Y;
552 slice_probs[y] = (*splist)[i].second;
557 void generate_nodelist_and_update_ids(MapNode *nodes, size_t nodecount,
558 std::vector<std::string> *usednodes, INodeDefManager *ndef)
560 std::unordered_map<content_t, content_t> nodeidmap;
561 content_t numids = 0;
563 for (size_t i = 0; i != nodecount; i++) {
565 content_t c = nodes[i].getContent();
567 std::unordered_map<content_t, content_t>::const_iterator it = nodeidmap.find(c);
568 if (it == nodeidmap.end()) {
572 usednodes->push_back(ndef->get(c).name);
573 nodeidmap.insert(std::make_pair(c, id));
577 nodes[i].setContent(id);