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 (const std::bad_cast &) {
63 ObjDefManager::clear();
67 ///////////////////////////////////////////////////////////////////////////////
70 Schematic::Schematic()
74 Schematic::~Schematic()
81 void Schematic::resolveNodeNames()
83 getIdsFromNrBacklog(&c_nodes, true, CONTENT_AIR);
85 size_t bufsize = size.X * size.Y * size.Z;
86 for (size_t i = 0; i != bufsize; i++) {
87 content_t c_original = schemdata[i].getContent();
88 content_t c_new = c_nodes[c_original];
89 schemdata[i].setContent(c_new);
94 void Schematic::blitToVManip(MMVManip *vm, v3s16 p, Rotation rot, bool force_place)
96 sanity_check(m_ndef != NULL);
100 int zstride = size.X * size.Y;
106 int i_start, i_step_x, i_step_z;
115 i_start = zstride * (sz - 1) + sx - 1;
120 i_start = zstride * (sz - 1);
132 for (s16 y = 0; y != sy; y++) {
133 if ((slice_probs[y] != MTSCHEM_PROB_ALWAYS) &&
134 (slice_probs[y] <= myrand_range(1, MTSCHEM_PROB_ALWAYS)))
137 for (s16 z = 0; z != sz; z++) {
138 u32 i = z * i_step_z + y * ystride + i_start;
139 for (s16 x = 0; x != sx; x++, i += i_step_x) {
140 u32 vi = vm->m_area.index(p.X + x, y_map, p.Z + z);
141 if (!vm->m_area.contains(vi))
144 if (schemdata[i].getContent() == CONTENT_IGNORE)
147 u8 placement_prob = schemdata[i].param1 & MTSCHEM_PROB_MASK;
148 bool force_place_node = schemdata[i].param1 & MTSCHEM_FORCE_PLACE;
150 if (placement_prob == MTSCHEM_PROB_NEVER)
153 if (!force_place && !force_place_node) {
154 content_t c = vm->m_data[vi].getContent();
155 if (c != CONTENT_AIR && c != CONTENT_IGNORE)
159 if ((placement_prob != MTSCHEM_PROB_ALWAYS) &&
160 (placement_prob <= myrand_range(1, MTSCHEM_PROB_ALWAYS)))
163 vm->m_data[vi] = schemdata[i];
164 vm->m_data[vi].param1 = 0;
167 vm->m_data[vi].rotateAlongYAxis(m_ndef, rot);
175 bool Schematic::placeOnVManip(MMVManip *vm, v3s16 p, u32 flags,
176 Rotation rot, bool force_place)
179 assert(schemdata != NULL);
180 sanity_check(m_ndef != NULL);
182 //// Determine effective rotation and effective schematic dimensions
183 if (rot == ROTATE_RAND)
184 rot = (Rotation)myrand_range(ROTATE_0, ROTATE_270);
186 v3s16 s = (rot == ROTATE_90 || rot == ROTATE_270) ?
187 v3s16(size.Z, size.Y, size.X) : size;
189 //// Adjust placement position if necessary
190 if (flags & DECO_PLACE_CENTER_X)
191 p.X -= (s.X + 1) / 2;
192 if (flags & DECO_PLACE_CENTER_Y)
193 p.Y -= (s.Y + 1) / 2;
194 if (flags & DECO_PLACE_CENTER_Z)
195 p.Z -= (s.Z + 1) / 2;
197 blitToVManip(vm, p, rot, force_place);
199 return vm->m_area.contains(VoxelArea(p, p + s - v3s16(1,1,1)));
202 void Schematic::placeOnMap(ServerMap *map, v3s16 p, u32 flags,
203 Rotation rot, bool force_place)
205 std::map<v3s16, MapBlock *> lighting_modified_blocks;
206 std::map<v3s16, MapBlock *> modified_blocks;
207 std::map<v3s16, MapBlock *>::iterator it;
210 assert(schemdata != NULL);
211 sanity_check(m_ndef != NULL);
213 //// Determine effective rotation and effective schematic dimensions
214 if (rot == ROTATE_RAND)
215 rot = (Rotation)myrand_range(ROTATE_0, ROTATE_270);
217 v3s16 s = (rot == ROTATE_90 || rot == ROTATE_270) ?
218 v3s16(size.Z, size.Y, size.X) : size;
220 //// Adjust placement position if necessary
221 if (flags & DECO_PLACE_CENTER_X)
222 p.X -= (s.X + 1) / 2;
223 if (flags & DECO_PLACE_CENTER_Y)
224 p.Y -= (s.Y + 1) / 2;
225 if (flags & DECO_PLACE_CENTER_Z)
226 p.Z -= (s.Z + 1) / 2;
228 //// Create VManip for effected area, emerge our area, modify area
229 //// inside VManip, then blit back.
230 v3s16 bp1 = getNodeBlockPos(p);
231 v3s16 bp2 = getNodeBlockPos(p + s - v3s16(1,1,1));
234 vm.initialEmerge(bp1, bp2);
236 blitToVManip(&vm, p, rot, force_place);
238 voxalgo::blit_back_with_light(map, &vm, &modified_blocks);
240 //// Carry out post-map-modification actions
242 //// Create & dispatch map modification events to observers
244 event.type = MEET_OTHER;
245 for (it = modified_blocks.begin(); it != modified_blocks.end(); ++it)
246 event.modified_blocks.insert(it->first);
248 map->dispatchEvent(&event);
252 bool Schematic::deserializeFromMts(std::istream *is,
253 std::vector<std::string> *names)
255 std::istream &ss = *is;
256 content_t cignore = CONTENT_IGNORE;
257 bool have_cignore = false;
260 u32 signature = readU32(ss);
261 if (signature != MTSCHEM_FILE_SIGNATURE) {
262 errorstream << __FUNCTION__ << ": invalid schematic "
268 u16 version = readU16(ss);
269 if (version > MTSCHEM_FILE_VER_HIGHEST_READ) {
270 errorstream << __FUNCTION__ << ": unsupported schematic "
271 "file version" << std::endl;
276 size = readV3S16(ss);
278 //// Read Y-slice probability values
279 delete []slice_probs;
280 slice_probs = new u8[size.Y];
281 for (int y = 0; y != size.Y; y++)
282 slice_probs[y] = (version >= 3) ? readU8(ss) : MTSCHEM_PROB_ALWAYS_OLD;
285 u16 nidmapcount = readU16(ss);
286 for (int i = 0; i != nidmapcount; i++) {
287 std::string name = deSerializeString(ss);
289 // Instances of "ignore" from v1 are converted to air (and instances
290 // are fixed to have MTSCHEM_PROB_NEVER later on).
291 if (name == "ignore") {
297 names->push_back(name);
301 size_t nodecount = size.X * size.Y * size.Z;
304 schemdata = new MapNode[nodecount];
306 MapNode::deSerializeBulk(ss, SER_FMT_VER_HIGHEST_READ, schemdata,
307 nodecount, 2, 2, true);
309 // Fix probability values for nodes that were ignore; removed in v2
311 for (size_t i = 0; i != nodecount; i++) {
312 if (schemdata[i].param1 == 0)
313 schemdata[i].param1 = MTSCHEM_PROB_ALWAYS_OLD;
314 if (have_cignore && schemdata[i].getContent() == cignore)
315 schemdata[i].param1 = MTSCHEM_PROB_NEVER;
319 // Fix probability values for probability range truncation introduced in v4
321 for (s16 y = 0; y != size.Y; y++)
322 slice_probs[y] >>= 1;
323 for (size_t i = 0; i != nodecount; i++)
324 schemdata[i].param1 >>= 1;
331 bool Schematic::serializeToMts(std::ostream *os,
332 const std::vector<std::string> &names)
334 std::ostream &ss = *os;
336 writeU32(ss, MTSCHEM_FILE_SIGNATURE); // signature
337 writeU16(ss, MTSCHEM_FILE_VER_HIGHEST_WRITE); // version
338 writeV3S16(ss, size); // schematic size
340 for (int y = 0; y != size.Y; y++) // Y slice probabilities
341 writeU8(ss, slice_probs[y]);
343 writeU16(ss, names.size()); // name count
344 for (size_t i = 0; i != names.size(); i++)
345 ss << serializeString(names[i]); // node names
347 // compressed bulk node data
348 MapNode::serializeBulk(ss, SER_FMT_VER_HIGHEST_WRITE,
349 schemdata, size.X * size.Y * size.Z, 2, 2, true);
355 bool Schematic::serializeToLua(std::ostream *os,
356 const std::vector<std::string> &names, bool use_comments, u32 indent_spaces)
358 std::ostream &ss = *os;
360 std::string indent("\t");
361 if (indent_spaces > 0)
362 indent.assign(indent_spaces, ' ');
366 ss << "schematic = {" << std::endl;
367 ss << indent << "size = "
371 << "}," << std::endl;
374 //// Write y-slice probabilities
376 ss << indent << "yslice_prob = {" << std::endl;
378 for (u16 y = 0; y != size.Y; y++) {
379 u8 probability = slice_probs[y] & MTSCHEM_PROB_MASK;
381 ss << indent << indent << "{"
383 << ", prob=" << (u16)probability * 2
384 << "}," << std::endl;
387 ss << indent << "}," << std::endl;
392 ss << indent << "data = {" << std::endl;
395 for (u16 z = 0; z != size.Z; z++)
396 for (u16 y = 0; y != size.Y; y++) {
401 << ", y=" << y << std::endl;
404 for (u16 x = 0; x != size.X; x++, i++) {
405 u8 probability = schemdata[i].param1 & MTSCHEM_PROB_MASK;
406 bool force_place = schemdata[i].param1 & MTSCHEM_FORCE_PLACE;
408 ss << indent << indent << "{"
409 << "name=\"" << names[schemdata[i].getContent()]
410 << "\", prob=" << (u16)probability * 2
411 << ", param2=" << (u16)schemdata[i].param2;
414 ss << ", force_place=true";
416 ss << "}," << std::endl;
420 ss << indent << "}," << std::endl;
423 ss << "}" << std::endl;
429 bool Schematic::loadSchematicFromFile(const std::string &filename,
430 INodeDefManager *ndef, StringMap *replace_names)
432 std::ifstream is(filename.c_str(), std::ios_base::binary);
434 errorstream << __FUNCTION__ << ": unable to open file '"
435 << filename << "'" << std::endl;
439 size_t origsize = m_nodenames.size();
440 if (!deserializeFromMts(&is, &m_nodenames))
443 m_nnlistsizes.push_back(m_nodenames.size() - origsize);
448 for (size_t i = origsize; i < m_nodenames.size(); i++) {
449 std::string &node_name = m_nodenames[i];
450 StringMap::iterator it = replace_names->find(node_name);
451 if (it != replace_names->end())
452 node_name = it->second;
457 ndef->pendNodeResolve(this);
463 bool Schematic::saveSchematicToFile(const std::string &filename,
464 INodeDefManager *ndef)
466 MapNode *orig_schemdata = schemdata;
467 std::vector<std::string> ndef_nodenames;
468 std::vector<std::string> *names;
470 if (m_resolve_done && ndef == NULL)
474 names = &ndef_nodenames;
476 u32 volume = size.X * size.Y * size.Z;
477 schemdata = new MapNode[volume];
478 for (u32 i = 0; i != volume; i++)
479 schemdata[i] = orig_schemdata[i];
481 generate_nodelist_and_update_ids(schemdata, volume, names, ndef);
482 } else { // otherwise, use the names we have on hand in the list
483 names = &m_nodenames;
486 std::ostringstream os(std::ios_base::binary);
487 bool status = serializeToMts(&os, *names);
491 schemdata = orig_schemdata;
497 return fs::safeWriteToFile(filename, os.str());
501 bool Schematic::getSchematicFromMap(Map *map, v3s16 p1, v3s16 p2)
503 MMVManip *vm = new MMVManip(map);
505 v3s16 bp1 = getNodeBlockPos(p1);
506 v3s16 bp2 = getNodeBlockPos(p2);
507 vm->initialEmerge(bp1, bp2);
511 slice_probs = new u8[size.Y];
512 for (s16 y = 0; y != size.Y; y++)
513 slice_probs[y] = MTSCHEM_PROB_ALWAYS;
515 schemdata = new MapNode[size.X * size.Y * size.Z];
518 for (s16 z = p1.Z; z <= p2.Z; z++)
519 for (s16 y = p1.Y; y <= p2.Y; y++) {
520 u32 vi = vm->m_area.index(p1.X, y, z);
521 for (s16 x = p1.X; x <= p2.X; x++, i++, vi++) {
522 schemdata[i] = vm->m_data[vi];
523 schemdata[i].param1 = MTSCHEM_PROB_ALWAYS;
532 void Schematic::applyProbabilities(v3s16 p0,
533 std::vector<std::pair<v3s16, u8> > *plist,
534 std::vector<std::pair<s16, u8> > *splist)
536 for (size_t i = 0; i != plist->size(); i++) {
537 v3s16 p = (*plist)[i].first - p0;
538 int index = p.Z * (size.Y * size.X) + p.Y * size.X + p.X;
539 if (index < size.Z * size.Y * size.X) {
540 u8 prob = (*plist)[i].second;
541 schemdata[index].param1 = prob;
543 // trim unnecessary node names from schematic
544 if (prob == MTSCHEM_PROB_NEVER)
545 schemdata[index].setContent(CONTENT_AIR);
549 for (size_t i = 0; i != splist->size(); i++) {
550 s16 y = (*splist)[i].first - p0.Y;
551 slice_probs[y] = (*splist)[i].second;
556 void generate_nodelist_and_update_ids(MapNode *nodes, size_t nodecount,
557 std::vector<std::string> *usednodes, INodeDefManager *ndef)
559 std::unordered_map<content_t, content_t> nodeidmap;
560 content_t numids = 0;
562 for (size_t i = 0; i != nodecount; i++) {
564 content_t c = nodes[i].getContent();
566 std::unordered_map<content_t, content_t>::const_iterator it = nodeidmap.find(c);
567 if (it == nodeidmap.end()) {
571 usednodes->push_back(ndef->get(c).name);
572 nodeidmap.insert(std::make_pair(c, id));
576 nodes[i].setContent(id);