1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-16 13:49:55 +00:00
openmw-tes3mp/apps/openmw/mwclass/container.cpp

248 lines
7.3 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"
2012-11-17 17:17:08 +00:00
#include "../mwworld/failedaction.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.addObject(ptr);
2012-07-24 16:22:11 +00:00
}
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-11-05 12:07:59 +00:00
assert(ref->mBase != NULL);
2011-11-12 04:01:12 +00:00
2012-11-05 12:07:59 +00:00
const std::string &model = ref->mBase->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
{
if (!MWBase::Environment::get().getWindowManager()->isAllowed(MWGui::GW_Inventory))
return boost::shared_ptr<MWWorld::Action> (new MWWorld::NullAction ());
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;
2012-11-02 21:21:32 +00:00
// make key id lowercase
std::string keyId = ptr.getCellRef().mKey;
2013-01-09 19:51:52 +00:00
Misc::StringUtils::toLower(keyId);
for (MWWorld::ContainerStoreIterator it = invStore.begin(); it != invStore.end(); ++it)
{
2012-11-02 21:21:32 +00:00
std::string refId = it->getCellRef().mRefID;
2013-01-09 19:51:52 +00:00
Misc::StringUtils::toLower(refId);
if (refId == keyId)
{
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;
2012-11-19 20:04:49 +00:00
boost::shared_ptr<MWWorld::Action> action(new MWWorld::FailedAction);
action->setSound(trapActivationSound);
2012-09-17 07:37:50 +00:00
ptr.getCellRef().mTrap = "";
return action;
}
}
else
{
2012-11-19 20:04:49 +00:00
boost::shared_ptr<MWWorld::Action> action(new MWWorld::FailedAction);
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-11-05 12:07:59 +00:00
return ref->mBase->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-11-05 12:07:59 +00:00
return ref->mBase->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-11-05 12:07:59 +00:00
return (ref->mBase->mName != "");
}
MWGui::ToolTipInfo Container::getToolTipInfo (const MWWorld::Ptr& ptr) const
{
MWWorld::LiveCellRef<ESM::Container> *ref =
ptr.get<ESM::Container>();
MWGui::ToolTipInfo info;
2012-11-05 12:07:59 +00:00
info.caption = ref->mBase->mName;
std::string text;
2012-11-05 12:07:59 +00:00
if (ref->mRef.mLockLevel > 0)
text += "\n#{sLockLevel}: " + MWGui::ToolTips::toString(ref->mRef.mLockLevel);
if (ref->mRef.mTrap != "")
2012-09-22 19:35:57 +00:00
text += "\n#{sTrapped}";
2012-04-15 15:52:39 +00:00
if (MWBase::Environment::get().getWindowManager()->getFullHelp()) {
2012-11-05 12:07:59 +00:00
text += MWGui::ToolTips::getMiscString(ref->mRef.mOwner, "Owner");
text += MWGui::ToolTips::getMiscString(ref->mBase->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-11-05 12:07:59 +00:00
return ref->mBase->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>();
2012-11-05 12:07:59 +00:00
return MWWorld::Ptr(&cell.mContainers.insert(*ref), &cell);
}
2010-08-03 13:24:44 +00:00
}