File: target-compass.cc

package info (click to toggle)
crawl 2%3A0.34.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 100,188 kB
  • sloc: cpp: 363,709; ansic: 27,765; javascript: 9,516; python: 8,463; perl: 3,293; java: 3,132; xml: 2,380; makefile: 1,835; sh: 611; objc: 250; cs: 15; sed: 9; lisp: 3
file content (120 lines) | stat: -rw-r--r-- 2,939 bytes parent folder | download | duplicates (4)
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
/**
 * @file
 * @brief Mini UI for choosing a compass direction around the player.
**/

#include "AppHdr.h"

#include "coord.h"
#include "directn.h"
#include "libutil.h"
#include "macro.h"
#include "message.h"
#include "state.h"
#include "target-compass.h"
#ifdef USE_TILE_LOCAL
# include "tilesdl.h"
#endif
#ifdef USE_TILE_WEB
# include "tileweb.h"
#endif

static int _targeting_cmd_to_compass(command_type command)
{
    switch (command)
    {
    case CMD_TARGET_UP:         case CMD_TARGET_DIR_UP:
        return 0;
    case CMD_TARGET_UP_RIGHT:   case CMD_TARGET_DIR_UP_RIGHT:
        return 1;
    case CMD_TARGET_RIGHT:      case CMD_TARGET_DIR_RIGHT:
        return 2;
    case CMD_TARGET_DOWN_RIGHT: case CMD_TARGET_DIR_DOWN_RIGHT:
        return 3;
    case CMD_TARGET_DOWN:       case CMD_TARGET_DIR_DOWN:
        return 4;
    case CMD_TARGET_DOWN_LEFT:  case CMD_TARGET_DIR_DOWN_LEFT:
        return 5;
    case CMD_TARGET_LEFT:       case CMD_TARGET_DIR_LEFT:
        return 6;
    case CMD_TARGET_UP_LEFT:    case CMD_TARGET_DIR_UP_LEFT:
        return 7;
    default:
        return -1;
    }
}

static int targeting_behaviour_get_key()
{
    if (!crawl_state.is_replaying_keys())
        flush_input_buffer(FLUSH_BEFORE_COMMAND);

    flush_prev_message();
    msgwin_got_input();
    return unmangle_direction_keys(getchm(KMC_TARGETING), KMC_TARGETING,
                                   false);
}

coord_def prompt_compass_direction()
{
    mprf(MSGCH_PROMPT, "Which direction?");

    coord_def delta = {0, 0};
    bool cancel = false;

    mouse_control mc(MOUSE_MODE_TARGET_DIR);

    do
    {
        const auto key = targeting_behaviour_get_key();
        const auto cmd = key_to_command(key, KMC_TARGETING);

        if (crawl_state.seen_hups)
        {
            mprf(MSGCH_ERROR, "Targeting interrupted by HUP signal.");
            return {0, 0};
        }

#ifdef USE_TILE
        if (cmd == CMD_TARGET_MOUSE_MOVE)
            continue;
        else if (cmd == CMD_TARGET_MOUSE_SELECT)
        {
            const coord_def &gc = tiles.get_cursor();
            if (gc == NO_CURSOR)
                continue;

            if (!map_bounds(gc))
                continue;

            delta = gc - you.pos();
            if (delta.rdist() > 1)
            {
                tiles.place_cursor(CURSOR_MOUSE, gc);
                delta = tiles.get_cursor() - you.pos();
                ASSERT(delta.rdist() <= 1);
            }
            break;
        }
#endif

        if (cmd == CMD_TARGET_SELECT)
        {
            delta.reset();
            break;
        }

        const int i = _targeting_cmd_to_compass(cmd);
        if (i != -1)
            delta = Compass[i];
        else if (cmd == CMD_TARGET_CANCEL)
            cancel = true;
    }
    while (!cancel && delta.origin());

#ifdef USE_TILE
    tiles.place_cursor(CURSOR_MOUSE, NO_CURSOR);
#endif

    return cancel ? coord_def(0, 0) : delta;
}