@@ -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)
0 commit comments