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
This commit is contained in:
Neale Pickett 2010-07-21 15:54:46 -06:00
parent a6a631e067
commit adfc19fc4c
5 changed files with 40 additions and 20 deletions

View File

@ -131,7 +131,7 @@ tanks_fire_cannon(struct tanks_game *game,
rpos[0] = vector[0]; rpos[0] = vector[0];
rpos[1] = vector[1]; rpos[1] = vector[1];
rotate_point(-theta, rpos); rotate_point(-theta, rpos);
if (fabsf(rpos[1]) < TANK_RADIUS) { if ((rpos[0] > 0) && (fabsf(rpos[1]) < TANK_RADIUS)) {
that->killer = this; that->killer = this;
that->cause_death = "shot"; 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 /* Now check if the edge of the arc intersects the tank. Do this
just like with firing. */ just like with firing. */
rotate_point(this->sensors[i].width / -2, rpos); 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; this->sensors[i].triggered = 1;
} }
} }

15
dump.h
View File

@ -1,6 +1,8 @@
#ifndef __DUMP_H__ #ifndef __DUMP_H__
/* Debugging help */ #include <stdio.h>
/* Debugging */
#define DUMPf(fmt, args...) fprintf(stderr, "%s:%s:%d " fmt "\n", __FILE__, __FUNCTION__, __LINE__, ##args) #define DUMPf(fmt, args...) fprintf(stderr, "%s:%s:%d " fmt "\n", __FILE__, __FUNCTION__, __LINE__, ##args)
#define DUMP() DUMPf("") #define DUMP() DUMPf("")
#define DUMP_d(v) DUMPf("%s = %d", #v, v) #define DUMP_d(v) DUMPf("%s = %d", #v, v)
@ -11,4 +13,15 @@
#define DUMP_p(v) DUMPf("%s = %p", #v, v) #define DUMP_p(v) DUMPf("%s = %p", #v, v)
#define DUMP_xy(v) DUMPf("%s = (%f, %f)", #v, v[0], v[1]); #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 #endif

View File

@ -353,7 +353,7 @@ print_header(FILE *f,
{ {
int i, j; 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); (int)game->size[0], (int)game->size[1], TANK_CANNON_RANGE);
for (i = 0; i < ntanks; i += 1) { for (i = 0; i < ntanks; i += 1) {
fprintf(f, " [\"%s\",[", forftanks[i].color); fprintf(f, " [\"%s\",[", forftanks[i].color);
@ -361,13 +361,14 @@ print_header(FILE *f,
struct sensor *s = &(tanks[i].sensors[j]); struct sensor *s = &(tanks[i].sensors[j]);
if (! s->range) { 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"); fprintf(f, "]],\n");
} }
@ -535,6 +536,7 @@ main(int argc, char *argv[])
mytanks[i].position[1] = (float)y; mytanks[i].position[1] = (float)y;
mytanks[i].angle = deg2rad(rand() % 360); mytanks[i].angle = deg2rad(rand() % 360);
mytanks[i].turret.current = deg2rad(rand() % 360); mytanks[i].turret.current = deg2rad(rand() % 360);
mytanks[i].turret.desired = mytanks[i].turret.current;
x += SPACING; x += SPACING;
if (x > game.size[0]) { if (x > game.size[0]) {

View File

@ -55,8 +55,8 @@ BEGIN {
# Find highest score # Find highest score
maxscore = -1; maxscore = -1;
for (p in scores) { for (p in scores) {
if (p > maxscore) { if (int(p) > maxscore) {
maxscore = p; maxscore = int(p);
} }
} }
if (maxscore == -1) { if (maxscore == -1) {

View File

@ -26,14 +26,19 @@ function Tank(ctx, width, height, color, sensors) {
this.sensors = new Array(); this.sensors = new Array();
for (i in sensors) { for (i in sensors) {
var s = sensors[i]; var s = sensors[i];
// r, angle, width, turret
this.sensors[i] = new Array(); if (! s) {
this.sensors[i][0] = s[0]; this.sensors[i] = [0,0,0,0];
this.sensors[i][1] = s[1] - s[2]/2; } else {
this.sensors[i][2] = s[1] + s[2]/2; // r, angle, width, turret
this.sensors[i][3] = s[3]?1:0; this.sensors[i] = new Array();
if (s[0] > maxlen) { this.sensors[i][0] = s[0];
maxlen = 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];
}
} }
} }