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 const char *SchematicManager::ELEMENT_TITLE = "schematic";
33 ///////////////////////////////////////////////////////////////////////////////
36 SchematicManager::SchematicManager(IGameDef *gamedef) :
37 GenElementManager(gamedef)
42 ///////////////////////////////////////////////////////////////////////////////
45 Schematic::Schematic()
50 size = v3s16(0, 0, 0);
54 Schematic::~Schematic()
61 void Schematic::resolveNodeNames(NodeResolveInfo *nri)
63 m_ndef->getIdsFromResolveInfo(nri, c_nodes);
67 void Schematic::updateContentIds()
69 if (flags & SCHEM_CIDS_UPDATED)
72 flags |= SCHEM_CIDS_UPDATED;
74 size_t bufsize = size.X * size.Y * size.Z;
75 for (size_t i = 0; i != bufsize; i++)
76 schemdata[i].setContent(c_nodes[schemdata[i].getContent()]);
80 void Schematic::blitToVManip(v3s16 p, ManualMapVoxelManipulator *vm,
81 Rotation rot, bool force_placement, INodeDefManager *ndef)
85 int zstride = size.X * size.Y;
93 int i_start, i_step_x, i_step_z;
102 i_start = zstride * (sz - 1) + sx - 1;
107 i_start = zstride * (sz - 1);
119 for (s16 y = 0; y != sy; y++) {
120 if (slice_probs[y] != MTSCHEM_PROB_ALWAYS &&
121 myrand_range(1, 255) > slice_probs[y])
124 for (s16 z = 0; z != sz; z++) {
125 u32 i = z * i_step_z + y * ystride + i_start;
126 for (s16 x = 0; x != sx; x++, i += i_step_x) {
127 u32 vi = vm->m_area.index(p.X + x, y_map, p.Z + z);
128 if (!vm->m_area.contains(vi))
131 if (schemdata[i].getContent() == CONTENT_IGNORE)
134 if (schemdata[i].param1 == MTSCHEM_PROB_NEVER)
137 if (!force_placement) {
138 content_t c = vm->m_data[vi].getContent();
139 if (c != CONTENT_AIR && c != CONTENT_IGNORE)
143 if (schemdata[i].param1 != MTSCHEM_PROB_ALWAYS &&
144 myrand_range(1, 255) > schemdata[i].param1)
147 vm->m_data[vi] = schemdata[i];
148 vm->m_data[vi].param1 = 0;
151 vm->m_data[vi].rotateAlongYAxis(ndef, rot);
159 void Schematic::placeStructure(Map *map, v3s16 p, u32 flags,
160 Rotation rot, bool force_placement, INodeDefManager *ndef)
162 assert(schemdata != NULL);
163 ManualMapVoxelManipulator *vm = new ManualMapVoxelManipulator(map);
165 if (rot == ROTATE_RAND)
166 rot = (Rotation)myrand_range(ROTATE_0, ROTATE_270);
168 v3s16 s = (rot == ROTATE_90 || rot == ROTATE_270) ?
169 v3s16(size.Z, size.Y, size.X) : size;
171 if (flags & DECO_PLACE_CENTER_X)
172 p.X -= (s.X + 1) / 2;
173 if (flags & DECO_PLACE_CENTER_Y)
174 p.Y -= (s.Y + 1) / 2;
175 if (flags & DECO_PLACE_CENTER_Z)
176 p.Z -= (s.Z + 1) / 2;
178 v3s16 bp1 = getNodeBlockPos(p);
179 v3s16 bp2 = getNodeBlockPos(p + s - v3s16(1,1,1));
180 vm->initialEmerge(bp1, bp2);
182 blitToVManip(p, vm, rot, force_placement, ndef);
184 std::map<v3s16, MapBlock *> lighting_modified_blocks;
185 std::map<v3s16, MapBlock *> modified_blocks;
186 vm->blitBackAll(&modified_blocks);
188 // TODO: Optimize this by using Mapgen::calcLighting() instead
189 lighting_modified_blocks.insert(modified_blocks.begin(), modified_blocks.end());
190 map->updateLighting(lighting_modified_blocks, modified_blocks);
193 event.type = MEET_OTHER;
194 for (std::map<v3s16, MapBlock *>::iterator
195 it = modified_blocks.begin();
196 it != modified_blocks.end(); ++it)
197 event.modified_blocks.insert(it->first);
199 map->dispatchEvent(&event);
203 bool Schematic::loadSchematicFromFile(const char *filename,
204 INodeDefManager *ndef, std::map<std::string, std::string> &replace_names)
206 content_t cignore = CONTENT_IGNORE;
207 bool have_cignore = false;
209 std::ifstream is(filename, std::ios_base::binary);
211 u32 signature = readU32(is);
212 if (signature != MTSCHEM_FILE_SIGNATURE) {
213 errorstream << "loadSchematicFile: invalid schematic "
218 u16 version = readU16(is);
219 if (version > MTSCHEM_FILE_VER_HIGHEST_READ) {
220 errorstream << "loadSchematicFile: unsupported schematic "
221 "file version" << std::endl;
225 size = readV3S16(is);
227 delete []slice_probs;
228 slice_probs = new u8[size.Y];
229 for (int y = 0; y != size.Y; y++)
230 slice_probs[y] = (version >= 3) ? readU8(is) : MTSCHEM_PROB_ALWAYS;
232 NodeResolveInfo *nri = new NodeResolveInfo(this);
234 u16 nidmapcount = readU16(is);
235 for (int i = 0; i != nidmapcount; i++) {
236 std::string name = deSerializeString(is);
237 if (name == "ignore") {
243 std::map<std::string, std::string>::iterator it;
244 it = replace_names.find(name);
245 if (it != replace_names.end())
248 nri->nodenames.push_back(name);
251 nri->nodelistinfo.push_back(NodeListInfo(nidmapcount, CONTENT_AIR));
252 ndef->pendNodeResolve(nri);
254 size_t nodecount = size.X * size.Y * size.Z;
257 schemdata = new MapNode[nodecount];
259 MapNode::deSerializeBulk(is, SER_FMT_VER_HIGHEST_READ, schemdata,
260 nodecount, 2, 2, true);
262 if (version == 1) { // fix up the probability values
263 for (size_t i = 0; i != nodecount; i++) {
264 if (schemdata[i].param1 == 0)
265 schemdata[i].param1 = MTSCHEM_PROB_ALWAYS;
266 if (have_cignore && schemdata[i].getContent() == cignore)
267 schemdata[i].param1 = MTSCHEM_PROB_NEVER;
276 Minetest Schematic File Format
278 All values are stored in big-endian byte order.
279 [u32] signature: 'MTSM'
285 [u8] slice probability value
286 [Name-ID table] Name ID Mapping Table
288 For each name-id mapping:
292 For each node in schematic: (for z, y, x)
294 For each node in schematic:
295 [u8] probability of occurance (param1)
296 For each node in schematic:
302 2 - Fixed messy never/always place; 0 probability is now never, 0xFF is always
303 3 - Added y-slice probabilities; this allows for variable height structures
305 void Schematic::saveSchematicToFile(const char *filename, INodeDefManager *ndef)
307 std::ostringstream ss(std::ios_base::binary);
309 writeU32(ss, MTSCHEM_FILE_SIGNATURE); // signature
310 writeU16(ss, MTSCHEM_FILE_VER_HIGHEST_WRITE); // version
311 writeV3S16(ss, size); // schematic size
313 for (int y = 0; y != size.Y; y++) // Y slice probabilities
314 writeU8(ss, slice_probs[y]);
316 std::vector<content_t> usednodes;
317 int nodecount = size.X * size.Y * size.Z;
318 build_nnlist_and_update_ids(schemdata, nodecount, &usednodes);
320 u16 numids = usednodes.size();
321 writeU16(ss, numids); // name count
322 for (int i = 0; i != numids; i++)
323 ss << serializeString(ndef->get(usednodes[i]).name); // node names
325 // compressed bulk node data
326 MapNode::serializeBulk(ss, SER_FMT_VER_HIGHEST_WRITE, schemdata,
327 nodecount, 2, 2, true);
329 fs::safeWriteToFile(filename, ss.str());
333 void build_nnlist_and_update_ids(MapNode *nodes, u32 nodecount,
334 std::vector<content_t> *usednodes)
336 std::map<content_t, content_t> nodeidmap;
337 content_t numids = 0;
339 for (u32 i = 0; i != nodecount; i++) {
341 content_t c = nodes[i].getContent();
343 std::map<content_t, content_t>::const_iterator it = nodeidmap.find(c);
344 if (it == nodeidmap.end()) {
348 usednodes->push_back(c);
349 nodeidmap.insert(std::make_pair(c, id));
353 nodes[i].setContent(id);
358 bool Schematic::getSchematicFromMap(Map *map, v3s16 p1, v3s16 p2)
360 ManualMapVoxelManipulator *vm = new ManualMapVoxelManipulator(map);
362 v3s16 bp1 = getNodeBlockPos(p1);
363 v3s16 bp2 = getNodeBlockPos(p2);
364 vm->initialEmerge(bp1, bp2);
368 slice_probs = new u8[size.Y];
369 for (s16 y = 0; y != size.Y; y++)
370 slice_probs[y] = MTSCHEM_PROB_ALWAYS;
372 schemdata = new MapNode[size.X * size.Y * size.Z];
375 for (s16 z = p1.Z; z <= p2.Z; z++)
376 for (s16 y = p1.Y; y <= p2.Y; y++) {
377 u32 vi = vm->m_area.index(p1.X, y, z);
378 for (s16 x = p1.X; x <= p2.X; x++, i++, vi++) {
379 schemdata[i] = vm->m_data[vi];
380 schemdata[i].param1 = MTSCHEM_PROB_ALWAYS;
389 void Schematic::applyProbabilities(v3s16 p0,
390 std::vector<std::pair<v3s16, u8> > *plist,
391 std::vector<std::pair<s16, u8> > *splist)
393 for (size_t i = 0; i != plist->size(); i++) {
394 v3s16 p = (*plist)[i].first - p0;
395 int index = p.Z * (size.Y * size.X) + p.Y * size.X + p.X;
396 if (index < size.Z * size.Y * size.X) {
397 u8 prob = (*plist)[i].second;
398 schemdata[index].param1 = prob;
400 // trim unnecessary node names from schematic
401 if (prob == MTSCHEM_PROB_NEVER)
402 schemdata[index].setContent(CONTENT_AIR);
406 for (size_t i = 0; i != splist->size(); i++) {
407 s16 y = (*splist)[i].first - p0.Y;
408 slice_probs[y] = (*splist)[i].second;