From d0a53b203f918c24837527e15cdc84c807de49ea Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Sat, 19 Mar 2016 00:26:10 +0300 Subject: Fix unit tests (add missing random initialisation). Also fix sprites animation if random number is zero. --- src/resources/sprite/animatedsprite.cpp | 22 +++++++++++++--------- src/resources/sprite/animatedsprite_unittest.cc | 2 ++ 2 files changed, 15 insertions(+), 9 deletions(-) (limited to 'src/resources') diff --git a/src/resources/sprite/animatedsprite.cpp b/src/resources/sprite/animatedsprite.cpp index f0af74f08..4daf6f7f6 100644 --- a/src/resources/sprite/animatedsprite.cpp +++ b/src/resources/sprite/animatedsprite.cpp @@ -240,8 +240,9 @@ bool AnimatedSprite::updateCurrentAnimation(const unsigned int time) restrict2 else if (mFrame->type == Frame::GOTO && !mFrame->nextAction.empty()) { - if (mFrame->rand == 100 || - mFrame->rand >= mrand() % 100) + const int rand = mFrame->rand; + if (rand == 100 || + (rand && rand >= mrand() % 100)) { for (size_t i = 0; i < mAnimation->getLength(); i ++) { @@ -272,8 +273,9 @@ bool AnimatedSprite::updateCurrentAnimation(const unsigned int time) restrict2 else if (mFrame->type == Frame::JUMP && !mFrame->nextAction.empty()) { - if (mFrame->rand == 100 || - mFrame->rand >= mrand() % 100) + const int rand = mFrame->rand; + if (rand == 100 || + (rand && rand >= mrand() % 100)) { play(mFrame->nextAction); return true; @@ -283,8 +285,9 @@ bool AnimatedSprite::updateCurrentAnimation(const unsigned int time) restrict2 else if (!mFrame->image && mFrame->type == Frame::ANIMATION) { - if (mFrame->rand == 100 || - mFrame->rand >= mrand() % 100) + const int rand = mFrame->rand; + if (rand == 100 || + (rand && rand >= mrand() % 100)) { mAnimation = nullptr; mFrame = nullptr; @@ -293,14 +296,15 @@ bool AnimatedSprite::updateCurrentAnimation(const unsigned int time) restrict2 } else { - if (mFrame->rand == 100 || mFrameIndex - >= CAST_U32(mAnimation->getLength())) + const int rand = mFrame->rand; + if (rand == 100 || + mFrameIndex >= CAST_U32(mAnimation->getLength())) { fail = false; } else { - if (mrand() % 100 <= mFrame->rand) + if (rand && mrand() % 100 <= rand) fail = false; } } diff --git a/src/resources/sprite/animatedsprite_unittest.cc b/src/resources/sprite/animatedsprite_unittest.cc index 715ce76b8..c924580a6 100644 --- a/src/resources/sprite/animatedsprite_unittest.cc +++ b/src/resources/sprite/animatedsprite_unittest.cc @@ -34,6 +34,7 @@ #include "resources/animation/animation.h" #include "utils/env.h" +#include "utils/mrand.h" #include "utils/physfstools.h" #include "debug.h" @@ -42,6 +43,7 @@ TEST_CASE("AnimatedSprite tests", "animatedsprite") { setEnv("SDL_VIDEODRIVER", "dummy"); + initRand(); client = new Client; PHYSFS_init("manaplus"); dirSeparator = "/"; -- cgit v1.2.3-60-g2f50