Skip to content

Commit b14aa30

Browse files
Desoursfan5
authored andcommittedAug 23, 2019
Make Mapgen::spreadLight use a queue (#8838)
1 parent 9c72560 commit b14aa30

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed
 

Diff for: ‎src/mapgen/mapgen.cpp

+23-18
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3939
#include "serialization.h"
4040
#include "util/serialize.h"
4141
#include "util/numeric.h"
42+
#include "util/directiontables.h"
4243
#include "filesys.h"
4344
#include "log.h"
4445
#include "mapgen_carpathian.h"
@@ -435,7 +436,8 @@ void Mapgen::setLighting(u8 light, v3s16 nmin, v3s16 nmax)
435436
}
436437

437438

438-
void Mapgen::lightSpread(VoxelArea &a, v3s16 p, u8 light)
439+
void Mapgen::lightSpread(VoxelArea &a, std::queue<std::pair<v3s16, u8>> &queue,
440+
const v3s16 &p, u8 light)
439441
{
440442
if (light <= 1 || !a.contains(p))
441443
return;
@@ -455,8 +457,8 @@ void Mapgen::lightSpread(VoxelArea &a, v3s16 p, u8 light)
455457
// Bail out only if we have no more light from either bank to propogate, or
456458
// we hit a solid block that light cannot pass through.
457459
if ((light_day <= (n.param1 & 0x0F) &&
458-
light_night <= (n.param1 & 0xF0)) ||
459-
!ndef->get(n).light_propagates)
460+
light_night <= (n.param1 & 0xF0)) ||
461+
!ndef->get(n).light_propagates)
460462
return;
461463

462464
// Since this recursive function only terminates when there is no light from
@@ -467,12 +469,8 @@ void Mapgen::lightSpread(VoxelArea &a, v3s16 p, u8 light)
467469

468470
n.param1 = light;
469471

470-
lightSpread(a, p + v3s16(0, 0, 1), light);
471-
lightSpread(a, p + v3s16(0, 1, 0), light);
472-
lightSpread(a, p + v3s16(1, 0, 0), light);
473-
lightSpread(a, p - v3s16(0, 0, 1), light);
474-
lightSpread(a, p - v3s16(0, 1, 0), light);
475-
lightSpread(a, p - v3s16(1, 0, 0), light);
472+
// add to queue
473+
queue.emplace(p, light);
476474
}
477475

478476

@@ -525,9 +523,10 @@ void Mapgen::propagateSunlight(v3s16 nmin, v3s16 nmax, bool propagate_shadow)
525523
}
526524

527525

528-
void Mapgen::spreadLight(v3s16 nmin, v3s16 nmax)
526+
void Mapgen::spreadLight(const v3s16 &nmin, const v3s16 &nmax)
529527
{
530528
//TimeTaker t("spreadLight");
529+
std::queue<std::pair<v3s16, u8>> queue;
531530
VoxelArea a(nmin, nmax);
532531

533532
for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
@@ -551,18 +550,24 @@ void Mapgen::spreadLight(v3s16 nmin, v3s16 nmax)
551550

552551
u8 light = n.param1;
553552
if (light) {
554-
lightSpread(a, v3s16(x, y, z + 1), light);
555-
lightSpread(a, v3s16(x, y + 1, z ), light);
556-
lightSpread(a, v3s16(x + 1, y, z ), light);
557-
lightSpread(a, v3s16(x, y, z - 1), light);
558-
lightSpread(a, v3s16(x, y - 1, z ), light);
559-
lightSpread(a, v3s16(x - 1, y, z ), light);
553+
const v3s16 p(x, y, z);
554+
// spread to all 6 neighbor nodes
555+
for (const auto &dir : g_6dirs)
556+
lightSpread(a, queue, p + dir, light);
560557
}
561558
}
562559
}
563560
}
564561

565-
//printf("spreadLight: %dms\n", t.stop());
562+
while (!queue.empty()) {
563+
const auto &i = queue.front();
564+
// spread to all 6 neighbor nodes
565+
for (const auto &dir : g_6dirs)
566+
lightSpread(a, queue, i.first + dir, i.second);
567+
queue.pop();
568+
}
569+
570+
//printf("spreadLight: %lums\n", t.stop());
566571
}
567572

568573

@@ -877,7 +882,7 @@ void MapgenBasic::generateDungeons(s16 max_stone_y)
877882
return;
878883

879884
PseudoRandom ps(blockseed + 70033);
880-
885+
881886
DungeonParams dp;
882887

883888
dp.np_alt_wall =

Diff for: ‎src/mapgen/mapgen.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,12 @@ class Mapgen {
190190
void updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax);
191191

192192
void setLighting(u8 light, v3s16 nmin, v3s16 nmax);
193-
void lightSpread(VoxelArea &a, v3s16 p, u8 light);
193+
void lightSpread(VoxelArea &a, std::queue<std::pair<v3s16, u8>> &queue,
194+
const v3s16 &p, u8 light);
194195
void calcLighting(v3s16 nmin, v3s16 nmax, v3s16 full_nmin, v3s16 full_nmax,
195196
bool propagate_shadow = true);
196197
void propagateSunlight(v3s16 nmin, v3s16 nmax, bool propagate_shadow);
197-
void spreadLight(v3s16 nmin, v3s16 nmax);
198+
void spreadLight(const v3s16 &nmin, const v3s16 &nmax);
198199

199200
virtual void makeChunk(BlockMakeData *data) {}
200201
virtual int getGroundLevelAtPoint(v2s16 p) { return 0; }

0 commit comments

Comments
 (0)
Please sign in to comment.