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.
21 #include "mg_schematic.h"
26 #include "util/numeric.h"
27 #include "util/serialize.h"
28 #include "serialization.h"
31 ///////////////////////////////////////////////////////////////////////////////
34 SchematicManager::SchematicManager(IGameDef *gamedef) :
35 ObjDefManager(gamedef, OBJDEF_SCHEMATIC)
40 ///////////////////////////////////////////////////////////////////////////////
43 Schematic::Schematic()
48 size = v3s16(0, 0, 0);
52 Schematic::~Schematic()
59 void Schematic::resolveNodeNames()
61 getIdsFromNrBacklog(&c_nodes, true, CONTENT_AIR);
63 size_t bufsize = size.X * size.Y * size.Z;
64 for (size_t i = 0; i != bufsize; i++) {
65 content_t c_original = schemdata[i].getContent();
66 content_t c_new = c_nodes[c_original];
67 schemdata[i].setContent(c_new);
72 void Schematic::blitToVManip(v3s16 p, MMVManip *vm, Rotation rot,
73 bool force_placement, INodeDefManager *ndef)
77 int zstride = size.X * size.Y;
83 int i_start, i_step_x, i_step_z;
92 i_start = zstride * (sz - 1) + sx - 1;
97 i_start = zstride * (sz - 1);
109 for (s16 y = 0; y != sy; y++) {
110 if (slice_probs[y] != MTSCHEM_PROB_ALWAYS &&
111 myrand_range(1, 255) > slice_probs[y])
114 for (s16 z = 0; z != sz; z++) {
115 u32 i = z * i_step_z + y * ystride + i_start;
116 for (s16 x = 0; x != sx; x++, i += i_step_x) {
117 u32 vi = vm->m_area.index(p.X + x, y_map, p.Z + z);
118 if (!vm->m_area.contains(vi))
121 if (schemdata[i].getContent() == CONTENT_IGNORE)
124 if (schemdata[i].param1 == MTSCHEM_PROB_NEVER)
127 if (!force_placement) {
128 content_t c = vm->m_data[vi].getContent();
129 if (c != CONTENT_AIR && c != CONTENT_IGNORE)
133 if (schemdata[i].param1 != MTSCHEM_PROB_ALWAYS &&
134 myrand_range(1, 255) > schemdata[i].param1)
137 vm->m_data[vi] = schemdata[i];
138 vm->m_data[vi].param1 = 0;
141 vm->m_data[vi].rotateAlongYAxis(ndef, rot);
149 void Schematic::placeStructure(Map *map, v3s16 p, u32 flags, Rotation rot,
150 bool force_placement, INodeDefManager *ndef)
152 assert(schemdata != NULL); // Pre-condition
153 MMVManip *vm = new MMVManip(map);
155 if (rot == ROTATE_RAND)
156 rot = (Rotation)myrand_range(ROTATE_0, ROTATE_270);
158 v3s16 s = (rot == ROTATE_90 || rot == ROTATE_270) ?
159 v3s16(size.Z, size.Y, size.X) : size;
161 if (flags & DECO_PLACE_CENTER_X)
162 p.X -= (s.X + 1) / 2;
163 if (flags & DECO_PLACE_CENTER_Y)
164 p.Y -= (s.Y + 1) / 2;
165 if (flags & DECO_PLACE_CENTER_Z)
166 p.Z -= (s.Z + 1) / 2;
168 v3s16 bp1 = getNodeBlockPos(p);
169 v3s16 bp2 = getNodeBlockPos(p + s - v3s16(1,1,1));
170 vm->initialEmerge(bp1, bp2);
172 blitToVManip(p, vm, rot, force_placement, ndef);
174 std::map<v3s16, MapBlock *> lighting_modified_blocks;
175 std::map<v3s16, MapBlock *> modified_blocks;
176 vm->blitBackAll(&modified_blocks);
178 // TODO: Optimize this by using Mapgen::calcLighting() instead
179 lighting_modified_blocks.insert(modified_blocks.begin(), modified_blocks.end());
180 map->updateLighting(lighting_modified_blocks, modified_blocks);
183 event.type = MEET_OTHER;
184 for (std::map<v3s16, MapBlock *>::iterator
185 it = modified_blocks.begin();
186 it != modified_blocks.end(); ++it)
187 event.modified_blocks.insert(it->first);
189 map->dispatchEvent(&event);
193 bool Schematic::deserializeFromMts(std::istream *is, std::vector<std::string> *names)
195 std::istream &ss = *is;
196 content_t cignore = CONTENT_IGNORE;
197 bool have_cignore = false;
199 u32 signature = readU32(ss);
200 if (signature != MTSCHEM_FILE_SIGNATURE) {
201 errorstream << "Schematic::deserializeFromMts: invalid schematic "
206 u16 version = readU16(ss);
207 if (version > MTSCHEM_FILE_VER_HIGHEST_READ) {
208 errorstream << "Schematic::deserializeFromMts: unsupported schematic "
209 "file version" << std::endl;
213 size = readV3S16(ss);
215 delete []slice_probs;
216 slice_probs = new u8[size.Y];
217 for (int y = 0; y != size.Y; y++)
218 slice_probs[y] = (version >= 3) ? readU8(ss) : MTSCHEM_PROB_ALWAYS;
220 u16 nidmapcount = readU16(ss);
221 for (int i = 0; i != nidmapcount; i++) {
222 std::string name = deSerializeString(ss);
224 // Instances of "ignore" from ver 1 are converted to air (and instances
225 // are fixed to have MTSCHEM_PROB_NEVER later on).
226 if (name == "ignore") {
232 names->push_back(name);
235 size_t nodecount = size.X * size.Y * size.Z;
238 schemdata = new MapNode[nodecount];
240 MapNode::deSerializeBulk(ss, SER_FMT_VER_HIGHEST_READ, schemdata,
241 nodecount, 2, 2, true);
243 // fix any probability values for nodes that were ignore
245 for (size_t i = 0; i != nodecount; i++) {
246 if (schemdata[i].param1 == 0)
247 schemdata[i].param1 = MTSCHEM_PROB_ALWAYS;
248 if (have_cignore && schemdata[i].getContent() == cignore)
249 schemdata[i].param1 = MTSCHEM_PROB_NEVER;
257 bool Schematic::serializeToMts(std::ostream *os)
259 std::ostream &ss = *os;
261 writeU32(ss, MTSCHEM_FILE_SIGNATURE); // signature
262 writeU16(ss, MTSCHEM_FILE_VER_HIGHEST_WRITE); // version
263 writeV3S16(ss, size); // schematic size
265 for (int y = 0; y != size.Y; y++) // Y slice probabilities
266 writeU8(ss, slice_probs[y]);
268 std::vector<content_t> usednodes;
269 int nodecount = size.X * size.Y * size.Z;
270 build_nnlist_and_update_ids(schemdata, nodecount, &usednodes);
272 u16 numids = usednodes.size();
273 writeU16(ss, numids); // name count
274 for (int i = 0; i != numids; i++)
275 ss << serializeString(getNodeName(usednodes[i])); // node names
277 // compressed bulk node data
278 MapNode::serializeBulk(ss, SER_FMT_VER_HIGHEST_WRITE,
279 schemdata, nodecount, 2, 2, true);
285 bool Schematic::serializeToLua(std::ostream *os, bool use_comments)
287 std::ostream &ss = *os;
291 ss << "schematic = {" << std::endl;
296 << "}," << std::endl;
299 //// Write y-slice probabilities
301 ss << "\tyslice_prob = {" << std::endl;
303 for (u16 y = 0; y != size.Y; y++) {
306 << ", prob=" << (u16)slice_probs[y]
307 << "}," << std::endl;
310 ss << "\t}," << std::endl;
315 ss << "\tdata = {" << std::endl;
318 for (u16 z = 0; z != size.Z; z++)
319 for (u16 y = 0; y != size.Y; y++) {
323 << ", y=" << y << std::endl;
326 for (u16 x = 0; x != size.X; x++, i++) {
328 << "name=\"" << getNodeName(schemdata[i].getContent())
329 << "\", param1=" << (u16)schemdata[i].param1
330 << ", param2=" << (u16)schemdata[i].param2
331 << "}," << std::endl;
335 ss << "\t}," << std::endl;
338 ss << "}" << std::endl;
344 bool Schematic::loadSchematicFromFile(const std::string &filename,
345 INodeDefManager *ndef, StringMap *replace_names,
346 NodeResolveMethod resolve_method)
348 std::ifstream is(filename, std::ios_base::binary);
350 errorstream << "Schematic::loadSchematicFile: unable to open file '"
351 << filename << "'" << std::endl;
355 size_t origsize = m_nodenames.size();
356 if (!deserializeFromMts(&is, &m_nodenames))
360 for (size_t i = origsize; i != m_nodenames.size(); i++) {
361 std::string &name = m_nodenames[i];
362 StringMap::iterator it = replace_names->find(name);
363 if (it != replace_names->end())
368 m_nnlistsizes.push_back(m_nodenames.size() - origsize);
370 ndef->pendNodeResolve(this, resolve_method);
376 bool Schematic::saveSchematicToFile(const std::string &filename)
378 std::ostringstream os(std::ios_base::binary);
380 return fs::safeWriteToFile(filename, os.str());
384 bool Schematic::getSchematicFromMap(Map *map, v3s16 p1, v3s16 p2)
386 MMVManip *vm = new MMVManip(map);
388 v3s16 bp1 = getNodeBlockPos(p1);
389 v3s16 bp2 = getNodeBlockPos(p2);
390 vm->initialEmerge(bp1, bp2);
394 slice_probs = new u8[size.Y];
395 for (s16 y = 0; y != size.Y; y++)
396 slice_probs[y] = MTSCHEM_PROB_ALWAYS;
398 schemdata = new MapNode[size.X * size.Y * size.Z];
401 for (s16 z = p1.Z; z <= p2.Z; z++)
402 for (s16 y = p1.Y; y <= p2.Y; y++) {
403 u32 vi = vm->m_area.index(p1.X, y, z);
404 for (s16 x = p1.X; x <= p2.X; x++, i++, vi++) {
405 schemdata[i] = vm->m_data[vi];
406 schemdata[i].param1 = MTSCHEM_PROB_ALWAYS;
415 void Schematic::applyProbabilities(v3s16 p0,
416 std::vector<std::pair<v3s16, u8> > *plist,
417 std::vector<std::pair<s16, u8> > *splist)
419 for (size_t i = 0; i != plist->size(); i++) {
420 v3s16 p = (*plist)[i].first - p0;
421 int index = p.Z * (size.Y * size.X) + p.Y * size.X + p.X;
422 if (index < size.Z * size.Y * size.X) {
423 u8 prob = (*plist)[i].second;
424 schemdata[index].param1 = prob;
426 // trim unnecessary node names from schematic
427 if (prob == MTSCHEM_PROB_NEVER)
428 schemdata[index].setContent(CONTENT_AIR);
432 for (size_t i = 0; i != splist->size(); i++) {
433 s16 y = (*splist)[i].first - p0.Y;
434 slice_probs[y] = (*splist)[i].second;
439 void build_nnlist_and_update_ids(MapNode *nodes, u32 nodecount,
440 std::vector<content_t> *usednodes)
442 std::map<content_t, content_t> nodeidmap;
443 content_t numids = 0;
445 for (u32 i = 0; i != nodecount; i++) {
447 content_t c = nodes[i].getContent();
449 std::map<content_t, content_t>::const_iterator it = nodeidmap.find(c);
450 if (it == nodeidmap.end()) {
454 usednodes->push_back(c);
455 nodeidmap.insert(std::make_pair(c, id));
459 nodes[i].setContent(id);