-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtibbeCrobot.c
331 lines (294 loc) · 9.72 KB
/
tibbeCrobot.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
/* tibbe */
/* strategy: move like hell and avoid everyone, shooot */
/* Assume everything is in meters */
/* The battlefield of the walls normal 0-1000 meters - asume its a square in meters
The battlefield is a 1,000 by 1,000 meter square.
The lower left corner has the coordinates x = 0, y = 0; the upper
right corner has the coordinated x = 999, y = 999.
A wall
surrounds the perimeter, so that a robot running into the wall
will incur damage.
The scanner is an optical device that can instantly scan any
chosen heading, 0-359. The scanner has a maximum resolution of
+/- 10 degrees. This enables the robot to quickly scan the field
at a low resolution, then use maximum resolution to pinpoint an
opponent.
The scan() function invokes the robot's scanner, at a specified
degree and resolution. scan() returns 0 if no robots are
within the scan range or a positive integer representing the
range to the closest robot. Degree should be within the range
0-359, otherwise degree is forced into 0-359 by a modulo 360
operation, and made positive if necessary. Resolution controls
the scanner's sensing resolution, up to +/- 10 degrees.
Examples:
range = scan(45,0); scan 45, with no variance
range = scan(365,10); scans the range from 355 to 15
The direction of the cannon and tank in 0-360 degrees
The compass system is oriented so that due east (right) is 0
degrees, 90 is north, 180 is west, 270 is south. One degree
below due east is 359.
Q2 135 90 45 Q1
\ | /
\ | /
180 --- x --- 0
/ | \
/ | \
Q3 225 270 315 Q4
*/
int
tnkDir, /* the direction of the tank in 0-360 degrees */
cOpAng, /* Current Angle of opponent */
scnDist, /* Scan distance returned from scan 0 if not found max 700 for cannon reach */
tnkWall, /* Are we inside wall zone? */
tnkTurn, /* have the tank turned? */
inZone, /* Is the tank in the wall zone */
fireOk, /* if it's ok to fire */
pOpAng, /* Prevoius angle of opponent */
pScnDist; /* prevoius Scan distance returned from scan 0 if not found max 700 for cannon reach */
/*int tnkFlag; what does the tank think about in a binary 8 bit,
0 0 0 0 0 0 0 value
7 6 5 4 3 2 1 bit number
bit 1 = Turn the tank
bit 2 = Turning
*/
/*--------------------------------------------------------------------------------------------*/
/*
TEMP CODE PASTE PLACE!!
*/
/* main loop */
main()
{
/*--------------------------------------------------------------------------------------------*/
/* Init values not needed right now as we do most static to minimize */
/*
max missile Range = 700;
max field size = 999;
min field size = 0;
Bound box for tank max = 900;
Bound box for tank min = 100;
aritmic scale for atan = 100000;
tank max power (speed) = 100;
tank Thought (future relase) = 0;
*/
fireOk = 1;
/* scnStat = 0; First value 0 for not found and init */
if (loc_y() > loc_x()) drive((tnkDir = rand(80) + 5 - (loc_x()/11) - (loc_y()/11)),100);
else drive((tnkDir = rand(80) + 5 + (loc_x()/11) + (loc_y()/11)),100);
cOpAng = tnkDir; /* set current opponent angle first time */
DoScnLo(1); /* do a low scan to find him again with first scan enable */
DoScnMd(1); /* do a medium scan with first scan enable */
pOpAng = cOpAng; pScnDist = scnDist; /* uppdatera föregående värden */
/* End of one time program and start of loop */
/*--------------------------------------------------------------------------------------------*/
while(1) {
/* ----------------------- MAIN Logic for Wall Avoidance -------------------------- */
if (fireOk) { /* if it's ok to fire. */
/* ----------------------- MAIN Logic for Fireing -------------------------- */
if (DoScnHi()) { /* do a high scan to get a fire solution */
if (scnDist < 710 && scnDist > 100) {
cannon(cOpAng - ((pOpAng - cOpAng)/2),scnDist - ((pScnDist - scnDist)/2));
}
} /* ----------------------- MAIN Logic for finding opponent again -------------------------- */
else if (DoScnMd(0)) { /* do a medium scan with first scan disable */
}
else if (DoScnLo(0) && !tnkWall) { /* do a low scan to find him again with first scan disable */
if (DoScnMd(1) && !tnkWall) { /* do a medium scan with first scan enable */
}
}
else if (DoScnLo(1) && !tnkWall) { /* do a low scan to find him again with first scan enable */
if (DoScnMd(1) && !tnkWall) { /* do a medium scan with first scan enable */
}
}
} /* End of Fire ok scenario */
if (tnkWall) { /* have we hit a wall and should start turning */
if (tnkTurn) { /* should we start turning (only once) */
if (loc_y() > loc_x()) (tnkDir = rand(80) + 5 - (loc_x()/11) - (loc_y()/11)); /* calc a new turn based on the x & y position of the tank., 11 is to get about 90 degree from position */
else (tnkDir = rand(80) + 5 + (loc_x()/11) + (loc_y()/11)); /* calc a new turn based on the x & y position of the tank. flip if y<x., 11 is to get about 90 degree from position */
tnkTurn = 0; /* don't turn anymore */
}
if (speed() < 50) {
drive(tnkDir,100); /* if the speed is below 50, full speed ahead in new direction*/
tnkWall = 0;
fireOk = 1;
}
} /* end wall scenario */
else { /* if not in a wall scenario */
pOpAng = cOpAng; pScnDist = scnDist; /* uppdatera föregående värden */
if (loc_x() > 900 || loc_y() > 900 || loc_x() < 100 || loc_y() < 100) { /* check if we get into the wall zone again */
if (!inZone) { /* if we are not in the zone still, from previous turn */
tnkWall = 1; tnkTurn = 1; fireOk = 0; inZone = 1; /* start the wall scenario and turn */
drive(tnkDir,40); /* slow down to turn speed */
}
}
else { /* we are not in the wall zone anymore */
inZone = 0;
}
}
} /* End of while() */
} /* End of main() */
/*--------------------------------------------------------------------------------------------*/
/* End of program and start of functions */
/*--------------------------------------------------------------------------------------------*/
/* DoScnLo around with max 20 degree jumps starting from startDir up to 170 degree, 10 degree resolution */
DoScnLo(frstScn) {
if (frstScn) {
if (( scnDist = scan(cOpAng, 10))) { /* 10 degree resolution scan 10 + | + 10 */
return 1;
}
if (( scnDist = scan(cOpAng + 10, 10))) {
cOpAng += 10;
return 1;
}
if (( scnDist = scan(cOpAng - 10, 10))) {
cOpAng -= 10;
return 1;
}
if (( scnDist = scan(cOpAng + 30, 10))) {
cOpAng += 30;
return 1;
}
if (( scnDist = scan(cOpAng - 30, 10))) {
cOpAng -= 30;
return 1;
}
}
if (( scnDist = scan(cOpAng + 50, 10))) {
cOpAng += 50;
return 1;
}
if (( scnDist = scan(cOpAng - 50, 10))) {
cOpAng -= 50;
return 1;
}
if (( scnDist = scan(cOpAng + 70, 10))) {
cOpAng += 70;
return 1;
}
if (( scnDist = scan(cOpAng - 70, 10))) {
cOpAng -= 70;
return 1;
}
if (( scnDist = scan(cOpAng + 90, 10))) {
cOpAng += 90;
return 1;
}
if (( scnDist = scan(cOpAng - 90, 10))) {
cOpAng -= 90;
return 1;
}
if (( scnDist = scan(cOpAng + 110, 10))) {
cOpAng += 110;
return 1;
}
if (( scnDist = scan(cOpAng - 110, 10))) {
cOpAng -= 110;
return 1;
}
if (( scnDist = scan(cOpAng + 130, 10))) {
cOpAng += 130;
return 1;
}
if (( scnDist = scan(cOpAng - 130, 10))) {
cOpAng -= 130;
return 1;
}
if (( scnDist = scan(cOpAng + 150, 10))) {
cOpAng += 150;
return 1;
}
if (( scnDist = scan(cOpAng - 150, 10))) {
cOpAng -= 150;
return 1;
}
if (( scnDist = scan(cOpAng + 170, 10))) {
cOpAng += 170;
return 1;
}
if (( scnDist = scan(cOpAng - 170, 10))) {
cOpAng -= 170;
return 1;
}
return 0;
}
/*--------------------------------------------------------------------------------------------*/
/* DoScnMe around with max 10 degree jumps starting from startDir up to 40 degree, 5 degree resolution */
DoScnMd(frstScn) {
if (frstScn) {
if (( scnDist = scan(cOpAng, 5))) { /* 5 degree resolution scan 5 + | + 5 */
return 1;
}
if (( scnDist = scan(cOpAng + 10, 5))) {
cOpAng += 10;
return 1;
}
if (( scnDist = scan(cOpAng - 10, 5))) {
cOpAng -= 10;
return 1;
}
if (( scnDist = scan(cOpAng + 20, 5))) {
cOpAng += 20;
return 1;
}
if (( scnDist = scan(cOpAng - 20, 5))) {
cOpAng -= 20;
return 1;
}
}
if (( scnDist = scan(cOpAng + 30, 5))) {
cOpAng += 30;
return 1;
}
if (( scnDist = scan(cOpAng - 30, 5))) {
cOpAng -= 30;
return 1;
}
if (( scnDist = scan(cOpAng + 40, 5))) {
cOpAng += 40;
return 1;
}
if (( scnDist = scan(cOpAng - 40, 5))) {
cOpAng -= 40;
return 1;
}
return 0;
}
/*--------------------------------------------------------------------------------------------*/
/* DoScnHi around with min 4 degree jumps starting from startDir up to 12 degree, 2 degree resolution */
DoScnHi() {
if (( scnDist = scan(cOpAng, 3))) { /* 2 degree resolution scan 2 + | + 2 */
return 1;
}
if (( scnDist = scan(cOpAng + 6, 3))) {
cOpAng += 6;
return 1;
}
if (( scnDist = scan(cOpAng - 6, 3))) {
cOpAng -= 6;
return 1;
}
if (( scnDist = scan(cOpAng + 12, 3))) {
cOpAng += 12;
return 1;
}
if (( scnDist = scan(cOpAng - 12, 3))) {
cOpAng -= 12;
return 1;
}
if (( scnDist = scan(cOpAng + 18, 3))) {
cOpAng += 18;
return 1;
}
if (( scnDist = scan(cOpAng - 18, 3))) {
cOpAng -= 18;
return 1;
}
if (( scnDist = scan(cOpAng + 24, 3))) {
cOpAng += 24;
return 1;
}
if (( scnDist = scan(cOpAng - 24, 3))) {
cOpAng -= 24;
return 1;
}
return 0;
}