]> git.lizzy.rs Git - dragonfireclient.git/blob - src/mapgen/mg_decoration.cpp
2491ef2fda5578b06d85ed33453a4198fc7e4fab
[dragonfireclient.git] / src / mapgen / mg_decoration.cpp
1 /*
2 Minetest
3 Copyright (C) 2014-2018 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4 Copyright (C) 2015-2018 paramat
5
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.
10
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.
15
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.
19 */
20
21 #include "mg_decoration.h"
22 #include "mg_schematic.h"
23 #include "mapgen.h"
24 #include "noise.h"
25 #include "map.h"
26 #include "log.h"
27 #include "util/numeric.h"
28 #include <algorithm>
29 #include <vector>
30
31
32 FlagDesc flagdesc_deco[] = {
33         {"place_center_x",  DECO_PLACE_CENTER_X},
34         {"place_center_y",  DECO_PLACE_CENTER_Y},
35         {"place_center_z",  DECO_PLACE_CENTER_Z},
36         {"force_placement", DECO_FORCE_PLACEMENT},
37         {"liquid_surface",  DECO_LIQUID_SURFACE},
38         {"all_floors",      DECO_ALL_FLOORS},
39         {"all_ceilings",    DECO_ALL_CEILINGS},
40         {NULL,              0}
41 };
42
43
44 ///////////////////////////////////////////////////////////////////////////////
45
46
47 DecorationManager::DecorationManager(IGameDef *gamedef) :
48         ObjDefManager(gamedef, OBJDEF_DECORATION)
49 {
50 }
51
52
53 size_t DecorationManager::placeAllDecos(Mapgen *mg, u32 blockseed,
54         v3s16 nmin, v3s16 nmax)
55 {
56         size_t nplaced = 0;
57
58         for (size_t i = 0; i != m_objects.size(); i++) {
59                 Decoration *deco = (Decoration *)m_objects[i];
60                 if (!deco)
61                         continue;
62
63                 nplaced += deco->placeDeco(mg, blockseed, nmin, nmax);
64                 blockseed++;
65         }
66
67         return nplaced;
68 }
69
70
71 ///////////////////////////////////////////////////////////////////////////////
72
73
74 void Decoration::resolveNodeNames()
75 {
76         getIdsFromNrBacklog(&c_place_on);
77         getIdsFromNrBacklog(&c_spawnby);
78 }
79
80
81 bool Decoration::canPlaceDecoration(MMVManip *vm, v3s16 p)
82 {
83         // Check if the decoration can be placed on this node
84         u32 vi = vm->m_area.index(p);
85         if (!CONTAINS(c_place_on, vm->m_data[vi].getContent()))
86                 return false;
87
88         // Don't continue if there are no spawnby constraints
89         if (nspawnby == -1)
90                 return true;
91
92         int nneighs = 0;
93         static const v3s16 dirs[16] = {
94                 v3s16( 0, 0,  1),
95                 v3s16( 0, 0, -1),
96                 v3s16( 1, 0,  0),
97                 v3s16(-1, 0,  0),
98                 v3s16( 1, 0,  1),
99                 v3s16(-1, 0,  1),
100                 v3s16(-1, 0, -1),
101                 v3s16( 1, 0, -1),
102
103                 v3s16( 0, 1,  1),
104                 v3s16( 0, 1, -1),
105                 v3s16( 1, 1,  0),
106                 v3s16(-1, 1,  0),
107                 v3s16( 1, 1,  1),
108                 v3s16(-1, 1,  1),
109                 v3s16(-1, 1, -1),
110                 v3s16( 1, 1, -1)
111         };
112
113         // Check these 16 neighbouring nodes for enough spawnby nodes
114         for (size_t i = 0; i != ARRLEN(dirs); i++) {
115                 u32 index = vm->m_area.index(p + dirs[i]);
116                 if (!vm->m_area.contains(index))
117                         continue;
118
119                 if (CONTAINS(c_spawnby, vm->m_data[index].getContent()))
120                         nneighs++;
121         }
122
123         if (nneighs < nspawnby)
124                 return false;
125
126         return true;
127 }
128
129
130 size_t Decoration::placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
131 {
132         PcgRandom ps(blockseed + 53);
133         int carea_size = nmax.X - nmin.X + 1;
134
135         // Divide area into parts
136         // If chunksize is changed it may no longer be divisable by sidelen
137         if (carea_size % sidelen)
138                 sidelen = carea_size;
139
140         s16 divlen = carea_size / sidelen;
141         int area = sidelen * sidelen;
142
143         for (s16 z0 = 0; z0 < divlen; z0++)
144         for (s16 x0 = 0; x0 < divlen; x0++) {
145                 v2s16 p2d_center( // Center position of part of division
146                         nmin.X + sidelen / 2 + sidelen * x0,
147                         nmin.Z + sidelen / 2 + sidelen * z0
148                 );
149                 v2s16 p2d_min( // Minimum edge of part of division
150                         nmin.X + sidelen * x0,
151                         nmin.Z + sidelen * z0
152                 );
153                 v2s16 p2d_max( // Maximum edge of part of division
154                         nmin.X + sidelen + sidelen * x0 - 1,
155                         nmin.Z + sidelen + sidelen * z0 - 1
156                 );
157
158                 // Amount of decorations
159                 float nval = (flags & DECO_USE_NOISE) ?
160                         NoisePerlin2D(&np, p2d_center.X, p2d_center.Y, mapseed) :
161                         fill_ratio;
162                 u32 deco_count = 0;
163                 float deco_count_f = (float)area * nval;
164                 if (deco_count_f >= 1.f) {
165                         deco_count = deco_count_f;
166                 } else if (deco_count_f > 0.f) {
167                         // For low density decorations calculate a chance for 1 decoration
168                         if (ps.range(1000) <= deco_count_f * 1000.f)
169                                 deco_count = 1;
170                 }
171
172                 for (u32 i = 0; i < deco_count; i++) {
173                         s16 x = ps.range(p2d_min.X, p2d_max.X);
174                         s16 z = ps.range(p2d_min.Y, p2d_max.Y);
175                         int mapindex = carea_size * (z - nmin.Z) + (x - nmin.X);
176
177                         if ((flags & DECO_ALL_FLOORS) ||
178                                         (flags & DECO_ALL_CEILINGS)) {
179                                 // All-surfaces decorations
180                                 // Check biome of column
181                                 if (mg->biomemap && !biomes.empty()) {
182                                         std::unordered_set<u8>::const_iterator iter =
183                                                 biomes.find(mg->biomemap[mapindex]);
184                                         if (iter == biomes.end())
185                                                 continue;
186                                 }
187
188                                 // Get all floors and ceilings in node column
189                                 u16 size = (nmax.Y - nmin.Y + 1) / 2;
190                                 std::vector<s16> floors;
191                                 std::vector<s16> ceilings;
192                                 floors.reserve(size);
193                                 ceilings.reserve(size);
194
195                                 mg->getSurfaces(v2s16(x, z), nmin.Y, nmax.Y, floors, ceilings);
196
197                                 if (flags & DECO_ALL_FLOORS) {
198                                         // Floor decorations
199                                         for (const s16 y : floors) {
200                                                 if (y < y_min || y > y_max)
201                                                         continue;
202
203                                                 v3s16 pos(x, y, z);
204                                                 if (generate(mg->vm, &ps, pos, false))
205                                                         mg->gennotify.addEvent(
206                                                                         GENNOTIFY_DECORATION, pos, index);
207                                         }
208                                 }
209
210                                 if (flags & DECO_ALL_CEILINGS) {
211                                         // Ceiling decorations
212                                         for (const s16 y : ceilings) {
213                                                 if (y < y_min || y > y_max)
214                                                         continue;
215
216                                                 v3s16 pos(x, y, z);
217                                                 if (generate(mg->vm, &ps, pos, true))
218                                                         mg->gennotify.addEvent(
219                                                                         GENNOTIFY_DECORATION, pos, index);
220                                         }
221                                 }
222                         } else { // Heightmap decorations
223                                 s16 y = -MAX_MAP_GENERATION_LIMIT;
224                                 if (flags & DECO_LIQUID_SURFACE)
225                                         y = mg->findLiquidSurface(v2s16(x, z), nmin.Y, nmax.Y);
226                                 else if (mg->heightmap)
227                                         y = mg->heightmap[mapindex];
228                                 else
229                                         y = mg->findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
230
231                                 if (y < y_min || y > y_max || y < nmin.Y || y > nmax.Y)
232                                         continue;
233
234                                 if (mg->biomemap && !biomes.empty()) {
235                                         std::unordered_set<u8>::const_iterator iter =
236                                                 biomes.find(mg->biomemap[mapindex]);
237                                         if (iter == biomes.end())
238                                                 continue;
239                                 }
240
241                                 v3s16 pos(x, y, z);
242                                 if (generate(mg->vm, &ps, pos, false))
243                                         mg->gennotify.addEvent(GENNOTIFY_DECORATION, pos, index);
244                         }
245                 }
246         }
247
248         return 0;
249 }
250
251
252 ///////////////////////////////////////////////////////////////////////////////
253
254
255 void DecoSimple::resolveNodeNames()
256 {
257         Decoration::resolveNodeNames();
258         getIdsFromNrBacklog(&c_decos);
259 }
260
261
262 size_t DecoSimple::generate(MMVManip *vm, PcgRandom *pr, v3s16 p, bool ceiling)
263 {
264         // Don't bother if there aren't any decorations to place
265         if (c_decos.empty())
266                 return 0;
267
268         if (!canPlaceDecoration(vm, p))
269                 return 0;
270
271         // Check for placement outside the voxelmanip volume
272         if (ceiling) {
273                 // Ceiling decorations
274                 // 'place offset y' is inverted
275                 if (p.Y - place_offset_y - std::max(deco_height, deco_height_max) <
276                                 vm->m_area.MinEdge.Y)
277                         return 0;
278
279                 if (p.Y - 1 - place_offset_y > vm->m_area.MaxEdge.Y)
280                         return 0;
281
282         } else { // Heightmap and floor decorations
283                 if (p.Y + place_offset_y + std::max(deco_height, deco_height_max) >
284                                 vm->m_area.MaxEdge.Y)
285                         return 0;
286
287                 if (p.Y + 1 + place_offset_y < vm->m_area.MinEdge.Y)
288                         return 0;
289         }
290
291         content_t c_place = c_decos[pr->range(0, c_decos.size() - 1)];
292         s16 height = (deco_height_max > 0) ?
293                 pr->range(deco_height, deco_height_max) : deco_height;
294         u8 param2 = (deco_param2_max > 0) ?
295                 pr->range(deco_param2, deco_param2_max) : deco_param2;
296         bool force_placement = (flags & DECO_FORCE_PLACEMENT);
297
298         const v3s16 &em = vm->m_area.getExtent();
299         u32 vi = vm->m_area.index(p);
300
301         if (ceiling) {
302                 // Ceiling decorations
303                 // 'place offset y' is inverted
304                 vm->m_area.add_y(em, vi, -place_offset_y);
305
306                 for (int i = 0; i < height; i++) {
307                         vm->m_area.add_y(em, vi, -1);
308                         content_t c = vm->m_data[vi].getContent();
309                         if (c != CONTENT_AIR && c != CONTENT_IGNORE && !force_placement)
310                                 break;
311
312                         vm->m_data[vi] = MapNode(c_place, 0, param2);
313                 }
314         } else { // Heightmap and floor decorations
315                 vm->m_area.add_y(em, vi, place_offset_y);
316
317                 for (int i = 0; i < height; i++) {
318                         vm->m_area.add_y(em, vi, 1);
319                         content_t c = vm->m_data[vi].getContent();
320                         if (c != CONTENT_AIR && c != CONTENT_IGNORE && !force_placement)
321                                 break;
322
323                         vm->m_data[vi] = MapNode(c_place, 0, param2);
324                 }
325         }
326
327         return 1;
328 }
329
330
331 ///////////////////////////////////////////////////////////////////////////////
332
333
334 size_t DecoSchematic::generate(MMVManip *vm, PcgRandom *pr, v3s16 p, bool ceiling)
335 {
336         // Schematic could have been unloaded but not the decoration
337         // In this case generate() does nothing (but doesn't *fail*)
338         if (schematic == NULL)
339                 return 0;
340
341         if (!canPlaceDecoration(vm, p))
342                 return 0;
343
344         if (flags & DECO_PLACE_CENTER_Y) {
345                 p.Y -= (schematic->size.Y - 1) / 2;
346         } else {
347                 // Only apply 'place offset y' if not 'deco place center y'
348                 if (ceiling)
349                         // Shift down so that schematic top layer is level with ceiling
350                         // 'place offset y' is inverted
351                         p.Y -= (place_offset_y + schematic->size.Y - 1);
352                 else
353                         p.Y += place_offset_y;
354         }
355
356         // Check schematic top and base are in voxelmanip
357         if (p.Y + schematic->size.Y - 1 > vm->m_area.MaxEdge.Y)
358                 return 0;
359
360         if (p.Y < vm->m_area.MinEdge.Y)
361                 return 0;
362
363         if (flags & DECO_PLACE_CENTER_X)
364                 p.X -= (schematic->size.X - 1) / 2;
365         if (flags & DECO_PLACE_CENTER_Z)
366                 p.Z -= (schematic->size.Z - 1) / 2;
367
368         Rotation rot = (rotation == ROTATE_RAND) ?
369                 (Rotation)pr->range(ROTATE_0, ROTATE_270) : rotation;
370         bool force_placement = (flags & DECO_FORCE_PLACEMENT);
371
372         schematic->blitToVManip(vm, p, rot, force_placement);
373
374         return 1;
375 }