1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-18 13:12:50 +00:00
OpenMW/apps/openmw/mwlua/worldview.cpp

153 lines
4.4 KiB
C++
Raw Normal View History

2020-12-18 23:21:10 +01:00
#include "worldview.hpp"
#include <components/esm/esmreader.hpp>
#include <components/esm/esmwriter.hpp>
2021-04-17 11:50:20 +02:00
#include <components/esm/loadcell.hpp>
2021-01-29 01:54:54 +01:00
#include "../mwclass/container.hpp"
2020-12-18 23:21:10 +01:00
#include "../mwworld/class.hpp"
2021-03-12 18:29:51 +01:00
#include "../mwworld/timestamp.hpp"
2020-12-18 23:21:10 +01:00
namespace MWLua
{
void WorldView::update()
{
mObjectRegistry.update();
2021-01-29 01:54:54 +01:00
mActivatorsInScene.updateList();
2020-12-18 23:21:10 +01:00
mActorsInScene.updateList();
2021-01-29 01:54:54 +01:00
mContainersInScene.updateList();
mDoorsInScene.updateList();
2020-12-18 23:21:10 +01:00
mItemsInScene.updateList();
}
void WorldView::clear()
{
mObjectRegistry.clear();
2021-01-29 01:54:54 +01:00
mActivatorsInScene.clear();
2020-12-18 23:21:10 +01:00
mActorsInScene.clear();
2021-01-29 01:54:54 +01:00
mContainersInScene.clear();
mDoorsInScene.clear();
2020-12-18 23:21:10 +01:00
mItemsInScene.clear();
}
2021-01-29 01:54:54 +01:00
WorldView::ObjectGroup* WorldView::chooseGroup(const MWWorld::Ptr& ptr)
{
2021-04-17 11:50:20 +02:00
// It is important to check `isMarker` first.
// For example "prisonmarker" has class "Door" despite that it is only an invisible marker.
if (isMarker(ptr))
return nullptr;
2021-01-29 01:54:54 +01:00
const MWWorld::Class& cls = ptr.getClass();
if (cls.isActivator())
return &mActivatorsInScene;
if (cls.isActor())
return &mActorsInScene;
if (cls.isDoor())
return &mDoorsInScene;
if (typeid(cls) == typeid(MWClass::Container))
return &mContainersInScene;
if (cls.hasToolTip(ptr))
return &mItemsInScene;
return nullptr;
}
2020-12-18 23:21:10 +01:00
void WorldView::objectAddedToScene(const MWWorld::Ptr& ptr)
{
2021-01-29 01:54:54 +01:00
mObjectRegistry.registerPtr(ptr);
ObjectGroup* group = chooseGroup(ptr);
if (group)
addToGroup(*group, ptr);
2020-12-18 23:21:10 +01:00
}
void WorldView::objectRemovedFromScene(const MWWorld::Ptr& ptr)
{
2021-01-29 01:54:54 +01:00
ObjectGroup* group = chooseGroup(ptr);
if (group)
removeFromGroup(*group, ptr);
2020-12-18 23:21:10 +01:00
}
2021-03-12 18:29:51 +01:00
double WorldView::getGameTimeInHours() const
{
MWBase::World* world = MWBase::Environment::get().getWorld();
MWWorld::TimeStamp timeStamp = world->getTimeStamp();
return static_cast<double>(timeStamp.getDay()) * 24 + timeStamp.getHour();
}
void WorldView::load(ESM::ESMReader& esm)
{
esm.getHNT(mGameSeconds, "LUAW");
ObjectId lastAssignedId;
lastAssignedId.load(esm, true);
mObjectRegistry.setLastAssignedId(lastAssignedId);
}
void WorldView::save(ESM::ESMWriter& esm) const
{
esm.writeHNT("LUAW", mGameSeconds);
mObjectRegistry.getLastAssignedId().save(esm, true);
}
2020-12-18 23:21:10 +01:00
void WorldView::ObjectGroup::updateList()
{
if (mChanged)
{
mList->clear();
for (const ObjectId& id : mSet)
mList->push_back(id);
mChanged = false;
}
}
void WorldView::ObjectGroup::clear()
{
mChanged = false;
mList->clear();
mSet.clear();
}
void WorldView::addToGroup(ObjectGroup& group, const MWWorld::Ptr& ptr)
{
group.mSet.insert(getId(ptr));
group.mChanged = true;
}
void WorldView::removeFromGroup(ObjectGroup& group, const MWWorld::Ptr& ptr)
{
group.mSet.erase(getId(ptr));
group.mChanged = true;
}
2021-04-17 11:50:20 +02:00
// TODO: If Lua scripts will use several threads at the same time, then `find*Cell` functions should have critical sections.
MWWorld::CellStore* WorldView::findCell(const std::string& name, osg::Vec3f position)
{
MWBase::World* world = MWBase::Environment::get().getWorld();
bool exterior = name.empty() || world->getExterior(name);
if (exterior)
{
int cellX, cellY;
world->positionToIndex(position.x(), position.y(), cellX, cellY);
return world->getExterior(cellX, cellY);
}
else
return world->getInterior(name);
}
MWWorld::CellStore* WorldView::findNamedCell(const std::string& name)
{
MWBase::World* world = MWBase::Environment::get().getWorld();
const ESM::Cell* esmCell = world->getExterior(name);
if (esmCell)
return world->getExterior(esmCell->getGridX(), esmCell->getGridY());
else
return world->getInterior(name);
}
MWWorld::CellStore* WorldView::findExteriorCell(int x, int y)
{
MWBase::World* world = MWBase::Environment::get().getWorld();
return world->getExterior(x, y);
}
2020-12-18 23:21:10 +01:00
}