3 Copyright (C) 2014-2016 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4 Copyright (C) 2015-2017 paramat
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "mg_decoration.h"
22 #include "mg_schematic.h"
27 #include "util/numeric.h"
31 FlagDesc flagdesc_deco[] = {
32 {"place_center_x", DECO_PLACE_CENTER_X},
33 {"place_center_y", DECO_PLACE_CENTER_Y},
34 {"place_center_z", DECO_PLACE_CENTER_Z},
35 {"force_placement", DECO_FORCE_PLACEMENT},
36 {"liquid_surface", DECO_LIQUID_SURFACE},
41 ///////////////////////////////////////////////////////////////////////////////
44 DecorationManager::DecorationManager(IGameDef *gamedef) :
45 ObjDefManager(gamedef, OBJDEF_DECORATION)
50 size_t DecorationManager::placeAllDecos(Mapgen *mg, u32 blockseed,
51 v3s16 nmin, v3s16 nmax)
55 for (size_t i = 0; i != m_objects.size(); i++) {
56 Decoration *deco = (Decoration *)m_objects[i];
60 nplaced += deco->placeDeco(mg, blockseed, nmin, nmax);
68 ///////////////////////////////////////////////////////////////////////////////
71 Decoration::Decoration()
80 Decoration::~Decoration()
85 void Decoration::resolveNodeNames()
87 getIdsFromNrBacklog(&c_place_on);
88 getIdsFromNrBacklog(&c_spawnby);
92 bool Decoration::canPlaceDecoration(MMVManip *vm, v3s16 p)
94 // Check if the decoration can be placed on this node
95 u32 vi = vm->m_area.index(p);
96 if (!CONTAINS(c_place_on, vm->m_data[vi].getContent()))
99 // Don't continue if there are no spawnby constraints
104 static const v3s16 dirs[16] = {
124 // Check these 16 neighbouring nodes for enough spawnby nodes
125 for (size_t i = 0; i != ARRLEN(dirs); i++) {
126 u32 index = vm->m_area.index(p + dirs[i]);
127 if (!vm->m_area.contains(index))
130 if (CONTAINS(c_spawnby, vm->m_data[index].getContent()))
134 if (nneighs < nspawnby)
141 size_t Decoration::placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
143 PcgRandom ps(blockseed + 53);
144 int carea_size = nmax.X - nmin.X + 1;
146 // Divide area into parts
147 // If chunksize is changed it may no longer be divisable by sidelen
148 if (carea_size % sidelen)
149 sidelen = carea_size;
151 s16 divlen = carea_size / sidelen;
152 int area = sidelen * sidelen;
154 for (s16 z0 = 0; z0 < divlen; z0++)
155 for (s16 x0 = 0; x0 < divlen; x0++) {
156 v2s16 p2d_center( // Center position of part of division
157 nmin.X + sidelen / 2 + sidelen * x0,
158 nmin.Z + sidelen / 2 + sidelen * z0
160 v2s16 p2d_min( // Minimum edge of part of division
161 nmin.X + sidelen * x0,
162 nmin.Z + sidelen * z0
164 v2s16 p2d_max( // Maximum edge of part of division
165 nmin.X + sidelen + sidelen * x0 - 1,
166 nmin.Z + sidelen + sidelen * z0 - 1
169 // Amount of decorations
170 float nval = (flags & DECO_USE_NOISE) ?
171 NoisePerlin2D(&np, p2d_center.X, p2d_center.Y, mapseed) :
174 float deco_count_f = (float)area * nval;
175 if (deco_count_f >= 1.f) {
176 deco_count = deco_count_f;
177 } else if (deco_count_f > 0.f) {
178 // For low density decorations calculate a chance for 1 decoration
179 if (ps.range(1000) <= deco_count_f * 1000.f)
183 for (u32 i = 0; i < deco_count; i++) {
184 s16 x = ps.range(p2d_min.X, p2d_max.X);
185 s16 z = ps.range(p2d_min.Y, p2d_max.Y);
187 int mapindex = carea_size * (z - nmin.Z) + (x - nmin.X);
189 s16 y = -MAX_MAP_GENERATION_LIMIT;
190 if (flags & DECO_LIQUID_SURFACE)
191 y = mg->findLiquidSurface(v2s16(x, z), nmin.Y, nmax.Y);
192 else if (mg->heightmap)
193 y = mg->heightmap[mapindex];
195 y = mg->findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
197 if (y < nmin.Y || y > nmax.Y ||
198 y < y_min || y > y_max)
201 if (y + getHeight() > mg->vm->m_area.MaxEdge.Y) {
204 printf("Decoration at (%d %d %d) cut off\n", x, y, z);
206 MutexAutoLock cutofflock(cutoff_mutex);
207 cutoffs.push_back(CutoffData(x, y, z, height));
212 UNORDERED_SET<u8>::iterator iter;
214 if (!biomes.empty()) {
215 iter = biomes.find(mg->biomemap[mapindex]);
216 if (iter == biomes.end())
222 if (generate(mg->vm, &ps, pos))
223 mg->gennotify.addEvent(GENNOTIFY_DECORATION, pos, index);
232 void Decoration::placeCutoffs(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
234 PcgRandom pr(blockseed + 53);
235 std::vector<CutoffData> handled_cutoffs;
237 // Copy over the cutoffs we're interested in so we don't needlessly hold a lock
239 MutexAutoLock cutofflock(cutoff_mutex);
240 for (std::list<CutoffData>::iterator i = cutoffs.begin();
241 i != cutoffs.end(); ++i) {
242 CutoffData cutoff = *i;
244 s16 height = cutoff.height;
245 if (p.X < nmin.X || p.X > nmax.X ||
246 p.Z < nmin.Z || p.Z > nmax.Z)
248 if (p.Y + height < nmin.Y || p.Y > nmax.Y)
251 handled_cutoffs.push_back(cutoff);
255 // Generate the cutoffs
256 for (size_t i = 0; i != handled_cutoffs.size(); i++) {
257 v3s16 p = handled_cutoffs[i].p;
258 s16 height = handled_cutoffs[i].height;
260 if (p.Y + height > nmax.Y) {
261 //printf("Decoration at (%d %d %d) cut off again!\n", p.X, p.Y, p.Z);
262 cuttoffs.push_back(v3s16(p.X, p.Y, p.Z));
265 generate(mg, &pr, nmax.Y, nmin.Y - p.Y, v3s16(p.X, nmin.Y, p.Z));
268 // Remove cutoffs that were handled from the cutoff list
270 MutexAutoLock cutofflock(cutoff_mutex);
271 for (std::list<CutoffData>::iterator i = cutoffs.begin();
272 i != cutoffs.end(); ++i) {
274 for (size_t j = 0; j != handled_cutoffs.size(); j++) {
275 CutoffData coff = *i;
276 if (coff.p == handled_cutoffs[j].p)
277 i = cutoffs.erase(i);
285 ///////////////////////////////////////////////////////////////////////////////
288 void DecoSimple::resolveNodeNames()
290 Decoration::resolveNodeNames();
291 getIdsFromNrBacklog(&c_decos);
295 size_t DecoSimple::generate(MMVManip *vm, PcgRandom *pr, v3s16 p)
297 // Don't bother if there aren't any decorations to place
298 if (c_decos.size() == 0)
301 if (!canPlaceDecoration(vm, p))
304 content_t c_place = c_decos[pr->range(0, c_decos.size() - 1)];
306 s16 height = (deco_height_max > 0) ?
307 pr->range(deco_height, deco_height_max) : deco_height;
309 bool force_placement = (flags & DECO_FORCE_PLACEMENT);
311 v3s16 em = vm->m_area.getExtent();
312 u32 vi = vm->m_area.index(p);
313 for (int i = 0; i < height; i++) {
314 vm->m_area.add_y(em, vi, 1);
316 content_t c = vm->m_data[vi].getContent();
317 if (c != CONTENT_AIR && c != CONTENT_IGNORE &&
321 vm->m_data[vi] = MapNode(c_place, 0, deco_param2);
328 int DecoSimple::getHeight()
330 return (deco_height_max > 0) ? deco_height_max : deco_height;
334 ///////////////////////////////////////////////////////////////////////////////
337 DecoSchematic::DecoSchematic()
343 size_t DecoSchematic::generate(MMVManip *vm, PcgRandom *pr, v3s16 p)
345 // Schematic could have been unloaded but not the decoration
346 // In this case generate() does nothing (but doesn't *fail*)
347 if (schematic == NULL)
350 if (!canPlaceDecoration(vm, p))
353 if (flags & DECO_PLACE_CENTER_X)
354 p.X -= (schematic->size.X - 1) / 2;
355 if (flags & DECO_PLACE_CENTER_Y)
356 p.Y -= (schematic->size.Y - 1) / 2;
357 if (flags & DECO_PLACE_CENTER_Z)
358 p.Z -= (schematic->size.Z - 1) / 2;
360 Rotation rot = (rotation == ROTATE_RAND) ?
361 (Rotation)pr->range(ROTATE_0, ROTATE_270) : rotation;
363 bool force_placement = (flags & DECO_FORCE_PLACEMENT);
365 schematic->blitToVManip(vm, p, rot, force_placement);
371 int DecoSchematic::getHeight()
373 // Account for a schematic being sunk into the ground by flag.
374 // When placed normally account for how a schematic is placed
375 // sunk 1 node into the ground.
376 return (flags & DECO_PLACE_CENTER_Y) ?
377 (schematic->size.Y - 1) / 2 : schematic->size.Y - 1;