summaryrefslogtreecommitdiff
path: root/src/common/utils.c
diff options
context:
space:
mode:
authorHaru <haru@dotalux.com>2015-12-19 18:15:04 +0100
committerHaru <haru@dotalux.com>2016-01-29 10:58:05 +0100
commitb7c5b53cccac17765fd7445b390853f5a0eb1a4d (patch)
treef137eb807480a94a67fc334178fffdfd3c029e65 /src/common/utils.c
parent50de6b49043968d01b2cc7565376d5e06c29b490 (diff)
downloadhercules-b7c5b53cccac17765fd7445b390853f5a0eb1a4d.tar.gz
hercules-b7c5b53cccac17765fd7445b390853f5a0eb1a4d.tar.bz2
hercules-b7c5b53cccac17765fd7445b390853f5a0eb1a4d.tar.xz
hercules-b7c5b53cccac17765fd7445b390853f5a0eb1a4d.zip
Added apply_percentrate and apply_percentrate64 functions
Signed-off-by: Haru <haru@dotalux.com>
Diffstat (limited to 'src/common/utils.c')
-rw-r--r--src/common/utils.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/common/utils.c b/src/common/utils.c
index dcf0a749a..73df3aae1 100644
--- a/src/common/utils.c
+++ b/src/common/utils.c
@@ -25,6 +25,7 @@
#include "common/cbasetypes.h"
#include "common/core.h"
#include "common/mmo.h"
+#include "common/nullpo.h"
#include "common/showmsg.h"
#include "common/socket.h"
#include "common/strlib.h"
@@ -352,6 +353,48 @@ unsigned int get_percentage(const unsigned int A, const unsigned int B)
return (unsigned int)floor(result);
}
+/**
+ * Applies a percentual rate modifier.
+ *
+ * @param value The base value.
+ * @param rate The rate modifier to apply.
+ * @param stdrate The rate modifier's divider (rate == stdrate => 100%).
+ * @return The modified value.
+ */
+int64 apply_percentrate64(int64 value, int rate, int stdrate)
+{
+ Assert_ret(stdrate > 0);
+ Assert_ret(rate >= 0);
+ if (rate == stdrate)
+ return value;
+ if (rate == 0)
+ return 0;
+ if (INT64_MAX / rate < value) {
+ // Give up some precision to prevent overflows
+ return value / stdrate * rate;
+ }
+ return value * rate / stdrate;
+}
+
+/**
+ * Applies a percentual rate modifier.
+ *
+ * @param value The base value.
+ * @param rate The rate modifier to apply. Must be <= maxrate.
+ * @param maxrate The rate modifier's divider (maxrate = 100%).
+ * @return The modified value.
+ */
+int apply_percentrate(int value, int rate, int maxrate)
+{
+ Assert_ret(maxrate > 0);
+ Assert_ret(rate >= 0);
+ if (rate == maxrate)
+ return value;
+ if (rate == 0)
+ return 0;
+ return (int)(value * (int64)rate / maxrate);
+}
+
//-----------------------------------------------------
// custom timestamp formatting (from eApp)
//-----------------------------------------------------