1 #include "npcanimation.hpp"
2 
3 #include <osg/UserDataContainer>
4 #include <osg/MatrixTransform>
5 #include <osg/Depth>
6 
7 #include <osgUtil/RenderBin>
8 #include <osgUtil/CullVisitor>
9 
10 #include <components/debug/debuglog.hpp>
11 
12 #include <components/misc/rng.hpp>
13 
14 #include <components/misc/resourcehelpers.hpp>
15 
16 #include <components/resource/resourcesystem.hpp>
17 #include <components/resource/scenemanager.hpp>
18 #include <components/sceneutil/actorutil.hpp>
19 #include <components/sceneutil/attach.hpp>
20 #include <components/sceneutil/visitor.hpp>
21 #include <components/sceneutil/skeleton.hpp>
22 #include <components/sceneutil/keyframe.hpp>
23 
24 #include <components/settings/settings.hpp>
25 
26 #include <components/vfs/manager.hpp>
27 
28 #include "../mwworld/esmstore.hpp"
29 #include "../mwworld/inventorystore.hpp"
30 #include "../mwworld/class.hpp"
31 #include "../mwworld/player.hpp"
32 
33 #include "../mwmechanics/npcstats.hpp"
34 #include "../mwmechanics/actorutil.hpp"
35 #include "../mwmechanics/weapontype.hpp"
36 
37 #include "../mwbase/environment.hpp"
38 #include "../mwbase/world.hpp"
39 #include "../mwbase/mechanicsmanager.hpp"
40 #include "../mwbase/soundmanager.hpp"
41 
42 #include "camera.hpp"
43 #include "rotatecontroller.hpp"
44 #include "renderbin.hpp"
45 #include "vismask.hpp"
46 
47 namespace
48 {
49 
getVampireHead(const std::string & race,bool female)50 std::string getVampireHead(const std::string& race, bool female)
51 {
52     static std::map <std::pair<std::string,int>, const ESM::BodyPart* > sVampireMapping;
53 
54     std::pair<std::string, int> thisCombination = std::make_pair(race, int(female));
55 
56     if (sVampireMapping.find(thisCombination) == sVampireMapping.end())
57     {
58         const MWWorld::ESMStore &store = MWBase::Environment::get().getWorld()->getStore();
59         for (const ESM::BodyPart& bodypart : store.get<ESM::BodyPart>())
60         {
61             if (!bodypart.mData.mVampire)
62                 continue;
63             if (bodypart.mData.mType != ESM::BodyPart::MT_Skin)
64                 continue;
65             if (bodypart.mData.mPart != ESM::BodyPart::MP_Head)
66                 continue;
67             if (female != (bodypart.mData.mFlags & ESM::BodyPart::BPF_Female))
68                 continue;
69             if (!Misc::StringUtils::ciEqual(bodypart.mRace, race))
70                 continue;
71             sVampireMapping[thisCombination] = &bodypart;
72         }
73     }
74 
75     sVampireMapping.emplace(thisCombination, nullptr);
76 
77     const ESM::BodyPart* bodyPart = sVampireMapping[thisCombination];
78     if (!bodyPart)
79         return std::string();
80     return "meshes\\" + bodyPart->mModel;
81 }
82 
getShieldBodypartMesh(const std::vector<ESM::PartReference> & bodyparts,bool female)83 std::string getShieldBodypartMesh(const std::vector<ESM::PartReference>& bodyparts, bool female)
84 {
85     const MWWorld::ESMStore &store = MWBase::Environment::get().getWorld()->getStore();
86     const MWWorld::Store<ESM::BodyPart> &partStore = store.get<ESM::BodyPart>();
87     for (const auto& part : bodyparts)
88     {
89         if (part.mPart != ESM::PRT_Shield)
90             continue;
91 
92         std::string bodypartName;
93         if (female && !part.mFemale.empty())
94             bodypartName = part.mFemale;
95         else if (!part.mMale.empty())
96             bodypartName = part.mMale;
97 
98         if (!bodypartName.empty())
99         {
100             const ESM::BodyPart *bodypart = partStore.search(bodypartName);
101             if (bodypart == nullptr || bodypart->mData.mType != ESM::BodyPart::MT_Armor)
102                 return std::string();
103             if (!bodypart->mModel.empty())
104                 return "meshes\\" + bodypart->mModel;
105         }
106     }
107 
108     return std::string();
109 }
110 
111 }
112 
113 
114 namespace MWRender
115 {
116 
117 class HeadAnimationTime : public SceneUtil::ControllerSource
118 {
119 private:
120     MWWorld::Ptr mReference;
121     float mTalkStart;
122     float mTalkStop;
123     float mBlinkStart;
124     float mBlinkStop;
125 
126     float mBlinkTimer;
127 
128     bool mEnabled;
129 
130     float mValue;
131 private:
132     void resetBlinkTimer();
133 public:
134     HeadAnimationTime(const MWWorld::Ptr& reference);
135 
136     void updatePtr(const MWWorld::Ptr& updated);
137 
138     void update(float dt);
139 
140     void setEnabled(bool enabled);
141 
142     void setTalkStart(float value);
143     void setTalkStop(float value);
144     void setBlinkStart(float value);
145     void setBlinkStop(float value);
146 
147     float getValue(osg::NodeVisitor* nv) override;
148 };
149 
150 // --------------------------------------------------------------------------------
151 
152 /// Subclass RotateController to add a Z-offset for sneaking in first person mode.
153 /// @note We use inheritance instead of adding another controller, so that we do not have to compute the worldOrient twice.
154 /// @note Must be set on a MatrixTransform.
155 class NeckController : public RotateController
156 {
157 public:
NeckController(osg::Node * relativeTo)158     NeckController(osg::Node* relativeTo)
159         : RotateController(relativeTo)
160     {
161     }
162 
setOffset(const osg::Vec3f & offset)163     void setOffset(const osg::Vec3f& offset)
164     {
165         mOffset = offset;
166     }
167 
operator ()(osg::Node * node,osg::NodeVisitor * nv)168     void operator()(osg::Node* node, osg::NodeVisitor* nv) override
169     {
170         osg::MatrixTransform* transform = static_cast<osg::MatrixTransform*>(node);
171         osg::Matrix matrix = transform->getMatrix();
172 
173         osg::Quat worldOrient = getWorldOrientation(node);
174         osg::Quat orient = worldOrient * mRotate * worldOrient.inverse() * matrix.getRotate();
175 
176         matrix.setRotate(orient);
177         matrix.setTrans(matrix.getTrans() + worldOrient.inverse() * mOffset);
178 
179         transform->setMatrix(matrix);
180 
181         traverse(node,nv);
182     }
183 
184 private:
185     osg::Vec3f mOffset;
186 };
187 
188 // --------------------------------------------------------------------------------------------------------------
189 
HeadAnimationTime(const MWWorld::Ptr & reference)190 HeadAnimationTime::HeadAnimationTime(const MWWorld::Ptr& reference)
191     : mReference(reference), mTalkStart(0), mTalkStop(0), mBlinkStart(0), mBlinkStop(0), mEnabled(true), mValue(0)
192 {
193     resetBlinkTimer();
194 }
195 
updatePtr(const MWWorld::Ptr & updated)196 void HeadAnimationTime::updatePtr(const MWWorld::Ptr &updated)
197 {
198     mReference = updated;
199 }
200 
setEnabled(bool enabled)201 void HeadAnimationTime::setEnabled(bool enabled)
202 {
203     mEnabled = enabled;
204 }
205 
resetBlinkTimer()206 void HeadAnimationTime::resetBlinkTimer()
207 {
208     mBlinkTimer = -(2.0f + Misc::Rng::rollDice(6));
209 }
210 
update(float dt)211 void HeadAnimationTime::update(float dt)
212 {
213     if (!mEnabled)
214         return;
215 
216     if (!MWBase::Environment::get().getSoundManager()->sayActive(mReference))
217     {
218         mBlinkTimer += dt;
219 
220         float duration = mBlinkStop - mBlinkStart;
221 
222         if (mBlinkTimer >= 0 && mBlinkTimer <= duration)
223         {
224             mValue = mBlinkStart + mBlinkTimer;
225         }
226         else
227             mValue = mBlinkStop;
228 
229         if (mBlinkTimer > duration)
230             resetBlinkTimer();
231     }
232     else
233     {
234         // FIXME: would be nice to hold on to the SoundPtr so we don't have to retrieve it every frame
235         mValue = mTalkStart +
236             (mTalkStop - mTalkStart) *
237             std::min(1.f, MWBase::Environment::get().getSoundManager()->getSaySoundLoudness(mReference)*2); // Rescale a bit (most voices are not very loud)
238     }
239 }
240 
getValue(osg::NodeVisitor *)241 float HeadAnimationTime::getValue(osg::NodeVisitor*)
242 {
243     return mValue;
244 }
245 
setTalkStart(float value)246 void HeadAnimationTime::setTalkStart(float value)
247 {
248     mTalkStart = value;
249 }
250 
setTalkStop(float value)251 void HeadAnimationTime::setTalkStop(float value)
252 {
253     mTalkStop = value;
254 }
255 
setBlinkStart(float value)256 void HeadAnimationTime::setBlinkStart(float value)
257 {
258     mBlinkStart = value;
259 }
260 
setBlinkStop(float value)261 void HeadAnimationTime::setBlinkStop(float value)
262 {
263     mBlinkStop = value;
264 }
265 
266 // ----------------------------------------------------
267 
getNpcType() const268 NpcAnimation::NpcType NpcAnimation::getNpcType() const
269 {
270     const MWWorld::Class &cls = mPtr.getClass();
271     // Dead vampires should typically stay vampires.
272     if (mNpcType == Type_Vampire && cls.getNpcStats(mPtr).isDead() && !cls.getNpcStats(mPtr).isWerewolf())
273         return mNpcType;
274     return getNpcType(mPtr);
275 }
276 
getNpcType(const MWWorld::Ptr & ptr)277 NpcAnimation::NpcType NpcAnimation::getNpcType(const MWWorld::Ptr& ptr)
278 {
279     const MWWorld::Class &cls = ptr.getClass();
280     NpcAnimation::NpcType curType = Type_Normal;
281     if (cls.getCreatureStats(ptr).getMagicEffects().get(ESM::MagicEffect::Vampirism).getMagnitude() > 0)
282         curType = Type_Vampire;
283     if (cls.getNpcStats(ptr).isWerewolf())
284         curType = Type_Werewolf;
285 
286     return curType;
287 }
288 
createPartListMap()289 static NpcAnimation::PartBoneMap createPartListMap()
290 {
291     NpcAnimation::PartBoneMap result;
292     result.insert(std::make_pair(ESM::PRT_Head, "Head"));
293     result.insert(std::make_pair(ESM::PRT_Hair, "Head")); // note it uses "Head" as attach bone, but "Hair" as filter
294     result.insert(std::make_pair(ESM::PRT_Neck, "Neck"));
295     result.insert(std::make_pair(ESM::PRT_Cuirass, "Chest"));
296     result.insert(std::make_pair(ESM::PRT_Groin, "Groin"));
297     result.insert(std::make_pair(ESM::PRT_Skirt, "Groin"));
298     result.insert(std::make_pair(ESM::PRT_RHand, "Right Hand"));
299     result.insert(std::make_pair(ESM::PRT_LHand, "Left Hand"));
300     result.insert(std::make_pair(ESM::PRT_RWrist, "Right Wrist"));
301     result.insert(std::make_pair(ESM::PRT_LWrist, "Left Wrist"));
302     result.insert(std::make_pair(ESM::PRT_Shield, "Shield Bone"));
303     result.insert(std::make_pair(ESM::PRT_RForearm, "Right Forearm"));
304     result.insert(std::make_pair(ESM::PRT_LForearm, "Left Forearm"));
305     result.insert(std::make_pair(ESM::PRT_RUpperarm, "Right Upper Arm"));
306     result.insert(std::make_pair(ESM::PRT_LUpperarm, "Left Upper Arm"));
307     result.insert(std::make_pair(ESM::PRT_RFoot, "Right Foot"));
308     result.insert(std::make_pair(ESM::PRT_LFoot, "Left Foot"));
309     result.insert(std::make_pair(ESM::PRT_RAnkle, "Right Ankle"));
310     result.insert(std::make_pair(ESM::PRT_LAnkle, "Left Ankle"));
311     result.insert(std::make_pair(ESM::PRT_RKnee, "Right Knee"));
312     result.insert(std::make_pair(ESM::PRT_LKnee, "Left Knee"));
313     result.insert(std::make_pair(ESM::PRT_RLeg, "Right Upper Leg"));
314     result.insert(std::make_pair(ESM::PRT_LLeg, "Left Upper Leg"));
315     result.insert(std::make_pair(ESM::PRT_RPauldron, "Right Clavicle"));
316     result.insert(std::make_pair(ESM::PRT_LPauldron, "Left Clavicle"));
317     result.insert(std::make_pair(ESM::PRT_Weapon, "Weapon Bone")); // Fallback. The real node name depends on the current weapon type.
318     result.insert(std::make_pair(ESM::PRT_Tail, "Tail"));
319     return result;
320 }
321 const NpcAnimation::PartBoneMap NpcAnimation::sPartList = createPartListMap();
322 
~NpcAnimation()323 NpcAnimation::~NpcAnimation()
324 {
325     mAmmunition.reset();
326 }
327 
NpcAnimation(const MWWorld::Ptr & ptr,osg::ref_ptr<osg::Group> parentNode,Resource::ResourceSystem * resourceSystem,bool disableSounds,ViewMode viewMode,float firstPersonFieldOfView)328 NpcAnimation::NpcAnimation(const MWWorld::Ptr& ptr, osg::ref_ptr<osg::Group> parentNode, Resource::ResourceSystem* resourceSystem,
329                            bool disableSounds, ViewMode viewMode, float firstPersonFieldOfView)
330   : ActorAnimation(ptr, parentNode, resourceSystem),
331     mViewMode(viewMode),
332     mShowWeapons(false),
333     mShowCarriedLeft(true),
334     mNpcType(getNpcType(ptr)),
335     mFirstPersonFieldOfView(firstPersonFieldOfView),
336     mSoundsDisabled(disableSounds),
337     mAccurateAiming(false),
338     mAimingFactor(0.f)
339 {
340     mNpc = mPtr.get<ESM::NPC>()->mBase;
341 
342     mHeadAnimationTime = std::shared_ptr<HeadAnimationTime>(new HeadAnimationTime(mPtr));
343     mWeaponAnimationTime = std::shared_ptr<WeaponAnimationTime>(new WeaponAnimationTime(this));
344 
345     for(size_t i = 0;i < ESM::PRT_Count;i++)
346     {
347         mPartslots[i] = -1;  //each slot is empty
348         mPartPriorities[i] = 0;
349     }
350 
351     std::fill(mSounds.begin(), mSounds.end(), nullptr);
352 
353     updateNpcBase();
354 }
355 
setViewMode(NpcAnimation::ViewMode viewMode)356 void NpcAnimation::setViewMode(NpcAnimation::ViewMode viewMode)
357 {
358     assert(viewMode != VM_HeadOnly);
359     if(mViewMode == viewMode)
360         return;
361 
362     mViewMode = viewMode;
363     MWBase::Environment::get().getWorld()->scaleObject(mPtr, mPtr.getCellRef().getScale()); // apply race height after view change
364 
365     mAmmunition.reset();
366     rebuild();
367     setRenderBin();
368 }
369 
370 /// @brief A RenderBin callback to clear the depth buffer before rendering.
371 class DepthClearCallback : public osgUtil::RenderBin::DrawCallback
372 {
373 public:
DepthClearCallback()374     DepthClearCallback()
375     {
376         mDepth = new osg::Depth;
377         mDepth->setWriteMask(true);
378     }
379 
drawImplementation(osgUtil::RenderBin * bin,osg::RenderInfo & renderInfo,osgUtil::RenderLeaf * & previous)380     void drawImplementation(osgUtil::RenderBin* bin, osg::RenderInfo& renderInfo, osgUtil::RenderLeaf*& previous) override
381     {
382         renderInfo.getState()->applyAttribute(mDepth);
383 
384         glClear(GL_DEPTH_BUFFER_BIT);
385 
386         bin->drawImplementation(renderInfo, previous);
387     }
388 
389     osg::ref_ptr<osg::Depth> mDepth;
390 };
391 
392 /// Overrides Field of View to given value for rendering the subgraph.
393 /// Must be added as cull callback.
394 class OverrideFieldOfViewCallback : public osg::NodeCallback
395 {
396 public:
OverrideFieldOfViewCallback(float fov)397     OverrideFieldOfViewCallback(float fov)
398         : mFov(fov)
399     {
400     }
401 
operator ()(osg::Node * node,osg::NodeVisitor * nv)402     void operator()(osg::Node* node, osg::NodeVisitor* nv) override
403     {
404         osgUtil::CullVisitor* cv = static_cast<osgUtil::CullVisitor*>(nv);
405         float fov, aspect, zNear, zFar;
406         if (cv->getProjectionMatrix()->getPerspective(fov, aspect, zNear, zFar))
407         {
408             fov = mFov;
409             osg::ref_ptr<osg::RefMatrix> newProjectionMatrix = new osg::RefMatrix();
410             newProjectionMatrix->makePerspective(fov, aspect, zNear, zFar);
411             osg::ref_ptr<osg::RefMatrix> invertedOldMatrix = cv->getProjectionMatrix();
412             invertedOldMatrix = new osg::RefMatrix(osg::RefMatrix::inverse(*invertedOldMatrix));
413             osg::ref_ptr<osg::RefMatrix> viewMatrix = new osg::RefMatrix(*cv->getModelViewMatrix());
414             viewMatrix->postMult(*newProjectionMatrix);
415             viewMatrix->postMult(*invertedOldMatrix);
416             cv->pushModelViewMatrix(viewMatrix, osg::Transform::ReferenceFrame::ABSOLUTE_RF);
417             traverse(node, nv);
418             cv->popModelViewMatrix();
419         }
420         else
421             traverse(node, nv);
422     }
423 
424 private:
425     float mFov;
426 };
427 
setRenderBin()428 void NpcAnimation::setRenderBin()
429 {
430     if (mViewMode == VM_FirstPerson)
431     {
432         static bool prototypeAdded = false;
433         if (!prototypeAdded)
434         {
435             osg::ref_ptr<osgUtil::RenderBin> depthClearBin (new osgUtil::RenderBin);
436             depthClearBin->setDrawCallback(new DepthClearCallback);
437             osgUtil::RenderBin::addRenderBinPrototype("DepthClear", depthClearBin);
438             prototypeAdded = true;
439         }
440         mObjectRoot->getOrCreateStateSet()->setRenderBinDetails(RenderBin_FirstPerson, "DepthClear", osg::StateSet::OVERRIDE_RENDERBIN_DETAILS);
441     }
442     else if (osg::StateSet* stateset = mObjectRoot->getStateSet())
443         stateset->setRenderBinToInherit();
444 }
445 
rebuild()446 void NpcAnimation::rebuild()
447 {
448     mScabbard.reset();
449     mHolsteredShield.reset();
450     updateNpcBase();
451 
452     MWBase::Environment::get().getMechanicsManager()->forceStateUpdate(mPtr);
453 }
454 
getSlot(const osg::NodePath & path) const455 int NpcAnimation::getSlot(const osg::NodePath &path) const
456 {
457     for (int i=0; i<ESM::PRT_Count; ++i)
458     {
459         PartHolderPtr part = mObjectParts[i];
460         if (!part.get())
461             continue;
462         if (std::find(path.begin(), path.end(), part->getNode().get()) != path.end())
463         {
464             return mPartslots[i];
465         }
466     }
467     return -1;
468 }
469 
updateNpcBase()470 void NpcAnimation::updateNpcBase()
471 {
472     clearAnimSources();
473     for(size_t i = 0;i < ESM::PRT_Count;i++)
474         removeIndividualPart((ESM::PartReferenceType)i);
475 
476     const MWWorld::ESMStore &store = MWBase::Environment::get().getWorld()->getStore();
477     const ESM::Race *race = store.get<ESM::Race>().find(mNpc->mRace);
478     NpcType curType = getNpcType();
479     bool isWerewolf = (curType == Type_Werewolf);
480     bool isVampire = (curType == Type_Vampire);
481     bool isFemale = !mNpc->isMale();
482 
483     mHeadModel.clear();
484     mHairModel.clear();
485 
486     std::string headName = isWerewolf ? "WerewolfHead" : mNpc->mHead;
487     std::string hairName = isWerewolf ? "WerewolfHair" : mNpc->mHair;
488 
489     if (!headName.empty())
490     {
491         const ESM::BodyPart* bp = store.get<ESM::BodyPart>().search(headName);
492         if (bp)
493             mHeadModel = "meshes\\" + bp->mModel;
494         else
495             Log(Debug::Warning) << "Warning: Failed to load body part '" << headName << "'";
496     }
497 
498     if (!hairName.empty())
499     {
500         const ESM::BodyPart* bp = store.get<ESM::BodyPart>().search(hairName);
501         if (bp)
502             mHairModel = "meshes\\" + bp->mModel;
503         else
504             Log(Debug::Warning) << "Warning: Failed to load body part '" << hairName << "'";
505     }
506 
507     const std::string& vampireHead = getVampireHead(mNpc->mRace, isFemale);
508     if (!isWerewolf && isVampire && !vampireHead.empty())
509         mHeadModel = vampireHead;
510 
511     bool is1stPerson = mViewMode == VM_FirstPerson;
512     bool isBeast = (race->mData.mFlags & ESM::Race::Beast) != 0;
513 
514     std::string defaultSkeleton = SceneUtil::getActorSkeleton(is1stPerson, isFemale, isBeast, isWerewolf);
515     defaultSkeleton = Misc::ResourceHelpers::correctActorModelPath(defaultSkeleton, mResourceSystem->getVFS());
516 
517     std::string smodel = defaultSkeleton;
518     if (!is1stPerson && !isWerewolf && !mNpc->mModel.empty())
519         smodel = Misc::ResourceHelpers::correctActorModelPath("meshes\\" + mNpc->mModel, mResourceSystem->getVFS());
520 
521     setObjectRoot(smodel, true, true, false);
522 
523     updateParts();
524 
525     if(!is1stPerson)
526     {
527         const std::string base = Settings::Manager::getString("xbaseanim", "Models");
528         if (smodel != base && !isWerewolf)
529             addAnimSource(base, smodel);
530 
531         if (smodel != defaultSkeleton && base != defaultSkeleton)
532             addAnimSource(defaultSkeleton, smodel);
533 
534         addAnimSource(smodel, smodel);
535 
536         if(!isWerewolf && Misc::StringUtils::lowerCase(mNpc->mRace).find("argonian") != std::string::npos)
537             addAnimSource("meshes\\xargonian_swimkna.nif", smodel);
538     }
539     else
540     {
541         const std::string base = Settings::Manager::getString("xbaseanim1st", "Models");
542         if (smodel != base && !isWerewolf)
543             addAnimSource(base, smodel);
544 
545         addAnimSource(smodel, smodel);
546 
547         mObjectRoot->setNodeMask(Mask_FirstPerson);
548         mObjectRoot->addCullCallback(new OverrideFieldOfViewCallback(mFirstPersonFieldOfView));
549     }
550 
551     mWeaponAnimationTime->updateStartTime();
552 }
553 
getShieldMesh(MWWorld::ConstPtr shield) const554 std::string NpcAnimation::getShieldMesh(MWWorld::ConstPtr shield) const
555 {
556     std::string mesh = shield.getClass().getModel(shield);
557     const ESM::Armor *armor = shield.get<ESM::Armor>()->mBase;
558     const std::vector<ESM::PartReference>& bodyparts = armor->mParts.mParts;
559     // Try to recover the body part model, use ground model as a fallback otherwise.
560     if (!bodyparts.empty())
561         mesh = getShieldBodypartMesh(bodyparts, !mNpc->isMale());
562 
563     if (mesh.empty())
564         return std::string();
565 
566     std::string holsteredName = mesh;
567     holsteredName = holsteredName.replace(holsteredName.size()-4, 4, "_sh.nif");
568     if(mResourceSystem->getVFS()->exists(holsteredName))
569     {
570         osg::ref_ptr<osg::Node> shieldTemplate = mResourceSystem->getSceneManager()->getInstance(holsteredName);
571         SceneUtil::FindByNameVisitor findVisitor ("Bip01 Sheath");
572         shieldTemplate->accept(findVisitor);
573         osg::ref_ptr<osg::Node> sheathNode = findVisitor.mFoundNode;
574         if(!sheathNode)
575             return std::string();
576     }
577 
578     return mesh;
579 }
580 
updateParts()581 void NpcAnimation::updateParts()
582 {
583     if (!mObjectRoot.get())
584         return;
585 
586     NpcType curType = getNpcType();
587     if (curType != mNpcType)
588     {
589         mNpcType = curType;
590         rebuild();
591         return;
592     }
593 
594     static const struct {
595         int mSlot;
596         int mBasePriority;
597     } slotlist[] = {
598         // FIXME: Priority is based on the number of reserved slots. There should be a better way.
599         { MWWorld::InventoryStore::Slot_Robe,         11 },
600         { MWWorld::InventoryStore::Slot_Skirt,         3 },
601         { MWWorld::InventoryStore::Slot_Helmet,        0 },
602         { MWWorld::InventoryStore::Slot_Cuirass,       0 },
603         { MWWorld::InventoryStore::Slot_Greaves,       0 },
604         { MWWorld::InventoryStore::Slot_LeftPauldron,  0 },
605         { MWWorld::InventoryStore::Slot_RightPauldron, 0 },
606         { MWWorld::InventoryStore::Slot_Boots,         0 },
607         { MWWorld::InventoryStore::Slot_LeftGauntlet,  0 },
608         { MWWorld::InventoryStore::Slot_RightGauntlet, 0 },
609         { MWWorld::InventoryStore::Slot_Shirt,         0 },
610         { MWWorld::InventoryStore::Slot_Pants,         0 },
611         { MWWorld::InventoryStore::Slot_CarriedLeft,   0 },
612         { MWWorld::InventoryStore::Slot_CarriedRight,  0 }
613     };
614     static const size_t slotlistsize = sizeof(slotlist)/sizeof(slotlist[0]);
615 
616     bool wasArrowAttached = isArrowAttached();
617     mAmmunition.reset();
618 
619     const MWWorld::InventoryStore& inv = mPtr.getClass().getInventoryStore(mPtr);
620     for(size_t i = 0;i < slotlistsize && mViewMode != VM_HeadOnly;i++)
621     {
622         MWWorld::ConstContainerStoreIterator store = inv.getSlot(slotlist[i].mSlot);
623 
624         removePartGroup(slotlist[i].mSlot);
625 
626         if(store == inv.end())
627             continue;
628 
629         if(slotlist[i].mSlot == MWWorld::InventoryStore::Slot_Helmet)
630             removeIndividualPart(ESM::PRT_Hair);
631 
632         int prio = 1;
633         bool enchantedGlow = !store->getClass().getEnchantment(*store).empty();
634         osg::Vec4f glowColor = store->getClass().getEnchantmentColor(*store);
635         if(store->getTypeName() == typeid(ESM::Clothing).name())
636         {
637             prio = ((slotlist[i].mBasePriority+1)<<1) + 0;
638             const ESM::Clothing *clothes = store->get<ESM::Clothing>()->mBase;
639             addPartGroup(slotlist[i].mSlot, prio, clothes->mParts.mParts, enchantedGlow, &glowColor);
640         }
641         else if(store->getTypeName() == typeid(ESM::Armor).name())
642         {
643             prio = ((slotlist[i].mBasePriority+1)<<1) + 1;
644             const ESM::Armor *armor = store->get<ESM::Armor>()->mBase;
645             addPartGroup(slotlist[i].mSlot, prio, armor->mParts.mParts, enchantedGlow, &glowColor);
646         }
647 
648         if(slotlist[i].mSlot == MWWorld::InventoryStore::Slot_Robe)
649         {
650             ESM::PartReferenceType parts[] = {
651                 ESM::PRT_Groin, ESM::PRT_Skirt, ESM::PRT_RLeg, ESM::PRT_LLeg,
652                 ESM::PRT_RUpperarm, ESM::PRT_LUpperarm, ESM::PRT_RKnee, ESM::PRT_LKnee,
653                 ESM::PRT_RForearm, ESM::PRT_LForearm, ESM::PRT_Cuirass
654             };
655             size_t parts_size = sizeof(parts)/sizeof(parts[0]);
656             for(size_t p = 0;p < parts_size;++p)
657                 reserveIndividualPart(parts[p], slotlist[i].mSlot, prio);
658         }
659         else if(slotlist[i].mSlot == MWWorld::InventoryStore::Slot_Skirt)
660         {
661             reserveIndividualPart(ESM::PRT_Groin, slotlist[i].mSlot, prio);
662             reserveIndividualPart(ESM::PRT_RLeg, slotlist[i].mSlot, prio);
663             reserveIndividualPart(ESM::PRT_LLeg, slotlist[i].mSlot, prio);
664         }
665     }
666 
667     if(mViewMode != VM_FirstPerson)
668     {
669         if(mPartPriorities[ESM::PRT_Head] < 1 && !mHeadModel.empty())
670             addOrReplaceIndividualPart(ESM::PRT_Head, -1,1, mHeadModel);
671         if(mPartPriorities[ESM::PRT_Hair] < 1 && mPartPriorities[ESM::PRT_Head] <= 1 && !mHairModel.empty())
672             addOrReplaceIndividualPart(ESM::PRT_Hair, -1,1, mHairModel);
673     }
674     if(mViewMode == VM_HeadOnly)
675         return;
676 
677     if(mPartPriorities[ESM::PRT_Shield] < 1)
678     {
679         MWWorld::ConstContainerStoreIterator store = inv.getSlot(MWWorld::InventoryStore::Slot_CarriedLeft);
680         MWWorld::ConstPtr part;
681         if(store != inv.end() && (part=*store).getTypeName() == typeid(ESM::Light).name())
682         {
683             const ESM::Light *light = part.get<ESM::Light>()->mBase;
684             addOrReplaceIndividualPart(ESM::PRT_Shield, MWWorld::InventoryStore::Slot_CarriedLeft,
685                                        1, "meshes\\"+light->mModel);
686             if (mObjectParts[ESM::PRT_Shield])
687                 addExtraLight(mObjectParts[ESM::PRT_Shield]->getNode()->asGroup(), light);
688         }
689     }
690 
691     showWeapons(mShowWeapons);
692     showCarriedLeft(mShowCarriedLeft);
693 
694     bool isWerewolf = (getNpcType() == Type_Werewolf);
695     std::string race = (isWerewolf ? "werewolf" : Misc::StringUtils::lowerCase(mNpc->mRace));
696 
697     const std::vector<const ESM::BodyPart*> &parts = getBodyParts(race, !mNpc->isMale(), mViewMode == VM_FirstPerson, isWerewolf);
698     for(int part = ESM::PRT_Neck; part < ESM::PRT_Count; ++part)
699     {
700         if(mPartPriorities[part] < 1)
701         {
702             const ESM::BodyPart* bodypart = parts[part];
703             if(bodypart)
704                 addOrReplaceIndividualPart((ESM::PartReferenceType)part, -1, 1,
705                                            "meshes\\"+bodypart->mModel);
706         }
707     }
708 
709     if (wasArrowAttached)
710         attachArrow();
711 }
712 
713 
714 
insertBoundedPart(const std::string & model,const std::string & bonename,const std::string & bonefilter,bool enchantedGlow,osg::Vec4f * glowColor)715 PartHolderPtr NpcAnimation::insertBoundedPart(const std::string& model, const std::string& bonename, const std::string& bonefilter, bool enchantedGlow, osg::Vec4f* glowColor)
716 {
717     osg::ref_ptr<osg::Node> instance = mResourceSystem->getSceneManager()->getInstance(model);
718 
719     const NodeMap& nodeMap = getNodeMap();
720     NodeMap::const_iterator found = nodeMap.find(Misc::StringUtils::lowerCase(bonename));
721     if (found == nodeMap.end())
722         throw std::runtime_error("Can't find attachment node " + bonename);
723 
724     osg::ref_ptr<osg::Node> attached = SceneUtil::attach(instance, mObjectRoot, bonefilter, found->second);
725     if (enchantedGlow)
726         mGlowUpdater = SceneUtil::addEnchantedGlow(attached, mResourceSystem, *glowColor);
727 
728     return PartHolderPtr(new PartHolder(attached));
729 }
730 
runAnimation(float timepassed)731 osg::Vec3f NpcAnimation::runAnimation(float timepassed)
732 {
733     osg::Vec3f ret = Animation::runAnimation(timepassed);
734 
735     mHeadAnimationTime->update(timepassed);
736 
737     if (mFirstPersonNeckController)
738     {
739         if (mAccurateAiming)
740             mAimingFactor = 1.f;
741         else
742             mAimingFactor = std::max(0.f, mAimingFactor - timepassed * 0.5f);
743 
744         float rotateFactor = 0.75f + 0.25f * mAimingFactor;
745 
746         mFirstPersonNeckController->setRotate(osg::Quat(mPtr.getRefData().getPosition().rot[0] * rotateFactor, osg::Vec3f(-1,0,0)));
747         mFirstPersonNeckController->setOffset(mFirstPersonOffset);
748     }
749 
750     WeaponAnimation::configureControllers(mPtr.getRefData().getPosition().rot[0] + getBodyPitchRadians());
751 
752     return ret;
753 }
754 
removeIndividualPart(ESM::PartReferenceType type)755 void NpcAnimation::removeIndividualPart(ESM::PartReferenceType type)
756 {
757     mPartPriorities[type] = 0;
758     mPartslots[type] = -1;
759 
760     mObjectParts[type].reset();
761     if (mSounds[type] != nullptr && !mSoundsDisabled)
762     {
763         MWBase::Environment::get().getSoundManager()->stopSound(mSounds[type]);
764         mSounds[type] = nullptr;
765     }
766 }
767 
reserveIndividualPart(ESM::PartReferenceType type,int group,int priority)768 void NpcAnimation::reserveIndividualPart(ESM::PartReferenceType type, int group, int priority)
769 {
770     if(priority > mPartPriorities[type])
771     {
772         removeIndividualPart(type);
773         mPartPriorities[type] = priority;
774         mPartslots[type] = group;
775     }
776 }
777 
removePartGroup(int group)778 void NpcAnimation::removePartGroup(int group)
779 {
780     for(int i = 0; i < ESM::PRT_Count; i++)
781     {
782         if(mPartslots[i] == group)
783             removeIndividualPart((ESM::PartReferenceType)i);
784     }
785 }
786 
isFirstPersonPart(const ESM::BodyPart * bodypart)787 bool NpcAnimation::isFirstPersonPart(const ESM::BodyPart* bodypart)
788 {
789     return bodypart->mId.size() >= 3 && bodypart->mId.substr(bodypart->mId.size()-3, 3) == "1st";
790 }
791 
isFemalePart(const ESM::BodyPart * bodypart)792 bool NpcAnimation::isFemalePart(const ESM::BodyPart* bodypart)
793 {
794     return bodypart->mData.mFlags & ESM::BodyPart::BPF_Female;
795 }
796 
addOrReplaceIndividualPart(ESM::PartReferenceType type,int group,int priority,const std::string & mesh,bool enchantedGlow,osg::Vec4f * glowColor)797 bool NpcAnimation::addOrReplaceIndividualPart(ESM::PartReferenceType type, int group, int priority, const std::string &mesh, bool enchantedGlow, osg::Vec4f* glowColor)
798 {
799     if(priority <= mPartPriorities[type])
800         return false;
801 
802     removeIndividualPart(type);
803     mPartslots[type] = group;
804     mPartPriorities[type] = priority;
805     try
806     {
807         std::string bonename = sPartList.at(type);
808         if (type == ESM::PRT_Weapon)
809         {
810             const MWWorld::InventoryStore& inv = mPtr.getClass().getInventoryStore(mPtr);
811             MWWorld::ConstContainerStoreIterator weapon = inv.getSlot(MWWorld::InventoryStore::Slot_CarriedRight);
812             if(weapon != inv.end() && weapon->getTypeName() == typeid(ESM::Weapon).name())
813             {
814                 int weaponType = weapon->get<ESM::Weapon>()->mBase->mData.mType;
815                 const std::string weaponBonename = MWMechanics::getWeaponType(weaponType)->mAttachBone;
816 
817                 if (weaponBonename != bonename)
818                 {
819                     const NodeMap& nodeMap = getNodeMap();
820                     NodeMap::const_iterator found = nodeMap.find(Misc::StringUtils::lowerCase(weaponBonename));
821                     if (found != nodeMap.end())
822                         bonename = weaponBonename;
823                 }
824             }
825         }
826 
827         // PRT_Hair seems to be the only type that breaks consistency and uses a filter that's different from the attachment bone
828         const std::string bonefilter = (type == ESM::PRT_Hair) ? "hair" : bonename;
829         mObjectParts[type] = insertBoundedPart(mesh, bonename, bonefilter, enchantedGlow, glowColor);
830     }
831     catch (std::exception& e)
832     {
833         Log(Debug::Error) << "Error adding NPC part: " << e.what();
834         return false;
835     }
836 
837     if (!mSoundsDisabled)
838     {
839         const MWWorld::InventoryStore& inv = mPtr.getClass().getInventoryStore(mPtr);
840         MWWorld::ConstContainerStoreIterator csi = inv.getSlot(group < 0 ? MWWorld::InventoryStore::Slot_Helmet : group);
841         if (csi != inv.end())
842         {
843             const auto soundId = csi->getClass().getSound(*csi);
844             if (!soundId.empty())
845             {
846                 mSounds[type] = MWBase::Environment::get().getSoundManager()->playSound3D(mPtr, soundId,
847                     1.0f, 1.0f, MWSound::Type::Sfx, MWSound::PlayMode::Loop
848                 );
849             }
850         }
851     }
852 
853     osg::Node* node = mObjectParts[type]->getNode();
854     if (node->getNumChildrenRequiringUpdateTraversal() > 0)
855     {
856         std::shared_ptr<SceneUtil::ControllerSource> src;
857         if (type == ESM::PRT_Head)
858         {
859             src = mHeadAnimationTime;
860 
861             if (node->getUserDataContainer())
862             {
863                 for (unsigned int i=0; i<node->getUserDataContainer()->getNumUserObjects(); ++i)
864                 {
865                     osg::Object* obj = node->getUserDataContainer()->getUserObject(i);
866                     if (SceneUtil::TextKeyMapHolder* keys = dynamic_cast<SceneUtil::TextKeyMapHolder*>(obj))
867                     {
868                         for (const auto &key : keys->mTextKeys)
869                         {
870                             if (Misc::StringUtils::ciEqual(key.second, "talk: start"))
871                                 mHeadAnimationTime->setTalkStart(key.first);
872                             if (Misc::StringUtils::ciEqual(key.second, "talk: stop"))
873                                 mHeadAnimationTime->setTalkStop(key.first);
874                             if (Misc::StringUtils::ciEqual(key.second, "blink: start"))
875                                 mHeadAnimationTime->setBlinkStart(key.first);
876                             if (Misc::StringUtils::ciEqual(key.second, "blink: stop"))
877                                 mHeadAnimationTime->setBlinkStop(key.first);
878                         }
879 
880                         break;
881                     }
882                 }
883             }
884         }
885         else if (type == ESM::PRT_Weapon)
886             src = mWeaponAnimationTime;
887         else
888             src.reset(new NullAnimationTime);
889 
890         SceneUtil::AssignControllerSourcesVisitor assignVisitor(src);
891         node->accept(assignVisitor);
892     }
893 
894     return true;
895 }
896 
addPartGroup(int group,int priority,const std::vector<ESM::PartReference> & parts,bool enchantedGlow,osg::Vec4f * glowColor)897 void NpcAnimation::addPartGroup(int group, int priority, const std::vector<ESM::PartReference> &parts, bool enchantedGlow, osg::Vec4f* glowColor)
898 {
899     const MWWorld::ESMStore &store = MWBase::Environment::get().getWorld()->getStore();
900     const MWWorld::Store<ESM::BodyPart> &partStore = store.get<ESM::BodyPart>();
901 
902     const char *ext = (mViewMode == VM_FirstPerson) ? ".1st" : "";
903     for(const ESM::PartReference& part : parts)
904     {
905         const ESM::BodyPart *bodypart = nullptr;
906         if(!mNpc->isMale() && !part.mFemale.empty())
907         {
908             bodypart = partStore.search(part.mFemale+ext);
909             if(!bodypart && mViewMode == VM_FirstPerson)
910             {
911                 bodypart = partStore.search(part.mFemale);
912                 if(bodypart && !(bodypart->mData.mPart == ESM::BodyPart::MP_Hand ||
913                                  bodypart->mData.mPart == ESM::BodyPart::MP_Wrist ||
914                                  bodypart->mData.mPart == ESM::BodyPart::MP_Forearm ||
915                                  bodypart->mData.mPart == ESM::BodyPart::MP_Upperarm))
916                     bodypart = nullptr;
917             }
918             else if (!bodypart)
919                 Log(Debug::Warning) << "Warning: Failed to find body part '" << part.mFemale << "'";
920         }
921         if(!bodypart && !part.mMale.empty())
922         {
923             bodypart = partStore.search(part.mMale+ext);
924             if(!bodypart && mViewMode == VM_FirstPerson)
925             {
926                 bodypart = partStore.search(part.mMale);
927                 if(bodypart && !(bodypart->mData.mPart == ESM::BodyPart::MP_Hand ||
928                                  bodypart->mData.mPart == ESM::BodyPart::MP_Wrist ||
929                                  bodypart->mData.mPart == ESM::BodyPart::MP_Forearm ||
930                                  bodypart->mData.mPart == ESM::BodyPart::MP_Upperarm))
931                     bodypart = nullptr;
932             }
933             else if (!bodypart)
934                 Log(Debug::Warning) << "Warning: Failed to find body part '" << part.mMale << "'";
935         }
936 
937         if(bodypart)
938             addOrReplaceIndividualPart((ESM::PartReferenceType)part.mPart, group, priority, "meshes\\"+bodypart->mModel, enchantedGlow, glowColor);
939         else
940             reserveIndividualPart((ESM::PartReferenceType)part.mPart, group, priority);
941     }
942 }
943 
addControllers()944 void NpcAnimation::addControllers()
945 {
946     Animation::addControllers();
947 
948     mFirstPersonNeckController = nullptr;
949     WeaponAnimation::deleteControllers();
950 
951     if (mViewMode == VM_FirstPerson)
952     {
953         NodeMap::iterator found = mNodeMap.find("bip01 neck");
954         if (found != mNodeMap.end())
955         {
956             osg::MatrixTransform* node = found->second.get();
957             mFirstPersonNeckController = new NeckController(mObjectRoot.get());
958             node->addUpdateCallback(mFirstPersonNeckController);
959             mActiveControllers.emplace_back(node, mFirstPersonNeckController);
960         }
961     }
962     else if (mViewMode == VM_Normal)
963     {
964         WeaponAnimation::addControllers(mNodeMap, mActiveControllers, mObjectRoot.get());
965     }
966 }
967 
showWeapons(bool showWeapon)968 void NpcAnimation::showWeapons(bool showWeapon)
969 {
970     mShowWeapons = showWeapon;
971     mAmmunition.reset();
972     if(showWeapon)
973     {
974         const MWWorld::InventoryStore& inv = mPtr.getClass().getInventoryStore(mPtr);
975         MWWorld::ConstContainerStoreIterator weapon = inv.getSlot(MWWorld::InventoryStore::Slot_CarriedRight);
976         if(weapon != inv.end())
977         {
978             osg::Vec4f glowColor = weapon->getClass().getEnchantmentColor(*weapon);
979             std::string mesh = weapon->getClass().getModel(*weapon);
980             addOrReplaceIndividualPart(ESM::PRT_Weapon, MWWorld::InventoryStore::Slot_CarriedRight, 1,
981                                        mesh, !weapon->getClass().getEnchantment(*weapon).empty(), &glowColor);
982 
983             // Crossbows start out with a bolt attached
984             if (weapon->getTypeName() == typeid(ESM::Weapon).name() &&
985                     weapon->get<ESM::Weapon>()->mBase->mData.mType == ESM::Weapon::MarksmanCrossbow)
986             {
987                 int ammotype = MWMechanics::getWeaponType(ESM::Weapon::MarksmanCrossbow)->mAmmoType;
988                 MWWorld::ConstContainerStoreIterator ammo = inv.getSlot(MWWorld::InventoryStore::Slot_Ammunition);
989                 if (ammo != inv.end() && ammo->get<ESM::Weapon>()->mBase->mData.mType == ammotype)
990                     attachArrow();
991             }
992         }
993     }
994     else
995     {
996         removeIndividualPart(ESM::PRT_Weapon);
997         // If we remove/hide weapon from player, we should reset attack animation as well
998         if (mPtr == MWMechanics::getPlayer())
999             MWBase::Environment::get().getWorld()->getPlayer().setAttackingOrSpell(false);
1000     }
1001 
1002     updateHolsteredWeapon(!mShowWeapons);
1003     updateQuiver();
1004 }
1005 
showCarriedLeft(bool show)1006 void NpcAnimation::showCarriedLeft(bool show)
1007 {
1008     mShowCarriedLeft = show;
1009     const MWWorld::InventoryStore& inv = mPtr.getClass().getInventoryStore(mPtr);
1010     MWWorld::ConstContainerStoreIterator iter = inv.getSlot(MWWorld::InventoryStore::Slot_CarriedLeft);
1011     if(show && iter != inv.end())
1012     {
1013         osg::Vec4f glowColor = iter->getClass().getEnchantmentColor(*iter);
1014         std::string mesh = iter->getClass().getModel(*iter);
1015         // For shields we must try to use the body part model
1016         if (iter->getTypeName() == typeid(ESM::Armor).name())
1017         {
1018             const ESM::Armor *armor = iter->get<ESM::Armor>()->mBase;
1019             const std::vector<ESM::PartReference>& bodyparts = armor->mParts.mParts;
1020             if (!bodyparts.empty())
1021                 mesh = getShieldBodypartMesh(bodyparts, !mNpc->isMale());
1022         }
1023         if (mesh.empty() || addOrReplaceIndividualPart(ESM::PRT_Shield, MWWorld::InventoryStore::Slot_CarriedLeft, 1,
1024                                         mesh, !iter->getClass().getEnchantment(*iter).empty(), &glowColor))
1025         {
1026             if (mesh.empty())
1027                 reserveIndividualPart(ESM::PRT_Shield, MWWorld::InventoryStore::Slot_CarriedLeft, 1);
1028             if (iter->getTypeName() == typeid(ESM::Light).name() && mObjectParts[ESM::PRT_Shield])
1029                 addExtraLight(mObjectParts[ESM::PRT_Shield]->getNode()->asGroup(), iter->get<ESM::Light>()->mBase);
1030         }
1031     }
1032     else
1033         removeIndividualPart(ESM::PRT_Shield);
1034 
1035     updateHolsteredShield(mShowCarriedLeft);
1036 }
1037 
attachArrow()1038 void NpcAnimation::attachArrow()
1039 {
1040     WeaponAnimation::attachArrow(mPtr);
1041 
1042     const MWWorld::InventoryStore& inv = mPtr.getClass().getInventoryStore(mPtr);
1043     MWWorld::ConstContainerStoreIterator ammo = inv.getSlot(MWWorld::InventoryStore::Slot_Ammunition);
1044     if (ammo != inv.end() && !ammo->getClass().getEnchantment(*ammo).empty())
1045     {
1046         osg::Group* bone = getArrowBone();
1047         if (bone != nullptr && bone->getNumChildren())
1048             SceneUtil::addEnchantedGlow(bone->getChild(0), mResourceSystem, ammo->getClass().getEnchantmentColor(*ammo));
1049     }
1050 
1051     updateQuiver();
1052 }
1053 
detachArrow()1054 void NpcAnimation::detachArrow()
1055 {
1056     WeaponAnimation::detachArrow(mPtr);
1057     updateQuiver();
1058 }
1059 
releaseArrow(float attackStrength)1060 void NpcAnimation::releaseArrow(float attackStrength)
1061 {
1062     WeaponAnimation::releaseArrow(mPtr, attackStrength);
1063     updateQuiver();
1064 }
1065 
getArrowBone()1066 osg::Group* NpcAnimation::getArrowBone()
1067 {
1068     PartHolderPtr part = mObjectParts[ESM::PRT_Weapon];
1069     if (!part)
1070         return nullptr;
1071 
1072     const MWWorld::InventoryStore& inv = mPtr.getClass().getInventoryStore(mPtr);
1073     MWWorld::ConstContainerStoreIterator weapon = inv.getSlot(MWWorld::InventoryStore::Slot_CarriedRight);
1074     if(weapon == inv.end() || weapon->getTypeName() != typeid(ESM::Weapon).name())
1075         return nullptr;
1076 
1077     int type = weapon->get<ESM::Weapon>()->mBase->mData.mType;
1078     int ammoType = MWMechanics::getWeaponType(type)->mAmmoType;
1079 
1080     // Try to find and attachment bone in actor's skeleton, otherwise fall back to the ArrowBone in weapon's mesh
1081     osg::Group* bone = getBoneByName(MWMechanics::getWeaponType(ammoType)->mAttachBone);
1082     if (bone == nullptr)
1083     {
1084         SceneUtil::FindByNameVisitor findVisitor ("ArrowBone");
1085         part->getNode()->accept(findVisitor);
1086         bone = findVisitor.mFoundNode;
1087     }
1088     return bone;
1089 }
1090 
getWeaponNode()1091 osg::Node* NpcAnimation::getWeaponNode()
1092 {
1093     PartHolderPtr part = mObjectParts[ESM::PRT_Weapon];
1094     if (!part)
1095         return nullptr;
1096     return part->getNode();
1097 }
1098 
getResourceSystem()1099 Resource::ResourceSystem* NpcAnimation::getResourceSystem()
1100 {
1101     return mResourceSystem;
1102 }
1103 
permanentEffectAdded(const ESM::MagicEffect * magicEffect,bool isNew)1104 void NpcAnimation::permanentEffectAdded(const ESM::MagicEffect *magicEffect, bool isNew)
1105 {
1106     // During first auto equip, we don't play any sounds.
1107     // Basically we don't want sounds when the actor is first loaded,
1108     // the items should appear as if they'd always been equipped.
1109     if (isNew)
1110     {
1111         static const std::string schools[] = {
1112             "alteration", "conjuration", "destruction", "illusion", "mysticism", "restoration"
1113         };
1114 
1115         MWBase::SoundManager *sndMgr = MWBase::Environment::get().getSoundManager();
1116         if(!magicEffect->mHitSound.empty())
1117             sndMgr->playSound3D(mPtr, magicEffect->mHitSound, 1.0f, 1.0f);
1118         else
1119             sndMgr->playSound3D(mPtr, schools[magicEffect->mData.mSchool]+" hit", 1.0f, 1.0f);
1120     }
1121 
1122     if (!magicEffect->mHit.empty())
1123     {
1124         const ESM::Static* castStatic = MWBase::Environment::get().getWorld()->getStore().get<ESM::Static>().find (magicEffect->mHit);
1125         bool loop = (magicEffect->mData.mFlags & ESM::MagicEffect::ContinuousVfx) != 0;
1126         // Don't play particle VFX unless the effect is new or it should be looping.
1127         if (isNew || loop)
1128             addEffect("meshes\\" + castStatic->mModel, magicEffect->mIndex, loop, "", magicEffect->mParticle);
1129     }
1130 }
1131 
enableHeadAnimation(bool enable)1132 void NpcAnimation::enableHeadAnimation(bool enable)
1133 {
1134     mHeadAnimationTime->setEnabled(enable);
1135 }
1136 
setWeaponGroup(const std::string & group,bool relativeDuration)1137 void NpcAnimation::setWeaponGroup(const std::string &group, bool relativeDuration)
1138 {
1139     mWeaponAnimationTime->setGroup(group, relativeDuration);
1140 }
1141 
equipmentChanged()1142 void NpcAnimation::equipmentChanged()
1143 {
1144     static const bool shieldSheathing = Settings::Manager::getBool("shield sheathing", "Game");
1145     if (shieldSheathing)
1146     {
1147         int weaptype = ESM::Weapon::None;
1148         MWMechanics::getActiveWeapon(mPtr, &weaptype);
1149         showCarriedLeft(updateCarriedLeftVisible(weaptype));
1150     }
1151 
1152     updateParts();
1153 }
1154 
setVampire(bool vampire)1155 void NpcAnimation::setVampire(bool vampire)
1156 {
1157     if (mNpcType == Type_Werewolf) // we can't have werewolf vampires, can we
1158         return;
1159     if ((mNpcType == Type_Vampire) != vampire)
1160     {
1161         if (mPtr == MWMechanics::getPlayer())
1162             MWBase::Environment::get().getWorld()->reattachPlayerCamera();
1163         else
1164             rebuild();
1165     }
1166 }
1167 
setFirstPersonOffset(const osg::Vec3f & offset)1168 void NpcAnimation::setFirstPersonOffset(const osg::Vec3f &offset)
1169 {
1170     mFirstPersonOffset = offset;
1171 }
1172 
updatePtr(const MWWorld::Ptr & updated)1173 void NpcAnimation::updatePtr(const MWWorld::Ptr &updated)
1174 {
1175     Animation::updatePtr(updated);
1176     mHeadAnimationTime->updatePtr(updated);
1177 }
1178 
1179 // Remember body parts so we only have to search through the store once for each race/gender/viewmode combination
1180 typedef std::map< std::pair<std::string,int>,std::vector<const ESM::BodyPart*> > RaceMapping;
1181 static RaceMapping sRaceMapping;
1182 
getBodyParts(const std::string & race,bool female,bool firstPerson,bool werewolf)1183 const std::vector<const ESM::BodyPart *>& NpcAnimation::getBodyParts(const std::string &race, bool female, bool firstPerson, bool werewolf)
1184 {
1185     static const int Flag_FirstPerson = 1<<1;
1186     static const int Flag_Female      = 1<<0;
1187 
1188     int flags = (werewolf ? -1 : 0);
1189     if(female)
1190         flags |= Flag_Female;
1191     if(firstPerson)
1192         flags |= Flag_FirstPerson;
1193 
1194     RaceMapping::iterator found = sRaceMapping.find(std::make_pair(race, flags));
1195     if (found != sRaceMapping.end())
1196         return found->second;
1197     else
1198     {
1199         std::vector<const ESM::BodyPart*>& parts = sRaceMapping[std::make_pair(race, flags)];
1200 
1201         typedef std::multimap<ESM::BodyPart::MeshPart,ESM::PartReferenceType> BodyPartMapType;
1202         static const BodyPartMapType sBodyPartMap =
1203         {
1204             {ESM::BodyPart::MP_Neck, ESM::PRT_Neck},
1205             {ESM::BodyPart::MP_Chest, ESM::PRT_Cuirass},
1206             {ESM::BodyPart::MP_Groin, ESM::PRT_Groin},
1207             {ESM::BodyPart::MP_Hand, ESM::PRT_RHand},
1208             {ESM::BodyPart::MP_Hand, ESM::PRT_LHand},
1209             {ESM::BodyPart::MP_Wrist, ESM::PRT_RWrist},
1210             {ESM::BodyPart::MP_Wrist, ESM::PRT_LWrist},
1211             {ESM::BodyPart::MP_Forearm, ESM::PRT_RForearm},
1212             {ESM::BodyPart::MP_Forearm, ESM::PRT_LForearm},
1213             {ESM::BodyPart::MP_Upperarm, ESM::PRT_RUpperarm},
1214             {ESM::BodyPart::MP_Upperarm, ESM::PRT_LUpperarm},
1215             {ESM::BodyPart::MP_Foot, ESM::PRT_RFoot},
1216             {ESM::BodyPart::MP_Foot, ESM::PRT_LFoot},
1217             {ESM::BodyPart::MP_Ankle, ESM::PRT_RAnkle},
1218             {ESM::BodyPart::MP_Ankle, ESM::PRT_LAnkle},
1219             {ESM::BodyPart::MP_Knee, ESM::PRT_RKnee},
1220             {ESM::BodyPart::MP_Knee, ESM::PRT_LKnee},
1221             {ESM::BodyPart::MP_Upperleg, ESM::PRT_RLeg},
1222             {ESM::BodyPart::MP_Upperleg, ESM::PRT_LLeg},
1223             {ESM::BodyPart::MP_Tail, ESM::PRT_Tail}
1224         };
1225 
1226         parts.resize(ESM::PRT_Count, nullptr);
1227 
1228         if (werewolf)
1229             return parts;
1230 
1231         const MWWorld::ESMStore &store = MWBase::Environment::get().getWorld()->getStore();
1232 
1233         for(const ESM::BodyPart& bodypart : store.get<ESM::BodyPart>())
1234         {
1235             if (bodypart.mData.mFlags & ESM::BodyPart::BPF_NotPlayable)
1236                 continue;
1237             if (bodypart.mData.mType != ESM::BodyPart::MT_Skin)
1238                 continue;
1239 
1240             if (!Misc::StringUtils::ciEqual(bodypart.mRace, race))
1241                 continue;
1242 
1243             bool partFirstPerson = isFirstPersonPart(&bodypart);
1244 
1245             bool isHand = bodypart.mData.mPart == ESM::BodyPart::MP_Hand ||
1246                                     bodypart.mData.mPart == ESM::BodyPart::MP_Wrist ||
1247                                     bodypart.mData.mPart == ESM::BodyPart::MP_Forearm ||
1248                                     bodypart.mData.mPart == ESM::BodyPart::MP_Upperarm;
1249 
1250             bool isSameGender = isFemalePart(&bodypart) == female;
1251 
1252             /* A fallback for the arms if 1st person is missing:
1253              1. Try to use 3d person skin for same gender
1254              2. Try to use 1st person skin for male, if female == true
1255              3. Try to use 3d person skin for male, if female == true
1256 
1257              A fallback in another cases: allow to use male bodyparts, if female == true
1258             */
1259             if (firstPerson && isHand && !partFirstPerson)
1260             {
1261                 // Allow 3rd person skins as a fallback for the arms if 1st person is missing
1262                 BodyPartMapType::const_iterator bIt = sBodyPartMap.lower_bound(BodyPartMapType::key_type(bodypart.mData.mPart));
1263                 while(bIt != sBodyPartMap.end() && bIt->first == bodypart.mData.mPart)
1264                 {
1265                     // If we have no fallback bodypart now and bodypart is for same gender (1)
1266                     if(!parts[bIt->second] && isSameGender)
1267                        parts[bIt->second] = &bodypart;
1268 
1269                     // If we have fallback bodypart for other gender and found fallback for current gender (1)
1270                     else if(isSameGender && isFemalePart(parts[bIt->second]) != female)
1271                        parts[bIt->second] = &bodypart;
1272 
1273                     // If we have no fallback bodypart and searching for female bodyparts (3)
1274                     else if(!parts[bIt->second] && female)
1275                        parts[bIt->second] = &bodypart;
1276 
1277                     ++bIt;
1278                 }
1279 
1280                 continue;
1281             }
1282 
1283             // Don't allow to use podyparts for a different view
1284             if (partFirstPerson != firstPerson)
1285                 continue;
1286 
1287             if (female && !isFemalePart(&bodypart))
1288             {
1289                 // Allow male parts as fallback for females if female parts are missing
1290                 BodyPartMapType::const_iterator bIt = sBodyPartMap.lower_bound(BodyPartMapType::key_type(bodypart.mData.mPart));
1291                 while(bIt != sBodyPartMap.end() && bIt->first == bodypart.mData.mPart)
1292                 {
1293                     // If we have no fallback bodypart now
1294                     if(!parts[bIt->second])
1295                         parts[bIt->second] = &bodypart;
1296 
1297                     // If we have 3d person fallback bodypart for hand and 1st person fallback found (2)
1298                     else if(isHand && !isFirstPersonPart(parts[bIt->second]) && partFirstPerson)
1299                         parts[bIt->second] = &bodypart;
1300 
1301                     ++bIt;
1302                 }
1303 
1304                 continue;
1305             }
1306 
1307             // Don't allow to use podyparts for another gender
1308             if (female != isFemalePart(&bodypart))
1309                 continue;
1310 
1311             // Use properly found bodypart, replacing fallbacks
1312             BodyPartMapType::const_iterator bIt = sBodyPartMap.lower_bound(BodyPartMapType::key_type(bodypart.mData.mPart));
1313             while(bIt != sBodyPartMap.end() && bIt->first == bodypart.mData.mPart)
1314             {
1315                 parts[bIt->second] = &bodypart;
1316                 ++bIt;
1317             }
1318         }
1319         return parts;
1320     }
1321 }
1322 
setAccurateAiming(bool enabled)1323 void NpcAnimation::setAccurateAiming(bool enabled)
1324 {
1325     mAccurateAiming = enabled;
1326 }
1327 
isArrowAttached() const1328 bool NpcAnimation::isArrowAttached() const
1329 {
1330     return mAmmunition != nullptr;
1331 }
1332 
1333 }
1334