diff options
author | Adam Borowski <kilobyte@angband.pl> | 2013-10-31 08:16:07 +0100 |
---|---|---|
committer | Adam Borowski <kilobyte@angband.pl> | 2013-10-31 16:57:54 +0100 |
commit | a57962fa1975ecc27863c2f847fa2a1c4484f7fa (patch) | |
tree | 7e5055301b3554b4598be94b52b65633fea156dc /crawl-ref/source/act-iter.cc | |
parent | b5aec581b1ad18e6eefb77947e822c089f778d6f (diff) | |
download | crawl-ref-a57962fa1975ecc27863c2f847fa2a1c4484f7fa.tar.gz crawl-ref-a57962fa1975ecc27863c2f847fa2a1c4484f7fa.zip |
monster_near_iterator
Does the "in view" part of functionality of monster_iterator, is simpler,
allows using los models other than LOS_DEFAULT, and gets rid of a lot of
uses of get_los().
The code is nearly identical as actor_near_iterator, but the old delegation
used more code than either of those. Still, perhaps templating could work?
This commit also fixes a buttload of ignoring invis / see invis / sense invis
(ie, visible_to()) and act-through-glass bugs.
Diffstat (limited to 'crawl-ref/source/act-iter.cc')
-rw-r--r-- | crawl-ref/source/act-iter.cc | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/crawl-ref/source/act-iter.cc b/crawl-ref/source/act-iter.cc index d5e0b3e41e..8e62e1acdb 100644 --- a/crawl-ref/source/act-iter.cc +++ b/crawl-ref/source/act-iter.cc @@ -68,3 +68,67 @@ void actor_near_iterator::advance() return; while (!valid(**this)); } + +////////////////////////////////////////////////////////////////////////// + +monster_near_iterator::monster_near_iterator(coord_def c, los_type los) + : center(c), _los(los), viewer(nullptr), i(0) +{ + if (!valid(&menv[0])) + advance(); +} + +monster_near_iterator::monster_near_iterator(const actor *a, los_type los) + : center(a->pos()), _los(los), viewer(a), i(0) +{ + if (!valid(&menv[0])) + advance(); +} + +monster_near_iterator::operator bool() const +{ + return valid(**this); +} + +monster* monster_near_iterator::operator*() const +{ + if (i < MAX_MONSTERS) + return &menv[i]; + else + return nullptr; +} + +monster* monster_near_iterator::operator->() const +{ + return **this; +} + +monster_near_iterator& monster_near_iterator::operator++() +{ + advance(); + return *this; +} + +monster_near_iterator monster_near_iterator::operator++(int) +{ + monster_near_iterator copy = *this; + ++(*this); + return copy; +} + +bool monster_near_iterator::valid(const monster* a) const +{ + if (!a || !a->alive()) + return false; + if (viewer && !a->visible_to(a)) + return false; + return cell_see_cell(center, a->pos(), _los); +} + +void monster_near_iterator::advance() +{ + do + if (++i >= MAX_MONSTERS) + return; + while (!valid(**this)); +} |