/
position.cpp
436 lines (401 loc) · 9.46 KB
/
position.cpp
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#include "position.h"
#include "realms/realm.h"
#include <algorithm>
#include <array>
#include <cstdlib>
#include <limits>
#include <tuple>
namespace Pol
{
namespace Core
{
namespace
{
const std::array<Vec2d, 8> move_delta = { { { 0, -1 }, // 0 is N
{ +1, -1 }, // 1 is NE
{ +1, 0 }, // ...
{ +1, +1 },
{ 0, +1 },
{ -1, +1 },
{ -1, 0 },
{ -1, -1 } } };
const std::array<UFACING, 8> away_cvt = { FACING_S, FACING_SW, FACING_W, FACING_NW,
FACING_N, FACING_NE, FACING_E, FACING_SE };
u16 clip_u16( int v )
{
return static_cast<u16>(
std::clamp( v, 0, static_cast<int>( std::numeric_limits<u16>::max() ) ) );
}
} // namespace
bool Pos2d::operator==( const Pos2d& other ) const
{
return _x == other._x && _y == other._y;
}
bool Pos2d::operator!=( const Pos2d& other ) const
{
return !( *this == other );
}
Pos2d& Pos2d::operator-=( const Vec2d& other )
{
int x = static_cast<int>( _x ) - other.x();
int y = static_cast<int>( _y ) - other.y();
_x = clip_u16( x );
_y = clip_u16( y );
return *this;
}
Pos2d& Pos2d::operator+=( const Vec2d& other )
{
int x = static_cast<int>( _x ) + other.x();
int y = static_cast<int>( _y ) + other.y();
_x = clip_u16( x );
_y = clip_u16( y );
return *this;
}
Vec2d operator-( const Pos2d& lhs, const Pos2d& rhs )
{
int x = static_cast<int>( lhs.x() ) - rhs.x();
int y = static_cast<int>( lhs.y() ) - rhs.y();
return Vec2d( Vec2d::clip( x ), Vec2d::clip( y ) );
}
Pos2d operator-( Pos2d lhs, const Vec2d& rhs )
{
lhs -= rhs;
return lhs;
}
Pos2d operator+( Pos2d lhs, const Vec2d& rhs )
{
lhs += rhs;
return lhs;
}
u16 Pos2d::pol_distance( const Pos2d& other ) const
{
int xd = std::abs( static_cast<int>( _x ) - other._x );
int yd = std::abs( static_cast<int>( _y ) - other._y );
return static_cast<u16>( std::max( xd, yd ) );
}
bool Pos2d::in_range( const Pos2d& other, u16 range ) const
{
return pol_distance( other ) <= range;
}
Pos2d& Pos2d::crop( const Realms::Realm* realm )
{
if ( realm == nullptr )
return *this;
if ( _x >= realm->width() )
_x = realm->width() - 1;
if ( _y >= realm->height() )
_y = realm->height() - 1;
return *this;
}
bool Pos2d::can_move_to( const Vec2d& displacement, const Realms::Realm* realm ) const
{
Vec2d vec_fromorigin = from_origin() + displacement;
if ( vec_fromorigin.x() < 0 || vec_fromorigin.y() < 0 )
return false;
// TODO: realm should have a pos member then this comparison could be done directly via
// Pos2d<Pos2d
if ( realm == nullptr ||
( vec_fromorigin.x() < realm->width() && vec_fromorigin.y() < realm->height() ) )
return true;
return false;
}
Pos2d& Pos2d::move_to( UFACING dir )
{
return *this += move_delta[dir];
}
UFACING Pos2d::direction_toward( const Pos2d& dst ) const
{
if ( x() < dst.x() ) // East to target
{
if ( y() < dst.y() )
return FACING_SE;
else if ( y() == dst.y() )
return FACING_E;
else /* y() > dst.y() */
return FACING_NE;
}
else if ( x() == dst.x() )
{
if ( y() < dst.y() )
return FACING_S;
else if ( y() > dst.y() )
return FACING_N;
}
else /* x() > dst.x() */ // West to target
{
if ( y() < dst.y() )
return FACING_SW;
else if ( y() == dst.y() )
return FACING_W;
else /* y() > dst.y() */
return FACING_NW;
}
return FACING_N;
}
UFACING Pos2d::direction_away( const Pos2d& dst ) const
{
UFACING toward = direction_toward( dst );
return away_cvt[static_cast<int>( toward )];
}
Pos2d Pos2d::min( const Pos2d& v ) const
{
return Pos2d( std::min( _x, v._x ), std::min( _y, v._y ) );
}
Pos2d Pos2d::max( const Pos2d& v ) const
{
return Pos2d( std::max( _x, v._x ), std::max( _y, v._y ) );
}
fmt::Writer& operator<<( fmt::Writer& w, const Pos2d& v )
{
w << "( " << v.x() << ", " << v.y() << " )";
return w;
}
bool Pos3d::operator==( const Pos3d& other ) const
{
return _xy == other._xy && _z == other._z;
}
bool Pos3d::operator!=( const Pos3d& other ) const
{
return !( *this == other );
}
bool Pos3d::operator==( const Pos2d& other ) const
{
return _xy == other;
}
bool Pos3d::operator!=( const Pos2d& other ) const
{
return _xy != other;
}
Pos3d& Pos3d::operator-=( const Vec2d& other )
{
_xy -= other;
return *this;
}
Pos3d& Pos3d::operator+=( const Vec2d& other )
{
_xy += other;
return *this;
}
Pos3d& Pos3d::operator-=( const Vec3d& other )
{
_xy -= other.xy();
int z = static_cast<int>( _z ) - other.z();
_z = clip_s8( z );
return *this;
}
Pos3d& Pos3d::operator+=( const Vec3d& other )
{
_xy += other.xy();
int z = static_cast<int>( _z ) + other.z();
_z = clip_s8( z );
return *this;
}
Pos3d operator-( Pos3d lhs, const Vec2d& rhs )
{
lhs -= rhs;
return lhs;
}
Pos3d operator+( Pos3d lhs, const Vec2d& rhs )
{
lhs += rhs;
return lhs;
}
Pos3d operator-( Pos3d lhs, const Vec3d& rhs )
{
lhs -= rhs;
return lhs;
}
Pos3d operator+( Pos3d lhs, const Vec3d& rhs )
{
lhs += rhs;
return lhs;
}
Vec2d operator-( const Pos3d& lhs, const Pos2d& rhs )
{
return lhs.xy() - rhs;
}
Vec2d operator-( const Pos2d& lhs, const Pos3d& rhs )
{
return lhs - rhs.xy();
}
Vec3d operator-( const Pos3d& lhs, const Pos3d& rhs )
{
Vec2d xy = lhs.xy() - rhs.xy();
int z = static_cast<int>( lhs.z() ) - rhs.z();
return Vec3d( xy, Vec2d::clip( z ) );
}
bool Pos3d::can_move_to( const Vec2d& displacement, const Realms::Realm* realm ) const
{
return xy().can_move_to( displacement, realm );
}
Pos3d& Pos3d::move_to( UFACING dir )
{
_xy.move_to( dir );
return *this;
}
u16 Pos3d::pol_distance( const Pos3d& other ) const
{
return _xy.pol_distance( other._xy );
}
bool Pos3d::in_range( const Pos3d& other, u16 range ) const
{
return _xy.in_range( other._xy, range );
}
bool Pos3d::in_range( const Pos2d& other, u16 range ) const
{
return _xy.in_range( other, range );
}
Pos3d& Pos3d::crop( const Realms::Realm* realm )
{
_xy.crop( realm );
return *this;
}
s8 Pos3d::clip_s8( int v )
{
return static_cast<s8>( std::clamp( v, static_cast<int>( std::numeric_limits<s8>::min() ),
static_cast<int>( std::numeric_limits<s8>::max() ) ) );
}
fmt::Writer& operator<<( fmt::Writer& w, const Pos3d& v )
{
w << "( " << v.x() << ", " << v.y() << ", " << (int)v.z() << " )";
return w;
}
u16 Pos4d::crop_x( u16 x ) const
{
if ( _realm != nullptr && x >= _realm->width() )
return _realm->width() - 1;
return x;
}
u16 Pos4d::crop_y( u16 y ) const
{
if ( _realm != nullptr && y >= _realm->height() )
return _realm->height() - 1;
return y;
}
bool Pos4d::operator==( const Pos4d& other ) const
{
return _xyz == other._xyz && _realm == other._realm;
}
bool Pos4d::operator!=( const Pos4d& other ) const
{
return !( *this == other );
}
bool Pos4d::operator==( const Pos3d& other ) const
{
return _xyz == other;
}
bool Pos4d::operator!=( const Pos3d& other ) const
{
return !( *this == other );
}
bool Pos4d::operator==( const Pos2d& other ) const
{
return _xyz.xy() == other;
}
bool Pos4d::operator!=( const Pos2d& other ) const
{
return !( *this == other );
}
Pos4d& Pos4d::operator-=( const Vec2d& other )
{
_xyz -= other;
_xyz.crop( _realm );
return *this;
}
Pos4d& Pos4d::operator+=( const Vec2d& other )
{
_xyz += other;
_xyz.crop( _realm );
return *this;
}
Pos4d& Pos4d::operator-=( const Vec3d& other )
{
_xyz -= other;
_xyz.crop( _realm );
return *this;
}
Pos4d& Pos4d::operator+=( const Vec3d& other )
{
_xyz += other;
_xyz.crop( _realm );
return *this;
}
Pos4d operator-( Pos4d lhs, const Vec2d& rhs )
{
lhs -= rhs;
return lhs;
}
Pos4d operator+( Pos4d lhs, const Vec2d& rhs )
{
lhs += rhs;
return lhs;
}
Pos4d operator-( Pos4d lhs, const Vec3d& rhs )
{
lhs -= rhs;
return lhs;
}
Pos4d operator+( Pos4d lhs, const Vec3d& rhs )
{
lhs += rhs;
return lhs;
}
Vec2d operator-( const Pos4d& lhs, const Pos2d& rhs )
{
return lhs.xyz() - rhs;
}
Vec2d operator-( const Pos2d& lhs, const Pos4d& rhs )
{
return lhs - rhs.xyz();
}
Vec3d operator-( const Pos4d& lhs, const Pos3d& rhs )
{
return lhs.xyz() - rhs;
}
Vec3d operator-( const Pos3d& lhs, const Pos4d& rhs )
{
return lhs - rhs.xyz();
}
/*Vec3d operator-( const Pos4d& lhs, const Pos4d& rhs )
{
return lhs.xyz() - rhs.xyz();
}*/
Pos4d Pos4d::move( UFACING dir ) const
{
return *this + move_delta[dir];
}
Pos4d& Pos4d::move_to( UFACING dir )
{
_xyz.move_to( dir );
return *this;
}
bool Pol::Core::Pos4d::can_move_to( const Vec2d& displacement ) const
{
return xy().can_move_to( displacement, realm() );
}
u16 Pos4d::pol_distance( const Pos4d& other ) const
{
if ( _realm != other._realm )
return std::numeric_limits<u16>::max();
return _xyz.pol_distance( other._xyz );
}
bool Pos4d::in_range( const Pos4d& other, u16 range ) const
{
return _realm == other._realm && _xyz.in_range( other._xyz, range );
}
bool Pos4d::in_range( const Pos3d& other, u16 range ) const
{
return _xyz.in_range( other, range );
}
bool Pos4d::in_range( const Pos2d& other, u16 range ) const
{
return _xyz.in_range( other, range );
}
fmt::Writer& operator<<( fmt::Writer& w, const Pos4d& v )
{
w << "( " << v.x() << ", " << v.y() << ", " << (int)v.z() << ", "
<< ( v.realm() != nullptr ? v.realm()->name() : "null" ) << " )";
return w;
}
} // namespace Core
} // namespace Pol