-
Notifications
You must be signed in to change notification settings - Fork 2
/
forftanks.c
589 lines (503 loc) · 13 KB
/
forftanks.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
581
582
583
584
585
586
587
588
589
/*
* This software has been authored by an employee or employees of Los
* Alamos National Security, LLC, operator of the Los Alamos National
* Laboratory (LANL) under Contract No. DE-AC52-06NA25396 with the U.S.
* Department of Energy. The U.S. Government has rights to use,
* reproduce, and distribute this software. The public may copy,
* distribute, prepare derivative works and publicly display this
* software without charge, provided that this Notice and any statement
* of authorship are reproduced on all copies. Neither the Government
* nor LANS makes any warranty, express or implied, or assumes any
* liability or responsibility for the use of this software. If
* software is modified to produce derivative works, such modified
* software should be clearly marked, so as not to confuse it with the
* version available from LANL.
*/
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "ctanks.h"
#include "forf.h"
#include "dump.h"
#define MAX_TANKS 50
#define ROUNDS 500
#define SPACING 150
#define LENV_SIZE 100
#define DSTACK_SIZE 200
#define CSTACK_SIZE 500
#define MEMORY_SIZE 10
struct forftank {
struct forf_env env;
int error_pos;
char color[8]; /* "#ff0088" */
char name[50];
char *path;
struct forf_stack _prog;
struct forf_value _progvals[CSTACK_SIZE];
struct forf_stack _cmd;
struct forf_value _cmdvals[CSTACK_SIZE];
struct forf_stack _data;
struct forf_value _datavals[DSTACK_SIZE];
struct forf_memory _mem;
long _memvals[MEMORY_SIZE];
};
#ifndef NODEBUG
void
forf_print_val(struct forf_value *val)
{
switch (val->type) {
case forf_type_number:
printf("%ld", val->v.i);
break;
case forf_type_proc:
printf("[proc %p]", val->v.p);
break;
case forf_type_stack_begin:
printf("{");
break;
case forf_type_stack_end:
printf("}");
break;
}
}
void
forf_print_stack(struct forf_stack *s)
{
size_t pos;
for (pos = 0; pos < s->top; pos += 1) {
forf_print_val(&(s->stack[pos]));
printf(" ");
}
}
void
forf_dump_stack(struct forf_stack *s)
{
printf("Stack at %p: ", s);
forf_print_stack(s);
printf("\n");
}
#endif
/*
*
* Forf API
*
*/
/** Has the turret recharged? */
void
forf_tank_fire_ready(struct forf_env *env)
{
struct tank *tank = (struct tank *)env->udata;
forf_push_num(env, tank_fire_ready(tank));
}
/** Fire! */
void
forf_tank_fire(struct forf_env *env)
{
struct tank *tank = (struct tank *)env->udata;
tank_fire(tank);
}
/** Set desired speed */
void
forf_tank_set_speed(struct forf_env *env)
{
struct tank *tank = (struct tank *)env->udata;
long right = forf_pop_num(env);
long left = forf_pop_num(env);
tank_set_speed(tank, left, right);
}
/** Get the current turret angle */
void
forf_tank_get_turret(struct forf_env *env)
{
struct tank *tank = (struct tank *)env->udata;
float angle = tank_get_turret(tank);
forf_push_num(env, rad2deg(angle));
}
/** Set the desired turret angle */
void
forf_tank_set_turret(struct forf_env *env)
{
struct tank *tank = (struct tank *)env->udata;
long angle = forf_pop_num(env);
tank_set_turret(tank, deg2rad(angle));
}
/** Is a sensor active? */
void
forf_tank_get_sensor(struct forf_env *env)
{
struct tank *tank = (struct tank *)env->udata;
long sensor_num = forf_pop_num(env);
forf_push_num(env, tank_get_sensor(tank, sensor_num));
}
/** Set the LED state */
void
forf_tank_set_led(struct forf_env *env)
{
struct tank *tank = (struct tank *)env->udata;
long active = forf_pop_num(env);
tank_set_led(tank, active);
}
/** Pick a random number */
void
forf_proc_random(struct forf_env *env)
{
long max = forf_pop_num(env);
if (max < 1) {
forf_push_num(env, 0);
return;
}
forf_push_num(env, rand() % max);
}
/* Tanks lexical environment */
struct forf_lexical_env tanks_lenv_addons[] = {
{"fire-ready?", forf_tank_fire_ready},
{"fire!", forf_tank_fire},
{"set-speed!", forf_tank_set_speed},
{"get-turret", forf_tank_get_turret},
{"set-turret!", forf_tank_set_turret},
{"sensor?", forf_tank_get_sensor},
{"set-led!", forf_tank_set_led},
{"random", forf_proc_random},
{NULL, NULL}
};
/*
*
* Filesystem stuff
*
*/
int
ft_read_file(char *ptr, size_t size, char *dir, char *fn)
{
char path[256];
FILE *f = NULL;
int ret;
int success = 0;
do {
snprintf(path, sizeof(path), "%s/%s", dir, fn);
f = fopen(path, "r");
if (! f) break;
ret = fread(ptr, 1, size - 1, f);
ptr[ret] = '\0';
if (! ret) break;
success = 1;
} while (0);
if (f) fclose(f);
if (! success) {
return 0;
}
return 1;
}
void
ft_bricked_tank(struct tank *tank, void *ignored)
{
/* Do nothing, the tank is comatose */
}
void
ft_run_tank(struct tank *tank, struct forftank *ftank)
{
int ret;
/* Copy program into command stack */
forf_stack_copy(&ftank->_cmd, &ftank->_prog);
forf_stack_reset(&ftank->_data);
ret = forf_eval(&ftank->env);
if (! ret) {
fprintf(stderr, "Error in %s: %s\n",
ftank->name,
forf_error_str[ftank->env.error]);
}
}
int
ft_read_program(struct forftank *ftank,
struct tank *tank,
struct forf_lexical_env *lenv,
char *path)
{
char progpath[256];
FILE *f;
/* Open program */
snprintf(progpath, sizeof(progpath), "%s/program", path);
f = fopen(progpath, "r");
if (! f) return 0;
/* Parse program */
ftank->error_pos = forf_parse_file(&ftank->env, f);
fclose(f);
if (ftank->error_pos) {
return 0;
}
/* Back up the program so we can run it over and over without
needing to re-parse */
forf_stack_copy(&ftank->_prog, &ftank->_cmd);
tank_init(tank, (tank_run_func *)ft_run_tank, ftank);
return 1;
}
void
ft_tank_init(struct forftank *ftank,
struct tank *tank,
struct forf_lexical_env *lenv)
{
/* Set up forf environment */
forf_stack_init(&ftank->_prog, ftank->_progvals, CSTACK_SIZE);
forf_stack_init(&ftank->_cmd, ftank->_cmdvals, CSTACK_SIZE);
forf_stack_init(&ftank->_data, ftank->_datavals, DSTACK_SIZE);
forf_memory_init(&ftank->_mem, ftank->_memvals, MEMORY_SIZE);
forf_env_init(&ftank->env,
lenv,
&ftank->_data,
&ftank->_cmd,
&ftank->_mem,
tank);
}
void
ft_read_sensors(struct tank *tank,
char *path)
{
int i;
for (i = 0; i < TANK_MAX_SENSORS; i += 1) {
int ret;
char fn[10];
char s[20];
char *p = s;
long range;
long angle;
long width;
long turret;
snprintf(fn, sizeof(fn), "sensor%d", i);
ret = ft_read_file(s, sizeof(s), path, fn);
if (! ret) {
s[0] = 0;
}
range = strtol(p, &p, 0);
angle = strtol(p, &p, 0);
width = strtol(p, &p, 0);
turret = strtol(p, &p, 0);
tank->sensors[i].range = min(range, TANK_SENSOR_RANGE);
tank->sensors[i].angle = deg2rad(angle % 360);
tank->sensors[i].width = deg2rad(width % 360);
tank->sensors[i].turret = (0 != turret);
}
}
int
ft_read_tank(struct forftank *ftank,
struct tank *tank,
struct forf_lexical_env *lenv,
char *path)
{
int ret;
ftank->path = path;
/* What is your name? */
ret = ft_read_file(ftank->name, sizeof(ftank->name), path, "name");
if (! ret) {
strncpy(ftank->name, path, sizeof(ftank->name));
}
/* What is your quest? */
ft_tank_init(ftank, tank, lenv);
ret = ft_read_program(ftank, tank, lenv, path);
if (ret) {
ft_read_sensors(tank, path);
} else {
/* Brick the tank */
tank_init(tank, ft_bricked_tank, NULL);
}
/* What is your favorite color? */
ret = ft_read_file(ftank->color, sizeof(ftank->color), path, "color");
if (! ret) {
strncpy(ftank->color, "#808080", sizeof(ftank->color));
}
return 1;
}
void
print_header(FILE *f,
struct tanks_game *game,
struct forftank *forftanks,
struct tank *tanks,
int ntanks)
{
int i, j;
fprintf(f, "[[%d,%d],[\n",
(int)game->size[0], (int)game->size[1]);
for (i = 0; i < ntanks; i += 1) {
fprintf(f, " [\"%s\",[", forftanks[i].color);
for (j = 0; j < TANK_MAX_SENSORS; j += 1) {
struct sensor *s = &(tanks[i].sensors[j]);
if (! s->range) {
fprintf(f, "0,");
} else {
fprintf(f, "[%d,%.2f,%.2f,%d],",
(int)(s->range),
s->angle,
s->width,
s->turret);
}
}
fprintf(f, "]],\n");
}
fprintf(f, "],[\n");
}
void
print_footer(FILE *f)
{
fprintf(f, "]]\n");
}
void
print_rounds(FILE *f,
struct tanks_game *game,
struct tank *tanks,
int ntanks)
{
int i;
int alive;
/* Run rounds */
alive = ntanks;
for (i = 0; (alive > 1) && (i < ROUNDS); i += 1) {
int j;
tanks_run_turn(game, tanks, ntanks);
fprintf(f, "[\n");
alive = ntanks;
for (j = 0; j < ntanks; j += 1) {
struct tank *t = &(tanks[j]);
int k;
int flags = 0;
int sensors = 0;
for (k = 0; k < TANK_MAX_SENSORS; k += 1) {
if (t->sensors[k].triggered) {
sensors |= (1 << k);
}
}
if (t->turret.firing) {
flags |= 1;
}
if (t->led) {
flags |= 2;
}
if (t->killer) {
alive -= 1;
flags |= 4;
}
fprintf(f, " [%d,%d,%.2f,%.2f,%d,%d],\n",
(int)t->position[0],
(int)(t->position[1]),
t->angle,
t->turret.current,
flags,
sensors);
}
fprintf(f, "],\n");
}
}
void
print_standings(FILE *f,
struct forftank *ftanks,
struct tank *tanks,
int ntanks)
{
int i;
for (i = 0; i < ntanks; i += 1) {
/* &tank path cause &killer parse_error_pos lasterr */
fprintf(f, "%p\t%s\t%s\t%p\t%d\t%s\n",
&(tanks[i]),
ftanks[i].path,
tanks[i].cause_death,
tanks[i].killer,
ftanks[i].error_pos,
forf_error_str[ftanks[i].env.error]);
}
}
int
main(int argc, char *argv[])
{
struct tanks_game game;
struct forftank myftanks[MAX_TANKS];
struct tank mytanks[MAX_TANKS];
struct forf_lexical_env lenv[LENV_SIZE];
int order[MAX_TANKS];
int ntanks = 0;
int i;
lenv[0].name = NULL;
lenv[0].proc = NULL;
if ((! forf_extend_lexical_env(lenv, forf_base_lexical_env, LENV_SIZE)) ||
(! forf_extend_lexical_env(lenv, tanks_lenv_addons, LENV_SIZE))) {
fprintf(stderr, "Unable to initialize lexical environment.\n");
return 1;
}
/* We only need slightly random numbers */
{
char *s = getenv("SEED");
int seed = atoi(s?s:"");
if (! seed) {
seed = getpid();
}
srand(seed);
fprintf(stdout, "// SEED=%d\n", seed);
}
/* Every argument is a tank directory */
for (i = 1; ntanks < MAX_TANKS && i < argc; i += 1) {
if (ft_read_tank(&myftanks[ntanks],
&mytanks[ntanks],
lenv,
argv[i])) {
ntanks += 1;
}
}
if (0 == ntanks) {
fprintf(stderr, "No usable tanks!\n");
return 1;
}
/* Calculate the size of the game board */
{
int x, y;
for (x = 1; x * x < ntanks; x += 1);
y = ntanks / x;
if (ntanks % x) {
y += 1;
}
game.size[0] = x * SPACING;
game.size[1] = y * SPACING;
}
/* Shuffle the order we place things on the game board */
for (i = 0; i < ntanks; i += 1) {
order[i] = i;
}
for (i = 0; i < ntanks; i += 1) {
int j = rand() % ntanks;
int n = order[j];
order[j] = order[i];
order[i] = n;
}
/* Position tanks */
{
int x = SPACING/2;
int y = SPACING/2;
for (i = 0; i < ntanks; i += 1) {
mytanks[order[i]].position[0] = (float)x;
mytanks[order[i]].position[1] = (float)y;
mytanks[order[i]].angle = deg2rad(rand() % 360);
mytanks[order[i]].turret.current = deg2rad(rand() % 360);
mytanks[order[i]].turret.desired = mytanks[order[i]].turret.current;
x += SPACING;
if (x > game.size[0]) {
x %= (int)(game.size[0]);
y += SPACING;
}
}
}
print_header(stdout, &game, myftanks, mytanks, ntanks);
print_rounds(stdout, &game, mytanks, ntanks);
print_footer(stdout);
/* Output standings to fd3.
*
* fd 3 is normally closed, so this won't normally do anything.
* To output to fd3 from the shell, you'll need to do something like this:
*
* ./run-tanks 3>standing
**/
{
FILE *standings = fdopen(3, "w");
if (standings) {
print_standings(standings, myftanks, mytanks, ntanks);
}
}
return 0;
}