2011-12-11 22:40:00 -05:00
|
|
|
#include "creatureanimation.hpp"
|
2012-07-03 15:32:38 +02:00
|
|
|
|
2017-02-03 23:15:37 +01:00
|
|
|
#include <osg/MatrixTransform>
|
|
|
|
|
2014-02-23 20:11:05 +01:00
|
|
|
#include <components/esm/loadcrea.hpp>
|
2018-08-14 23:05:43 +04:00
|
|
|
#include <components/debug/debuglog.hpp>
|
2015-04-19 03:05:47 +02:00
|
|
|
#include <components/resource/resourcesystem.hpp>
|
|
|
|
#include <components/resource/scenemanager.hpp>
|
|
|
|
#include <components/sceneutil/attach.hpp>
|
2015-05-31 01:07:43 +02:00
|
|
|
#include <components/sceneutil/visitor.hpp>
|
2015-11-20 21:57:04 +01:00
|
|
|
#include <components/sceneutil/positionattitudetransform.hpp>
|
2016-07-02 20:19:55 +02:00
|
|
|
#include <components/sceneutil/skeleton.hpp>
|
2020-12-30 22:11:32 +02:00
|
|
|
#include <components/settings/settings.hpp>
|
2017-02-03 23:15:37 +01:00
|
|
|
#include <components/misc/stringops.hpp>
|
|
|
|
|
2020-01-10 13:00:57 +03:00
|
|
|
#include "../mwbase/environment.hpp"
|
2012-07-03 12:30:50 +02:00
|
|
|
#include "../mwbase/world.hpp"
|
2011-12-11 22:40:00 -05:00
|
|
|
|
2018-12-26 13:45:28 +04:00
|
|
|
#include "../mwmechanics/weapontype.hpp"
|
|
|
|
|
2014-01-19 13:05:26 +01:00
|
|
|
#include "../mwworld/class.hpp"
|
2020-01-10 13:00:57 +03:00
|
|
|
#include "../mwworld/esmstore.hpp"
|
2014-01-19 13:05:26 +01:00
|
|
|
|
2013-01-18 16:21:29 -08:00
|
|
|
namespace MWRender
|
|
|
|
{
|
2011-12-11 23:42:39 -05:00
|
|
|
|
2015-04-19 03:05:47 +02:00
|
|
|
CreatureAnimation::CreatureAnimation(const MWWorld::Ptr &ptr,
|
|
|
|
const std::string& model, Resource::ResourceSystem* resourceSystem)
|
2016-10-08 03:49:50 +02:00
|
|
|
: ActorAnimation(ptr, osg::ref_ptr<osg::Group>(ptr.getRefData().getBaseNode()), resourceSystem)
|
2012-07-12 20:12:18 -07:00
|
|
|
{
|
2015-04-23 20:41:31 +02:00
|
|
|
MWWorld::LiveCellRef<ESM::Creature> *ref = mPtr.get<ESM::Creature>();
|
2011-12-11 22:40:00 -05:00
|
|
|
|
2014-01-19 13:05:26 +01:00
|
|
|
if(!model.empty())
|
2012-07-12 20:12:18 -07:00
|
|
|
{
|
2015-08-15 19:01:21 +02:00
|
|
|
setObjectRoot(model, false, false, true);
|
2014-01-19 13:05:26 +01:00
|
|
|
|
2015-04-23 20:41:31 +02:00
|
|
|
if((ref->mBase->mFlags&ESM::Creature::Bipedal))
|
2020-12-30 22:11:32 +02:00
|
|
|
addAnimSource(Settings::Manager::getString("xbaseanim", "Models"), model);
|
2017-11-10 22:02:43 +01:00
|
|
|
addAnimSource(model, model);
|
2014-01-19 13:05:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-12 20:12:18 -07:00
|
|
|
|
2015-04-19 03:05:47 +02:00
|
|
|
CreatureWeaponAnimation::CreatureWeaponAnimation(const MWWorld::Ptr &ptr, const std::string& model, Resource::ResourceSystem* resourceSystem)
|
2016-10-08 03:49:50 +02:00
|
|
|
: ActorAnimation(ptr, osg::ref_ptr<osg::Group>(ptr.getRefData().getBaseNode()), resourceSystem)
|
2014-01-19 13:31:17 +01:00
|
|
|
, mShowWeapons(false)
|
|
|
|
, mShowCarriedLeft(false)
|
2014-01-19 13:05:26 +01:00
|
|
|
{
|
2015-04-23 20:41:31 +02:00
|
|
|
MWWorld::LiveCellRef<ESM::Creature> *ref = mPtr.get<ESM::Creature>();
|
2014-01-19 13:05:26 +01:00
|
|
|
|
|
|
|
if(!model.empty())
|
|
|
|
{
|
2015-08-15 19:01:21 +02:00
|
|
|
setObjectRoot(model, true, false, true);
|
2013-05-07 16:59:32 -07:00
|
|
|
|
2015-04-23 20:41:31 +02:00
|
|
|
if((ref->mBase->mFlags&ESM::Creature::Bipedal))
|
2018-03-03 14:16:21 +04:00
|
|
|
{
|
2020-12-30 22:11:32 +02:00
|
|
|
addAnimSource(Settings::Manager::getString("xbaseanim", "Models"), model);
|
2018-03-03 14:16:21 +04:00
|
|
|
}
|
2017-11-10 22:02:43 +01:00
|
|
|
addAnimSource(model, model);
|
2014-01-19 13:05:26 +01:00
|
|
|
|
2016-10-08 03:49:50 +02:00
|
|
|
mPtr.getClass().getInventoryStore(mPtr).setInvListener(this, mPtr);
|
2014-01-19 13:05:26 +01:00
|
|
|
|
|
|
|
updateParts();
|
|
|
|
}
|
2014-03-12 11:30:44 +01:00
|
|
|
|
2017-05-05 19:26:09 +02:00
|
|
|
mWeaponAnimationTime = std::shared_ptr<WeaponAnimationTime>(new WeaponAnimationTime(this));
|
2014-01-19 13:05:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CreatureWeaponAnimation::showWeapons(bool showWeapon)
|
|
|
|
{
|
|
|
|
if (showWeapon != mShowWeapons)
|
|
|
|
{
|
|
|
|
mShowWeapons = showWeapon;
|
|
|
|
updateParts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CreatureWeaponAnimation::showCarriedLeft(bool show)
|
|
|
|
{
|
|
|
|
if (show != mShowCarriedLeft)
|
|
|
|
{
|
|
|
|
mShowCarriedLeft = show;
|
|
|
|
updateParts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CreatureWeaponAnimation::updateParts()
|
|
|
|
{
|
2016-06-07 02:42:57 +02:00
|
|
|
mAmmunition.reset();
|
2015-04-19 03:05:47 +02:00
|
|
|
mWeapon.reset();
|
|
|
|
mShield.reset();
|
2014-01-19 13:05:26 +01:00
|
|
|
|
2018-03-03 14:16:21 +04:00
|
|
|
updateHolsteredWeapon(!mShowWeapons);
|
|
|
|
updateQuiver();
|
2019-08-11 15:01:48 +04:00
|
|
|
updateHolsteredShield(mShowCarriedLeft);
|
2018-03-03 14:16:21 +04:00
|
|
|
|
2014-01-19 13:05:26 +01:00
|
|
|
if (mShowWeapons)
|
|
|
|
updatePart(mWeapon, MWWorld::InventoryStore::Slot_CarriedRight);
|
|
|
|
if (mShowCarriedLeft)
|
|
|
|
updatePart(mShield, MWWorld::InventoryStore::Slot_CarriedLeft);
|
|
|
|
}
|
|
|
|
|
2015-04-19 03:05:47 +02:00
|
|
|
void CreatureWeaponAnimation::updatePart(PartHolderPtr& scene, int slot)
|
2014-01-19 13:05:26 +01:00
|
|
|
{
|
2015-04-19 03:05:47 +02:00
|
|
|
if (!mObjectRoot)
|
2015-03-02 15:19:57 +01:00
|
|
|
return;
|
|
|
|
|
2017-02-28 14:31:51 +00:00
|
|
|
const MWWorld::InventoryStore& inv = mPtr.getClass().getInventoryStore(mPtr);
|
|
|
|
MWWorld::ConstContainerStoreIterator it = inv.getSlot(slot);
|
2014-01-19 13:05:26 +01:00
|
|
|
|
|
|
|
if (it == inv.end())
|
|
|
|
{
|
2015-04-19 03:05:47 +02:00
|
|
|
scene.reset();
|
2014-01-19 13:05:26 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-02-28 14:31:51 +00:00
|
|
|
MWWorld::ConstPtr item = *it;
|
2014-01-19 13:05:26 +01:00
|
|
|
|
|
|
|
std::string bonename;
|
2020-01-10 13:00:57 +03:00
|
|
|
std::string itemModel = item.getClass().getModel(item);
|
2014-01-19 13:05:26 +01:00
|
|
|
if (slot == MWWorld::InventoryStore::Slot_CarriedRight)
|
2018-12-26 13:45:28 +04:00
|
|
|
{
|
|
|
|
if(item.getTypeName() == typeid(ESM::Weapon).name())
|
|
|
|
{
|
|
|
|
int type = item.get<ESM::Weapon>()->mBase->mData.mType;
|
|
|
|
bonename = MWMechanics::getWeaponType(type)->mAttachBone;
|
2019-08-09 10:00:40 +04:00
|
|
|
if (bonename != "Weapon Bone")
|
|
|
|
{
|
|
|
|
const NodeMap& nodeMap = getNodeMap();
|
|
|
|
NodeMap::const_iterator found = nodeMap.find(Misc::StringUtils::lowerCase(bonename));
|
|
|
|
if (found == nodeMap.end())
|
|
|
|
bonename = "Weapon Bone";
|
|
|
|
}
|
2018-12-26 13:45:28 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
bonename = "Weapon Bone";
|
|
|
|
}
|
2014-01-19 13:05:26 +01:00
|
|
|
else
|
2020-01-10 13:00:57 +03:00
|
|
|
{
|
2014-01-19 13:05:26 +01:00
|
|
|
bonename = "Shield Bone";
|
2020-01-10 13:00:57 +03:00
|
|
|
if (item.getTypeName() == typeid(ESM::Armor).name())
|
|
|
|
{
|
|
|
|
// Shield body part model should be used if possible.
|
|
|
|
const MWWorld::ESMStore &store = MWBase::Environment::get().getWorld()->getStore();
|
|
|
|
for (const auto& part : item.get<ESM::Armor>()->mBase->mParts.mParts)
|
|
|
|
{
|
|
|
|
// Assume all creatures use the male mesh.
|
|
|
|
if (part.mPart != ESM::PRT_Shield || part.mMale.empty())
|
|
|
|
continue;
|
|
|
|
const ESM::BodyPart *bodypart = store.get<ESM::BodyPart>().search(part.mMale);
|
|
|
|
if (bodypart && bodypart->mData.mType == ESM::BodyPart::MT_Armor && !bodypart->mModel.empty())
|
|
|
|
{
|
|
|
|
itemModel = "meshes\\" + bodypart->mModel;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-19 13:05:26 +01:00
|
|
|
|
2017-02-03 23:15:37 +01:00
|
|
|
try
|
2014-03-12 11:30:44 +01:00
|
|
|
{
|
2021-09-09 20:56:57 +00:00
|
|
|
osg::ref_ptr<const osg::Node> node = mResourceSystem->getSceneManager()->getTemplate(itemModel);
|
2017-02-03 23:15:37 +01:00
|
|
|
|
|
|
|
const NodeMap& nodeMap = getNodeMap();
|
2017-09-09 23:22:16 +03:00
|
|
|
NodeMap::const_iterator found = nodeMap.find(Misc::StringUtils::lowerCase(bonename));
|
2017-02-03 23:15:37 +01:00
|
|
|
if (found == nodeMap.end())
|
|
|
|
throw std::runtime_error("Can't find attachment node " + bonename);
|
|
|
|
osg::ref_ptr<osg::Node> attached = SceneUtil::attach(node, mObjectRoot, bonename, found->second.get());
|
|
|
|
|
|
|
|
scene.reset(new PartHolder(attached));
|
|
|
|
|
|
|
|
if (!item.getClass().getEnchantment(item).empty())
|
2019-08-07 11:03:26 +04:00
|
|
|
mGlowUpdater = SceneUtil::addEnchantedGlow(attached, mResourceSystem, item.getClass().getEnchantmentColor(item));
|
2017-02-03 23:15:37 +01:00
|
|
|
|
|
|
|
// Crossbows start out with a bolt attached
|
|
|
|
// FIXME: code duplicated from NpcAnimation
|
|
|
|
if (slot == MWWorld::InventoryStore::Slot_CarriedRight &&
|
|
|
|
item.getTypeName() == typeid(ESM::Weapon).name() &&
|
|
|
|
item.get<ESM::Weapon>()->mBase->mData.mType == ESM::Weapon::MarksmanCrossbow)
|
|
|
|
{
|
2018-12-26 13:45:28 +04:00
|
|
|
const ESM::WeaponType* weaponInfo = MWMechanics::getWeaponType(ESM::Weapon::MarksmanCrossbow);
|
2017-02-28 14:31:51 +00:00
|
|
|
MWWorld::ConstContainerStoreIterator ammo = inv.getSlot(MWWorld::InventoryStore::Slot_Ammunition);
|
2018-12-26 13:45:28 +04:00
|
|
|
if (ammo != inv.end() && ammo->get<ESM::Weapon>()->mBase->mData.mType == weaponInfo->mAmmoType)
|
2017-02-03 23:15:37 +01:00
|
|
|
attachArrow();
|
|
|
|
else
|
|
|
|
mAmmunition.reset();
|
|
|
|
}
|
2015-05-31 01:07:43 +02:00
|
|
|
else
|
|
|
|
mAmmunition.reset();
|
2014-03-12 11:30:44 +01:00
|
|
|
|
2017-05-05 19:26:09 +02:00
|
|
|
std::shared_ptr<SceneUtil::ControllerSource> source;
|
2015-05-31 01:07:43 +02:00
|
|
|
|
2017-02-03 23:15:37 +01:00
|
|
|
if (slot == MWWorld::InventoryStore::Slot_CarriedRight)
|
|
|
|
source = mWeaponAnimationTime;
|
|
|
|
else
|
|
|
|
source.reset(new NullAnimationTime);
|
2015-05-31 01:07:43 +02:00
|
|
|
|
2017-02-03 23:15:37 +01:00
|
|
|
SceneUtil::AssignControllerSourcesVisitor assignVisitor(source);
|
|
|
|
attached->accept(assignVisitor);
|
|
|
|
}
|
|
|
|
catch (std::exception& e)
|
|
|
|
{
|
2018-08-14 23:05:43 +04:00
|
|
|
Log(Debug::Error) << "Can not add creature part: " << e.what();
|
2017-02-03 23:15:37 +01:00
|
|
|
}
|
2011-12-11 22:40:00 -05:00
|
|
|
}
|
2011-12-25 22:37:26 -05:00
|
|
|
|
2018-03-03 14:16:21 +04:00
|
|
|
bool CreatureWeaponAnimation::isArrowAttached() const
|
|
|
|
{
|
|
|
|
return mAmmunition != nullptr;
|
|
|
|
}
|
|
|
|
|
2020-08-28 15:28:26 +04:00
|
|
|
void CreatureWeaponAnimation::detachArrow()
|
|
|
|
{
|
|
|
|
WeaponAnimation::detachArrow(mPtr);
|
|
|
|
updateQuiver();
|
|
|
|
}
|
|
|
|
|
2014-03-12 11:30:44 +01:00
|
|
|
void CreatureWeaponAnimation::attachArrow()
|
|
|
|
{
|
2015-05-31 01:07:43 +02:00
|
|
|
WeaponAnimation::attachArrow(mPtr);
|
2019-08-07 09:45:10 +04:00
|
|
|
|
|
|
|
const MWWorld::InventoryStore& inv = mPtr.getClass().getInventoryStore(mPtr);
|
|
|
|
MWWorld::ConstContainerStoreIterator ammo = inv.getSlot(MWWorld::InventoryStore::Slot_Ammunition);
|
|
|
|
if (ammo != inv.end() && !ammo->getClass().getEnchantment(*ammo).empty())
|
|
|
|
{
|
|
|
|
osg::Group* bone = getArrowBone();
|
|
|
|
if (bone != nullptr && bone->getNumChildren())
|
2019-08-07 11:03:26 +04:00
|
|
|
SceneUtil::addEnchantedGlow(bone->getChild(0), mResourceSystem, ammo->getClass().getEnchantmentColor(*ammo));
|
2019-08-07 09:45:10 +04:00
|
|
|
}
|
|
|
|
|
2018-03-03 14:16:21 +04:00
|
|
|
updateQuiver();
|
2014-03-12 11:30:44 +01:00
|
|
|
}
|
|
|
|
|
2015-06-26 05:15:07 +02:00
|
|
|
void CreatureWeaponAnimation::releaseArrow(float attackStrength)
|
2014-03-12 11:30:44 +01:00
|
|
|
{
|
2015-06-26 05:15:07 +02:00
|
|
|
WeaponAnimation::releaseArrow(mPtr, attackStrength);
|
2018-03-03 14:16:21 +04:00
|
|
|
updateQuiver();
|
2015-05-31 01:07:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
osg::Group *CreatureWeaponAnimation::getArrowBone()
|
|
|
|
{
|
|
|
|
if (!mWeapon)
|
2018-10-09 10:21:12 +04:00
|
|
|
return nullptr;
|
2015-05-31 01:07:43 +02:00
|
|
|
|
2018-12-26 13:45:28 +04:00
|
|
|
if (!mPtr.getClass().hasInventoryStore(mPtr))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
const MWWorld::InventoryStore& inv = mPtr.getClass().getInventoryStore(mPtr);
|
|
|
|
MWWorld::ConstContainerStoreIterator weapon = inv.getSlot(MWWorld::InventoryStore::Slot_CarriedRight);
|
|
|
|
if(weapon == inv.end() || weapon->getTypeName() != typeid(ESM::Weapon).name())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
int type = weapon->get<ESM::Weapon>()->mBase->mData.mType;
|
|
|
|
int ammoType = MWMechanics::getWeaponType(type)->mAmmoType;
|
|
|
|
|
2020-09-09 10:14:26 +04:00
|
|
|
// Try to find and attachment bone in actor's skeleton, otherwise fall back to the ArrowBone in weapon's mesh
|
|
|
|
osg::Group* bone = getBoneByName(MWMechanics::getWeaponType(ammoType)->mAttachBone);
|
|
|
|
if (bone == nullptr)
|
|
|
|
{
|
|
|
|
SceneUtil::FindByNameVisitor findVisitor ("ArrowBone");
|
|
|
|
mWeapon->getNode()->accept(findVisitor);
|
|
|
|
bone = findVisitor.mFoundNode;
|
|
|
|
}
|
|
|
|
return bone;
|
2015-05-31 01:07:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
osg::Node *CreatureWeaponAnimation::getWeaponNode()
|
|
|
|
{
|
2018-10-09 10:21:12 +04:00
|
|
|
return mWeapon ? mWeapon->getNode().get() : nullptr;
|
2015-05-31 01:07:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Resource::ResourceSystem *CreatureWeaponAnimation::getResourceSystem()
|
|
|
|
{
|
|
|
|
return mResourceSystem;
|
2014-03-12 11:30:44 +01:00
|
|
|
}
|
|
|
|
|
2015-05-31 18:53:16 +02:00
|
|
|
void CreatureWeaponAnimation::addControllers()
|
|
|
|
{
|
2015-06-22 21:06:27 +02:00
|
|
|
Animation::addControllers();
|
2015-05-31 18:53:16 +02:00
|
|
|
WeaponAnimation::addControllers(mNodeMap, mActiveControllers, mObjectRoot.get());
|
|
|
|
}
|
|
|
|
|
2015-04-19 03:05:47 +02:00
|
|
|
osg::Vec3f CreatureWeaponAnimation::runAnimation(float duration)
|
2014-03-12 11:30:44 +01:00
|
|
|
{
|
2015-05-22 01:54:06 +02:00
|
|
|
osg::Vec3f ret = Animation::runAnimation(duration);
|
2015-03-02 15:19:57 +01:00
|
|
|
|
2020-07-09 06:47:37 +00:00
|
|
|
WeaponAnimation::configureControllers(mPtr.getRefData().getPosition().rot[0] + getBodyPitchRadians());
|
2014-06-27 03:11:37 +02:00
|
|
|
|
2015-05-22 01:54:06 +02:00
|
|
|
return ret;
|
2014-03-12 11:30:44 +01:00
|
|
|
}
|
|
|
|
|
2012-01-17 15:10:53 +01:00
|
|
|
}
|