You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
401 lines
13 KiB
C++
401 lines
13 KiB
C++
#include "container.hpp"
|
|
|
|
/*
|
|
Start of tes3mp addition
|
|
|
|
Include additional headers for multiplayer purposes
|
|
*/
|
|
#include "../mwmp/Main.hpp"
|
|
#include "../mwmp/Networking.hpp"
|
|
#include "../mwmp/ObjectList.hpp"
|
|
/*
|
|
End of tes3mp addition
|
|
*/
|
|
|
|
#include <components/esm/loadcont.hpp>
|
|
#include <components/esm/containerstate.hpp>
|
|
#include <components/settings/settings.hpp>
|
|
|
|
#include "../mwbase/environment.hpp"
|
|
#include "../mwbase/world.hpp"
|
|
#include "../mwbase/windowmanager.hpp"
|
|
#include "../mwbase/soundmanager.hpp"
|
|
|
|
#include "../mwworld/ptr.hpp"
|
|
#include "../mwworld/failedaction.hpp"
|
|
#include "../mwworld/nullaction.hpp"
|
|
#include "../mwworld/containerstore.hpp"
|
|
#include "../mwworld/customdata.hpp"
|
|
#include "../mwworld/cellstore.hpp"
|
|
#include "../mwworld/esmstore.hpp"
|
|
#include "../mwworld/actionharvest.hpp"
|
|
#include "../mwworld/actionopen.hpp"
|
|
#include "../mwworld/actiontrap.hpp"
|
|
#include "../mwphysics/physicssystem.hpp"
|
|
#include "../mwworld/inventorystore.hpp"
|
|
|
|
#include "../mwgui/tooltips.hpp"
|
|
|
|
#include "../mwrender/animation.hpp"
|
|
#include "../mwrender/objects.hpp"
|
|
#include "../mwrender/renderinginterface.hpp"
|
|
|
|
#include "../mwmechanics/actorutil.hpp"
|
|
#include "../mwmechanics/npcstats.hpp"
|
|
|
|
namespace MWClass
|
|
{
|
|
ContainerCustomData::ContainerCustomData(const ESM::Container& container, MWWorld::CellStore* cell)
|
|
{
|
|
unsigned int seed = Misc::Rng::rollDice(std::numeric_limits<int>::max());
|
|
// setting ownership not needed, since taking items from a container inherits the
|
|
// container's owner automatically
|
|
mStore.fillNonRandom(container.mInventory, "", seed);
|
|
}
|
|
|
|
ContainerCustomData::ContainerCustomData(const ESM::InventoryState& inventory)
|
|
{
|
|
mStore.readState(inventory);
|
|
}
|
|
|
|
ContainerCustomData& ContainerCustomData::asContainerCustomData()
|
|
{
|
|
return *this;
|
|
}
|
|
const ContainerCustomData& ContainerCustomData::asContainerCustomData() const
|
|
{
|
|
return *this;
|
|
}
|
|
|
|
Container::Container()
|
|
{
|
|
mHarvestEnabled = Settings::Manager::getBool("graphic herbalism", "Game");
|
|
}
|
|
|
|
void Container::ensureCustomData (const MWWorld::Ptr& ptr) const
|
|
{
|
|
if (!ptr.getRefData().getCustomData())
|
|
{
|
|
MWWorld::LiveCellRef<ESM::Container> *ref = ptr.get<ESM::Container>();
|
|
|
|
// store
|
|
ptr.getRefData().setCustomData (std::make_unique<ContainerCustomData>(*ref->mBase, ptr.getCell()));
|
|
|
|
MWBase::Environment::get().getWorld()->addContainerScripts(ptr, ptr.getCell());
|
|
}
|
|
}
|
|
|
|
bool Container::canBeHarvested(const MWWorld::ConstPtr& ptr) const
|
|
{
|
|
if (!mHarvestEnabled)
|
|
return false;
|
|
const MWRender::Animation* animation = MWBase::Environment::get().getWorld()->getAnimation(ptr);
|
|
if (animation == nullptr)
|
|
return false;
|
|
|
|
return animation->canBeHarvested();
|
|
}
|
|
|
|
void Container::respawn(const MWWorld::Ptr &ptr) const
|
|
{
|
|
MWWorld::LiveCellRef<ESM::Container> *ref =
|
|
ptr.get<ESM::Container>();
|
|
if (ref->mBase->mFlags & ESM::Container::Respawn)
|
|
{
|
|
// Container was not touched, there is no need to modify its content.
|
|
if (ptr.getRefData().getCustomData() == nullptr)
|
|
return;
|
|
|
|
MWBase::Environment::get().getWorld()->removeContainerScripts(ptr);
|
|
ptr.getRefData().setCustomData(nullptr);
|
|
}
|
|
}
|
|
|
|
void Container::insertObjectRendering (const MWWorld::Ptr& ptr, const std::string& model, MWRender::RenderingInterface& renderingInterface) const
|
|
{
|
|
if (!model.empty()) {
|
|
renderingInterface.getObjects().insertModel(ptr, model, true);
|
|
}
|
|
}
|
|
|
|
void Container::insertObject(const MWWorld::Ptr& ptr, const std::string& model, MWPhysics::PhysicsSystem& physics) const
|
|
{
|
|
if(!model.empty())
|
|
physics.addObject(ptr, model);
|
|
}
|
|
|
|
std::string Container::getModel(const MWWorld::ConstPtr &ptr) const
|
|
{
|
|
const MWWorld::LiveCellRef<ESM::Container> *ref = ptr.get<ESM::Container>();
|
|
|
|
const std::string &model = ref->mBase->mModel;
|
|
if (!model.empty()) {
|
|
return "meshes\\" + model;
|
|
}
|
|
return "";
|
|
}
|
|
|
|
bool Container::useAnim() const
|
|
{
|
|
return true;
|
|
}
|
|
|
|
std::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 std::shared_ptr<MWWorld::Action> (new MWWorld::NullAction ());
|
|
|
|
if(actor.getClass().isNpc() && actor.getClass().getNpcStats(actor).isWerewolf())
|
|
{
|
|
const MWWorld::ESMStore &store = MWBase::Environment::get().getWorld()->getStore();
|
|
const ESM::Sound *sound = store.get<ESM::Sound>().searchRandom("WolfContainer");
|
|
|
|
std::shared_ptr<MWWorld::Action> action(new MWWorld::FailedAction("#{sWerewolfRefusal}"));
|
|
if(sound) action->setSound(sound->mId);
|
|
|
|
return action;
|
|
}
|
|
|
|
const std::string lockedSound = "LockedChest";
|
|
const std::string trapActivationSound = "Disarm Trap Fail";
|
|
|
|
MWWorld::Ptr player = MWBase::Environment::get().getWorld ()->getPlayerPtr();
|
|
MWWorld::InventoryStore& invStore = player.getClass().getInventoryStore(player);
|
|
|
|
bool isLocked = ptr.getCellRef().getLockLevel() > 0;
|
|
bool isTrapped = !ptr.getCellRef().getTrap().empty();
|
|
bool hasKey = false;
|
|
std::string keyName;
|
|
|
|
const std::string keyId = ptr.getCellRef().getKey();
|
|
if (!keyId.empty())
|
|
{
|
|
MWWorld::Ptr keyPtr = invStore.search(keyId);
|
|
if (!keyPtr.isEmpty())
|
|
{
|
|
hasKey = true;
|
|
keyName = keyPtr.getClass().getName(keyPtr);
|
|
}
|
|
}
|
|
|
|
if (isLocked && hasKey)
|
|
{
|
|
MWBase::Environment::get().getWindowManager ()->messageBox (keyName + " #{sKeyUsed}");
|
|
|
|
/*
|
|
Start of tes3mp change (major)
|
|
|
|
Disable unilateral unlocking on this client and expect the server's reply to our
|
|
packet to do it instead
|
|
*/
|
|
//ptr.getCellRef().unlock();
|
|
/*
|
|
End of tes3mp change (major)
|
|
*/
|
|
|
|
// using a key disarms the trap
|
|
if(isTrapped)
|
|
{
|
|
/*
|
|
Start of tes3mp change (major)
|
|
|
|
Disable unilateral trap disarming on this client and expect the server's reply to our
|
|
packet to do it instead
|
|
*/
|
|
//ptr.getCellRef().setTrap("");
|
|
//MWBase::Environment::get().getSoundManager()->playSound3D(ptr, "Disarm Trap", 1.0f, 1.0f);
|
|
/*
|
|
End of tes3mp change (major)
|
|
*/
|
|
|
|
isTrapped = false;
|
|
|
|
/*
|
|
Start of tes3mp addition
|
|
|
|
Send an ID_OBJECT_TRAP packet every time a trap is disarmed
|
|
*/
|
|
mwmp::ObjectList *objectList = mwmp::Main::get().getNetworking()->getObjectList();
|
|
objectList->reset();
|
|
objectList->packetOrigin = mwmp::CLIENT_GAMEPLAY;
|
|
objectList->addObjectTrap(ptr, ptr.getRefData().getPosition(), true);
|
|
objectList->sendObjectTrap();
|
|
/*
|
|
End of tes3mp addition
|
|
*/
|
|
}
|
|
|
|
/*
|
|
Start of tes3mp addition
|
|
|
|
Send an ID_OBJECT_LOCK packet every time a container is unlocked here
|
|
*/
|
|
if (isLocked)
|
|
{
|
|
mwmp::ObjectList *objectList = mwmp::Main::get().getNetworking()->getObjectList();
|
|
objectList->reset();
|
|
objectList->packetOrigin = mwmp::CLIENT_GAMEPLAY;
|
|
objectList->addObjectLock(ptr, 0);
|
|
objectList->sendObjectLock();
|
|
}
|
|
/*
|
|
End of tes3mp addition
|
|
*/
|
|
}
|
|
|
|
|
|
if (!isLocked || hasKey)
|
|
{
|
|
if(!isTrapped)
|
|
{
|
|
if (canBeHarvested(ptr))
|
|
{
|
|
std::shared_ptr<MWWorld::Action> action (new MWWorld::ActionHarvest(ptr));
|
|
return action;
|
|
}
|
|
|
|
std::shared_ptr<MWWorld::Action> action (new MWWorld::ActionOpen(ptr));
|
|
return action;
|
|
}
|
|
else
|
|
{
|
|
// Activate trap
|
|
std::shared_ptr<MWWorld::Action> action(new MWWorld::ActionTrap(ptr.getCellRef().getTrap(), ptr));
|
|
action->setSound(trapActivationSound);
|
|
return action;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
std::shared_ptr<MWWorld::Action> action(new MWWorld::FailedAction(std::string(), ptr));
|
|
action->setSound(lockedSound);
|
|
return action;
|
|
}
|
|
}
|
|
|
|
std::string Container::getName (const MWWorld::ConstPtr& ptr) const
|
|
{
|
|
const MWWorld::LiveCellRef<ESM::Container> *ref = ptr.get<ESM::Container>();
|
|
const std::string& name = ref->mBase->mName;
|
|
|
|
return !name.empty() ? name : ref->mBase->mId;
|
|
}
|
|
|
|
MWWorld::ContainerStore& Container::getContainerStore (const MWWorld::Ptr& ptr) const
|
|
{
|
|
ensureCustomData (ptr);
|
|
auto& data = ptr.getRefData().getCustomData()->asContainerCustomData();
|
|
data.mStore.mPtr = ptr;
|
|
return data.mStore;
|
|
}
|
|
|
|
std::string Container::getScript (const MWWorld::ConstPtr& ptr) const
|
|
{
|
|
const MWWorld::LiveCellRef<ESM::Container> *ref = ptr.get<ESM::Container>();
|
|
|
|
return ref->mBase->mScript;
|
|
}
|
|
|
|
void Container::registerSelf()
|
|
{
|
|
std::shared_ptr<Class> instance (new Container);
|
|
|
|
registerClass (typeid (ESM::Container).name(), instance);
|
|
}
|
|
|
|
bool Container::hasToolTip (const MWWorld::ConstPtr& ptr) const
|
|
{
|
|
if (const MWWorld::CustomData* data = ptr.getRefData().getCustomData())
|
|
return !canBeHarvested(ptr) || data->asContainerCustomData().mStore.hasVisibleItems();
|
|
return true;
|
|
}
|
|
|
|
MWGui::ToolTipInfo Container::getToolTipInfo (const MWWorld::ConstPtr& ptr, int count) const
|
|
{
|
|
const MWWorld::LiveCellRef<ESM::Container> *ref = ptr.get<ESM::Container>();
|
|
|
|
MWGui::ToolTipInfo info;
|
|
info.caption = MyGUI::TextIterator::toTagsString(getName(ptr));
|
|
|
|
std::string text;
|
|
int lockLevel = ptr.getCellRef().getLockLevel();
|
|
if (lockLevel > 0 && lockLevel != ESM::UnbreakableLock)
|
|
text += "\n#{sLockLevel}: " + MWGui::ToolTips::toString(lockLevel);
|
|
else if (lockLevel < 0)
|
|
text += "\n#{sUnlocked}";
|
|
if (ptr.getCellRef().getTrap() != "")
|
|
text += "\n#{sTrapped}";
|
|
|
|
if (MWBase::Environment::get().getWindowManager()->getFullHelp())
|
|
{ text += MWGui::ToolTips::getCellRefString(ptr.getCellRef());
|
|
text += MWGui::ToolTips::getMiscString(ref->mBase->mScript, "Script");
|
|
if (Misc::StringUtils::ciEqual(ptr.getCellRef().getRefId(), "stolen_goods"))
|
|
text += "\nYou can not use evidence chests";
|
|
}
|
|
|
|
info.text = text;
|
|
|
|
return info;
|
|
}
|
|
|
|
float Container::getCapacity (const MWWorld::Ptr& ptr) const
|
|
{
|
|
MWWorld::LiveCellRef<ESM::Container> *ref =
|
|
ptr.get<ESM::Container>();
|
|
|
|
return ref->mBase->mWeight;
|
|
}
|
|
|
|
float Container::getEncumbrance (const MWWorld::Ptr& ptr) const
|
|
{
|
|
return getContainerStore (ptr).getWeight();
|
|
}
|
|
|
|
bool Container::canLock(const MWWorld::ConstPtr &ptr) const
|
|
{
|
|
const MWWorld::LiveCellRef<ESM::Container> *ref = ptr.get<ESM::Container>();
|
|
return !(ref->mBase->mFlags & ESM::Container::Organic);
|
|
}
|
|
|
|
void Container::modifyBaseInventory(const std::string& containerId, const std::string& itemId, int amount) const
|
|
{
|
|
MWMechanics::modifyBaseInventory<ESM::Container>(containerId, itemId, amount);
|
|
}
|
|
|
|
MWWorld::Ptr Container::copyToCellImpl(const MWWorld::ConstPtr &ptr, MWWorld::CellStore &cell) const
|
|
{
|
|
const MWWorld::LiveCellRef<ESM::Container> *ref = ptr.get<ESM::Container>();
|
|
|
|
return MWWorld::Ptr(cell.insert(ref), &cell);
|
|
}
|
|
|
|
void Container::readAdditionalState (const MWWorld::Ptr& ptr, const ESM::ObjectState& state) const
|
|
{
|
|
if (!state.mHasCustomState)
|
|
return;
|
|
|
|
const ESM::ContainerState& containerState = state.asContainerState();
|
|
ptr.getRefData().setCustomData(std::make_unique<ContainerCustomData>(containerState.mInventory));
|
|
}
|
|
|
|
void Container::writeAdditionalState (const MWWorld::ConstPtr& ptr, ESM::ObjectState& state) const
|
|
{
|
|
if (!ptr.getRefData().getCustomData())
|
|
{
|
|
state.mHasCustomState = false;
|
|
return;
|
|
}
|
|
|
|
const ContainerCustomData& customData = ptr.getRefData().getCustomData()->asContainerCustomData();
|
|
if (!customData.mStore.isResolved())
|
|
{
|
|
state.mHasCustomState = false;
|
|
return;
|
|
}
|
|
|
|
ESM::ContainerState& containerState = state.asContainerState();
|
|
customData.mStore.writeState (containerState.mInventory);
|
|
}
|
|
}
|