/
finals.c
580 lines (538 loc) · 12.9 KB
/
finals.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/*
*
* Written by:
* Flavio 'darkjoker' Giobergia
* Marco 'The Nano' Pomponio
*
*/
#include <math.h>
#include <string.h>
#include "ZR_API.h"
#include "ZRGame.h"
#include "math_matrix.h"
#include "spheres_constants.h"
#ifdef ZRSIMULATION
extern void _init(void);
void *_start = &_init;
#endif
static int st; //DECL::VAR::st
static float a; //DECL::VAR::a
static float tmp; //DECL::VAR::tmp
static char ok; //DECL::VAR::ok
static int act; //DECL::VAR::act
static char ast; //DECL::VAR::ast
static char sp; //DECL::VAR::sp
static float checkTarget (float other[12], int n); //DECL::PROC::checkTarget
static void mathVecMul (float *v, float *a, float n); //DECL::PROC::mathVecMul
static void funz (float a[3], float q[4], float v[3]); //DECL::PROC::funz
static void SetPosFaster (float c[3], float state[12]); //DECL::PROC::SetPosFaster
static int getInTime (float cother[3], float other[12], float c[3], float state[12]); //DECL::PROC::getInTime
static void spin (float c[3], float mystate[12]); //DECL::PROC::spin
static int revDir (float state[12], float c[3]); //DECL::PROC::revDir
static void meltIce (float state[12], float other[12], float time); //DECL::PROC::meltIce
static float timeStation (float c[3], float state[12]); //DECL::PROC::timeStation
static void revolve (float c[3], float mystate[12], float other[12]); //DECL::PROC::revolve
void ZRUser01(float *myState, float *otherState, float time)
{
//BEGIN::PROC::ZRUser
// These are just some costants, don't pay to much attention to them :)
#define WAIT_TIME 13 // Seconds to wait before starting spinning
#define TIME_OPP_STAT 140 // Will only start considering about going
// to a station after TIME_OPP_STAT seconds.
#define ACCURACY_STAT 1.975f // Number between 1 and 2, the higher it is,
// the more accurate in deciding whether the
// opponent is headed towards the station.
#define FUEL_STAT 25 // The minimum amount of fuel required to go
// to a mining station (below this value,
// the sphere's not going anywhere
// msg : contains the messages received from the opponent
// tt : contains a condition used a couple of times (space optimization)
// out : contains the message we're sending our opponent
unsigned short msg,tt,out=0;
// avoid: is true or false whether the collision avoidance system is activated or not
unsigned char avoid;
// Don't worry about x and v ^^
float x[2];
float v[3];
// stat : these are some coordinates of stuff, like stations, asteroids and
// a couple of corners of the playground
float stat[6][3] = { {-0.5f,+0.31f,-0.55f},
{+0.5f,-0.31f,+0.55f},
{0,-0.35f,-0.2f},
{0,+0.35f,+0.2f},
{-0.5f,+0.65f,-0.55f},
{+0.5f,-0.65f,+0.55f}};
msg=PgetMessage();
avoid = PisAvoidingCollision();
tt=(tmp && tmp+WAIT_TIME<time);
/*
* y0b0tics protocol will be used only when
* we're controlling the SPH # 1
*/
if (!time) {
if (myState[0]>0) { // SPH1
a=0.4; // a is the X coordinate of the nearest laser
out=0x0400; // bit 11
sp=1;
}
else
a=-0.4;
}
/*
* If the opponent is willing to work
* on Indigens, we're going to Indigens
* as well, but we'll try to revolve.
* Otherwise we keep sending our
* opponent a "I'll revolve on Opulens"
* message.
*/
if (time<60 && time) {
if ((msg>3 && msg<6) || ast==1) {
out=5;
act=2; // Revolve
ast=1; // Indigens
}
else {
out=3;
}
}
/*
* If we're headed to a mining station,
* we're telling our opponents which one
* it is
*/
else if (act==4 && st<2)
out=7-st;
PsendMessage(out);
/*
* First of all, the Laser is taken
* (btw, there's a "minor" bug in this
* condition (&& time<60) is needed
* (will add that as soon as some space
* is available)
*/
if (!PhaveLaser()) {
/*
* If the opponent is trying to "steal"
* our laser, we will get the other one
*/
if (avoid && myState[0]*a>0)
a*=-1;
/* v contains the
* laser coordinates
*/
v[0]=a;
v[1]=0;
v[2]=0;
ZRSetPositionTarget(v);
}
else {
if (time>TIME_OPP_STAT && st<2) {
/*
* x[0] is given the value of the distance
* between us and our opponent
*/
mathVecSubtract (v,myState,otherState,3);
x[0]=mathVecMagnitude (v,3);
/*
* if our opponent is going to a station
* we decide to go to the other one, but
* only if we're able to arrive first
*/
if (checkTarget(otherState,0)>ACCURACY_STAT) {
/*
* Even if we're headed towards a station,
* if our opponent is going to
* the same station as us, we stop going
* there and, instead, we move to the
* closest corner of the playground,
* so that, even if we don't gain anything,
* at least we won't activate the collision
* avoidance system
*/
if (!st && x[0]<0.65f) {
st=4;
}
else if (st==-1) {
/*
* We go to the other station only if
* we can reach it first
*/
if (getInTime(stat[0],otherState,stat[1],myState)) {
st=1;
act=4;
}
else
st=-2;
}
}
/*
* Same stuff as before, but now
* the other station is checked.
*/
else if (checkTarget(otherState,1)>ACCURACY_STAT) {
if (st==1 && x[0]<0.65f) {
st=5;
}
else if (st==-1) {
if (getInTime(stat[1],otherState,stat[0],myState)) {
st=0;
act=4;
}
else
st=-2;
}
}
/*
* although our opponent is not going to
* a station, we leave the asteroid we're
* working on to go to a station.
* The time we're leaving the asteroid is
* not fixed, but is calculated every time
* in order to leave the asteroid as late as
* possible
*/
else if (PgetPercentFuelRemaining()>FUEL_STAT && st==-1) {
/*
* x[0] contains the value of time
* at which we have to leave the asteroid
* to reach a station
* 180-(time needed to reach a station)
*/
x[0]=timeStation(stat[0],myState);
x[1]=timeStation(stat[1],myState);
/*
* It is then decided which station is
* better, keeping in account that, if
* we're spinning, our opponent could be
* on our way to the station.
*/
if ((time>=x[0]) && (x[0]>x[1]) && !(act==3 && otherState[0]<0 && otherState[1]>0 && otherState[2]<0.2)) {
act=4;
st=0;
}
if ((x[0]<x[1]) && (time>=x[1]) && !(act==3 && otherState[0]>0 && otherState[1]<0 && otherState[2]>-0.2)) {
act=4;
st=1;
}
}
}
/*
* act (action) contains a value
* related to the action we're
* doing.
*/
switch (act) {
/* act = 1 -> melting ice on Opulens */
case 1:
meltIce(myState,otherState,time);
if (PiceMelted())
act=2; // if the ice is melted, we start revolving
// on Opulens
break;
/* act = 2 -> revolve */
case 2:
/*
* if the opponent is revolving as
* well, we keep revolving as well
* for WAIT_TIME seconds. After that
* seconds, if the opponent is
* still revolving, we start spinning
*/
if (PisRevolving (otherState)==ast-1) {
if (!tmp)
tmp=time;
if (tt && (checkTarget (otherState,ast)<1.9f) && st!=-2) {
act=3;
tmp=0;
}
}
revolve(stat[ast],myState,otherState);
break;
/* act = 3 -> spinning */
case 3:
/*
* if the avoiding collision system
* is working, it probably means that
* our opponent wants to spin and he's
* close to us so, after a few seconds,
* we let him spin and we start revolving ^^
*/
if (avoid) {
if (!tmp)
tmp=time;
if (tt) {
tmp=0;
act=2;
}
}
spin(stat[ast],myState);
break;
/* act = 4 -> going somewhere */
case 4:
SetPosFaster(stat[st],myState);
break;
default:
break;
}
if (tt)
tmp=0;
}
//END::PROC::ZRUser
}
void ZRInit01()
{
//BEGIN::PROC::ZRInit
st = -1;
a = 0.0f;
tmp = 0;
ok = 0;
act = 1;
ast = 2;
sp = -1;
//END::PROC::ZRInit
}
//User-defined procedures
static float checkTarget (float other[12], int n)
{
//BEGIN::PROC::checkTarget
/*
* checkTarget returns a value
* between 1 and 2: an high
* value means that the player
* "other" is headed towards
* the point "c".
*/
float v[3]={0,0,0};
float a[3];
float c[4][3]= { {-0.5f,+0.31f,-0.55f},
{+0.5f,-0.31f,+0.55f},
{0,-0.35f,-0.2f},
{0,0.35f,0.2f}
};
mathVecAdd (v,v,other+3,3);
mathVecSubtract (a,c[n],other,3);
mathVecNormalize(v, 3);
mathVecNormalize(a, 3);
mathVecAdd (a,a,v,3);
return mathVecMagnitude (a,3);
//END::PROC::checkTarget
}
static void mathVecMul (float *v, float *a, float n)
{
//BEGIN::PROC::mathVecMul
/*
* The code is self-explanatory
*/
int i;
for (i=0;i<3;i++)
v[i]=a[i]*n;
//END::PROC::mathVecMul
}
static void funz (float a[3], float q[4], float v[3])
{
//BEGIN::PROC::funz
/*
* Magic happens here :)
*/
a[0] = q[0] * v[3] + q[1] * v[2] - q[2] * v[1] + q[3] * v[0];
a[1] = -q[0] * v[2] + q[1] * v[3] + q[2] * v[0] + q[3] * v[1];
a[2] = q[0] * v[1] - q[1] * v[0] + q[2] * v[3] + q[3] * v[2];
a[3] = -q[0] * v[0] - q[1] * v[1] - q[2] * v[2] + q[3] * v[3];
//END::PROC::funz
}
static void SetPosFaster (float c[3], float state[12])
{
//BEGIN::PROC::SetPosFaster
/*
* SetPosFaster, as the name says,
* is used to reach a point in
* a shorter amount of seconds
* than ZRSetPositionTarget ().
*/
float v[3];
mathVecSubtract(v,c,state,3);
mathVecMul (v,v,1.315f);
mathVecAdd (v,v,state,3);
ZRSetPositionTarget(v);
//END::PROC::SetPosFaster
}
static int getInTime (float cother[3], float other[12], float c[3], float state[12])
{
//BEGIN::PROC::getInTime
/*
* getInTime () returns a boolean
* value, which will be false if our
* opponent is able to reach his mining
* station before us, true otherwise
*/
float targetother[3];
float target[3];
float v1[3];
float v2[3];
mathVecSubtract(targetother,cother,other,3);
mathVecSubtract(target,c,state,3);
mathVecMul (other+3,other+3,2.05f);
mathVecMul (state+3,other+3,2.05f);
mathVecSubtract (v1,targetother,other+3,3);
mathVecSubtract (v2,target,state+3,3);
return ((mathVecMagnitude(v1, 3)+ 0.14f)>(mathVecMagnitude(v2, 3)));
//END::PROC::getInTime
}
static void spin (float c[3], float mystate[12])
{
//BEGIN::PROC::spin
/*
* mmh.. Do I really need to
* exaplain what this function
* is for?
*/
float v[3];
char i;
float cosalfa=0;
if (ok<3) {
PgetAsteroidNormal(v);
for(i=0;i<3;i++)
cosalfa+=v[i]*mystate[i+6];
if(cosalfa<0)
mathVecMul (v,v,-1);
ZRSetAttitudeTarget(v);
if (fabs(cosalfa)>0.986f)
ok++;
}
else {
v[0]=(0.5236f - mystate[9])/50;
v[1]=0;
v[2]=0;
ZRSetTorques(v);
}
ZRSetPositionTarget(c);
//END::PROC::spin
}
static int revDir (float state[12], float c[3])
{
//BEGIN::PROC::revDir
/*
* revDir () returns a value
* between -1, 0 and 1:
* if 0 is returned, our
* opponent is not revolving,
* otherwise the number returned
* is related to the direction
* of the revolving (clockwise
* or counterclockwise)
*/
float asse[3];
float direction[3];
float result[3];
float directionAsteroid[3];
PgetAsteroidNormal(asse);
mathVecCross(direction, state+3, asse);
mathVecNormalize(direction,3);
mathVecSubtract(directionAsteroid, c, state,3);
mathVecNormalize(directionAsteroid,3);
mathVecAdd(result, direction, directionAsteroid,3);
if(mathVecMagnitude(result, 3) > 1.85) {
return 1;
}
else if(mathVecMagnitude(result, 3) < 0.15){
return -1;
}
else
return 0;
//END::PROC::revDir
}
static void meltIce (float state[12], float other[12], float time)
{
//BEGIN::PROC::meltIce
/*
* meltIce melts the
* ice sheet on Opulens
* (no shit!)
*/
float x[3]={0,-0.35,-0.2};
if (PiceHits()+PotherIceHits()>18 || time<47) {
revolve (x,state,other);
}
else {
ZRSetPositionTarget (state);
mathVecSubtract(x,x,state,3);
ZRSetAttitudeTarget(x);
}
if (time>59)
Plaser();
//END::PROC::meltIce
}
static float timeStation (float c[3], float state[12])
{
//BEGIN::PROC::timeStation
/*
* timeStation return the second
* at which we can leave the asteroid
* to go to the mining station and
* reach it before the end of
* phase 3
*/
float target[3];
float v2[3],d;
mathVecSubtract(target,c,state,3);
mathVecMul (state+3,state+3,2.85);
mathVecSubtract (v2,target,state+3,3);
d=mathVecMagnitude (v2,3);
d*=26;
return 180-(d+6);
//END::PROC::timeStation
}
static void revolve (float c[3], float mystate[12], float other[12])
{
//BEGIN::PROC::revolve
/*
* Revolving function
*/
float v[4];
float q[4];
float a[4];
float a2[4];
float asse[3];
float d;
float b;
float b2;
int dir=sp;
if (PisRevolving (other)) {
dir = revDir (other,c);
if (!dir)
dir=sp;
}
PgetAsteroidNormal(asse);
v[3]=0;
mathVecSubtract (v,c,mystate,3);
d=mathVecMagnitude(v, 3);
if(d >= 0.3f) {
d=0.2355f/d; // (pi/2)*(0.3/distance)
}
else {
d= 1.57f - 2.615f*d; // pi - (pi/2)*(distance/0.25)
}
// This creates the quaternion
// (yeah, a quaternion, that's cool right?)
b=d-1.57f;
b2=b*b*b;
q[3]=(-b + b2/6 - b2*b*b/120);
b2=d*d*d;
mathVecMul (q,asse,dir*(d - b2/6 + b2*d*d/120));
// v[] is rotated according to the quaternion
funz(a2,q,v);
mathVecMul(q,q,-1);
funz(a,a2,q);
//then it's normalized and converted in velocity
mathVecNormalize(a, 3);
mathVecMul(a,a,0.0232f);
ZRSetVelocityTarget(a);
q[3]=1;
mathVecMul(q,asse,-dir*0.043619f);
funz(a2,q,v);
mathVecMul(q,q,-1);
funz(a,a2,q);
ZRSetAttitudeTarget(a);
//END::PROC::revolve
}