From adfc19fc4c6b4036b7524b3c2d0e145b3506e426 Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Wed, 21 Jul 2010 15:54:46 -0600 Subject: [PATCH] Fix all 3 known bugs * Sensor sometimes triggers when tank directly behind it * Sensor range 0 screws up sensor order in canvas * Summary not sorted --- ctanks.c | 4 ++-- dump.h | 15 ++++++++++++++- run-tanks.c | 16 +++++++++------- summary.awk | 4 ++-- tanks.js | 21 +++++++++++++-------- 5 files changed, 40 insertions(+), 20 deletions(-) diff --git a/ctanks.c b/ctanks.c index e3ed999..28190b5 100644 --- a/ctanks.c +++ b/ctanks.c @@ -131,7 +131,7 @@ tanks_fire_cannon(struct tanks_game *game, rpos[0] = vector[0]; rpos[1] = vector[1]; rotate_point(-theta, rpos); - if (fabsf(rpos[1]) < TANK_RADIUS) { + if ((rpos[0] > 0) && (fabsf(rpos[1]) < TANK_RADIUS)) { that->killer = this; that->cause_death = "shot"; } @@ -204,7 +204,7 @@ tanks_sensor_calc(struct tanks_game *game, /* Now check if the edge of the arc intersects the tank. Do this just like with firing. */ rotate_point(this->sensors[i].width / -2, rpos); - if (fabsf(rpos[1]) < TANK_RADIUS) { + if ((rpos[0] > 0) && (fabsf(rpos[1]) < TANK_RADIUS)) { this->sensors[i].triggered = 1; } } diff --git a/dump.h b/dump.h index d7f454c..514f676 100644 --- a/dump.h +++ b/dump.h @@ -1,6 +1,8 @@ #ifndef __DUMP_H__ -/* Debugging help */ +#include + +/* Debugging */ #define DUMPf(fmt, args...) fprintf(stderr, "%s:%s:%d " fmt "\n", __FILE__, __FUNCTION__, __LINE__, ##args) #define DUMP() DUMPf("") #define DUMP_d(v) DUMPf("%s = %d", #v, v) @@ -11,4 +13,15 @@ #define DUMP_p(v) DUMPf("%s = %p", #v, v) #define DUMP_xy(v) DUMPf("%s = (%f, %f)", #v, v[0], v[1]); +/* Tektronix 4014 drawing */ +#define TEK_ENABLE "\033[?38h" +#define TEK_DISABLE "\033\003" +#define TEK(fmt, args...) fprintf(stderr, TEK_ENABLE fmt TEK_DISABLE, ##args) +#define TEK_coord(x, y) ((int)y/32)+32,((int)y%32)+96,((int)x/32)+32,((int)x%32)+64 + +#define TEK_cls() TEK("\033\014") +#define TEK_line(x1, y1, x2, y2) TEK("\035%c%c%c%c%c%c%c%c", TEK_coord(x1, y1), TEK_coord(x2, y2)) +#define TEK_point(x, y) TEK("\034%c%c%c%c", TEK_coord(x, y)) +#define TEK_text(x, y, s) TEK("\035%c%c%c%c\037%s", TEK_coord(x, y), s) + #endif diff --git a/run-tanks.c b/run-tanks.c index 7fb177d..0dfb800 100644 --- a/run-tanks.c +++ b/run-tanks.c @@ -353,7 +353,7 @@ print_header(FILE *f, { int i, j; - fprintf(f, "[[%d, %d, %d],[\n", + fprintf(f, "[[%d,%d,%d],[\n", (int)game->size[0], (int)game->size[1], TANK_CANNON_RANGE); for (i = 0; i < ntanks; i += 1) { fprintf(f, " [\"%s\",[", forftanks[i].color); @@ -361,13 +361,14 @@ print_header(FILE *f, struct sensor *s = &(tanks[i].sensors[j]); if (! s->range) { - continue; + fprintf(f, "0,"); + } else { + fprintf(f, "[%d,%.2f,%.2f,%d],", + (int)(s->range), + s->angle, + s->width, + s->turret); } - fprintf(f, "[%d, %.2f, %.2f, %d],", - (int)(s->range), - s->angle, - s->width, - s->turret); } fprintf(f, "]],\n"); } @@ -535,6 +536,7 @@ main(int argc, char *argv[]) mytanks[i].position[1] = (float)y; mytanks[i].angle = deg2rad(rand() % 360); mytanks[i].turret.current = deg2rad(rand() % 360); + mytanks[i].turret.desired = mytanks[i].turret.current; x += SPACING; if (x > game.size[0]) { diff --git a/summary.awk b/summary.awk index 3891303..bc802c2 100755 --- a/summary.awk +++ b/summary.awk @@ -55,8 +55,8 @@ BEGIN { # Find highest score maxscore = -1; for (p in scores) { - if (p > maxscore) { - maxscore = p; + if (int(p) > maxscore) { + maxscore = int(p); } } if (maxscore == -1) { diff --git a/tanks.js b/tanks.js index 266d6b5..283f051 100644 --- a/tanks.js +++ b/tanks.js @@ -26,14 +26,19 @@ function Tank(ctx, width, height, color, sensors) { this.sensors = new Array(); for (i in sensors) { var s = sensors[i]; - // r, angle, width, turret - this.sensors[i] = new Array(); - this.sensors[i][0] = s[0]; - this.sensors[i][1] = s[1] - s[2]/2; - this.sensors[i][2] = s[1] + s[2]/2; - this.sensors[i][3] = s[3]?1:0; - if (s[0] > maxlen) { - maxlen = s[0]; + + if (! s) { + this.sensors[i] = [0,0,0,0]; + } else { + // r, angle, width, turret + this.sensors[i] = new Array(); + this.sensors[i][0] = s[0]; + this.sensors[i][1] = s[1] - s[2]/2; + this.sensors[i][2] = s[1] + s[2]/2; + this.sensors[i][3] = s[3]?1:0; + if (s[0] > maxlen) { + maxlen = s[0]; + } } }