Use appropriate sized type for VoxelArea extent

This commit is contained in:
sfan5 2024-12-06 21:00:47 +01:00
parent 3c5e0d10fc
commit 8957739cdf
16 changed files with 62 additions and 47 deletions

@ -79,7 +79,7 @@ struct MapEditEvent
VoxelArea a;
for (v3s16 p : modified_blocks) {
v3s16 np1 = p*MAP_BLOCKSIZE;
v3s16 np2 = np1 + v3s16(1,1,1)*MAP_BLOCKSIZE - v3s16(1,1,1);
v3s16 np2 = np1 + v3s16(MAP_BLOCKSIZE-1);
a.addPoint(np1);
a.addPoint(np2);
}

@ -64,7 +64,7 @@ void CavesNoiseIntersection::generateCaves(MMVManip *vm,
noise_cave1->perlinMap3D(nmin.X, nmin.Y - 1, nmin.Z);
noise_cave2->perlinMap3D(nmin.X, nmin.Y - 1, nmin.Z);
const v3s16 &em = vm->m_area.getExtent();
const v3s32 &em = vm->m_area.getExtent();
u32 index2d = 0; // Biomemap index
for (s16 z = nmin.Z; z <= nmax.Z; z++)
@ -230,7 +230,7 @@ bool CavernsNoise::generateCaverns(MMVManip *vm, v3s16 nmin, v3s16 nmax)
//// Place nodes
bool near_cavern = false;
const v3s16 &em = vm->m_area.getExtent();
const v3s32 &em = vm->m_area.getExtent();
u32 index2d = 0;
for (s16 z = nmin.Z; z <= nmax.Z; z++)

@ -127,7 +127,7 @@ void DungeonGen::generate(MMVManip *vm, u32 bseed, v3s16 nmin, v3s16 nmax)
void DungeonGen::makeDungeon(v3s16 start_padding)
{
const v3s16 &areasize = vm->m_area.getExtent();
const v3s32 &areasize = vm->m_area.getExtent();
v3s16 roomsize;
v3s16 roomplace;

@ -240,7 +240,7 @@ u32 Mapgen::getBlockSeed2(v3s16 p, s32 seed)
// Returns -MAX_MAP_GENERATION_LIMIT if not found
s16 Mapgen::findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax)
{
const v3s16 &em = vm->m_area.getExtent();
const v3s32 &em = vm->m_area.getExtent();
u32 i = vm->m_area.index(p2d.X, ymax, p2d.Y);
s16 y;
@ -258,7 +258,7 @@ s16 Mapgen::findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax)
// Returns -MAX_MAP_GENERATION_LIMIT if not found or if ground is found first
s16 Mapgen::findLiquidSurface(v2s16 p2d, s16 ymin, s16 ymax)
{
const v3s16 &em = vm->m_area.getExtent();
const v3s32 &em = vm->m_area.getExtent();
u32 i = vm->m_area.index(p2d.X, ymax, p2d.Y);
s16 y;
@ -296,7 +296,7 @@ void Mapgen::updateHeightmap(v3s16 nmin, v3s16 nmax)
void Mapgen::getSurfaces(v2s16 p2d, s16 ymin, s16 ymax,
std::vector<s16> &floors, std::vector<s16> &ceilings)
{
const v3s16 &em = vm->m_area.getExtent();
const v3s32 &em = vm->m_area.getExtent();
bool is_walkable = false;
u32 vi = vm->m_area.index(p2d.X, ymax, p2d.Y);
@ -320,7 +320,7 @@ void Mapgen::getSurfaces(v2s16 p2d, s16 ymin, s16 ymax,
}
inline bool Mapgen::isLiquidHorizontallyFlowable(u32 vi, v3s16 em)
inline bool Mapgen::isLiquidHorizontallyFlowable(u32 vi, v3s32 em)
{
u32 vi_neg_x = vi;
VoxelArea::add_x(em, vi_neg_x, -1);
@ -357,7 +357,7 @@ void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nm
{
bool isignored, isliquid, wasignored, wasliquid, waschecked, waspushed;
content_t was_n;
const v3s16 &em = vm->m_area.getExtent();
const v3s32 &em = vm->m_area.getExtent();
isignored = true;
isliquid = false;
@ -481,7 +481,7 @@ void Mapgen::propagateSunlight(v3s16 nmin, v3s16 nmax, bool propagate_shadow)
//TimeTaker t("propagateSunlight");
VoxelArea a(nmin, nmax);
bool block_is_underground = (water_level >= nmax.Y);
const v3s16 &em = vm->m_area.getExtent();
const v3s32 &em = vm->m_area.getExtent();
// NOTE: Direct access to the low 4 bits of param1 is okay here because,
// by definition, sunlight will never be in the night lightbank.
@ -629,7 +629,7 @@ void MapgenBasic::generateBiomes()
assert(biomegen);
assert(biomemap);
const v3s16 &em = vm->m_area.getExtent();
const v3s32 &em = vm->m_area.getExtent();
u32 index = 0;
noise_filler_depth->perlinMap2D(node_min.X, node_min.Z);
@ -774,7 +774,7 @@ void MapgenBasic::dustTopNodes()
if (node_max.Y < water_level)
return;
const v3s16 &em = vm->m_area.getExtent();
const v3s32 &em = vm->m_area.getExtent();
u32 index = 0;
for (s16 z = node_min.Z; z <= node_max.Z; z++)

@ -261,7 +261,7 @@ private:
// isLiquidHorizontallyFlowable() is a helper function for updateLiquid()
// that checks whether there are floodable nodes without liquid beneath
// the node at index vi.
inline bool isLiquidHorizontallyFlowable(u32 vi, v3s16 em);
inline bool isLiquidHorizontallyFlowable(u32 vi, v3s32 em);
};
/*

@ -445,7 +445,7 @@ int MapgenCarpathian::generateTerrain()
noise_rivers->perlinMap2D(node_min.X, node_min.Z);
//// Place nodes
const v3s16 &em = vm->m_area.getExtent();
const v3s32 &em = vm->m_area.getExtent();
s16 stone_surface_max_y = -MAX_MAP_GENERATION_LIMIT;
u32 index2d = 0;

@ -278,7 +278,7 @@ s16 MapgenFlat::generateTerrain()
MapNode n_stone(c_stone);
MapNode n_water(c_water_source);
const v3s16 &em = vm->m_area.getExtent();
const v3s32 &em = vm->m_area.getExtent();
s16 stone_surface_max_y = -MAX_MAP_GENERATION_LIMIT;
u32 ni2d = 0;

@ -222,7 +222,7 @@ void MapgenV6Params::setDefaultSettings(Settings *settings)
// Returns Y one under area minimum if not found
s16 MapgenV6::find_stone_level(v2s16 p2d)
{
const v3s16 &em = vm->m_area.getExtent();
const v3s32 &em = vm->m_area.getExtent();
s16 y_nodes_max = vm->m_area.MaxEdge.Y;
s16 y_nodes_min = vm->m_area.MinEdge.Y;
u32 i = vm->m_area.index(p2d.X, y_nodes_max, p2d.Y);
@ -670,7 +670,7 @@ int MapgenV6::generateGround()
BiomeV6Type bt = getBiome(v2s16(x, z));
// Fill ground with stone
const v3s16 &em = vm->m_area.getExtent();
const v3s32 &em = vm->m_area.getExtent();
u32 i = vm->m_area.index(x, node_min.Y, z);
for (s16 y = node_min.Y; y <= node_max.Y; y++) {
if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
@ -739,7 +739,7 @@ void MapgenV6::addMud()
// Add mud on ground
s16 mudcount = 0;
const v3s16 &em = vm->m_area.getExtent();
const v3s32 &em = vm->m_area.getExtent();
s16 y_start = surface_y + 1;
u32 i = vm->m_area.index(x, y_start, z);
for (s16 y = y_start; y <= node_max.Y; y++) {
@ -757,7 +757,7 @@ void MapgenV6::addMud()
void MapgenV6::flowMud(s16 &mudflow_minpos, s16 &mudflow_maxpos)
{
const v3s16 &em = vm->m_area.getExtent();
const v3s32 &em = vm->m_area.getExtent();
static const v3s16 dirs4[4] = {
v3s16(0, 0, 1), // Back
v3s16(1, 0, 0), // Right
@ -870,7 +870,7 @@ void MapgenV6::flowMud(s16 &mudflow_minpos, s16 &mudflow_maxpos)
void MapgenV6::moveMud(u32 remove_index, u32 place_index,
u32 above_remove_index, v2s16 pos, v3s16 em)
u32 above_remove_index, v2s16 pos, v3s32 em)
{
MapNode n_air(CONTENT_AIR);
// Copy mud from old place to new place
@ -920,7 +920,7 @@ void MapgenV6::placeTreesAndJungleGrass()
if (c_junglegrass == CONTENT_IGNORE)
c_junglegrass = CONTENT_AIR;
MapNode n_junglegrass(c_junglegrass);
const v3s16 &em = vm->m_area.getExtent();
const v3s32 &em = vm->m_area.getExtent();
// Divide area into parts
s16 div = 8;
@ -1027,7 +1027,7 @@ void MapgenV6::growGrass() // Add surface nodes
MapNode n_dirt_with_grass(c_dirt_with_grass);
MapNode n_dirt_with_snow(c_dirt_with_snow);
MapNode n_snowblock(c_snowblock);
const v3s16 &em = vm->m_area.getExtent();
const v3s32 &em = vm->m_area.getExtent();
u32 index = 0;
for (s16 z = full_node_min.Z; z <= full_node_max.Z; z++)

@ -150,7 +150,7 @@ public:
void addMud();
void flowMud(s16 &mudflow_minpos, s16 &mudflow_maxpos);
void moveMud(u32 remove_index, u32 place_index,
u32 above_remove_index, v2s16 pos, v3s16 em);
u32 above_remove_index, v2s16 pos, v3s32 em);
void growGrass();
void placeTreesAndJungleGrass();
virtual void generateCaves(int max_stone_y);

@ -523,7 +523,7 @@ int MapgenV7::generateTerrain()
}
//// Place nodes
const v3s16 &em = vm->m_area.getExtent();
const v3s32 &em = vm->m_area.getExtent();
s16 stone_surface_max_y = -MAX_MAP_GENERATION_LIMIT;
u32 index2d = 0;

@ -344,7 +344,7 @@ int MapgenValleys::generateTerrain()
noise_inter_valley_fill->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z);
const v3s16 &em = vm->m_area.getExtent();
const v3s32 &em = vm->m_area.getExtent();
s16 surface_max_y = -MAX_MAP_GENERATION_LIMIT;
u32 index_2d = 0;

@ -346,7 +346,7 @@ size_t DecoSimple::generate(MMVManip *vm, PcgRandom *pr, v3s16 p, bool ceiling)
pr->range(deco_param2, deco_param2_max) : deco_param2;
bool force_placement = (flags & DECO_FORCE_PLACEMENT);
const v3s16 &em = vm->m_area.getExtent();
const v3s32 &em = vm->m_area.getExtent();
u32 vi = vm->m_area.index(p);
if (ceiling) {

@ -107,11 +107,16 @@ void TestVoxelArea::test_pad()
void TestVoxelArea::test_extent()
{
VoxelArea v1(v3s16(-1337, -547, -789), v3s16(-147, 447, 669));
UASSERT(v1.getExtent() == v3s16(1191, 995, 1459));
UASSERT(v1.getExtent() == v3s32(1191, 995, 1459));
VoxelArea v2(v3s16(32493, -32507, 32752), v3s16(32508, -32492, 32767));
UASSERT(v2.getExtent() == v3s16(16, 16, 16));
UASSERT(v2.getExtent() == v3s32(16, 16, 16));
// side length bigger than S16_MAX
VoxelArea v3({-20000, 12, 34}, {20000, 12, 34});
UASSERT(v3.getExtent() == v3s32(40001, 1, 1));
UASSERT(VoxelArea().hasEmptyExtent());
UASSERT(VoxelArea({2,3,4}, {1,2,3}).hasEmptyExtent());
UASSERT(VoxelArea({2,3,4}, {2,2,3}).hasEmptyExtent() == false);
}
@ -124,6 +129,9 @@ void TestVoxelArea::test_volume()
VoxelArea v2(v3s16(32493, -32507, 32752), v3s16(32508, -32492, 32767));
UASSERTEQ(s32, v2.getVolume(), 4096);
VoxelArea v3({-20000, 12, 34}, {20000, 12, 34});
UASSERTEQ(s32, v3.getVolume(), 40000);
UASSERTEQ(s32, VoxelArea({2,3,4}, {1,2,3}).getVolume(), 0);
UASSERTEQ(s32, VoxelArea({2,3,4}, {2,2,3}).getVolume(), 0);
}
@ -388,7 +396,7 @@ void TestVoxelArea::test_index_v3s16_all_neg()
void TestVoxelArea::test_add_x()
{
v3s16 extent;
v3s32 extent;
u32 i = 4;
VoxelArea::add_x(extent, i, 8);
UASSERTEQ(u32, i, 12)
@ -396,7 +404,7 @@ void TestVoxelArea::test_add_x()
void TestVoxelArea::test_add_y()
{
v3s16 extent(740, 16, 87);
v3s32 extent(740, 16, 87);
u32 i = 8;
VoxelArea::add_y(extent, i, 88);
UASSERTEQ(u32, i, 65128)
@ -404,7 +412,7 @@ void TestVoxelArea::test_add_y()
void TestVoxelArea::test_add_z()
{
v3s16 extent(114, 80, 256);
v3s32 extent(114, 80, 256);
u32 i = 4;
VoxelArea::add_z(extent, i, 8);
UASSERTEQ(u32, i, 72964)
@ -412,7 +420,7 @@ void TestVoxelArea::test_add_z()
void TestVoxelArea::test_add_p()
{
v3s16 extent(33, 14, 742);
v3s32 extent(33, 14, 742);
v3s16 a(15, 12, 369);
u32 i = 4;
VoxelArea::add_p(extent, i, a);

@ -38,7 +38,7 @@ void VoxelManipulator::clear()
void VoxelManipulator::print(std::ostream &o, const NodeDefManager *ndef,
VoxelPrintMode mode) const
{
const v3s16 &em = m_area.getExtent();
auto &em = m_area.getExtent();
v3s16 of = m_area.MinEdge;
o<<"size: "<<em.X<<"x"<<em.Y<<"x"<<em.Z
<<" offset: ("<<of.X<<","<<of.Y<<","<<of.Z<<")"<<std::endl;

@ -109,21 +109,22 @@ public:
const methods
*/
const v3s16 &getExtent() const
const v3s32 &getExtent() const
{
return m_cache_extent;
}
/// @note `getVolume() == 0` and `getEmptyExtent()` are not identical.
/// @note `getVolume() == 0` and `getEmptyExtent()` are not equivalent.
bool hasEmptyExtent() const
{
// FIXME: shouldn't this actually be a component-wise check?
return m_cache_extent == v3s16(0,0,0);
return m_cache_extent == v3s32(0,0,0);
}
s32 getVolume() const
{
return (s32)m_cache_extent.X * (s32)m_cache_extent.Y * (s32)m_cache_extent.Z;
// FIXME: possible integer overflow here
return m_cache_extent.X * m_cache_extent.Y * m_cache_extent.Z;
}
bool contains(const VoxelArea &a) const
@ -280,15 +281,16 @@ public:
/**
* Translate index in the X coordinate
*/
static void add_x(const v3s16 &extent, u32 &i, s16 a)
static void add_x(const v3s32 &extent, u32 &i, s16 a)
{
(void)extent;
i += a;
}
/**
* Translate index in the Y coordinate
*/
static void add_y(const v3s16 &extent, u32 &i, s16 a)
static void add_y(const v3s32 &extent, u32 &i, s16 a)
{
i += a * extent.X;
}
@ -296,7 +298,7 @@ public:
/**
* Translate index in the Z coordinate
*/
static void add_z(const v3s16 &extent, u32 &i, s16 a)
static void add_z(const v3s32 &extent, u32 &i, s16 a)
{
i += a * extent.X * extent.Y;
}
@ -304,7 +306,7 @@ public:
/**
* Translate index in space
*/
static void add_p(const v3s16 &extent, u32 &i, v3s16 a)
static void add_p(const v3s32 &extent, u32 &i, v3s16 a)
{
i += a.Z * extent.X * extent.Y + a.Y * extent.X + a.X;
}
@ -329,15 +331,20 @@ public:
private:
void cacheExtent()
{
m_cache_extent = MaxEdge - MinEdge + v3s16(1,1,1);
m_cache_extent = {
MaxEdge.X - MinEdge.X + 1,
MaxEdge.Y - MinEdge.Y + 1,
MaxEdge.Z - MinEdge.Z + 1
};
// If positions were sorted correctly this must always hold.
// Note that this still permits empty areas (where MinEdge = MaxEdge + 1).
assert(m_cache_extent.X >= 0);
assert(m_cache_extent.Y >= 0);
assert(m_cache_extent.Z >= 0);
assert(m_cache_extent.X >= 0 && m_cache_extent.X <= MAX_EXTENT);
assert(m_cache_extent.Y >= 0 && m_cache_extent.Y <= MAX_EXTENT);
assert(m_cache_extent.Z >= 0 && m_cache_extent.Z <= MAX_EXTENT);
}
v3s16 m_cache_extent = v3s16(0,0,0);
static constexpr s32 MAX_EXTENT = S16_MAX - S16_MIN + 1;
v3s32 m_cache_extent;
};
enum : u8 {

@ -767,7 +767,7 @@ void fill_with_sunlight(MMVManip *vm, const NodeDefManager *ndef, v2s16 offset,
bool light[MAP_BLOCKSIZE][MAP_BLOCKSIZE])
{
// Distance in array between two nodes on top of each other.
s16 ystride = vm->m_area.getExtent().X;
s32 ystride = vm->m_area.getExtent().X;
// Cache the ignore node.
MapNode ignore = MapNode(CONTENT_IGNORE);
// For each column of nodes: