Skip to content

Commit 86c0811

Browse files
committed
Planet: Split chunk creation and chunk generation
Allows to generate only empty chunks
1 parent 61c9cfb commit 86c0811

File tree

3 files changed

+62
-46
lines changed

3 files changed

+62
-46
lines changed

include/CommonLib/Planet.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ namespace tsom
3434
~Planet() = default;
3535

3636
Chunk& AddChunk(const BlockLibrary& blockLibrary, const ChunkIndices& indices, const Nz::FunctionRef<void(BlockIndex* blocks)>& initCallback = nullptr);
37+
void AddChunks(const BlockLibrary& blockLibrary, const Nz::Vector3ui& chunkCount);
3738

3839
GravityForce ComputeGravity(const Nz::Vector3f& position) const override;
3940
Nz::Vector3f ComputeUpDirection(const Nz::Vector3f& position) const;

src/CommonLib/Planet.cpp

Lines changed: 54 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,18 @@ namespace tsom
9292
return *it->second.chunk;
9393
}
9494

95+
void Planet::AddChunks(const BlockLibrary& blockLibrary, const Nz::Vector3ui& chunkCount)
96+
{
97+
for (int chunkZ = 0; chunkZ < chunkCount.z; ++chunkZ)
98+
{
99+
for (int chunkY = 0; chunkY < chunkCount.y; ++chunkY)
100+
{
101+
for (int chunkX = 0; chunkX < chunkCount.x; ++chunkX)
102+
AddChunk(blockLibrary, { chunkX - int(chunkCount.x / 2), chunkY - int(chunkCount.y / 2), chunkZ - int(chunkCount.z / 2) });
103+
}
104+
}
105+
}
106+
95107
auto Planet::ComputeGravity(const Nz::Vector3f& position) const -> GravityForce
96108
{
97109
constexpr float PlanetGravityCenterStartDecrease = 16.f;
@@ -488,62 +500,61 @@ namespace tsom
488500
sol::protected_function generationFunction;
489501
};
490502

491-
std::mutex threadMutex;
492-
std::unordered_map<std::thread::id, std::unique_ptr<ThreadState>> threadStates;
503+
struct GenerationContext
504+
{
505+
std::mutex threadMutex;
506+
std::unordered_map<std::thread::id, std::unique_ptr<ThreadState>> threadStates;
507+
};
508+
509+
auto context = std::make_shared<GenerationContext>();
493510

494-
for (int chunkZ = 0; chunkZ < chunkCount.z; ++chunkZ)
511+
ForEachChunk([=, &taskScheduler](const ChunkIndices& chunkIndices, Chunk& chunk)
495512
{
496-
for (int chunkY = 0; chunkY < chunkCount.y; ++chunkY)
513+
if (chunk.HasContent())
514+
return;
515+
516+
taskScheduler.AddTask([=, &chunk]
497517
{
498-
for (int chunkX = 0; chunkX < chunkCount.x; ++chunkX)
518+
ThreadState* currentThreadState = nullptr;
499519
{
500-
auto& chunk = AddChunk(blockLibrary, { chunkX - int(chunkCount.x / 2), chunkY - int(chunkCount.y / 2), chunkZ - int(chunkCount.z / 2) });
501-
taskScheduler.AddTask([&]
520+
std::unique_lock lock(context->threadMutex);
521+
auto id = std::this_thread::get_id();
522+
auto it = context->threadStates.find(id);
523+
if (it == context->threadStates.end())
502524
{
503-
ThreadState* currentThreadState = nullptr;
504-
{
505-
std::unique_lock lock(threadMutex);
506-
auto id = std::this_thread::get_id();
507-
auto it = threadStates.find(id);
508-
if (it == threadStates.end())
509-
{
510-
lock.unlock();
525+
lock.unlock();
511526

512-
std::unique_ptr<ThreadState> threadState = std::make_unique<ThreadState>(m_app);
513-
threadState->scriptingContext.RegisterLibrary<MathScriptingLibrary>();
514-
threadState->scriptingContext.RegisterLibrary<ChunkScriptingLibrary>();
527+
std::unique_ptr<ThreadState> threadState = std::make_unique<ThreadState>(m_app);
528+
threadState->scriptingContext.RegisterLibrary<MathScriptingLibrary>();
529+
threadState->scriptingContext.RegisterLibrary<ChunkScriptingLibrary>();
515530

516-
Nz::Result execResult = threadState->scriptingContext.LoadFile(fmt::format("scripts/planets/{}.lua", scriptName));
517-
if (!execResult)
518-
return;
531+
Nz::Result execResult = threadState->scriptingContext.LoadFile(fmt::format("scripts/planets/{}.lua", scriptName));
532+
if (!execResult)
533+
return;
519534

520-
threadState->generationFunction = execResult.GetValue();
535+
threadState->generationFunction = execResult.GetValue();
521536

522-
currentThreadState = threadState.get();
537+
currentThreadState = threadState.get();
523538

524-
lock.lock();
525-
threadStates.emplace(id, std::move(threadState));
526-
}
527-
else
528-
currentThreadState = it->second.get();
529-
}
539+
lock.lock();
540+
context->threadStates.emplace(id, std::move(threadState));
541+
}
542+
else
543+
currentThreadState = it->second.get();
544+
}
530545

531-
chunk.LockWrite();
532-
NAZARA_DEFER({ chunk.UnlockWrite(); });
546+
chunk.LockWrite();
547+
NAZARA_DEFER({ chunk.UnlockWrite(); });
533548

534-
auto result = currentThreadState->generationFunction(chunk, seed, chunkCount);
535-
if (!result.valid())
536-
{
537-
sol::error err = result;
538-
fmt::print("chunk {};{};{} failed to generate: {}", chunkX, chunkY, chunkZ, err.what());
539-
return;
540-
}
541-
});
549+
auto result = currentThreadState->generationFunction(chunk, seed, chunkCount);
550+
if (!result.valid())
551+
{
552+
sol::error err = result;
553+
fmt::print("chunk {};{};{} failed to generate: {}", chunkIndices.x, chunkIndices.y, chunkIndices.z, err.what());
554+
return;
542555
}
543-
}
544-
}
545-
546-
taskScheduler.WaitForTasks();
556+
});
557+
});
547558
}
548559

549560
void Planet::GeneratePlatform(const BlockLibrary& blockLibrary, Direction upDirection, const BlockIndices& platformCenter)

src/ServerLib/ServerPlanetEnvironment.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,19 @@ namespace tsom
5656
auto& taskScheduler = app.GetComponent<Nz::TaskSchedulerAppComponent>();
5757

5858
auto& planetComponent = m_planetEntity.get<PlanetComponent>();
59+
planetComponent.planet->AddChunks(blockLibrary, chunkCount);
60+
61+
if (!m_savePath.empty())
62+
LoadFromDirectory();
63+
5964
planetComponent.planet->GenerateChunks(blockLibrary, taskScheduler, seed, chunkCount, "alice");
65+
taskScheduler.WaitForTasks();
66+
6067
planetComponent.planet->GeneratePlatform(blockLibrary, tsom::Direction::Right, { 65, -18, -39 });
6168
planetComponent.planet->GeneratePlatform(blockLibrary, tsom::Direction::Back, { -34, 2, 53 });
6269
planetComponent.planet->GeneratePlatform(blockLibrary, tsom::Direction::Front, { 22, -35, -59 });
6370
planetComponent.planet->GeneratePlatform(blockLibrary, tsom::Direction::Down, { 23, -62, 26 });
6471

65-
if (!m_savePath.empty())
66-
LoadFromDirectory();
67-
6872
planetComponent.planet->OnChunkUpdated.Connect([this](ChunkContainer* /*planet*/, Chunk* chunk, DirectionMask /*neighborMask*/)
6973
{
7074
m_dirtyChunks.insert(chunk->GetIndices());

0 commit comments

Comments
 (0)