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,
98 bool force_placement, INodeDefManager *ndef)
101 int ystride = size.X;
102 int zstride = size.X * size.Y;
108 int i_start, i_step_x, i_step_z;
117 i_start = zstride * (sz - 1) + sx - 1;
122 i_start = zstride * (sz - 1);
134 for (s16 y = 0; y != sy; y++) {
135 if (slice_probs[y] != MTSCHEM_PROB_ALWAYS &&
136 myrand_range(1, 255) > slice_probs[y])
139 for (s16 z = 0; z != sz; z++) {
140 u32 i = z * i_step_z + y * ystride + i_start;
141 for (s16 x = 0; x != sx; x++, i += i_step_x) {
142 u32 vi = vm->m_area.index(p.X + x, y_map, p.Z + z);
143 if (!vm->m_area.contains(vi))
146 if (schemdata[i].getContent() == CONTENT_IGNORE)
149 if (schemdata[i].param1 == MTSCHEM_PROB_NEVER)
152 if (!force_placement) {
153 content_t c = vm->m_data[vi].getContent();
154 if (c != CONTENT_AIR && c != CONTENT_IGNORE)
158 if (schemdata[i].param1 != MTSCHEM_PROB_ALWAYS &&
159 myrand_range(1, 255) > schemdata[i].param1)
162 vm->m_data[vi] = schemdata[i];
163 vm->m_data[vi].param1 = 0;
166 vm->m_data[vi].rotateAlongYAxis(ndef, rot);
174 void Schematic::placeStructure(Map *map, v3s16 p, u32 flags, Rotation rot,
175 bool force_placement, INodeDefManager *ndef)
177 assert(schemdata != NULL); // Pre-condition
178 MMVManip *vm = new MMVManip(map);
180 if (rot == ROTATE_RAND)
181 rot = (Rotation)myrand_range(ROTATE_0, ROTATE_270);
183 v3s16 s = (rot == ROTATE_90 || rot == ROTATE_270) ?
184 v3s16(size.Z, size.Y, size.X) : size;
186 if (flags & DECO_PLACE_CENTER_X)
187 p.X -= (s.X + 1) / 2;
188 if (flags & DECO_PLACE_CENTER_Y)
189 p.Y -= (s.Y + 1) / 2;
190 if (flags & DECO_PLACE_CENTER_Z)
191 p.Z -= (s.Z + 1) / 2;
193 v3s16 bp1 = getNodeBlockPos(p);
194 v3s16 bp2 = getNodeBlockPos(p + s - v3s16(1,1,1));
195 vm->initialEmerge(bp1, bp2);
197 blitToVManip(p, vm, rot, force_placement, ndef);
199 std::map<v3s16, MapBlock *> lighting_modified_blocks;
200 std::map<v3s16, MapBlock *> modified_blocks;
201 vm->blitBackAll(&modified_blocks);
203 // TODO: Optimize this by using Mapgen::calcLighting() instead
204 lighting_modified_blocks.insert(modified_blocks.begin(), modified_blocks.end());
205 map->updateLighting(lighting_modified_blocks, modified_blocks);
208 event.type = MEET_OTHER;
209 for (std::map<v3s16, MapBlock *>::iterator
210 it = modified_blocks.begin();
211 it != modified_blocks.end(); ++it)
212 event.modified_blocks.insert(it->first);
214 map->dispatchEvent(&event);
218 bool Schematic::deserializeFromMts(std::istream *is, std::vector<std::string> *names)
220 std::istream &ss = *is;
221 content_t cignore = CONTENT_IGNORE;
222 bool have_cignore = false;
224 u32 signature = readU32(ss);
225 if (signature != MTSCHEM_FILE_SIGNATURE) {
226 errorstream << "Schematic::deserializeFromMts: invalid schematic "
231 u16 version = readU16(ss);
232 if (version > MTSCHEM_FILE_VER_HIGHEST_READ) {
233 errorstream << "Schematic::deserializeFromMts: unsupported schematic "
234 "file version" << std::endl;
238 size = readV3S16(ss);
240 delete []slice_probs;
241 slice_probs = new u8[size.Y];
242 for (int y = 0; y != size.Y; y++)
243 slice_probs[y] = (version >= 3) ? readU8(ss) : MTSCHEM_PROB_ALWAYS;
245 u16 nidmapcount = readU16(ss);
246 for (int i = 0; i != nidmapcount; i++) {
247 std::string name = deSerializeString(ss);
249 // Instances of "ignore" from ver 1 are converted to air (and instances
250 // are fixed to have MTSCHEM_PROB_NEVER later on).
251 if (name == "ignore") {
257 names->push_back(name);
260 size_t nodecount = size.X * size.Y * size.Z;
263 schemdata = new MapNode[nodecount];
265 MapNode::deSerializeBulk(ss, SER_FMT_VER_HIGHEST_READ, schemdata,
266 nodecount, 2, 2, true);
268 // fix any probability values for nodes that were ignore
270 for (size_t i = 0; i != nodecount; i++) {
271 if (schemdata[i].param1 == 0)
272 schemdata[i].param1 = MTSCHEM_PROB_ALWAYS;
273 if (have_cignore && schemdata[i].getContent() == cignore)
274 schemdata[i].param1 = MTSCHEM_PROB_NEVER;
282 bool Schematic::serializeToMts(std::ostream *os)
284 std::ostream &ss = *os;
286 writeU32(ss, MTSCHEM_FILE_SIGNATURE); // signature
287 writeU16(ss, MTSCHEM_FILE_VER_HIGHEST_WRITE); // version
288 writeV3S16(ss, size); // schematic size
290 for (int y = 0; y != size.Y; y++) // Y slice probabilities
291 writeU8(ss, slice_probs[y]);
293 std::vector<content_t> usednodes;
294 int nodecount = size.X * size.Y * size.Z;
295 build_nnlist_and_update_ids(schemdata, nodecount, &usednodes);
297 u16 numids = usednodes.size();
298 writeU16(ss, numids); // name count
299 for (int i = 0; i != numids; i++)
300 ss << serializeString(getNodeName(usednodes[i])); // node names
302 // compressed bulk node data
303 MapNode::serializeBulk(ss, SER_FMT_VER_HIGHEST_WRITE,
304 schemdata, nodecount, 2, 2, true);
310 bool Schematic::serializeToLua(std::ostream *os, bool use_comments)
312 std::ostream &ss = *os;
316 ss << "schematic = {" << std::endl;
321 << "}," << std::endl;
324 //// Write y-slice probabilities
326 ss << "\tyslice_prob = {" << std::endl;
328 for (u16 y = 0; y != size.Y; y++) {
331 << ", prob=" << (u16)slice_probs[y]
332 << "}," << std::endl;
335 ss << "\t}," << std::endl;
340 ss << "\tdata = {" << std::endl;
343 for (u16 z = 0; z != size.Z; z++)
344 for (u16 y = 0; y != size.Y; y++) {
348 << ", y=" << y << std::endl;
351 for (u16 x = 0; x != size.X; x++, i++) {
353 << "name=\"" << getNodeName(schemdata[i].getContent())
354 << "\", param1=" << (u16)schemdata[i].param1
355 << ", param2=" << (u16)schemdata[i].param2
356 << "}," << std::endl;
360 ss << "\t}," << std::endl;
363 ss << "}" << std::endl;
369 bool Schematic::loadSchematicFromFile(const std::string &filename,
370 INodeDefManager *ndef, StringMap *replace_names,
371 NodeResolveMethod resolve_method)
373 std::ifstream is(filename.c_str(), std::ios_base::binary);
375 errorstream << "Schematic::loadSchematicFile: unable to open file '"
376 << filename << "'" << std::endl;
380 size_t origsize = m_nodenames.size();
381 if (!deserializeFromMts(&is, &m_nodenames))
385 for (size_t i = origsize; i != m_nodenames.size(); i++) {
386 std::string &name = m_nodenames[i];
387 StringMap::iterator it = replace_names->find(name);
388 if (it != replace_names->end())
393 m_nnlistsizes.push_back(m_nodenames.size() - origsize);
395 ndef->pendNodeResolve(this, resolve_method);
401 bool Schematic::saveSchematicToFile(const std::string &filename)
403 std::ostringstream os(std::ios_base::binary);
405 return fs::safeWriteToFile(filename, os.str());
409 bool Schematic::getSchematicFromMap(Map *map, v3s16 p1, v3s16 p2)
411 MMVManip *vm = new MMVManip(map);
413 v3s16 bp1 = getNodeBlockPos(p1);
414 v3s16 bp2 = getNodeBlockPos(p2);
415 vm->initialEmerge(bp1, bp2);
419 slice_probs = new u8[size.Y];
420 for (s16 y = 0; y != size.Y; y++)
421 slice_probs[y] = MTSCHEM_PROB_ALWAYS;
423 schemdata = new MapNode[size.X * size.Y * size.Z];
426 for (s16 z = p1.Z; z <= p2.Z; z++)
427 for (s16 y = p1.Y; y <= p2.Y; y++) {
428 u32 vi = vm->m_area.index(p1.X, y, z);
429 for (s16 x = p1.X; x <= p2.X; x++, i++, vi++) {
430 schemdata[i] = vm->m_data[vi];
431 schemdata[i].param1 = MTSCHEM_PROB_ALWAYS;
440 void Schematic::applyProbabilities(v3s16 p0,
441 std::vector<std::pair<v3s16, u8> > *plist,
442 std::vector<std::pair<s16, u8> > *splist)
444 for (size_t i = 0; i != plist->size(); i++) {
445 v3s16 p = (*plist)[i].first - p0;
446 int index = p.Z * (size.Y * size.X) + p.Y * size.X + p.X;
447 if (index < size.Z * size.Y * size.X) {
448 u8 prob = (*plist)[i].second;
449 schemdata[index].param1 = prob;
451 // trim unnecessary node names from schematic
452 if (prob == MTSCHEM_PROB_NEVER)
453 schemdata[index].setContent(CONTENT_AIR);
457 for (size_t i = 0; i != splist->size(); i++) {
458 s16 y = (*splist)[i].first - p0.Y;
459 slice_probs[y] = (*splist)[i].second;
464 void build_nnlist_and_update_ids(MapNode *nodes, u32 nodecount,
465 std::vector<content_t> *usednodes)
467 std::map<content_t, content_t> nodeidmap;
468 content_t numids = 0;
470 for (u32 i = 0; i != nodecount; i++) {
472 content_t c = nodes[i].getContent();
474 std::map<content_t, content_t>::const_iterator it = nodeidmap.find(c);
475 if (it == nodeidmap.end()) {
479 usednodes->push_back(c);
480 nodeidmap.insert(std::make_pair(c, id));
484 nodes[i].setContent(id);