diff options
author | Pete Hurst <pete@streamuniverse.tv> | 2013-06-22 20:34:41 +0100 |
---|---|---|
committer | Pete Hurst <pete@streamuniverse.tv> | 2013-06-23 02:43:28 +0100 |
commit | 0e5d46227251053be2ac5fb92cba2427c9c42b20 (patch) | |
tree | 1e208212c0030d59dff08393d05c946a92c73429 /crawl-ref/source/mon-pick.h | |
parent | 94225b818f4224d364b89af2accef481d8af3f80 (diff) | |
download | crawl-ref-0e5d46227251053be2ac5fb92cba2427c9c42b20.tar.gz crawl-ref-0e5d46227251053be2ac5fb92cba2427c9c42b20.zip |
Rework mon-pick algorithm as a class template
This enables the distributions to be easily used for picking over
enums other than monster_type, and even for arbitrary objects.
The new template is contained in random_pick. It can be used
simply by creating a random_picker<T> and calling its pick
method, or can be subclassed if more complex veto behaviour
is required.
Diffstat (limited to 'crawl-ref/source/mon-pick.h')
-rw-r--r-- | crawl-ref/source/mon-pick.h | 40 |
1 files changed, 21 insertions, 19 deletions
diff --git a/crawl-ref/source/mon-pick.h b/crawl-ref/source/mon-pick.h index 47ddce1755..7ad9154a7f 100644 --- a/crawl-ref/source/mon-pick.h +++ b/crawl-ref/source/mon-pick.h @@ -8,27 +8,11 @@ #define MONPICK_H #include "externs.h" +#include "random-pick.h" #define DEPTH_NOWHERE 999 -enum distrib_type -{ - FLAT, // full chance throughout the range - SEMI, // 50% chance at range ends, 100% in the middle - PEAK, // 0% chance just outside range ends, 100% in the middle, range - // ends typically get ~20% or more - UP, // linearly from near-zero to 100%, increasing with depth - DOWN, // linearly from 100% at the start to near-zero -}; - -typedef struct -{ - int minr; - int maxr; - int rarity; // 0..1000 - distrib_type distrib; - monster_type mons; -} pop_entry; +#define pop_entry random_pick_entry<monster_type> typedef bool (*mon_pick_vetoer)(monster_type); @@ -36,7 +20,8 @@ int mons_rarity(monster_type mcls, branch_type branch); int mons_depth(monster_type mcls, branch_type branch); monster_type pick_monster(level_id place, mon_pick_vetoer veto = nullptr); -monster_type pick_monster_from(const pop_entry *fpop, int depth, mon_pick_vetoer veto = nullptr); +monster_type pick_monster_from(const pop_entry *fpop, int depth, + mon_pick_vetoer = nullptr); monster_type pick_monster_no_rarity(branch_type branch); monster_type pick_monster_by_hash(branch_type branch, uint32_t hash); monster_type pick_monster_all_branches(int absdepth0, mon_pick_vetoer veto = nullptr); @@ -44,4 +29,21 @@ bool branch_has_monsters(branch_type branch); int branch_ood_cap(branch_type branch); void debug_monpick(); + +// Subclass the random_picker template to make a monster_picker class. +// The main reason for this is that passing delegates into template functions +// is fraught with peril when the delegate's arguments use T, until 0x at least. +// There is supposedly a nasty workaround but this wouldn't even compile for me. +class monster_picker : public random_picker<monster_type, NUM_MONSTERS> +{ +public: + monster_picker(mon_pick_vetoer vetoer = nullptr) : _veto(vetoer) { }; + +protected: + virtual bool veto(monster_type mon); + +private: + mon_pick_vetoer _veto; +}; + #endif |