summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/tilereg-map.cc
blob: cd34f73bff839039d38261ad32e9c7db685b6146 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include "AppHdr.h"

#ifdef USE_TILE_LOCAL

#include "tilereg-map.h"

#include "cio.h"
#include "command.h"
#include "food.h"
#include "libutil.h"
#include "misc.h"
#include "options.h"
#include "tilesdl.h"
#include "travel.h"
#include "viewgeom.h"

MapRegion::MapRegion(int pixsz) :
    m_buf(NULL),
    m_dirty(true),
    m_far_view(false)
{
    ASSERT(pixsz > 0);

    dx = pixsz;
    dy = pixsz;
    clear();
    init_colours();
}

void MapRegion::on_resize()
{
    delete[] m_buf;

    int size = mx * my;
    m_buf    = new unsigned char[size];
    memset(m_buf, 0, sizeof(unsigned char) * size);
}

void MapRegion::init_colours()
{
    // TODO enne - the options array for colours should be
    // tied to the map feature enumeration to avoid this function.
    m_colours[MF_UNSEEN]        = Options.tile_unseen_col;
    m_colours[MF_FLOOR]         = Options.tile_floor_col;
    m_colours[MF_WALL]          = Options.tile_wall_col;
    m_colours[MF_MAP_FLOOR]     = Options.tile_mapped_floor_col;
    m_colours[MF_MAP_WALL]      = Options.tile_mapped_wall_col;
    m_colours[MF_DOOR]          = Options.tile_door_col;
    m_colours[MF_ITEM]          = Options.tile_item_col;
    m_colours[MF_MONS_FRIENDLY] = Options.tile_friendly_col;
    m_colours[MF_MONS_PEACEFUL] = Options.tile_peaceful_col;
    m_colours[MF_MONS_NEUTRAL]  = Options.tile_neutral_col;
    m_colours[MF_MONS_HOSTILE]  = Options.tile_monster_col;
    m_colours[MF_MONS_NO_EXP]   = Options.tile_plant_col;
    m_colours[MF_STAIR_UP]      = Options.tile_upstairs_col;
    m_colours[MF_STAIR_DOWN]    = Options.tile_downstairs_col;
    m_colours[MF_STAIR_BRANCH]  = Options.tile_branchstairs_col;
    m_colours[MF_PORTAL]        = Options.tile_portal_col;
    m_colours[MF_FEATURE]       = Options.tile_feature_col;
    m_colours[MF_WATER]         = Options.tile_water_col;
    m_colours[MF_DEEP_WATER]    = Options.tile_deep_water_col;
    m_colours[MF_LAVA]          = Options.tile_lava_col;
    m_colours[MF_TRAP]          = Options.tile_trap_col;
    m_colours[MF_EXCL_ROOT]     = Options.tile_excl_centre_col;
    m_colours[MF_EXCL]          = Options.tile_excluded_col;
    m_colours[MF_PLAYER]        = Options.tile_player_col;
}

MapRegion::~MapRegion()
{
    delete[] m_buf;
}

void MapRegion::pack_buffers()
{
    m_buf_map.clear();
    m_buf_lines.clear();

    for (int x = m_min_gx; x <= m_max_gx; x++)
        for (int y = m_min_gy; y <= m_max_gy; y++)
        {
            map_feature f = (map_feature)m_buf[x + y * mx];

            float pos_x = x - m_min_gx;
            float pos_y = y - m_min_gy;
            m_buf_map.add(pos_x, pos_y, pos_x + 1, pos_y + 1, m_colours[f]);
        }

    // Draw window box.
    if (m_win_start.x == -1 && m_win_end.x == -1)
        return;

    float pos_sx = (m_win_start.x - m_min_gx);
    float pos_sy = (m_win_start.y - m_min_gy);
    float pos_ex = (m_win_end.x - m_min_gx) - 1 / (float)dx;
    float pos_ey = (m_win_end.y - m_min_gy) - 1 / (float)dy;

    m_buf_lines.add_square(pos_sx, pos_sy, pos_ex, pos_ey,
                           Options.tile_window_col);
}

void MapRegion::render()
{
    if (m_min_gx > m_max_gx || m_min_gy > m_max_gy)
        return;

#ifdef DEBUG_TILES_REDRAW
    cprintf("rendering MapRegion\n");
#endif
    if (m_dirty)
    {
        pack_buffers();
        m_dirty = false;
    }

    set_transform();
    m_buf_map.draw();
    m_buf_lines.draw();
}

void MapRegion::recenter()
{
    // adjust offsets to center map.
    ox = (wx - dx * (m_max_gx - m_min_gx)) / 2;
    oy = (wy - dy * (m_max_gy - m_min_gy)) / 2;
}

void MapRegion::set(const coord_def &gc, map_feature f)
{
    ASSERT((unsigned int)f <= (unsigned char)~0);
    m_buf[gc.x + gc.y * mx] = f;

    if (f == MF_UNSEEN)
        return;

    // Get map extents
    m_min_gx = min(m_min_gx, gc.x);
    m_max_gx = max(m_max_gx, gc.x);
    m_min_gy = min(m_min_gy, gc.y);
    m_max_gy = max(m_max_gy, gc.y);

    recenter();
}

void MapRegion::update_bounds()
{
    int min_gx = m_min_gx;
    int max_gx = m_max_gx;
    int min_gy = m_min_gy;
    int max_gy = m_max_gy;

    m_min_gx = GXM;
    m_max_gx = 0;
    m_min_gy = GYM;
    m_max_gy = 0;

    for (int x = min_gx; x <= max_gx; x++)
        for (int y = min_gy; y <= max_gy; y++)
        {
            map_feature f = (map_feature)m_buf[x + y * mx];
            if (f == MF_UNSEEN)
                continue;

            m_min_gx = min(m_min_gx, x);
            m_max_gx = max(m_max_gx, x);
            m_min_gy = min(m_min_gy, y);
            m_max_gy = max(m_max_gy, y);
        }

    recenter();
#if 0
    // Not needed? (jpeg)
    m_dirty = true;
#endif
}

void MapRegion::set_window(const coord_def &start, const coord_def &end)
{
    m_win_start = start;
    m_win_end   = end;

    m_dirty = true;
}

void MapRegion::clear()
{
    m_min_gx = GXM;
    m_max_gx = 0;
    m_min_gy = GYM;
    m_max_gy = 0;

    m_win_start.x = -1;
    m_win_start.y = -1;
    m_win_end.x = -1;
    m_win_end.y = -1;

    recenter();

    if (m_buf)
        memset(m_buf, 0, sizeof(*m_buf) * mx * my);

    m_buf_map.clear();
    m_buf_lines.clear();
}

int MapRegion::handle_mouse(MouseEvent &event)
{
    if (mouse_control::current_mode() != MOUSE_MODE_COMMAND
        && !tiles.get_map_display())
    {
        return 0;
    }

    if (!inside(event.px, event.py))
    {
        if (m_far_view)
        {
            m_far_view = false;
            tiles.load_dungeon(crawl_view.vgrdc);
            return 0;
        }
        return 0;
    }

    int cx, cy;
    mouse_pos(event.px, event.py, cx, cy);
    const coord_def gc(m_min_gx + cx, m_min_gy + cy);

    tiles.place_cursor(CURSOR_MOUSE, gc);

    switch (event.event)
    {
    case MouseEvent::MOVE:
        if (m_far_view)
            tiles.load_dungeon(gc);
        return 0;
    case MouseEvent::PRESS:
#ifdef TOUCH_UI
        // ctrl-rolley-wheel on the minimap (this ensures busting out of minimap when zooming in again on very small layouts)
        if ( (event.mod & MOD_CTRL)
        && (event.button == MouseEvent::SCROLL_UP || event.button == MouseEvent::SCROLL_DOWN))
        {
            tiles.zoom_dungeon(event.button == MouseEvent::SCROLL_UP);
            return CK_NO_KEY; // prevents this being handled by the dungeon underneath too(!)
        }
        if (event.button == MouseEvent::LEFT)
        {
            m_far_view = true;
            tiles.load_dungeon(gc);
            if (!tiles.get_map_display())
            {
                process_command(CMD_DISPLAY_MAP);
                m_far_view = false;
                return CK_MOUSE_CMD;
            }
        }
        return 0;
    case MouseEvent::RELEASE:
        if (event.button == MouseEvent::LEFT && m_far_view)
            m_far_view = false;
        return 0;
#else
        if (event.button == MouseEvent::LEFT)
        {
            if (event.mod & MOD_SHIFT)
            {
                // Start autotravel, or give an appropriate message.
                do_explore_cmd();
                return CK_MOUSE_CMD;
            }
            else
            {
                const int cmd = click_travel(gc, event.mod & MOD_CTRL);
                if (cmd != CK_MOUSE_CMD)
                    process_command((command_type) cmd);

                return CK_MOUSE_CMD;
            }
        }
        else if (event.button == MouseEvent::RIGHT)
        {
            m_far_view = true;
            tiles.load_dungeon(gc);
            if (!tiles.get_map_display())
            {
                process_command(CMD_DISPLAY_MAP);
                m_far_view = false;
                return CK_MOUSE_CMD;
            }
        }
        return 0;
    case MouseEvent::RELEASE:
        if (event.button == MouseEvent::RIGHT && m_far_view)
            m_far_view = false;
        return 0;
#endif
    default:
        return 0;
    }
}

bool MapRegion::update_tip_text(string& tip)
{
    if (mouse_control::current_mode() != MOUSE_MODE_COMMAND)
        return false;

#ifdef TOUCH_UI
    tip = "[L-Click] Enable map mode";
#else
    tip = "[L-Click] Travel / [R-Click] View";
    if (!player_in_branch(BRANCH_LABYRINTH)
        && (you.hunger_state > HS_STARVING || you_min_hunger())
        && i_feel_safe())
    {
        tip += "\n[Shift + L-Click] Autoexplore";
    }
#endif
    return true;
}

#endif