1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-26 09:35:28 +00:00
OpenMW/apps/openmw/mwlua/worldview.cpp

117 lines
3.0 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-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)
{
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;
}
}