forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
point.h
393 lines (338 loc) · 11.4 KB
/
point.h
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#pragma once
#ifndef CATA_SRC_POINT_H
#define CATA_SRC_POINT_H
// The CATA_NO_STL macro is used by the cata clang-tidy plugin tests so they
// can include this header when compiling with -nostdinc++
#ifndef CATA_NO_STL
#include <array>
#include <cassert>
#include <climits>
#include <cstdint>
#include <cstdlib>
#include <functional>
#include <ostream>
#include <string>
#include <vector>
#else
#define assert(...)
namespace std
{
class string;
class ostream;
}
#endif // CATA_NO_STL
class JsonIn;
class JsonOut;
// NOLINTNEXTLINE(cata-xy)
struct point {
static constexpr int dimension = 2;
int x = 0;
int y = 0;
constexpr point() = default;
constexpr point( int X, int Y ) : x( X ), y( Y ) {}
constexpr point operator+( point rhs ) const {
return point( x + rhs.x, y + rhs.y );
}
point &operator+=( point rhs ) {
x += rhs.x;
y += rhs.y;
return *this;
}
constexpr point operator-() const {
return point( -x, -y );
}
constexpr point operator-( point rhs ) const {
return point( x - rhs.x, y - rhs.y );
}
point &operator-=( point rhs ) {
x -= rhs.x;
y -= rhs.y;
return *this;
}
constexpr point operator*( const int rhs ) const {
return point( x * rhs, y * rhs );
}
friend constexpr point operator*( int lhs, point rhs ) {
return rhs * lhs;
}
point &operator*=( const int rhs ) {
x *= rhs;
y *= rhs;
return *this;
}
constexpr point operator/( const int rhs ) const {
return point( x / rhs, y / rhs );
}
#ifndef CATA_NO_STL
inline point abs() const {
return point( std::abs( x ), std::abs( y ) );
}
#endif
/**
* Rotate point clockwise @param turns times, 90 degrees per turn,
* around the center of a rectangle with the dimensions specified
* by @param dim
* By default rotates around the origin (0, 0).
* NOLINTNEXTLINE(cata-use-named-point-constants) */
point rotate( int turns, point dim = { 1, 1 } ) const;
std::string to_string() const;
void serialize( JsonOut &jsout ) const;
void deserialize( JsonIn &jsin );
friend inline constexpr bool operator<( point a, point b ) {
return a.x < b.x || ( a.x == b.x && a.y < b.y );
}
friend inline constexpr bool operator==( point a, point b ) {
return a.x == b.x && a.y == b.y;
}
friend inline constexpr bool operator!=( point a, point b ) {
return !( a == b );
}
friend std::ostream &operator<<( std::ostream &, point );
};
inline int divide_round_to_minus_infinity( int n, int d )
{
// The NOLINT comments here are to suppress a clang-tidy warning that seems
// to be a clang-tidy bug. I'd like to get rid of them if the bug is ever
// fixed. The warning comes via a project_remain call in
// mission_companion.cpp.
if( n >= 0 ) {
return n / d; // NOLINT(clang-analyzer-core.DivideZero)
}
return ( n - d + 1 ) / d; // NOLINT(clang-analyzer-core.DivideZero)
}
inline point multiply_xy( point p, int f )
{
return point( p.x * f, p.y * f );
}
inline point divide_xy_round_to_minus_infinity( point p, int d )
{
return point( divide_round_to_minus_infinity( p.x, d ),
divide_round_to_minus_infinity( p.y, d ) );
}
// NOLINTNEXTLINE(cata-xy)
struct tripoint {
static constexpr int dimension = 3;
int x = 0;
int y = 0;
int z = 0;
constexpr tripoint() = default;
constexpr tripoint( int X, int Y, int Z ) : x( X ), y( Y ), z( Z ) {}
constexpr tripoint( point p, int Z ) : x( p.x ), y( p.y ), z( Z ) {}
constexpr tripoint operator+( const tripoint &rhs ) const {
return tripoint( x + rhs.x, y + rhs.y, z + rhs.z );
}
constexpr tripoint operator-( const tripoint &rhs ) const {
return tripoint( x - rhs.x, y - rhs.y, z - rhs.z );
}
tripoint &operator+=( const tripoint &rhs ) {
x += rhs.x;
y += rhs.y;
z += rhs.z;
return *this;
}
constexpr tripoint operator-() const {
return tripoint( -x, -y, -z );
}
constexpr tripoint operator*( const int rhs ) const {
return tripoint( x * rhs, y * rhs, z * rhs );
}
friend constexpr tripoint operator*( int lhs, const tripoint &rhs ) {
return rhs * lhs;
}
tripoint &operator*=( const int rhs ) {
x *= rhs;
y *= rhs;
z *= rhs;
return *this;
}
constexpr tripoint operator/( const int rhs ) const {
return tripoint( x / rhs, y / rhs, z / rhs );
}
/*** some point operators and functions ***/
constexpr tripoint operator+( point rhs ) const {
return tripoint( x + rhs.x, y + rhs.y, z );
}
friend constexpr tripoint operator+( point lhs, const tripoint &rhs ) {
return rhs + lhs;
}
constexpr tripoint operator-( point rhs ) const {
return tripoint( x - rhs.x, y - rhs.y, z );
}
tripoint &operator+=( point rhs ) {
x += rhs.x;
y += rhs.y;
return *this;
}
tripoint &operator-=( point rhs ) {
x -= rhs.x;
y -= rhs.y;
return *this;
}
tripoint &operator-=( const tripoint &rhs ) {
x -= rhs.x;
y -= rhs.y;
z -= rhs.z;
return *this;
}
#ifndef CATA_NO_STL
inline tripoint abs() const {
return tripoint( std::abs( x ), std::abs( y ), std::abs( z ) );
}
#endif
constexpr point xy() const {
return point( x, y );
}
std::string to_string() const;
/**
* Rotate x and y components clockwise @param turns times,
* 90 degrees per turn, around the center of a rectangle with
* the dimensions specified by @param dim.
* By default rotates around the origin (0, 0).
* NOLINTNEXTLINE(cata-use-named-point-constants) */
inline tripoint rotate_2d( int turns, point dim = { 1, 1 } ) const {
return tripoint( xy().rotate( turns, dim ), z );
}
void serialize( JsonOut &jsout ) const;
void deserialize( JsonIn &jsin );
friend std::ostream &operator<<( std::ostream &, const tripoint & );
friend inline constexpr bool operator==( const tripoint &a, const tripoint &b ) {
return a.x == b.x && a.y == b.y && a.z == b.z;
}
friend inline constexpr bool operator!=( const tripoint &a, const tripoint &b ) {
return !( a == b );
}
friend inline bool operator<( const tripoint &a, const tripoint &b ) {
if( a.x != b.x ) {
return a.x < b.x;
}
if( a.y != b.y ) {
return a.y < b.y;
}
if( a.z != b.z ) {
return a.z < b.z;
}
return false;
}
};
inline tripoint multiply_xy( const tripoint &p, int f )
{
return tripoint( p.x * f, p.y * f, p.z );
}
inline tripoint divide_xy_round_to_minus_infinity( const tripoint &p, int d )
{
return tripoint( divide_round_to_minus_infinity( p.x, d ),
divide_round_to_minus_infinity( p.y, d ),
p.z );
}
static constexpr tripoint tripoint_zero{};
static constexpr point point_zero{};
static constexpr point point_north{ 0, -1 };
static constexpr point point_north_east{ 1, -1 };
static constexpr point point_east{ 1, 0 };
static constexpr point point_south_east{ 1, 1 };
static constexpr point point_south{ 0, 1 };
static constexpr point point_south_west{ -1, 1 };
static constexpr point point_west{ -1, 0 };
static constexpr point point_north_west{ -1, -1 };
static constexpr tripoint tripoint_north{ point_north, 0 };
static constexpr tripoint tripoint_north_east{ point_north_east, 0 };
static constexpr tripoint tripoint_east{ point_east, 0 };
static constexpr tripoint tripoint_south_east{ point_south_east, 0 };
static constexpr tripoint tripoint_south{ point_south, 0 };
static constexpr tripoint tripoint_south_west{ point_south_west, 0 };
static constexpr tripoint tripoint_west{ point_west, 0 };
static constexpr tripoint tripoint_north_west{ point_north_west, 0 };
static constexpr tripoint tripoint_above{ 0, 0, 1 };
static constexpr tripoint tripoint_below{ 0, 0, -1 };
struct sphere {
int radius = 0;
tripoint center = tripoint_zero;
sphere() = default;
explicit sphere( const tripoint ¢er ) : radius( 1 ), center( center ) {}
explicit sphere( const tripoint ¢er, int radius ) : radius( radius ), center( center ) {}
};
#ifndef CATA_NO_STL
/**
* Following functions return points in a spiral pattern starting at center_x/center_y until it hits the radius. Clockwise fashion.
* Credit to Tom J Nowell; http://stackoverflow.com/a/1555236/1269969
*/
std::vector<tripoint> closest_points_first( const tripoint ¢er, int max_dist );
std::vector<tripoint> closest_points_first( const tripoint ¢er, int min_dist, int max_dist );
std::vector<point> closest_points_first( point center, int max_dist );
std::vector<point> closest_points_first( point center, int min_dist, int max_dist );
static constexpr tripoint tripoint_min { INT_MIN, INT_MIN, INT_MIN };
static constexpr tripoint tripoint_max{ INT_MAX, INT_MAX, INT_MAX };
static constexpr point point_min{ tripoint_min.xy() };
static constexpr point point_max{ tripoint_max.xy() };
// Make point hashable so it can be used as an unordered_set or unordered_map key,
// or a component of one.
namespace std
{
template <>
struct hash<point> {
std::size_t operator()( point k ) const noexcept {
constexpr uint64_t a = 2862933555777941757;
size_t result = k.y;
result *= a;
result += k.x;
return result;
}
};
} // namespace std
// Make tripoint hashable so it can be used as an unordered_set or unordered_map key,
// or a component of one.
namespace std
{
template <>
struct hash<tripoint> {
std::size_t operator()( const tripoint &k ) const noexcept {
constexpr uint64_t a = 2862933555777941757;
size_t result = k.z;
result *= a;
result += k.y;
result *= a;
result += k.x;
return result;
}
};
} // namespace std
static constexpr std::array<point, 4> four_adjacent_offsets{{
point_north, point_east, point_south, point_west
}};
static constexpr std::array<point, 4> four_diagonal_offsets{{
point_north_east, point_south_east, point_south_west, point_north_west
}};
static constexpr std::array<point, 8> eight_adjacent_offsets{{
point_north, point_north_east, point_east, point_south_east,
point_south, point_south_west, point_west, point_north_west
}};
static constexpr std::array<point, 4> neighborhood{ {
point_south, point_east, point_west, point_north
}};
static constexpr std::array<point, 4> offsets = {{
point_south, point_east, point_west, point_north
}
};
static constexpr std::array<point, 4> four_cardinal_directions{{
point_west, point_east, point_north, point_south
}};
static constexpr std::array<point, 5> five_cardinal_directions{{
point_west, point_east, point_north, point_south, point_zero
}};
static constexpr std::array<tripoint, 6> six_cardinal_directions{{
tripoint_west, tripoint_east, tripoint_north, tripoint_south,
tripoint_above, tripoint_below
}};
static const std::array<tripoint, 8> eight_horizontal_neighbors = { {
{ tripoint_north_west },
{ tripoint_north },
{ tripoint_north_east },
{ tripoint_west },
{ tripoint_east },
{ tripoint_south_west },
{ tripoint_south },
{ tripoint_south_east },
}
};
#endif // CATA_NO_STL
#endif // CATA_SRC_POINT_H