summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/random.cc
diff options
context:
space:
mode:
authorAdam Borowski <kilobyte@angband.pl>2011-01-18 01:37:37 +0100
committerAdam Borowski <kilobyte@angband.pl>2011-10-31 12:54:20 +0100
commitc56cb9b32b1f15ec37cbdf79c9d0d4e16839ae09 (patch)
treec22b503e8667f882c13f49bac8538ad474b5b525 /crawl-ref/source/random.cc
parentaacbd52137bd2f5668c1628c099a9fb1e366ceef (diff)
downloadcrawl-ref-c56cb9b32b1f15ec37cbdf79c9d0d4e16839ae09.tar.gz
crawl-ref-c56cb9b32b1f15ec37cbdf79c9d0d4e16839ae09.zip
Simplify use of random_choose() by using overloading.
Diffstat (limited to 'crawl-ref/source/random.cc')
-rw-r--r--crawl-ref/source/random.cc50
1 files changed, 39 insertions, 11 deletions
diff --git a/crawl-ref/source/random.cc b/crawl-ref/source/random.cc
index 59072a12e1..323fa45ebc 100644
--- a/crawl-ref/source/random.cc
+++ b/crawl-ref/source/random.cc
@@ -20,11 +20,8 @@ int random_range(int low, int high, int nrolls)
// Chooses one of the numbers passed in at random. The list of numbers
// must be terminated with -1.
-int random_choose(int first, ...)
+static int _random_choose(int first, va_list args)
{
- va_list args;
- va_start(args, first);
-
int chosen = first, count = 1, nargs = 100;
while (nargs-- > 0)
@@ -37,13 +34,30 @@ int random_choose(int first, ...)
}
ASSERT(nargs > 0);
+ return (chosen);
+}
+
+int random_choose(int first, ...)
+{
+ va_list args;
+ va_start(args, first);
+ int chosen = _random_choose(first, args);
+ va_end(args);
+ return chosen;
+}
+monster_type random_choose(monster_type first, ...)
+{
+ va_list args;
+ va_start(args, first);
+ int chosen = _random_choose(first, args);
va_end(args);
- return (chosen);
+ return (monster_type)chosen;
}
// Chooses one of the strings passed in at random. The list of strings
-// must be terminated with NULL.
+// must be terminated with NULL. Sadly, NULL is not -1, and 0 is popular
+// value for enums, so we need to copy the function.
const char* random_choose_string(const char* first, ...)
{
va_list args;
@@ -67,11 +81,8 @@ const char* random_choose_string(const char* first, ...)
return (chosen);
}
-int random_choose_weighted(int weight, int first, ...)
+static int _random_choose_weighted(int weight, int first, va_list args)
{
- va_list args;
- va_start(args, first);
-
int chosen = first, cweight = weight, nargs = 100;
while (nargs-- > 0)
@@ -87,10 +98,27 @@ int random_choose_weighted(int weight, int first, ...)
ASSERT(nargs > 0);
- va_end(args);
return (chosen);
}
+int random_choose_weighted(int weight, int first, ...)
+{
+ va_list args;
+ va_start(args, first);
+ int chosen = _random_choose_weighted(weight, first, args);
+ va_end(args);
+ return chosen;
+}
+
+monster_type random_choose_weighted(int weight, monster_type first, ...)
+{
+ va_list args;
+ va_start(args, first);
+ int chosen = _random_choose_weighted(weight, first, args);
+ va_end(args);
+ return (monster_type)chosen;
+}
+
#ifndef UINT32_MAX
#define UINT32_MAX ((uint32_t)(-1))
#endif