-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmega-energy.c
410 lines (339 loc) · 11.2 KB
/
mega-energy.c
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*******************************************************************************
* ZR alliance competitor -- advanced rush bot *
* *
* Authors *
* Written by Yicheng *
* Optimised by Ethan *
* Strategy by Ishraq *
* With code from Shaumik and Young *
* *
* Description *
* First go for Energy *
* Then go for items *
* Then stay on top/mirror *
******************************************************************************/
/* TODO
* Determine if we want to go to the middle energy pack
* Write/implement minDistPair
*/
/* Direct quote from Ishraq:
* Strategy:
We will go for the closest energy pack and then head for the score packs,
where we will try to acquire them in the order of Lowest one closest to us
first, then middle one closer to enemy, then top, and then the last one.
Then we move to the top and then utilize Yeech's mimic and bomb strategy for
the rest of the game
* Purpose:
Essentially, our goal is to gain an edge over the enemy in the beginning of
the game by getting more score packs than them, and then for our middle and
end game strategy we want to use Yeech's mimick or bomb strategy to ensure at
least a tie with the enemy team for the remainder of the game.
* Note:
Using Shaumik's move function, we can pick up the first energy pack in ~25
seconds
*/
#define STOP -1
#define NORMAL 0
#define UPLOAD 1
#define GET_ITEM 2
#define ON_TOP 4
#define TO_ORIGIN 5
#define GET_ENERGY 6
/* ZR states of me and other */
ZRState me;
ZRState other;
/* Items:
* 0 - 2 = energy
* 3 - 6 = score <== 3 being the one on the bottom
* 7 - 8 = mirror
*/
float items[9][3];
/* Keeps Track of who has the items */
int itemPossession[9];
/* Where to go/point at */
float target[3];
float facing[3];
int targetID;
/* temp target vector, see moveTo */
float tarVec[3];
/* Standard Locations */
float earth[3];
float origin[3];
/* Temp/Counter */
float temp[3];
int n;
/* State Vars */
int movestate;
int rotatestate;
bool am_i_blue;
char debugs[5][50];
/* Initializes everything */
void init() {
for (n = 0 ; n < 9 ; n++) {
game.getItemLoc(items[n], n);
}
updateGameState();
origin[0] = origin[1] = origin[2] = earth[0] = earth[1] = 0.0f;
earth[2] = 1.0f;
movestate = GET_ENERGY;
rotatestate = NORMAL;
targetID = minDistEl(items, me, 0, 3); // find the min distance energy pack
am_i_blue = me[0] > 0;
n = 0;
strcpy(debugs[0], "we renouce weapons, hate and violence");
strcpy(debugs[1], "we strive for peace and understanding");
strcpy(debugs[2], "we wish for a better world in the future");
strcpy(debugs[3], "we condemn the terrible actions taken");
strcpy(debugs[4], "and pray for the lives that were lost");
}
/* Runs every second */
void loop() {
if (game.getCurrentTime() < 5) {
if (am_i_blue) {
DEBUG(("%s\n", debugs[n]));
n++;
}
}
else if (game.getCurrentTime() < 10) {
if (!am_i_blue) {
DEBUG(("%s\n", debugs[n]));
n++;
}
}
else {
updateGameState();
changeState();
if (game.getFuelRemaining() == 0) {
game.takePic();
}
/* Design concept: 2 separate state vars,
* rotatestate governs the picture taking mechanism/rotation
* movestate governs the item getting mechanism/movement
*/
DEBUG(("%f\n", mathVecMagnitude(me + 3, 3)));
switch (rotatestate) {
case NORMAL:
if (game.posInArea(other) > 0 &&
game.isFacingOther() &&
game.getEnergy() > 1 && // don't nuke yourself
game.getPicPoints() > 0) { // shortcircuit eval makes sure doesn't waste energy
game.takePic();
}
else {
mathVecSubtract(facing, other, me, 3);
api.setAttitudeTarget(facing); // always face toward the other
}
break;
case UPLOAD:
api.setAttitudeTarget(earth);
if (game.getEnergy() > 1 && checkUpload(me)) {
game.uploadPics();
}
break;
case STOP:
api.setAttRateTarget(origin);
break;
}
switch (movestate) {
case GET_ITEM: // item + try to point to the other sphere
if (game.hasItem(targetID) == -1) {
if (areWeThereYet(items[targetID], me)) {
api.setAttRateTarget(earth);
}
else {
moveTo(items[targetID]);
}
}
else {
moveTo(items[targetID]);
}
break;
case GET_ENERGY:
if (game.hasItem(targetID) == -1) {
targetID = minDistEl(items, me, 0, 3);
if (areWeThereYet(items[targetID], me)) {
api.setAttRateTarget(earth);
}
else {
moveTo(items[targetID]);
}
}
else {
targetID = minDistEl(items, me, 3, 7);
movestate = GET_ITEM;
}
break;
//case GET_MIRROR: // get mirror // deprecated for now
// if (game.hasItem(targetID) == -1) {
// targetID = minDistEl(items, me, 7, 9);
// if (areWeThereYet(items[targetID], me)) {
// api.setAttRateTarget(earth);
// }
// else {
// moveTo(items[targetID]);
// }
// }
// else {
// targetID = minDistEl(items, me, 3, 7);
// movestate = GET_ITEM;
// }
// break;
case ON_TOP:
getAbove(other, -0.4);
break;
case STOP: // if we have extremely low energy
api.setVelocityTarget(origin);
break;
case TO_ORIGIN:
moveTo(origin);
break;
}
}
}
/* Gets above other at certain altitude
* If other is already there, mimic across the x-axis
* Unless out of bounds, in which case stay near 0
*/
void getAbove(float other[], float altitude) {
memcpy(target, other, 3 * sizeof(float));
target[2] = altitude;
if (distance(target, other) < 0.5) {
target[0] = -other[0];
}
if (!inBounds(other)) {
target[2] = 0;
target[1] = 0;
target[0] = 0;
}
moveTo(target);
}
/* Updates:
* me
* other
* itemPossession
*/
void updateGameState() {
api.getMyZRState(me);
api.getOtherZRState(other);
for (n = 0 ; n < 9 ; n++) {
itemPossession[n] = game.hasItem(n);
}
}
/* State changing function that changes state under a variety of
* circumstances, more detail follows in code
*/
void changeState() {
// first determine rotatestate
if (game.getCurrentTime() > 170 || // if we are out of time
game.getFuelRemaining() < 10 || // if we are out of fuel
game.getMemoryFilled() == 2) { // or if memory is full
rotatestate = UPLOAD;
}
else if (rotatestate == UPLOAD && game.getMemoryFilled() == 0) {
rotatestate = NORMAL;
}
else if (rotatestate == STOP) { // reset after low-energy
rotatestate = NORMAL;
}
// then movestate
if (movestate == GET_ITEM) {
targetID = minDistEl(items, me, 3, 7);
if (targetID == -1) { // if there is no avaliable item
movestate = ON_TOP;
}
}
else if (movestate == STOP){
movestate = GET_ITEM;
}
else if (movestate == ON_TOP && !inBounds(other)) { // don't follow the other out of bounds
movestate = STOP;
}
if (game.getCurrentTime() > 177) {
movestate = TO_ORIGIN; //win in the end
}
// finally energy concerns
if (game.getEnergy() < 1) {
movestate = STOP;
rotatestate = STOP;
}
}
/* Checks if we are at target
* Returns true if distance < 0.05 and velocity < 0.01
*/
bool areWeThereYet(float target[], float me[]) {
memcpy(temp, me + 3, 3 * sizeof(float));
return (distance(target, me) < 0.05 && mathVecMagnitude(temp, 3) < 0.01);
}
/* Simple distance function, returns distance between p1 and p2 */
float distance(float p1[], float p2[]){
mathVecSubtract(temp, p1, p2, 3);
return mathVecMagnitude(temp, 3);
}
/* Figures out the index of an element of a 2D array that is closest to me
* Arguments: 2D array, me, beginning index (inclusive), ending index
* (exclusive)
*
* Returns the index of the element in the 2D array closest to me
* Returns -1 if no items are avaliable
*/
int minDistEl(float choices[][3], float me[], int beginning, int end) {
int result = -1;
float minDist = 101.0f;
for (n = beginning ; n < end ; n++) {
if (itemPossession[n] == -1 && distance(choices[n], me) < minDist) {
minDist = distance(choices[n], me);
result = n;
}
}
if (minDist > 100) {
return -1;
}
return result;
}
/* Figures out a pair of items,
*/
void minDistPair(float path[2][3], float choices[][3]) {
}
/* Young's magical function that checks if we can upload in the current
* facing
* TODO: how does this work...
*/
bool checkUpload( float me[] ){
if( ( me[8] > 0.9689f ) && (mathVecMagnitude(me+9,3) < 0.05f) )
return true;
return false;
}
/* bounds checking function
* returns true if location given is within bounds
* false otherwise
*/
bool inBounds(float loc[]) {
return abs(loc[0]) < 0.64 &&
abs(loc[1]) < 0.8 &&
abs(loc[2]) < 0.64;
}
/* Shaumik's better setPositionTarget function
* NOTE: THIS IS INTENDED TO BE USED GOING TO A STATIC POINT
* USING IT TO MOVE TO A CONSTANTLY CHANGING POINT MAY MESS IT UP!!!!
*/
void moveTo(float target[]) {
float disp[3];
float dist, speed;
mathVecSubtract(disp, target, me, 3);
dist = mathVecNormalize(disp, 3); //disp normalized to unit vector, holds direction of desired velocity
/* Condition here is based off of equation: d = (1/2)at^2 + vt
* a = 0.01, which is the approx maximum acceleration I have experimentally found of a SPHERE
* I assume t = 8s, which works and is faster then api.setPositionTarget
* You can change t if there is an overshoot
*/
if (dist > 0.5*0.01*64+mathVecMagnitude(me+3,3)*8) {
speed = dist;
for(n = 0; n < 3; n++) { //scale velocity (disp) to speed
disp[n] *= speed;
}
api.setVelocityTarget(disp);
}
else {
api.setPositionTarget(target);
}
}