3 Copyright (C) 2010-2014 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "mg_schematic.h"
29 #include "util/numeric.h"
30 #include "util/serialize.h"
31 #include "serialization.h"
34 ///////////////////////////////////////////////////////////////////////////////
37 SchematicManager::SchematicManager(IGameDef *gamedef) :
38 ObjDefManager(gamedef, OBJDEF_SCHEMATIC)
44 void SchematicManager::clear()
46 EmergeManager *emerge = m_gamedef->getEmergeManager();
48 // Remove all dangling references in Decorations
49 DecorationManager *decomgr = emerge->decomgr;
50 for (size_t i = 0; i != decomgr->getNumObjects(); i++) {
51 Decoration *deco = (Decoration *)decomgr->getRaw(i);
54 DecoSchematic *dschem = dynamic_cast<DecoSchematic *>(deco);
56 dschem->schematic = NULL;
57 } catch (std::bad_cast) {
61 ObjDefManager::clear();
65 ///////////////////////////////////////////////////////////////////////////////
68 Schematic::Schematic()
73 size = v3s16(0, 0, 0);
77 Schematic::~Schematic()
84 void Schematic::resolveNodeNames()
86 getIdsFromNrBacklog(&c_nodes, true, CONTENT_AIR);
88 size_t bufsize = size.X * size.Y * size.Z;
89 for (size_t i = 0; i != bufsize; i++) {
90 content_t c_original = schemdata[i].getContent();
91 content_t c_new = c_nodes[c_original];
92 schemdata[i].setContent(c_new);
97 void Schematic::blitToVManip(v3s16 p, MMVManip *vm, Rotation rot, bool force_place)
99 sanity_check(m_ndef != NULL);
102 int ystride = size.X;
103 int zstride = size.X * size.Y;
109 int i_start, i_step_x, i_step_z;
118 i_start = zstride * (sz - 1) + sx - 1;
123 i_start = zstride * (sz - 1);
135 for (s16 y = 0; y != sy; y++) {
136 if ((slice_probs[y] != MTSCHEM_PROB_ALWAYS) &&
137 (slice_probs[y] <= myrand_range(1, MTSCHEM_PROB_ALWAYS)))
140 for (s16 z = 0; z != sz; z++) {
141 u32 i = z * i_step_z + y * ystride + i_start;
142 for (s16 x = 0; x != sx; x++, i += i_step_x) {
143 u32 vi = vm->m_area.index(p.X + x, y_map, p.Z + z);
144 if (!vm->m_area.contains(vi))
147 if (schemdata[i].getContent() == CONTENT_IGNORE)
150 u8 placement_prob = schemdata[i].param1 & MTSCHEM_PROB_MASK;
151 bool force_place_node = schemdata[i].param1 & MTSCHEM_FORCE_PLACE;
153 if (placement_prob == MTSCHEM_PROB_NEVER)
156 if (!force_place && !force_place_node) {
157 content_t c = vm->m_data[vi].getContent();
158 if (c != CONTENT_AIR && c != CONTENT_IGNORE)
162 if ((placement_prob != MTSCHEM_PROB_ALWAYS) &&
163 (placement_prob <= myrand_range(1, MTSCHEM_PROB_ALWAYS)))
166 vm->m_data[vi] = schemdata[i];
167 vm->m_data[vi].param1 = 0;
170 vm->m_data[vi].rotateAlongYAxis(m_ndef, rot);
178 void Schematic::placeStructure(Map *map, v3s16 p, u32 flags,
179 Rotation rot, bool force_place)
181 assert(schemdata != NULL); // Pre-condition
182 sanity_check(m_ndef != NULL);
184 MMVManip *vm = new MMVManip(map);
186 if (rot == ROTATE_RAND)
187 rot = (Rotation)myrand_range(ROTATE_0, ROTATE_270);
189 v3s16 s = (rot == ROTATE_90 || rot == ROTATE_270) ?
190 v3s16(size.Z, size.Y, size.X) : size;
192 if (flags & DECO_PLACE_CENTER_X)
193 p.X -= (s.X + 1) / 2;
194 if (flags & DECO_PLACE_CENTER_Y)
195 p.Y -= (s.Y + 1) / 2;
196 if (flags & DECO_PLACE_CENTER_Z)
197 p.Z -= (s.Z + 1) / 2;
199 v3s16 bp1 = getNodeBlockPos(p);
200 v3s16 bp2 = getNodeBlockPos(p + s - v3s16(1,1,1));
201 vm->initialEmerge(bp1, bp2);
203 blitToVManip(p, vm, rot, force_place);
205 std::map<v3s16, MapBlock *> lighting_modified_blocks;
206 std::map<v3s16, MapBlock *> modified_blocks;
207 vm->blitBackAll(&modified_blocks);
209 // TODO: Optimize this by using Mapgen::calcLighting() instead
210 lighting_modified_blocks.insert(modified_blocks.begin(), modified_blocks.end());
211 map->updateLighting(lighting_modified_blocks, modified_blocks);
214 event.type = MEET_OTHER;
215 for (std::map<v3s16, MapBlock *>::iterator
216 it = modified_blocks.begin();
217 it != modified_blocks.end(); ++it)
218 event.modified_blocks.insert(it->first);
220 map->dispatchEvent(&event);
224 bool Schematic::deserializeFromMts(std::istream *is,
225 std::vector<std::string> *names)
227 std::istream &ss = *is;
228 content_t cignore = CONTENT_IGNORE;
229 bool have_cignore = false;
232 u32 signature = readU32(ss);
233 if (signature != MTSCHEM_FILE_SIGNATURE) {
234 errorstream << "Schematic::deserializeFromMts: invalid schematic "
240 u16 version = readU16(ss);
241 if (version > MTSCHEM_FILE_VER_HIGHEST_READ) {
242 errorstream << "Schematic::deserializeFromMts: unsupported schematic "
243 "file version" << std::endl;
248 size = readV3S16(ss);
250 //// Read Y-slice probability values
251 delete []slice_probs;
252 slice_probs = new u8[size.Y];
253 for (int y = 0; y != size.Y; y++)
254 slice_probs[y] = (version >= 3) ? readU8(ss) : MTSCHEM_PROB_ALWAYS_OLD;
257 u16 nidmapcount = readU16(ss);
258 for (int i = 0; i != nidmapcount; i++) {
259 std::string name = deSerializeString(ss);
261 // Instances of "ignore" from v1 are converted to air (and instances
262 // are fixed to have MTSCHEM_PROB_NEVER later on).
263 if (name == "ignore") {
269 names->push_back(name);
273 size_t nodecount = size.X * size.Y * size.Z;
276 schemdata = new MapNode[nodecount];
278 MapNode::deSerializeBulk(ss, SER_FMT_VER_HIGHEST_READ, schemdata,
279 nodecount, 2, 2, true);
281 // Fix probability values for nodes that were ignore; removed in v2
283 for (size_t i = 0; i != nodecount; i++) {
284 if (schemdata[i].param1 == 0)
285 schemdata[i].param1 = MTSCHEM_PROB_ALWAYS;
286 if (have_cignore && schemdata[i].getContent() == cignore)
287 schemdata[i].param1 = MTSCHEM_PROB_NEVER;
291 // Fix probability values for probability range truncation introduced in v4
293 for (s16 y = 0; y != size.Y; y++)
294 slice_probs[y] >>= 1;
295 for (size_t i = 0; i != nodecount; i++)
296 schemdata[i].param1 >>= 1;
303 bool Schematic::serializeToMts(std::ostream *os,
304 const std::vector<std::string> &names)
306 std::ostream &ss = *os;
308 writeU32(ss, MTSCHEM_FILE_SIGNATURE); // signature
309 writeU16(ss, MTSCHEM_FILE_VER_HIGHEST_WRITE); // version
310 writeV3S16(ss, size); // schematic size
312 for (int y = 0; y != size.Y; y++) // Y slice probabilities
313 writeU8(ss, slice_probs[y]);
315 writeU16(ss, names.size()); // name count
316 for (size_t i = 0; i != names.size(); i++)
317 ss << serializeString(names[i]); // node names
319 // compressed bulk node data
320 MapNode::serializeBulk(ss, SER_FMT_VER_HIGHEST_WRITE,
321 schemdata, size.X * size.Y * size.Z, 2, 2, true);
327 bool Schematic::serializeToLua(std::ostream *os,
328 const std::vector<std::string> &names, bool use_comments, u32 indent_spaces)
330 std::ostream &ss = *os;
332 std::string indent("\t");
333 if (indent_spaces > 0)
334 indent.assign(indent_spaces, ' ');
338 ss << "schematic = {" << std::endl;
339 ss << indent << "size = "
343 << "}," << std::endl;
346 //// Write y-slice probabilities
348 ss << indent << "yslice_prob = {" << std::endl;
350 for (u16 y = 0; y != size.Y; y++) {
351 u8 probability = slice_probs[y] & MTSCHEM_PROB_MASK;
353 ss << indent << indent << "{"
355 << ", prob=" << (u16)probability * 2
356 << "}," << std::endl;
359 ss << indent << "}," << std::endl;
364 ss << indent << "data = {" << std::endl;
367 for (u16 z = 0; z != size.Z; z++)
368 for (u16 y = 0; y != size.Y; y++) {
373 << ", y=" << y << std::endl;
376 for (u16 x = 0; x != size.X; x++, i++) {
377 u8 probability = schemdata[i].param1 & MTSCHEM_PROB_MASK;
378 bool force_place = schemdata[i].param1 & MTSCHEM_FORCE_PLACE;
380 ss << indent << indent << "{"
381 << "name=\"" << names[schemdata[i].getContent()]
382 << "\", prob=" << (u16)probability * 2
383 << ", param2=" << (u16)schemdata[i].param2;
386 ss << ", force_place=true";
388 ss << "}," << std::endl;
392 ss << indent << "}," << std::endl;
395 ss << "}" << std::endl;
401 bool Schematic::loadSchematicFromFile(const std::string &filename,
402 INodeDefManager *ndef, StringMap *replace_names)
404 std::ifstream is(filename.c_str(), std::ios_base::binary);
406 errorstream << "Schematic::loadSchematicFile: unable to open file '"
407 << filename << "'" << std::endl;
411 size_t origsize = m_nodenames.size();
412 if (!deserializeFromMts(&is, &m_nodenames))
416 for (size_t i = origsize; i != m_nodenames.size(); i++) {
417 std::string &name = m_nodenames[i];
418 StringMap::iterator it = replace_names->find(name);
419 if (it != replace_names->end())
424 m_nnlistsizes.push_back(m_nodenames.size() - origsize);
427 ndef->pendNodeResolve(this);
433 bool Schematic::saveSchematicToFile(const std::string &filename,
434 INodeDefManager *ndef)
436 MapNode *orig_schemdata = schemdata;
437 std::vector<std::string> ndef_nodenames;
438 std::vector<std::string> *names;
440 if (m_resolve_done && ndef == NULL)
444 names = &ndef_nodenames;
446 u32 volume = size.X * size.Y * size.Z;
447 schemdata = new MapNode[volume];
448 for (u32 i = 0; i != volume; i++)
449 schemdata[i] = orig_schemdata[i];
451 generate_nodelist_and_update_ids(schemdata, volume, names, ndef);
452 } else { // otherwise, use the names we have on hand in the list
453 names = &m_nodenames;
456 std::ostringstream os(std::ios_base::binary);
457 bool status = serializeToMts(&os, *names);
461 schemdata = orig_schemdata;
467 return fs::safeWriteToFile(filename, os.str());
471 bool Schematic::getSchematicFromMap(Map *map, v3s16 p1, v3s16 p2)
473 MMVManip *vm = new MMVManip(map);
475 v3s16 bp1 = getNodeBlockPos(p1);
476 v3s16 bp2 = getNodeBlockPos(p2);
477 vm->initialEmerge(bp1, bp2);
481 slice_probs = new u8[size.Y];
482 for (s16 y = 0; y != size.Y; y++)
483 slice_probs[y] = MTSCHEM_PROB_ALWAYS;
485 schemdata = new MapNode[size.X * size.Y * size.Z];
488 for (s16 z = p1.Z; z <= p2.Z; z++)
489 for (s16 y = p1.Y; y <= p2.Y; y++) {
490 u32 vi = vm->m_area.index(p1.X, y, z);
491 for (s16 x = p1.X; x <= p2.X; x++, i++, vi++) {
492 schemdata[i] = vm->m_data[vi];
493 schemdata[i].param1 = MTSCHEM_PROB_ALWAYS;
502 void Schematic::applyProbabilities(v3s16 p0,
503 std::vector<std::pair<v3s16, u8> > *plist,
504 std::vector<std::pair<s16, u8> > *splist)
506 for (size_t i = 0; i != plist->size(); i++) {
507 v3s16 p = (*plist)[i].first - p0;
508 int index = p.Z * (size.Y * size.X) + p.Y * size.X + p.X;
509 if (index < size.Z * size.Y * size.X) {
510 u8 prob = (*plist)[i].second;
511 schemdata[index].param1 = prob;
513 // trim unnecessary node names from schematic
514 if (prob == MTSCHEM_PROB_NEVER)
515 schemdata[index].setContent(CONTENT_AIR);
519 for (size_t i = 0; i != splist->size(); i++) {
520 s16 y = (*splist)[i].first - p0.Y;
521 slice_probs[y] = (*splist)[i].second;
526 void generate_nodelist_and_update_ids(MapNode *nodes, size_t nodecount,
527 std::vector<std::string> *usednodes, INodeDefManager *ndef)
529 std::map<content_t, content_t> nodeidmap;
530 content_t numids = 0;
532 for (size_t i = 0; i != nodecount; i++) {
534 content_t c = nodes[i].getContent();
536 std::map<content_t, content_t>::const_iterator it = nodeidmap.find(c);
537 if (it == nodeidmap.end()) {
541 usednodes->push_back(ndef->get(c).name);
542 nodeidmap.insert(std::make_pair(c, id));
546 nodes[i].setContent(id);