From 75561abfcadf7041804168664359a2a85f134765 Mon Sep 17 00:00:00 2001 From: "florent.teppe" Date: Tue, 9 May 2023 22:08:17 +0200 Subject: [PATCH] Factorises code and fixes preload bug. --- apps/openmw/mwphysics/physicssystem.cpp | 2 +- apps/openmw/mwrender/landmanager.cpp | 2 +- apps/openmw/mwworld/scene.cpp | 16 +++++++++------- apps/openmw/mwworld/worldimp.cpp | 20 +++++++++----------- apps/openmw/mwworld/worldimp.hpp | 2 +- apps/openmw/mwworld/worldmodel.cpp | 2 +- components/esm/util.hpp | 11 +++++++++++ components/misc/constants.hpp | 4 ---- 8 files changed, 33 insertions(+), 26 deletions(-) diff --git a/apps/openmw/mwphysics/physicssystem.cpp b/apps/openmw/mwphysics/physicssystem.cpp index a86e02ff06..dbc31fccb1 100644 --- a/apps/openmw/mwphysics/physicssystem.cpp +++ b/apps/openmw/mwphysics/physicssystem.cpp @@ -485,7 +485,7 @@ namespace MWPhysics const HeightField* PhysicsSystem::getHeightField(ESM::ExteriorCellIndex cellIndex) const { - if (cellIndex.mWorldspace != ESM::Cell::sDefaultWorldspaceId) + if (ESM::isEsm4Ext(cellIndex.mWorldspace)) return nullptr; const auto heightField = mHeightFields.find(std::make_pair(cellIndex.mX, cellIndex.mY)); if (heightField == mHeightFields.end()) diff --git a/apps/openmw/mwrender/landmanager.cpp b/apps/openmw/mwrender/landmanager.cpp index 3563f88ec7..a3248d21a7 100644 --- a/apps/openmw/mwrender/landmanager.cpp +++ b/apps/openmw/mwrender/landmanager.cpp @@ -20,7 +20,7 @@ namespace MWRender osg::ref_ptr LandManager::getLand(ESM::ExteriorCellIndex cellIndex) { - if (cellIndex.mWorldspace != ESM::Cell::sDefaultWorldspaceId) + if (ESM::isEsm4Ext(cellIndex.mWorldspace)) return osg::ref_ptr(nullptr); int x = cellIndex.mX; int y = cellIndex.mY; diff --git a/apps/openmw/mwworld/scene.cpp b/apps/openmw/mwworld/scene.cpp index ffb6be9958..dd55488719 100644 --- a/apps/openmw/mwworld/scene.cpp +++ b/apps/openmw/mwworld/scene.cpp @@ -511,13 +511,13 @@ namespace MWWorld osg::Vec2i Scene::getNewGridCenter(const osg::Vec3f& pos, const osg::Vec2i* currentGridCenter) const { - bool isEsm4Ext = mCurrentCell && mCurrentCell->getCell()->getWorldSpace() != ESM::Cell::sDefaultWorldspaceId; + bool isEsm4Ext = mCurrentCell && ESM::isEsm4Ext(mCurrentCell->getCell()->getWorldSpace()); if (currentGridCenter) { float centerX, centerY; mWorld.indexToPosition(currentGridCenter->x(), currentGridCenter->y(), centerX, centerY, true, isEsm4Ext); float distance = std::max(std::abs(centerX - pos.x()), std::abs(centerY - pos.y())); - float cellSize = Constants::getCellSize(isEsm4Ext); + float cellSize = ESM::getCellSize(isEsm4Ext); const float maxDistance = cellSize / 2 + mCellLoadingThreshold; // 1/2 cell size + threshold if (distance <= maxDistance) return *currentGridCenter; @@ -585,7 +585,7 @@ namespace MWWorld { for (int y = playerCellY - range; y <= playerCellY + range; ++y) { - if (!isCellInCollection(playerCellIndex, collection)) + if (!isCellInCollection(ESM::ExteriorCellIndex(x, y, playerCellIndex.mWorldspace), collection)) { refsToLoad += mWorld.getWorldModel().getExterior(playerCellIndex).count(); cellsPositionsToLoad.emplace_back(x, y); @@ -1102,7 +1102,7 @@ namespace MWWorld mLastPlayerPos = playerPos; - if (mPreloadEnabled && mCurrentCell->getCell()->getWorldSpace() == ESM::Cell::sDefaultWorldspaceId) + if (mPreloadEnabled && !ESM::isEsm4Ext(mCurrentCell->getCell()->getWorldSpace())) { if (mPreloadDoors) preloadTeleportDoorDestinations(playerPos, predictedPos, exteriorPositions); @@ -1166,8 +1166,10 @@ namespace MWWorld float centerX, centerY; mWorld.indexToPosition(cellX, cellY, centerX, centerY, true); - float cellSize = mWorld.getCurrentCellSize(); - bool esm4Ext = cellSize == Constants::ESM4CellSizeInUnits; + + ESM::RefId extWorldspace = mWorld.getCurrentWorldspace(); + bool esm4Ext = ESM::isEsm4Ext(extWorldspace); + float cellSize = ESM::getCellSize(esm4Ext); for (int dx = -halfGridSizePlusOne; dx <= halfGridSizePlusOne; ++dx) { @@ -1189,7 +1191,7 @@ namespace MWWorld if (dist < loadDist) preloadCell(mWorld.getWorldModel().getExterior( - ESM::ExteriorCellIndex(cellX + dx, cellY + dy, ESM::Cell::sDefaultWorldspaceId))); + ESM::ExteriorCellIndex(cellX + dx, cellY + dy, extWorldspace))); } } } diff --git a/apps/openmw/mwworld/worldimp.cpp b/apps/openmw/mwworld/worldimp.cpp index 9eeddc32e4..6ba7659f39 100644 --- a/apps/openmw/mwworld/worldimp.cpp +++ b/apps/openmw/mwworld/worldimp.cpp @@ -1374,9 +1374,8 @@ namespace MWWorld && !(ptr.getClass().isPersistent(ptr) && ptr.getClass().getCreatureStats(ptr).isDeathAnimationFinished()); if (force || !ptr.getClass().isActor() || (!isFlying(ptr) && !swims && isActorCollisionEnabled(ptr))) { - bool esm4Ext = ptr.getCell()->isExterior() - && ptr.getCell()->getCell()->getWorldSpace() != ESM::Cell::sDefaultWorldspaceId; - osg::Vec3f traced = mPhysics->traceDown(ptr, pos, Constants::getCellSize(esm4Ext)); + bool esm4Ext = ptr.getCell()->isExterior() && ESM::isEsm4Ext(ptr.getCell()->getCell()->getWorldSpace()); + osg::Vec3f traced = mPhysics->traceDown(ptr, pos, ESM::getCellSize(esm4Ext)); pos.z() = std::min(pos.z(), traced.z()); } @@ -1411,10 +1410,9 @@ namespace MWWorld if (!mPhysics->castRay(pos, targetPos, MWPhysics::CollisionType_World | MWPhysics::CollisionType_Door).mHit) break; } - bool esm4Ext = actor.getCell()->isExterior() - && actor.getCell()->getCell()->getWorldSpace() != ESM::Cell::sDefaultWorldspaceId; + bool esm4Ext = actor.getCell()->isExterior() && ESM::isEsm4Ext(actor.getCell()->getCell()->getWorldSpace()); targetPos.z() += distance / 2.f; // move up a bit to get out from geometry, will snap down later - osg::Vec3f traced = mPhysics->traceDown(actor, targetPos, Constants::getCellSize(esm4Ext)); + osg::Vec3f traced = mPhysics->traceDown(actor, targetPos, ESM::getCellSize(esm4Ext)); if (traced != pos) { esmPos.pos[0] = traced.x(); @@ -1500,7 +1498,7 @@ namespace MWWorld void World::indexToPosition(int cellX, int cellY, float& x, float& y, bool centre, bool esm4Ext) const { - const int cellSize = Constants::getCellSize(esm4Ext); + const int cellSize = ESM::getCellSize(esm4Ext); x = static_cast(cellSize * cellX); y = static_cast(cellSize * cellY); @@ -1910,12 +1908,12 @@ namespace MWWorld return false; } - float World::getCurrentCellSize() const + ESM::RefId World::getCurrentWorldspace() const { const CellStore* cellStore = mWorldScene->getCurrentCell(); if (cellStore) - return Constants::getCellSize(cellStore->getCell()->isEsm4()); - return Constants::getCellSize(false); + return cellStore->getCell()->getWorldSpace(); + return ESM::Cell::sDefaultWorldspaceId; } int World::getCurrentWeather() const @@ -2762,7 +2760,7 @@ namespace MWWorld { int x = ext->getGridX(); int y = ext->getGridY(); - bool esm4Ext = ext->getWorldSpace() != ESM::Cell::sDefaultWorldspaceId; + bool esm4Ext = ESM::isEsm4Ext(ext->getWorldSpace()); indexToPosition(x, y, pos.pos[0], pos.pos[1], true, esm4Ext); // Note: Z pos will be adjusted by adjustPosition later diff --git a/apps/openmw/mwworld/worldimp.hpp b/apps/openmw/mwworld/worldimp.hpp index 306372dbb1..7189c8b4be 100644 --- a/apps/openmw/mwworld/worldimp.hpp +++ b/apps/openmw/mwworld/worldimp.hpp @@ -246,7 +246,7 @@ namespace MWWorld bool isCellQuasiExterior() const override; - float getCurrentCellSize() const; + ESM::RefId getCurrentWorldspace() const; void getDoorMarkers(MWWorld::CellStore& cell, std::vector& out) override; ///< get a list of teleport door markers for a given cell, to be displayed on the local map diff --git a/apps/openmw/mwworld/worldmodel.cpp b/apps/openmw/mwworld/worldmodel.cpp index 0dc30569bc..16564b4e91 100644 --- a/apps/openmw/mwworld/worldmodel.cpp +++ b/apps/openmw/mwworld/worldmodel.cpp @@ -169,7 +169,7 @@ MWWorld::CellStore& MWWorld::WorldModel::getExterior(ESM::ExteriorCellIndex cell if (result == mExteriors.end()) { - if (cellIndex.mWorldspace == ESM::Cell::sDefaultWorldspaceId) + if (!ESM::isEsm4Ext(cellIndex.mWorldspace)) { const ESM::Cell* cell = mStore.get().search(cellIndex.mX, cellIndex.mY); diff --git a/components/esm/util.hpp b/components/esm/util.hpp index 8735b301f6..d6d9feb3c4 100644 --- a/components/esm/util.hpp +++ b/components/esm/util.hpp @@ -5,6 +5,8 @@ #include #include +#include +#include namespace ESM { @@ -69,6 +71,15 @@ namespace ESM friend struct std::hash; }; + static inline bool isEsm4Ext(ESM::RefId worldspaceId) + { + return worldspaceId != ESM::Cell::sDefaultWorldspaceId; + } + + static inline int getCellSize(bool isESM4Ext) + { + return isESM4Ext ? Constants::ESM4CellSizeInUnits : Constants::CellSizeInUnits; + } } namespace std diff --git a/components/misc/constants.hpp b/components/misc/constants.hpp index 07ea0b6ca8..545db87094 100644 --- a/components/misc/constants.hpp +++ b/components/misc/constants.hpp @@ -24,10 +24,6 @@ namespace Constants // Size of one exterior cell in game units constexpr int CellSizeInUnits = 8192; constexpr int ESM4CellSizeInUnits = 4096; - static inline int getCellSize(bool isESM4Ext) - { - return isESM4Ext ? ESM4CellSizeInUnits : CellSizeInUnits; - } // Size of active cell grid in cells (it is a square with the (2 * CellGridRadius + 1) cells side) constexpr int CellGridRadius = 1;