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(NodeResolveInfo *nri)
61 m_ndef->getIdsFromResolveInfo(nri, c_nodes);
65 void Schematic::updateContentIds()
67 if (flags & SCHEM_CIDS_UPDATED)
70 flags |= SCHEM_CIDS_UPDATED;
72 size_t bufsize = size.X * size.Y * size.Z;
73 for (size_t i = 0; i != bufsize; i++)
74 schemdata[i].setContent(c_nodes[schemdata[i].getContent()]);
78 void Schematic::blitToVManip(v3s16 p, MMVManip *vm, Rotation rot,
79 bool force_placement, INodeDefManager *ndef)
83 int zstride = size.X * size.Y;
91 int i_start, i_step_x, i_step_z;
100 i_start = zstride * (sz - 1) + sx - 1;
105 i_start = zstride * (sz - 1);
117 for (s16 y = 0; y != sy; y++) {
118 if (slice_probs[y] != MTSCHEM_PROB_ALWAYS &&
119 myrand_range(1, 255) > slice_probs[y])
122 for (s16 z = 0; z != sz; z++) {
123 u32 i = z * i_step_z + y * ystride + i_start;
124 for (s16 x = 0; x != sx; x++, i += i_step_x) {
125 u32 vi = vm->m_area.index(p.X + x, y_map, p.Z + z);
126 if (!vm->m_area.contains(vi))
129 if (schemdata[i].getContent() == CONTENT_IGNORE)
132 if (schemdata[i].param1 == MTSCHEM_PROB_NEVER)
135 if (!force_placement) {
136 content_t c = vm->m_data[vi].getContent();
137 if (c != CONTENT_AIR && c != CONTENT_IGNORE)
141 if (schemdata[i].param1 != MTSCHEM_PROB_ALWAYS &&
142 myrand_range(1, 255) > schemdata[i].param1)
145 vm->m_data[vi] = schemdata[i];
146 vm->m_data[vi].param1 = 0;
149 vm->m_data[vi].rotateAlongYAxis(ndef, rot);
157 void Schematic::placeStructure(Map *map, v3s16 p, u32 flags, Rotation rot,
158 bool force_placement, INodeDefManager *ndef)
160 assert(schemdata != NULL); // Pre-condition
161 MMVManip *vm = new MMVManip(map);
163 if (rot == ROTATE_RAND)
164 rot = (Rotation)myrand_range(ROTATE_0, ROTATE_270);
166 v3s16 s = (rot == ROTATE_90 || rot == ROTATE_270) ?
167 v3s16(size.Z, size.Y, size.X) : size;
169 if (flags & DECO_PLACE_CENTER_X)
170 p.X -= (s.X + 1) / 2;
171 if (flags & DECO_PLACE_CENTER_Y)
172 p.Y -= (s.Y + 1) / 2;
173 if (flags & DECO_PLACE_CENTER_Z)
174 p.Z -= (s.Z + 1) / 2;
176 v3s16 bp1 = getNodeBlockPos(p);
177 v3s16 bp2 = getNodeBlockPos(p + s - v3s16(1,1,1));
178 vm->initialEmerge(bp1, bp2);
180 blitToVManip(p, vm, rot, force_placement, ndef);
182 std::map<v3s16, MapBlock *> lighting_modified_blocks;
183 std::map<v3s16, MapBlock *> modified_blocks;
184 vm->blitBackAll(&modified_blocks);
186 // TODO: Optimize this by using Mapgen::calcLighting() instead
187 lighting_modified_blocks.insert(modified_blocks.begin(), modified_blocks.end());
188 map->updateLighting(lighting_modified_blocks, modified_blocks);
191 event.type = MEET_OTHER;
192 for (std::map<v3s16, MapBlock *>::iterator
193 it = modified_blocks.begin();
194 it != modified_blocks.end(); ++it)
195 event.modified_blocks.insert(it->first);
197 map->dispatchEvent(&event);
201 bool Schematic::loadSchematicFromFile(const char *filename, INodeDefManager *ndef,
202 StringMap *replace_names)
204 content_t cignore = CONTENT_IGNORE;
205 bool have_cignore = false;
207 std::ifstream is(filename, std::ios_base::binary);
209 errorstream << "loadSchematicFile: unable to open file '"
210 << filename << "'" << std::endl;
214 u32 signature = readU32(is);
215 if (signature != MTSCHEM_FILE_SIGNATURE) {
216 errorstream << "loadSchematicFile: invalid schematic "
221 u16 version = readU16(is);
222 if (version > MTSCHEM_FILE_VER_HIGHEST_READ) {
223 errorstream << "loadSchematicFile: unsupported schematic "
224 "file version" << std::endl;
228 size = readV3S16(is);
230 delete []slice_probs;
231 slice_probs = new u8[size.Y];
232 for (int y = 0; y != size.Y; y++)
233 slice_probs[y] = (version >= 3) ? readU8(is) : MTSCHEM_PROB_ALWAYS;
235 NodeResolveInfo *nri = new NodeResolveInfo(this);
237 u16 nidmapcount = readU16(is);
238 for (int i = 0; i != nidmapcount; i++) {
239 std::string name = deSerializeString(is);
240 if (name == "ignore") {
246 std::map<std::string, std::string>::iterator it;
247 it = replace_names->find(name);
248 if (it != replace_names->end())
251 nri->nodenames.push_back(name);
254 nri->nodelistinfo.push_back(NodeListInfo(nidmapcount, CONTENT_AIR));
255 ndef->pendNodeResolve(nri);
257 size_t nodecount = size.X * size.Y * size.Z;
260 schemdata = new MapNode[nodecount];
262 MapNode::deSerializeBulk(is, SER_FMT_VER_HIGHEST_READ, schemdata,
263 nodecount, 2, 2, true);
265 if (version == 1) { // fix up the probability values
266 for (size_t i = 0; i != nodecount; i++) {
267 if (schemdata[i].param1 == 0)
268 schemdata[i].param1 = MTSCHEM_PROB_ALWAYS;
269 if (have_cignore && schemdata[i].getContent() == cignore)
270 schemdata[i].param1 = MTSCHEM_PROB_NEVER;
279 Minetest Schematic File Format
281 All values are stored in big-endian byte order.
282 [u32] signature: 'MTSM'
288 [u8] slice probability value
289 [Name-ID table] Name ID Mapping Table
291 For each name-id mapping:
295 For each node in schematic: (for z, y, x)
297 For each node in schematic:
298 [u8] probability of occurance (param1)
299 For each node in schematic:
305 2 - Fixed messy never/always place; 0 probability is now never, 0xFF is always
306 3 - Added y-slice probabilities; this allows for variable height structures
308 void Schematic::saveSchematicToFile(const char *filename, INodeDefManager *ndef)
310 std::ostringstream ss(std::ios_base::binary);
312 writeU32(ss, MTSCHEM_FILE_SIGNATURE); // signature
313 writeU16(ss, MTSCHEM_FILE_VER_HIGHEST_WRITE); // version
314 writeV3S16(ss, size); // schematic size
316 for (int y = 0; y != size.Y; y++) // Y slice probabilities
317 writeU8(ss, slice_probs[y]);
319 std::vector<content_t> usednodes;
320 int nodecount = size.X * size.Y * size.Z;
321 build_nnlist_and_update_ids(schemdata, nodecount, &usednodes);
323 u16 numids = usednodes.size();
324 writeU16(ss, numids); // name count
325 for (int i = 0; i != numids; i++)
326 ss << serializeString(ndef->get(usednodes[i]).name); // node names
328 // compressed bulk node data
329 MapNode::serializeBulk(ss, SER_FMT_VER_HIGHEST_WRITE, schemdata,
330 nodecount, 2, 2, true);
332 fs::safeWriteToFile(filename, ss.str());
336 void build_nnlist_and_update_ids(MapNode *nodes, u32 nodecount,
337 std::vector<content_t> *usednodes)
339 std::map<content_t, content_t> nodeidmap;
340 content_t numids = 0;
342 for (u32 i = 0; i != nodecount; i++) {
344 content_t c = nodes[i].getContent();
346 std::map<content_t, content_t>::const_iterator it = nodeidmap.find(c);
347 if (it == nodeidmap.end()) {
351 usednodes->push_back(c);
352 nodeidmap.insert(std::make_pair(c, id));
356 nodes[i].setContent(id);
361 bool Schematic::getSchematicFromMap(Map *map, v3s16 p1, v3s16 p2)
363 MMVManip *vm = new MMVManip(map);
365 v3s16 bp1 = getNodeBlockPos(p1);
366 v3s16 bp2 = getNodeBlockPos(p2);
367 vm->initialEmerge(bp1, bp2);
371 slice_probs = new u8[size.Y];
372 for (s16 y = 0; y != size.Y; y++)
373 slice_probs[y] = MTSCHEM_PROB_ALWAYS;
375 schemdata = new MapNode[size.X * size.Y * size.Z];
378 for (s16 z = p1.Z; z <= p2.Z; z++)
379 for (s16 y = p1.Y; y <= p2.Y; y++) {
380 u32 vi = vm->m_area.index(p1.X, y, z);
381 for (s16 x = p1.X; x <= p2.X; x++, i++, vi++) {
382 schemdata[i] = vm->m_data[vi];
383 schemdata[i].param1 = MTSCHEM_PROB_ALWAYS;
392 void Schematic::applyProbabilities(v3s16 p0,
393 std::vector<std::pair<v3s16, u8> > *plist,
394 std::vector<std::pair<s16, u8> > *splist)
396 for (size_t i = 0; i != plist->size(); i++) {
397 v3s16 p = (*plist)[i].first - p0;
398 int index = p.Z * (size.Y * size.X) + p.Y * size.X + p.X;
399 if (index < size.Z * size.Y * size.X) {
400 u8 prob = (*plist)[i].second;
401 schemdata[index].param1 = prob;
403 // trim unnecessary node names from schematic
404 if (prob == MTSCHEM_PROB_NEVER)
405 schemdata[index].setContent(CONTENT_AIR);
409 for (size_t i = 0; i != splist->size(); i++) {
410 s16 y = (*splist)[i].first - p0.Y;
411 slice_probs[y] = (*splist)[i].second;