diff --git a/src/map.cpp b/src/map.cpp index 1af8684e1..240788944 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -766,7 +766,7 @@ void MMVManip::initialEmerge(v3s16 blockpos_min, v3s16 blockpos_max, VoxelArea block_area_nodes (p_min*MAP_BLOCKSIZE, (p_max+1)*MAP_BLOCKSIZE-v3s16(1,1,1)); - u32 size_MB = block_area_nodes.getVolume()*4/1000000; + u32 size_MB = block_area_nodes.getVolume() * sizeof(MapNode) / 1000000U; if(size_MB >= 1) { infostream<<"initialEmerge: area: "; @@ -855,7 +855,7 @@ MMVManip *MMVManip::clone() const { MMVManip *ret = new MMVManip(); - const s32 size = m_area.getVolume(); + const u32 size = m_area.getVolume(); ret->m_area = m_area; if (m_data) { ret->m_data = new MapNode[size]; diff --git a/src/mapgen/treegen.cpp b/src/mapgen/treegen.cpp index 688833fa2..8198cf266 100644 --- a/src/mapgen/treegen.cpp +++ b/src/mapgen/treegen.cpp @@ -77,7 +77,7 @@ void make_tree(MMVManip &vmanip, v3s16 p0, bool is_apple_tree, VoxelArea leaves_a(v3s16(-2, -1, -2), v3s16(2, 2, 2)); Buffer leaves_d(leaves_a.getVolume()); - for (s32 i = 0; i < leaves_a.getVolume(); i++) + for (u32 i = 0; i < leaves_d.getSize(); i++) leaves_d[i] = 0; // Force leaves at near the end of the trunk @@ -697,9 +697,8 @@ void make_jungletree(MMVManip &vmanip, v3s16 p0, const NodeDefManager *ndef, p1.Y -= 1; VoxelArea leaves_a(v3s16(-3, -2, -3), v3s16(3, 2, 3)); - //SharedPtr leaves_d(new u8[leaves_a.getVolume()]); Buffer leaves_d(leaves_a.getVolume()); - for (s32 i = 0; i < leaves_a.getVolume(); i++) + for (u32 i = 0; i < leaves_d.getSize(); i++) leaves_d[i] = 0; // Force leaves at near the end of the trunk @@ -788,7 +787,7 @@ void make_pine_tree(MMVManip &vmanip, v3s16 p0, const NodeDefManager *ndef, VoxelArea leaves_a(v3s16(-3, -6, -3), v3s16(3, 3, 3)); Buffer leaves_d(leaves_a.getVolume()); - for (s32 i = 0; i < leaves_a.getVolume(); i++) + for (u32 i = 0; i < leaves_d.getSize(); i++) leaves_d[i] = 0; // Upper branches diff --git a/src/unittest/test_voxelalgorithms.cpp b/src/unittest/test_voxelalgorithms.cpp index 2a98412b4..b8ba11248 100644 --- a/src/unittest/test_voxelalgorithms.cpp +++ b/src/unittest/test_voxelalgorithms.cpp @@ -98,8 +98,8 @@ void TestVoxelAlgorithms::testLighting(IGameDef *gamedef) std::map modified_blocks; MMVManip vm(&map); vm.initialEmerge(bpmin, bpmax, false); - s32 volume = vm.m_area.getVolume(); - for (s32 i = 0; i < volume; i++) + u32 volume = vm.m_area.getVolume(); + for (u32 i = 0; i < volume; i++) vm.m_data[i] = MapNode(CONTENT_AIR); for (s16 z = -10; z <= 10; z++) for (s16 y = -10; y <= 10; y++) diff --git a/src/unittest/test_voxelarea.cpp b/src/unittest/test_voxelarea.cpp index 321420a52..f594a9be7 100644 --- a/src/unittest/test_voxelarea.cpp +++ b/src/unittest/test_voxelarea.cpp @@ -124,16 +124,17 @@ void TestVoxelArea::test_extent() void TestVoxelArea::test_volume() { VoxelArea v1(v3s16(-1337, -547, -789), v3s16(-147, 447, 669)); - UASSERTEQ(s32, v1.getVolume(), 1728980655); + UASSERTEQ(u32, v1.getVolume(), 1728980655); VoxelArea v2(v3s16(32493, -32507, 32752), v3s16(32508, -32492, 32767)); - UASSERTEQ(s32, v2.getVolume(), 4096); + UASSERTEQ(u32, v2.getVolume(), 4096); - VoxelArea v3({-20000, 12, 34}, {20000, 12, 34}); - UASSERTEQ(s32, v3.getVolume(), 40000); + // volume bigger than S32_MAX + VoxelArea v3({1, 1, 1}, {1337, 1337, 1337}); + UASSERTEQ(u32, v3.getVolume(), 2389979753U); - UASSERTEQ(s32, VoxelArea({2,3,4}, {1,2,3}).getVolume(), 0); - UASSERTEQ(s32, VoxelArea({2,3,4}, {2,2,3}).getVolume(), 0); + UASSERTEQ(u32, VoxelArea({2,3,4}, {1,2,3}).getVolume(), 0); + UASSERTEQ(u32, VoxelArea({2,3,4}, {2,2,3}).getVolume(), 0); } void TestVoxelArea::test_contains_voxelarea() diff --git a/src/voxel.cpp b/src/voxel.cpp index d9fceb2b9..8f3858a1f 100644 --- a/src/voxel.cpp +++ b/src/voxel.cpp @@ -137,7 +137,7 @@ void VoxelManipulator::addArea(const VoxelArea &area) new_area.addArea(area); } - s32 new_size = new_area.getVolume(); + u32 new_size = new_area.getVolume(); // Allocate new data and clear flags MapNode *new_data = new MapNode[new_size]; @@ -147,7 +147,7 @@ void VoxelManipulator::addArea(const VoxelArea &area) memset(new_flags, VOXELFLAG_NO_DATA, new_size); // Copy old data - s32 old_x_width = m_area.MaxEdge.X - m_area.MinEdge.X + 1; + u32 old_x_width = m_area.getExtent().X; for(s32 z=m_area.MinEdge.Z; z<=m_area.MaxEdge.Z; z++) for(s32 y=m_area.MinEdge.Y; y<=m_area.MaxEdge.Y; y++) { diff --git a/src/voxel.h b/src/voxel.h index a5ffc7ce6..a35be3e19 100644 --- a/src/voxel.h +++ b/src/voxel.h @@ -119,10 +119,10 @@ public: return !m_cache_extent.X || !m_cache_extent.Y || !m_cache_extent.Z; } - s32 getVolume() const + u32 getVolume() const { // FIXME: possible integer overflow here - return m_cache_extent.X * m_cache_extent.Y * m_cache_extent.Z; + return (u32)m_cache_extent.X * (u32)m_cache_extent.Y * (u32)m_cache_extent.Z; } bool contains(const VoxelArea &a) const @@ -148,8 +148,9 @@ public: } bool contains(s32 i) const { - return (i >= 0 && i < getVolume()); + return i >= 0 && static_cast(i) < getVolume(); } + bool operator==(const VoxelArea &other) const { return (MinEdge == other.MinEdge