forked from hoijui/KAIK
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Maths.cpp
254 lines (194 loc) · 6.51 KB
/
Maths.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
#include <ctime>
#include "IncExternAI.h"
#include "IncGlobalAI.h"
CMaths::CMaths(AIClasses* ai) {
this->ai = ai;
mapfloat3height = ai->cb->GetMapHeight() * MAPUNIT2POS;
mapfloat3width = ai->cb->GetMapWidth() * MAPUNIT2POS;
MTRandInt.seed(time(NULL));
MTRandFloat.seed(MTRandInt());
#ifdef WIN32
QueryPerformanceFrequency(&ticksPerSecond);
#endif
}
CMaths::~CMaths() {
}
void CMaths::F3MapBound(float3& pos) {
if (pos.x < 65)
pos.x = 65;
else if (pos.x > mapfloat3width - 65)
pos.x = mapfloat3width - 65;
if (pos.z < 65)
pos.z = 65;
else if (pos.z > mapfloat3height - 65)
pos.z = mapfloat3height - 65;
}
float3 CMaths::F3Randomize(const float3& pos, float radius) {
float3 p;
p.x = pos.x + sin(float(RANDINT / 1000)) * radius;
p.z = pos.z + sin(float(RANDINT / 1000)) * radius;
return p;
}
void CMaths::F32XY(const float3& pos, int* x, int* y, int resolution) {
*x = int(pos.x / 8 / resolution);
*y = int(pos.z / 8 / resolution);
}
float3 CMaths::XY2F3(int x ,int y, int resolution) {
float3 testpos;
testpos.x = x * 8 * resolution;
testpos.y = 0;
testpos.z = y * 8 * resolution;
return testpos;
}
float CMaths::BuildMetalPerSecond(const UnitDef* builder,const UnitDef* built) {
if (builder->buildSpeed) {
float buildtime = built->buildTime / builder->buildSpeed;
return ((built->metalCost) / buildtime);
}
// MPS FAILED, unit has no buildspeed
return -1;
}
float CMaths::BuildEnergyPerSecond(const UnitDef* builder,const UnitDef* built) {
if (builder->buildSpeed) {
float buildtime = built->buildTime / builder->buildSpeed;
return ((built->energyCost) / buildtime);
}
// EPS FAILED, unit has no buildspeed
return -1;
}
float CMaths::BuildTime(const UnitDef* builder,const UnitDef* built) {
if (builder->buildSpeed)
return ((built->buildTime) / (builder->buildSpeed));
return -1;
}
/*
bool CMaths::FeasibleConstruction(const UnitDef* builder, const UnitDef* built, float MinMpc, float MinEpc) {
if (builder->buildSpeed) {
float buildtime = (built->buildTime) / (builder->buildSpeed);
float Echange = ((ai->cb->GetEnergyIncome()) * ECONRATIO) - (ai->cb->GetEnergyUsage()) - (built->energyCost / buildtime);
float ResultingRatio = (ai->cb->GetEnergy() + (Echange * buildtime)) / ai->cb->GetEnergyStorage();
if (ResultingRatio > MinEpc) {
float Mchange = ai->cb->GetMetalIncome() * ECONRATIO - ai->cb->GetMetalUsage() - built->metalCost / buildtime;
ResultingRatio = (ai->cb->GetMetal() + (Mchange * buildtime)) / (ai->cb->GetMetalStorage());
if (ResultingRatio > MinMpc) {
return true;
}
}
}
return false;
}
*/
bool CMaths::MFeasibleConstruction(const UnitDef* builder, const UnitDef* built, float MinMpc) {
if (builder->buildSpeed) {
float buildtime = (built->buildTime) / (builder->buildSpeed);
float Mchange = ((ai->cb->GetMetalIncome()) * ECONRATIO) - (ai->cb->GetMetalUsage()) - (built->metalCost / buildtime);
// KLOOTNOTE: dividing by actual metal storage
// means things become infeasible with greater
// M storage capacity
float denom = 1.0f; // ai->cb->GetMetalStorage();
float ResultingRatio = (ai->cb->GetMetal() + (Mchange * buildtime)) / denom;
if (ResultingRatio > MinMpc) {
return true;
}
}
return false;
}
bool CMaths::EFeasibleConstruction(const UnitDef* builder, const UnitDef* built, float MinEpc) {
if (builder->buildSpeed) {
float buildtime = (built->buildTime) / (builder->buildSpeed);
float Echange = ((ai->cb->GetEnergyIncome()) * ECONRATIO) - (ai->cb->GetEnergyUsage()) - (built->energyCost / buildtime);
// KLOOTNOTE: dividing by actual energy storage
// means things become infeasible with greater
// E storage capacity
float denom = 1.0f; // ai->cb->GetEnergyStorage();
float ResultingRatio = (ai->cb->GetEnergy() + (Echange * buildtime)) / denom;
if (ResultingRatio > MinEpc) {
return true;
}
}
return false;
}
float CMaths::ETA(int unit, const float3& destination) {
float speed = ai->cb->GetUnitDef(unit)->speed;
float distance = destination.distance2D(ai->cb->GetUnitPos(unit));
return (distance / speed * 2);
}
float CMaths::ETT(BuildTask& bt) {
float percentdone = (ai->cb->GetUnitHealth(bt.id)) / (ai->cb->GetUnitMaxHealth(bt.id));
float buildpower = 0.0f;
std::list<int> killbuilders;
for (std::list<int>::iterator i = bt.builders.begin(); i != bt.builders.end(); i++) {
if (ai->cb->GetUnitDef(*i))
buildpower += ai->cb->GetUnitDef(*i)->buildSpeed;
else
killbuilders.push_back(*i);
}
for (std::list<int>::iterator i = killbuilders.begin(); i != killbuilders.end(); i++) {
bt.builders.remove(*i);
}
if (buildpower > 0.0f) {
return ((ai->cb->GetUnitDef(bt.id)->buildTime) / buildpower) * (1 - percentdone);
}
return MY_FLT_MAX;
}
float CMaths::GetUnitCost(const UnitDef* unit) {
return ((unit->metalCost * METAL2ENERGY) + (unit->energyCost));
}
float CMaths::GetUnitCost(int unit) {
return (ai->cb->GetUnitDef(unit)->metalCost * METAL2ENERGY) + (ai->cb->GetUnitDef(unit)->energyCost);
}
float CMaths::RandNormal(float m, float s, bool positiveonly) {
// normal distribution with mean m and standard deviation s
float normal_x1, normal_x2, w;
// make two normally distributed variates by Box-Muller transformation
do {
normal_x1 = 2.0 * RANDFLOAT - 1.0;
normal_x2 = 2.0 * RANDFLOAT - 1.0;
w = normal_x1 * normal_x1 + normal_x2 * normal_x2;
}
while (w >= 1.0 || w < 1E-30);
w = sqrt(log(w) * (-2.0 / w));
// normal_x1 and normal_x2 are independent normally distributed variates
normal_x1 *= w;
if (!positiveonly)
return (normal_x1 * s + m);
else
return std::max(0.0f, normal_x1 * s + m);
}
float CMaths::RandFloat() {
return MTRandFloat();
}
unsigned int CMaths::RandInt() {
return MTRandInt();
}
void CMaths::TimerStart() {
#ifdef WIN32
QueryPerformanceCounter(&tick_start);
tick_laststop = tick_start;
#else
gettimeofday(&tick_start, NULL);
tick_laststop = tick_start;
#endif
}
int CMaths::TimerTicks() {
#ifdef WIN32
QueryPerformanceCounter(&tick_end);
tick_laststop = tick_end;
return (tick_end.QuadPart - tick_start.QuadPart);
#else
gettimeofday(&tick_end, NULL);
tick_laststop = tick_end;
return ((tick_end.tv_sec - tick_start.tv_sec) * 1000000 + (tick_end.tv_usec - tick_start.tv_usec));
#endif
}
float CMaths::TimerSecs() {
#ifdef WIN32
QueryPerformanceCounter(&tick_end);
tick_laststop = tick_end;
return ((float(tick_end.QuadPart) - float(tick_start.QuadPart)) / float(ticksPerSecond.QuadPart));
#else
gettimeofday(&tick_end, NULL);
tick_laststop = tick_end;
return ((tick_end.tv_sec - tick_start.tv_sec) + (tick_end.tv_usec - tick_start.tv_usec) * 1.0e-6f);
#endif
}