Apply the new ground_level method.
[crawl.git] / crawl-ref / source / tilereg-mon.cc
blob5654b3f085d3685307911afebcea1a96f0e1c1c2
1 /*
2 * File: tilereg-mon.cc
3 */
5 #include "AppHdr.h"
7 #ifdef USE_TILE
8 #include <algorithm>
10 #include "tilereg-mon.h"
12 #include "cio.h"
13 #include "directn.h"
14 #include "env.h"
15 #include "libutil.h"
16 #include "monster.h"
17 #include "stuff.h"
18 #include "tiledef-dngn.h"
19 #include "tiledef-icons.h"
20 #include "tiledef-player.h"
21 #include "tilereg-dgn.h"
22 #include "tilepick.h"
23 #include "tileview.h"
24 #include "viewgeom.h"
26 MonsterRegion::MonsterRegion(const TileRegionInit &init) : GridRegion(init)
30 void MonsterRegion::update()
32 m_items.clear();
33 m_mon_info.clear();
34 m_dirty = true;
36 const size_t max_mons = mx * my;
38 if (max_mons == 0)
39 return;
41 get_monster_info(m_mon_info);
42 std::sort(m_mon_info.begin(), m_mon_info.end(),
43 monster_info::less_than_wrapper);
45 unsigned int num_mons = std::min(max_mons, m_mon_info.size());
46 for (size_t i = 0; i < num_mons; ++i)
48 InventoryTile desc;
49 desc.idx = i;
51 m_items.push_back(desc);
55 int MonsterRegion::handle_mouse(MouseEvent &event)
57 int cx, cy;
58 if (!mouse_pos(event.px, event.py, cx, cy))
60 place_cursor(NO_CURSOR);
61 return (0);
64 const coord_def cursor(cx, cy);
65 place_cursor(cursor);
67 if (mouse_control::current_mode() != MOUSE_MODE_COMMAND)
68 return (0);
71 unsigned int item_idx = cursor_index();
72 const monster* mon = get_monster(item_idx);
73 if (!mon)
74 return (0);
76 const coord_def &gc = mon->position;
77 tiles.place_cursor(CURSOR_MOUSE, gc);
79 if (event.event != MouseEvent::PRESS)
80 return (0);
82 if (event.button == MouseEvent::LEFT)
84 m_last_clicked_item = item_idx;
85 return (tile_click_cell(gc, event.mod));
87 else if (event.button == MouseEvent::RIGHT)
89 full_describe_square(gc);
90 redraw_screen();
91 return (CK_MOUSE_CMD);
94 return (0);
97 bool MonsterRegion::update_tip_text(std::string &tip)
99 if (m_cursor == NO_CURSOR)
100 return (false);
102 unsigned int item_idx = cursor_index();
103 const monster* mon = get_monster(item_idx);
104 if (!mon)
105 return (false);
107 return (tile_dungeon_tip(mon->position, tip));
110 bool MonsterRegion::update_tab_tip_text(std::string &tip, bool active)
112 return (false);
115 bool MonsterRegion::update_alt_text(std::string &alt)
117 if (m_cursor == NO_CURSOR)
118 return (false);
120 unsigned int item_idx = cursor_index();
121 if (m_last_clicked_item >= 0
122 && item_idx == (unsigned int) m_last_clicked_item)
124 return (false);
127 const monster* mon = get_monster(item_idx);
128 if (!mon)
129 return (false);
131 const coord_def &gc = mon->position;
133 describe_info inf;
134 if (!you.see_cell(gc))
135 return (false);
137 get_square_desc(gc, inf, true, false);
139 alt_desc_proc proc(crawl_view.msgsz.x, crawl_view.msgsz.y);
140 process_description<alt_desc_proc>(proc, inf);
142 proc.get_string(alt);
144 return (true);
147 const monster* MonsterRegion::get_monster(unsigned int idx) const
149 if (idx >= m_items.size())
150 return (NULL);
152 const InventoryTile &item = m_items[idx];
153 if (item.idx >= static_cast<int>(m_mon_info.size()))
154 return (NULL);
156 return (m_mon_info[item.idx].mon());
159 void MonsterRegion::pack_buffers()
161 update();
162 const int num_floor = tile_dngn_count(env.tile_default.floor);
164 unsigned int i = 0;
165 for (int y = 0; y < my; y++)
167 for (int x = 0; x < mx; x++)
169 bool cursor = (i < m_items.size()) ?
170 (m_items[i].flag & TILEI_FLAG_CURSOR) : false;
172 const monster* mon = get_monster(i++);
173 if (mon)
175 const coord_def gc = mon->position;
176 const coord_def ep = grid2show(gc);
178 if (crawl_view.in_los_bounds_g(gc))
180 packed_cell cell;
181 cell.fg = env.tile_fg(ep);
182 cell.bg = env.tile_bg(ep);
183 cell.flv = env.tile_flv(gc);
184 tile_apply_properties(gc, cell);
186 m_buf.add(cell, x, y);
188 if (cursor)
189 m_buf.add_icons_tile(TILEI_CURSOR, x, y);
190 continue;
194 // Fill the rest of the space with out of sight floor tiles.
195 int tileidx = env.tile_default.floor + m_flavour[i] % num_floor;
196 m_buf.add_dngn_tile(tileidx, x, y);
197 m_buf.add_icons_tile(TILEI_MESH, x, y);
202 void MonsterRegion::draw_tag()
204 if (m_cursor == NO_CURSOR)
205 return;
207 int curs_index = cursor_index();
208 if (curs_index >= (int)m_items.size())
209 return;
210 int idx = m_items[curs_index].idx;
211 if (idx == -1)
212 return;
214 const monster* mon = get_monster(idx);
215 if (!mon)
216 return;
218 std::string desc = mon->name(DESC_CAP_A);
219 draw_desc(desc.c_str());
222 void MonsterRegion::activate()
226 #endif