summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Cline <zelgadis@sourceforge.net>2009-11-14 04:35:48 -0800
committerMatthew Cline <zelgadis@sourceforge.net>2009-11-14 04:35:48 -0800
commit4cb4bea3108f8fa560669bab120623729b3ff4b8 (patch)
tree2ce3cfc994e90bab1e59200b1c35293933c42880
parent704b61fedeafc27485cbe688b135b3f8ebd00955 (diff)
downloadcrawl-ref-4cb4bea3108f8fa560669bab120623729b3ff4b8.tar.gz
crawl-ref-4cb4bea3108f8fa560669bab120623729b3ff4b8.zip
stash: ST_ItemIterator class
Class for iterating over all the items in the stash tracker.
-rw-r--r--crawl-ref/source/stash.cc146
-rw-r--r--crawl-ref/source/stash.h39
2 files changed, 185 insertions, 0 deletions
diff --git a/crawl-ref/source/stash.cc b/crawl-ref/source/stash.cc
index 7b861de749..7c403a1a98 100644
--- a/crawl-ref/source/stash.cc
+++ b/crawl-ref/source/stash.cc
@@ -1966,3 +1966,149 @@ void StashTracker::update_corpses()
iter->second._update_corpses(rot_time);
}
}
+
+//////////////////////////////////////////////
+
+ST_ItemIterator::ST_ItemIterator()
+{
+ m_stash_level_it = StashTrack.levels.begin();
+ new_level();
+ //(*this)++;
+}
+
+ST_ItemIterator::operator bool() const
+{
+ return (m_item != NULL);
+}
+
+const item_def& ST_ItemIterator::operator *() const
+{
+ return (*m_item);
+}
+
+const item_def* ST_ItemIterator::operator->() const
+{
+ return (m_item);
+}
+
+const level_id &ST_ItemIterator::place()
+{
+ return (m_place);
+}
+
+const ShopInfo* ST_ItemIterator::shop()
+{
+ return (m_shop);
+}
+
+const unsigned ST_ItemIterator::price()
+{
+ return (m_price);
+}
+
+const ST_ItemIterator& ST_ItemIterator::operator ++ ()
+{
+ m_item = NULL;
+ m_shop = NULL;
+
+ const LevelStashes &ls = m_stash_level_it->second;
+
+ if (m_stash_it == ls.m_stashes.end())
+ {
+ if (m_shop_it == ls.m_shops.end())
+ {
+ m_stash_level_it++;
+ if (m_stash_level_it == StashTrack.levels.end())
+ return (*this);
+
+ new_level();
+ return (*this);
+ }
+ m_shop = &(*m_shop_it);
+
+ if (m_shop_item_it != m_shop->items.end())
+ {
+ const ShopInfo::shop_item &item = *m_shop_item_it++;
+ m_item = &(item.item);
+ ASSERT(m_item->is_valid());
+ m_price = item.price;
+ return (*this);
+ }
+
+ m_shop_it++;
+ if (m_shop_it != ls.m_shops.end())
+ m_shop_item_it = m_shop_it->items.begin();
+
+ ++(*this);
+ }
+ else
+ {
+ if (m_stash_item_it != m_stash_it->second.items.end())
+ {
+ m_item = &(*m_stash_item_it++);
+ ASSERT(m_item->is_valid());
+ return (*this);
+ }
+
+ m_stash_it++;
+ if (m_stash_it == ls.m_stashes.end())
+ {
+ ++(*this);
+ return (*this);
+ }
+
+ m_stash_item_it = m_stash_it->second.items.begin();
+ ++(*this);
+ }
+
+ return (*this);
+}
+
+void ST_ItemIterator::new_level()
+{
+ m_item = NULL;
+ m_shop = NULL;
+ m_price = 0;
+
+ if (m_stash_level_it == StashTrack.levels.end())
+ return;
+
+ const LevelStashes &ls = m_stash_level_it->second;
+
+ m_place = ls.m_place;
+
+ m_stash_it = ls.m_stashes.begin();
+ if (m_stash_it != ls.m_stashes.end())
+ {
+ m_stash_item_it = m_stash_it->second.items.begin();
+ if (m_stash_item_it != m_stash_it->second.items.end())
+ {
+ m_item = &(*m_stash_item_it++);
+ ASSERT(m_item->is_valid());
+ }
+ }
+
+ m_shop_it = ls.m_shops.begin();
+ if (m_shop_it != ls.m_shops.end())
+ {
+ const ShopInfo &si = *m_shop_it;
+
+ m_shop_item_it = si.items.begin();
+
+ if (m_item == NULL && m_shop_item_it != si.items.end())
+ {
+ const ShopInfo::shop_item &item = *m_shop_item_it++;
+ m_item = &(item.item);
+ ASSERT(m_item->is_valid());
+ m_price = item.price;
+ m_shop = &si;
+ }
+ }
+}
+
+ST_ItemIterator ST_ItemIterator::operator ++ (int dummy)
+{
+ const ST_ItemIterator copy = *this;
+ ++(*this);
+ return (copy);
+}
diff --git a/crawl-ref/source/stash.h b/crawl-ref/source/stash.h
index e35ce2b591..abb05d13d8 100644
--- a/crawl-ref/source/stash.h
+++ b/crawl-ref/source/stash.h
@@ -114,6 +114,7 @@ private:
static bool is_filtered(const item_def &item);
friend class LevelStashes;
+ friend class ST_ItemIterator;
};
class ShopInfo
@@ -164,6 +165,8 @@ private:
std::string shop_item_name(const shop_item &si) const;
std::string shop_item_desc(const shop_item &si) const;
void describe_shop_item(const shop_item &si) const;
+
+ friend class ST_ItemIterator;
};
struct stash_search_result
@@ -273,6 +276,7 @@ public:
shops_t m_shops;
friend class StashTracker;
+ friend class ST_ItemIterator;
};
extern std::ostream &operator << (std::ostream &, const LevelStashes &);
@@ -352,6 +356,41 @@ private:
stash_levels_t levels;
double last_corpse_update;
+
+ friend class ST_ItemIterator;
+};
+
+class ST_ItemIterator
+{
+public:
+ ST_ItemIterator();
+
+ const ST_ItemIterator& operator ++ ();
+ ST_ItemIterator operator ++ (int);
+
+ operator bool() const;
+ const item_def& operator *() const;
+ const item_def* operator->() const;
+
+ const level_id &place();
+ const ShopInfo* shop();
+ const unsigned price();
+
+private:
+ level_id m_place;
+ const item_def* m_item;
+ unsigned m_price;
+ const ShopInfo* m_shop;
+
+ StashTracker::stash_levels_t::const_iterator m_stash_level_it;
+ LevelStashes::stashes_t::const_iterator m_stash_it;
+ LevelStashes::shops_t::const_iterator m_shop_it;
+ std::vector<item_def>::const_iterator m_stash_item_it;
+
+ std::vector<ShopInfo::shop_item>::const_iterator m_shop_item_it;
+
+private:
+ void new_level();
};
extern StashTracker StashTrack;