forked from crawl/crawl
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcoord.cc
100 lines (83 loc) · 2.47 KB
/
coord.cc
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
#include "AppHdr.h"
#include "coord.h"
#include "libutil.h"
#include "state.h"
#include "viewgeom.h"
const coord_def INVALID_COORD(-1, -1);
const coord_def NO_CURSOR(-1, -1);
//////////////////////////////////////////////////////////////////////////
// coord_def
int coord_def::distance_from(const coord_def &other) const
{
return grid_distance(*this, other);
}
int grid_distance(const coord_def& p1, const coord_def& p2)
{
return (p2 - p1).rdist();
}
int distance2(const coord_def& p1, const coord_def& p2)
{
return (p2 - p1).abs();
}
bool adjacent(const coord_def& p1, const coord_def& p2)
{
return grid_distance(p1, p2) <= 1;
}
bool map_bounds_with_margin(coord_def p, int margin)
{
return p.x >= X_BOUND_1 + margin && p.x <= X_BOUND_2 - margin
&& p.y >= Y_BOUND_1 + margin && p.y <= Y_BOUND_2 - margin;
}
coord_def clip(const coord_def &p)
{
if (crawl_state.game_is_arena())
{
const coord_def &ul = crawl_view.glos1; // Upper left
const coord_def &lr = crawl_view.glos2; // Lower right
int x = p.x % (lr.x - ul.x + 1);
int y = p.y % (lr.y - ul.y + 1);
return coord_def(x, y);
}
int x = abs(p.x) % (GXM - 1) + MAPGEN_BORDER - 1;
int y = abs(p.y) % (GYM - 1) + MAPGEN_BORDER - 1;
return coord_def(x, y);
}
coord_def random_in_bounds()
{
if (crawl_state.game_is_arena())
{
const coord_def &ul = crawl_view.glos1; // Upper left
const coord_def &lr = crawl_view.glos2; // Lower right
return coord_def(random_range(ul.x, lr.x - 1),
random_range(ul.y, lr.y - 1));
}
else
return coord_def(random_range(MAPGEN_BORDER, GXM - MAPGEN_BORDER - 1),
random_range(MAPGEN_BORDER, GYM - MAPGEN_BORDER - 1));
}
// Coordinate system conversions.
coord_def player2grid(const coord_def &pc)
{
return pc + you.pos();
}
coord_def grid2player(const coord_def &gc)
{
return gc - you.pos();
}
//rotates a coord_def that points to an adjacent square
//clockwise (direction > 0), or counter-clockwise (direction < 0)
coord_def rotate_adjacent(coord_def vector, int direction)
{
int xn, yn;
xn = vector.x - sgn(direction) * vector.y;
yn = sgn(direction) * vector.x + vector.y;
vector.x = xn;
vector.y = yn;
return vector.sgn();
}
coord_def clamp_in_bounds(const coord_def &p)
{
return coord_def(
min(X_BOUND_2 - 1, max(X_BOUND_1 + 1, p.x)),
min(Y_BOUND_2 - 1, max(Y_BOUND_1 + 1, p.y)));
}