1 /**
2  * @file
3  * @brief Monster cache support
4 **/
5 
6 #ifdef USE_TILE
7 #pragma once
8 
9 #include <vector>
10 
11 struct dolls_data;
12 
13 // The monster cache is designed to hold extra information about monsters that
14 // can't be contained in a single tile. This is usually for equipment,
15 // doll parts, or demon parts.
16 //
17 // Monster cache entries for monsters that are out of sight are ref-counted
18 // that they can be drawn even if that monster no longer exists. When no
19 // out-of-sight tiles refer to them, they can be deleted.
20 
21 class tile_draw_info
22 {
23 public:
tile_draw_info()24     tile_draw_info() : idx(~0), ofs_x(0), ofs_y(0) {}
25 
26     void set(tileidx_t _idx, int _ofs_x = 0, int _ofs_y = 0)
27         { idx = _idx; ofs_x = _ofs_x; ofs_y = _ofs_y; }
28 
29     tileidx_t idx;
30     int ofs_x;
31     int ofs_y;
32 };
33 
34 class mcache_entry
35 {
36 public:
mcache_entry()37     mcache_entry() : m_ref_count(0) {}
~mcache_entry()38     virtual ~mcache_entry() {}
39 
inc_ref()40     void inc_ref() { m_ref_count++; }
dec_ref()41     void dec_ref() { m_ref_count--; if (m_ref_count < 0) m_ref_count = 0; }
ref_count()42     int ref_count() { return m_ref_count; }
43 
44     enum
45     {
46         // The maximum number of values written in the info function.
47         // XXX: just use a vector?
48         MAX_INFO_COUNT = 9
49     };
50 
info(tile_draw_info *)51     virtual int info(tile_draw_info */*dinfo*/) const { return 0; }
doll()52     virtual const dolls_data *doll() const { return nullptr; }
53 
transparent()54     virtual bool transparent() const { return false; }
55 
56 protected:
57 
58     // ref count in backstore
59     int m_ref_count;
60 };
61 
62 class mcache_manager
63 {
64 public:
65     ~mcache_manager();
66 
67     unsigned int register_monster(const monster_info& mon);
68     mcache_entry *get(tileidx_t idx);
69 
70     void clear_nonref();
71     void clear_all();
72 
empty()73     bool empty() { return m_entries.empty(); }
74 
75 protected:
76     vector<mcache_entry*> m_entries;
77 };
78 
79 // The global monster cache.
80 extern mcache_manager mcache;
81 
82 #endif
83