1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-12 03:36:32 +00:00
OpenMW/apps/openmw/mwclass/armor.cpp

317 lines
10 KiB
C++
Raw Normal View History

2010-08-03 12:14:04 +00:00
#include "armor.hpp"
#include <components/esm/loadarmo.hpp>
#include <components/esm/loadskil.hpp>
#include <components/esm/loadgmst.hpp>
2010-08-03 12:14:04 +00:00
#include "../mwbase/environment.hpp"
#include "../mwbase/world.hpp"
#include "../mwbase/windowmanager.hpp"
2010-08-03 12:14:04 +00:00
#include "../mwworld/ptr.hpp"
2010-08-07 18:25:17 +00:00
#include "../mwworld/actiontake.hpp"
#include "../mwworld/actionequip.hpp"
#include "../mwworld/inventorystore.hpp"
#include "../mwworld/cellstore.hpp"
#include "../mwworld/physicssystem.hpp"
#include "../mwworld/nullaction.hpp"
2010-08-03 12:14:04 +00:00
2012-01-27 14:11:02 +00:00
#include "../mwrender/objects.hpp"
#include "../mwrender/renderinginterface.hpp"
#include "../mwgui/tooltips.hpp"
2010-08-03 12:14:04 +00:00
namespace MWClass
{
2011-11-12 04:01:12 +00:00
void Armor::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 Armor::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,true);
2012-07-24 16:22:11 +00:00
}
2012-07-24 16:22:11 +00:00
std::string Armor::getModel(const MWWorld::Ptr &ptr) const
2011-11-12 04:01:12 +00:00
{
MWWorld::LiveCellRef<ESM::Armor> *ref =
2011-11-12 04:01:12 +00:00
ptr.get<ESM::Armor>();
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
}
2010-08-03 15:11:41 +00:00
std::string Armor::getName (const MWWorld::Ptr& ptr) const
{
MWWorld::LiveCellRef<ESM::Armor> *ref =
2010-08-03 15:11:41 +00:00
ptr.get<ESM::Armor>();
2012-11-05 12:07:59 +00:00
return ref->mBase->mName;
2010-08-03 15:11:41 +00:00
}
2010-08-07 18:25:17 +00:00
boost::shared_ptr<MWWorld::Action> Armor::activate (const MWWorld::Ptr& ptr,
const MWWorld::Ptr& actor) const
2010-08-07 18:25:17 +00:00
{
if (!MWBase::Environment::get().getWindowManager()->isAllowed(MWGui::GW_Inventory))
return boost::shared_ptr<MWWorld::Action> (new MWWorld::NullAction ());
boost::shared_ptr<MWWorld::Action> action(new MWWorld::ActionTake (ptr));
2012-03-13 16:05:38 +00:00
action->setSound(getUpSoundId(ptr));
return action;
2010-08-07 18:25:17 +00:00
}
2010-08-03 12:14:04 +00:00
bool Armor::hasItemHealth (const MWWorld::Ptr& ptr) const
{
return true;
}
int Armor::getItemMaxHealth (const MWWorld::Ptr& ptr) const
{
MWWorld::LiveCellRef<ESM::Armor> *ref =
2010-08-03 12:14:04 +00:00
ptr.get<ESM::Armor>();
2012-11-05 12:07:59 +00:00
return ref->mBase->mData.mHealth;
2010-08-03 12:14:04 +00:00
}
std::string Armor::getScript (const MWWorld::Ptr& ptr) const
{
MWWorld::LiveCellRef<ESM::Armor> *ref =
ptr.get<ESM::Armor>();
2012-11-05 12:07:59 +00:00
return ref->mBase->mScript;
}
std::pair<std::vector<int>, bool> Armor::getEquipmentSlots (const MWWorld::Ptr& ptr) const
{
MWWorld::LiveCellRef<ESM::Armor> *ref =
ptr.get<ESM::Armor>();
std::vector<int> slots;
const int size = 11;
static const int sMapping[size][2] =
{
{ ESM::Armor::Helmet, MWWorld::InventoryStore::Slot_Helmet },
{ ESM::Armor::Cuirass, MWWorld::InventoryStore::Slot_Cuirass },
{ ESM::Armor::LPauldron, MWWorld::InventoryStore::Slot_LeftPauldron },
{ ESM::Armor::RPauldron, MWWorld::InventoryStore::Slot_RightPauldron },
{ ESM::Armor::Greaves, MWWorld::InventoryStore::Slot_Greaves },
{ ESM::Armor::Boots, MWWorld::InventoryStore::Slot_Boots },
{ ESM::Armor::LGauntlet, MWWorld::InventoryStore::Slot_LeftGauntlet },
{ ESM::Armor::RGauntlet, MWWorld::InventoryStore::Slot_RightGauntlet },
{ ESM::Armor::Shield, MWWorld::InventoryStore::Slot_CarriedLeft },
{ ESM::Armor::LBracer, MWWorld::InventoryStore::Slot_LeftGauntlet },
{ ESM::Armor::RBracer, MWWorld::InventoryStore::Slot_RightGauntlet }
};
for (int i=0; i<size; ++i)
2012-11-05 12:07:59 +00:00
if (sMapping[i][0]==ref->mBase->mData.mType)
{
slots.push_back (int (sMapping[i][1]));
break;
}
return std::make_pair (slots, false);
}
int Armor::getEquipmentSkill (const MWWorld::Ptr& ptr) const
{
MWWorld::LiveCellRef<ESM::Armor> *ref =
ptr.get<ESM::Armor>();
std::string typeGmst;
2012-11-05 12:07:59 +00:00
switch (ref->mBase->mData.mType)
{
case ESM::Armor::Helmet: typeGmst = "iHelmWeight"; break;
case ESM::Armor::Cuirass: typeGmst = "iCuirassWeight"; break;
case ESM::Armor::LPauldron:
case ESM::Armor::RPauldron: typeGmst = "iPauldronWeight"; break;
case ESM::Armor::Greaves: typeGmst = "iGreavesWeight"; break;
case ESM::Armor::Boots: typeGmst = "iBootsWeight"; break;
case ESM::Armor::LGauntlet:
case ESM::Armor::RGauntlet: typeGmst = "iGauntletWeight"; break;
case ESM::Armor::Shield: typeGmst = "iShieldWeight"; break;
case ESM::Armor::LBracer:
case ESM::Armor::RBracer: typeGmst = "iGauntletWeight"; break;
}
if (typeGmst.empty())
return -1;
const MWWorld::Store<ESM::GameSetting> &gmst =
MWBase::Environment::get().getWorld()->getStore().get<ESM::GameSetting>();
float iWeight = gmst.find (typeGmst)->getInt();
if (iWeight * gmst.find ("fLightMaxMod")->getFloat()>=
2012-11-05 12:07:59 +00:00
ref->mBase->mData.mWeight)
return ESM::Skill::LightArmor;
2012-11-06 10:44:56 +00:00
if (iWeight * gmst.find ("fMedMaxMod")->getFloat()>=
2012-11-05 12:07:59 +00:00
ref->mBase->mData.mWeight)
return ESM::Skill::MediumArmor;
return ESM::Skill::HeavyArmor;
}
int Armor::getValue (const MWWorld::Ptr& ptr) const
{
MWWorld::LiveCellRef<ESM::Armor> *ref =
ptr.get<ESM::Armor>();
2012-11-05 12:07:59 +00:00
return ref->mBase->mData.mValue;
}
2010-08-03 12:14:04 +00:00
void Armor::registerSelf()
{
boost::shared_ptr<Class> instance (new Armor);
registerClass (typeid (ESM::Armor).name(), instance);
}
2012-03-13 16:05:38 +00:00
std::string Armor::getUpSoundId (const MWWorld::Ptr& ptr) const
2012-03-13 16:05:38 +00:00
{
int es = getEquipmentSkill(ptr);
if (es == ESM::Skill::LightArmor)
2012-03-13 16:05:38 +00:00
return std::string("Item Armor Light Up");
else if (es == ESM::Skill::MediumArmor)
2012-03-13 16:05:38 +00:00
return std::string("Item Armor Medium Up");
else
return std::string("Item Armor Heavy Up");
}
std::string Armor::getDownSoundId (const MWWorld::Ptr& ptr) const
2012-03-13 16:05:38 +00:00
{
int es = getEquipmentSkill(ptr);
if (es == ESM::Skill::LightArmor)
2012-03-13 16:05:38 +00:00
return std::string("Item Armor Light Down");
else if (es == ESM::Skill::MediumArmor)
2012-03-13 16:05:38 +00:00
return std::string("Item Armor Medium Down");
else
return std::string("Item Armor Heavy Down");
}
2012-04-15 15:52:39 +00:00
std::string Armor::getInventoryIcon (const MWWorld::Ptr& ptr) const
2012-04-15 15:52:39 +00:00
{
MWWorld::LiveCellRef<ESM::Armor> *ref =
2012-04-15 15:52:39 +00:00
ptr.get<ESM::Armor>();
2012-11-05 12:07:59 +00:00
return ref->mBase->mIcon;
2012-04-15 15:52:39 +00:00
}
bool Armor::hasToolTip (const MWWorld::Ptr& ptr) const
{
MWWorld::LiveCellRef<ESM::Armor> *ref =
ptr.get<ESM::Armor>();
2012-11-05 12:07:59 +00:00
return (ref->mBase->mName != "");
}
MWGui::ToolTipInfo Armor::getToolTipInfo (const MWWorld::Ptr& ptr) const
{
MWWorld::LiveCellRef<ESM::Armor> *ref =
ptr.get<ESM::Armor>();
MWGui::ToolTipInfo info;
2012-11-05 12:07:59 +00:00
info.caption = ref->mBase->mName + MWGui::ToolTips::getCountString(ptr.getRefData().getCount());
info.icon = ref->mBase->mIcon;
std::string text;
// get armor type string (light/medium/heavy)
int armorType = getEquipmentSkill(ptr);
std::string typeText;
if (armorType == ESM::Skill::LightArmor)
2012-09-22 19:35:57 +00:00
typeText = "#{sLight}";
else if (armorType == ESM::Skill::MediumArmor)
2012-09-22 19:35:57 +00:00
typeText = "#{sMedium}";
else
2012-09-22 19:35:57 +00:00
typeText = "#{sHeavy}";
2012-11-05 12:07:59 +00:00
text += "\n#{sArmorRating}: " + MWGui::ToolTips::toString(ref->mBase->mData.mArmor);
int remainingHealth = (ptr.getCellRef().mCharge != -1) ? ptr.getCellRef().mCharge : ref->mBase->mData.mHealth;
text += "\n#{sCondition}: " + MWGui::ToolTips::toString(remainingHealth) + "/"
+ MWGui::ToolTips::toString(ref->mBase->mData.mHealth);
2012-11-05 12:07:59 +00:00
text += "\n#{sWeight}: " + MWGui::ToolTips::toString(ref->mBase->mData.mWeight) + " (" + typeText + ")";
text += MWGui::ToolTips::getValueString(ref->mBase->mData.mValue, "#{sValue}");
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");
}
2012-11-05 12:07:59 +00:00
info.enchant = ref->mBase->mEnchant;
info.text = text;
return info;
}
2012-05-12 14:17:03 +00:00
std::string Armor::getEnchantment (const MWWorld::Ptr& ptr) const
{
MWWorld::LiveCellRef<ESM::Armor> *ref =
2012-05-12 14:17:03 +00:00
ptr.get<ESM::Armor>();
2012-11-05 12:07:59 +00:00
return ref->mBase->mEnchant;
2012-05-12 14:17:03 +00:00
}
2013-03-31 21:18:23 +00:00
void Armor::applyEnchantment(const MWWorld::Ptr &ptr, const std::string& enchId, int enchCharge, const std::string& newName) const
2013-03-28 16:41:00 +00:00
{
2013-03-28 22:39:20 +00:00
MWWorld::LiveCellRef<ESM::Armor> *ref =
ptr.get<ESM::Armor>();
ESM::Armor newItem = *ref->mBase;
2013-03-28 16:41:00 +00:00
newItem.mId="";
newItem.mName=newName;
newItem.mData.mEnchant=enchCharge;
newItem.mEnchant=enchId;
const ESM::Armor *record = MWBase::Environment::get().getWorld()->createRecord (newItem);
2013-03-31 21:18:23 +00:00
ref->mBase = record;
2013-03-28 16:41:00 +00:00
}
boost::shared_ptr<MWWorld::Action> Armor::use (const MWWorld::Ptr& ptr) const
{
boost::shared_ptr<MWWorld::Action> action(new MWWorld::ActionEquip(ptr));
action->setSound(getUpSoundId(ptr));
return action;
}
MWWorld::Ptr
Armor::copyToCellImpl(const MWWorld::Ptr &ptr, MWWorld::CellStore &cell) const
{
MWWorld::LiveCellRef<ESM::Armor> *ref =
ptr.get<ESM::Armor>();
2012-11-05 12:07:59 +00:00
return MWWorld::Ptr(&cell.mArmors.insert(*ref), &cell);
}
2013-03-16 18:00:14 +00:00
short Armor::getEnchantmentPoints (const MWWorld::Ptr& ptr) const
{
MWWorld::LiveCellRef<ESM::Armor> *ref =
ptr.get<ESM::Armor>();
return ref->mBase->mData.mEnchant;
}
2010-08-03 12:14:04 +00:00
}