2014-11-01 18:16:23 +01:00
|
|
|
/*
|
|
|
|
Minetest
|
2018-01-15 03:26:15 +01:00
|
|
|
Copyright (C) 2014-2018 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
|
|
|
|
Copyright (C) 2015-2018 paramat
|
2014-11-01 18:16:23 +01:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "mg_decoration.h"
|
|
|
|
#include "mg_schematic.h"
|
|
|
|
#include "mapgen.h"
|
2014-11-13 05:01:13 +01:00
|
|
|
#include "noise.h"
|
2014-11-01 18:16:23 +01:00
|
|
|
#include "map.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "util/numeric.h"
|
2015-07-06 18:53:30 +02:00
|
|
|
#include <algorithm>
|
2017-12-10 09:07:24 +01:00
|
|
|
#include <vector>
|
2015-07-06 18:53:30 +02:00
|
|
|
|
2014-11-01 18:16:23 +01:00
|
|
|
|
2014-12-10 06:56:44 +01:00
|
|
|
FlagDesc flagdesc_deco[] = {
|
2016-09-12 00:34:43 +02:00
|
|
|
{"place_center_x", DECO_PLACE_CENTER_X},
|
|
|
|
{"place_center_y", DECO_PLACE_CENTER_Y},
|
|
|
|
{"place_center_z", DECO_PLACE_CENTER_Z},
|
2015-03-05 02:53:11 +01:00
|
|
|
{"force_placement", DECO_FORCE_PLACEMENT},
|
2016-09-12 00:34:43 +02:00
|
|
|
{"liquid_surface", DECO_LIQUID_SURFACE},
|
2017-10-11 02:06:40 +02:00
|
|
|
{"all_floors", DECO_ALL_FLOORS},
|
|
|
|
{"all_ceilings", DECO_ALL_CEILINGS},
|
2016-09-12 00:34:43 +02:00
|
|
|
{NULL, 0}
|
2014-11-13 05:01:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-11-01 18:16:23 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
2014-12-12 20:07:49 +01:00
|
|
|
DecorationManager::DecorationManager(IGameDef *gamedef) :
|
2015-03-31 05:40:35 +02:00
|
|
|
ObjDefManager(gamedef, OBJDEF_DECORATION)
|
2014-12-12 20:07:49 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-05 04:37:20 +01:00
|
|
|
size_t DecorationManager::placeAllDecos(Mapgen *mg, u32 blockseed,
|
2017-09-17 01:26:20 +02:00
|
|
|
v3s16 nmin, v3s16 nmax)
|
2014-11-01 18:16:23 +01:00
|
|
|
{
|
2014-11-13 05:01:13 +01:00
|
|
|
size_t nplaced = 0;
|
|
|
|
|
2015-03-31 05:40:35 +02:00
|
|
|
for (size_t i = 0; i != m_objects.size(); i++) {
|
|
|
|
Decoration *deco = (Decoration *)m_objects[i];
|
2014-11-13 05:01:13 +01:00
|
|
|
if (!deco)
|
|
|
|
continue;
|
|
|
|
|
2017-09-17 01:26:20 +02:00
|
|
|
nplaced += deco->placeDeco(mg, blockseed, nmin, nmax);
|
2015-01-05 04:37:20 +01:00
|
|
|
blockseed++;
|
2014-11-01 18:16:23 +01:00
|
|
|
}
|
2014-11-13 05:01:13 +01:00
|
|
|
|
|
|
|
return nplaced;
|
2014-11-01 18:16:23 +01:00
|
|
|
}
|
|
|
|
|
2020-04-09 23:40:12 +02:00
|
|
|
DecorationManager *DecorationManager::clone() const
|
|
|
|
{
|
|
|
|
auto mgr = new DecorationManager();
|
|
|
|
ObjDefManager::cloneTo(mgr);
|
|
|
|
return mgr;
|
|
|
|
}
|
|
|
|
|
2014-11-01 18:16:23 +01:00
|
|
|
|
2014-11-13 05:01:13 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2017-09-06 23:20:09 +02:00
|
|
|
|
2015-04-16 10:12:26 +02:00
|
|
|
void Decoration::resolveNodeNames()
|
2014-12-17 09:20:17 +01:00
|
|
|
{
|
2015-04-16 10:12:26 +02:00
|
|
|
getIdsFromNrBacklog(&c_place_on);
|
2016-09-12 00:34:43 +02:00
|
|
|
getIdsFromNrBacklog(&c_spawnby);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Decoration::canPlaceDecoration(MMVManip *vm, v3s16 p)
|
|
|
|
{
|
|
|
|
// Check if the decoration can be placed on this node
|
|
|
|
u32 vi = vm->m_area.index(p);
|
|
|
|
if (!CONTAINS(c_place_on, vm->m_data[vi].getContent()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Don't continue if there are no spawnby constraints
|
|
|
|
if (nspawnby == -1)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
int nneighs = 0;
|
|
|
|
static const v3s16 dirs[16] = {
|
|
|
|
v3s16( 0, 0, 1),
|
|
|
|
v3s16( 0, 0, -1),
|
|
|
|
v3s16( 1, 0, 0),
|
|
|
|
v3s16(-1, 0, 0),
|
|
|
|
v3s16( 1, 0, 1),
|
|
|
|
v3s16(-1, 0, 1),
|
|
|
|
v3s16(-1, 0, -1),
|
|
|
|
v3s16( 1, 0, -1),
|
|
|
|
|
|
|
|
v3s16( 0, 1, 1),
|
|
|
|
v3s16( 0, 1, -1),
|
|
|
|
v3s16( 1, 1, 0),
|
|
|
|
v3s16(-1, 1, 0),
|
|
|
|
v3s16( 1, 1, 1),
|
|
|
|
v3s16(-1, 1, 1),
|
|
|
|
v3s16(-1, 1, -1),
|
|
|
|
v3s16( 1, 1, -1)
|
|
|
|
};
|
|
|
|
|
2022-11-09 17:57:19 +01:00
|
|
|
// Check these 16 neighboring nodes for enough spawnby nodes
|
2016-09-12 00:34:43 +02:00
|
|
|
for (size_t i = 0; i != ARRLEN(dirs); i++) {
|
|
|
|
u32 index = vm->m_area.index(p + dirs[i]);
|
|
|
|
if (!vm->m_area.contains(index))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (CONTAINS(c_spawnby, vm->m_data[index].getContent()))
|
|
|
|
nneighs++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nneighs < nspawnby)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2014-12-17 09:20:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-17 01:26:20 +02:00
|
|
|
size_t Decoration::placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
|
2014-11-01 18:16:23 +01:00
|
|
|
{
|
2015-11-09 05:40:18 +01:00
|
|
|
PcgRandom ps(blockseed + 53);
|
2014-11-01 18:16:23 +01:00
|
|
|
int carea_size = nmax.X - nmin.X + 1;
|
|
|
|
|
|
|
|
// Divide area into parts
|
2015-09-26 01:28:48 +02:00
|
|
|
// If chunksize is changed it may no longer be divisable by sidelen
|
|
|
|
if (carea_size % sidelen)
|
2014-11-01 18:16:23 +01:00
|
|
|
sidelen = carea_size;
|
|
|
|
|
|
|
|
s16 divlen = carea_size / sidelen;
|
|
|
|
int area = sidelen * sidelen;
|
|
|
|
|
|
|
|
for (s16 z0 = 0; z0 < divlen; z0++)
|
|
|
|
for (s16 x0 = 0; x0 < divlen; x0++) {
|
|
|
|
v2s16 p2d_center( // Center position of part of division
|
|
|
|
nmin.X + sidelen / 2 + sidelen * x0,
|
|
|
|
nmin.Z + sidelen / 2 + sidelen * z0
|
|
|
|
);
|
|
|
|
v2s16 p2d_min( // Minimum edge of part of division
|
|
|
|
nmin.X + sidelen * x0,
|
|
|
|
nmin.Z + sidelen * z0
|
|
|
|
);
|
|
|
|
v2s16 p2d_max( // Maximum edge of part of division
|
|
|
|
nmin.X + sidelen + sidelen * x0 - 1,
|
|
|
|
nmin.Z + sidelen + sidelen * z0 - 1
|
|
|
|
);
|
|
|
|
|
2018-06-24 18:14:18 +02:00
|
|
|
bool cover = false;
|
2014-11-01 18:16:23 +01:00
|
|
|
// Amount of decorations
|
2014-12-10 06:56:44 +01:00
|
|
|
float nval = (flags & DECO_USE_NOISE) ?
|
|
|
|
NoisePerlin2D(&np, p2d_center.X, p2d_center.Y, mapseed) :
|
2014-11-01 18:16:23 +01:00
|
|
|
fill_ratio;
|
2015-11-13 08:16:15 +01:00
|
|
|
u32 deco_count = 0;
|
2018-06-24 18:14:18 +02:00
|
|
|
|
|
|
|
if (nval >= 10.0f) {
|
|
|
|
// Complete coverage. Disable random placement to avoid
|
|
|
|
// redundant multiple placements at one position.
|
|
|
|
cover = true;
|
|
|
|
deco_count = area;
|
|
|
|
} else {
|
|
|
|
float deco_count_f = (float)area * nval;
|
|
|
|
if (deco_count_f >= 1.0f) {
|
|
|
|
deco_count = deco_count_f;
|
|
|
|
} else if (deco_count_f > 0.0f) {
|
|
|
|
// For very low density calculate a chance for 1 decoration
|
|
|
|
if (ps.range(1000) <= deco_count_f * 1000.0f)
|
|
|
|
deco_count = 1;
|
|
|
|
}
|
2015-11-13 08:16:15 +01:00
|
|
|
}
|
2014-11-01 18:16:23 +01:00
|
|
|
|
2018-06-24 18:14:18 +02:00
|
|
|
s16 x = p2d_min.X - 1;
|
|
|
|
s16 z = p2d_min.Y;
|
|
|
|
|
2014-11-01 18:16:23 +01:00
|
|
|
for (u32 i = 0; i < deco_count; i++) {
|
2018-06-24 18:14:18 +02:00
|
|
|
if (!cover) {
|
|
|
|
x = ps.range(p2d_min.X, p2d_max.X);
|
|
|
|
z = ps.range(p2d_min.Y, p2d_max.Y);
|
|
|
|
} else {
|
|
|
|
x++;
|
|
|
|
if (x == p2d_max.X + 1) {
|
|
|
|
z++;
|
|
|
|
x = p2d_min.X;
|
|
|
|
}
|
|
|
|
}
|
2014-11-01 18:16:23 +01:00
|
|
|
int mapindex = carea_size * (z - nmin.Z) + (x - nmin.X);
|
|
|
|
|
2017-10-11 02:06:40 +02:00
|
|
|
if ((flags & DECO_ALL_FLOORS) ||
|
|
|
|
(flags & DECO_ALL_CEILINGS)) {
|
|
|
|
// All-surfaces decorations
|
|
|
|
// Check biome of column
|
|
|
|
if (mg->biomemap && !biomes.empty()) {
|
2020-05-20 23:16:14 +02:00
|
|
|
auto iter = biomes.find(mg->biomemap[mapindex]);
|
2017-10-11 02:06:40 +02:00
|
|
|
if (iter == biomes.end())
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get all floors and ceilings in node column
|
|
|
|
u16 size = (nmax.Y - nmin.Y + 1) / 2;
|
2017-12-10 09:07:24 +01:00
|
|
|
std::vector<s16> floors;
|
|
|
|
std::vector<s16> ceilings;
|
|
|
|
floors.reserve(size);
|
|
|
|
ceilings.reserve(size);
|
2017-10-11 02:06:40 +02:00
|
|
|
|
2017-12-10 09:07:24 +01:00
|
|
|
mg->getSurfaces(v2s16(x, z), nmin.Y, nmax.Y, floors, ceilings);
|
2017-10-11 02:06:40 +02:00
|
|
|
|
2017-12-10 09:07:24 +01:00
|
|
|
if (flags & DECO_ALL_FLOORS) {
|
2017-10-11 02:06:40 +02:00
|
|
|
// Floor decorations
|
2017-12-10 09:07:24 +01:00
|
|
|
for (const s16 y : floors) {
|
2017-10-11 02:06:40 +02:00
|
|
|
if (y < y_min || y > y_max)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
v3s16 pos(x, y, z);
|
|
|
|
if (generate(mg->vm, &ps, pos, false))
|
|
|
|
mg->gennotify.addEvent(
|
|
|
|
GENNOTIFY_DECORATION, pos, index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-10 09:07:24 +01:00
|
|
|
if (flags & DECO_ALL_CEILINGS) {
|
2017-10-11 02:06:40 +02:00
|
|
|
// Ceiling decorations
|
2017-12-10 09:07:24 +01:00
|
|
|
for (const s16 y : ceilings) {
|
2017-10-11 02:06:40 +02:00
|
|
|
if (y < y_min || y > y_max)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
v3s16 pos(x, y, z);
|
|
|
|
if (generate(mg->vm, &ps, pos, true))
|
|
|
|
mg->gennotify.addEvent(
|
|
|
|
GENNOTIFY_DECORATION, pos, index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else { // Heightmap decorations
|
|
|
|
s16 y = -MAX_MAP_GENERATION_LIMIT;
|
|
|
|
if (flags & DECO_LIQUID_SURFACE)
|
|
|
|
y = mg->findLiquidSurface(v2s16(x, z), nmin.Y, nmax.Y);
|
|
|
|
else if (mg->heightmap)
|
|
|
|
y = mg->heightmap[mapindex];
|
|
|
|
else
|
|
|
|
y = mg->findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
|
|
|
|
|
|
|
|
if (y < y_min || y > y_max || y < nmin.Y || y > nmax.Y)
|
|
|
|
continue;
|
2014-11-01 18:16:23 +01:00
|
|
|
|
2017-10-11 02:06:40 +02:00
|
|
|
if (mg->biomemap && !biomes.empty()) {
|
2020-05-20 23:16:14 +02:00
|
|
|
auto iter = biomes.find(mg->biomemap[mapindex]);
|
2017-10-11 02:06:40 +02:00
|
|
|
if (iter == biomes.end())
|
|
|
|
continue;
|
|
|
|
}
|
2014-11-01 18:16:23 +01:00
|
|
|
|
2017-10-11 02:06:40 +02:00
|
|
|
v3s16 pos(x, y, z);
|
|
|
|
if (generate(mg->vm, &ps, pos, false))
|
|
|
|
mg->gennotify.addEvent(GENNOTIFY_DECORATION, pos, index);
|
2014-11-01 18:16:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-13 05:01:13 +01:00
|
|
|
|
|
|
|
return 0;
|
2014-11-01 18:16:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-09 23:40:12 +02:00
|
|
|
void Decoration::cloneTo(Decoration *def) const
|
|
|
|
{
|
|
|
|
ObjDef::cloneTo(def);
|
|
|
|
def->flags = flags;
|
|
|
|
def->mapseed = mapseed;
|
|
|
|
def->c_place_on = c_place_on;
|
|
|
|
def->sidelen = sidelen;
|
|
|
|
def->y_min = y_min;
|
|
|
|
def->y_max = y_max;
|
|
|
|
def->fill_ratio = fill_ratio;
|
|
|
|
def->np = np;
|
|
|
|
def->c_spawnby = c_spawnby;
|
|
|
|
def->nspawnby = nspawnby;
|
|
|
|
def->place_offset_y = place_offset_y;
|
|
|
|
def->biomes = biomes;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-01 18:16:23 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-11-13 05:01:13 +01:00
|
|
|
|
2020-04-09 23:40:12 +02:00
|
|
|
ObjDef *DecoSimple::clone() const
|
|
|
|
{
|
|
|
|
auto def = new DecoSimple();
|
|
|
|
Decoration::cloneTo(def);
|
|
|
|
|
|
|
|
def->c_decos = c_decos;
|
|
|
|
def->deco_height = deco_height;
|
|
|
|
def->deco_height_max = deco_height_max;
|
|
|
|
def->deco_param2 = deco_param2;
|
|
|
|
def->deco_param2_max = deco_param2_max;
|
|
|
|
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-16 10:12:26 +02:00
|
|
|
void DecoSimple::resolveNodeNames()
|
2014-12-17 09:20:17 +01:00
|
|
|
{
|
2015-04-16 10:12:26 +02:00
|
|
|
Decoration::resolveNodeNames();
|
|
|
|
getIdsFromNrBacklog(&c_decos);
|
2014-12-17 09:20:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-11 02:06:40 +02:00
|
|
|
size_t DecoSimple::generate(MMVManip *vm, PcgRandom *pr, v3s16 p, bool ceiling)
|
2014-11-01 18:16:23 +01:00
|
|
|
{
|
|
|
|
// Don't bother if there aren't any decorations to place
|
2017-08-20 19:37:29 +02:00
|
|
|
if (c_decos.empty())
|
2016-09-12 00:34:43 +02:00
|
|
|
return 0;
|
2014-11-01 18:16:23 +01:00
|
|
|
|
|
|
|
if (!canPlaceDecoration(vm, p))
|
2014-12-06 10:18:04 +01:00
|
|
|
return 0;
|
2014-11-01 18:16:23 +01:00
|
|
|
|
2017-10-11 02:06:40 +02:00
|
|
|
// Check for placement outside the voxelmanip volume
|
|
|
|
if (ceiling) {
|
|
|
|
// Ceiling decorations
|
|
|
|
// 'place offset y' is inverted
|
|
|
|
if (p.Y - place_offset_y - std::max(deco_height, deco_height_max) <
|
|
|
|
vm->m_area.MinEdge.Y)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (p.Y - 1 - place_offset_y > vm->m_area.MaxEdge.Y)
|
|
|
|
return 0;
|
2014-11-01 18:16:23 +01:00
|
|
|
|
2017-10-11 02:06:40 +02:00
|
|
|
} else { // Heightmap and floor decorations
|
|
|
|
if (p.Y + place_offset_y + std::max(deco_height, deco_height_max) >
|
|
|
|
vm->m_area.MaxEdge.Y)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (p.Y + 1 + place_offset_y < vm->m_area.MinEdge.Y)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
content_t c_place = c_decos[pr->range(0, c_decos.size() - 1)];
|
2014-11-01 18:16:23 +01:00
|
|
|
s16 height = (deco_height_max > 0) ?
|
|
|
|
pr->range(deco_height, deco_height_max) : deco_height;
|
2017-10-08 22:08:52 +02:00
|
|
|
u8 param2 = (deco_param2_max > 0) ?
|
|
|
|
pr->range(deco_param2, deco_param2_max) : deco_param2;
|
2016-02-28 00:49:53 +01:00
|
|
|
bool force_placement = (flags & DECO_FORCE_PLACEMENT);
|
|
|
|
|
2017-07-26 07:35:09 +02:00
|
|
|
const v3s16 &em = vm->m_area.getExtent();
|
2014-11-01 18:16:23 +01:00
|
|
|
u32 vi = vm->m_area.index(p);
|
2017-10-09 21:13:10 +02:00
|
|
|
|
2017-10-11 02:06:40 +02:00
|
|
|
if (ceiling) {
|
|
|
|
// Ceiling decorations
|
|
|
|
// 'place offset y' is inverted
|
2018-03-09 08:49:00 +01:00
|
|
|
VoxelArea::add_y(em, vi, -place_offset_y);
|
2014-11-01 18:16:23 +01:00
|
|
|
|
2017-10-11 02:06:40 +02:00
|
|
|
for (int i = 0; i < height; i++) {
|
2018-03-09 08:49:00 +01:00
|
|
|
VoxelArea::add_y(em, vi, -1);
|
2017-10-11 02:06:40 +02:00
|
|
|
content_t c = vm->m_data[vi].getContent();
|
|
|
|
if (c != CONTENT_AIR && c != CONTENT_IGNORE && !force_placement)
|
|
|
|
break;
|
2014-12-06 10:18:04 +01:00
|
|
|
|
2017-10-11 02:06:40 +02:00
|
|
|
vm->m_data[vi] = MapNode(c_place, 0, param2);
|
|
|
|
}
|
|
|
|
} else { // Heightmap and floor decorations
|
2018-03-09 08:49:00 +01:00
|
|
|
VoxelArea::add_y(em, vi, place_offset_y);
|
2014-11-01 18:16:23 +01:00
|
|
|
|
2017-10-11 02:06:40 +02:00
|
|
|
for (int i = 0; i < height; i++) {
|
2018-03-09 08:49:00 +01:00
|
|
|
VoxelArea::add_y(em, vi, 1);
|
2017-10-11 02:06:40 +02:00
|
|
|
content_t c = vm->m_data[vi].getContent();
|
|
|
|
if (c != CONTENT_AIR && c != CONTENT_IGNORE && !force_placement)
|
|
|
|
break;
|
2014-11-01 18:16:23 +01:00
|
|
|
|
2017-10-11 02:06:40 +02:00
|
|
|
vm->m_data[vi] = MapNode(c_place, 0, param2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
2014-11-01 18:16:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-13 05:01:13 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2017-09-06 23:20:09 +02:00
|
|
|
|
2020-04-10 02:05:20 +02:00
|
|
|
DecoSchematic::~DecoSchematic()
|
|
|
|
{
|
|
|
|
if (was_cloned)
|
|
|
|
delete schematic;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-09 23:40:12 +02:00
|
|
|
ObjDef *DecoSchematic::clone() const
|
|
|
|
{
|
|
|
|
auto def = new DecoSchematic();
|
|
|
|
Decoration::cloneTo(def);
|
|
|
|
NodeResolver::cloneTo(def);
|
|
|
|
|
|
|
|
def->rotation = rotation;
|
2020-04-10 02:05:20 +02:00
|
|
|
/* FIXME: We do not own this schematic, yet we only have a pointer to it
|
|
|
|
* and not a handle. We are left with no option but to clone it ourselves.
|
|
|
|
* This is a waste of memory and should be replaced with an alternative
|
|
|
|
* approach sometime. */
|
|
|
|
def->schematic = dynamic_cast<Schematic*>(schematic->clone());
|
|
|
|
def->was_cloned = true;
|
2020-04-09 23:40:12 +02:00
|
|
|
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-11 02:06:40 +02:00
|
|
|
size_t DecoSchematic::generate(MMVManip *vm, PcgRandom *pr, v3s16 p, bool ceiling)
|
2014-11-13 05:01:13 +01:00
|
|
|
{
|
2015-04-17 06:52:48 +02:00
|
|
|
// Schematic could have been unloaded but not the decoration
|
|
|
|
// In this case generate() does nothing (but doesn't *fail*)
|
|
|
|
if (schematic == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2016-09-12 00:34:43 +02:00
|
|
|
if (!canPlaceDecoration(vm, p))
|
2014-12-06 10:18:04 +01:00
|
|
|
return 0;
|
2014-11-13 05:01:13 +01:00
|
|
|
|
2017-10-11 02:06:40 +02:00
|
|
|
if (flags & DECO_PLACE_CENTER_Y) {
|
|
|
|
p.Y -= (schematic->size.Y - 1) / 2;
|
|
|
|
} else {
|
|
|
|
// Only apply 'place offset y' if not 'deco place center y'
|
|
|
|
if (ceiling)
|
|
|
|
// Shift down so that schematic top layer is level with ceiling
|
|
|
|
// 'place offset y' is inverted
|
|
|
|
p.Y -= (place_offset_y + schematic->size.Y - 1);
|
|
|
|
else
|
|
|
|
p.Y += place_offset_y;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check schematic top and base are in voxelmanip
|
|
|
|
if (p.Y + schematic->size.Y - 1 > vm->m_area.MaxEdge.Y)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (p.Y < vm->m_area.MinEdge.Y)
|
|
|
|
return 0;
|
|
|
|
|
2014-11-13 05:01:13 +01:00
|
|
|
Rotation rot = (rotation == ROTATE_RAND) ?
|
|
|
|
(Rotation)pr->range(ROTATE_0, ROTATE_270) : rotation;
|
2018-05-24 03:52:35 +02:00
|
|
|
|
|
|
|
if (flags & DECO_PLACE_CENTER_X) {
|
|
|
|
if (rot == ROTATE_0 || rot == ROTATE_180)
|
|
|
|
p.X -= (schematic->size.X - 1) / 2;
|
|
|
|
else
|
|
|
|
p.Z -= (schematic->size.X - 1) / 2;
|
|
|
|
}
|
|
|
|
if (flags & DECO_PLACE_CENTER_Z) {
|
|
|
|
if (rot == ROTATE_0 || rot == ROTATE_180)
|
|
|
|
p.Z -= (schematic->size.Z - 1) / 2;
|
|
|
|
else
|
|
|
|
p.X -= (schematic->size.Z - 1) / 2;
|
|
|
|
}
|
|
|
|
|
2015-04-21 13:09:32 +02:00
|
|
|
bool force_placement = (flags & DECO_FORCE_PLACEMENT);
|
|
|
|
|
2015-11-04 09:33:12 +01:00
|
|
|
schematic->blitToVManip(vm, p, rot, force_placement);
|
2014-12-06 10:18:04 +01:00
|
|
|
|
|
|
|
return 1;
|
2014-11-13 05:01:13 +01:00
|
|
|
}
|