1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-12 21:39:26 +00:00
OpenMW/apps/openmw/mwclass/container.cpp

241 lines
7.0 KiB
C++
Raw Normal View History

2010-08-03 13:24:44 +00:00
#include "container.hpp"
#include <components/esm/loadcont.hpp>
#include "../mwbase/environment.hpp"
#include "../mwbase/world.hpp"
#include "../mwbase/windowmanager.hpp"
2010-08-03 15:11:41 +00:00
#include "../mwworld/ptr.hpp"
#include "../mwworld/nullaction.hpp"
#include "../mwworld/containerstore.hpp"
#include "../mwworld/customdata.hpp"
#include "../mwworld/cellstore.hpp"
#include "../mwworld/actionopen.hpp"
#include "../mwworld/physicssystem.hpp"
#include "../mwworld/player.hpp"
#include "../mwworld/inventorystore.hpp"
#include "../mwgui/tooltips.hpp"
2012-01-27 14:11:02 +00:00
#include "../mwrender/objects.hpp"
#include "../mwrender/renderinginterface.hpp"
2012-01-27 14:11:02 +00:00
namespace
{
struct CustomData : public MWWorld::CustomData
{
MWWorld::ContainerStore mContainerStore;
virtual MWWorld::CustomData *clone() const;
};
MWWorld::CustomData *CustomData::clone() const
{
return new CustomData (*this);
}
}
2010-08-03 15:11:41 +00:00
2010-08-03 13:24:44 +00:00
namespace MWClass
{
void Container::ensureCustomData (const MWWorld::Ptr& ptr) const
{
if (!ptr.getRefData().getCustomData())
{
std::auto_ptr<CustomData> data (new CustomData);
// \todo add initial container content
// store
ptr.getRefData().setCustomData (data.release());
}
}
2011-11-12 04:01:12 +00:00
void Container::insertObjectRendering (const MWWorld::Ptr& ptr, MWRender::RenderingInterface& renderingInterface) const
{
2012-07-24 16:22:11 +00:00
const std::string model = getModel(ptr);
if (!model.empty()) {
MWRender::Objects& objects = renderingInterface.getObjects();
2011-11-12 04:01:12 +00:00
objects.insertBegin(ptr, ptr.getRefData().isEnabled(), false);
2012-07-24 16:22:11 +00:00
objects.insertMesh(ptr, model);
}
}
void Container::insertObject(const MWWorld::Ptr& ptr, MWWorld::PhysicsSystem& physics) const
2012-07-24 16:22:11 +00:00
{
const std::string model = getModel(ptr);
if(!model.empty()) {
physics.insertObjectPhysics(ptr, model);
}
}
2012-07-24 16:22:11 +00:00
std::string Container::getModel(const MWWorld::Ptr &ptr) const
2011-11-12 04:01:12 +00:00
{
MWWorld::LiveCellRef<ESM::Container> *ref =
2011-11-12 04:01:12 +00:00
ptr.get<ESM::Container>();
2012-07-24 16:22:11 +00:00
assert(ref->base != NULL);
2011-11-12 04:01:12 +00:00
2012-09-17 07:37:50 +00:00
const std::string &model = ref->base->mModel;
2012-07-24 16:22:11 +00:00
if (!model.empty()) {
return "meshes\\" + model;
2011-11-12 04:01:12 +00:00
}
2012-07-24 16:22:11 +00:00
return "";
2011-11-12 04:01:12 +00:00
}
boost::shared_ptr<MWWorld::Action> Container::activate (const MWWorld::Ptr& ptr,
const MWWorld::Ptr& actor) const
{
const std::string lockedSound = "LockedChest";
const std::string trapActivationSound = "Disarm Trap Fail";
MWWorld::Ptr player = MWBase::Environment::get().getWorld ()->getPlayer().getPlayer();
MWWorld::InventoryStore& invStore = MWWorld::Class::get(player).getInventoryStore(player);
2012-09-17 07:37:50 +00:00
bool needKey = ptr.getCellRef().mLockLevel>0;
bool hasKey = false;
std::string keyName;
for (MWWorld::ContainerStoreIterator it = invStore.begin(); it != invStore.end(); ++it)
{
2012-09-17 07:37:50 +00:00
if (it->getCellRef ().mRefID == ptr.getCellRef().mKey)
{
hasKey = true;
keyName = MWWorld::Class::get(*it).getName(*it);
}
}
if (needKey && hasKey)
{
MWBase::Environment::get().getWindowManager ()->messageBox (keyName + " #{sKeyUsed}", std::vector<std::string>());
2012-09-17 07:37:50 +00:00
ptr.getCellRef().mLockLevel = 0;
// using a key disarms the trap
2012-09-17 07:37:50 +00:00
ptr.getCellRef().mTrap = "";
}
if (!needKey || hasKey)
{
2012-09-17 07:37:50 +00:00
if(ptr.getCellRef().mTrap.empty())
{
boost::shared_ptr<MWWorld::Action> action (new MWWorld::ActionOpen(ptr));
return action;
}
else
{
// Trap activation goes here
2012-09-17 07:37:50 +00:00
std::cout << "Activated trap: " << ptr.getCellRef().mTrap << std::endl;
boost::shared_ptr<MWWorld::Action> action(new MWWorld::NullAction);
action->setSound(trapActivationSound);
2012-09-17 07:37:50 +00:00
ptr.getCellRef().mTrap = "";
return action;
}
}
else
{
boost::shared_ptr<MWWorld::Action> action(new MWWorld::NullAction);
action->setSound(lockedSound);
return action;
}
}
2010-08-03 15:11:41 +00:00
std::string Container::getName (const MWWorld::Ptr& ptr) const
{
MWWorld::LiveCellRef<ESM::Container> *ref =
2010-08-03 15:11:41 +00:00
ptr.get<ESM::Container>();
2012-09-17 07:37:50 +00:00
return ref->base->mName;
2010-08-03 15:11:41 +00:00
}
MWWorld::ContainerStore& Container::getContainerStore (const MWWorld::Ptr& ptr)
2010-08-04 12:37:23 +00:00
const
{
ensureCustomData (ptr);
2010-08-04 12:37:23 +00:00
return dynamic_cast<CustomData&> (*ptr.getRefData().getCustomData()).mContainerStore;
2010-08-04 12:37:23 +00:00
}
std::string Container::getScript (const MWWorld::Ptr& ptr) const
{
MWWorld::LiveCellRef<ESM::Container> *ref =
ptr.get<ESM::Container>();
2012-09-17 07:37:50 +00:00
return ref->base->mScript;
}
2010-08-03 13:24:44 +00:00
void Container::registerSelf()
{
boost::shared_ptr<Class> instance (new Container);
registerClass (typeid (ESM::Container).name(), instance);
}
2012-04-15 15:52:39 +00:00
bool Container::hasToolTip (const MWWorld::Ptr& ptr) const
{
MWWorld::LiveCellRef<ESM::Container> *ref =
ptr.get<ESM::Container>();
2012-09-17 07:37:50 +00:00
return (ref->base->mName != "");
}
MWGui::ToolTipInfo Container::getToolTipInfo (const MWWorld::Ptr& ptr) const
{
MWWorld::LiveCellRef<ESM::Container> *ref =
ptr.get<ESM::Container>();
MWGui::ToolTipInfo info;
2012-09-17 07:37:50 +00:00
info.caption = ref->base->mName;
const ESMS::ESMStore& store = MWBase::Environment::get().getWorld()->getStore();
std::string text;
2012-09-17 07:37:50 +00:00
if (ref->ref.mLockLevel > 0)
text += "\n" + store.gameSettings.search("sLockLevel")->mStr + ": " + MWGui::ToolTips::toString(ref->ref.mLockLevel);
if (ref->ref.mTrap != "")
text += "\n" + store.gameSettings.search("sTrapped")->mStr;
2012-04-15 15:52:39 +00:00
if (MWBase::Environment::get().getWindowManager()->getFullHelp()) {
2012-09-17 07:37:50 +00:00
text += MWGui::ToolTips::getMiscString(ref->ref.mOwner, "Owner");
text += MWGui::ToolTips::getMiscString(ref->base->mScript, "Script");
}
info.text = text;
return info;
}
2012-05-15 19:17:00 +00:00
2012-05-15 20:31:52 +00:00
float Container::getCapacity (const MWWorld::Ptr& ptr) const
2012-05-15 19:17:00 +00:00
{
MWWorld::LiveCellRef<ESM::Container> *ref =
2012-05-15 19:17:00 +00:00
ptr.get<ESM::Container>();
2012-09-17 07:37:50 +00:00
return ref->base->mWeight;
2012-05-15 19:17:00 +00:00
}
2012-05-15 19:34:00 +00:00
float Container::getEncumbrance (const MWWorld::Ptr& ptr) const
{
return getContainerStore (ptr).getWeight();
}
void Container::lock (const MWWorld::Ptr& ptr, int lockLevel) const
{
if (lockLevel<0)
lockLevel = 0;
2012-09-17 07:37:50 +00:00
ptr.getCellRef().mLockLevel = lockLevel;
}
void Container::unlock (const MWWorld::Ptr& ptr) const
{
2012-09-17 07:37:50 +00:00
ptr.getCellRef().mLockLevel = 0;
}
MWWorld::Ptr
Container::copyToCellImpl(const MWWorld::Ptr &ptr, MWWorld::CellStore &cell) const
{
MWWorld::LiveCellRef<ESM::Container> *ref =
ptr.get<ESM::Container>();
return MWWorld::Ptr(&cell.containers.insert(*ref), &cell);
}
2010-08-03 13:24:44 +00:00
}