2011-07-31 17:07:11 +02:00
|
|
|
#include "scene.hpp"
|
|
|
|
|
2021-07-11 14:43:52 +02:00
|
|
|
#include <atomic>
|
2020-06-25 21:46:07 +02:00
|
|
|
#include <chrono>
|
2015-06-03 23:04:35 +02:00
|
|
|
#include <limits>
|
2013-08-19 20:30:22 +02:00
|
|
|
|
2018-03-14 01:49:08 +03:00
|
|
|
#include <BulletCollision/CollisionDispatch/btCollisionObject.h>
|
|
|
|
|
2018-08-14 23:05:43 +04:00
|
|
|
#include <components/debug/debuglog.hpp>
|
2022-08-12 00:09:49 +02:00
|
|
|
#include <components/detournavigator/agentbounds.hpp>
|
2023-01-17 23:31:17 +01:00
|
|
|
#include <components/detournavigator/debug.hpp>
|
2021-07-14 20:57:52 +02:00
|
|
|
#include <components/detournavigator/heightfieldshape.hpp>
|
2018-03-14 01:49:08 +03:00
|
|
|
#include <components/detournavigator/navigator.hpp>
|
2023-04-21 01:45:21 +02:00
|
|
|
#include <components/detournavigator/updateguard.hpp>
|
2023-05-26 02:14:56 +02:00
|
|
|
#include <components/esm/esmterrain.hpp>
|
2022-09-08 21:08:59 +02:00
|
|
|
#include <components/esm/records.hpp>
|
2023-04-24 21:20:07 +02:00
|
|
|
#include <components/esm3/loadcell.hpp>
|
2015-07-09 19:22:04 +02:00
|
|
|
#include <components/loadinglistener/loadinglistener.hpp>
|
2019-02-28 20:03:42 +00:00
|
|
|
#include <components/misc/convert.hpp>
|
2015-01-12 11:29:56 +01:00
|
|
|
#include <components/misc/resourcehelpers.hpp>
|
2015-04-22 19:08:56 +02:00
|
|
|
#include <components/resource/resourcesystem.hpp>
|
2017-02-15 00:55:35 +01:00
|
|
|
#include <components/resource/scenemanager.hpp>
|
2020-05-10 13:37:00 +00:00
|
|
|
#include <components/sceneutil/positionattitudetransform.hpp>
|
2023-05-22 14:23:06 +02:00
|
|
|
#include <components/settings/values.hpp>
|
2015-04-12 15:34:50 +02:00
|
|
|
|
2012-04-23 15:27:03 +02:00
|
|
|
#include "../mwbase/environment.hpp"
|
2012-08-09 14:33:21 +02:00
|
|
|
#include "../mwbase/luamanager.hpp"
|
2012-08-11 17:30:55 +02:00
|
|
|
#include "../mwbase/mechanicsmanager.hpp"
|
2012-08-12 18:11:09 +02:00
|
|
|
#include "../mwbase/soundmanager.hpp"
|
|
|
|
#include "../mwbase/windowmanager.hpp"
|
2020-12-19 00:02:31 +01:00
|
|
|
#include "../mwbase/world.hpp"
|
2012-03-10 16:05:12 +01:00
|
|
|
|
2017-03-06 19:04:17 +01:00
|
|
|
#include "../mwrender/landmanager.hpp"
|
2022-05-13 18:58:00 -07:00
|
|
|
#include "../mwrender/postprocessor.hpp"
|
2015-04-12 15:34:50 +02:00
|
|
|
#include "../mwrender/renderingmanager.hpp"
|
|
|
|
|
2018-03-14 01:49:08 +03:00
|
|
|
#include "../mwphysics/actor.hpp"
|
|
|
|
#include "../mwphysics/heightfield.hpp"
|
|
|
|
#include "../mwphysics/object.hpp"
|
2015-05-10 01:09:00 +02:00
|
|
|
#include "../mwphysics/physicssystem.hpp"
|
|
|
|
|
2016-02-07 00:36:31 +01:00
|
|
|
#include "cellpreloader.hpp"
|
2022-05-06 21:03:02 +02:00
|
|
|
#include "cellstore.hpp"
|
2015-12-06 18:13:04 +01:00
|
|
|
#include "cellvisitors.hpp"
|
2013-01-05 21:12:08 -08:00
|
|
|
#include "class.hpp"
|
2012-10-01 19:17:04 +04:00
|
|
|
#include "esmstore.hpp"
|
2012-07-03 12:30:50 +02:00
|
|
|
#include "localscripts.hpp"
|
2011-07-31 17:07:11 +02:00
|
|
|
#include "player.hpp"
|
2014-10-02 16:30:23 +02:00
|
|
|
#include "worldimp.hpp"
|
2011-07-31 17:07:11 +02:00
|
|
|
|
2012-05-14 17:41:17 +02:00
|
|
|
namespace
|
2011-08-09 00:05:16 +02:00
|
|
|
{
|
2019-11-30 12:29:31 +01:00
|
|
|
using MWWorld::RotationOrder;
|
|
|
|
|
2018-08-26 23:27:38 +03:00
|
|
|
osg::Quat makeActorOsgQuat(const ESM::Position& position)
|
|
|
|
{
|
|
|
|
return osg::Quat(position.rot[2], osg::Vec3(0, 0, -1));
|
|
|
|
}
|
|
|
|
|
|
|
|
osg::Quat makeInversedOrderObjectOsgQuat(const ESM::Position& position)
|
|
|
|
{
|
|
|
|
const float xr = position.rot[0];
|
|
|
|
const float yr = position.rot[1];
|
|
|
|
const float zr = position.rot[2];
|
|
|
|
|
|
|
|
return osg::Quat(xr, osg::Vec3(-1, 0, 0)) * osg::Quat(yr, osg::Vec3(0, -1, 0))
|
|
|
|
* osg::Quat(zr, osg::Vec3(0, 0, -1));
|
|
|
|
}
|
2015-01-13 17:14:54 +01:00
|
|
|
|
2021-11-04 01:57:27 +01:00
|
|
|
osg::Quat makeInverseNodeRotation(const MWWorld::Ptr& ptr)
|
2017-02-04 15:12:01 +01:00
|
|
|
{
|
2021-02-05 14:55:42 +01:00
|
|
|
const auto pos = ptr.getRefData().getPosition();
|
2021-11-04 01:57:27 +01:00
|
|
|
return ptr.getClass().isActor() ? makeActorOsgQuat(pos) : makeInversedOrderObjectOsgQuat(pos);
|
|
|
|
}
|
2021-02-05 14:55:42 +01:00
|
|
|
|
2021-11-04 01:57:27 +01:00
|
|
|
osg::Quat makeDirectNodeRotation(const MWWorld::Ptr& ptr)
|
|
|
|
{
|
|
|
|
const auto pos = ptr.getRefData().getPosition();
|
|
|
|
return ptr.getClass().isActor() ? makeActorOsgQuat(pos) : Misc::Convert::makeOsgQuat(pos);
|
|
|
|
}
|
2021-02-05 14:55:42 +01:00
|
|
|
|
2021-11-04 01:57:27 +01:00
|
|
|
osg::Quat makeNodeRotation(const MWWorld::Ptr& ptr, RotationOrder order)
|
|
|
|
{
|
|
|
|
if (order == RotationOrder::inverse)
|
|
|
|
return makeInverseNodeRotation(ptr);
|
|
|
|
return makeDirectNodeRotation(ptr);
|
2021-02-05 14:55:42 +01:00
|
|
|
}
|
2021-01-23 10:43:32 +01:00
|
|
|
|
2021-08-15 19:50:28 +02:00
|
|
|
void setNodeRotation(const MWWorld::Ptr& ptr, MWRender::RenderingManager& rendering, const osg::Quat& rotation)
|
2021-02-05 14:55:42 +01:00
|
|
|
{
|
|
|
|
if (ptr.getRefData().getBaseNode())
|
|
|
|
rendering.rotateObject(ptr, rotation);
|
2017-02-04 15:12:01 +01:00
|
|
|
}
|
|
|
|
|
2023-01-14 04:36:06 +03:00
|
|
|
std::string getModel(const MWWorld::Ptr& ptr)
|
2020-05-11 13:37:00 +00:00
|
|
|
{
|
2022-10-03 19:49:20 +02:00
|
|
|
if (Misc::ResourceHelpers::isHiddenMarker(ptr.getCellRef().getRefId()))
|
2021-06-29 03:47:23 +02:00
|
|
|
return {};
|
2023-01-14 04:36:06 +03:00
|
|
|
return ptr.getClass().getModel(ptr);
|
2020-05-11 13:37:00 +00:00
|
|
|
}
|
|
|
|
|
2022-07-26 21:53:06 +02:00
|
|
|
void addObject(const MWWorld::Ptr& ptr, const MWWorld::World& world, const std::vector<ESM::RefNum>& pagedRefs,
|
|
|
|
MWPhysics::PhysicsSystem& physics, MWRender::RenderingManager& rendering)
|
2015-01-13 17:14:54 +01:00
|
|
|
{
|
2017-06-28 22:40:13 +02:00
|
|
|
if (ptr.getRefData().getBaseNode() || physics.getActor(ptr))
|
|
|
|
{
|
2018-08-14 23:05:43 +04:00
|
|
|
Log(Debug::Warning) << "Warning: Tried to add " << ptr.getCellRef().getRefId() << " to the scene twice";
|
2017-06-28 22:40:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-14 04:36:06 +03:00
|
|
|
std::string model = getModel(ptr);
|
2021-11-04 01:57:27 +01:00
|
|
|
const auto rotation = makeDirectNodeRotation(ptr);
|
2017-02-20 19:04:02 +01:00
|
|
|
|
2021-01-29 16:51:05 +04:00
|
|
|
const ESM::RefNum& refnum = ptr.getCellRef().getRefNum();
|
2022-07-26 21:53:06 +02:00
|
|
|
if (!refnum.hasContentFile() || !std::binary_search(pagedRefs.begin(), pagedRefs.end(), refnum))
|
2021-01-29 16:51:05 +04:00
|
|
|
ptr.getClass().insertObjectRendering(ptr, model, rendering);
|
2021-01-29 16:51:13 +04:00
|
|
|
else
|
|
|
|
ptr.getRefData().setBaseNode(
|
|
|
|
new SceneUtil::PositionAttitudeTransform); // FIXME remove this when physics code is fixed not to depend
|
|
|
|
// on basenode
|
2021-02-05 14:55:42 +01:00
|
|
|
setNodeRotation(ptr, rendering, rotation);
|
2015-06-16 20:36:48 +02:00
|
|
|
|
2021-02-05 14:55:42 +01:00
|
|
|
if (ptr.getClass().useAnim())
|
2021-01-29 16:51:05 +04:00
|
|
|
MWBase::Environment::get().getMechanicsManager()->add(ptr);
|
2017-02-20 19:04:02 +01:00
|
|
|
|
2021-01-29 16:51:05 +04:00
|
|
|
if (ptr.getClass().isActor())
|
|
|
|
rendering.addWaterRippleEmitter(ptr);
|
2018-03-17 13:41:13 +04:00
|
|
|
|
2021-01-29 16:51:05 +04:00
|
|
|
// Restore effect particles
|
2022-05-06 21:03:02 +02:00
|
|
|
world.applyLoopingParticles(ptr);
|
2021-02-05 14:55:42 +01:00
|
|
|
|
2022-04-03 17:38:33 +00:00
|
|
|
if (!model.empty())
|
|
|
|
ptr.getClass().insertObject(ptr, model, rotation, physics);
|
2020-12-19 00:02:31 +01:00
|
|
|
|
|
|
|
MWBase::Environment::get().getLuaManager()->objectAddedToScene(ptr);
|
2015-01-13 17:14:54 +01:00
|
|
|
}
|
|
|
|
|
2022-05-06 21:03:02 +02:00
|
|
|
void addObject(const MWWorld::Ptr& ptr, const MWWorld::World& world, const MWPhysics::PhysicsSystem& physics,
|
2022-09-05 09:23:14 +02:00
|
|
|
DetourNavigator::Navigator& navigator, const DetourNavigator::UpdateGuard* navigatorUpdateGuard = nullptr)
|
2018-08-26 23:27:38 +03:00
|
|
|
{
|
|
|
|
if (const auto object = physics.getObject(ptr))
|
|
|
|
{
|
2021-11-04 01:57:27 +01:00
|
|
|
const DetourNavigator::ObjectTransform objectTransform{ ptr.getRefData().getPosition(),
|
|
|
|
ptr.getCellRef().getScale() };
|
|
|
|
|
2018-08-26 23:27:38 +03:00
|
|
|
if (ptr.getClass().isDoor() && !ptr.getCellRef().getTeleport())
|
|
|
|
{
|
|
|
|
btVector3 aabbMin;
|
|
|
|
btVector3 aabbMax;
|
2021-10-30 03:30:38 +02:00
|
|
|
object->getShapeInstance()->mCollisionShape->getAabb(btTransform::getIdentity(), aabbMin, aabbMax);
|
2018-08-26 23:27:38 +03:00
|
|
|
|
|
|
|
const auto center = (aabbMax + aabbMin) * 0.5f;
|
|
|
|
|
2022-05-06 21:03:02 +02:00
|
|
|
const auto distanceFromDoor = world.getMaxActivationDistance() * 0.5f;
|
2018-08-26 23:27:38 +03:00
|
|
|
const auto toPoint = aabbMax.x() - aabbMin.x() < aabbMax.y() - aabbMin.y()
|
|
|
|
? btVector3(distanceFromDoor, 0, 0)
|
|
|
|
: btVector3(0, distanceFromDoor, 0);
|
|
|
|
|
2020-10-14 15:55:15 +02:00
|
|
|
const auto transform = object->getTransform();
|
2018-08-26 23:27:38 +03:00
|
|
|
const btTransform closedDoorTransform(
|
2021-06-28 23:35:34 +02:00
|
|
|
Misc::Convert::makeBulletQuaternion(ptr.getCellRef().getPosition()), transform.getOrigin());
|
2018-08-26 23:27:38 +03:00
|
|
|
|
2021-11-05 22:54:50 +01:00
|
|
|
const auto start = Misc::Convert::toOsg(closedDoorTransform(center + toPoint));
|
2018-08-26 23:27:38 +03:00
|
|
|
const auto startPoint = physics.castRay(start, start - osg::Vec3f(0, 0, 1000), ptr, {},
|
|
|
|
MWPhysics::CollisionType_World | MWPhysics::CollisionType_HeightMap
|
|
|
|
| MWPhysics::CollisionType_Water);
|
|
|
|
const auto connectionStart = startPoint.mHit ? startPoint.mHitPos : start;
|
|
|
|
|
2021-11-05 22:54:50 +01:00
|
|
|
const auto end = Misc::Convert::toOsg(closedDoorTransform(center - toPoint));
|
2018-08-26 23:27:38 +03:00
|
|
|
const auto endPoint = physics.castRay(end, end - osg::Vec3f(0, 0, 1000), ptr, {},
|
|
|
|
MWPhysics::CollisionType_World | MWPhysics::CollisionType_HeightMap
|
|
|
|
| MWPhysics::CollisionType_Water);
|
|
|
|
const auto connectionEnd = endPoint.mHit ? endPoint.mHitPos : end;
|
|
|
|
|
2018-09-22 18:36:57 +03:00
|
|
|
navigator.addObject(DetourNavigator::ObjectId(object),
|
2021-11-04 01:57:27 +01:00
|
|
|
DetourNavigator::DoorShapes(
|
|
|
|
object->getShapeInstance(), objectTransform, connectionStart, connectionEnd),
|
2022-09-05 09:23:14 +02:00
|
|
|
transform, navigatorUpdateGuard);
|
2018-08-26 23:27:38 +03:00
|
|
|
}
|
2022-08-25 00:14:17 +02:00
|
|
|
else if (object->getShapeInstance()->mVisualCollisionType == Resource::VisualCollisionType::None)
|
2018-08-26 23:27:38 +03:00
|
|
|
{
|
2018-09-22 18:36:57 +03:00
|
|
|
navigator.addObject(DetourNavigator::ObjectId(object),
|
2021-11-04 01:57:27 +01:00
|
|
|
DetourNavigator::ObjectShapes(object->getShapeInstance(), objectTransform), object->getTransform(),
|
2022-09-05 09:23:14 +02:00
|
|
|
navigatorUpdateGuard);
|
2018-08-26 23:27:38 +03:00
|
|
|
}
|
|
|
|
}
|
2019-03-03 14:58:03 +03:00
|
|
|
else if (physics.getActor(ptr))
|
2018-08-26 23:27:38 +03:00
|
|
|
{
|
2023-01-17 23:31:17 +01:00
|
|
|
const DetourNavigator::AgentBounds agentBounds = world.getPathfindingAgentBounds(ptr);
|
|
|
|
if (!navigator.addAgent(agentBounds))
|
|
|
|
Log(Debug::Warning) << "Agent bounds are not supported by navigator: " << agentBounds;
|
2018-08-26 23:27:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-06 18:13:04 +01:00
|
|
|
struct InsertVisitor
|
2014-02-23 21:21:27 +01:00
|
|
|
{
|
|
|
|
MWWorld::CellStore& mCell;
|
2021-09-24 22:23:55 +02:00
|
|
|
Loading::Listener* mLoadingListener;
|
2014-02-23 21:21:27 +01:00
|
|
|
|
2016-02-05 18:29:33 +01:00
|
|
|
std::vector<MWWorld::Ptr> mToInsert;
|
|
|
|
|
2021-09-30 22:58:16 +02:00
|
|
|
InsertVisitor(MWWorld::CellStore& cell, Loading::Listener* loadingListener);
|
2014-02-23 21:21:27 +01:00
|
|
|
|
|
|
|
bool operator()(const MWWorld::Ptr& ptr);
|
2018-08-26 23:27:38 +03:00
|
|
|
|
|
|
|
template <class AddObject>
|
|
|
|
void insert(AddObject&& addObject);
|
2014-02-23 21:21:27 +01:00
|
|
|
};
|
|
|
|
|
2021-09-30 22:58:16 +02:00
|
|
|
InsertVisitor::InsertVisitor(MWWorld::CellStore& cell, Loading::Listener* loadingListener)
|
|
|
|
: mCell(cell)
|
|
|
|
, mLoadingListener(loadingListener)
|
2014-02-23 21:21:27 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-12-06 18:13:04 +01:00
|
|
|
bool InsertVisitor::operator()(const MWWorld::Ptr& ptr)
|
2014-02-23 21:21:27 +01:00
|
|
|
{
|
2016-02-05 18:29:33 +01:00
|
|
|
// do not insert directly as we can't modify the cell from within the visitation
|
|
|
|
// CreatureLevList::insertObjectRendering may spawn a new creature
|
|
|
|
mToInsert.push_back(ptr);
|
|
|
|
return true;
|
|
|
|
}
|
2014-02-23 21:21:27 +01:00
|
|
|
|
2018-08-26 23:27:38 +03:00
|
|
|
template <class AddObject>
|
|
|
|
void InsertVisitor::insert(AddObject&& addObject)
|
2016-02-05 18:29:33 +01:00
|
|
|
{
|
2019-03-07 12:38:55 +04:00
|
|
|
for (MWWorld::Ptr& ptr : mToInsert)
|
2014-02-23 21:21:27 +01:00
|
|
|
{
|
2021-09-30 22:58:16 +02:00
|
|
|
if (!ptr.getRefData().isDeleted() && ptr.getRefData().isEnabled())
|
2014-02-23 21:21:27 +01:00
|
|
|
{
|
2016-02-05 18:29:33 +01:00
|
|
|
try
|
|
|
|
{
|
2018-08-26 23:27:38 +03:00
|
|
|
addObject(ptr);
|
2016-02-05 18:29:33 +01:00
|
|
|
}
|
|
|
|
catch (const std::exception& e)
|
|
|
|
{
|
2022-08-27 13:07:59 +02:00
|
|
|
Log(Debug::Error) << "failed to render '" << ptr.getCellRef().getRefId() << "': " << e.what();
|
2016-02-05 18:29:33 +01:00
|
|
|
}
|
2014-02-23 21:21:27 +01:00
|
|
|
}
|
|
|
|
|
2021-09-24 22:23:55 +02:00
|
|
|
if (mLoadingListener != nullptr)
|
|
|
|
mLoadingListener->increaseProgress(1);
|
2016-02-05 18:29:33 +01:00
|
|
|
}
|
2014-02-23 21:21:27 +01:00
|
|
|
}
|
2015-12-04 19:46:02 +01:00
|
|
|
|
2018-04-01 16:54:10 +03:00
|
|
|
int getCellPositionDistanceToOrigin(const std::pair<int, int>& cellPosition)
|
|
|
|
{
|
|
|
|
return std::abs(cellPosition.first) + std::abs(cellPosition.second);
|
|
|
|
}
|
|
|
|
|
2023-05-12 13:24:59 +02:00
|
|
|
bool isCellInCollection(ESM::ExteriorCellLocation cellIndex, MWWorld::Scene::CellStoreCollection& collection)
|
2021-02-05 14:55:42 +01:00
|
|
|
{
|
|
|
|
for (auto* cell : collection)
|
|
|
|
{
|
|
|
|
assert(cell->getCell()->isExterior());
|
2023-05-12 22:54:52 +02:00
|
|
|
if (cellIndex == cell->getCell()->getExteriorCellLocation())
|
2021-02-05 14:55:42 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2022-07-26 21:53:06 +02:00
|
|
|
|
|
|
|
bool removeFromSorted(const ESM::RefNum& refNum, std::vector<ESM::RefNum>& pagedRefs)
|
|
|
|
{
|
|
|
|
const auto it = std::lower_bound(pagedRefs.begin(), pagedRefs.end(), refNum);
|
|
|
|
if (it == pagedRefs.end() || *it != refNum)
|
|
|
|
return false;
|
|
|
|
pagedRefs.erase(it);
|
|
|
|
return true;
|
|
|
|
}
|
2011-08-09 00:05:16 +02:00
|
|
|
}
|
|
|
|
|
2011-07-31 17:07:11 +02:00
|
|
|
namespace MWWorld
|
|
|
|
{
|
2012-03-18 21:31:31 -04:00
|
|
|
|
2020-05-11 13:37:00 +00:00
|
|
|
void Scene::removeFromPagedRefs(const Ptr& ptr)
|
|
|
|
{
|
|
|
|
const ESM::RefNum& refnum = ptr.getCellRef().getRefNum();
|
2022-07-26 21:53:06 +02:00
|
|
|
if (refnum.hasContentFile() && removeFromSorted(refnum, mPagedRefs))
|
2020-05-11 13:37:00 +00:00
|
|
|
{
|
|
|
|
if (!ptr.getRefData().getBaseNode())
|
|
|
|
return;
|
2023-01-14 04:36:06 +03:00
|
|
|
ptr.getClass().insertObjectRendering(ptr, getModel(ptr), mRendering);
|
2021-02-05 14:55:42 +01:00
|
|
|
setNodeRotation(ptr, mRendering, makeNodeRotation(ptr, RotationOrder::direct));
|
2020-05-11 13:37:00 +00:00
|
|
|
reloadTerrain();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scene::updateObjectRotation(const Ptr& ptr, RotationOrder order)
|
2014-06-14 17:56:41 +02:00
|
|
|
{
|
2021-02-05 14:55:42 +01:00
|
|
|
const auto rot = makeNodeRotation(ptr, order);
|
|
|
|
setNodeRotation(ptr, mRendering, rot);
|
|
|
|
mPhysics->updateRotation(ptr, rot);
|
2014-06-14 17:56:41 +02:00
|
|
|
}
|
|
|
|
|
2015-04-24 14:49:20 +02:00
|
|
|
void Scene::updateObjectScale(const Ptr& ptr)
|
|
|
|
{
|
2020-05-11 13:37:00 +00:00
|
|
|
float scale = ptr.getCellRef().getScale();
|
|
|
|
osg::Vec3f scaleVec(scale, scale, scale);
|
|
|
|
ptr.getClass().adjustScale(ptr, scaleVec, true);
|
|
|
|
mRendering.scaleObject(ptr, scaleVec);
|
|
|
|
mPhysics->updateScale(ptr);
|
2015-04-24 14:49:20 +02:00
|
|
|
}
|
|
|
|
|
2022-05-06 20:12:17 +02:00
|
|
|
void Scene::update(float duration)
|
2014-05-11 15:17:25 +02:00
|
|
|
{
|
2021-09-06 00:40:13 +02:00
|
|
|
if (mChangeCellGridRequest.has_value())
|
|
|
|
{
|
2023-05-05 10:31:06 +02:00
|
|
|
changeCellGrid(mChangeCellGridRequest->mPosition, mChangeCellGridRequest->mCellIndex,
|
2023-04-23 00:12:24 +02:00
|
|
|
mChangeCellGridRequest->mChangeEvent);
|
2021-09-06 00:40:13 +02:00
|
|
|
mChangeCellGridRequest.reset();
|
|
|
|
}
|
|
|
|
|
2020-05-12 13:37:00 +00:00
|
|
|
mPreloader->updateCache(mRendering.getReferenceTime());
|
2020-05-12 13:37:00 +00:00
|
|
|
preloadCells(duration);
|
2011-12-26 19:23:46 -05:00
|
|
|
}
|
2012-03-18 21:31:31 -04:00
|
|
|
|
2022-09-05 09:23:14 +02:00
|
|
|
void Scene::unloadCell(CellStore* cell, const DetourNavigator::UpdateGuard* navigatorUpdateGuard)
|
2011-07-31 17:07:11 +02:00
|
|
|
{
|
2021-02-05 14:55:42 +01:00
|
|
|
if (mActiveCells.find(cell) == mActiveCells.end())
|
|
|
|
return;
|
2023-01-29 19:18:50 +01:00
|
|
|
Log(Debug::Info) << "Unloading cell " << cell->getCell()->getDescription();
|
2018-03-14 01:49:08 +03:00
|
|
|
|
2015-12-06 18:13:04 +01:00
|
|
|
ListAndResetObjectsVisitor visitor;
|
2013-03-10 10:00:20 +01:00
|
|
|
|
2021-02-05 14:55:42 +01:00
|
|
|
cell->forEach(visitor);
|
2018-03-14 01:49:08 +03:00
|
|
|
for (const auto& ptr : visitor.mObjects)
|
2011-11-21 12:52:28 +01:00
|
|
|
{
|
2018-03-14 01:49:08 +03:00
|
|
|
if (const auto object = mPhysics->getObject(ptr))
|
2021-02-05 14:55:42 +01:00
|
|
|
{
|
2022-08-25 00:14:17 +02:00
|
|
|
if (object->getShapeInstance()->mVisualCollisionType == Resource::VisualCollisionType::None)
|
2022-09-05 09:23:14 +02:00
|
|
|
mNavigator.removeObject(DetourNavigator::ObjectId(object), navigatorUpdateGuard);
|
2021-09-30 22:58:16 +02:00
|
|
|
mPhysics->remove(ptr);
|
|
|
|
ptr.mRef->mData.mPhysicsPostponed = false;
|
2021-02-05 14:55:42 +01:00
|
|
|
}
|
2019-03-10 14:24:03 +03:00
|
|
|
else if (mPhysics->getActor(ptr))
|
2018-07-21 13:37:02 +03:00
|
|
|
{
|
2022-06-17 00:28:44 +02:00
|
|
|
mNavigator.removeAgent(mWorld.getPathfindingAgentBounds(ptr));
|
2018-07-21 13:37:02 +03:00
|
|
|
mRendering.removeActorPath(ptr);
|
2021-02-05 14:55:42 +01:00
|
|
|
mPhysics->remove(ptr);
|
2018-07-21 13:37:02 +03:00
|
|
|
}
|
2020-12-19 00:02:31 +01:00
|
|
|
MWBase::Environment::get().getLuaManager()->objectRemovedFromScene(ptr);
|
2013-04-17 17:05:54 +02:00
|
|
|
}
|
2012-03-13 17:09:50 +01:00
|
|
|
|
2021-02-05 14:55:42 +01:00
|
|
|
const auto cellX = cell->getCell()->getGridX();
|
|
|
|
const auto cellY = cell->getCell()->getGridY();
|
2018-07-20 22:11:34 +03:00
|
|
|
|
2021-02-05 14:55:42 +01:00
|
|
|
if (cell->getCell()->isExterior())
|
2013-04-17 17:05:54 +02:00
|
|
|
{
|
2023-05-12 13:24:59 +02:00
|
|
|
if (mPhysics->getHeightField(ESM::ExteriorCellLocation(cellX, cellY, cell->getCell()->getWorldSpace()))
|
2023-05-05 10:31:06 +02:00
|
|
|
!= nullptr)
|
2022-09-05 09:23:14 +02:00
|
|
|
mNavigator.removeHeightfield(osg::Vec2i(cellX, cellY), navigatorUpdateGuard);
|
2021-09-30 22:58:16 +02:00
|
|
|
|
|
|
|
mPhysics->removeHeightField(cellX, cellY);
|
2011-11-19 01:01:19 -05:00
|
|
|
}
|
2012-03-18 21:31:31 -04:00
|
|
|
|
2021-02-05 14:55:42 +01:00
|
|
|
if (cell->getCell()->hasWater())
|
2022-09-05 09:23:14 +02:00
|
|
|
mNavigator.removeWater(osg::Vec2i(cellX, cellY), navigatorUpdateGuard);
|
2018-07-20 22:11:34 +03:00
|
|
|
|
2023-02-04 18:45:53 +01:00
|
|
|
ESM::visit(ESM::VisitOverload{
|
|
|
|
[&](const ESM::Cell& cell) {
|
|
|
|
if (const auto pathgrid = mWorld.getStore().get<ESM::Pathgrid>().search(cell))
|
|
|
|
mNavigator.removePathgrid(*pathgrid);
|
|
|
|
},
|
|
|
|
[&](const ESM4::Cell& cell) {},
|
|
|
|
},
|
|
|
|
*cell->getCell());
|
2020-06-11 23:23:30 +02:00
|
|
|
|
2021-02-05 14:55:42 +01:00
|
|
|
MWBase::Environment::get().getMechanicsManager()->drop(cell);
|
2015-05-22 00:55:43 +02:00
|
|
|
|
2021-02-05 14:55:42 +01:00
|
|
|
mRendering.removeCell(cell);
|
|
|
|
MWBase::Environment::get().getWindowManager()->removeCell(cell);
|
2012-01-23 14:33:06 +01:00
|
|
|
|
2022-05-06 21:03:02 +02:00
|
|
|
mWorld.getLocalScripts().clearCell(cell);
|
2013-11-30 10:50:02 +01:00
|
|
|
|
2021-02-05 14:55:42 +01:00
|
|
|
MWBase::Environment::get().getSoundManager()->stopSound(cell);
|
|
|
|
mActiveCells.erase(cell);
|
2022-07-30 10:59:27 +02:00
|
|
|
// Clean up any effects that may have been spawned while unloading all cells
|
|
|
|
if (mActiveCells.empty())
|
|
|
|
mRendering.notifyWorldSpaceChanged();
|
2011-07-31 17:07:11 +02:00
|
|
|
}
|
2011-08-01 03:33:02 +02:00
|
|
|
|
2023-04-20 16:16:17 +02:00
|
|
|
void Scene::loadCell(CellStore& cell, Loading::Listener* loadingListener, bool respawn, const osg::Vec3f& position,
|
2022-09-05 09:23:14 +02:00
|
|
|
const DetourNavigator::UpdateGuard* navigatorUpdateGuard)
|
2011-07-31 17:07:11 +02:00
|
|
|
{
|
2021-07-14 20:57:52 +02:00
|
|
|
using DetourNavigator::HeightfieldShape;
|
|
|
|
|
2023-04-20 16:16:17 +02:00
|
|
|
assert(mActiveCells.find(&cell) == mActiveCells.end());
|
|
|
|
mActiveCells.insert(&cell);
|
2021-02-05 14:55:42 +01:00
|
|
|
|
2023-04-20 16:16:17 +02:00
|
|
|
Log(Debug::Info) << "Loading cell " << cell.getCell()->getDescription();
|
2021-02-05 14:55:42 +01:00
|
|
|
|
2023-04-20 16:16:17 +02:00
|
|
|
const int cellX = cell.getCell()->getGridX();
|
|
|
|
const int cellY = cell.getCell()->getGridY();
|
|
|
|
const MWWorld::Cell& cellVariant = *cell.getCell();
|
2023-04-24 21:20:07 +02:00
|
|
|
ESM::RefId worldspace = cellVariant.getWorldSpace();
|
2023-05-12 13:24:59 +02:00
|
|
|
ESM::ExteriorCellLocation cellIndex(cellX, cellY, worldspace);
|
2023-01-22 19:03:19 +01:00
|
|
|
|
2023-01-27 14:07:50 +01:00
|
|
|
if (cellVariant.isExterior())
|
|
|
|
{
|
2023-05-05 10:31:06 +02:00
|
|
|
osg::ref_ptr<const ESMTerrain::LandObject> land = mRendering.getLandManager()->getLand(cellIndex);
|
2023-05-14 23:42:15 +02:00
|
|
|
const ESM::LandData* data = land ? land->getData(ESM::Land::DATA_VHGT) : nullptr;
|
2023-05-18 11:56:45 +02:00
|
|
|
const int verts = ESM::getLandSize(worldspace);
|
|
|
|
const int worldsize = ESM::getCellSize(worldspace);
|
|
|
|
|
2023-01-27 14:07:50 +01:00
|
|
|
if (data)
|
2021-07-14 20:57:52 +02:00
|
|
|
{
|
2023-05-14 23:42:15 +02:00
|
|
|
mPhysics->addHeightField(data->getHeights().data(), cellX, cellY, worldsize, verts,
|
|
|
|
data->getMinHeight(), data->getMaxHeight(), land.get());
|
2023-01-27 14:07:50 +01:00
|
|
|
}
|
2023-05-19 23:35:18 +02:00
|
|
|
else if (!ESM::isEsm4Ext(worldspace))
|
2023-01-27 14:07:50 +01:00
|
|
|
{
|
|
|
|
static std::vector<float> defaultHeight;
|
|
|
|
defaultHeight.resize(verts * verts, ESM::Land::DEFAULT_HEIGHT);
|
|
|
|
mPhysics->addHeightField(defaultHeight.data(), cellX, cellY, worldsize, verts,
|
|
|
|
ESM::Land::DEFAULT_HEIGHT, ESM::Land::DEFAULT_HEIGHT, land.get());
|
|
|
|
}
|
2023-05-05 10:31:06 +02:00
|
|
|
if (const auto heightField = mPhysics->getHeightField(cellIndex))
|
2023-01-27 14:07:50 +01:00
|
|
|
{
|
|
|
|
const osg::Vec2i cellPosition(cellX, cellY);
|
|
|
|
const btVector3& origin = heightField->getCollisionObject()->getWorldTransform().getOrigin();
|
|
|
|
const osg::Vec3f shift(origin.x(), origin.y(), origin.z());
|
|
|
|
const HeightfieldShape shape = [&]() -> HeightfieldShape {
|
|
|
|
if (data == nullptr)
|
|
|
|
{
|
|
|
|
return DetourNavigator::HeightfieldPlane{ static_cast<float>(ESM::Land::DEFAULT_HEIGHT) };
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DetourNavigator::HeightfieldSurface heights;
|
2023-05-14 23:42:15 +02:00
|
|
|
heights.mHeights = data->getHeights().data();
|
|
|
|
heights.mSize = static_cast<std::size_t>(data->getLandSize());
|
|
|
|
heights.mMinHeight = data->getMinHeight();
|
|
|
|
heights.mMaxHeight = data->getMaxHeight();
|
2023-01-27 14:07:50 +01:00
|
|
|
return heights;
|
|
|
|
}
|
|
|
|
}();
|
2023-05-20 00:31:38 +02:00
|
|
|
mNavigator.addHeightfield(cellPosition, worldsize, shape, navigatorUpdateGuard);
|
2021-07-14 20:57:52 +02:00
|
|
|
}
|
2023-01-27 14:07:50 +01:00
|
|
|
}
|
2012-03-13 17:09:50 +01:00
|
|
|
|
2023-02-04 18:45:53 +01:00
|
|
|
ESM::visit(ESM::VisitOverload{
|
|
|
|
[&](const ESM::Cell& cell) {
|
|
|
|
if (const auto pathgrid = mWorld.getStore().get<ESM::Pathgrid>().search(cell))
|
|
|
|
mNavigator.addPathgrid(cell, *pathgrid);
|
|
|
|
},
|
|
|
|
[&](const ESM4::Cell& cell) {},
|
|
|
|
},
|
2023-04-20 16:16:17 +02:00
|
|
|
*cell.getCell());
|
2018-03-14 01:49:08 +03:00
|
|
|
|
2021-02-05 14:55:42 +01:00
|
|
|
// register local scripts
|
|
|
|
// do this before insertCell, to make sure we don't add scripts from levelled creature spawning twice
|
2023-04-20 16:16:17 +02:00
|
|
|
mWorld.getLocalScripts().addCell(&cell);
|
2018-07-20 22:11:34 +03:00
|
|
|
|
2021-02-05 14:55:42 +01:00
|
|
|
if (respawn)
|
2023-04-20 16:16:17 +02:00
|
|
|
cell.respawn();
|
2021-02-05 14:55:42 +01:00
|
|
|
|
2023-04-20 16:16:17 +02:00
|
|
|
insertCell(cell, loadingListener, navigatorUpdateGuard);
|
2021-01-23 20:59:24 +01:00
|
|
|
|
2023-04-20 16:16:17 +02:00
|
|
|
mRendering.addCell(&cell);
|
2021-09-24 22:23:55 +02:00
|
|
|
|
2023-04-20 16:16:17 +02:00
|
|
|
MWBase::Environment::get().getWindowManager()->addCell(&cell);
|
|
|
|
bool waterEnabled = cellVariant.hasWater() || cell.isExterior();
|
|
|
|
float waterLevel = cell.getWaterLevel();
|
2021-09-24 22:23:55 +02:00
|
|
|
mRendering.setWaterEnabled(waterEnabled);
|
|
|
|
if (waterEnabled)
|
2021-02-05 14:55:42 +01:00
|
|
|
{
|
2021-09-24 22:23:55 +02:00
|
|
|
mPhysics->enableWater(waterLevel);
|
|
|
|
mRendering.setWaterHeight(waterLevel);
|
2021-02-05 14:55:42 +01:00
|
|
|
|
2023-02-04 18:45:53 +01:00
|
|
|
if (cellVariant.isExterior())
|
2021-09-24 22:23:55 +02:00
|
|
|
{
|
2023-05-05 10:31:06 +02:00
|
|
|
if (const auto heightField = mPhysics->getHeightField(cellIndex))
|
2022-09-05 09:23:14 +02:00
|
|
|
mNavigator.addWater(
|
|
|
|
osg::Vec2i(cellX, cellY), ESM::Land::REAL_SIZE, waterLevel, navigatorUpdateGuard);
|
2012-03-28 22:46:29 +02:00
|
|
|
}
|
2021-02-05 14:55:42 +01:00
|
|
|
else
|
2021-08-13 18:01:16 +02:00
|
|
|
{
|
2022-09-05 09:23:14 +02:00
|
|
|
mNavigator.addWater(
|
|
|
|
osg::Vec2i(cellX, cellY), std::numeric_limits<int>::max(), waterLevel, navigatorUpdateGuard);
|
2021-08-13 18:01:16 +02:00
|
|
|
}
|
2021-09-24 22:23:55 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
mPhysics->disableWater();
|
2021-08-13 18:01:16 +02:00
|
|
|
|
2023-04-20 16:16:17 +02:00
|
|
|
if (!cell.isExterior() && !cellVariant.isQuasiExterior())
|
2023-01-22 23:40:55 +01:00
|
|
|
mRendering.configureAmbient(cellVariant);
|
2021-09-24 22:23:55 +02:00
|
|
|
|
2023-04-20 16:16:17 +02:00
|
|
|
mPreloader->notifyLoaded(&cell);
|
2021-02-05 14:55:42 +01:00
|
|
|
}
|
2019-11-24 17:40:19 +04:00
|
|
|
|
2017-02-09 04:03:38 +01:00
|
|
|
void Scene::clear()
|
2013-05-15 17:54:18 +02:00
|
|
|
{
|
2022-09-05 09:23:14 +02:00
|
|
|
auto navigatorUpdateGuard = mNavigator.makeUpdateGuard();
|
2021-09-30 22:58:16 +02:00
|
|
|
for (auto iter = mActiveCells.begin(); iter != mActiveCells.end();)
|
2021-02-05 14:55:42 +01:00
|
|
|
{
|
|
|
|
auto* cell = *iter++;
|
2022-09-05 09:23:14 +02:00
|
|
|
unloadCell(cell, navigatorUpdateGuard.get());
|
2021-02-05 14:55:42 +01:00
|
|
|
}
|
2022-09-05 09:23:14 +02:00
|
|
|
navigatorUpdateGuard.reset();
|
2013-05-15 17:54:18 +02:00
|
|
|
assert(mActiveCells.empty());
|
2018-10-09 10:21:12 +04:00
|
|
|
mCurrentCell = nullptr;
|
2017-02-09 04:03:38 +01:00
|
|
|
|
|
|
|
mPreloader->clear();
|
2013-05-15 17:54:18 +02:00
|
|
|
}
|
|
|
|
|
2019-06-13 13:37:00 +00:00
|
|
|
osg::Vec4i Scene::gridCenterToBounds(const osg::Vec2i& centerCell) const
|
|
|
|
{
|
|
|
|
return osg::Vec4i(centerCell.x() - mHalfGridSize, centerCell.y() - mHalfGridSize,
|
|
|
|
centerCell.x() + mHalfGridSize + 1, centerCell.y() + mHalfGridSize + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
osg::Vec2i Scene::getNewGridCenter(const osg::Vec3f& pos, const osg::Vec2i* currentGridCenter) const
|
|
|
|
{
|
2023-05-09 23:44:57 +02:00
|
|
|
ESM::RefId worldspace
|
|
|
|
= mCurrentCell ? mCurrentCell->getCell()->getWorldSpace() : ESM::Cell::sDefaultWorldspaceId;
|
2019-06-13 13:37:00 +00:00
|
|
|
if (currentGridCenter)
|
|
|
|
{
|
2023-05-10 00:20:05 +02:00
|
|
|
osg::Vec2 center = ESM::indexToPosition(
|
2023-05-12 13:24:59 +02:00
|
|
|
ESM::ExteriorCellLocation(currentGridCenter->x(), currentGridCenter->y(), worldspace), true);
|
2023-05-09 23:44:57 +02:00
|
|
|
float distance = std::max(std::abs(center.x() - pos.x()), std::abs(center.y() - pos.y()));
|
2023-05-12 12:04:04 +02:00
|
|
|
float cellSize = ESM::getCellSize(worldspace);
|
2023-05-05 23:20:02 +02:00
|
|
|
const float maxDistance = cellSize / 2 + mCellLoadingThreshold; // 1/2 cell size + threshold
|
2019-06-13 13:37:00 +00:00
|
|
|
if (distance <= maxDistance)
|
|
|
|
return *currentGridCenter;
|
|
|
|
}
|
2023-05-27 01:18:17 +02:00
|
|
|
ESM::ExteriorCellLocation cellPos = ESM::positionToExteriorCellLocation(pos.x(), pos.y(), worldspace);
|
2023-05-12 12:04:04 +02:00
|
|
|
return { cellPos.mX, cellPos.mY };
|
2019-06-13 13:37:00 +00:00
|
|
|
}
|
|
|
|
|
2015-04-12 15:34:50 +02:00
|
|
|
void Scene::playerMoved(const osg::Vec3f& pos)
|
2014-10-02 16:30:23 +02:00
|
|
|
{
|
2021-09-18 15:52:03 +02:00
|
|
|
if (!mCurrentCell || !mCurrentCell->isExterior())
|
2014-10-02 16:30:23 +02:00
|
|
|
return;
|
|
|
|
|
2019-06-13 13:37:00 +00:00
|
|
|
osg::Vec2i newCell = getNewGridCenter(pos, &mCurrentGridCenter);
|
|
|
|
if (newCell != mCurrentGridCenter)
|
2021-09-06 00:40:13 +02:00
|
|
|
requestChangeCellGrid(pos, newCell);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scene::requestChangeCellGrid(const osg::Vec3f& position, const osg::Vec2i& cell, bool changeEvent)
|
|
|
|
{
|
2023-05-05 10:31:06 +02:00
|
|
|
mChangeCellGridRequest = ChangeCellGridRequest{ position,
|
2023-05-12 13:24:59 +02:00
|
|
|
ESM::ExteriorCellLocation(cell.x(), cell.y(), mCurrentCell->getCell()->getWorldSpace()), changeEvent };
|
2014-10-02 16:30:23 +02:00
|
|
|
}
|
|
|
|
|
2023-05-12 13:24:59 +02:00
|
|
|
void Scene::changeCellGrid(const osg::Vec3f& pos, ESM::ExteriorCellLocation playerCellIndex, bool changeEvent)
|
2011-07-31 17:07:11 +02:00
|
|
|
{
|
2023-05-19 17:52:12 +02:00
|
|
|
mHalfGridSize
|
|
|
|
= isEsm4Ext(playerCellIndex.mWorldspace) ? Constants::ESM4CellGridRadius : Constants::CellGridRadius;
|
2022-09-05 09:23:14 +02:00
|
|
|
auto navigatorUpdateGuard = mNavigator.makeUpdateGuard();
|
2023-05-05 10:31:06 +02:00
|
|
|
int playerCellX = playerCellIndex.mX;
|
|
|
|
int playerCellY = playerCellIndex.mY;
|
2022-09-05 09:23:14 +02:00
|
|
|
|
2021-09-30 22:58:16 +02:00
|
|
|
for (auto iter = mActiveCells.begin(); iter != mActiveCells.end();)
|
2011-07-31 17:07:11 +02:00
|
|
|
{
|
2021-02-05 14:55:42 +01:00
|
|
|
auto* cell = *iter++;
|
2023-05-19 14:32:05 +02:00
|
|
|
if (cell->getCell()->isExterior() && cell->getCell()->getWorldSpace() == playerCellIndex.mWorldspace)
|
2021-01-23 20:59:24 +01:00
|
|
|
{
|
2021-02-05 14:55:42 +01:00
|
|
|
const auto dx = std::abs(playerCellX - cell->getCell()->getGridX());
|
|
|
|
const auto dy = std::abs(playerCellY - cell->getCell()->getGridY());
|
|
|
|
if (dx > mHalfGridSize || dy > mHalfGridSize)
|
2022-09-05 09:23:14 +02:00
|
|
|
unloadCell(cell, navigatorUpdateGuard.get());
|
2021-02-05 14:55:42 +01:00
|
|
|
}
|
|
|
|
else
|
2022-09-05 09:23:14 +02:00
|
|
|
unloadCell(cell, navigatorUpdateGuard.get());
|
2011-07-31 17:07:11 +02:00
|
|
|
}
|
2023-04-22 13:59:53 +02:00
|
|
|
mNavigator.setWorldspace(
|
2023-05-10 14:51:33 +02:00
|
|
|
mWorld.getWorldModel().getExterior(playerCellIndex).getCell()->getWorldSpace().serializeText(),
|
2022-11-06 21:06:32 +01:00
|
|
|
navigatorUpdateGuard.get());
|
2022-09-05 09:23:14 +02:00
|
|
|
mNavigator.updateBounds(pos, navigatorUpdateGuard.get());
|
2022-02-01 01:21:47 +01:00
|
|
|
|
2020-05-10 13:37:00 +00:00
|
|
|
mCurrentGridCenter = osg::Vec2i(playerCellX, playerCellY);
|
|
|
|
osg::Vec4i newGrid = gridCenterToBounds(mCurrentGridCenter);
|
|
|
|
mRendering.setActiveGrid(newGrid);
|
2023-05-19 00:16:58 +02:00
|
|
|
mRendering.enableTerrain(true, playerCellIndex.mWorldspace);
|
|
|
|
mPreloader->setTerrain(mRendering.getTerrain());
|
2021-09-16 20:11:19 +00:00
|
|
|
if (mRendering.pagingUnlockCache())
|
|
|
|
mPreloader->abortTerrainPreloadExcept(nullptr);
|
|
|
|
if (!mPreloader->isTerrainLoaded(std::make_pair(pos, newGrid), mRendering.getReferenceTime()))
|
2023-06-29 11:09:06 +02:00
|
|
|
preloadTerrain(pos, playerCellIndex.mWorldspace, true);
|
2020-05-10 13:37:00 +00:00
|
|
|
mPagedRefs.clear();
|
|
|
|
mRendering.getPagedRefnums(newGrid, mPagedRefs);
|
|
|
|
|
2018-04-01 16:48:25 +03:00
|
|
|
std::size_t refsToLoad = 0;
|
2022-05-06 21:03:02 +02:00
|
|
|
const auto cellsToLoad = [&](CellStoreCollection& collection, int range) -> std::vector<std::pair<int, int>> {
|
2021-02-05 14:55:42 +01:00
|
|
|
std::vector<std::pair<int, int>> cellsPositionsToLoad;
|
|
|
|
for (int x = playerCellX - range; x <= playerCellX + range; ++x)
|
2012-09-11 16:37:54 +02:00
|
|
|
{
|
2021-02-05 14:55:42 +01:00
|
|
|
for (int y = playerCellY - range; y <= playerCellY + range; ++y)
|
2021-01-29 16:51:05 +04:00
|
|
|
{
|
2023-05-12 13:24:59 +02:00
|
|
|
if (!isCellInCollection(ESM::ExteriorCellLocation(x, y, playerCellIndex.mWorldspace), collection))
|
2021-02-05 14:55:42 +01:00
|
|
|
{
|
2023-05-05 10:31:06 +02:00
|
|
|
refsToLoad += mWorld.getWorldModel().getExterior(playerCellIndex).count();
|
2021-02-05 14:55:42 +01:00
|
|
|
cellsPositionsToLoad.emplace_back(x, y);
|
|
|
|
}
|
2018-04-01 16:54:10 +03:00
|
|
|
}
|
2012-09-11 16:37:54 +02:00
|
|
|
}
|
2021-02-05 14:55:42 +01:00
|
|
|
return cellsPositionsToLoad;
|
|
|
|
};
|
2021-03-05 22:21:53 +01:00
|
|
|
|
2022-06-19 20:16:31 +02:00
|
|
|
addPostponedPhysicsObjects();
|
2021-03-05 22:21:53 +01:00
|
|
|
|
2021-02-05 14:55:42 +01:00
|
|
|
auto cellsPositionsToLoad = cellsToLoad(mActiveCells, mHalfGridSize);
|
2012-09-11 16:37:54 +02:00
|
|
|
|
2020-05-10 13:37:00 +00:00
|
|
|
Loading::Listener* loadingListener = MWBase::Environment::get().getWindowManager()->getLoadingScreen();
|
|
|
|
Loading::ScopedLoad load(loadingListener);
|
2023-03-22 22:54:42 +01:00
|
|
|
loadingListener->setLabel("#{OMWEngine:LoadingExterior}");
|
2013-08-27 15:48:13 +02:00
|
|
|
loadingListener->setProgressRange(refsToLoad);
|
|
|
|
|
2018-04-01 16:54:10 +03:00
|
|
|
const auto getDistanceToPlayerCell = [&](const std::pair<int, int>& cellPosition) {
|
|
|
|
return std::abs(cellPosition.first - playerCellX) + std::abs(cellPosition.second - playerCellY);
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto getCellPositionPriority = [&](const std::pair<int, int>& cellPosition) {
|
|
|
|
return std::make_pair(getDistanceToPlayerCell(cellPosition), getCellPositionDistanceToOrigin(cellPosition));
|
|
|
|
};
|
|
|
|
|
|
|
|
std::sort(cellsPositionsToLoad.begin(), cellsPositionsToLoad.end(),
|
|
|
|
[&](const std::pair<int, int>& lhs, const std::pair<int, int>& rhs) {
|
|
|
|
return getCellPositionPriority(lhs) < getCellPositionPriority(rhs);
|
|
|
|
});
|
|
|
|
|
2021-02-05 14:55:42 +01:00
|
|
|
for (const auto& [x, y] : cellsPositionsToLoad)
|
|
|
|
{
|
2023-05-12 13:24:59 +02:00
|
|
|
ESM::ExteriorCellLocation indexToLoad = { x, y, playerCellIndex.mWorldspace };
|
2023-05-05 10:31:06 +02:00
|
|
|
if (!isCellInCollection(indexToLoad, mActiveCells))
|
2018-04-01 16:54:10 +03:00
|
|
|
{
|
2023-05-05 10:31:06 +02:00
|
|
|
CellStore& cell = mWorld.getWorldModel().getExterior(indexToLoad);
|
2023-04-20 16:16:17 +02:00
|
|
|
loadCell(cell, loadingListener, changeEvent, pos, navigatorUpdateGuard.get());
|
2011-07-31 17:07:11 +02:00
|
|
|
}
|
2014-10-02 16:30:23 +02:00
|
|
|
}
|
2011-07-31 17:07:11 +02:00
|
|
|
|
2022-09-05 09:23:14 +02:00
|
|
|
mNavigator.update(pos, navigatorUpdateGuard.get());
|
|
|
|
|
|
|
|
navigatorUpdateGuard.reset();
|
|
|
|
|
2023-05-05 10:31:06 +02:00
|
|
|
CellStore& current = mWorld.getWorldModel().getExterior(playerCellIndex);
|
2023-04-20 15:55:40 +02:00
|
|
|
MWBase::Environment::get().getWindowManager()->changeCell(¤t);
|
2011-07-31 17:07:11 +02:00
|
|
|
|
2016-02-27 12:53:07 +01:00
|
|
|
if (changeEvent)
|
|
|
|
mCellChanged = true;
|
2021-05-05 19:23:07 +02:00
|
|
|
|
2021-09-18 15:52:03 +02:00
|
|
|
mCellLoaded = true;
|
2014-10-02 16:30:23 +02:00
|
|
|
}
|
2011-07-31 17:07:11 +02:00
|
|
|
|
2022-06-19 20:16:31 +02:00
|
|
|
void Scene::addPostponedPhysicsObjects()
|
|
|
|
{
|
|
|
|
for (const auto& cell : mActiveCells)
|
|
|
|
{
|
|
|
|
cell->forEach([&](const MWWorld::Ptr& ptr) {
|
|
|
|
if (ptr.mRef->mData.mPhysicsPostponed)
|
|
|
|
{
|
|
|
|
ptr.mRef->mData.mPhysicsPostponed = false;
|
|
|
|
if (ptr.mRef->mData.isEnabled() && ptr.mRef->mData.getCount() > 0)
|
|
|
|
{
|
2023-01-14 04:36:06 +03:00
|
|
|
std::string model = getModel(ptr);
|
2022-06-19 20:16:31 +02:00
|
|
|
if (!model.empty())
|
|
|
|
{
|
|
|
|
const auto rotation = makeNodeRotation(ptr, RotationOrder::direct);
|
|
|
|
ptr.getClass().insertObjectPhysics(ptr, model, rotation, *mPhysics);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-24 17:40:19 +04:00
|
|
|
void Scene::testExteriorCells()
|
|
|
|
{
|
|
|
|
// Note: temporary disable ICO to decrease memory usage
|
|
|
|
mRendering.getResourceSystem()->getSceneManager()->setIncrementalCompileOperation(nullptr);
|
|
|
|
|
|
|
|
mRendering.getResourceSystem()->setExpiryDelay(1.f);
|
|
|
|
|
2022-05-06 21:03:02 +02:00
|
|
|
const MWWorld::Store<ESM::Cell>& cells = mWorld.getStore().get<ESM::Cell>();
|
2019-11-24 17:40:19 +04:00
|
|
|
|
|
|
|
Loading::Listener* loadingListener = MWBase::Environment::get().getWindowManager()->getLoadingScreen();
|
|
|
|
Loading::ScopedLoad load(loadingListener);
|
|
|
|
loadingListener->setProgressRange(cells.getExtSize());
|
|
|
|
|
|
|
|
MWWorld::Store<ESM::Cell>::iterator it = cells.extBegin();
|
|
|
|
int i = 1;
|
2022-09-05 09:23:14 +02:00
|
|
|
auto navigatorUpdateGuard = mNavigator.makeUpdateGuard();
|
2019-11-24 17:40:19 +04:00
|
|
|
for (; it != cells.extEnd(); ++it)
|
|
|
|
{
|
2023-04-21 09:41:01 +04:00
|
|
|
loadingListener->setLabel("#{OMWEngine:TestingExteriorCells} (" + std::to_string(i) + "/"
|
|
|
|
+ std::to_string(cells.getExtSize()) + ")...");
|
2019-11-24 17:40:19 +04:00
|
|
|
|
2023-05-05 10:31:06 +02:00
|
|
|
CellStore& cell = mWorld.getWorldModel().getExterior(
|
2023-05-12 13:24:59 +02:00
|
|
|
ESM::ExteriorCellLocation(it->mData.mX, it->mData.mY, ESM::Cell::sDefaultWorldspaceId));
|
2023-05-10 14:51:33 +02:00
|
|
|
mNavigator.setWorldspace(cell.getCell()->getWorldSpace().serializeText(), navigatorUpdateGuard.get());
|
2022-09-04 20:06:52 +02:00
|
|
|
const osg::Vec3f position
|
|
|
|
= osg::Vec3f(it->mData.mX + 0.5f, it->mData.mY + 0.5f, 0) * Constants::CellSizeInUnits;
|
2022-09-05 09:23:14 +02:00
|
|
|
mNavigator.updateBounds(position, navigatorUpdateGuard.get());
|
2023-04-20 16:16:17 +02:00
|
|
|
loadCell(cell, nullptr, false, position, navigatorUpdateGuard.get());
|
2022-09-05 09:23:14 +02:00
|
|
|
|
|
|
|
mNavigator.update(position, navigatorUpdateGuard.get());
|
|
|
|
navigatorUpdateGuard.reset();
|
|
|
|
mNavigator.wait(DetourNavigator::WaitConditionType::requiredTilesPresent, nullptr);
|
|
|
|
navigatorUpdateGuard = mNavigator.makeUpdateGuard();
|
2019-11-24 17:40:19 +04:00
|
|
|
|
2021-09-30 22:58:16 +02:00
|
|
|
auto iter = mActiveCells.begin();
|
|
|
|
while (iter != mActiveCells.end())
|
2019-11-24 17:40:19 +04:00
|
|
|
{
|
|
|
|
if (it->isExterior() && it->mData.mX == (*iter)->getCell()->getGridX()
|
|
|
|
&& it->mData.mY == (*iter)->getCell()->getGridY())
|
|
|
|
{
|
2022-09-05 09:23:14 +02:00
|
|
|
unloadCell(*iter, navigatorUpdateGuard.get());
|
2019-11-24 17:40:19 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
mRendering.getResourceSystem()->updateCache(mRendering.getReferenceTime());
|
|
|
|
|
|
|
|
loadingListener->increaseProgress(1);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
mRendering.getResourceSystem()->getSceneManager()->setIncrementalCompileOperation(
|
|
|
|
mRendering.getIncrementalCompileOperation());
|
2023-05-22 14:23:06 +02:00
|
|
|
mRendering.getResourceSystem()->setExpiryDelay(Settings::cells().mCacheExpiryDelay);
|
2019-11-24 17:40:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Scene::testInteriorCells()
|
|
|
|
{
|
|
|
|
// Note: temporary disable ICO to decrease memory usage
|
|
|
|
mRendering.getResourceSystem()->getSceneManager()->setIncrementalCompileOperation(nullptr);
|
|
|
|
|
|
|
|
mRendering.getResourceSystem()->setExpiryDelay(1.f);
|
|
|
|
|
2022-05-06 21:03:02 +02:00
|
|
|
const MWWorld::Store<ESM::Cell>& cells = mWorld.getStore().get<ESM::Cell>();
|
2019-11-24 17:40:19 +04:00
|
|
|
|
|
|
|
Loading::Listener* loadingListener = MWBase::Environment::get().getWindowManager()->getLoadingScreen();
|
|
|
|
Loading::ScopedLoad load(loadingListener);
|
|
|
|
loadingListener->setProgressRange(cells.getIntSize());
|
|
|
|
|
|
|
|
int i = 1;
|
|
|
|
MWWorld::Store<ESM::Cell>::iterator it = cells.intBegin();
|
2022-09-05 09:23:14 +02:00
|
|
|
auto navigatorUpdateGuard = mNavigator.makeUpdateGuard();
|
2019-11-24 17:40:19 +04:00
|
|
|
for (; it != cells.intEnd(); ++it)
|
|
|
|
{
|
2023-04-21 09:41:01 +04:00
|
|
|
loadingListener->setLabel("#{OMWEngine:TestingInteriorCells} (" + std::to_string(i) + "/"
|
|
|
|
+ std::to_string(cells.getIntSize()) + ")...");
|
2019-11-24 17:40:19 +04:00
|
|
|
|
2023-04-20 15:55:40 +02:00
|
|
|
CellStore& cell = mWorld.getWorldModel().getInterior(it->mName);
|
2023-05-10 14:51:33 +02:00
|
|
|
mNavigator.setWorldspace(cell.getCell()->getWorldSpace().serializeText(), navigatorUpdateGuard.get());
|
2022-01-31 22:33:42 +01:00
|
|
|
ESM::Position position;
|
2022-05-06 21:03:02 +02:00
|
|
|
mWorld.findInteriorPosition(it->mName, position);
|
2022-09-05 09:23:14 +02:00
|
|
|
mNavigator.updateBounds(position.asVec3(), navigatorUpdateGuard.get());
|
2023-04-20 16:16:17 +02:00
|
|
|
loadCell(cell, nullptr, false, position.asVec3(), navigatorUpdateGuard.get());
|
2022-09-05 09:23:14 +02:00
|
|
|
|
|
|
|
mNavigator.update(position.asVec3(), navigatorUpdateGuard.get());
|
|
|
|
navigatorUpdateGuard.reset();
|
|
|
|
mNavigator.wait(DetourNavigator::WaitConditionType::requiredTilesPresent, nullptr);
|
|
|
|
navigatorUpdateGuard = mNavigator.makeUpdateGuard();
|
2019-11-24 17:40:19 +04:00
|
|
|
|
2021-09-30 22:58:16 +02:00
|
|
|
auto iter = mActiveCells.begin();
|
|
|
|
while (iter != mActiveCells.end())
|
2019-11-24 17:40:19 +04:00
|
|
|
{
|
|
|
|
assert(!(*iter)->getCell()->isExterior());
|
|
|
|
|
2023-04-02 22:00:39 +02:00
|
|
|
if (it->mName == (*iter)->getCell()->getNameId())
|
2019-11-24 17:40:19 +04:00
|
|
|
{
|
2022-09-05 09:23:14 +02:00
|
|
|
unloadCell(*iter, navigatorUpdateGuard.get());
|
2019-11-24 17:40:19 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
mRendering.getResourceSystem()->updateCache(mRendering.getReferenceTime());
|
|
|
|
|
|
|
|
loadingListener->increaseProgress(1);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
mRendering.getResourceSystem()->getSceneManager()->setIncrementalCompileOperation(
|
|
|
|
mRendering.getIncrementalCompileOperation());
|
2023-05-22 14:23:06 +02:00
|
|
|
mRendering.getResourceSystem()->setExpiryDelay(Settings::cells().mCacheExpiryDelay);
|
2019-11-24 17:40:19 +04:00
|
|
|
}
|
|
|
|
|
2023-04-20 16:16:17 +02:00
|
|
|
void Scene::changePlayerCell(CellStore& cell, const ESM::Position& pos, bool adjustPlayerPos)
|
2014-10-02 16:30:23 +02:00
|
|
|
{
|
2023-05-16 10:21:48 +02:00
|
|
|
mHalfGridSize = cell.getCell()->isEsm4() ? Constants::ESM4CellGridRadius : Constants::CellGridRadius;
|
2023-04-20 16:16:17 +02:00
|
|
|
mCurrentCell = &cell;
|
2011-07-31 17:07:11 +02:00
|
|
|
|
2023-05-14 22:00:13 +02:00
|
|
|
mRendering.enableTerrain(cell.isExterior(), cell.getCell()->getWorldSpace());
|
2017-03-09 02:17:25 +01:00
|
|
|
|
2022-05-06 21:03:02 +02:00
|
|
|
MWWorld::Ptr old = mWorld.getPlayerPtr();
|
2023-04-20 16:16:17 +02:00
|
|
|
mWorld.getPlayer().setCell(&cell);
|
2011-07-31 17:07:11 +02:00
|
|
|
|
2022-05-06 21:03:02 +02:00
|
|
|
MWWorld::Ptr player = mWorld.getPlayerPtr();
|
2015-05-21 23:54:39 +02:00
|
|
|
mRendering.updatePlayerPtr(player);
|
2011-07-31 17:07:11 +02:00
|
|
|
|
2022-07-29 23:16:21 +02:00
|
|
|
// The player is loaded before the scene and by default it is grounded, with the scene fully loaded,
|
|
|
|
// we validate and correct this. Only run once, during initial cell load.
|
2023-04-20 16:16:17 +02:00
|
|
|
if (old.mCell == &cell)
|
2022-07-29 23:16:21 +02:00
|
|
|
mPhysics->traceDown(player, player.getRefData().getPosition().asVec3(), 10.f);
|
|
|
|
|
2022-05-06 21:03:02 +02:00
|
|
|
if (adjustPlayerPos)
|
|
|
|
{
|
|
|
|
mWorld.moveObject(player, pos.asVec3());
|
|
|
|
mWorld.rotateObject(player, pos.asRotationVec3());
|
2011-07-31 17:07:11 +02:00
|
|
|
|
Some PVS-Studio and cppcheck fixes
cppcheck:
[apps/esmtool/record.cpp:697]: (performance) Prefer prefix ++/-- operators for non-primitive types.
[apps/esmtool/record.cpp:1126]: (performance) Prefer prefix ++/-- operators for non-primitive types.
[apps/esmtool/record.cpp:1138]: (performance) Prefer prefix ++/-- operators for non-primitive types.
[apps/niftest/niftest.cpp:36]: (performance) Function parameter 'filename' should be passed by reference.
[apps/niftest/niftest.cpp:41]: (performance) Function parameter 'filename' should be passed by reference.
[apps/opencs/model/prefs/boolsetting.cpp:25]: (warning) Possible leak in public function. The pointer 'mWidget' is not deallocated before it is allocated.
[apps/opencs/model/prefs/shortcuteventhandler.cpp:52]: (warning) Return value of std::remove() ignored. Elements remain in container.
[apps/openmw/mwstate/quicksavemanager.cpp:5]: (performance) Variable 'mSaveName' is assigned in constructor body. Consider performing initialization in initialization list.
PVS-Studio:
apps/opencs/model/filter/parser.cpp 582 warn V560 A part of conditional expression is always true: allowPredefined.
apps/opencs/view/world/referencecreator.cpp 67 warn V547 Expression '!errors.empty()' is always false.
apps/opencs/view/world/referencecreator.cpp 74 warn V547 Expression '!errors.empty()' is always false.
apps/opencs/view/doc/loader.cpp 170 warn V560 A part of conditional expression is always true: !completed.
apps/opencs/view/doc/loader.cpp 170 warn V560 A part of conditional expression is always true: !error.empty().
apps/opencs/model/tools/pathgridcheck.cpp 32 err V517 The use of 'if (A) {...} else if (A) {...}' pattern was detected. There is a probability of logical error presence. Check lines: 32, 34.
apps/opencs/model/world/refidadapterimp.cpp 1376 err V547 Expression 'subColIndex < 3' is always true.
apps/openmw/mwgui/widgets.hpp 318 warn V703 It is odd that the 'mEnableRepeat' field in derived class 'MWScrollBar' overwrites field in base class 'ScrollBar'. Check lines: widgets.hpp:318, MyGUI_ScrollBar.h:179.
apps/openmw/mwgui/widgets.hpp 319 warn V703 It is odd that the 'mRepeatTriggerTime' field in derived class 'MWScrollBar' overwrites field in base class 'ScrollBar'. Check lines: widgets.hpp:319, MyGUI_ScrollBar.h:180.
apps/openmw/mwgui/widgets.hpp 320 warn V703 It is odd that the 'mRepeatStepTime' field in derived class 'MWScrollBar' overwrites field in base class 'ScrollBar'. Check lines: widgets.hpp:320, MyGUI_ScrollBar.h:181
apps/openmw/mwmechanics/actors.cpp 1425 warn V547 Expression '!detected' is always true.
apps/openmw/mwmechanics/character.cpp 2155 err V547 Expression 'mode == 0' is always true.
apps/openmw/mwmechanics/character.cpp 1192 warn V592 The expression was enclosed by parentheses twice: ((expression)). One pair of parentheses is unnecessary or misprint is present.
apps/openmw/mwmechanics/character.cpp 521 warn V560 A part of conditional expression is always true: (idle == mIdleState).
apps/openmw/mwmechanics/pathfinding.cpp 317 err V547 Expression 'mPath.size() >= 2' is always true.
apps/openmw/mwscript/interpretercontext.cpp 409 warn V560 A part of conditional expression is always false: rank > 9.
apps/openmw/mwgui/windowbase.cpp 28 warn V560 A part of conditional expression is always true: !visible.
apps/openmw/mwgui/journalwindow.cpp 561 warn V547 Expression '!mAllQuests' is always false.
apps/openmw/mwgui/referenceinterface.cpp 18 warn V571 Recurring check. The '!mPtr.isEmpty()' condition was already verified in line 16.
apps/openmw/mwworld/scene.cpp 463 warn V547 Expression 'adjustPlayerPos' is always true.
apps/openmw/mwworld/worldimp.cpp 409 err V766 An item with the same key '"sCompanionShare"' has already been added.
apps/openmw/mwworld/cellstore.cpp 691 warn V519 The 'state.mWaterLevel' variable is assigned values twice successively. Perhaps this is a mistake. Check lines: 689, 691.
apps/openmw/mwworld/weather.cpp 1125 warn V519 The 'mResult.mParticleEffect' variable is assigned values twice successively. Perhaps this is a mistake. Check lines: 1123, 1125.
apps/openmw/mwworld/weather.cpp 1137 warn V519 The 'mResult.mParticleEffect' variable is assigned values twice successively. Perhaps this is a mistake. Check lines: 1135, 1137.
apps/wizard/unshield/unshieldworker.cpp 475 warn V728 An excessive check can be simplified. The '(A && B) || (!A && !B)' expression is equivalent to the 'bool(A) == bool(B)' expression.
apps/wizard/installationpage.cpp 163 warn V735 Possibly an incorrect HTML. The "</p" closing tag was encountered, while the "</span" tag was expected.
components/fontloader/fontloader.cpp 427 err V547 Expression 'i == 1' is always true.
components/nifosg/nifloader.cpp 282 warn V519 The 'created' variable is assigned values twice successively. Perhaps this is a mistake. Check lines: 278, 282.
components/esm/loadregn.cpp 119 err V586 The 'clear' function is called twice for deallocation of the same resource. Check lines: 112, 119.
components/esm/cellref.cpp 178 warn V581 The conditional expressions of the 'if' statements situated alongside each other are identical. Check lines: 175, 178.
components/esmterrain/storage.cpp 235 warn V560 A part of conditional expression is always true: colStart == 0.
components/esmterrain/storage.cpp 237 warn V560 A part of conditional expression is always true: rowStart == 0.
2018-04-09 18:55:16 +03:00
|
|
|
player.getClass().adjustPosition(player, true);
|
2014-10-02 16:30:23 +02:00
|
|
|
}
|
|
|
|
|
2020-06-05 18:22:53 +04:00
|
|
|
MWBase::Environment::get().getMechanicsManager()->updateCell(old, player);
|
|
|
|
MWBase::Environment::get().getWindowManager()->watchActor(player);
|
2014-10-02 16:30:23 +02:00
|
|
|
|
2017-06-25 12:47:57 +02:00
|
|
|
mPhysics->updatePtr(old, player);
|
|
|
|
|
2022-05-06 21:03:02 +02:00
|
|
|
mWorld.adjustSky();
|
2017-02-09 01:24:13 +01:00
|
|
|
|
2020-05-12 13:37:00 +00:00
|
|
|
mLastPlayerPos = player.getRefData().getPosition().asVec3();
|
2011-07-31 17:07:11 +02:00
|
|
|
}
|
2011-08-01 03:33:02 +02:00
|
|
|
|
2022-05-06 21:03:02 +02:00
|
|
|
Scene::Scene(MWWorld::World& world, MWRender::RenderingManager& rendering, MWPhysics::PhysicsSystem* physics,
|
2018-08-26 23:27:38 +03:00
|
|
|
DetourNavigator::Navigator& navigator)
|
2022-05-06 21:03:02 +02:00
|
|
|
: mCurrentCell(nullptr)
|
|
|
|
, mCellChanged(false)
|
|
|
|
, mWorld(world)
|
|
|
|
, mPhysics(physics)
|
|
|
|
, mRendering(rendering)
|
|
|
|
, mNavigator(navigator)
|
2016-02-07 07:37:56 -08:00
|
|
|
, mCellLoadingThreshold(1024.f)
|
2023-05-22 14:23:06 +02:00
|
|
|
, mPreloadDistance(Settings::cells().mPreloadDistance)
|
|
|
|
, mPreloadEnabled(Settings::cells().mPreloadEnabled)
|
|
|
|
, mPreloadExteriorGrid(Settings::cells().mPreloadExteriorGrid)
|
|
|
|
, mPreloadDoors(Settings::cells().mPreloadDoors)
|
|
|
|
, mPreloadFastTravel(Settings::cells().mPreloadFastTravel)
|
|
|
|
, mPredictionTime(Settings::cells().mPredictionTime)
|
2011-08-01 03:33:02 +02:00
|
|
|
{
|
2022-05-29 13:24:48 +02:00
|
|
|
mPreloader = std::make_unique<CellPreloader>(rendering.getResourceSystem(), physics->getShapeManager(),
|
|
|
|
rendering.getTerrain(), rendering.getLandManager());
|
2016-02-09 01:02:40 +01:00
|
|
|
mPreloader->setWorkQueue(mRendering.getWorkQueue());
|
2016-02-07 19:05:55 +01:00
|
|
|
|
2023-05-22 14:23:06 +02:00
|
|
|
rendering.getResourceSystem()->setExpiryDelay(Settings::cells().mCacheExpiryDelay);
|
2016-02-18 17:47:10 +01:00
|
|
|
|
2023-05-22 14:23:06 +02:00
|
|
|
mPreloader->setExpiryDelay(Settings::cells().mPreloadCellExpiryDelay);
|
|
|
|
mPreloader->setMinCacheSize(Settings::cells().mPreloadCellCacheMin);
|
|
|
|
mPreloader->setMaxCacheSize(Settings::cells().mPreloadCellCacheMax);
|
|
|
|
mPreloader->setPreloadInstances(Settings::cells().mPreloadInstances);
|
2011-08-01 03:33:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Scene::~Scene()
|
|
|
|
{
|
2021-07-11 14:43:52 +02:00
|
|
|
for (const osg::ref_ptr<SceneUtil::WorkItem>& v : mWorkItems)
|
|
|
|
v->abort();
|
|
|
|
|
|
|
|
for (const osg::ref_ptr<SceneUtil::WorkItem>& v : mWorkItems)
|
|
|
|
v->waitTillDone();
|
2011-08-01 03:33:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Scene::hasCellChanged() const
|
|
|
|
{
|
|
|
|
return mCellChanged;
|
|
|
|
}
|
2011-08-09 09:56:09 +02:00
|
|
|
|
2011-11-15 23:31:18 -05:00
|
|
|
const Scene::CellStoreCollection& Scene::getActiveCells() const
|
2011-08-01 04:06:38 +02:00
|
|
|
{
|
|
|
|
return mActiveCells;
|
|
|
|
}
|
2011-08-09 09:56:09 +02:00
|
|
|
|
2016-03-24 17:18:08 +01:00
|
|
|
void Scene::changeToInteriorCell(
|
2023-01-19 17:31:45 +01:00
|
|
|
std::string_view cellName, const ESM::Position& position, bool adjustPlayerPos, bool changeEvent)
|
2011-07-31 17:07:11 +02:00
|
|
|
{
|
2023-04-20 15:55:40 +02:00
|
|
|
CellStore& cell = mWorld.getWorldModel().getInterior(cellName);
|
2019-11-18 22:10:31 +04:00
|
|
|
bool useFading = (mCurrentCell != nullptr);
|
|
|
|
if (useFading)
|
|
|
|
MWBase::Environment::get().getWindowManager()->fadeScreenOut(0.5);
|
2013-02-28 17:54:42 +01:00
|
|
|
|
2013-08-27 15:48:13 +02:00
|
|
|
Loading::Listener* loadingListener = MWBase::Environment::get().getWindowManager()->getLoadingScreen();
|
2023-03-22 22:54:42 +01:00
|
|
|
loadingListener->setLabel("#{OMWEngine:LoadingInterior}");
|
2014-08-07 16:37:03 +02:00
|
|
|
Loading::ScopedLoad load(loadingListener);
|
2013-02-28 17:54:42 +01:00
|
|
|
|
2023-05-29 19:27:06 +02:00
|
|
|
if (mCurrentCell == &cell)
|
2012-09-11 16:37:54 +02:00
|
|
|
{
|
2022-05-06 21:03:02 +02:00
|
|
|
mWorld.moveObject(mWorld.getPlayerPtr(), position.asVec3());
|
|
|
|
mWorld.rotateObject(mWorld.getPlayerPtr(), position.asRotationVec3());
|
2013-04-03 23:55:57 +02:00
|
|
|
|
2016-03-24 17:18:08 +01:00
|
|
|
if (adjustPlayerPos)
|
2022-05-06 21:03:02 +02:00
|
|
|
mWorld.getPlayerPtr().getClass().adjustPosition(mWorld.getPlayerPtr(), true);
|
2014-08-01 16:25:41 +02:00
|
|
|
MWBase::Environment::get().getWindowManager()->fadeScreenIn(0.5);
|
2012-11-04 23:22:30 +00:00
|
|
|
return;
|
2012-09-11 16:37:54 +02:00
|
|
|
}
|
2013-03-10 10:00:20 +01:00
|
|
|
|
2018-08-14 23:05:43 +04:00
|
|
|
Log(Debug::Info) << "Changing to interior";
|
2011-07-31 17:07:11 +02:00
|
|
|
|
2022-09-05 09:23:14 +02:00
|
|
|
auto navigatorUpdateGuard = mNavigator.makeUpdateGuard();
|
|
|
|
|
2012-11-04 23:22:30 +00:00
|
|
|
// unload
|
2021-09-30 22:58:16 +02:00
|
|
|
for (auto iter = mActiveCells.begin(); iter != mActiveCells.end();)
|
2021-02-05 14:55:42 +01:00
|
|
|
{
|
2021-09-29 11:36:05 +04:00
|
|
|
auto* cellToUnload = *iter++;
|
2022-09-05 09:23:14 +02:00
|
|
|
unloadCell(cellToUnload, navigatorUpdateGuard.get());
|
2021-02-05 14:55:42 +01:00
|
|
|
}
|
|
|
|
assert(mActiveCells.empty());
|
2012-04-23 15:27:03 +02:00
|
|
|
|
2023-04-20 15:55:40 +02:00
|
|
|
loadingListener->setProgressRange(cell.count());
|
2013-08-27 15:48:13 +02:00
|
|
|
|
2023-05-10 14:51:33 +02:00
|
|
|
mNavigator.setWorldspace(cell.getCell()->getWorldSpace().serializeText(), navigatorUpdateGuard.get());
|
2022-09-05 09:23:14 +02:00
|
|
|
mNavigator.updateBounds(position.asVec3(), navigatorUpdateGuard.get());
|
2022-02-01 01:21:47 +01:00
|
|
|
|
2012-11-04 23:22:30 +00:00
|
|
|
// Load cell.
|
2020-05-10 13:37:00 +00:00
|
|
|
mPagedRefs.clear();
|
2023-04-20 16:16:17 +02:00
|
|
|
loadCell(cell, loadingListener, changeEvent, position.asVec3(), navigatorUpdateGuard.get());
|
2022-09-05 09:23:14 +02:00
|
|
|
|
|
|
|
navigatorUpdateGuard.reset();
|
2011-07-31 17:07:11 +02:00
|
|
|
|
2023-04-20 16:16:17 +02:00
|
|
|
changePlayerCell(cell, position, adjustPlayerPos);
|
2012-11-03 19:29:55 +00:00
|
|
|
|
2012-11-04 23:22:30 +00:00
|
|
|
// adjust fog
|
2023-01-27 01:13:17 +01:00
|
|
|
mRendering.configureFog(*mCurrentCell->getCell());
|
2013-03-10 10:00:20 +01:00
|
|
|
|
2011-07-31 17:07:11 +02:00
|
|
|
// Sky system
|
2022-05-06 21:03:02 +02:00
|
|
|
mWorld.adjustSky();
|
2011-07-31 17:07:11 +02:00
|
|
|
|
2016-02-27 12:53:07 +01:00
|
|
|
if (changeEvent)
|
|
|
|
mCellChanged = true;
|
|
|
|
|
2021-09-18 15:52:03 +02:00
|
|
|
mCellLoaded = true;
|
|
|
|
|
2019-11-18 22:10:31 +04:00
|
|
|
if (useFading)
|
|
|
|
MWBase::Environment::get().getWindowManager()->fadeScreenIn(0.5);
|
2014-10-02 16:30:23 +02:00
|
|
|
|
|
|
|
MWBase::Environment::get().getWindowManager()->changeCell(mCurrentCell);
|
2021-05-05 18:13:17 +02:00
|
|
|
|
2023-04-20 15:55:40 +02:00
|
|
|
MWBase::Environment::get().getWorld()->getPostProcessor()->setExteriorFlag(cell.getCell()->isQuasiExterior());
|
2011-07-31 17:07:11 +02:00
|
|
|
}
|
2011-08-01 03:33:02 +02:00
|
|
|
|
2023-02-19 19:19:11 +01:00
|
|
|
void Scene::changeToExteriorCell(
|
|
|
|
const ESM::RefId& extCellId, const ESM::Position& position, bool adjustPlayerPos, bool changeEvent)
|
2011-07-31 17:07:11 +02:00
|
|
|
{
|
|
|
|
|
2017-10-05 18:39:28 +02:00
|
|
|
if (changeEvent)
|
|
|
|
MWBase::Environment::get().getWindowManager()->fadeScreenOut(0.5);
|
2023-04-20 15:55:40 +02:00
|
|
|
CellStore& current = mWorld.getWorldModel().getCell(extCellId);
|
2023-02-19 19:19:11 +01:00
|
|
|
|
2023-04-20 15:55:40 +02:00
|
|
|
const osg::Vec2i cellIndex(current.getCell()->getGridX(), current.getCell()->getGridY());
|
2017-10-05 18:39:28 +02:00
|
|
|
|
2023-05-05 10:31:06 +02:00
|
|
|
changeCellGrid(position.asVec3(),
|
2023-05-12 13:24:59 +02:00
|
|
|
ESM::ExteriorCellLocation(cellIndex.x(), cellIndex.y(), current.getCell()->getWorldSpace()), changeEvent);
|
2014-10-02 16:30:23 +02:00
|
|
|
|
2023-04-20 16:16:17 +02:00
|
|
|
changePlayerCell(current, position, adjustPlayerPos);
|
2017-10-05 18:39:28 +02:00
|
|
|
|
|
|
|
if (changeEvent)
|
|
|
|
MWBase::Environment::get().getWindowManager()->fadeScreenIn(0.5);
|
2022-05-13 18:58:00 -07:00
|
|
|
|
|
|
|
MWBase::Environment::get().getWorld()->getPostProcessor()->setExteriorFlag(true);
|
2011-07-31 17:07:11 +02:00
|
|
|
}
|
2011-08-09 09:56:09 +02:00
|
|
|
|
2013-12-05 13:21:26 +01:00
|
|
|
CellStore* Scene::getCurrentCell()
|
2011-08-01 04:06:38 +02:00
|
|
|
{
|
|
|
|
return mCurrentCell;
|
|
|
|
}
|
2011-08-01 03:33:02 +02:00
|
|
|
|
|
|
|
void Scene::markCellAsUnchanged()
|
|
|
|
{
|
|
|
|
mCellChanged = false;
|
|
|
|
}
|
2011-08-27 10:30:38 +02:00
|
|
|
|
2022-09-05 09:23:14 +02:00
|
|
|
void Scene::insertCell(
|
|
|
|
CellStore& cell, Loading::Listener* loadingListener, const DetourNavigator::UpdateGuard* navigatorUpdateGuard)
|
2012-05-14 17:41:17 +02:00
|
|
|
{
|
2021-09-30 22:58:16 +02:00
|
|
|
InsertVisitor insertVisitor(cell, loadingListener);
|
2015-12-06 18:13:04 +01:00
|
|
|
cell.forEach(insertVisitor);
|
2022-07-26 21:53:06 +02:00
|
|
|
insertVisitor.insert(
|
|
|
|
[&](const MWWorld::Ptr& ptr) { addObject(ptr, mWorld, mPagedRefs, *mPhysics, mRendering); });
|
2022-09-05 09:23:14 +02:00
|
|
|
insertVisitor.insert(
|
|
|
|
[&](const MWWorld::Ptr& ptr) { addObject(ptr, mWorld, *mPhysics, mNavigator, navigatorUpdateGuard); });
|
2012-05-14 17:41:17 +02:00
|
|
|
}
|
2011-08-09 00:05:16 +02:00
|
|
|
|
2012-05-25 17:28:27 +02:00
|
|
|
void Scene::addObjectToScene(const Ptr& ptr)
|
|
|
|
{
|
2015-01-31 19:21:16 +01:00
|
|
|
try
|
|
|
|
{
|
2022-07-26 21:53:06 +02:00
|
|
|
addObject(ptr, mWorld, mPagedRefs, *mPhysics, mRendering);
|
2022-05-06 21:03:02 +02:00
|
|
|
addObject(ptr, mWorld, *mPhysics, mNavigator);
|
|
|
|
mWorld.scaleObject(ptr, ptr.getCellRef().getScale());
|
2015-01-31 19:21:16 +01:00
|
|
|
}
|
|
|
|
catch (std::exception& e)
|
|
|
|
{
|
2018-08-14 23:05:43 +04:00
|
|
|
Log(Debug::Error) << "failed to render '" << ptr.getCellRef().getRefId() << "': " << e.what();
|
2015-01-31 19:21:16 +01:00
|
|
|
}
|
2012-05-25 17:28:27 +02:00
|
|
|
}
|
2012-08-09 14:33:21 +02:00
|
|
|
|
2021-08-28 14:36:30 +02:00
|
|
|
void Scene::removeObjectFromScene(const Ptr& ptr, bool keepActive)
|
2012-05-25 17:28:27 +02:00
|
|
|
{
|
2021-08-28 14:36:30 +02:00
|
|
|
MWBase::Environment::get().getMechanicsManager()->remove(ptr, keepActive);
|
2022-08-01 04:03:00 +03:00
|
|
|
// You'd expect the sounds attached to the object to be stopped here
|
|
|
|
// because the object is nowhere to be heard, but in Morrowind, they're not.
|
|
|
|
// They're still stopped when the cell is unloaded
|
|
|
|
// or if the player moves away far from the object's position.
|
|
|
|
// Todd Howard, Who art in Bethesda, hallowed be Thy name.
|
2020-12-19 00:02:31 +01:00
|
|
|
MWBase::Environment::get().getLuaManager()->objectRemovedFromScene(ptr);
|
2018-03-14 01:49:08 +03:00
|
|
|
if (const auto object = mPhysics->getObject(ptr))
|
2018-04-21 15:27:25 +03:00
|
|
|
{
|
2022-08-25 00:14:17 +02:00
|
|
|
if (object->getShapeInstance()->mVisualCollisionType == Resource::VisualCollisionType::None)
|
2022-09-05 09:23:14 +02:00
|
|
|
mNavigator.removeObject(DetourNavigator::ObjectId(object), nullptr);
|
2018-04-21 15:27:25 +03:00
|
|
|
}
|
2019-03-10 14:24:03 +03:00
|
|
|
else if (mPhysics->getActor(ptr))
|
2018-04-22 19:22:00 +03:00
|
|
|
{
|
2022-06-17 00:28:44 +02:00
|
|
|
mNavigator.removeAgent(mWorld.getPathfindingAgentBounds(ptr));
|
2018-04-22 19:22:00 +03:00
|
|
|
}
|
2015-05-12 03:02:15 +02:00
|
|
|
mPhysics->remove(ptr);
|
2015-05-22 00:55:43 +02:00
|
|
|
mRendering.removeObject(ptr);
|
2015-06-16 20:36:48 +02:00
|
|
|
if (ptr.getClass().isActor())
|
|
|
|
mRendering.removeWaterRippleEmitter(ptr);
|
2020-05-10 13:37:00 +00:00
|
|
|
ptr.getRefData().setBaseNode(nullptr);
|
2012-05-25 17:28:27 +02:00
|
|
|
}
|
2012-07-26 16:14:11 +04:00
|
|
|
|
|
|
|
bool Scene::isCellActive(const CellStore& cell)
|
|
|
|
{
|
2023-05-29 19:27:06 +02:00
|
|
|
return mActiveCells.contains(&cell);
|
2012-07-26 16:14:11 +04:00
|
|
|
}
|
2014-04-29 15:27:49 +02:00
|
|
|
|
|
|
|
Ptr Scene::searchPtrViaActorId(int actorId)
|
|
|
|
{
|
|
|
|
for (CellStoreCollection::const_iterator iter(mActiveCells.begin()); iter != mActiveCells.end(); ++iter)
|
2023-01-17 01:21:37 +01:00
|
|
|
{
|
|
|
|
Ptr ptr = (*iter)->searchViaActorId(actorId);
|
|
|
|
if (!ptr.isEmpty())
|
2014-04-29 15:27:49 +02:00
|
|
|
return ptr;
|
2023-01-17 01:21:37 +01:00
|
|
|
}
|
2014-04-29 15:27:49 +02:00
|
|
|
return Ptr();
|
|
|
|
}
|
2016-02-07 05:13:46 -08:00
|
|
|
|
2017-02-15 00:55:35 +01:00
|
|
|
class PreloadMeshItem : public SceneUtil::WorkItem
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PreloadMeshItem(const std::string& mesh, Resource::SceneManager* sceneManager)
|
|
|
|
: mMesh(mesh)
|
|
|
|
, mSceneManager(sceneManager)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-10-16 22:18:54 +04:00
|
|
|
void doWork() override
|
2017-02-15 00:55:35 +01:00
|
|
|
{
|
2021-07-11 14:43:52 +02:00
|
|
|
if (mAborted)
|
|
|
|
return;
|
|
|
|
|
2017-02-15 00:55:35 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
mSceneManager->getTemplate(mMesh);
|
|
|
|
}
|
2021-04-19 15:43:00 +04:00
|
|
|
catch (std::exception&)
|
2017-02-15 00:55:35 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
2021-07-11 14:43:52 +02:00
|
|
|
|
|
|
|
void abort() override { mAborted = true; }
|
|
|
|
|
2017-02-15 00:55:35 +01:00
|
|
|
private:
|
|
|
|
std::string mMesh;
|
|
|
|
Resource::SceneManager* mSceneManager;
|
2021-07-11 14:43:52 +02:00
|
|
|
std::atomic_bool mAborted{ false };
|
2017-02-15 00:55:35 +01:00
|
|
|
};
|
|
|
|
|
2017-02-20 19:58:00 +01:00
|
|
|
void Scene::preload(const std::string& mesh, bool useAnim)
|
2017-02-15 00:55:35 +01:00
|
|
|
{
|
2017-02-20 19:58:00 +01:00
|
|
|
std::string mesh_ = mesh;
|
|
|
|
if (useAnim)
|
|
|
|
mesh_ = Misc::ResourceHelpers::correctActorModelPath(mesh_, mRendering.getResourceSystem()->getVFS());
|
|
|
|
|
|
|
|
if (!mRendering.getResourceSystem()->getSceneManager()->checkLoaded(mesh_, mRendering.getReferenceTime()))
|
2021-07-11 14:43:52 +02:00
|
|
|
{
|
|
|
|
osg::ref_ptr<PreloadMeshItem> item(
|
|
|
|
new PreloadMeshItem(mesh_, mRendering.getResourceSystem()->getSceneManager()));
|
|
|
|
mRendering.getWorkQueue()->addWorkItem(item);
|
|
|
|
const auto isDone = [](const osg::ref_ptr<SceneUtil::WorkItem>& v) { return v->isDone(); };
|
|
|
|
mWorkItems.erase(std::remove_if(mWorkItems.begin(), mWorkItems.end(), isDone), mWorkItems.end());
|
|
|
|
mWorkItems.emplace_back(std::move(item));
|
|
|
|
}
|
2017-02-15 00:55:35 +01:00
|
|
|
}
|
|
|
|
|
2017-02-09 01:24:13 +01:00
|
|
|
void Scene::preloadCells(float dt)
|
2016-02-07 07:37:56 -08:00
|
|
|
{
|
2020-05-12 13:37:00 +00:00
|
|
|
if (dt <= 1e-06)
|
|
|
|
return;
|
2019-06-13 13:37:00 +00:00
|
|
|
std::vector<PositionCellGrid> exteriorPositions;
|
2017-03-09 04:18:35 +01:00
|
|
|
|
2022-05-06 21:03:02 +02:00
|
|
|
const MWWorld::ConstPtr player = mWorld.getPlayerPtr();
|
2017-02-09 01:24:13 +01:00
|
|
|
osg::Vec3f playerPos = player.getRefData().getPosition().asVec3();
|
|
|
|
osg::Vec3f moved = playerPos - mLastPlayerPos;
|
2017-07-19 16:43:29 +02:00
|
|
|
osg::Vec3f predictedPos = playerPos + moved / dt * mPredictionTime;
|
2017-02-09 01:24:13 +01:00
|
|
|
|
2017-03-09 04:18:35 +01:00
|
|
|
if (mCurrentCell->isExterior())
|
2019-06-13 13:37:00 +00:00
|
|
|
exteriorPositions.emplace_back(
|
|
|
|
predictedPos, gridCenterToBounds(getNewGridCenter(predictedPos, &mCurrentGridCenter)));
|
2017-03-09 04:18:35 +01:00
|
|
|
|
2017-02-09 01:24:13 +01:00
|
|
|
mLastPlayerPos = playerPos;
|
|
|
|
|
2023-05-09 22:28:37 +02:00
|
|
|
if (mPreloadEnabled)
|
2017-03-09 21:45:38 +01:00
|
|
|
{
|
|
|
|
if (mPreloadDoors)
|
|
|
|
preloadTeleportDoorDestinations(playerPos, predictedPos, exteriorPositions);
|
|
|
|
if (mPreloadExteriorGrid)
|
|
|
|
preloadExteriorGrid(playerPos, predictedPos);
|
|
|
|
if (mPreloadFastTravel)
|
2017-03-12 00:47:06 +01:00
|
|
|
preloadFastTravelDestinations(playerPos, predictedPos, exteriorPositions);
|
2017-03-09 21:45:38 +01:00
|
|
|
}
|
2017-03-09 04:18:35 +01:00
|
|
|
|
|
|
|
mPreloader->setTerrainPreloadPositions(exteriorPositions);
|
2016-02-07 07:37:56 -08:00
|
|
|
}
|
|
|
|
|
2019-06-13 13:37:00 +00:00
|
|
|
void Scene::preloadTeleportDoorDestinations(
|
|
|
|
const osg::Vec3f& playerPos, const osg::Vec3f& predictedPos, std::vector<PositionCellGrid>& exteriorPositions)
|
2016-02-07 05:13:46 -08:00
|
|
|
{
|
|
|
|
std::vector<MWWorld::ConstPtr> teleportDoors;
|
2019-03-07 12:38:55 +04:00
|
|
|
for (const MWWorld::CellStore* cellStore : mActiveCells)
|
2016-02-07 05:13:46 -08:00
|
|
|
{
|
|
|
|
typedef MWWorld::CellRefList<ESM::Door>::List DoorList;
|
|
|
|
const DoorList& doors = cellStore->getReadOnlyDoors().mList;
|
2019-03-07 12:38:55 +04:00
|
|
|
for (auto& door : doors)
|
2016-02-07 05:13:46 -08:00
|
|
|
{
|
2019-03-07 12:38:55 +04:00
|
|
|
if (!door.mRef.getTeleport())
|
|
|
|
{
|
2016-02-07 05:13:46 -08:00
|
|
|
continue;
|
|
|
|
}
|
2020-10-17 12:26:35 +04:00
|
|
|
teleportDoors.emplace_back(&door, cellStore);
|
2016-02-07 05:13:46 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-07 12:38:55 +04:00
|
|
|
for (const MWWorld::ConstPtr& door : teleportDoors)
|
2016-02-07 05:13:46 -08:00
|
|
|
{
|
2017-02-09 01:24:13 +01:00
|
|
|
float sqrDistToPlayer = (playerPos - door.getRefData().getPosition().asVec3()).length2();
|
|
|
|
sqrDistToPlayer
|
|
|
|
= std::min(sqrDistToPlayer, (predictedPos - door.getRefData().getPosition().asVec3()).length2());
|
2016-02-07 05:13:46 -08:00
|
|
|
|
2016-02-07 07:37:56 -08:00
|
|
|
if (sqrDistToPlayer < mPreloadDistance * mPreloadDistance)
|
2016-02-07 05:13:46 -08:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2023-04-20 16:16:17 +02:00
|
|
|
preloadCell(mWorld.getWorldModel().getCell(door.getCellRef().getDestCell()));
|
2016-02-07 05:13:46 -08:00
|
|
|
}
|
2021-04-19 15:43:00 +04:00
|
|
|
catch (std::exception&)
|
2016-02-07 05:13:46 -08:00
|
|
|
{
|
|
|
|
// ignore error for now, would spam the log too much
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-07 07:37:56 -08:00
|
|
|
|
2017-02-09 01:24:13 +01:00
|
|
|
void Scene::preloadExteriorGrid(const osg::Vec3f& playerPos, const osg::Vec3f& predictedPos)
|
2016-02-07 07:37:56 -08:00
|
|
|
{
|
2022-05-06 21:03:02 +02:00
|
|
|
if (!mWorld.isCellExterior())
|
2016-02-07 07:37:56 -08:00
|
|
|
return;
|
|
|
|
|
|
|
|
int halfGridSizePlusOne = mHalfGridSize + 1;
|
|
|
|
|
|
|
|
int cellX, cellY;
|
2019-06-13 13:37:00 +00:00
|
|
|
cellX = mCurrentGridCenter.x();
|
|
|
|
cellY = mCurrentGridCenter.y();
|
2023-05-09 23:44:57 +02:00
|
|
|
ESM::RefId extWorldspace = mWorld.getCurrentWorldspace();
|
2016-02-07 07:37:56 -08:00
|
|
|
|
2023-05-12 12:04:04 +02:00
|
|
|
float cellSize = ESM::getCellSize(extWorldspace);
|
2016-02-07 07:37:56 -08:00
|
|
|
|
|
|
|
for (int dx = -halfGridSizePlusOne; dx <= halfGridSizePlusOne; ++dx)
|
|
|
|
{
|
|
|
|
for (int dy = -halfGridSizePlusOne; dy <= halfGridSizePlusOne; ++dy)
|
|
|
|
{
|
|
|
|
if (dy != halfGridSizePlusOne && dy != -halfGridSizePlusOne && dx != halfGridSizePlusOne
|
|
|
|
&& dx != -halfGridSizePlusOne)
|
|
|
|
continue; // only care about the outer (not yet loaded) part of the grid
|
2023-05-12 13:24:59 +02:00
|
|
|
ESM::ExteriorCellLocation cellIndex(cellX + dx, cellY + dy, extWorldspace);
|
2023-05-10 09:11:20 +02:00
|
|
|
osg::Vec2 thisCellCenter = ESM::indexToPosition(cellIndex, true);
|
2016-02-07 07:37:56 -08:00
|
|
|
|
2023-05-09 23:44:57 +02:00
|
|
|
float dist = std::max(
|
|
|
|
std::abs(thisCellCenter.x() - playerPos.x()), std::abs(thisCellCenter.y() - playerPos.y()));
|
2017-02-09 01:24:13 +01:00
|
|
|
dist = std::min(dist,
|
2023-05-09 23:44:57 +02:00
|
|
|
std::max(std::abs(thisCellCenter.x() - predictedPos.x()),
|
|
|
|
std::abs(thisCellCenter.y() - predictedPos.y())));
|
2023-05-05 23:20:02 +02:00
|
|
|
float loadDist = cellSize / 2 + cellSize - mCellLoadingThreshold + mPreloadDistance;
|
2016-02-07 07:37:56 -08:00
|
|
|
|
|
|
|
if (dist < loadDist)
|
2023-05-10 09:11:20 +02:00
|
|
|
preloadCell(mWorld.getWorldModel().getExterior(cellIndex));
|
2016-02-09 01:51:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-20 16:16:17 +02:00
|
|
|
void Scene::preloadCell(CellStore& cell, bool preloadSurrounding)
|
2016-02-09 01:51:23 +01:00
|
|
|
{
|
2023-04-20 16:16:17 +02:00
|
|
|
if (preloadSurrounding && cell.isExterior())
|
2016-02-09 01:51:23 +01:00
|
|
|
{
|
2023-04-20 16:16:17 +02:00
|
|
|
int x = cell.getCell()->getGridX();
|
|
|
|
int y = cell.getCell()->getGridY();
|
2016-03-29 00:48:47 +02:00
|
|
|
unsigned int numpreloaded = 0;
|
2016-02-09 01:51:23 +01:00
|
|
|
for (int dx = -mHalfGridSize; dx <= mHalfGridSize; ++dx)
|
|
|
|
{
|
|
|
|
for (int dy = -mHalfGridSize; dy <= mHalfGridSize; ++dy)
|
|
|
|
{
|
2023-05-05 10:31:06 +02:00
|
|
|
mPreloader->preload(mWorld.getWorldModel().getExterior(
|
2023-05-12 13:24:59 +02:00
|
|
|
ESM::ExteriorCellLocation(x + dx, y + dy, cell.getCell()->getWorldSpace())),
|
2023-04-22 13:59:53 +02:00
|
|
|
mRendering.getReferenceTime());
|
2016-03-29 00:48:47 +02:00
|
|
|
if (++numpreloaded >= mPreloader->getMaxCacheSize())
|
|
|
|
break;
|
2016-02-09 01:51:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2023-04-20 16:27:50 +02:00
|
|
|
mPreloader->preload(cell, mRendering.getReferenceTime());
|
2016-02-09 01:51:23 +01:00
|
|
|
}
|
|
|
|
|
2023-06-29 11:09:06 +02:00
|
|
|
void Scene::preloadTerrain(const osg::Vec3f& pos, ESM::RefId worldspace, bool sync)
|
2017-03-12 22:38:50 +01:00
|
|
|
{
|
2023-06-29 11:09:06 +02:00
|
|
|
ESM::ExteriorCellLocation cellPos = ESM::positionToExteriorCellLocation(pos.x(), pos.y(), worldspace);
|
2019-06-13 13:37:00 +00:00
|
|
|
std::vector<PositionCellGrid> vec;
|
2023-06-29 11:09:06 +02:00
|
|
|
vec.emplace_back(pos, gridCenterToBounds({ cellPos.mX, cellPos.mY }));
|
2022-09-03 16:41:35 +01:00
|
|
|
mPreloader->abortTerrainPreloadExcept(vec.data());
|
2017-03-12 22:38:50 +01:00
|
|
|
mPreloader->setTerrainPreloadPositions(vec);
|
2020-05-12 13:37:00 +00:00
|
|
|
if (!sync)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Loading::Listener* loadingListener = MWBase::Environment::get().getWindowManager()->getLoadingScreen();
|
|
|
|
Loading::ScopedLoad load(loadingListener);
|
2021-09-05 17:43:46 +02:00
|
|
|
|
2023-03-22 22:54:42 +01:00
|
|
|
loadingListener->setLabel("#{OMWEngine:InitializingData}");
|
2021-09-05 17:43:46 +02:00
|
|
|
|
2023-05-01 16:36:48 +02:00
|
|
|
mPreloader->syncTerrainLoad(*loadingListener);
|
2017-03-12 22:38:50 +01:00
|
|
|
}
|
|
|
|
|
2020-05-08 13:37:00 +00:00
|
|
|
void Scene::reloadTerrain()
|
|
|
|
{
|
|
|
|
mPreloader->setTerrainPreloadPositions(std::vector<PositionCellGrid>());
|
|
|
|
}
|
|
|
|
|
2016-02-09 01:51:23 +01:00
|
|
|
struct ListFastTravelDestinationsVisitor
|
|
|
|
{
|
|
|
|
ListFastTravelDestinationsVisitor(float preloadDist, const osg::Vec3f& playerPos)
|
|
|
|
: mPreloadDist(preloadDist)
|
|
|
|
, mPlayerPos(playerPos)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator()(const MWWorld::Ptr& ptr)
|
|
|
|
{
|
|
|
|
if ((ptr.getRefData().getPosition().asVec3() - mPlayerPos).length2() > mPreloadDist * mPreloadDist)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (ptr.getClass().isNpc())
|
|
|
|
{
|
|
|
|
const std::vector<ESM::Transport::Dest>& transport = ptr.get<ESM::NPC>()->mBase->mTransport.mList;
|
|
|
|
mList.insert(mList.begin(), transport.begin(), transport.end());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const std::vector<ESM::Transport::Dest>& transport = ptr.get<ESM::Creature>()->mBase->mTransport.mList;
|
|
|
|
mList.insert(mList.begin(), transport.begin(), transport.end());
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
float mPreloadDist;
|
|
|
|
osg::Vec3f mPlayerPos;
|
|
|
|
std::vector<ESM::Transport::Dest> mList;
|
|
|
|
};
|
|
|
|
|
2019-06-13 13:37:00 +00:00
|
|
|
void Scene::preloadFastTravelDestinations(const osg::Vec3f& playerPos, const osg::Vec3f& /*predictedPos*/,
|
2023-05-05 10:31:06 +02:00
|
|
|
std::vector<PositionCellGrid>& exteriorPositions) // ignore predictedPos here since opening dialogue with
|
|
|
|
// travel service takes extra time
|
2016-02-09 01:51:23 +01:00
|
|
|
{
|
2022-05-06 21:03:02 +02:00
|
|
|
const MWWorld::ConstPtr player = mWorld.getPlayerPtr();
|
2016-02-09 01:51:23 +01:00
|
|
|
ListFastTravelDestinationsVisitor listVisitor(mPreloadDistance, player.getRefData().getPosition().asVec3());
|
2023-05-09 22:28:37 +02:00
|
|
|
ESM::RefId extWorldspace = mWorld.getCurrentWorldspace();
|
2019-03-07 12:38:55 +04:00
|
|
|
for (MWWorld::CellStore* cellStore : mActiveCells)
|
2016-02-09 01:51:23 +01:00
|
|
|
{
|
|
|
|
cellStore->forEachType<ESM::NPC>(listVisitor);
|
|
|
|
cellStore->forEachType<ESM::Creature>(listVisitor);
|
|
|
|
}
|
|
|
|
|
2019-03-07 12:38:55 +04:00
|
|
|
for (ESM::Transport::Dest& dest : listVisitor.mList)
|
2016-02-09 01:51:23 +01:00
|
|
|
{
|
2019-03-07 12:38:55 +04:00
|
|
|
if (!dest.mCellName.empty())
|
2023-04-20 16:16:17 +02:00
|
|
|
preloadCell(mWorld.getWorldModel().getInterior(dest.mCellName));
|
2016-02-09 01:51:23 +01:00
|
|
|
else
|
|
|
|
{
|
2019-03-07 12:38:55 +04:00
|
|
|
osg::Vec3f pos = dest.mPos.asVec3();
|
2023-05-27 01:18:17 +02:00
|
|
|
const ESM::ExteriorCellLocation cellIndex
|
|
|
|
= ESM::positionToExteriorCellLocation(pos.x(), pos.y(), extWorldspace);
|
2023-05-12 12:04:04 +02:00
|
|
|
preloadCell(mWorld.getWorldModel().getExterior(cellIndex), true);
|
2019-06-13 13:37:00 +00:00
|
|
|
exteriorPositions.emplace_back(pos, gridCenterToBounds(getNewGridCenter(pos)));
|
2016-02-07 07:37:56 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-07-31 17:07:11 +02:00
|
|
|
}
|