2626#include < cstddef>
2727#include < random>
2828#include < string_view>
29- #include < utility>
3029#include < vector>
3130
3231namespace NavMeshTool
@@ -78,8 +77,8 @@ namespace NavMeshTool
7877 public:
7978 std::atomic_size_t mExpected { 0 };
8079
81- explicit NavMeshTileConsumer (NavMeshDb&& db, bool removeUnusedTiles, bool writeBinaryLog)
82- : mDb(std::move(db) )
80+ explicit NavMeshTileConsumer (NavMeshDb& db, bool removeUnusedTiles, bool writeBinaryLog)
81+ : mDb(db )
8382 , mRemoveUnusedTiles(removeUnusedTiles)
8483 , mWriteBinaryLog(writeBinaryLog)
8584 , mTransaction(mDb .startTransaction(Sqlite3::TransactionMode::Immediate))
@@ -211,12 +210,6 @@ namespace NavMeshTool
211210 mTransaction .commit ();
212211 }
213212
214- void vacuum ()
215- {
216- const std::lock_guard lock (mMutex );
217- mDb .vacuum ();
218- }
219-
220213 void removeTilesOutsideRange (ESM::RefId worldspace, const TilesPositionsRange& range)
221214 {
222215 const std::lock_guard lock (mMutex );
@@ -233,7 +226,7 @@ namespace NavMeshTool
233226 std::size_t mDeleted = 0 ;
234227 Status mStatus = Status::Ok;
235228 mutable std::mutex mMutex ;
236- NavMeshDb mDb ;
229+ NavMeshDb& mDb ;
237230 const bool mRemoveUnusedTiles ;
238231 const bool mWriteBinaryLog ;
239232 Transaction mTransaction ;
@@ -254,45 +247,39 @@ namespace NavMeshTool
254247 };
255248 }
256249
257- Status generateAllNavMeshTiles (const AgentBounds& agentBounds, const Settings& settings, std:: size_t threadsNumber ,
258- bool removeUnusedTiles, bool writeBinaryLog, WorldspaceData& data, NavMeshDb&& db)
250+ Result generateAllNavMeshTiles (const AgentBounds& agentBounds, const Settings& settings, bool removeUnusedTiles ,
251+ bool writeBinaryLog, const WorldspaceData& data, NavMeshDb& db, SceneUtil::WorkQueue& workQueue )
259252 {
260- Log (Debug::Info) << " Generating navmesh tiles by " << threadsNumber << " parallel workers ..." ;
253+ Log (Debug::Info) << " Generating navmesh tiles for " << data. mWorldspace << " worldspace ..." ;
261254
262- SceneUtil::WorkQueue workQueue (threadsNumber);
263- auto navMeshTileConsumer
264- = std::make_shared<NavMeshTileConsumer>(std::move (db), removeUnusedTiles, writeBinaryLog);
265- std::size_t tiles = 0 ;
266- std::mt19937_64 random;
255+ auto navMeshTileConsumer = std::make_shared<NavMeshTileConsumer>(db, removeUnusedTiles, writeBinaryLog);
267256
268- for (const std::unique_ptr<WorldspaceNavMeshInput>& input : data.mNavMeshInputs )
269- {
270- const auto range = DetourNavigator::makeTilesPositionsRange (Misc::Convert::toOsgXY (input->mAabb .m_min ),
271- Misc::Convert::toOsgXY (input->mAabb .m_max ), settings.mRecast );
272-
273- if (removeUnusedTiles)
274- navMeshTileConsumer->removeTilesOutsideRange (input->mWorldspace , range);
257+ const auto range = DetourNavigator::makeTilesPositionsRange (
258+ Misc::Convert::toOsgXY (data.mAabb .m_min ), Misc::Convert::toOsgXY (data.mAabb .m_max ), settings.mRecast );
275259
276- std::vector<TilePosition> worldspaceTiles;
260+ if (removeUnusedTiles)
261+ navMeshTileConsumer->removeTilesOutsideRange (data.mWorldspace , range);
277262
278- DetourNavigator::getTilesPositions (
279- range, [&](const TilePosition& tilePosition) { worldspaceTiles.push_back (tilePosition); });
263+ std::vector<TilePosition> worldspaceTiles = data.mTiles ;
280264
281- tiles += worldspaceTiles.size ();
265+ {
266+ const std::size_t tiles = worldspaceTiles.size ();
282267
283268 if (writeBinaryLog)
284269 serializeToStderr (ExpectedTiles{ static_cast <std::uint64_t >(tiles) });
285270
286271 navMeshTileConsumer->mExpected = tiles;
272+ }
287273
274+ {
275+ std::mt19937_64 random;
288276 std::shuffle (worldspaceTiles.begin (), worldspaceTiles.end (), random);
289-
290- for (const TilePosition& tilePosition : worldspaceTiles)
291- workQueue.addWorkItem (new GenerateNavMeshTile (input->mWorldspace , tilePosition,
292- RecastMeshProvider (input->mTileCachedRecastMeshManager ), agentBounds, settings,
293- navMeshTileConsumer));
294277 }
295278
279+ for (const TilePosition& tilePosition : worldspaceTiles)
280+ workQueue.addWorkItem (new GenerateNavMeshTile (data.mWorldspace , tilePosition,
281+ RecastMeshProvider (*data.mTileCachedRecastMeshManager ), agentBounds, settings, navMeshTileConsumer));
282+
296283 const Status status = navMeshTileConsumer->wait ();
297284 if (status == Status::Ok)
298285 navMeshTileConsumer->commit ();
@@ -304,12 +291,9 @@ namespace NavMeshTool
304291 Log (Debug::Info) << " Generated navmesh for " << navMeshTileConsumer->getProvided () << " tiles, " << inserted
305292 << " are inserted, " << updated << " updated and " << deleted << " deleted" ;
306293
307- if (inserted + updated + deleted > 0 )
308- {
309- Log (Debug::Info) << " Vacuuming the database..." ;
310- navMeshTileConsumer->vacuum ();
311- }
312-
313- return status;
294+ return Result{
295+ .mStatus = status,
296+ .mNeedVacuum = inserted + updated + deleted > 0 ,
297+ };
314298 }
315299}
0 commit comments