10 #include <cereal/cereal.hpp> 11 #include <fmt/format.h> 29 float constexpr
kfInfty = std::numeric_limits<float>::infinity();
30 float constexpr
kfLowest = std::numeric_limits<float>::lowest();
31 float constexpr
kfMax = std::numeric_limits<float>::max();
32 float constexpr
kfEpsilon = std::numeric_limits<float>::epsilon();
33 double constexpr
kdInfty = std::numeric_limits<double>::infinity();
34 double constexpr
kdLowest = std::numeric_limits<double>::lowest();
35 double constexpr
kdMax = std::numeric_limits<double>::max();
36 double constexpr
kdEpsilon = std::numeric_limits<double>::epsilon();
37 int constexpr
kForever = 24 * 60 * 60 * 24 * 7;
46 constexpr
Vec2T() : x(0), y(0) {}
47 constexpr
Vec2T(T x, T y) : x(x), y(y) {}
49 explicit Vec2T(U
const& other) : x(other.x), y(other.y) {}
51 explicit Vec2T(U* other) : x(other->x), y(other->y) {}
52 template <
typename U,
typename V>
53 explicit Vec2T(std::pair<U, V>
const& other)
54 : x(other.first), y(other.second) {}
63 return x == other.
x && y == other.
y;
66 return x != other.
x || y != other.
y;
69 bool operator<(Vec2T<T>
const& other)
const {
70 return x < other.x || (x == other.x && y < other.y);
74 return Vec2T(x + scalar, y + scalar);
77 return Vec2T(x - scalar, y - scalar);
80 return Vec2T(x * scalar, y * scalar);
83 return Vec2T(x / scalar, y / scalar);
86 return Vec2T(x + other.
x, y + other.
y);
89 return Vec2T(x - other.
x, y - other.
y);
123 return Vec2T(other.
x - x, other.
y - y).length();
125 template <
typename U>
127 return Vec2T(other->x - x, other->y - y).length();
130 return std::sqrt(x * x + y * y);
136 *
this *= len == 0 ? 1. : 1. / len;
141 double sine = std::sin(radians), cosine = std::cos(radians);
142 double xNew = x * cosine - y * sine;
143 double yNew = x * sine + y * cosine;
154 return (towards - *
this) * (distance / separation) + (*
this);
159 return denominator == 0 ? 0 :
Vec2T::dot(a, b) / denominator;
162 return a.
x * b.
x + a.
y * b.
y;
165 return dot(*
this, other);
168 return (a.
x * b.
y) - (a.
y * b.
x);
171 template <
class Archive>
173 ar(CEREAL_NVP(x), CEREAL_NVP(y));
181 template <
typename T>
190 Rect2T(T x, T y, T width, T height) : x(x), y(y), w(width), h(height) {}
194 w(bottomRight.x - topLeft.x),
195 h(bottomRight.y - topLeft.y) {}
197 : x(topLeft.x), y(topLeft.y), w(width), h(height) {}
198 template <
typename T2>
200 template <
typename T2>
209 return x == r.
x && y == r.
y && w == r.
w && h == r.
w;
232 return Vec2T<T>(x + w / 2, y + h / 2);
236 t.
x = center.
x - width / 2;
237 t.
y = center.
y - height / 2;
244 return w == T(0) && h == T(0);
247 return w <= T(0) && h <= T(0);
259 t.
x = std::min(x, r.
x);
260 t.
y = std::min(y, r.
y);
261 t.
w = std::max(x + w, r.
x + r.
w) - t.
x;
262 t.
h = std::max(y + h, r.
y + r.
h) - t.
y;
267 if (empty() || r.
empty()) {
274 T right2 = r.
x + r.
w;
276 if (left1 >= right2 || left2 >= right1) {
284 T bottom2 = r.
y + r.
h;
286 if (top1 >= bottom2 || top2 >= bottom1) {
292 t.
x = std::max(left1, left2);
293 t.
y = std::max(top1, top2);
294 t.
w = std::min(right1, right2) - t.
x;
295 t.
h = std::min(bottom1, bottom2) - t.
y;
300 return pt.
x >= left() && pt.
x < right() && pt.
y >= top() && pt.
y < bottom();
303 template <
class Archive>
305 ar(CEREAL_NVP(x), CEREAL_NVP(y), CEREAL_NVP(w), CEREAL_NVP(h));
334 static_assert(Command::MAX > 0,
"Invalid MAX command value");
335 #if defined(__GNUC__) || defined(__clang__) 336 return __builtin_ctzl(Command::MAX);
338 std::underlying_type<Command>::type n = Command::MAX;
350 template <
typename T>
352 template <
typename ParseContext>
353 constexpr
auto parse(ParseContext& ctx) {
357 template <
typename FormatContext>
359 return format_to(ctx.begin(),
"({},{})", p.
x, p.
y);
365 template <
typename T>
366 inline ostream& operator<<(ostream& strm, cherrypi::Vec2T<T>
const& p) {
367 return strm <<
"(" << p.x <<
"," << p.y <<
")";
370 template <
typename T>
371 inline ostream& operator<<(ostream& strm, cherrypi::Rect2T<T>
const& r) {
372 return strm <<
"(" << r.x <<
"," << r.y <<
" " << r.w <<
"x" << r.h <<
")";
375 template <
typename T>
378 return hashT(pos.
x * pos.
y) ^ hashT(pos.
y);
382 function<size_t(T)> hashT = hash<T>();
387 using utype = std::underlying_type<cherrypi::Command>::type;
389 return h(static_cast<utype>(cmd));
double distanceTo(U *other) const
Definition: basetypes.h:126
Rect2T(Vec2T< T > const &topLeft, T width, T height)
Definition: basetypes.h:196
int FrameNum
Definition: basetypes.h:22
Definition: basetypes.h:325
T bottom() const
Definition: basetypes.h:221
constexpr Vec2T()
Definition: basetypes.h:46
Vec2T< T > center() const
Definition: basetypes.h:231
bool contains(const Vec2T< T > &pt) const
Definition: basetypes.h:299
T dot(Vec2T const &other)
Definition: basetypes.h:164
Command
Abstract "meta" commands for UPCTuples.
Definition: basetypes.h:314
double constexpr kdInfty
Definition: basetypes.h:33
Vec2T operator+(Vec2T const &other) const
Definition: basetypes.h:85
Vec2T & operator=(Vec2T const &other)
Definition: basetypes.h:56
T width() const
Definition: basetypes.h:224
Vec2T & operator/=(T scalar)
Definition: basetypes.h:106
T right() const
Definition: basetypes.h:215
Vec2T & rotateDegrees(double degrees)
Definition: basetypes.h:139
bool operator==(Vec2T const &other) const
Definition: basetypes.h:62
Rect2T< T > united(Rect2T< T > const &r)
Definition: basetypes.h:250
T w
Definition: basetypes.h:186
Rect2T< int > Rect
Definition: basetypes.h:309
Vec2T & operator*=(T scalar)
Definition: basetypes.h:101
Definition: basetypes.h:318
double distanceTo(Vec2T const &other) const
Definition: basetypes.h:122
static T cross(Vec2T const &a, Vec2T const &b)
Definition: basetypes.h:167
Rect2T< T > & operator=(Rect2T< T2 > const &r)
Definition: basetypes.h:201
T y
Definition: basetypes.h:44
Definition: basetypes.h:322
Vec2T operator-(Vec2T const &other) const
Definition: basetypes.h:88
double constexpr kDegPerRad
Definition: basetypes.h:28
Definition: basetypes.h:320
Definition: basetypes.h:328
Vec2T & normalize()
Definition: basetypes.h:132
double length() const
Definition: basetypes.h:129
Rect2T(Rect2T< T2 > const &r)
Definition: basetypes.h:199
Rect2T(Vec2T< T > const &topLeft, Vec2T< T > const &bottomRight)
Definition: basetypes.h:191
Definition: basetypes.h:321
static Rect2T< T > centeredWithSize(Vec2T< T > const ¢er, T width, T height)
Definition: basetypes.h:234
UpcId constexpr kRootUpcId
Definition: basetypes.h:24
bool operator==(Rect2T< T > const &r) const
Definition: basetypes.h:208
double constexpr kdEpsilon
Definition: basetypes.h:36
Rect2T< T > intersected(Rect2T< T > const &r) const
Definition: basetypes.h:266
double constexpr kdMax
Definition: basetypes.h:35
Definition: basetypes.h:327
Vec2T operator-(T scalar) const
Definition: basetypes.h:76
T left() const
Definition: basetypes.h:212
float constexpr kfLowest
Definition: basetypes.h:30
Rect2T()
Definition: basetypes.h:189
T y
Definition: basetypes.h:185
bool null() const
Definition: basetypes.h:243
T top() const
Definition: basetypes.h:218
Definition: basetypes.h:317
void serialize(Archive &ar)
Definition: basetypes.h:304
Definition: basetypes.h:324
std::underlying_type< cherrypi::Command >::type utype
Definition: basetypes.h:387
double constexpr kdLowest
Definition: basetypes.h:34
Vec2T & operator-=(Vec2T const &other)
Definition: basetypes.h:116
Vec2T project(Vec2T towards, T distance)
Definition: basetypes.h:148
constexpr int kLarvaFrames
Definition: basetypes.h:38
T x
Definition: basetypes.h:184
Vec2T & operator+=(Vec2T const &other)
Definition: basetypes.h:111
Definition: basetypes.h:319
constexpr Vec2T(T x, T y)
Definition: basetypes.h:47
constexpr Position kInvalidPosition
Definition: basetypes.h:179
Vec2T & operator-=(T scalar)
Definition: basetypes.h:96
float constexpr kfMax
Definition: basetypes.h:31
float constexpr kfInfty
Definition: basetypes.h:29
Definition: basetypes.h:316
Vec2T(std::pair< U, V > const &other)
Definition: basetypes.h:53
Definition: basetypes.h:182
Vec2T(U const &other)
Definition: basetypes.h:49
int constexpr kForever
Definition: basetypes.h:37
bool empty() const
Definition: basetypes.h:246
Definition: basetypes.h:326
Vec2T operator+(T scalar) const
Definition: basetypes.h:73
int PlayerId
Definition: basetypes.h:21
size_t operator()(cherrypi::Command const &cmd) const
Definition: basetypes.h:388
T h
Definition: basetypes.h:187
Definition: basetypes.h:323
T x
Definition: basetypes.h:43
Main namespace for bot-related code.
Definition: areainfo.cpp:17
Definition: basetypes.h:349
float constexpr kfEpsilon
Definition: basetypes.h:32
Vec2T< float > Vec2
Definition: basetypes.h:177
Definition: basetypes.h:41
static double cos(Vec2T const &a, Vec2T const &b)
Definition: basetypes.h:157
void serialize(Archive &ar)
Definition: basetypes.h:172
int UpcId
Definition: basetypes.h:23
Rect2T(T x, T y, T width, T height)
Definition: basetypes.h:190
bool operator!=(Vec2T const &other) const
Definition: basetypes.h:65
Vec2T(U *other)
Definition: basetypes.h:51
Vec2T & operator+=(T scalar)
Definition: basetypes.h:91
T height() const
Definition: basetypes.h:227
Vec2T operator/(T scalar) const
Definition: basetypes.h:82
Vec2T operator*(T scalar) const
Definition: basetypes.h:79
int constexpr numUpcCommands()
Does not count the "None" command.
Definition: basetypes.h:333
size_t operator()(cherrypi::Vec2T< T > const &pos) const
Definition: basetypes.h:377
UpcId constexpr kFilteredUpcId
Definition: basetypes.h:26
static T dot(Vec2T const &a, Vec2T const &b)
Definition: basetypes.h:161
Vec2T< int > Position
Definition: basetypes.h:178
UpcId constexpr kInvalidUpcId
Definition: basetypes.h:25