From 45447fb9f45ccf3dbf38993a1af102e8c60c693e Mon Sep 17 00:00:00 2001 From: Stefan O'Rear Date: Mon, 2 Nov 2009 14:03:40 -0800 Subject: Factor a visitor object out of apply_to_level --- crawl-ref/source/files.cc | 69 ++++++++++++++++++++------------------------ crawl-ref/source/files.h | 13 +++++++-- crawl-ref/source/monstuff.cc | 4 +-- 3 files changed, 45 insertions(+), 41 deletions(-) (limited to 'crawl-ref') 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); -- cgit v1.2.3-54-g00ecf