summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2016-03-18 20:34:04 +0300
committerAndrei Karas <akaras@inbox.ru>2016-03-18 20:41:06 +0300
commitb5fead8f53a555b62c9de2dc23b0f2ce9a136e8e (patch)
treeb4b3be6ca5dec68b68cabf83dd21626b70707c08 /src
parent5b293feb5367ccabd3edf49783511c895807876b (diff)
downloadplus-b5fead8f53a555b62c9de2dc23b0f2ce9a136e8e.tar.gz
plus-b5fead8f53a555b62c9de2dc23b0f2ce9a136e8e.tar.bz2
plus-b5fead8f53a555b62c9de2dc23b0f2ce9a136e8e.tar.xz
plus-b5fead8f53a555b62c9de2dc23b0f2ce9a136e8e.zip
Add custom rand function implimentation, based on precreated random sequence.
Diffstat (limited to 'src')
-rw-r--r--src/CMakeLists.txt4
-rw-r--r--src/Makefile.am2
-rw-r--r--src/client.cpp4
-rw-r--r--src/dyetool/client.cpp3
-rw-r--r--src/particle/particle.cpp7
-rw-r--r--src/resources/sprite/animatedsprite.cpp9
-rw-r--r--src/utils/mrand.cpp45
-rw-r--r--src/utils/mrand.h30
8 files changed, 97 insertions, 7 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index fa1239794..379d39ce7 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -826,6 +826,8 @@ SET(SRCS
utils/naclmessages.h
utils/mkdir.cpp
utils/mkdir.h
+ utils/mrand.cpp
+ utils/mrand.h
utils/xml.h
utils/xml.inc
utils/xmlutils.cpp
@@ -1410,6 +1412,8 @@ SET(DYE_CMD_SRCS
utils/files.h
utils/mkdir.cpp
utils/mkdir.h
+ utils/mrand.cpp
+ utils/mrand.h
utils/paths.cpp
utils/paths.h
utils/perfomance.cpp
diff --git a/src/Makefile.am b/src/Makefile.am
index ee7fdd151..84d3c0bb0 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -442,6 +442,8 @@ SRC += events/actionevent.h \
utils/mathutils.h \
utils/mkdir.cpp \
utils/mkdir.h \
+ utils/mrand.cpp \
+ utils/mrand.h \
utils/parameters.cpp \
utils/parameters.h \
utils/paths.cpp \
diff --git a/src/client.cpp b/src/client.cpp
index 07b35b6f8..7a550d80a 100644
--- a/src/client.cpp
+++ b/src/client.cpp
@@ -131,6 +131,7 @@
#include "utils/fuzzer.h"
#include "utils/gettext.h"
#include "utils/gettexthelper.h"
+#include "utils/mrand.h"
#ifdef ANDROID
#include "utils/paths.h"
#endif
@@ -243,6 +244,7 @@ void Client::testsInit()
}
else
{
+ initRand();
logger = new Logger;
Dirs::initLocalDataDir();
Dirs::initTempDir();
@@ -255,6 +257,8 @@ void Client::gameInit()
{
logger = new Logger;
+ initRand();
+
// Load branding information
if (!settings.options.brandingPath.empty())
branding.init(settings.options.brandingPath);
diff --git a/src/dyetool/client.cpp b/src/dyetool/client.cpp
index 939239d72..30dccd228 100644
--- a/src/dyetool/client.cpp
+++ b/src/dyetool/client.cpp
@@ -56,6 +56,7 @@
#include "utils/fuzzer.h"
#include "utils/gettext.h"
#include "utils/gettexthelper.h"
+#include "utils/mrand.h"
#ifdef ANDROID
#include "utils/paths.h"
#endif
@@ -136,6 +137,8 @@ void Client::gameInit()
{
logger = new Logger;
+ initRand();
+
// Load branding information
if (!settings.options.brandingPath.empty())
branding.init(settings.options.brandingPath);
diff --git a/src/particle/particle.cpp b/src/particle/particle.cpp
index 4bf01207c..eb2456306 100644
--- a/src/particle/particle.cpp
+++ b/src/particle/particle.cpp
@@ -42,6 +42,7 @@
#include "utils/delete2.h"
#include "utils/dtor.h"
#include "utils/mathutils.h"
+#include "utils/mrand.h"
#include "debug.h"
@@ -161,11 +162,11 @@ void Particle::updateSelf() restrict2
if (mRandomness >= 10) // reduce useless calculations
{
const int rand2 = mRandomness * 2;
- mVelocity.x += static_cast<float>(std::rand() % rand2 - mRandomness)
+ mVelocity.x += static_cast<float>(mrand() % rand2 - mRandomness)
/ 1000.0F;
- mVelocity.y += static_cast<float>(std::rand() % rand2 - mRandomness)
+ mVelocity.y += static_cast<float>(mrand() % rand2 - mRandomness)
/ 1000.0F;
- mVelocity.z += static_cast<float>(std::rand() % rand2 - mRandomness)
+ mVelocity.z += static_cast<float>(mrand() % rand2 - mRandomness)
/ 1000.0F;
}
diff --git a/src/resources/sprite/animatedsprite.cpp b/src/resources/sprite/animatedsprite.cpp
index 34aca4706..f0af74f08 100644
--- a/src/resources/sprite/animatedsprite.cpp
+++ b/src/resources/sprite/animatedsprite.cpp
@@ -36,6 +36,7 @@
#include "resources/sprite/animationdelayload.h"
#include "utils/delete2.h"
+#include "utils/mrand.h"
#include "debug.h"
@@ -240,7 +241,7 @@ bool AnimatedSprite::updateCurrentAnimation(const unsigned int time) restrict2
!mFrame->nextAction.empty())
{
if (mFrame->rand == 100 ||
- mFrame->rand >= rand() % 100)
+ mFrame->rand >= mrand() % 100)
{
for (size_t i = 0; i < mAnimation->getLength(); i ++)
{
@@ -272,7 +273,7 @@ bool AnimatedSprite::updateCurrentAnimation(const unsigned int time) restrict2
!mFrame->nextAction.empty())
{
if (mFrame->rand == 100 ||
- mFrame->rand >= rand() % 100)
+ mFrame->rand >= mrand() % 100)
{
play(mFrame->nextAction);
return true;
@@ -283,7 +284,7 @@ bool AnimatedSprite::updateCurrentAnimation(const unsigned int time) restrict2
mFrame->type == Frame::ANIMATION)
{
if (mFrame->rand == 100 ||
- mFrame->rand >= rand() % 100)
+ mFrame->rand >= mrand() % 100)
{
mAnimation = nullptr;
mFrame = nullptr;
@@ -299,7 +300,7 @@ bool AnimatedSprite::updateCurrentAnimation(const unsigned int time) restrict2
}
else
{
- if (rand() % 100 <= mFrame->rand)
+ if (mrand() % 100 <= mFrame->rand)
fail = false;
}
}
diff --git a/src/utils/mrand.cpp b/src/utils/mrand.cpp
new file mode 100644
index 000000000..85b3e6b9c
--- /dev/null
+++ b/src/utils/mrand.cpp
@@ -0,0 +1,45 @@
+/*
+ * The ManaPlus Client
+ * Copyright (C) 2016 The ManaPlus Developers
+ *
+ * This file is part of The ManaPlus Client.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "utils/mrand.h"
+
+#include <cstdlib>
+
+#include "debug.h"
+
+namespace
+{
+ constexpr const int randNumbers = 1024;
+ int mPos = 0;
+ int mRand[randNumbers];
+} // namespace
+
+void initRand()
+{
+ for (int f = 0; f < randNumbers; f ++)
+ mRand[f] = std::rand();
+}
+
+int mrand()
+{
+ if (mPos >= randNumbers)
+ mPos = 0;
+ return mRand[++mPos];
+}
diff --git a/src/utils/mrand.h b/src/utils/mrand.h
new file mode 100644
index 000000000..11a3a22a8
--- /dev/null
+++ b/src/utils/mrand.h
@@ -0,0 +1,30 @@
+/*
+ * The ManaPlus Client
+ * Copyright (C) 2016 The ManaPlus Developers
+ *
+ * This file is part of The ManaPlus Client.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef UTILS_MRAND_H
+#define UTILS_MRAND_H
+
+#include "localconsts.h"
+
+void initRand();
+
+int mrand();
+
+#endif // UTILS_MRAND_H