1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-02-22 12:39:59 +00:00

Use free function instead of virtual MWBase::World::positionToIndex

The virtual function does the same thing.

* Change return type to osg::Vec2i to avoid dependency on ESM3.
* Rename to positionToCellIndex to make it clear what is the index.
This commit is contained in:
elsid 2022-05-23 19:21:44 +02:00
parent 499e688289
commit 639bdd5801
No known key found for this signature in database
GPG Key ID: B845CB9FEE18AB40
11 changed files with 51 additions and 71 deletions

View File

@ -319,9 +319,6 @@ namespace MWBase
const = 0; const = 0;
///< Convert cell numbers to position. ///< Convert cell numbers to position.
virtual void positionToIndex (float x, float y, int &cellX, int &cellY) const = 0;
///< Convert position to cell numbers
virtual void queueMovement(const MWWorld::Ptr &ptr, const osg::Vec3f &velocity) = 0; virtual void queueMovement(const MWWorld::Ptr &ptr, const osg::Vec3f &velocity) = 0;
///< Queues movement for \a ptr (in local space), to be applied in the next call to ///< Queues movement for \a ptr (in local space), to be applied in the next call to
/// doPhysics. /// doPhysics.

View File

@ -19,6 +19,7 @@
#include "../mwworld/inventorystore.hpp" #include "../mwworld/inventorystore.hpp"
#include "../mwworld/actiontrap.hpp" #include "../mwworld/actiontrap.hpp"
#include "../mwworld/customdata.hpp" #include "../mwworld/customdata.hpp"
#include "../mwworld/cellutils.hpp"
#include "../mwgui/tooltips.hpp" #include "../mwgui/tooltips.hpp"
@ -303,10 +304,10 @@ namespace MWClass
if (dest.empty()) if (dest.empty())
{ {
// door leads to exterior, use cell name (if any), otherwise translated region name // door leads to exterior, use cell name (if any), otherwise translated region name
int x,y;
auto world = MWBase::Environment::get().getWorld(); auto world = MWBase::Environment::get().getWorld();
world->positionToIndex (door.mRef.getDoorDest().pos[0], door.mRef.getDoorDest().pos[1], x, y); const osg::Vec2i index = MWWorld::positionToCellIndex(door.mRef.getDoorDest().pos[0],
const ESM::Cell* cell = world->getStore().get<ESM::Cell>().search(x,y); door.mRef.getDoorDest().pos[1]);
const ESM::Cell* cell = world->getStore().get<ESM::Cell>().search(index.x(), index.y());
dest = world->getCellName(cell); dest = world->getCellName(cell);
} }

View File

@ -23,6 +23,7 @@
#include "../mwworld/player.hpp" #include "../mwworld/player.hpp"
#include "../mwworld/cellstore.hpp" #include "../mwworld/cellstore.hpp"
#include "../mwworld/esmstore.hpp" #include "../mwworld/esmstore.hpp"
#include "../mwworld/cellutils.hpp"
#include "../mwrender/globalmap.hpp" #include "../mwrender/globalmap.hpp"
#include "../mwrender/localmap.hpp" #include "../mwrender/localmap.hpp"
@ -277,22 +278,22 @@ namespace MWGui
MyGUI::IntPoint LocalMapBase::getMarkerPosition(float worldX, float worldY, MarkerUserData& markerPos) const MyGUI::IntPoint LocalMapBase::getMarkerPosition(float worldX, float worldY, MarkerUserData& markerPos) const
{ {
int cellX, cellY; osg::Vec2i cellIndex;
// normalized cell coordinates // normalized cell coordinates
float nX,nY; float nX,nY;
if (!mInterior) if (!mInterior)
{ {
MWBase::Environment::get().getWorld()->positionToIndex(worldX, worldY, cellX, cellY); cellIndex = MWWorld::positionToCellIndex(worldX, worldY);
nX = (worldX - cellSize * cellX) / cellSize; nX = (worldX - cellSize * cellIndex.x()) / cellSize;
// Image space is -Y up, cells are Y up // Image space is -Y up, cells are Y up
nY = 1 - (worldY - cellSize * cellY) / cellSize; nY = 1 - (worldY - cellSize * cellIndex.y()) / cellSize;
} }
else else
mLocalMapRender->worldToInteriorMapPosition({ worldX, worldY }, nX, nY, cellX, cellY); mLocalMapRender->worldToInteriorMapPosition({ worldX, worldY }, nX, nY, cellIndex.x(), cellIndex.y());
markerPos.cellX = cellX; markerPos.cellX = cellIndex.x();
markerPos.cellY = cellY; markerPos.cellY = cellIndex.y();
markerPos.nX = nX; markerPos.nX = nX;
markerPos.nY = nY; markerPos.nY = nY;
return getPosition(markerPos.cellX, markerPos.cellY, markerPos.nX, markerPos.nY); return getPosition(markerPos.cellX, markerPos.cellY, markerPos.nX, markerPos.nY);

View File

@ -15,6 +15,7 @@
#include "../mwworld/containerstore.hpp" #include "../mwworld/containerstore.hpp"
#include "../mwworld/actionteleport.hpp" #include "../mwworld/actionteleport.hpp"
#include "../mwworld/cellstore.hpp" #include "../mwworld/cellstore.hpp"
#include "../mwworld/cellutils.hpp"
namespace MWGui namespace MWGui
@ -119,12 +120,10 @@ namespace MWGui
{ {
std::string cellname = transport[i].mCellName; std::string cellname = transport[i].mCellName;
bool interior = true; bool interior = true;
int x,y; const osg::Vec2i cellIndex = MWWorld::positionToCellIndex(transport[i].mPos.pos[0], transport[i].mPos.pos[1]);
MWBase::Environment::get().getWorld()->positionToIndex(transport[i].mPos.pos[0],
transport[i].mPos.pos[1],x,y);
if (cellname == "") if (cellname == "")
{ {
MWWorld::CellStore* cell = MWBase::Environment::get().getWorld()->getExterior(x,y); MWWorld::CellStore* cell = MWBase::Environment::get().getWorld()->getExterior(cellIndex.x(), cellIndex.y());
cellname = MWBase::Environment::get().getWorld()->getCellName(cell); cellname = MWBase::Environment::get().getWorld()->getCellName(cell);
interior = false; interior = false;
} }

View File

@ -10,6 +10,7 @@
#include "../mwworld/class.hpp" #include "../mwworld/class.hpp"
#include "../mwworld/timestamp.hpp" #include "../mwworld/timestamp.hpp"
#include "../mwworld/cellutils.hpp"
namespace MWLua namespace MWLua
{ {
@ -128,9 +129,8 @@ namespace MWLua
bool exterior = name.empty() || world->getExterior(name); bool exterior = name.empty() || world->getExterior(name);
if (exterior) if (exterior)
{ {
int cellX, cellY; const osg::Vec2i cellIndex = MWWorld::positionToCellIndex(position.x(), position.y());
world->positionToIndex(position.x(), position.y(), cellX, cellY); return world->getExterior(cellIndex.x(), cellIndex.y());
return world->getExterior(cellX, cellY);
} }
else else
return world->getInterior(name); return world->getInterior(name);

View File

@ -17,6 +17,7 @@
#include "../mwworld/class.hpp" #include "../mwworld/class.hpp"
#include "../mwworld/manualref.hpp" #include "../mwworld/manualref.hpp"
#include "../mwworld/player.hpp" #include "../mwworld/player.hpp"
#include "../mwworld/cellutils.hpp"
#include "../mwmechanics/actorutil.hpp" #include "../mwmechanics/actorutil.hpp"
@ -411,9 +412,8 @@ namespace MWScript
if(!isPlayer) if(!isPlayer)
return; return;
} }
int cx,cy; const osg::Vec2i cellIndex = MWWorld::positionToCellIndex(x, y);
MWBase::Environment::get().getWorld()->positionToIndex(x,y,cx,cy); store = MWBase::Environment::get().getWorld()->getExterior(cellIndex.x(), cellIndex.y());
store = MWBase::Environment::get().getWorld()->getExterior(cx,cy);
} }
if(store) if(store)
{ {
@ -460,15 +460,14 @@ namespace MWScript
{ {
MWBase::Environment::get().getWorld()->getPlayer().setTeleported(true); MWBase::Environment::get().getWorld()->getPlayer().setTeleported(true);
} }
int cx,cy; const osg::Vec2i cellIndex = MWWorld::positionToCellIndex(x, y);
MWBase::Environment::get().getWorld()->positionToIndex(x,y,cx,cy);
// another morrowind oddity: player will be moved to the exterior cell at this location, // another morrowind oddity: player will be moved to the exterior cell at this location,
// non-player actors will move within the cell they are in. // non-player actors will move within the cell they are in.
MWWorld::Ptr base = ptr; MWWorld::Ptr base = ptr;
if (ptr == MWMechanics::getPlayer()) if (ptr == MWMechanics::getPlayer())
{ {
MWWorld::CellStore* cell = MWBase::Environment::get().getWorld()->getExterior(cx,cy); MWWorld::CellStore* cell = MWBase::Environment::get().getWorld()->getExterior(cellIndex.x(), cellIndex.y());
ptr = MWBase::Environment::get().getWorld()->moveObject(ptr, cell, osg::Vec3(x, y, z)); ptr = MWBase::Environment::get().getWorld()->moveObject(ptr, cell, osg::Vec3(x, y, z));
} }
else else
@ -517,9 +516,8 @@ namespace MWScript
catch(std::exception&) catch(std::exception&)
{ {
const ESM::Cell* cell = MWBase::Environment::get().getWorld()->getExterior(cellID); const ESM::Cell* cell = MWBase::Environment::get().getWorld()->getExterior(cellID);
int cx,cy; const osg::Vec2i cellIndex = MWWorld::positionToCellIndex(x, y);
MWBase::Environment::get().getWorld()->positionToIndex(x,y,cx,cy); store = MWBase::Environment::get().getWorld()->getExterior(cellIndex.x(), cellIndex.y());
store = MWBase::Environment::get().getWorld()->getExterior(cx,cy);
if(!cell) if(!cell)
{ {
runtime.getContext().report ("unknown cell (" + cellID + ")"); runtime.getContext().report ("unknown cell (" + cellID + ")");
@ -569,9 +567,8 @@ namespace MWScript
MWWorld::CellStore* store = nullptr; MWWorld::CellStore* store = nullptr;
if (player.getCell()->isExterior()) if (player.getCell()->isExterior())
{ {
int cx,cy; const osg::Vec2i cellIndex = MWWorld::positionToCellIndex(x, y);
MWBase::Environment::get().getWorld()->positionToIndex(x,y,cx,cy); store = MWBase::Environment::get().getWorld()->getExterior(cellIndex.x(), cellIndex.y());
store = MWBase::Environment::get().getWorld()->getExterior(cx,cy);
} }
else else
store = player.getCell(); store = player.getCell();

View File

@ -8,6 +8,7 @@
#include "../mwworld/cellstore.hpp" #include "../mwworld/cellstore.hpp"
#include "../mwworld/class.hpp" #include "../mwworld/class.hpp"
#include "../mwworld/cellutils.hpp"
#include "player.hpp" #include "player.hpp"
@ -52,11 +53,8 @@ namespace MWWorld
actor.getClass().getCreatureStats(actor).getAiSequence().stopCombat(); actor.getClass().getCreatureStats(actor).getAiSequence().stopCombat();
else if (mCellName.empty()) else if (mCellName.empty())
{ {
int cellX; const osg::Vec2i index = positionToCellIndex(mPosition.pos[0], mPosition.pos[1]);
int cellY; world->moveObject(actor, world->getExterior(index.x(), index.y()), mPosition.asVec3(), true, true);
world->positionToIndex(mPosition.pos[0],mPosition.pos[1],cellX,cellY);
world->moveObject(actor,world->getExterior(cellX,cellY),
mPosition.asVec3(), true, true);
} }
else else
world->moveObject(actor,world->getInterior(mCellName),mPosition.asVec3(), true, true); world->moveObject(actor,world->getInterior(mCellName),mPosition.asVec3(), true, true);

View File

@ -2,13 +2,14 @@
#define OPENMW_MWWORLD_CELLUTILS_H #define OPENMW_MWWORLD_CELLUTILS_H
#include <components/misc/constants.hpp> #include <components/misc/constants.hpp>
#include <components/esm3/cellid.hpp>
#include <osg/Vec2i>
#include <cmath> #include <cmath>
namespace MWWorld namespace MWWorld
{ {
inline ESM::CellId::CellIndex positionToIndex(float x, float y) inline osg::Vec2i positionToCellIndex(float x, float y)
{ {
return { return {
static_cast<int>(std::floor(x / Constants::CellSizeInUnits)), static_cast<int>(std::floor(x / Constants::CellSizeInUnits)),

View File

@ -42,6 +42,7 @@
#include "cellstore.hpp" #include "cellstore.hpp"
#include "cellpreloader.hpp" #include "cellpreloader.hpp"
#include "worldimp.hpp" #include "worldimp.hpp"
#include "cellutils.hpp"
namespace namespace
{ {
@ -504,9 +505,7 @@ namespace MWWorld
if (distance <= maxDistance) if (distance <= maxDistance)
return *currentGridCenter; return *currentGridCenter;
} }
osg::Vec2i newCenter; return positionToCellIndex(pos.x(), pos.y());
mWorld.positionToIndex(pos.x(), pos.y(), newCenter.x(), newCenter.y());
return newCenter;
} }
void Scene::playerMoved(const osg::Vec3f &pos) void Scene::playerMoved(const osg::Vec3f &pos)
@ -867,17 +866,14 @@ namespace MWWorld
void Scene::changeToExteriorCell (const ESM::Position& position, bool adjustPlayerPos, bool changeEvent) void Scene::changeToExteriorCell (const ESM::Position& position, bool adjustPlayerPos, bool changeEvent)
{ {
int x = 0; const osg::Vec2i cellIndex = positionToCellIndex(position.pos[0], position.pos[1]);
int y = 0;
mWorld.positionToIndex (position.pos[0], position.pos[1], x, y);
if (changeEvent) if (changeEvent)
MWBase::Environment::get().getWindowManager()->fadeScreenOut(0.5); MWBase::Environment::get().getWindowManager()->fadeScreenOut(0.5);
changeCellGrid(position.asVec3(), x, y, changeEvent); changeCellGrid(position.asVec3(), cellIndex.x(), cellIndex.y(), changeEvent);
CellStore* current = mWorld.getExterior(x, y); CellStore* current = mWorld.getExterior(cellIndex.x(), cellIndex.y());
changePlayerCell(current, position, adjustPlayerPos); changePlayerCell(current, position, adjustPlayerPos);
if (changeEvent) if (changeEvent)
@ -1078,9 +1074,8 @@ namespace MWWorld
else else
{ {
osg::Vec3f pos = door.getCellRef().getDoorDest().asVec3(); osg::Vec3f pos = door.getCellRef().getDoorDest().asVec3();
int x,y; const osg::Vec2i cellIndex = positionToCellIndex(pos.x(), pos.y());
mWorld.positionToIndex (pos.x(), pos.y(), x, y); preloadCell(mWorld.getExterior(cellIndex.x(), cellIndex.y()), true);
preloadCell(mWorld.getExterior(x,y), true);
exteriorPositions.emplace_back(pos, gridCenterToBounds(getNewGridCenter(pos))); exteriorPositions.emplace_back(pos, gridCenterToBounds(getNewGridCenter(pos)));
} }
} }
@ -1216,9 +1211,8 @@ namespace MWWorld
else else
{ {
osg::Vec3f pos = dest.mPos.asVec3(); osg::Vec3f pos = dest.mPos.asVec3();
int x,y; const osg::Vec2i cellIndex = positionToCellIndex(pos.x(), pos.y());
mWorld.positionToIndex( pos.x(), pos.y(), x, y); preloadCell(mWorld.getExterior(cellIndex.x(), cellIndex.y()), true);
preloadCell(mWorld.getExterior(x,y), true);
exteriorPositions.emplace_back(pos, gridCenterToBounds(getNewGridCenter(pos))); exteriorPositions.emplace_back(pos, gridCenterToBounds(getNewGridCenter(pos)));
} }
} }

View File

@ -1279,10 +1279,10 @@ namespace MWWorld
MWWorld::Ptr World::moveObject(const Ptr& ptr, const osg::Vec3f& position, bool movePhysics, bool moveToActive) MWWorld::Ptr World::moveObject(const Ptr& ptr, const osg::Vec3f& position, bool movePhysics, bool moveToActive)
{ {
const auto index = MWWorld::positionToIndex(position.x(), position.y()); const osg::Vec2i index = positionToCellIndex(position.x(), position.y());
CellStore* cell = ptr.getCell(); CellStore* cell = ptr.getCell();
CellStore* newCell = getExterior(index.mX, index.mY); CellStore* newCell = getExterior(index.x(), index.y());
bool isCellActive = getPlayerPtr().isInCell() && getPlayerPtr().getCell()->isExterior() && mWorldScene->isCellActive(*newCell); bool isCellActive = getPlayerPtr().isInCell() && getPlayerPtr().getCell()->isExterior() && mWorldScene->isCellActive(*newCell);
if (cell->isExterior() || (moveToActive && isCellActive && ptr.getClass().isActor())) if (cell->isExterior() || (moveToActive && isCellActive && ptr.getClass().isActor()))
@ -1518,13 +1518,6 @@ namespace MWWorld
} }
} }
void World::positionToIndex (float x, float y, int &cellX, int &cellY) const
{
const auto index = MWWorld::positionToIndex(x, y);
cellX = index.mX;
cellY = index.mY;
}
void World::queueMovement(const Ptr &ptr, const osg::Vec3f &velocity) void World::queueMovement(const Ptr &ptr, const osg::Vec3f &velocity)
{ {
mPhysics->queueObjectMovement(ptr, velocity); mPhysics->queueObjectMovement(ptr, velocity);
@ -2112,7 +2105,9 @@ namespace MWWorld
else else
{ {
cellid.mPaged = true; cellid.mPaged = true;
cellid.mIndex = MWWorld::positionToIndex(ref.mRef.getDoorDest().pos[0], ref.mRef.getDoorDest().pos[1]); const osg::Vec2i index = positionToCellIndex(ref.mRef.getDoorDest().pos[0], ref.mRef.getDoorDest().pos[1]);
cellid.mIndex.mX = index.x();
cellid.mIndex.mY = index.y();
} }
newMarker.dest = cellid; newMarker.dest = cellid;
@ -2214,8 +2209,8 @@ namespace MWWorld
throw std::runtime_error("copyObjectToCell(): cannot copy object to null cell"); throw std::runtime_error("copyObjectToCell(): cannot copy object to null cell");
if (cell->isExterior()) if (cell->isExterior())
{ {
const auto index = MWWorld::positionToIndex(pos.pos[0], pos.pos[1]); const osg::Vec2i index = positionToCellIndex(pos.pos[0], pos.pos[1]);
cell = mCells.getExterior(index.mX, index.mY); cell = mCells.getExterior(index.x(), index.y());
} }
MWWorld::Ptr dropped = MWWorld::Ptr dropped =
@ -2823,8 +2818,8 @@ namespace MWWorld
if (door->getDestCell().empty()) if (door->getDestCell().empty())
{ {
ESM::Position doorDest = door->getDoorDest(); ESM::Position doorDest = door->getDoorDest();
const auto index = MWWorld::positionToIndex(doorDest.pos[0], doorDest.pos[1]); const osg::Vec2i index = positionToCellIndex(doorDest.pos[0], doorDest.pos[1]);
source = getExterior(index.mX, index.mY); source = getExterior(index.x(), index.y());
} }
// door to interior // door to interior
else else

View File

@ -407,9 +407,6 @@ namespace MWWorld
const override; const override;
///< Convert cell numbers to position. ///< Convert cell numbers to position.
void positionToIndex (float x, float y, int &cellX, int &cellY) const override;
///< Convert position to cell numbers
void queueMovement(const Ptr &ptr, const osg::Vec3f &velocity) override; void queueMovement(const Ptr &ptr, const osg::Vec3f &velocity) override;
///< Queues movement for \a ptr (in local space), to be applied in the next call to ///< Queues movement for \a ptr (in local space), to be applied in the next call to
/// doPhysics. /// doPhysics.