diff options
author | Jesse Luehrs <doy@tozt.net> | 2014-08-02 20:06:34 -0400 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2014-08-02 20:10:40 -0400 |
commit | 4e1b44a4905cccd1a8509a91f9d10d7a06f956e7 (patch) | |
tree | 62b76b8e00325f1f7a96c646f10b5a5fe62919e6 /crawl-ref/source/branch.cc | |
parent | 756871af9b64cee576375e9ed5f73cf3cfa1b453 (diff) | |
download | crawl-ref-4e1b44a4905cccd1a8509a91f9d10d7a06f956e7.tar.gz crawl-ref-4e1b44a4905cccd1a8509a91f9d10d7a06f956e7.zip |
allow iterating over branches in a non-enum order (8742)
This allows us to have a consistent and logical ordering of branches
without requiring the branch enum itself to be reordered (which could
have save compatibility implications). The new ordering of branches just
moves Depths to the place in the ordering that it already is planned to
go on the next major save compat bump, but other changes are possible,
if desired. All places in the code that iterate over branches have been
updated to use the new iterator except for code dealing with save files,
which still uses enum order, so that we can change the display ordering
without affecting saves.
Diffstat (limited to 'crawl-ref/source/branch.cc')
-rw-r--r-- | crawl-ref/source/branch.cc | 94 |
1 files changed, 90 insertions, 4 deletions
diff --git a/crawl-ref/source/branch.cc b/crawl-ref/source/branch.cc index d4477564a5..61bde7c82f 100644 --- a/crawl-ref/source/branch.cc +++ b/crawl-ref/source/branch.cc @@ -11,6 +11,87 @@ FixedVector<int, NUM_BRANCHES> brdepth; FixedVector<int, NUM_BRANCHES> branch_bribe; branch_type root_branch; +branch_iterator::branch_iterator() : + i(BRANCH_DUNGEON) +{ +} + +branch_iterator::operator bool() const +{ + return i < NUM_BRANCHES; +} + +const Branch* branch_iterator::operator*() const +{ + static const branch_type branch_order[] = { + BRANCH_DUNGEON, + BRANCH_TEMPLE, + BRANCH_ORC, + BRANCH_ELF, +#if TAG_MAJOR_VERSION == 34 + BRANCH_DWARF, +#endif + BRANCH_LAIR, + BRANCH_SWAMP, + BRANCH_SHOALS, + BRANCH_SNAKE, + BRANCH_SPIDER, + BRANCH_SLIME, + BRANCH_VAULTS, +#if TAG_MAJOR_VERSION == 34 + BRANCH_BLADE, +#endif + BRANCH_CRYPT, + BRANCH_TOMB, + BRANCH_DEPTHS, + BRANCH_VESTIBULE, + BRANCH_DIS, + BRANCH_GEHENNA, + BRANCH_COCYTUS, + BRANCH_TARTARUS, + BRANCH_ZOT, +#if TAG_MAJOR_VERSION == 34 + BRANCH_FOREST, +#endif + BRANCH_ABYSS, + BRANCH_PANDEMONIUM, + BRANCH_ZIGGURAT, + BRANCH_LABYRINTH, + BRANCH_BAZAAR, + BRANCH_TROVE, + BRANCH_SEWER, + BRANCH_OSSUARY, + BRANCH_BAILEY, + BRANCH_ICE_CAVE, + BRANCH_VOLCANO, + BRANCH_WIZLAB + }; + COMPILE_CHECK(ARRAYSZ(branch_order) == NUM_BRANCHES); + + if (i < NUM_BRANCHES) + return &branches[branch_order[i]]; + else + return nullptr; +} + +const Branch* branch_iterator::operator->() const +{ + return **this; +} + +branch_iterator& branch_iterator::operator++() +{ + i++; + return *this; +} + +branch_iterator branch_iterator::operator++(int) +{ + branch_iterator copy = *this; + ++(*this); + return copy; +} + const Branch& your_branch() { return branches[you.where_are_you]; @@ -44,10 +125,15 @@ bool is_random_subbranch(branch_type branch) || branch == BRANCH_CRYPT; } +bool is_connected_branch(const Branch *branch) +{ + return !(branch->branch_flags & BFLAG_NO_XLEV_TRAVEL); +} + bool is_connected_branch(branch_type branch) { ASSERT_RANGE(branch, 0, NUM_BRANCHES); - return !(branches[branch].branch_flags & BFLAG_NO_XLEV_TRAVEL); + return is_connected_branch(&branches[branch]); } bool is_connected_branch(level_id place) @@ -57,9 +143,9 @@ bool is_connected_branch(level_id place) branch_type str_to_branch(const string &branch, branch_type err) { - for (int i = 0; i < NUM_BRANCHES; ++i) - if (branches[i].abbrevname && branches[i].abbrevname == branch) - return static_cast<branch_type>(i); + for (branch_iterator it; it; ++it) + if (it->abbrevname && it->abbrevname == branch) + return it->id; return err; } |