summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/random-var.cc
diff options
context:
space:
mode:
authorAdam Borowski <kilobyte@angband.pl>2011-09-19 12:40:50 +0200
committerAdam Borowski <kilobyte@angband.pl>2011-09-19 12:43:20 +0200
commit44e7ef5f797353e07ffc1e900918a32e9877dc50 (patch)
tree514a49de0c218594b65a6fcf10f85a620ad23740 /crawl-ref/source/random-var.cc
parenta51d98d6c6065e0a5ac6655ea1cfe28c5ef8eb87 (diff)
downloadcrawl-ref-44e7ef5f797353e07ffc1e900918a32e9877dc50.tar.gz
crawl-ref-44e7ef5f797353e07ffc1e900918a32e9877dc50.zip
Implement div_rand_round() for random_var, use it for combat delay.
Two constants divide to the same value no matter the scale, using random_var was just a waste of CPU. What we want is to allow partial values a percentage of the time.
Diffstat (limited to 'crawl-ref/source/random-var.cc')
-rw-r--r--crawl-ref/source/random-var.cc17
1 files changed, 17 insertions, 0 deletions
diff --git a/crawl-ref/source/random-var.cc b/crawl-ref/source/random-var.cc
index ebfe54deba..33f7029d6a 100644
--- a/crawl-ref/source/random-var.cc
+++ b/crawl-ref/source/random-var.cc
@@ -155,6 +155,23 @@ random_var operator/(const random_var& x, int d)
return (random_var(start, end, weights));
}
+random_var div_rand_round(const random_var& x, int d)
+{
+ const int start = x.min() / d;
+ const int end = (x.max() + d - 1) / d + 1;
+ std::vector<int> weights(end - start, 0);
+
+ for (int v = x.min(); v <= x.max(); ++v)
+ {
+ int rem = v % d;
+ weights[v / d - start] += x.weight(v) * (d - rem);
+ if (rem > 0)
+ weights[v / d + 1 - start] += x.weight(v) * rem;
+ }
+
+ return (random_var(start, end, weights));
+}
+
random_var rv::max(const random_var& x, const random_var& y)
{
const int start = std::max(x.min(), y.min());