summaryrefslogtreecommitdiffstats
path: root/stone_soup/crawl-ref/source/travel.h
blob: e4478af0fb139302030a55b6ee98d7e8395e3997 (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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
 *  File:       travel.cc
 *  Summary:    Travel stuff
 *  Written by: Darshan Shaligram
 *
 *  Change History (most recent first):
 *
 *     <1>     -/--/--     SD     Created
 */
#ifndef TRAVEL_H
#   define TRAVEL_H

#   include "externs.h"
#   include <stdio.h>
#   include <string>
#   include <vector>
#   include <map>

/* ***********************************************************************
 * Initialises the travel subsystem.
 *
 * ***********************************************************************
 * called from: initfile (what's a better place to initialise stuff?)
 * *********************************************************************** */
void initialise_travel();
void stop_running(void);
void travel_init_new_level();
void toggle_exclude(int x, int y);
void clear_excludes();
unsigned char is_waypoint(int x, int y);
void update_excludes();
bool is_stair(unsigned gridc);
bool is_travelable_stair(unsigned gridc);
int stair_direction(int stair_feat);
bool is_player_mapped(unsigned char envch);
bool is_resting( void );
bool can_travel_interlevel();

inline bool is_player_mapped(int grid_x, int grid_y)
{
    return (is_player_mapped( env.map[grid_x - 1][grid_y - 1] ));
}

void find_travel_pos(int you_x, int you_y, char *move_x, char *move_y, 
                     std::vector<coord_def>* coords = NULL);

/* ***********************************************************************
 * Initiates explore - the character runs around the level to map it. Note
 * that the caller has to ensure that the level is mappable before calling
 * start_explore. start_explore may lock up the game on unmappable levels.
 *
 * ***********************************************************************
 * called from: acr
 * *********************************************************************** */
void start_explore();

struct level_pos;
void start_translevel_travel(const level_pos &pos);

void start_translevel_travel(bool prompt_for_destination = true);

void start_travel(int x, int y);

void travel(int *keyin, char *move_x, char *move_y);

int travel_direction(unsigned char branch, int subdungeondepth);

void prevent_travel_to(const std::string &dungeon_feature_name);

int subdungeon_depth(unsigned char branch, int depth);

int absdungeon_depth(unsigned char branch, int subdepth);

// Sort dungeon features as appropriate.
void arrange_features(std::vector<coord_def> &features);

// Magic numbers for point_distance:

// This square is a trap
#define PD_TRAP             -42

// The user never wants to travel this square
#define PD_EXCLUDED         -20099

// This square is within LOS radius of an excluded square
#define PD_EXCLUDED_RADIUS  -20100

// This square is a waypoint
#define PD_WAYPOINT         -20200

/* ***********************************************************************
 * Array of points on the map, each value being the distance the character
 * would have to travel to get there. Negative distances imply that the point
 * is a) a trap or hostile terrain or b) only reachable by crossing a trap or
 * hostile terrain.
 * ***********************************************************************
 * referenced in: travel - view
 * *********************************************************************** */
extern short point_distance[GXM][GYM];

// Possible values of you.running
enum RUN_MODES
{
    RUN_TRAVEL      = -1,   // Classic or Plain Old travel
    RUN_EXPLORE     = -2,   // Exploring (Ctrl+O)
    RUN_INTERLEVEL  = -3    // Interlevel travel (Ctrl+G)
};

enum EXPLORE_STOP
{
    ES_NONE     = 0,
    ES_ITEM     = 1,
    ES_STAIR    = 2,
    ES_SHOP     = 4,
    ES_ALTAR    = 8
};

////////////////////////////////////////////////////////////////////////////
// Structs for interlevel travel.

struct level_id
{
    unsigned char branch;   // The branch in which the level is.
    int           depth;    // What depth (in this branch - starting from 1)
                            // this level is.

    level_id() : branch(0), depth(-1) { }

    level_id(unsigned char br, int dep) : branch(br), depth(dep) { }

    // Returns the level_id of the current level.
    static level_id get_current_level_id();

    // Returns the level_id of the level that the stair/portal/whatever at
    // 'pos' on the current level leads to.
    static level_id get_next_level_id(const coord_def &pos);

    bool operator == ( const level_id &id ) const
    {
        return branch == id.branch && depth == id.depth;
    }

    bool operator != ( const level_id &id ) const
    {
        return branch != id.branch || depth != id.depth;
    }

    struct less_than
    {
        bool operator () (const level_id &first, const level_id &second) const
        {
            return first.branch < second.branch ||
                (first.branch == second.branch && first.depth < second.depth);
        }
    };

    void save(FILE *) const;
    void load(FILE *);
};

// A position on a particular level.
struct level_pos
{
    level_id      id;
    coord_def     pos;      // The grid coordinates on this level.

    level_pos() : id(), pos()
    {
        pos.x = pos.y = -1;
    }

    level_pos(const level_id &lid, const coord_def &coord) 
        : id(lid), pos(coord)
    {
    }

    level_pos(const level_id &lid)
        : id(lid), pos()
    {
        pos.x = pos.y = -1;
    }

    bool operator == ( const level_pos &lp ) const
    {
        return id == lp.id && pos == lp.pos;
    }

    bool operator != ( const level_pos &lp ) const
    {
        return id != lp.id || pos != lp.pos;
    }
    
    bool is_valid() const
    {
        return id.depth > -1 && pos.x != -1 && pos.y != -1;
    }

    void save(FILE *) const;
    void load(FILE *);
};

struct stair_info
{
    coord_def position;     // Position of stair

    level_pos destination;  // The level and the position on the level this
                            // stair leads to. This may be a guess.

    int       distance;     // The distance traveled to reach this stair.

    bool      guessed_pos;  // true if we're not sure that 'destination' is
                            // correct.

    stair_info() : destination(), distance(-1), guessed_pos(true)
    {
        position.x = position.y = -1;
    }

    void reset_distance()
    {
        distance = -1;
    }

    void save(FILE *) const;
    void load(FILE *);
};

// Information on a level that interlevel travel needs.
struct LevelInfo
{
    LevelInfo() : stairs()
    {
        stair_distances = NULL;
    }
    LevelInfo(const LevelInfo &li);

    ~LevelInfo();

    const LevelInfo &operator = (const LevelInfo &other);

    void save(FILE *) const;
    void load(FILE *);

    std::vector<stair_info> &get_stairs()
    {
        return stairs;
    }

    stair_info *get_stair(int x, int y);
    stair_info *get_stair(const coord_def &pos);
    int get_stair_index(const coord_def &pos) const;

    void reset_distances();
    void set_level_excludes();

    // Returns the travel distance between two stairs. If either stair is NULL,
    // or does not exist in our list of stairs, returns 0.
    int distance_between(const stair_info *s1, const stair_info *s2) const;

    void update();              // Update LevelInfo to be correct for the
                                // current level.

    // Updates/creates a StairInfo for the stair at (x, y) in grid coordinates
    void update_stair(int x, int y, const level_pos &p, bool guess = false);

    // Returns true if the given branch is known to be accessible from the 
    // current level.
    bool is_known_branch(unsigned char branch) const;

    void add_waypoint(const coord_def &pos);
    void remove_waypoint(const coord_def &pos);

    void travel_to_waypoint(const coord_def &pos);
private:
    // Gets a list of coordinates of all player-known stairs on the current
    // level.
    static void get_stairs(std::vector<coord_def> &stairs);

    void correct_stair_list(const std::vector<coord_def> &s);
    void update_stair_distances();
    void fixup();

private:
    std::vector<stair_info> stairs;

    // Squares that are not safe to travel to.
    std::vector<coord_def> excludes;

    short *stair_distances;       // Distances between the various stairs
    level_id id;

    friend class TravelCache;
};

#define TRAVEL_WAYPOINT_COUNT 10
// Tracks all levels that the player has seen.
class TravelCache
{
public:
    void reset_distances();

    // Get the LevelInfo for the specified level (defaults to the current
    // level).
    LevelInfo& get_level_info(unsigned char branch = 0, int depth = -1)
    {
        return get_level_info( level_id(branch, depth) );
    }

    LevelInfo& get_level_info(const level_id &lev)
    {
        LevelInfo &li = levels[lev];
        li.id = lev;
        return li;
    }

    bool know_level(const level_id &lev) const
    {
        return levels.find(lev) != levels.end();
    }

    const level_pos &get_waypoint(int number) const
    {
        return waypoints[number]; 
    }

    int get_waypoint_count() const;

    void set_level_excludes();

    void add_waypoint(int x = -1, int y = -1);
    unsigned char is_waypoint(const level_pos &lp) const;
    void list_waypoints() const;
    void travel_to_waypoint(int number);
    void update_waypoints() const;


    void update();

    void save(FILE *) const;
    void load(FILE *);

    bool is_known_branch(unsigned char branch) const;

private:
    void fixup_levels();

private:
    std::map<level_id, LevelInfo, level_id::less_than> levels;
    level_pos waypoints[TRAVEL_WAYPOINT_COUNT];
};

int level_distance(level_id first, level_id second);

extern TravelCache travel_cache;

#endif // TRAVEL_H