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);
37  bool operator!=(const Point3D& rhs);
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 
64  Point2D(float in_x, float in_y) :
65  x(in_x),
66  y(in_y) {
67  }
68 
69  Point2D& operator+=(const Point2D& rhs);
70  Point2D& operator-=(const Point2D& rhs);
71  Point2D& operator*=(float rhs);
72  Point2D& operator/=(float rhs);
73 
74  bool operator==(const Point2D& rhs);
75  bool operator!=(const Point2D& rhs);
76 };
77 
78 Point2D operator+(const Point2D& lhs, const Point2D& rhs);
79 Point2D operator-(const Point2D& lhs, const Point2D& rhs);
80 Point2D operator*(const Point2D& lhs, float rhs);
81 Point2D operator*(float lhs, const Point2D& rhs);
82 Point2D operator/(const Point2D& lhs, float rhs);
83 Point2D operator/(float lhs, const Point2D& rhs);
84 
85 typedef Point2D Vector2D;
86 typedef Point3D Vector3D;
87 
89 struct Rect2D {
90  Point2D from;
91  Point2D to;
92 };
93 
95 struct Point2DI {
96  int x;
97  int y;
98 
99  Point2DI(int in_x = 0, int in_y = 0) :
100  x(in_x),
101  y(in_y) {
102  }
103 
104  bool operator==(const Point2DI& rhs) const;
105  bool operator!=(const Point2DI& rhs) const;
106 };
107 
109 struct Rect2DI {
110  Point2DI from;
111  Point2DI to;
112 };
113 
115 struct Color {
116  uint8_t r;
117  uint8_t g;
118  uint8_t b;
119 
120  Color() :
121  r(255),
122  g(255),
123  b(255) {
124  }
125 
126  Color(uint8_t in_r, uint8_t in_g, uint8_t in_b) :
127  r(in_r),
128  g(in_g),
129  b(in_b) {
130  }
131 };
132 
133 namespace Colors {
134  static const Color White = Color(255, 255, 255);
135  static const Color Red = Color(255, 0, 0);
136  static const Color Green = Color(0, 255, 0);
137  static const Color Yellow = Color(255, 255, 0);
138  static const Color Blue = Color(0, 0, 255);
139  static const Color Teal = Color(0, 255, 255);
140  static const Color Purple = Color(255, 0, 255);
141  static const Color Black = Color(0, 0, 0);
142  static const Color Gray = Color(128, 128, 128);
143 };
144 
147 float GetRandomScalar();
150 float GetRandomFraction();
155 int GetRandomInteger(int min, int max);
156 
160 template <typename Container>
161 typename Container::value_type& GetRandomEntry(Container& container) {
162  typename Container::iterator iter = container.begin();
163  std::advance(iter, GetRandomInteger(0, static_cast<int>(container.size()) - 1));
164  return *iter;
165 }
166 
171 float Distance2D(const Point2D& a, const Point2D& b);
176 float DistanceSquared2D(const Point2D& a, const Point2D& b);
179 void Normalize2D(Point2D& a);
184 float Dot2D(const Point2D& a, const Point2D& b);
185 
190 float Distance3D(const Point3D& a, const Point3D& b);
195 float DistanceSquared3D(const Point3D& a, const Point3D& b);
198 void Normalize3D(Point3D& a);
203 float Dot3D(const Point3D& a, const Point3D& b);
204 
205 }
2D integer point.
Definition: sc2_common.h:95
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:115
void Normalize3D(Point3D &a)
float Dot3D(const Point3D &a, const Point3D &b)
2D rectangle.
Definition: sc2_common.h:89
float DistanceSquared3D(const Point3D &a, const Point3D &b)
float DistanceSquared2D(const Point2D &a, const Point2D &b)
2D integer rectangle.
Definition: sc2_common.h:109
float Distance3D(const Point3D &a, const Point3D &b)
Container::value_type & GetRandomEntry(Container &container)
Definition: sc2_common.h:161