Radar strategy

This commit is contained in:
Stefan Bühler 2008-07-13 15:19:43 +02:00
parent 8be5c7e20e
commit 07830c6bbd
14 changed files with 3956 additions and 61 deletions

777
maps/martians2.wrld Normal file
View File

@ -0,0 +1,777 @@
{
"size" : 200,
"timeLimit" : 30000,
"vehicleParams" : {
"maxSpeed" : 20,
"accel" : 2,
"brake" : 3,
"turn" : 20,
"hardTurn" : 60,
"frontView" : 60,
"rotAccel" : 120,
"rearView" : 30
},
"martianParams" : {
"maxSpeed" : 20,
"accel" : 2,
"brake" : 3,
"turn" : 20,
"hardTurn" : 60,
"frontView" : 60,
"rotAccel" : 120,
"rearView" : 30
},
"craters" : [
],
"boulders" : [
],
"runs" : [
{
"vehicle" : {
"x" : 0,
"y" : 90,
"dir" : 270
},
"enemies" : [
{
"x" : -50,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -45,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -40,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -35,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -30,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -25,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -20,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -15,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -10,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -5,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 0,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 5,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 10,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 15,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 20,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 25,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 30,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 35,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 40,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 45,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
}
]
},
{
"vehicle" : {
"x" : 90,
"y" : 90,
"dir" : -80
},
"enemies" : [
{
"x" : -50,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -45,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -40,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -35,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -30,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -25,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -20,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -15,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -10,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -5,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 0,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 5,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 10,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 15,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 20,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 25,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 30,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 35,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 40,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 45,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
}
]
},
{
"vehicle" : {
"x" : -90,
"y" : 90,
"dir" : 0
},
"enemies" : [
{
"x" : -50,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -45,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -40,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -35,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -30,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -25,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -20,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -15,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -10,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -5,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 0,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 5,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 10,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 15,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 20,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 25,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 30,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 35,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 40,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 45,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
}
]
},
{
"vehicle" : {
"x" : 20,
"y" : 20,
"dir" : 180
},
"enemies" : [
{
"x" : -50,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -45,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -40,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -35,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -30,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -25,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -20,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -15,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -10,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -5,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 0,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 5,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 10,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 15,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 20,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 25,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 30,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 35,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 40,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 45,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
}
]
},
{
"vehicle" : {
"x" : 90,
"y" : 10,
"dir" : 10
},
"enemies" : [
{
"x" : -50,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -45,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -40,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -35,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -30,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -25,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -20,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -15,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -10,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : -5,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 0,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 5,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 10,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 15,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 20,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 25,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 30,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 35,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 40,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
},
{
"x" : 45,
"y" : 10,
"dir" : 100,
"speed" : 5,
"view" : 120
}
]
}
]
}

2898
maps/maze.wrld Normal file

File diff suppressed because it is too large Load Diff

View File

@ -85,6 +85,7 @@ trial *trial_new(const char *hostname, const char *port) {
t->socket = sock;
g_queue_init(&t->telemetry);
t->map.solid_objects = g_array_new(FALSE, FALSE, sizeof(object));
t->sim.tm.objects = g_array_new(FALSE, FALSE, sizeof(object));
control_parser_new(t);
t->runcnt = 0;
return t;
@ -145,9 +146,14 @@ int trial_wait_for_input(trial *t) {
if (t->parse_ctx->buffer->len) return trial_check_input(t);
ssize_t len = read(t->socket, buffer, sizeof(buffer));
if (len < 0) {
if (errno == ECONNRESET) {
len = 0; /* treat as simply connection close */
} else {
fprintf(stderr, "read error: %s\n", strerror(errno));
return -1;
} else if (len == 0) {
}
}
if (len == 0) {
if (t->alive) {
fprintf(stderr, "unexpected connection close\n");
return -1;
@ -165,6 +171,7 @@ void trial_free(trial *t) {
g_queue_foreach(&t->telemetry, telemetry_free, NULL);
g_queue_clear(&t->telemetry);
g_array_free(t->map.solid_objects, TRUE);
g_array_free(t->sim.tm.objects, TRUE);
control_parser_free(t);
g_slice_free(trial, t);
}
@ -210,4 +217,4 @@ void vehicle_print(vehicle *v){
fprintf(stderr,"Vehicle: Accel: %d Turn: %d x: %f y: %f dir: %f speed: %f\n",v->accel,v->turn,v->x,v->y,v->dir,v->speed);
}
void map_print(map *m){}
// void map_print(map *m){}

View File

@ -73,8 +73,8 @@ struct map {
};
struct simulated {
timestamp ts;
vehicle vehicle;
telemetry tm;
guint steps;
};
struct trial {
@ -96,6 +96,7 @@ struct trial {
/* extern */
void simulate(trial *t, timestamp ts);
void godown(trial *t);
void goradar(trial *t);
/* trial */
trial *trial_new(const char *hostname, const char *port);

View File

@ -27,7 +27,7 @@ static timestamp extract_ts(context *ctx, char *fpc) {
return atoi(ctx->tmp->str);
}
#line 138 "control_parser.rl"
#line 142 "control_parser.rl"
@ -243,7 +243,7 @@ static const int control_parser_error = 0;
static const int control_parser_en_main = 1;
#line 141 "control_parser.rl"
#line 145 "control_parser.rl"
static int control_parser_has_error(context *ctx) {
return ctx->cs == control_parser_error;
@ -260,7 +260,7 @@ void control_parser_new(trial *t) {
{
( ctx->cs) = control_parser_start;
}
#line 153 "control_parser.rl"
#line 157 "control_parser.rl"
ctx->buffer = g_string_sized_new(0);
ctx->tmp = g_string_sized_new(0);
ctx->mark = -1;
@ -274,7 +274,7 @@ void control_parser_reset(trial *t) {
{
( ctx->cs) = control_parser_start;
}
#line 162 "control_parser.rl"
#line 166 "control_parser.rl"
g_string_truncate(ctx->tmp, 0);
ctx->mark = -1;
/* fprintf(stderr, "Parser reset\n"); */
@ -398,14 +398,18 @@ _match:
/* fprintf(stderr, "New run\n"); */
} else {
/* fprintf(stderr, "time difference [ms]: %i\n", getcurts(t) - ctx->tm->ts); */
fprintf(stderr, "Miss predict: dx=%f, dy=%f, ddir=%f\n",
t->sim.vehicle.x - t->vehicle.x,
t->sim.vehicle.y - t->vehicle.y,
t->sim.vehicle.dir - t->vehicle.dir);
fprintf(stderr, "Miss predict: dx=%f, dy=%f, ddir=%f, steps=%u\n",
t->sim.tm.vehicle.x - t->vehicle.x,
t->sim.tm.vehicle.y - t->vehicle.y,
t->sim.tm.vehicle.dir - t->vehicle.dir,
t->sim.steps);
}
/* Reset simulation */
t->sim.ts = ctx->tm->ts;
t->sim.vehicle = t->vehicle;
t->sim.tm.ts = ctx->tm->ts;
t->sim.tm.vehicle = ctx->tm->vehicle;
g_array_set_size(t->sim.tm.objects, 0);
g_array_append_vals(t->sim.tm.objects, ctx->tm->objects->data, ctx->tm->objects->len);
t->sim.steps = 0;
t->last_ts = ctx->tm->ts;
ctx->tm = NULL;
@ -413,7 +417,7 @@ _match:
}
break;
case 4:
#line 63 "control_parser.rl"
#line 67 "control_parser.rl"
{
ctx->tm->ts = ctx->ts;
ctx->tm->vehicle.x = ctx->x;
@ -428,115 +432,115 @@ _match:
}
break;
case 5:
#line 80 "control_parser.rl"
#line 84 "control_parser.rl"
{ t->map.dx = extract_double(ctx, p); }
break;
case 6:
#line 81 "control_parser.rl"
#line 85 "control_parser.rl"
{ t->map.dy = extract_double(ctx, p); }
break;
case 7:
#line 82 "control_parser.rl"
#line 86 "control_parser.rl"
{ t->map.min_sensor = extract_double(ctx, p); }
break;
case 8:
#line 83 "control_parser.rl"
#line 87 "control_parser.rl"
{ t->map.max_sensor = extract_double(ctx, p); }
break;
case 9:
#line 84 "control_parser.rl"
#line 88 "control_parser.rl"
{ t->map.max_speed = extract_double(ctx, p); }
break;
case 10:
#line 85 "control_parser.rl"
#line 89 "control_parser.rl"
{ t->map.max_turn = extract_double(ctx, p); }
break;
case 11:
#line 86 "control_parser.rl"
#line 90 "control_parser.rl"
{ t->map.max_hard_turn = extract_double(ctx, p); }
break;
case 12:
#line 90 "control_parser.rl"
#line 94 "control_parser.rl"
{ ctx->x = extract_double(ctx, p); }
break;
case 13:
#line 91 "control_parser.rl"
#line 95 "control_parser.rl"
{ ctx->y = extract_double(ctx, p); }
break;
case 14:
#line 92 "control_parser.rl"
#line 96 "control_parser.rl"
{ ctx->r = extract_double(ctx, p); }
break;
case 15:
#line 93 "control_parser.rl"
#line 97 "control_parser.rl"
{ ctx->dir = extract_double(ctx, p); }
break;
case 16:
#line 94 "control_parser.rl"
#line 98 "control_parser.rl"
{ ctx->speed = extract_double(ctx, p); }
break;
case 17:
#line 96 "control_parser.rl"
#line 100 "control_parser.rl"
{ ctx->tm->vehicle.accel = ACCEL; }
break;
case 18:
#line 97 "control_parser.rl"
#line 101 "control_parser.rl"
{ ctx->tm->vehicle.accel = ROLL; }
break;
case 19:
#line 98 "control_parser.rl"
#line 102 "control_parser.rl"
{ ctx->tm->vehicle.accel = BRAKE; }
break;
case 20:
#line 100 "control_parser.rl"
#line 104 "control_parser.rl"
{ ctx->tm->vehicle.turn = TURN_HARD_LEFT; }
break;
case 21:
#line 101 "control_parser.rl"
#line 105 "control_parser.rl"
{ ctx->tm->vehicle.turn = TURN_LEFT; }
break;
case 22:
#line 102 "control_parser.rl"
#line 106 "control_parser.rl"
{ ctx->tm->vehicle.turn = TURN_STRAIGHT; }
break;
case 23:
#line 103 "control_parser.rl"
#line 107 "control_parser.rl"
{ ctx->tm->vehicle.turn = TURN_RIGHT; }
break;
case 24:
#line 104 "control_parser.rl"
#line 108 "control_parser.rl"
{ ctx->tm->vehicle.turn = TURN_HARD_RIGHT; }
break;
case 25:
#line 106 "control_parser.rl"
#line 110 "control_parser.rl"
{ ctx->ts = extract_ts(ctx, p); }
break;
case 26:
#line 107 "control_parser.rl"
#line 111 "control_parser.rl"
{ printf("Score %u\n", extract_ts(ctx, p)); }
break;
case 27:
#line 109 "control_parser.rl"
#line 113 "control_parser.rl"
{
object o = { BOLDER, ctx->x, ctx->y, ctx->r, 0, 0 };
g_array_append_val(ctx->tm->objects, o);
}
break;
case 28:
#line 113 "control_parser.rl"
#line 117 "control_parser.rl"
{
object o = { CRATER, ctx->x, ctx->y, ctx->r, 0, 0 };
g_array_append_val(ctx->tm->objects, o);
}
break;
case 29:
#line 118 "control_parser.rl"
#line 122 "control_parser.rl"
{
object o = { MARTIAN, ctx->x, ctx->y, MARTIAN_RADIUS, ctx->dir, ctx->speed };
g_array_append_val(ctx->tm->objects, o);
}
break;
#line 540 "control_parser.c"
#line 544 "control_parser.c"
}
}
@ -548,7 +552,7 @@ _again:
_test_eof: {}
_out: {}
}
#line 185 "control_parser.rl"
#line 189 "control_parser.rl"
ctx->pos = p - ctx->buffer->str;
if (ctx->mark == -1) {

View File

@ -47,14 +47,18 @@ static timestamp extract_ts(context *ctx, char *fpc) {
/* fprintf(stderr, "New run\n"); */
} else {
/* fprintf(stderr, "time difference [ms]: %i\n", getcurts(t) - ctx->tm->ts); */
fprintf(stderr, "Miss predict: dx=%f, dy=%f, ddir=%f\n",
t->sim.vehicle.x - t->vehicle.x,
t->sim.vehicle.y - t->vehicle.y,
t->sim.vehicle.dir - t->vehicle.dir);
fprintf(stderr, "Miss predict: dx=%f, dy=%f, ddir=%f, steps=%u\n",
t->sim.tm.vehicle.x - t->vehicle.x,
t->sim.tm.vehicle.y - t->vehicle.y,
t->sim.tm.vehicle.dir - t->vehicle.dir,
t->sim.steps);
}
/* Reset simulation */
t->sim.ts = ctx->tm->ts;
t->sim.vehicle = t->vehicle;
t->sim.tm.ts = ctx->tm->ts;
t->sim.tm.vehicle = ctx->tm->vehicle;
g_array_set_size(t->sim.tm.objects, 0);
g_array_append_vals(t->sim.tm.objects, ctx->tm->objects->data, ctx->tm->objects->len);
t->sim.steps = 0;
t->last_ts = ctx->tm->ts;
ctx->tm = NULL;

View File

@ -96,7 +96,7 @@ int path_app_target(path* p, trial* t,int offset, int x, int y){
/* Turn towards origin, take shorter direction, TODO: fix, thats only an approximation*/
angle = atan2(y-v->y,x-v->x)*180/M_PI;
angle = angle_for_rot(v->dir, angle);
stop = abs(angle)/m->max_hard_turn*1000;
stop = fabs(angle)/m->max_hard_turn*1000;
printf("Angle: %f stop: %f\n",angle,stop);
if(angle > 0){
/*clockwise/left turn*/

View File

@ -20,9 +20,9 @@ void godown(trial *t) {
double xpot, ypot, r, vr, angle, vdirx, vdiry;
/* todo: simulate movement */
v = t->sim.vehicle;
v = t->sim.tm.vehicle;
tm = (telemetry*) g_queue_peek_tail(&t->telemetry);
tm = &t->sim.tm;
vr = sqrt(v.x*v.x + v.y*v.y);
if (vr < 4) return; /* done */
@ -80,7 +80,7 @@ void godown(trial *t) {
}
}
// fprintf(stderr, "Angle: %f, objects: %u\n", angle, tm->objects->len);
angle = abs(angle);
angle = fabs(angle);
if (angle > 60) {
vehicle_break(t);
} else if (angle > 50) {

142
src/radar.c Normal file
View File

@ -0,0 +1,142 @@
#include "control.h"
#include <math.h>
#include <stdlib.h>
#include <assert.h>
struct angle {
double a;
int c;
};
typedef struct angle angle;
gint cmp_angle(gconstpointer _a, gconstpointer _b, gpointer p) {
const angle *a = (const angle*) _a, *b = (const angle*) _b;
UNUSED(p);
return (a->a == b->a) ? 0 : (a->a < b->a) ? -1 : 1;
}
void angle_free(angle* a) {
g_slice_free(angle, a);
}
angle* angle_new(double a, int c) {
angle *x = g_slice_new(angle);
x->a = a; x->c = c;
return x;
}
static double fixangle(double a) {
while (a < -180) a += 360;
while (a > 180) a -= 360;
return a;
}
void goradar(trial *t) {
vehicle v;
telemetry *tm;
GSequence *angles = g_sequence_new((GDestroyNotify) angle_free);
GSequenceIter *prev, *cur, *end;
angle *acur, *aprev;
double hang, ang = 1000, weight = 0;
int c;
guint i;
v = t->sim.tm.vehicle;
hang = atan2(-v.y, -v.x)*180/M_PI;
tm = &t->sim.tm;
g_sequence_insert_sorted(angles, angle_new(-45, 0), cmp_angle, NULL);
g_sequence_insert_sorted(angles, angle_new(45, 0), cmp_angle, NULL);
for (i = 0; i < tm->objects->len; i++) {
object *o = &g_array_index(tm->objects, object, i);
double phi, phi2, avoid = 0, oangle;
double distance = sqrt((o->y - v.y) * (o->y - v.y) + (o->x - v.x) * (o->x - v.x));
oangle = fixangle(atan2(-o->y, -o->x)*180/M_PI - hang);
if (fabs(oangle) > 180) continue;
// if (distance > 100) continue;
phi = fixangle(atan2(o->y - v.y, o->x - v.x)*180/M_PI - hang);
if (fabs(phi) > 45) continue;
switch (o->type) {
case BOLDER:
avoid = 1.2 * o->rad + VEHICLE_RADIUS; break;
case CRATER:
avoid = 1.2 * o->rad + VEHICLE_RADIUS + 1; break;
case MARTIAN:
avoid = o->rad + VEHICLE_RADIUS + 5; break;
}
phi2 = atan(avoid / distance )*180/M_PI;
g_sequence_insert_sorted(angles, angle_new(phi - phi2, 1), cmp_angle, NULL);
g_sequence_insert_sorted(angles, angle_new(phi + phi2, -1), cmp_angle, NULL);
}
prev = g_sequence_get_begin_iter(angles), end = g_sequence_get_end_iter(angles);
if (prev != end) {
aprev = (angle*) g_sequence_get(prev);
c = aprev->c;
while ((cur = g_sequence_iter_next(prev)) != end) {
acur = (angle*) g_sequence_get(cur);
assert(aprev->a <= acur->a);
if (!c) {
// fprintf(stderr, "Gap between %f and %f\n", aprev->a, acur->a);
if (acur->a - aprev->a > 1) {
double ax, ad, w1 = 1E20, w2 = 1E20, w;
if (aprev->a <= 0) {
if (acur->a <= 0) {
ax = acur->a;
} else {
ax = 0;
}
} else {
ax = aprev->a;
}
if (ax != 0) w1 = 1/fabs(ax);
ad = fixangle(ax - v.dir + hang);
if (ad != 0) w2 = 1/fabs(ad);
w = w1+w2;
if (w > weight) {
fprintf(stderr, "Selected %f\n", ax);
ang = ax;
weight = w;
}
}
}
c += acur->c;
prev = cur;
aprev = acur;
}
}
if (weight == 0) {
fprintf(stderr, "Potential\n");
godown(t);
return;
}
ang = fixangle(ang - v.dir + hang);
fprintf(stderr, "Angle: %f, objects: %u\n", ang, tm->objects->len);
if (ang < 1) {
if (ang < -50) {
vehicle_hard_right(t);
} else {
vehicle_right(t);
}
} else if (ang > 1) {
if (ang > 50) {
vehicle_hard_left(t);
} else {
vehicle_left(t);
}
}
ang = fabs(ang);
if (ang > 90) {
vehicle_break(t);
} else if (ang > 45) {
vehicle_roll(t);
} else {
vehicle_accel(t);
}
}

40
src/radar_main.c Normal file
View File

@ -0,0 +1,40 @@
#include "control.h"
#include "path.h"
#include <stdio.h>
#define LATENCY 20
void trial_loop(trial *t) {
do {
if (-1 == trial_wait_for_start(t)) return;
if (t->finished) break;
while (t->alive) {
simulate(t, getcurts(t) + LATENCY);
goradar(t);
if (-1 == trial_check_input(t)) return;
// if (-1 == trial_wait_for_input(t)) return;
}
trial_reset_run(t);
} while (!t->finished);
}
int main(int argc, char **argv) {
trial *t;
if (argc <= 2) {
fprintf(stderr, "Syntax: %s hostname port\n", argv[0]);
return 1;
}
if (NULL == (t = trial_new(argv[1], argv[2]))) {
return 2;
}
trial_loop(t);
trial_free(t);
return 0;
}

View File

@ -4,7 +4,7 @@
#include <math.h>
static double fixdir(double a) {
static inline double fixdir(double a) {
if (a < 0) a += 360;
else if (a > 360) a -= 360;
return a;
@ -13,14 +13,15 @@ static double fixdir(double a) {
void simulate(trial *t, timestamp ts) {
double move, angle;
timestamp diff = ts - t->sim.ts;
timestamp diff = ts - t->sim.tm.ts;
if (diff == 0) return;
t->sim.ts = ts;
t->sim.tm.ts = ts;
t->sim.steps++;
move = t->sim.vehicle.speed / 1000 * diff;
angle = t->sim.vehicle.dir*M_PI/180;
t->sim.vehicle.x += cos(angle)*move;
t->sim.vehicle.y += sin(angle)*move;
move = t->sim.tm.vehicle.speed / 1000 * diff;
angle = t->sim.tm.vehicle.dir*M_PI/180;
t->sim.tm.vehicle.x += cos(angle)*move;
t->sim.tm.vehicle.y += sin(angle)*move;
/* switch (t->sim.vehicle.turn) {
case TURN_HARD_LEFT : t->sim.vehicle.dir = fixdir(t->sim.vehicle.dir + t->map.max_hard_turn/1000*diff); break;

View File

@ -12,9 +12,25 @@ main_source = '''
simulate.c
'''
radarmain_source = '''
radar_main.c
control.c
control_parser.c
radar.c
potential.c
simulate.c
'''
def build(bld):
main = bld.new_task_gen('cc', 'program')
main.name = 'icfp08'
main.source = main_source
main.target = 'icfp08'
main.uselib += 'glib'
radar = bld.new_task_gen('cc', 'program')
radar.name = 'icfp08radar'
radar.source = radarmain_source
radar.target = 'icfp08radar'
radar.uselib += 'glib'

4
testitradar Executable file
View File

@ -0,0 +1,4 @@
#!/bin/bash
./waf distclean && ./waf configure && ./waf build --targets icfp08radar
./build/default/src/icfp08radar localhost 17676

View File

@ -70,6 +70,7 @@ def configure(conf):
common_ccflags = [
'-std=gnu99', '-Wall', '-g', '-Wshadow', '-W', '-pedantic',
'-O2',
# '-fPIC', '-D_GNU_SOURCE',
]
conf.env['CCFLAGS'] += common_ccflags