Mapblock: nodecount refactor

Spare direct multoplication, use constant MapBlock::nodecount instead of
local nodecount variables.

Also use strides at one place instead of multiplications.
This commit is contained in:
est31 2015-05-31 06:23:10 +02:00
parent b4dfaa3a7a
commit 06a2eee692
2 changed files with 10 additions and 15 deletions

@ -132,7 +132,7 @@ MapNode MapBlock::getNodeParent(v3s16 p, bool *is_valid_position)
} }
if (is_valid_position) if (is_valid_position)
*is_valid_position = true; *is_valid_position = true;
return data[p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X]; return data[p.Z * zstride + p.Y * ystride + p.X];
} }
std::string MapBlock::getModifiedReasonString() std::string MapBlock::getModifiedReasonString()
@ -388,7 +388,7 @@ void MapBlock::actuallyUpdateDayNightDiff()
/* /*
Check if any lighting value differs Check if any lighting value differs
*/ */
for (u32 i = 0; i < MAP_BLOCKSIZE*MAP_BLOCKSIZE*MAP_BLOCKSIZE; i++) { for (u32 i = 0; i < nodecount; i++) {
MapNode &n = data[i]; MapNode &n = data[i];
differs = !n.isLightDayNightEq(nodemgr); differs = !n.isLightDayNightEq(nodemgr);
@ -402,7 +402,7 @@ void MapBlock::actuallyUpdateDayNightDiff()
*/ */
if (differs) { if (differs) {
bool only_air = true; bool only_air = true;
for (u32 i = 0; i < MAP_BLOCKSIZE*MAP_BLOCKSIZE*MAP_BLOCKSIZE; i++) { for (u32 i = 0; i < nodecount; i++) {
MapNode &n = data[i]; MapNode &n = data[i];
if (n.getContent() != CONTENT_AIR) { if (n.getContent() != CONTENT_AIR) {
only_air = false; only_air = false;
@ -473,8 +473,7 @@ static void getBlockNodeIdMapping(NameIdMapping *nimap, MapNode *nodes,
std::set<content_t> unknown_contents; std::set<content_t> unknown_contents;
content_t id_counter = 0; content_t id_counter = 0;
for(u32 i=0; i<MAP_BLOCKSIZE*MAP_BLOCKSIZE*MAP_BLOCKSIZE; i++) for (u32 i = 0; i < MapBlock::nodecount; i++) {
{
content_t global_id = nodes[i].getContent(); content_t global_id = nodes[i].getContent();
content_t id = CONTENT_IGNORE; content_t id = CONTENT_IGNORE;
@ -519,8 +518,7 @@ static void correctBlockNodeIds(const NameIdMapping *nimap, MapNode *nodes,
// correct ids. // correct ids.
std::set<content_t> unnamed_contents; std::set<content_t> unnamed_contents;
std::set<std::string> unallocatable_contents; std::set<std::string> unallocatable_contents;
for(u32 i=0; i<MAP_BLOCKSIZE*MAP_BLOCKSIZE*MAP_BLOCKSIZE; i++) for (u32 i = 0; i < MapBlock::nodecount; i++) {
{
content_t local_id = nodes[i].getContent(); content_t local_id = nodes[i].getContent();
std::string name; std::string name;
bool found = nimap->getName(local_id, name); bool found = nimap->getName(local_id, name);
@ -583,7 +581,6 @@ void MapBlock::serialize(std::ostream &os, u8 version, bool disk)
Bulk node data Bulk node data
*/ */
NameIdMapping nimap; NameIdMapping nimap;
u32 nodecount = MAP_BLOCKSIZE*MAP_BLOCKSIZE*MAP_BLOCKSIZE;
if(disk) if(disk)
{ {
MapNode *tmp_nodes = new MapNode[nodecount]; MapNode *tmp_nodes = new MapNode[nodecount];
@ -683,7 +680,6 @@ void MapBlock::deSerialize(std::istream &is, u8 version, bool disk)
*/ */
TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos()) TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
<<": Bulk node data"<<std::endl); <<": Bulk node data"<<std::endl);
u32 nodecount = MAP_BLOCKSIZE*MAP_BLOCKSIZE*MAP_BLOCKSIZE;
u8 content_width = readU8(is); u8 content_width = readU8(is);
u8 params_width = readU8(is); u8 params_width = readU8(is);
if(content_width != 1 && content_width != 2) if(content_width != 1 && content_width != 2)
@ -786,8 +782,6 @@ void MapBlock::deSerializeNetworkSpecific(std::istream &is)
void MapBlock::deSerialize_pre22(std::istream &is, u8 version, bool disk) void MapBlock::deSerialize_pre22(std::istream &is, u8 version, bool disk)
{ {
u32 nodecount = MAP_BLOCKSIZE*MAP_BLOCKSIZE*MAP_BLOCKSIZE;
// Initialize default flags // Initialize default flags
is_underground = false; is_underground = false;
m_day_night_differs = false; m_day_night_differs = false;

@ -145,9 +145,8 @@ public:
void reallocate() void reallocate()
{ {
delete[] data; delete[] data;
u32 datasize = MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE; data = new MapNode[nodecount];
data = new MapNode[datasize]; for (u32 i = 0; i < nodecount; i++)
for (u32 i = 0; i < datasize; i++)
data[i] = MapNode(CONTENT_IGNORE); data[i] = MapNode(CONTENT_IGNORE);
raiseModified(MOD_STATE_WRITE_NEEDED, MOD_REASON_REALLOCATE); raiseModified(MOD_STATE_WRITE_NEEDED, MOD_REASON_REALLOCATE);
@ -294,7 +293,7 @@ public:
if (!*valid_position) if (!*valid_position)
return MapNode(CONTENT_IGNORE); return MapNode(CONTENT_IGNORE);
return data[z * MAP_BLOCKSIZE * MAP_BLOCKSIZE + y * MAP_BLOCKSIZE + x]; return data[z * zstride + y * ystride + x];
} }
inline MapNode getNode(v3s16 p, bool *valid_position) inline MapNode getNode(v3s16 p, bool *valid_position)
@ -553,6 +552,8 @@ public:
static const u32 ystride = MAP_BLOCKSIZE; static const u32 ystride = MAP_BLOCKSIZE;
static const u32 zstride = MAP_BLOCKSIZE * MAP_BLOCKSIZE; static const u32 zstride = MAP_BLOCKSIZE * MAP_BLOCKSIZE;
static const u32 nodecount = MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
private: private:
/* /*
Private member variables Private member variables