diff --git a/Source/Objects/Person.cpp b/Source/Objects/Person.cpp index 52be7080..f593db66 100644 --- a/Source/Objects/Person.cpp +++ b/Source/Objects/Person.cpp @@ -2238,7 +2238,7 @@ void Person::DoAnimations() animCurrent = animTarget; frameTarget++; - if (animTarget == removeknifeanim && Animation::animations[animTarget].frames[frameCurrent].label == 5) { + if (animTarget == removeknifeanim && currentFrame().label == 5) { for (unsigned i = 0; i < weapons.size(); i++) { if (weapons[i].owner == -1) { if (distsqflat(&coords, &weapons[i].position) < 4 && weaponactive == -1) { @@ -2254,7 +2254,7 @@ void Person::DoAnimations() } } - if (animTarget == crouchremoveknifeanim && Animation::animations[animTarget].frames[frameCurrent].label == 5) { + if (animTarget == crouchremoveknifeanim && currentFrame().label == 5) { for (unsigned i = 0; i < weapons.size(); i++) { bool willwork = true; if (weapons[i].owner != -1) { @@ -2340,7 +2340,7 @@ void Person::DoAnimations() } } - if (animCurrent == drawleftanim && Animation::animations[animTarget].frames[frameCurrent].label == 5) { + if (animCurrent == drawleftanim && currentFrame().label == 5) { if (weaponactive == -1) { weaponactive = 0; } else if (weaponactive == 0) { @@ -3912,7 +3912,7 @@ void Person::DoAnimations() } //Animation end - if (frameTarget > int(Animation::animations[animCurrent].frames.size()) - 1) { + if (frameTarget >= int(Animation::animations[animCurrent].frames.size())) { frameTarget = 0; if (wasStop()) { animTarget = getIdle(); @@ -4385,6 +4385,11 @@ void Person::DoAnimations() frameCurrent = frameTarget; target = 1; } + + if (frameCurrent >= int(Animation::animations[animCurrent].frames.size())) { + frameCurrent = Animation::animations[animCurrent].frames.size() - 1; + } + oldrot = rot; rot = targetrot * target; yaw += rot - oldrot; @@ -4393,9 +4398,7 @@ void Person::DoAnimations() oldrot = 0; targetrot = 0; } - if (frameCurrent >= int(Animation::animations[animCurrent].frames.size())) { - frameCurrent = Animation::animations[animCurrent].frames.size() - 1; - } + if (animCurrent != oldanimCurrent || animTarget != oldanimTarget || ((frameCurrent != oldframeCurrent || frameTarget != oldframeTarget) && !calcrot)) { //Old rotates for (unsigned i = 0; i < skeleton.joints.size(); i++) { diff --git a/Source/Objects/Person.hpp b/Source/Objects/Person.hpp index 2ead16e9..76e7863a 100644 --- a/Source/Objects/Person.hpp +++ b/Source/Objects/Person.hpp @@ -324,8 +324,32 @@ class Person : public enable_shared_from_this inline Joint& joint(int bodypart) { return skeleton.joints[skeleton.jointlabels[bodypart]]; } inline XYZ& jointPos(int bodypart) { return joint(bodypart).position; } inline XYZ& jointVel(int bodypart) { return joint(bodypart).velocity; } - inline AnimationFrame& currentFrame() { return Animation::animations.at(animCurrent).frames.at(frameCurrent); } - inline AnimationFrame& targetFrame() { return Animation::animations.at(animTarget).frames.at(frameTarget); } + AnimationFrame& currentFrame() { + /* FIXME - try/catch is a temporary fix to avoid crashes but game logic should be fixed instead */ + try { + return Animation::animations.at(animCurrent).frames.at(frameCurrent); + } catch (const std::exception& error) { + /* Most likely frameCurrent is too big, work around */ + if ((unsigned)frameCurrent >= +Animation::animations.at(animCurrent).frames.size()) { + frameCurrent = Animation::animations.at(animCurrent).frames.size() - 1; + } + return Animation::animations.at(animCurrent).frames.back(); + } + } + AnimationFrame& targetFrame() { + /* FIXME - try/catch is a temporary fix to avoid crashes but game logic should be fixed instead */ + try { + return Animation::animations.at(animTarget).frames.at(frameTarget); + } catch (const std::exception& error) { + /* Most likely frameTarget is too big, work around */ + if ((unsigned)frameTarget >= +Animation::animations.at(animTarget).frames.size()) { + frameTarget = Animation::animations.at(animTarget).frames.size() - 1; + } + return Animation::animations.at(animTarget).frames.back(); + } + } void setProportions(float head, float body, float arms, float legs); float getProportion(int part) const;