-
Notifications
You must be signed in to change notification settings - Fork 0
/
OB_3.0_Vinay
332 lines (284 loc) · 8.24 KB
/
OB_3.0_Vinay
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
#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 unsigned char state; //DECL::VAR::state
static unsigned char SphereNumber; //DECL::VAR::SphereNumber
static char action; //DECL::VAR::action
static char switchTimer; //DECL::VAR::switchTimer
static char cooloff; //DECL::VAR::cooloff
static float Asteroid[3]; //DECL::VAR::Asteroid
static char op; //DECL::VAR::op
static char y0received; //DECL::VAR::y0received
static int lastMessage; //DECL::VAR::lastMessage
static int gotToTangent; //DECL::VAR::gotToTangent
static float Station[3]; //DECL::VAR::Station
static void shoot (float myState[12], float target[3], unsigned int fire); //DECL::PROC::shoot
static void tangent (float *v, float r, float myState[3], char ccw); //DECL::PROC::tangent
void ZRUser01(float *myState, float *otherState, float time)
{
//BEGIN::PROC::ZRUser
#define thetastep dist
#define theta mag
#define dirs Laser
float opulens[3] = {0.0, -0.35, -0.2};
float opulens_spin_point[3] = {0.04, -0.35, -0.2};
float Laser[3] = {0.4, 0.0, 0.0};
float difference[3], target[3];
float dist, mag;
if(!time)
{
SphereNumber = !!(myState[0] < 0) + 1;
}
Laser[0] = ((SphereNumber == 1) - (PotherHasLaser() == SphereNumber)) ? 0.4 : -0.4;
if((int)PgetMessage())
lastMessage = (int)PgetMessage();
switch(lastMessage)
{
case 2:
case 3:
if((time < 20) && !y0received && (time))
op = 1;
break;
case 4:
case 5:
if((time < 20) && !y0received && (time))
op = 0;
break;
default:
if(((int)PgetMessage() & 0x0400) && (time < 40)) // y0b0tics: both of us are fully capable, so SPH1 orbits, SPH2 spins
{
op = 1;
action = SphereNumber % 2;
y0received = 1;
}
else if(((int)PgetMessage() & 0x0200) && (time < 40)) // y0b0tics: they're not as capable, so we orbit
// not specific enough, delta mk2 should tell us exactly what they can/cannot do
{
op = 1;
action = 1;
y0received = 1;
}
break;
}
if(!time || y0received)
PsendMessage(0x0400); // y0b0tics: "I can do everything"
else if(time == 1)
PsendMessage((unsigned short)(3)); // I will orbit on Opulens
else
PsendMessage((unsigned short)(4 - (2 * op) + action));
Asteroid[1] = (op) ? -0.35 : 0.35;
Asteroid[2] = (op) ? -0.2 : 0.2;
if(!state){
ZRSetPositionTarget(Laser);
shoot(myState, opulens, 0);
if(PhaveLaser() || PgetPhase() == 2)
{
state = 1;
}
}
else{
if(switchTimer == 5)
{
if(time < 130)
{
action = 0;
}
switchTimer = 10;
cooloff = 8;
}
if(switchTimer == 15)
{
if(time < 130)
{
action = 1;
}
switchTimer = 0;
cooloff = 8;
}
if(cooloff)
{
cooloff--;
}
if(action)
{
if(!cooloff)
{
mathVecSubtract(difference, Asteroid, otherState, 3);
mathVecNormalize(difference, 3);
if((PisRevolving(otherState) == 2 - op) && (mathVecInner(&otherState[3], difference, 3) < (0.9 * mathVecMagnitude(&otherState[3], 3))))
{
if(switchTimer < 5 && switchTimer >= 0)
{
switchTimer++;
}
else
{
switchTimer = 1;
}
}
else
{
switchTimer = 0;
}
}
mathVecSubtract(difference, myState, Asteroid, 3);
if((!PiceMelted()) && op)
{
shoot(myState, opulens, (PgetPhase() > 1));
}
if (fabsf(mathVecMagnitude(difference, 3) - 0.35) < 0.01)
{
gotToTangent = 1;
DEBUG(("Got to tangent!\n"));
/*
theta = atan2f(difference[1],difference[0]);
thetastep = 0.070996444 * ((SphereNumber * 2) - 3); // TAU / 88.5, zach's magic value
theta += thetastep;
difference[0] = .45 * sinf(PI/2 - theta);
difference[1] = .45 * sinf(theta);
mathVecAdd(target, Asteroid, difference, 3); // target is new position
mathVecSubtract(difference, target, myState, 3); // difference points from myState to target
ZRSetVelocityTarget(difference);
*/
}
else
{
tangent(target, .35, myState, SphereNumber % 2);
DEBUG(("target: %.2f, %.2f, %.2f\n", target[0], target[1], target[2]));
ZRSetPositionTarget(target);
}
}
else
{
if(!cooloff)
{
mathVecSubtract(difference, otherState, Asteroid, 3);
if(!(PisRevolving(otherState) == 2 - op))
{
if(switchTimer < 15 && switchTimer >= 10)
{
switchTimer++;
}
else
{
switchTimer = 11;
}
}
else
{
switchTimer = 10;
}
}
if(!(PiceMelted()) && op)
{
opulens_spin_point[0] = (SphereNumber == 1) ? 0.04 : -0.04;
ZRSetPositionTarget(opulens_spin_point);
shoot(myState, opulens, (PgetPhase() > 1));
return;
}
dirs[2] = 1.872459067 + atan2f(myState[7], myState[6]);
dirs[0] = sinf(PI/2 - dirs[2]);
dirs[1] = sinf(dirs[2]);
dirs[2] = 0;
ZRSetAttitudeTarget(dirs);
ZRSetPositionTarget(Asteroid);
}
}
//END::PROC::ZRUser
}
void ZRInit01()
{
//BEGIN::PROC::ZRInit
state = 0;
SphereNumber = 0;
action = 1;
switchTimer = 0;
cooloff = 0;
memset(Asteroid,0,sizeof(float)*3);
op = 1;
y0received = 0;
lastMessage = 0;
gotToTangent = 0;
memset(Station,0,sizeof(float)*3);
//END::PROC::ZRInit
}
//User-defined procedures
static void shoot (float myState[12], float target[3], unsigned int fire)
{
//BEGIN::PROC::shoot
float direction[3];
float new_pos[3];
// new_pos: predict my new position in 2 seconds
mathVecAdd(new_pos, &myState[3], &myState[3], 3);
mathVecAdd(new_pos, myState, new_pos, 3);
// direction: where should I aim in the future
mathVecSubtract(direction, target, new_pos, 3);
mathVecNormalize(direction, 3);
ZRSetAttitudeTarget(direction);
// direction: where should I be pointing NOW
mathVecSubtract(direction, target, myState, 3);
mathVecNormalize(direction, 3);
// shoot if I "see" the target
if (fire && (mathVecInner(&myState[6], direction, 3) / mathVecMagnitude(&myState[6], 3)) > (0.995))
Plaser();
//END::PROC::shoot
}
static void tangent (float *v, float r, float myState[3], char ccw)
{
//BEGIN::PROC::tangent
#define cross norm
#define s dist
#define n norm
#define to_rotate diff
#define rotated proj
float diff[3], proj[3], norm[3], c, dist;
PgetAsteroidNormal(norm);
DEBUG(("Normal: %.2f, %.2f, %.2f\n", n[0], n[1], n[2]));
//Find signed distance to plane determined by asteroid normal:
mathVecSubtract(diff, myState, Asteroid, 3);
dist = mathVecInner(diff, norm, 3);
//Find projection of position relative to asteroid center onto asteroid normal:
proj[0] = norm[0] * dist;
proj[1] = norm[1] * dist;
proj[2] = norm[2] * dist;
//Find projection of position onto affine subspace determined by asteroid normal:
mathVecSubtract(proj, myState, proj, 3);
//Now there exist two tangent points to the orbit path.
//Find distance to asteroid:
mathVecSubtract(diff, proj, Asteroid, 3);
dist = mathVecMagnitude(diff, 3);
/*
/ Rotate vector pointing from asteroid center to projection of position in plane
/ of revolution (to_rotate) with respect to asteroid normal by angle whose sine is
/ r/dist:
*/
// Makes to_rotate have magnitude r. (Already points in correct direction.)
to_rotate[0] *= r / dist;
to_rotate[1] *= r / dist;
to_rotate[2] *= r / dist;
// If projection is inside orbit, go to closest point.
if(dist <= r){
mathVecAdd(v, Asteroid, to_rotate, 3);
return;
}
s = r / dist * (2 * ccw - 1);
c = sqrt(1 - s * s);
rotated[0] = (c + n[0] * n[0] * (1 - c)) * to_rotate[0]
+ (n[0] * n[1] * (1 - c) - n[2] * s) * to_rotate[1]
+ (n[0] * n[2] * (1 - c) + n[1] * s) * to_rotate[2];
rotated[1] = (n[1] * n[0] * (1 - c) + n[2] * s) * to_rotate[0]
+ (c + n[1] * n[1] * (1 - c)) * to_rotate[1]
+ (n[1] * n[2] * (1 - c) - n[0] * s) * to_rotate[2];
rotated[2] = (n[2] * n[0] * (1 - c) - n[1] * s) * to_rotate[0]
+ (n[2] * n[1] * (1 - c) + n[0] * s) * to_rotate[1]
+ (c + n[2] * n[2] * (1 - c)) * to_rotate[2];
mathVecAdd(v, Asteroid, rotated, 3);
//END::PROC::tangent
}