1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-11 00:39:59 +00:00
OpenMW/apps/openmw/mwrender/creatureanimation.cpp

275 lines
8.3 KiB
C++
Raw Normal View History

#include "creatureanimation.hpp"
#include <osg/MatrixTransform>
#include <components/esm3/loadcrea.hpp>
2018-08-14 19:05:43 +00:00
#include <components/debug/debuglog.hpp>
2015-04-19 01:05:47 +00:00
#include <components/resource/resourcesystem.hpp>
#include <components/resource/scenemanager.hpp>
#include <components/sceneutil/attach.hpp>
2015-05-30 23:07:43 +00:00
#include <components/sceneutil/visitor.hpp>
#include <components/sceneutil/positionattitudetransform.hpp>
#include <components/sceneutil/skeleton.hpp>
#include <components/settings/settings.hpp>
#include <components/misc/stringops.hpp>
#include "../mwbase/environment.hpp"
#include "../mwbase/world.hpp"
#include "../mwmechanics/weapontype.hpp"
#include "../mwworld/class.hpp"
#include "../mwworld/esmstore.hpp"
namespace MWRender
{
2011-12-12 04:42:39 +00:00
2015-04-19 01:05:47 +00:00
CreatureAnimation::CreatureAnimation(const MWWorld::Ptr &ptr,
const std::string& model, Resource::ResourceSystem* resourceSystem)
: ActorAnimation(ptr, osg::ref_ptr<osg::Group>(ptr.getRefData().getBaseNode()), resourceSystem)
{
MWWorld::LiveCellRef<ESM::Creature> *ref = mPtr.get<ESM::Creature>();
if(!model.empty())
{
setObjectRoot(model, false, false, true);
if((ref->mBase->mFlags&ESM::Creature::Bipedal))
addAnimSource(Settings::Manager::getString("xbaseanim", "Models"), model);
addAnimSource(model, model);
}
}
2015-04-19 01:05:47 +00:00
CreatureWeaponAnimation::CreatureWeaponAnimation(const MWWorld::Ptr &ptr, const std::string& model, Resource::ResourceSystem* resourceSystem)
: ActorAnimation(ptr, osg::ref_ptr<osg::Group>(ptr.getRefData().getBaseNode()), resourceSystem)
, mShowWeapons(false)
, mShowCarriedLeft(false)
{
MWWorld::LiveCellRef<ESM::Creature> *ref = mPtr.get<ESM::Creature>();
if(!model.empty())
{
setObjectRoot(model, true, false, true);
if((ref->mBase->mFlags&ESM::Creature::Bipedal))
{
addAnimSource(Settings::Manager::getString("xbaseanim", "Models"), model);
}
addAnimSource(model, model);
mPtr.getClass().getInventoryStore(mPtr).setInvListener(this, mPtr);
updateParts();
}
2014-03-12 10:30:44 +00:00
mWeaponAnimationTime = std::make_shared<WeaponAnimationTime>(this);
}
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()
{
mAmmunition.reset();
2015-04-19 01:05:47 +00:00
mWeapon.reset();
mShield.reset();
updateHolsteredWeapon(!mShowWeapons);
updateQuiver();
updateHolsteredShield(mShowCarriedLeft);
if (mShowWeapons)
updatePart(mWeapon, MWWorld::InventoryStore::Slot_CarriedRight);
if (mShowCarriedLeft)
updatePart(mShield, MWWorld::InventoryStore::Slot_CarriedLeft);
}
2015-04-19 01:05:47 +00:00
void CreatureWeaponAnimation::updatePart(PartHolderPtr& scene, int slot)
{
2015-04-19 01:05:47 +00:00
if (!mObjectRoot)
return;
const MWWorld::InventoryStore& inv = mPtr.getClass().getInventoryStore(mPtr);
MWWorld::ConstContainerStoreIterator it = inv.getSlot(slot);
if (it == inv.end())
{
2015-04-19 01:05:47 +00:00
scene.reset();
return;
}
MWWorld::ConstPtr item = *it;
std::string bonename;
std::string itemModel = item.getClass().getModel(item);
if (slot == MWWorld::InventoryStore::Slot_CarriedRight)
{
if(item.getType() == ESM::Weapon::sRecordId)
{
int type = item.get<ESM::Weapon>()->mBase->mData.mType;
bonename = MWMechanics::getWeaponType(type)->mAttachBone;
2019-08-09 06:00:40 +00:00
if (bonename != "Weapon Bone")
{
const NodeMap& nodeMap = getNodeMap();
NodeMap::const_iterator found = nodeMap.find(bonename);
2019-08-09 06:00:40 +00:00
if (found == nodeMap.end())
bonename = "Weapon Bone";
}
}
else
bonename = "Weapon Bone";
}
else
{
bonename = "Shield Bone";
if (item.getType() == ESM::Armor::sRecordId)
{
itemModel = getShieldMesh(item, false);
}
}
try
2014-03-12 10:30:44 +00:00
{
2021-11-02 17:01:22 +00:00
osg::ref_ptr<osg::Node> attached = attach(itemModel, bonename, bonename, item.getType() == ESM::Light::sRecordId);
scene.reset(new PartHolder(attached));
if (!item.getClass().getEnchantment(item).empty())
mGlowUpdater = SceneUtil::addEnchantedGlow(attached, mResourceSystem, item.getClass().getEnchantmentColor(item));
// Crossbows start out with a bolt attached
// FIXME: code duplicated from NpcAnimation
if (slot == MWWorld::InventoryStore::Slot_CarriedRight &&
item.getType() == ESM::Weapon::sRecordId &&
item.get<ESM::Weapon>()->mBase->mData.mType == ESM::Weapon::MarksmanCrossbow)
{
const ESM::WeaponType* weaponInfo = MWMechanics::getWeaponType(ESM::Weapon::MarksmanCrossbow);
MWWorld::ConstContainerStoreIterator ammo = inv.getSlot(MWWorld::InventoryStore::Slot_Ammunition);
if (ammo != inv.end() && ammo->get<ESM::Weapon>()->mBase->mData.mType == weaponInfo->mAmmoType)
attachArrow();
else
mAmmunition.reset();
}
2015-05-30 23:07:43 +00:00
else
mAmmunition.reset();
2014-03-12 10:30:44 +00:00
std::shared_ptr<SceneUtil::ControllerSource> source;
2015-05-30 23:07:43 +00:00
if (slot == MWWorld::InventoryStore::Slot_CarriedRight)
source = mWeaponAnimationTime;
else
source.reset(new NullAnimationTime);
2015-05-30 23:07:43 +00:00
SceneUtil::AssignControllerSourcesVisitor assignVisitor(source);
attached->accept(assignVisitor);
}
catch (std::exception& e)
{
2018-08-14 19:05:43 +00:00
Log(Debug::Error) << "Can not add creature part: " << e.what();
}
}
bool CreatureWeaponAnimation::isArrowAttached() const
{
return mAmmunition != nullptr;
}
void CreatureWeaponAnimation::detachArrow()
{
WeaponAnimation::detachArrow(mPtr);
updateQuiver();
}
2014-03-12 10:30:44 +00:00
void CreatureWeaponAnimation::attachArrow()
{
2015-05-30 23:07:43 +00:00
WeaponAnimation::attachArrow(mPtr);
2019-08-07 05:45:10 +00: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())
SceneUtil::addEnchantedGlow(bone->getChild(0), mResourceSystem, ammo->getClass().getEnchantmentColor(*ammo));
2019-08-07 05:45:10 +00:00
}
updateQuiver();
2014-03-12 10:30:44 +00:00
}
void CreatureWeaponAnimation::releaseArrow(float attackStrength)
2014-03-12 10:30:44 +00:00
{
WeaponAnimation::releaseArrow(mPtr, attackStrength);
updateQuiver();
2015-05-30 23:07:43 +00:00
}
osg::Group *CreatureWeaponAnimation::getArrowBone()
{
if (!mWeapon)
2018-10-09 06:21:12 +00:00
return nullptr;
2015-05-30 23:07:43 +00: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->getType() != ESM::Weapon::sRecordId)
return nullptr;
int type = weapon->get<ESM::Weapon>()->mBase->mData.mType;
int ammoType = MWMechanics::getWeaponType(type)->mAmmoType;
if (ammoType == ESM::Weapon::None)
return nullptr;
// 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-30 23:07:43 +00:00
}
osg::Node *CreatureWeaponAnimation::getWeaponNode()
{
2018-10-09 06:21:12 +00:00
return mWeapon ? mWeapon->getNode().get() : nullptr;
2015-05-30 23:07:43 +00:00
}
Resource::ResourceSystem *CreatureWeaponAnimation::getResourceSystem()
{
return mResourceSystem;
2014-03-12 10:30:44 +00:00
}
2015-05-31 16:53:16 +00:00
void CreatureWeaponAnimation::addControllers()
{
Animation::addControllers();
2015-05-31 16:53:16 +00:00
WeaponAnimation::addControllers(mNodeMap, mActiveControllers, mObjectRoot.get());
}
2015-04-19 01:05:47 +00:00
osg::Vec3f CreatureWeaponAnimation::runAnimation(float duration)
2014-03-12 10:30:44 +00:00
{
2015-05-21 23:54:06 +00:00
osg::Vec3f ret = Animation::runAnimation(duration);
WeaponAnimation::configureControllers(mPtr.getRefData().getPosition().rot[0] + getBodyPitchRadians());
2015-05-21 23:54:06 +00:00
return ret;
2014-03-12 10:30:44 +00:00
}
2012-01-17 14:10:53 +00:00
}