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 FlagDesc flagdesc_deco_schematic[] = {
32 {"place_center_x", DECO_PLACE_CENTER_X},
33 {"place_center_y", DECO_PLACE_CENTER_Y},
34 {"place_center_z", DECO_PLACE_CENTER_Z},
38 ///////////////////////////////////////////////////////////////////////////////
41 DecoSchematic::DecoSchematic()
46 size = v3s16(0, 0, 0);
50 DecoSchematic::~DecoSchematic()
57 void DecoSchematic::updateContentIds()
59 if (flags & DECO_SCHEM_CIDS_UPDATED)
62 flags |= DECO_SCHEM_CIDS_UPDATED;
64 for (int i = 0; i != size.X * size.Y * size.Z; i++)
65 schematic[i].setContent(c_nodes[schematic[i].getContent()]);
69 void DecoSchematic::generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, v3s16 p)
71 ManualMapVoxelManipulator *vm = mg->vm;
73 if (flags & DECO_PLACE_CENTER_X)
74 p.X -= (size.X + 1) / 2;
75 if (flags & DECO_PLACE_CENTER_Y)
76 p.Y -= (size.Y + 1) / 2;
77 if (flags & DECO_PLACE_CENTER_Z)
78 p.Z -= (size.Z + 1) / 2;
80 u32 vi = vm->m_area.index(p);
81 content_t c = vm->m_data[vi].getContent();
82 if (!CONTAINS(c_place_on, c))
85 Rotation rot = (rotation == ROTATE_RAND) ?
86 (Rotation)pr->range(ROTATE_0, ROTATE_270) : rotation;
88 blitToVManip(p, vm, rot, false);
92 int DecoSchematic::getHeight() {
97 std::string DecoSchematic::getName() {
102 void DecoSchematic::blitToVManip(v3s16 p, ManualMapVoxelManipulator *vm,
103 Rotation rot, bool force_placement)
106 int ystride = size.X;
107 int zstride = size.X * size.Y;
115 int i_start, i_step_x, i_step_z;
124 i_start = zstride * (sz - 1) + sx - 1;
129 i_start = zstride * (sz - 1);
141 for (s16 y = 0; y != sy; y++) {
142 if (slice_probs[y] != MTSCHEM_PROB_ALWAYS &&
143 myrand_range(1, 255) > slice_probs[y])
146 for (s16 z = 0; z != sz; z++) {
147 u32 i = z * i_step_z + y * ystride + i_start;
148 for (s16 x = 0; x != sx; x++, i += i_step_x) {
149 u32 vi = vm->m_area.index(p.X + x, y_map, p.Z + z);
150 if (!vm->m_area.contains(vi))
153 if (schematic[i].getContent() == CONTENT_IGNORE)
156 if (schematic[i].param1 == MTSCHEM_PROB_NEVER)
159 if (!force_placement) {
160 content_t c = vm->m_data[vi].getContent();
161 if (c != CONTENT_AIR && c != CONTENT_IGNORE)
165 if (schematic[i].param1 != MTSCHEM_PROB_ALWAYS &&
166 myrand_range(1, 255) > schematic[i].param1)
169 vm->m_data[vi] = schematic[i];
170 vm->m_data[vi].param1 = 0;
173 vm->m_data[vi].rotateAlongYAxis(ndef, rot);
181 void DecoSchematic::placeStructure(Map *map, v3s16 p, bool force_placement)
183 assert(schematic != NULL);
184 ManualMapVoxelManipulator *vm = new ManualMapVoxelManipulator(map);
186 Rotation rot = (rotation == ROTATE_RAND) ?
187 (Rotation)myrand_range(ROTATE_0, ROTATE_270) : rotation;
189 v3s16 s = (rot == ROTATE_90 || rot == ROTATE_270) ?
190 v3s16(size.Z, size.Y, size.X) : size;
192 if (flags & DECO_PLACE_CENTER_X)
193 p.X -= (s.X + 1) / 2;
194 if (flags & DECO_PLACE_CENTER_Y)
195 p.Y -= (s.Y + 1) / 2;
196 if (flags & DECO_PLACE_CENTER_Z)
197 p.Z -= (s.Z + 1) / 2;
199 v3s16 bp1 = getNodeBlockPos(p);
200 v3s16 bp2 = getNodeBlockPos(p + s - v3s16(1,1,1));
201 vm->initialEmerge(bp1, bp2);
203 blitToVManip(p, vm, rot, force_placement);
205 std::map<v3s16, MapBlock *> lighting_modified_blocks;
206 std::map<v3s16, MapBlock *> modified_blocks;
207 vm->blitBackAll(&modified_blocks);
209 // TODO: Optimize this by using Mapgen::calcLighting() instead
210 lighting_modified_blocks.insert(modified_blocks.begin(), modified_blocks.end());
211 map->updateLighting(lighting_modified_blocks, modified_blocks);
214 event.type = MEET_OTHER;
215 for (std::map<v3s16, MapBlock *>::iterator
216 it = modified_blocks.begin();
217 it != modified_blocks.end(); ++it)
218 event.modified_blocks.insert(it->first);
220 map->dispatchEvent(&event);
224 bool DecoSchematic::loadSchematicFile(NodeResolver *resolver,
225 std::map<std::string, std::string> &replace_names)
227 content_t cignore = CONTENT_IGNORE;
228 bool have_cignore = false;
230 std::ifstream is(filename.c_str(), std::ios_base::binary);
232 u32 signature = readU32(is);
233 if (signature != MTSCHEM_FILE_SIGNATURE) {
234 errorstream << "loadSchematicFile: invalid schematic "
239 u16 version = readU16(is);
240 if (version > MTSCHEM_FILE_VER_HIGHEST_READ) {
241 errorstream << "loadSchematicFile: unsupported schematic "
242 "file version" << std::endl;
246 size = readV3S16(is);
248 delete []slice_probs;
249 slice_probs = new u8[size.Y];
251 for (int y = 0; y != size.Y; y++)
252 slice_probs[y] = readU8(is);
254 for (int y = 0; y != size.Y; y++)
255 slice_probs[y] = MTSCHEM_PROB_ALWAYS;
258 int nodecount = size.X * size.Y * size.Z;
260 u16 nidmapcount = readU16(is);
262 for (int i = 0; i != nidmapcount; i++) {
263 std::string name = deSerializeString(is);
264 if (name == "ignore") {
270 std::map<std::string, std::string>::iterator it;
272 it = replace_names.find(name);
273 if (it != replace_names.end())
276 resolver->addNodeList(name.c_str(), &c_nodes);
280 schematic = new MapNode[nodecount];
281 MapNode::deSerializeBulk(is, SER_FMT_VER_HIGHEST_READ, schematic,
282 nodecount, 2, 2, true);
284 if (version == 1) { // fix up the probability values
285 for (int i = 0; i != nodecount; i++) {
286 if (schematic[i].param1 == 0)
287 schematic[i].param1 = MTSCHEM_PROB_ALWAYS;
288 if (have_cignore && schematic[i].getContent() == cignore)
289 schematic[i].param1 = MTSCHEM_PROB_NEVER;
298 Minetest Schematic File Format
300 All values are stored in big-endian byte order.
301 [u32] signature: 'MTSM'
307 [u8] slice probability value
308 [Name-ID table] Name ID Mapping Table
310 For each name-id mapping:
314 For each node in schematic: (for z, y, x)
316 For each node in schematic:
317 [u8] probability of occurance (param1)
318 For each node in schematic:
324 2 - Fixed messy never/always place; 0 probability is now never, 0xFF is always
325 3 - Added y-slice probabilities; this allows for variable height structures
327 void DecoSchematic::saveSchematicFile(INodeDefManager *ndef)
329 std::ostringstream ss(std::ios_base::binary);
331 writeU32(ss, MTSCHEM_FILE_SIGNATURE); // signature
332 writeU16(ss, MTSCHEM_FILE_VER_HIGHEST_WRITE); // version
333 writeV3S16(ss, size); // schematic size
335 for (int y = 0; y != size.Y; y++) // Y slice probabilities
336 writeU8(ss, slice_probs[y]);
338 std::vector<content_t> usednodes;
339 int nodecount = size.X * size.Y * size.Z;
340 build_nnlist_and_update_ids(schematic, nodecount, &usednodes);
342 u16 numids = usednodes.size();
343 writeU16(ss, numids); // name count
344 for (int i = 0; i != numids; i++)
345 ss << serializeString(ndef->get(usednodes[i]).name); // node names
347 // compressed bulk node data
348 MapNode::serializeBulk(ss, SER_FMT_VER_HIGHEST_WRITE, schematic,
349 nodecount, 2, 2, true);
351 fs::safeWriteToFile(filename, ss.str());
355 void build_nnlist_and_update_ids(MapNode *nodes, u32 nodecount,
356 std::vector<content_t> *usednodes)
358 std::map<content_t, content_t> nodeidmap;
359 content_t numids = 0;
361 for (u32 i = 0; i != nodecount; i++) {
363 content_t c = nodes[i].getContent();
365 std::map<content_t, content_t>::const_iterator it = nodeidmap.find(c);
366 if (it == nodeidmap.end()) {
370 usednodes->push_back(c);
371 nodeidmap.insert(std::make_pair(c, id));
375 nodes[i].setContent(id);
380 bool DecoSchematic::getSchematicFromMap(Map *map, v3s16 p1, v3s16 p2)
382 ManualMapVoxelManipulator *vm = new ManualMapVoxelManipulator(map);
384 v3s16 bp1 = getNodeBlockPos(p1);
385 v3s16 bp2 = getNodeBlockPos(p2);
386 vm->initialEmerge(bp1, bp2);
390 slice_probs = new u8[size.Y];
391 for (s16 y = 0; y != size.Y; y++)
392 slice_probs[y] = MTSCHEM_PROB_ALWAYS;
394 schematic = new MapNode[size.X * size.Y * size.Z];
397 for (s16 z = p1.Z; z <= p2.Z; z++)
398 for (s16 y = p1.Y; y <= p2.Y; y++) {
399 u32 vi = vm->m_area.index(p1.X, y, z);
400 for (s16 x = p1.X; x <= p2.X; x++, i++, vi++) {
401 schematic[i] = vm->m_data[vi];
402 schematic[i].param1 = MTSCHEM_PROB_ALWAYS;
411 void DecoSchematic::applyProbabilities(v3s16 p0,
412 std::vector<std::pair<v3s16, u8> > *plist,
413 std::vector<std::pair<s16, u8> > *splist)
415 for (size_t i = 0; i != plist->size(); i++) {
416 v3s16 p = (*plist)[i].first - p0;
417 int index = p.Z * (size.Y * size.X) + p.Y * size.X + p.X;
418 if (index < size.Z * size.Y * size.X) {
419 u8 prob = (*plist)[i].second;
420 schematic[index].param1 = prob;
422 // trim unnecessary node names from schematic
423 if (prob == MTSCHEM_PROB_NEVER)
424 schematic[index].setContent(CONTENT_AIR);
428 for (size_t i = 0; i != splist->size(); i++) {
429 s16 y = (*splist)[i].first - p0.Y;
430 slice_probs[y] = (*splist)[i].second;