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 myrand_range(1, 255) > slice_probs[y])
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 if (schemdata[i].param1 == MTSCHEM_PROB_NEVER)
154 content_t c = vm->m_data[vi].getContent();
155 if (c != CONTENT_AIR && c != CONTENT_IGNORE)
159 if (schemdata[i].param1 != MTSCHEM_PROB_ALWAYS &&
160 myrand_range(1, 255) > schemdata[i].param1)
163 vm->m_data[vi] = schemdata[i];
164 vm->m_data[vi].param1 = 0;
167 vm->m_data[vi].rotateAlongYAxis(m_ndef, rot);
175 void Schematic::placeStructure(Map *map, v3s16 p, u32 flags,
176 Rotation rot, bool force_place)
178 assert(schemdata != NULL); // Pre-condition
179 sanity_check(m_ndef != NULL);
181 MMVManip *vm = new MMVManip(map);
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 if (flags & DECO_PLACE_CENTER_X)
190 p.X -= (s.X + 1) / 2;
191 if (flags & DECO_PLACE_CENTER_Y)
192 p.Y -= (s.Y + 1) / 2;
193 if (flags & DECO_PLACE_CENTER_Z)
194 p.Z -= (s.Z + 1) / 2;
196 v3s16 bp1 = getNodeBlockPos(p);
197 v3s16 bp2 = getNodeBlockPos(p + s - v3s16(1,1,1));
198 vm->initialEmerge(bp1, bp2);
200 blitToVManip(p, vm, rot, force_place);
202 std::map<v3s16, MapBlock *> lighting_modified_blocks;
203 std::map<v3s16, MapBlock *> modified_blocks;
204 vm->blitBackAll(&modified_blocks);
206 // TODO: Optimize this by using Mapgen::calcLighting() instead
207 lighting_modified_blocks.insert(modified_blocks.begin(), modified_blocks.end());
208 map->updateLighting(lighting_modified_blocks, modified_blocks);
211 event.type = MEET_OTHER;
212 for (std::map<v3s16, MapBlock *>::iterator
213 it = modified_blocks.begin();
214 it != modified_blocks.end(); ++it)
215 event.modified_blocks.insert(it->first);
217 map->dispatchEvent(&event);
221 bool Schematic::deserializeFromMts(std::istream *is,
222 std::vector<std::string> *names)
224 std::istream &ss = *is;
225 content_t cignore = CONTENT_IGNORE;
226 bool have_cignore = false;
228 u32 signature = readU32(ss);
229 if (signature != MTSCHEM_FILE_SIGNATURE) {
230 errorstream << "Schematic::deserializeFromMts: invalid schematic "
235 u16 version = readU16(ss);
236 if (version > MTSCHEM_FILE_VER_HIGHEST_READ) {
237 errorstream << "Schematic::deserializeFromMts: unsupported schematic "
238 "file version" << std::endl;
242 size = readV3S16(ss);
244 delete []slice_probs;
245 slice_probs = new u8[size.Y];
246 for (int y = 0; y != size.Y; y++)
247 slice_probs[y] = (version >= 3) ? readU8(ss) : MTSCHEM_PROB_ALWAYS;
249 u16 nidmapcount = readU16(ss);
250 for (int i = 0; i != nidmapcount; i++) {
251 std::string name = deSerializeString(ss);
253 // Instances of "ignore" from ver 1 are converted to air (and instances
254 // are fixed to have MTSCHEM_PROB_NEVER later on).
255 if (name == "ignore") {
261 names->push_back(name);
264 size_t nodecount = size.X * size.Y * size.Z;
267 schemdata = new MapNode[nodecount];
269 MapNode::deSerializeBulk(ss, SER_FMT_VER_HIGHEST_READ, schemdata,
270 nodecount, 2, 2, true);
272 // fix any probability values for nodes that were ignore
274 for (size_t i = 0; i != nodecount; i++) {
275 if (schemdata[i].param1 == 0)
276 schemdata[i].param1 = MTSCHEM_PROB_ALWAYS;
277 if (have_cignore && schemdata[i].getContent() == cignore)
278 schemdata[i].param1 = MTSCHEM_PROB_NEVER;
286 bool Schematic::serializeToMts(std::ostream *os,
287 const std::vector<std::string> &names)
289 std::ostream &ss = *os;
291 writeU32(ss, MTSCHEM_FILE_SIGNATURE); // signature
292 writeU16(ss, MTSCHEM_FILE_VER_HIGHEST_WRITE); // version
293 writeV3S16(ss, size); // schematic size
295 for (int y = 0; y != size.Y; y++) // Y slice probabilities
296 writeU8(ss, slice_probs[y]);
298 writeU16(ss, names.size()); // name count
299 for (size_t i = 0; i != names.size(); i++)
300 ss << serializeString(names[i]); // node names
302 // compressed bulk node data
303 MapNode::serializeBulk(ss, SER_FMT_VER_HIGHEST_WRITE,
304 schemdata, size.X * size.Y * size.Z, 2, 2, true);
310 bool Schematic::serializeToLua(std::ostream *os,
311 const std::vector<std::string> &names, bool use_comments, u32 indent_spaces)
313 std::ostream &ss = *os;
315 std::string indent("\t");
316 if (indent_spaces > 0)
317 indent.assign(indent_spaces, ' ');
321 ss << "schematic = {" << std::endl;
322 ss << indent << "size = "
326 << "}," << std::endl;
329 //// Write y-slice probabilities
331 ss << indent << "yslice_prob = {" << std::endl;
333 for (u16 y = 0; y != size.Y; y++) {
334 ss << indent << indent << "{"
336 << ", prob=" << (u16)slice_probs[y]
337 << "}," << std::endl;
340 ss << indent << "}," << std::endl;
345 ss << indent << "data = {" << std::endl;
348 for (u16 z = 0; z != size.Z; z++)
349 for (u16 y = 0; y != size.Y; y++) {
354 << ", y=" << y << std::endl;
357 for (u16 x = 0; x != size.X; x++, i++) {
358 ss << indent << indent << "{"
359 << "name=\"" << names[schemdata[i].getContent()]
360 << "\", param1=" << (u16)schemdata[i].param1
361 << ", param2=" << (u16)schemdata[i].param2
362 << "}," << std::endl;
366 ss << indent << "}," << std::endl;
369 ss << "}" << std::endl;
375 bool Schematic::loadSchematicFromFile(const std::string &filename,
376 INodeDefManager *ndef, StringMap *replace_names)
378 std::ifstream is(filename.c_str(), std::ios_base::binary);
380 errorstream << "Schematic::loadSchematicFile: unable to open file '"
381 << filename << "'" << std::endl;
385 size_t origsize = m_nodenames.size();
386 if (!deserializeFromMts(&is, &m_nodenames))
390 for (size_t i = origsize; i != m_nodenames.size(); i++) {
391 std::string &name = m_nodenames[i];
392 StringMap::iterator it = replace_names->find(name);
393 if (it != replace_names->end())
398 m_nnlistsizes.push_back(m_nodenames.size() - origsize);
401 ndef->pendNodeResolve(this);
407 bool Schematic::saveSchematicToFile(const std::string &filename,
408 INodeDefManager *ndef)
410 MapNode *orig_schemdata = schemdata;
411 std::vector<std::string> ndef_nodenames;
412 std::vector<std::string> *names;
414 if (m_resolve_done && ndef == NULL)
418 names = &ndef_nodenames;
420 u32 volume = size.X * size.Y * size.Z;
421 schemdata = new MapNode[volume];
422 for (u32 i = 0; i != volume; i++)
423 schemdata[i] = orig_schemdata[i];
425 generate_nodelist_and_update_ids(schemdata, volume, names, ndef);
426 } else { // otherwise, use the names we have on hand in the list
427 names = &m_nodenames;
430 std::ostringstream os(std::ios_base::binary);
431 bool status = serializeToMts(&os, *names);
435 schemdata = orig_schemdata;
441 return fs::safeWriteToFile(filename, os.str());
445 bool Schematic::getSchematicFromMap(Map *map, v3s16 p1, v3s16 p2)
447 MMVManip *vm = new MMVManip(map);
449 v3s16 bp1 = getNodeBlockPos(p1);
450 v3s16 bp2 = getNodeBlockPos(p2);
451 vm->initialEmerge(bp1, bp2);
455 slice_probs = new u8[size.Y];
456 for (s16 y = 0; y != size.Y; y++)
457 slice_probs[y] = MTSCHEM_PROB_ALWAYS;
459 schemdata = new MapNode[size.X * size.Y * size.Z];
462 for (s16 z = p1.Z; z <= p2.Z; z++)
463 for (s16 y = p1.Y; y <= p2.Y; y++) {
464 u32 vi = vm->m_area.index(p1.X, y, z);
465 for (s16 x = p1.X; x <= p2.X; x++, i++, vi++) {
466 schemdata[i] = vm->m_data[vi];
467 schemdata[i].param1 = MTSCHEM_PROB_ALWAYS;
476 void Schematic::applyProbabilities(v3s16 p0,
477 std::vector<std::pair<v3s16, u8> > *plist,
478 std::vector<std::pair<s16, u8> > *splist)
480 for (size_t i = 0; i != plist->size(); i++) {
481 v3s16 p = (*plist)[i].first - p0;
482 int index = p.Z * (size.Y * size.X) + p.Y * size.X + p.X;
483 if (index < size.Z * size.Y * size.X) {
484 u8 prob = (*plist)[i].second;
485 schemdata[index].param1 = prob;
487 // trim unnecessary node names from schematic
488 if (prob == MTSCHEM_PROB_NEVER)
489 schemdata[index].setContent(CONTENT_AIR);
493 for (size_t i = 0; i != splist->size(); i++) {
494 s16 y = (*splist)[i].first - p0.Y;
495 slice_probs[y] = (*splist)[i].second;
500 void generate_nodelist_and_update_ids(MapNode *nodes, size_t nodecount,
501 std::vector<std::string> *usednodes, INodeDefManager *ndef)
503 std::map<content_t, content_t> nodeidmap;
504 content_t numids = 0;
506 for (size_t i = 0; i != nodecount; i++) {
508 content_t c = nodes[i].getContent();
510 std::map<content_t, content_t>::const_iterator it = nodeidmap.find(c);
511 if (it == nodeidmap.end()) {
515 usednodes->push_back(ndef->get(c).name);
516 nodeidmap.insert(std::make_pair(c, id));
520 nodes[i].setContent(id);