summaryrefslogtreecommitdiffstats
path: root/crawl-ref
diff options
context:
space:
mode:
authorStefan O'Rear <stefanor@cox.net>2009-11-02 14:03:40 -0800
committerStefan O'Rear <stefanor@cox.net>2009-11-02 14:03:40 -0800
commit45447fb9f45ccf3dbf38993a1af102e8c60c693e (patch)
treee58691256464e0023461ebe881f9f33e2be0d52e /crawl-ref
parent0f69bed947d7d06ce3655279d22f32cd15bdd3a1 (diff)
downloadcrawl-ref-45447fb9f45ccf3dbf38993a1af102e8c60c693e.tar.gz
crawl-ref-45447fb9f45ccf3dbf38993a1af102e8c60c693e.zip
Factor a visitor object out of apply_to_level
Diffstat (limited to 'crawl-ref')
-rw-r--r--crawl-ref/source/files.cc69
-rw-r--r--crawl-ref/source/files.h13
-rw-r--r--crawl-ref/source/monstuff.cc4
3 files changed, 45 insertions, 41 deletions
diff --git a/crawl-ref/source/files.cc b/crawl-ref/source/files.cc
index 82c308de6d..3c8ee8e2ed 100644
--- a/crawl-ref/source/files.cc
+++ b/crawl-ref/source/files.cc
@@ -1901,55 +1901,50 @@ bool is_existing_level(const level_id &level)
return (Generated_Levels.find(level) != Generated_Levels.end());
}
-// Applies an operation (applicator) after switching to the specified level.
-// If preserve_current is true, will reload the original level after
-// modifying the target level.
-//
-// If the target level has not already been visited by the player, this
-// function will assert.
-bool apply_to_level(const level_id &level, bool preserve_current,
- bool (*applicator)())
+// This class provides a way to walk the dungeon with a bit more flexibility
+// than you get with apply_to_all_dungeons.
+level_excursion::level_excursion()
+ : original(level_id::current())
{
- ASSERT(is_existing_level(level));
+}
- const level_id original = level_id::current();
- if (level != original)
+void level_excursion::go_to(const level_id& next)
+{
+ if (level_id::current() != next)
{
- if (preserve_current)
- _save_level(you.your_level, you.level_type, you.where_are_you);
+ _save_level(you.your_level, you.level_type, you.where_are_you);
+ _restore_level(next);
+ }
+}
- you.where_are_you = level.branch;
- you.your_level = level.dungeon_absdepth();
- you.level_type = level.level_type;
+level_excursion::~level_excursion()
+{
+ go_to(original);
+}
- // Load the dungeon level...
- load(DNGN_STONE_STAIRS_DOWN_I, LOAD_VISITOR,
- LEVEL_DUNGEON, original.absdepth(),
- original.branch);
- }
- // Apply the change.
- const bool result = applicator();
+// Applies an operation (applicator) after switching to the specified level.
+// Will reload the original level after modifying the target level.
+//
+// If the target level has not already been visited by the player, this
+// function will assert.
+bool apply_to_level(const level_id &level, bool (*applicator)())
+{
+ ASSERT(is_existing_level(level));
- if (level != original)
- {
- // And save it back.
- _save_level(you.your_level, you.level_type, you.where_are_you);
+ level_excursion le;
- if (preserve_current)
- _restore_level(original);
- }
+ le.go_to(level);
- return (result);
+ return applicator();
}
bool apply_to_all_dungeons(bool (*applicator)())
{
- const level_id original = level_id::current();
+ level_excursion le;
+ level_id original(level_id::current());
- // Apply to current level, then save it out.
bool success = applicator();
- _save_level(original.absdepth(), original.level_type, original.branch);
for (int i = 0; i < MAX_LEVELS; ++i)
for (int j = 0; j < NUM_BRANCHES; ++j)
@@ -1964,12 +1959,12 @@ bool apply_to_all_dungeons(bool (*applicator)())
if (original == thislevel)
continue;
- if (apply_to_level(thislevel, false, applicator))
+ le.go_to(thislevel);
+
+ if (applicator())
success = true;
}
- _restore_level(original);
-
return (success);
}
diff --git a/crawl-ref/source/files.h b/crawl-ref/source/files.h
index 5c639eeb14..eded735f1b 100644
--- a/crawl-ref/source/files.h
+++ b/crawl-ref/source/files.h
@@ -102,11 +102,20 @@ bool apply_to_all_dungeons(bool (*applicator)());
class level_id;
-bool apply_to_level(const level_id &level, bool preserve_current,
- bool (*applicator)());
+bool apply_to_level(const level_id &level, bool (*applicator)());
bool is_existing_level(const level_id &level);
+class level_excursion
+{
+ level_id original;
+
+public:
+ level_excursion();
+ ~level_excursion();
+
+ void go_to(const level_id &level);
+};
// last updated 12may2000 {dlb}
/* ***********************************************************************
diff --git a/crawl-ref/source/monstuff.cc b/crawl-ref/source/monstuff.cc
index 3b1aa9e86c..0503db420d 100644
--- a/crawl-ref/source/monstuff.cc
+++ b/crawl-ref/source/monstuff.cc
@@ -829,14 +829,14 @@ void slime_vault_change(bool glass)
{
if (glass)
{
- apply_to_level(target, true,
+ apply_to_level(target,
target == level_id::current() ?
_slime_vault_to_glass_onlevel :
_slime_vault_to_glass_offlevel);
}
else
{
- apply_to_level(target, true,
+ apply_to_level(target,
target == level_id::current() ?
_slime_vault_to_floor_onlevel :
_slime_vault_to_floor_offlevel);