Apply the new ground_level method.
[crawl.git] / crawl-ref / source / stash.h
blob1138305b315c8e7ebead6bc59fa5ce2ace5c9477
1 /*
2 * File: stash.h
3 * Summary: Classes tracking player stashes
4 * Written by: Darshan Shaligram
5 */
7 #ifndef STASH_H
8 #define STASH_H
10 #include "shopping.h"
11 #include <string>
13 #include <iostream>
14 #include <map>
15 #include <vector>
17 #include "externs.h"
18 #include "player.h"
20 class input_history;
21 class reader;
22 class writer;
23 class StashMenu;
25 // Stash definitions
26 void stash_init_new_level();
28 enum STASH_TRACK_MODES
30 STM_NONE, // Stashes are not tracked
31 STM_EXPLICIT, // Only explicitly marked stashes are tracked
32 STM_DROPPED, // Dropped items and explicitly marked stashes are
33 // tracked
34 STM_ALL, // All seen items are tracked
37 struct stash_search_result;
38 class Stash
40 public:
41 Stash(int xp = -1, int yp = -1);
43 static bool is_boring_feature(dungeon_feature_type feat);
44 static void filter(object_class_type base_type, uint8_t sub_type);
45 static void filter(const std::string &filt);
47 static std::string stash_item_name(const item_def &item);
48 void update();
49 void save(writer&) const;
50 void load(reader&);
52 std::string description() const;
53 std::string feature_description() const;
54 std::vector<item_def> get_items() const;
56 bool show_menu(const level_pos &place, bool can_travel) const;
58 // Returns true if this Stash contains items that are eligible for
59 // autopickup.
60 bool pickup_eligible() const;
62 // Returns true if this Stash is unverified (a visit by the character will
63 // verify the stash).
64 bool unverified() const;
66 bool matches_search(const std::string &prefix,
67 const base_pattern &search,
68 stash_search_result &res)
69 const;
71 void write(std::ostream &os, int refx = 0, int refy = 0,
72 std::string place = "",
73 bool identify = false) const;
75 bool empty() const
77 return enabled && items.empty() && feat == DNGN_FLOOR;
80 bool isAt(const coord_def& c) const { return c.x == x && c.y == y; }
81 int abs_pos() const { return abspos; }
82 int getX() const { return x; }
83 int getY() const { return y; }
85 bool is_verified() const { return verified; }
87 bool enabled; // If false, this Stash will neither track
88 // items nor dump itself. Disabled stashes are
89 // also never removed from the level's map of
90 // stashes.
92 public:
93 static bool is_filtered(const item_def &item);
95 private:
96 void _update_corpses(int rot_time);
97 void add_item(const item_def &item, bool add_to_front = false);
99 private:
100 bool verified; // Is this correct to the best of our knowledge?
101 uint8_t x, y;
102 int abspos;
103 dungeon_feature_type feat;
104 trap_type trap;
106 std::vector<item_def> items;
109 * If true (the default), the stash-tracker is a lot more likely to consider
110 * squares that the player is not standing on as correctly-verified. This
111 * will lead to cleaner dumps, but the chance of stashes not accurately
112 * reflecting what's in the dungeon also increases.
114 static bool aggressive_verify;
115 static std::vector<item_def> filters;
117 static bool are_items_same(const item_def &, const item_def &);
119 friend class LevelStashes;
120 friend class ST_ItemIterator;
123 class ShopInfo
125 public:
126 ShopInfo(int xp, int yp);
128 bool matches_search(const std::string &prefix,
129 const base_pattern &search,
130 stash_search_result &res)
131 const;
133 std::string description() const;
135 void save(writer&) const;
136 void load(reader&);
138 bool show_menu(const level_pos &place, bool can_travel) const;
139 bool is_visited() const { return items.size() || visited; }
141 void write(std::ostream &os, bool identify = false) const;
143 void reset() { items.clear(); visited = true; }
144 void set_name(const std::string& s) { name = s; }
146 void add_item(const item_def &item, unsigned price);
148 bool isAt(const coord_def& c) const { return x == c.x && y == c.y; }
150 // Messy!
151 struct shop_item
153 item_def item;
154 unsigned price;
157 private:
158 int x, y;
159 std::string name;
161 int shoptype;
163 // Set true if the player has visited this shop
164 bool visited;
166 std::vector<shop_item> items;
168 std::string shop_item_name(const shop_item &si) const;
169 std::string shop_item_desc(const shop_item &si) const;
170 void describe_shop_item(const shop_item &si) const;
171 void fill_out_menu(StashMenu &menu, const level_pos &place) const;
173 friend class ST_ItemIterator;
176 struct stash_search_result
178 // Where's this thingummy of interest.
179 level_pos pos;
181 // Number of levels the player must cross to reach the level the stash/shop
182 // is on.
183 int player_distance;
185 // Number of items in the stash that match the search.
186 int matches;
188 // Count of items stacks in the stash that matched. This will be the same as
189 // matches if each matching stack's quantity is 1.
190 int count;
192 // Text that describes this search result - usually the name of
193 // the first matching item in the stash or the name of the shop.
194 std::string match;
196 // The stash or shop in question. Both can be null if this is a feature.
197 const Stash *stash;
198 const ShopInfo *shop;
200 stash_search_result() : pos(), player_distance(0), matches(0),
201 count(0), match(), stash(NULL), shop(NULL)
205 bool operator < (const stash_search_result &ssr) const
207 return (player_distance < ssr.player_distance
208 || (player_distance == ssr.player_distance
209 && matches > ssr.matches));
213 extern std::ostream &operator << (std::ostream &, const Stash &);
215 class LevelStashes
217 public:
218 LevelStashes();
220 const Stash *find_stash(coord_def c) const;
221 Stash *find_stash(coord_def c);
222 const ShopInfo *find_shop(const coord_def& c) const;
223 ShopInfo &get_shop(const coord_def& c);
225 level_id where() const;
227 void get_matching_stashes(const base_pattern &search,
228 std::vector<stash_search_result> &results) const;
230 // Update stash at (x,y) on current level, defaulting to player's current
231 // location if no parameters are supplied.
232 bool update_stash(const coord_def& c);
234 // Returns true if the square at c contains potentially interesting
235 // swag that merits a personal visit (for EXPLORE_GREEDY).
236 bool needs_visit(const coord_def& c) const;
237 bool shop_needs_visit(const coord_def& c) const;
239 // Returns true if the items at c are not fully known to the stash-tracker
240 // and can be updated if the character steps on the square.
241 bool unverified_stash(const coord_def &c) const;
243 // Add stash at (x,y), or player's current location if no parameters are
244 // supplied
245 void add_stash(int x = -1, int y = -1);
247 // Mark square (x,y) as not stashworthy. The player's current location is
248 // used if no parameters are supplied.
249 void no_stash(int x = -1, int y = -1);
251 void kill_stash(const Stash &s);
253 void save(writer&) const;
254 void load(reader&);
256 void write(std::ostream &os, bool identify = false) const;
257 std::string level_name() const;
258 std::string short_level_name() const;
260 int stash_count() const { return m_stashes.size() + m_shops.size(); }
261 int visible_stash_count() const { return _num_enabled_stashes() + m_shops.size(); }
263 bool is_current() const;
265 void remove_shop(const coord_def& c);
266 private:
267 int _num_enabled_stashes() const;
268 void _update_corpses(int rot_time);
270 private:
271 typedef std::map<int, Stash> stashes_t;
272 typedef std::vector<ShopInfo> shops_t;
274 // which level
275 level_id m_place;
276 stashes_t m_stashes;
277 shops_t m_shops;
279 friend class StashTracker;
280 friend class ST_ItemIterator;
283 extern std::ostream &operator << (std::ostream &, const LevelStashes &);
285 class StashTracker
287 public:
288 static bool is_level_untrackable()
290 return you.level_type == LEVEL_LABYRINTH
291 || you.level_type == LEVEL_ABYSS;
294 StashTracker() : levels(), last_corpse_update(0)
298 void search_stashes();
300 LevelStashes &get_current_level();
301 LevelStashes *find_current_level();
302 LevelStashes *find_level(const level_id &pos);
304 ShopInfo &get_shop(const coord_def& c)
306 return get_current_level().get_shop(c);
309 void remove_level(const level_id &which = level_id::current());
311 enum stash_update_mode
313 ST_PASSIVE, // Maintain existing stashes only.
314 ST_AGGRESSIVE, // Create stashes for each square containing
315 // objects, even if those squares were not
316 // previously marked as stashes.
319 void update_corpses();
321 void update_visible_stashes(StashTracker::stash_update_mode = ST_PASSIVE);
323 // Update stash at (x,y) on current level, defaulting to player's current
324 // location if no parameters are supplied, return true if a stash was
325 // updated.
326 bool update_stash(const coord_def& c);
328 // Add stash at (x,y), or player's current location if no parameters are
329 // supplied.
330 void add_stash(int x = -1, int y = -1, bool verbose = false);
332 // Mark square (x,y) as not stashworthy. The player's current location is
333 // used if no parameters are supplied.
334 void no_stash(int x = -1, int y = -1);
336 void save(writer&) const;
337 void load(reader&);
339 void write(std::ostream &os, bool identify = false) const;
341 void dump(const char *filename, bool identify = false) const;
343 void remove_shop(const coord_def& c);
344 private:
345 void get_matching_stashes(const base_pattern &search,
346 std::vector<stash_search_result> &results) const;
347 bool display_search_results(std::vector<stash_search_result> &results,
348 const char* sort_style);
349 std::string stash_search_prompt();
351 private:
352 typedef std::map<level_id, LevelStashes> stash_levels_t;
353 stash_levels_t levels;
355 int last_corpse_update;
357 friend class ST_ItemIterator;
360 class ST_ItemIterator
362 public:
363 ST_ItemIterator();
365 const ST_ItemIterator& operator ++ ();
366 ST_ItemIterator operator ++ (int);
368 operator bool() const;
369 const item_def& operator *() const;
370 const item_def* operator->() const;
372 const level_id &place();
373 const ShopInfo* shop();
374 unsigned price();
376 private:
377 level_id m_place;
378 const item_def* m_item;
379 unsigned m_price;
380 const ShopInfo* m_shop;
382 StashTracker::stash_levels_t::const_iterator m_stash_level_it;
383 LevelStashes::stashes_t::const_iterator m_stash_it;
384 LevelStashes::shops_t::const_iterator m_shop_it;
385 std::vector<item_def>::const_iterator m_stash_item_it;
387 std::vector<ShopInfo::shop_item>::const_iterator m_shop_item_it;
389 private:
390 void new_level();
393 extern StashTracker StashTrack;
395 void maybe_update_stashes();
396 bool is_stash(const coord_def& c);
397 std::string get_stash_desc(const coord_def& c);
398 void describe_stash(const coord_def& c);
400 std::vector<item_def> item_list_in_stash(const coord_def& pos);
402 std::string userdef_annotate_item(const char *s, const item_def *item,
403 bool exclusive = false);
405 #define STASH_LUA_SEARCH_ANNOTATE "ch_stash_search_annotate_item"
406 #define STASH_LUA_DUMP_ANNOTATE "ch_stash_dump_annotate_item"
407 #define STASH_LUA_VIEW_ANNOTATE "ch_stash_view_annotate_item"
409 #endif