summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/random.cc
diff options
context:
space:
mode:
authorAdam Borowski <kilobyte@angband.pl>2011-10-31 13:24:02 +0100
committerAdam Borowski <kilobyte@angband.pl>2011-10-31 13:24:02 +0100
commitbc61b047875b68f362a964ae5ab53d3cd283a26e (patch)
tree6e5b907e1d953e826691cabb4843d6582a623734 /crawl-ref/source/random.cc
parentc56cb9b32b1f15ec37cbdf79c9d0d4e16839ae09 (diff)
downloadcrawl-ref-bc61b047875b68f362a964ae5ab53d3cd283a26e.tar.gz
crawl-ref-bc61b047875b68f362a964ae5ab53d3cd283a26e.zip
Use templates for random_choose() to eliminate the need for multiple definitions.
Diffstat (limited to 'crawl-ref/source/random.cc')
-rw-r--r--crawl-ref/source/random.cc80
1 files changed, 3 insertions, 77 deletions
diff --git a/crawl-ref/source/random.cc b/crawl-ref/source/random.cc
index 323fa45ebc..91ba6191b6 100644
--- a/crawl-ref/source/random.cc
+++ b/crawl-ref/source/random.cc
@@ -18,47 +18,11 @@ int random_range(int low, int high, int nrolls)
return low + roll;
}
-// Chooses one of the numbers passed in at random. The list of numbers
-// must be terminated with -1.
-static int _random_choose(int first, va_list args)
-{
- int chosen = first, count = 1, nargs = 100;
-
- while (nargs-- > 0)
- {
- const int pick = va_arg(args, int);
- if (pick == -1)
- break;
- if (one_chance_in(++count))
- chosen = pick;
- }
-
- 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 (monster_type)chosen;
-}
-
// Chooses one of the strings passed in at random. The list of strings
-// must be terminated with NULL. Sadly, NULL is not -1, and 0 is popular
+// must be terminated with NULL. 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, ...)
+template <>
+const char* random_choose<const char*>(const char* first, ...)
{
va_list args;
va_start(args, first);
@@ -81,44 +45,6 @@ const char* random_choose_string(const char* first, ...)
return (chosen);
}
-static int _random_choose_weighted(int weight, int first, va_list args)
-{
- int chosen = first, cweight = weight, nargs = 100;
-
- while (nargs-- > 0)
- {
- const int nweight = va_arg(args, int);
- if (!nweight)
- break;
-
- const int choice = va_arg(args, int);
- if (random2(cweight += nweight) < nweight)
- chosen = choice;
- }
-
- ASSERT(nargs > 0);
-
- 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