SC2API
An API for AI for StarCraft II
sc2_common.h
Go to the documentation of this file.
1 
4 #pragma once
5 
6 #include <algorithm>
7 #include <cmath>
8 #include <cstdint>
9 
10 namespace sc2 {
11 
14 struct Point3D {
15  float x;
16  float y;
17  float z;
18 
19  Point3D() :
20  x(0.0f),
21  y(0.0f),
22  z(0.0f) {
23  }
24 
25  Point3D(float in_x, float in_y, float in_z) :
26  x(in_x),
27  y(in_y),
28  z(in_z) {
29  }
30 
31  Point3D& operator+=(const Point3D& rhs);
32  Point3D& operator-=(const Point3D& rhs);
33  Point3D& operator*=(float rhs);
34  Point3D& operator/=(float rhs);
35 
36  bool operator==(const Point3D& rhs) const;
37  bool operator!=(const Point3D& rhs) const;
38 };
39 
40 Point3D operator+(const Point3D& lhs, const Point3D& rhs);
41 Point3D operator-(const Point3D& lhs, const Point3D& rhs);
42 Point3D operator*(const Point3D& lhs, float rhs);
43 Point3D operator*(float lhs, const Point3D& rhs);
44 Point3D operator/(const Point3D& lhs, float rhs);
45 Point3D operator/(float lhs, const Point3D& rhs);
46 
49 struct Point2D {
50  float x;
51  float y;
52 
53  Point2D() :
54  x(0.0f),
55  y(0.0f) {
56  }
57 
58  Point2D(Point3D a) :
59  x(a.x),
60  y(a.y) {
61  }
62 
63  Point2D(float in_x, float in_y) :
64  x(in_x),
65  y(in_y) {
66  }
67 
68  Point2D& operator+=(const Point2D& rhs);
69  Point2D& operator-=(const Point2D& rhs);
70  Point2D& operator*=(float rhs);
71  Point2D& operator/=(float rhs);
72 
73  bool operator==(const Point2D& rhs) const;
74  bool operator!=(const Point2D& rhs) const;
75 };
76 
77 Point2D operator+(const Point2D& lhs, const Point2D& rhs);
78 Point2D operator-(const Point2D& lhs, const Point2D& rhs);
79 Point2D operator*(const Point2D& lhs, float rhs);
80 Point2D operator*(float lhs, const Point2D& rhs);
81 Point2D operator/(const Point2D& lhs, float rhs);
82 Point2D operator/(float lhs, const Point2D& rhs);
83 
84 typedef Point2D Vector2D;
85 typedef Point3D Vector3D;
86 
88 struct Rect2D {
89  Point2D from;
90  Point2D to;
91 };
92 
94 struct Point2DI {
95  int x;
96  int y;
97 
98  Point2DI(int in_x = 0, int in_y = 0) :
99  x(in_x),
100  y(in_y) {
101  }
102 
103  Point2DI(const Point2D& point) :
104  x(static_cast<int>(point.x)),
105  y(static_cast<int>(point.y)) {
106  }
107 
108  bool operator==(const Point2DI& rhs) const;
109  bool operator!=(const Point2DI& rhs) const;
110 };
111 
113 struct Rect2DI {
114  Point2DI from;
115  Point2DI to;
116 
117  Rect2DI() {
118  }
119 
120  Rect2DI(const Point2DI& in_from, const Point2DI& in_to) :
121  from(in_from),
122  to(in_to) {
123  }
124 
125  int Width() const;
126 
127  int Height() const;
128 
129  bool Contain(const sc2::Point2DI& point) const;
130 };
131 
133 struct Color {
134  uint8_t r;
135  uint8_t g;
136  uint8_t b;
137 
138  Color() :
139  r(255),
140  g(255),
141  b(255) {
142  }
143 
144  Color(uint8_t in_r, uint8_t in_g, uint8_t in_b) :
145  r(in_r),
146  g(in_g),
147  b(in_b) {
148  }
149 };
150 
151 namespace Colors {
152  static const Color White = Color(255, 255, 255);
153  static const Color Red = Color(255, 0, 0);
154  static const Color Green = Color(0, 255, 0);
155  static const Color Yellow = Color(255, 255, 0);
156  static const Color Blue = Color(0, 0, 255);
157  static const Color Teal = Color(0, 255, 255);
158  static const Color Purple = Color(255, 0, 255);
159  static const Color Black = Color(0, 0, 0);
160  static const Color Gray = Color(128, 128, 128);
161 };
162 
165 float GetRandomScalar();
168 float GetRandomFraction();
173 int GetRandomInteger(int min, int max);
174 
178 template <typename Container>
179 typename Container::value_type& GetRandomEntry(Container& container) {
180  typename Container::iterator iter = container.begin();
181  std::advance(iter, GetRandomInteger(0, static_cast<int>(container.size()) - 1));
182  return *iter;
183 }
184 
189 float Distance2D(const Point2D& a, const Point2D& b);
194 float DistanceSquared2D(const Point2D& a, const Point2D& b);
197 void Normalize2D(Point2D& a);
202 float Dot2D(const Point2D& a, const Point2D& b);
203 
208 float Distance3D(const Point3D& a, const Point3D& b);
213 float DistanceSquared3D(const Point3D& a, const Point3D& b);
216 void Normalize3D(Point3D& a);
221 float Dot3D(const Point3D& a, const Point3D& b);
222 
223 }
2D integer point.
Definition: sc2_common.h:94
Definition: sc2_common.h:14
Definition: sc2_common.h:49
float Dot2D(const Point2D &a, const Point2D &b)
Definition: sc2_action.h:9
float GetRandomFraction()
int GetRandomInteger(int min, int max)
float Distance2D(const Point2D &a, const Point2D &b)
float GetRandomScalar()
void Normalize2D(Point2D &a)
RGB Color.
Definition: sc2_common.h:133
void Normalize3D(Point3D &a)
float Dot3D(const Point3D &a, const Point3D &b)
2D rectangle.
Definition: sc2_common.h:88
float DistanceSquared3D(const Point3D &a, const Point3D &b)
float DistanceSquared2D(const Point2D &a, const Point2D &b)
2D integer rectangle.
Definition: sc2_common.h:113
float Distance3D(const Point3D &a, const Point3D &b)
Container::value_type & GetRandomEntry(Container &container)
Definition: sc2_common.h:179