Noise: Prevent unittest crash caused by division by zero
[oweals/minetest.git] / src / mapblock.cpp
index f52a5ee3fabdf488cd65e84f38a24e5bebf378a2..ca589479419438ab7df7ac56ad4822d0058730cc 100644 (file)
@@ -366,12 +366,19 @@ void MapBlock::actuallyUpdateDayNightDiff()
        /*
                Check if any lighting value differs
        */
+
+       MapNode previous_n(CONTENT_IGNORE);
        for (u32 i = 0; i < nodecount; i++) {
-               MapNode &n = data[i];
+               MapNode n = data[i];
+
+               // If node is identical to previous node, don't verify if it differs
+               if (n == previous_n)
+                       continue;
 
                differs = !n.isLightDayNightEq(nodemgr);
                if (differs)
                        break;
+               previous_n = n;
        }
 
        /*
@@ -495,7 +502,7 @@ static void correctBlockNodeIds(const NameIdMapping *nimap, MapNode *nodes,
        std::unordered_set<content_t> unnamed_contents;
        std::unordered_set<std::string> unallocatable_contents;
 
-       bool previous_was_found = false;
+       bool previous_exists = false;
        content_t previous_local_id = CONTENT_IGNORE;
        content_t previous_global_id = CONTENT_IGNORE;
 
@@ -505,7 +512,7 @@ static void correctBlockNodeIds(const NameIdMapping *nimap, MapNode *nodes,
                // apply directly previous resolved id
                // This permits to massively improve loading performance when nodes are similar
                // example: default:air, default:stone are massively present
-               if (previous_was_found && local_id == previous_local_id) {
+               if (previous_exists && local_id == previous_local_id) {
                        nodes[i].setContent(previous_global_id);
                        continue;
                }
@@ -513,7 +520,7 @@ static void correctBlockNodeIds(const NameIdMapping *nimap, MapNode *nodes,
                std::string name;
                if (!nimap->getName(local_id, name)) {
                        unnamed_contents.insert(local_id);
-                       previous_was_found = false;
+                       previous_exists = false;
                        continue;
                }
 
@@ -522,7 +529,7 @@ static void correctBlockNodeIds(const NameIdMapping *nimap, MapNode *nodes,
                        global_id = gamedef->allocateUnknownNodeId(name);
                        if (global_id == CONTENT_IGNORE) {
                                unallocatable_contents.insert(name);
-                               previous_was_found = false;
+                               previous_exists = false;
                                continue;
                        }
                }
@@ -531,7 +538,7 @@ static void correctBlockNodeIds(const NameIdMapping *nimap, MapNode *nodes,
                // Save previous node local_id & global_id result
                previous_local_id = local_id;
                previous_global_id = global_id;
-               previous_was_found = true;
+               previous_exists = true;
        }
 
        for (const content_t c: unnamed_contents) {