forked from Voidious/BerryBots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bbutil.h
369 lines (330 loc) · 7.6 KB
/
bbutil.h
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
/*
Copyright (C) 2012-2013 - Voidious
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#ifndef BBUTIL_H
#define BBUTIL_H
#include <complex>
#include <platformstl/performance/performance_counter.hpp>
#include "bbconst.h"
extern "C" {
#include "lua.h"
}
#define SHIP "Ship"
#define SENSORS "Sensors"
#define STAGE_BUILDER "StageBuilder"
#define WALL "Wall"
#define ZONE "Zone"
#define WORLD "World"
#define SHIP_GFX "ShipGfx"
#define ADMIN "Admin"
#define STAGE_SENSORS "StageSensors"
#define STAGE_GFX "StageGfx"
#define RUNNER_FORM "RunnerForm"
#define MATCH_RUNNER "MatchRunner"
#define RUNNER_FILES "RunnerFiles"
class BerryBotsEngine;
class GameRunner;
class ReplayBuilder;
// Graphic definition structs
typedef struct {
int r;
int g;
int b;
int a;
} RgbaColor;
typedef struct {
char *text;
double x;
double y;
int fontSize;
int startTime;
short textR;
short textG;
short textB;
short textA;
int drawTicks;
} StageText;
typedef struct {
double left;
double bottom;
double width;
double height;
double rotation;
short fillR;
short fillG;
short fillB;
short fillA;
double outlineThickness;
short outlineR;
short outlineG;
short outlineB;
short outlineA;
int startTime;
int drawTicks;
} UserGfxRectangle;
typedef struct {
double x;
double y;
double angle;
double length;
double thickness;
short fillR;
short fillG;
short fillB;
short fillA;
double outlineThickness;
short outlineR;
short outlineG;
short outlineB;
short outlineA;
int startTime;
int drawTicks;
} UserGfxLine;
typedef struct {
double x;
double y;
double radius;
short fillR;
short fillG;
short fillB;
short fillA;
double outlineThickness;
short outlineR;
short outlineG;
short outlineB;
short outlineA;
int startTime;
int drawTicks;
} UserGfxCircle;
typedef struct {
char *text;
double x;
double y;
int fontSize;
short textR;
short textG;
short textB;
short textA;
int startTime;
int drawTicks;
} UserGfxText;
// Game engine structs
typedef struct {
char *key;
double value;
} ScoreStat;
typedef struct {
int rank;
double score;
ScoreStat* stats[MAX_SCORE_STATS];
int numStats;
bool showResult;
} TeamResult;
typedef struct {
short index;
short firstShipIndex;
short numShips;
short shipsAlive;
bool hasRoundOver;
bool hasGameOver;
int stageEventRef;
lua_State *state;
char name[MAX_NAME_LENGTH + 1];
char filename[MAX_NAME_LENGTH + 1];
platformstl::performance_counter counter;
unsigned long long cpuTime[CPU_TIME_TICKS];
unsigned long long totalCpuTime;
unsigned int totalCpuTicks;
bool stageShip;
bool disabled;
bool errored;
bool ownedByLua;
bool gfxEnabled;
UserGfxRectangle* gfxRectangles[MAX_USER_RECTANGLES];
int numRectangles;
bool tooManyRectangles;
UserGfxLine* gfxLines[MAX_USER_LINES];
int numLines;
bool tooManyLines;
UserGfxCircle* gfxCircles[MAX_USER_RECTANGLES];
int numCircles;
bool tooManyCircles;
UserGfxText* gfxTexts[MAX_USER_TEXTS];
int numTexts;
bool tooManyTexts;
TeamResult result;
} Team;
// Static ship properties that we don't need to copy around.
typedef struct {
char name[MAX_NAME_LENGTH + 1];
short shipR;
short shipG;
short shipB;
short laserR;
short laserG;
short laserB;
short thrusterR;
short thrusterG;
short thrusterB;
short shieldsR;
short shieldsG;
short shieldsB;
bool stageShip;
bool disabled;
bool ownedByLua;
BerryBotsEngine *engine;
} ShipProperties;
typedef struct {
short index;
short teamIndex;
double thrusterAngle;
double thrusterForce;
double x;
double y;
double heading;
double speed;
double momentum;
double energy;
double power;
double shields;
short torpedoAmmo;
short laserGunHeat;
short torpedoGunHeat;
bool hitWall;
bool hitShip;
bool alive;
bool laserEnabled;
bool torpedoEnabled;
bool thrusterEnabled;
bool energyEnabled;
bool powerEnabled;
bool shieldsEnabled;
bool showName;
bool newColors;
double kills;
double friendlyKills;
double damage;
double friendlyDamage;
double shieldedDamage;
ShipProperties *properties;
} Ship;
typedef struct {
int hitByShipRef;
int hitByLaserRef;
int hitByTorpedoRef;
int hitWallRef;
int shipDestroyedRef;
int shipFiredLaserRef;
int shipFiredTorpedoRef;
int laserHitShipRef;
int stageEventRef;
} Sensors;
typedef struct {
int id;
short shipIndex;
int fireTime;
double srcX;
double srcY;
double x;
double y;
double heading;
double dx;
double dy;
bool dead;
} Laser;
typedef struct {
int id;
short shipIndex;
int fireTime;
double srcX;
double srcY;
double x;
double y;
double heading;
double distance;
double dx;
double dy;
double distanceTraveled;
} Torpedo;
typedef struct {
BerryBotsEngine *engine;
} StageBuilder;
typedef struct {
int width;
int height;
short numShips;
short teamSize;
int time;
int wallsRef;
int zonesRef;
int constantsRef;
BerryBotsEngine *engine;
} World;
typedef struct {
Team *team;
BerryBotsEngine *engine;
} ShipGfx;
typedef struct {
BerryBotsEngine *engine;
} Admin;
typedef struct {
BerryBotsEngine *engine;
} StageGfx;
typedef struct {
int shipHitShipRef;
int laserHitShipRef;
int torpedoHitShipRef;
int shipHitWallRef;
int shipDestroyedRef;
int shipFiredLaserRef;
int shipFiredTorpedoRef;
} StageSensors;
typedef struct {
GameRunner *gameRunner;
} LuaRunnerForm;
typedef struct {
GameRunner *gameRunner;
ReplayBuilder *replayBuilder;
} MatchRunner;
typedef struct {
GameRunner *gameRunner;
} RunnerFiles;
extern double limit(double p, double q, double r);
extern int signum(double x);
extern double square(double x);
extern double abs(double x);
extern double round(double d, int x);
extern double normalRelativeAngle(double x);
extern double normalAbsoluteAngle(double x);
extern double toDegrees(double x);
extern char** parseFlag(
int argc, char *argv[], const char *flag, int numValues);
extern bool flagExists(int argc, char *argv[], const char *flag);
extern bool isWhitespace(const char *s);
extern char* getTimestamp();
extern double approxReal(std::complex<double> zz, double epsRel, double epsAbs);
extern double getPosMin(double aa, double bb);
extern double getPosMin(double aa, double bb, double cc, double dd);
extern void solveQuadratic(double aa, double bb, double cc, double *t1, double *t2);
extern void solveCubic(
double aa, double bb, double cc, double dd,
double *t1, double *t2, double *t3);
extern void solveQuartic(
double aa, double bb, double cc, double dd, double ee,
double *t1, double *t2, double *t3, double *t4);
typedef int (*rootFun)(double* xx, void* params, double* out);
extern int newtonBisect(rootFun fun, double* xStart, void* params, double* out, double epsRel, double epsAbs);
#endif