diff options
author | Adam Borowski <kilobyte@angband.pl> | 2013-10-26 16:02:11 +0200 |
---|---|---|
committer | Adam Borowski <kilobyte@angband.pl> | 2013-10-27 01:53:01 +0200 |
commit | b95792fc170e37c89afa91322c1e0f4f2d291392 (patch) | |
tree | 028677eabc3fe5fb502e2b2a7deb8362abb41a95 /crawl-ref/source/random.cc | |
parent | 1b0b748f7c32abe7bb3e5d60667eef29f9426dde (diff) | |
download | crawl-ref-b95792fc170e37c89afa91322c1e0f4f2d291392.tar.gz crawl-ref-b95792fc170e37c89afa91322c1e0f4f2d291392.zip |
Refactor additional messages for new monsters spawning.
Not sure why do we need both these and "comes into view".
This includes simplification of:
* shoving the player out of stairs
* "wandering" monsters spawning on the stairs
* fancy messages for in-LOS abyss spawns
For the last part, they had really obscure probabilities, this commit makes
them easier to read. Also, it avoid mallocs and RNG calls, but that's a
rather negligible optimization.
This commit eliminates a mysterious call to viewwindow() as well. A comment
claimed that "Special case: must update the view for monsters created in
player LOS." yet this was not done for band members. It appears removing
that call has no ill effects, but I might have missed something.
Diffstat (limited to 'crawl-ref/source/random.cc')
-rw-r--r-- | crawl-ref/source/random.cc | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/crawl-ref/source/random.cc b/crawl-ref/source/random.cc index cd617c8b1a..ec4ebfec8c 100644 --- a/crawl-ref/source/random.cc +++ b/crawl-ref/source/random.cc @@ -87,6 +87,30 @@ const char* random_choose<const char*>(const char* first, ...) return chosen; } +const char* random_choose_weighted(int weight, const char* first, ...) +{ + va_list args; + va_start(args, first); + const char* chosen = first; + int cweight = weight, nargs = 100; + + while (nargs-- > 0) + { + const int nweight = va_arg(args, int); + if (!nweight) + break; + + const char* choice = va_arg(args, const char*); + if (random2(cweight += nweight) < nweight) + chosen = choice; + } + + va_end(args); + ASSERT(nargs > 0); + + return chosen; +} + #ifndef UINT32_MAX #define UINT32_MAX ((uint32_t)(-1)) #endif |