Apply the new ground_level method.
[crawl.git] / crawl-ref / source / coord.cc
blob0507ab67324af622227f011b233b87096bf0ad49
1 #include "AppHdr.h"
3 #include "coord.h"
5 #include "random.h"
6 #include "state.h"
7 #include "viewgeom.h"
9 //////////////////////////////////////////////////////////////////////////
10 // coord_def
11 int coord_def::distance_from(const coord_def &other) const
13 return (grid_distance(*this, other));
16 int grid_distance(const coord_def& p1, const coord_def& p2)
18 return ((p2 - p1).rdist());
21 int distance(const coord_def& p1, const coord_def& p2)
23 return ((p2 - p1).abs());
26 bool adjacent(const coord_def& p1, const coord_def& p2)
28 return grid_distance(p1, p2) <= 1;
31 bool in_bounds_x(int x)
33 return (x > X_BOUND_1 && x < X_BOUND_2);
36 bool in_bounds_y(int y)
38 return (y > Y_BOUND_1 && y < Y_BOUND_2);
41 // Returns true if inside the area the player can move and dig (ie exclusive).
42 bool in_bounds(int x, int y)
44 return (in_bounds_x(x) && in_bounds_y(y));
47 bool map_bounds_x(int x)
49 return (x >= X_BOUND_1 && x <= X_BOUND_2);
52 bool map_bounds_y(int y)
54 return (y >= Y_BOUND_1 && y <= Y_BOUND_2);
57 // Returns true if inside the area the player can map (ie inclusive).
58 // Note that terrain features should be in_bounds() leaving an outer
59 // ring of rock to frame the level.
60 bool map_bounds(int x, int y)
62 return (map_bounds_x(x) && map_bounds_y(y));
65 bool map_bounds_with_margin(coord_def p, int margin)
67 return (p.x >= X_BOUND_1 + margin && p.x <= X_BOUND_2 - margin
68 && p.y >= Y_BOUND_1 + margin && p.y <= Y_BOUND_2 - margin);
71 coord_def random_in_bounds()
73 if (crawl_state.game_is_arena())
75 const coord_def &ul = crawl_view.glos1; // Upper left
76 const coord_def &lr = crawl_view.glos2; // Lower right
78 return coord_def(random_range(ul.x, lr.x - 1),
79 random_range(ul.y, lr.y - 1));
81 else
82 return coord_def(random_range(MAPGEN_BORDER, GXM - MAPGEN_BORDER - 1),
83 random_range(MAPGEN_BORDER, GYM - MAPGEN_BORDER - 1));
86 // Coordinate system conversions.
88 coord_def player2grid(const coord_def &pc)
90 return (pc + you.pos());
93 coord_def grid2player(const coord_def &gc)
95 return (gc - you.pos());