Clean compile of ctanks.c, but doubtless buggy as heck

This commit is contained in:
Neale Pickett 2010-06-28 17:12:20 -06:00
parent 4c3e1af017
commit 4720524639
8 changed files with 367 additions and 300 deletions

159
brad.c
View File

@ -1,159 +0,0 @@
#include <stdlib.h>
#include "brad.h"
#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif
/* Approximate vector length
*
* http://www.flipcode.com/archives/Fast_Approximate_Distance_Functions.shtml
*
* (1007/1024) * 256 == 251.75
* (441/1024) * 256 == 110.25
*/
bs_t
bs_approx_dist(bs_t dx, bs_t dy)
{
bs_t x = abs(dx);
bs_t y = abs(dy);
return (252 * max(x, y)) + (110 * min(x, y));
}
static bs_t bs_cos_table[] = {
256, 256, 256, 255, 255, 254, 253, 252,
251, 250, 248, 247, 245, 243, 241, 239,
237, 234, 231, 229, 226, 223, 220, 216,
213, 209, 206, 202, 198, 194, 190, 185,
181, 177, 172, 167, 162, 157, 152, 147,
142, 137, 132, 126, 121, 115, 109, 104,
98, 92, 86, 80, 74, 68, 62, 56,
50, 44, 38, 31, 25, 19, 13, 6,
0
};
bs_t
bs_cos(brad_t angle)
{
brad_t cos;
bs_t a;
a = abs(angle) % 128;
if (a > 64) {
a = 128 - a;
}
cos = bs_cos_table[a];
a = abs(angle) % 256;
if ((a > 64) && (a < 192)) {
cos = -cos;
}
return cos;
}
bs_t
bs_sin(brad_t angle)
{
return bs_cos(64 - angle);
}
bs_t
bs_tan(brad_t angle)
{
return bs_sin(angle) / bs_cos(angle);
}
static brad_t bs_asin_table[] = {
0, 0, 1, 1, 1, 2, 2, 2,
3, 3, 3, 4, 4, 4, 4, 5,
5, 5, 6, 6, 6, 7, 7, 7,
8, 8, 8, 9, 9, 9, 10, 10,
10, 11, 11, 11, 11, 12, 12, 12,
13, 13, 13, 14, 14, 14, 15, 15,
15, 16, 16, 16, 17, 17, 17, 18,
18, 18, 19, 19, 19, 20, 20, 20,
21, 21, 21, 22, 22, 22, 23, 23,
23, 24, 24, 24, 25, 25, 25, 26,
26, 26, 27, 27, 27, 28, 28, 28,
29, 29, 29, 30, 30, 30, 31, 31,
31, 32, 32, 32, 33, 33, 33, 34,
34, 34, 35, 35, 35, 36, 36, 37,
37, 37, 38, 38, 38, 39, 39, 39,
40, 40, 40, 41, 41, 42, 42, 42,
43, 43, 43, 44, 44, 45, 45, 45,
46, 46, 46, 47, 47, 48, 48, 48,
49, 49, 49, 50, 50, 51, 51, 51,
52, 52, 53, 53, 53, 54, 54, 55,
55, 55, 56, 56, 57, 57, 57, 58,
58, 59, 59, 60, 60, 60, 61, 61,
62, 62, 63, 63, 64, 64, 64, 65,
65, 66, 66, 67, 67, 68, 68, 69,
69, 70, 70, 71, 71, 72, 72, 73,
73, 74, 74, 75, 75, 76, 76, 77,
77, 78, 78, 79, 80, 80, 81, 81,
82, 82, 83, 84, 84, 85, 86, 86,
87, 87, 88, 89, 90, 90, 91, 92,
92, 93, 94, 95, 96, 96, 97, 98,
99, 100, 101, 102, 103, 104, 105, 106,
108, 109, 110, 112, 114, 116, 118, 121,
128
};
brad_t
bs_asin(bs_t n)
{
bs_t a = abs(n);
brad_t ret;
if (a > 256) {
return 0;
}
ret = bs_asin_table[a];
if (n < 0) {
ret = -ret;
}
return ret;
}
brad_t
bs_acos(bs_t n)
{
return 128 - bs_asin(n);
}
brad_t
bs_atan2(bs_t y, bs_t x)
{
bs_t r = bs_approx_dist(x, y);
brad_t t;
t = bs_acos(x / r);
if (y < 0) {
t = 256 - t;
}
return t;
}
#include <math.h>
#include <stdio.h>
#define PI 3.14159265358979323846
int
main()
{
int i, j;
float f;
int t;
for (i = 0; i < 257; i += 1) {
for (j = 0; j < 257; j += 1) {
f = sqrt(i*i + j*j);
t = bs_approx_dist(i, j);
printf("%d %d %d\n", i, (int)round(f*256)/256, t/256);
}
}
return 0;
}

50
brad.h
View File

@ -1,50 +0,0 @@
#ifndef __BRAD_H__
#define __BRAD_H__
/** Binary scaling library
*
* There are B8 binary scaled ints. That means the 8 lowest bits are
* the fractional part. To convert to a float you'd just divide by 256.
*
* The trig functions use brads (Binary Radians). There are 128 brads
* in 2pi radians. One brad is about 1.4 degrees. Using brads makes
* trig really fast on a binary computer, and reallier faster on a
* binary computer without an FPU.
*
* You must be careful not to overflow your bs_t. For instance, I wrote
* this library for a game with a 2^9-pixel-wide playfield. That's
* 2^17 B8, but I also needed to do distance calculations, which
* requires squaring things. 2^34 is obviously too big to represent in
* 32 bits.
*
* C's type system leaves a lot to be desired when it comes to making
* sure you're not mixing these things up with normal ints. Be careful!
*/
/* Just to make it clear what scale these functions are dealing with */
typedef struct {
int v;
} bs_t;
typedef int brad_t;
/* If you change this, you must also change the lookup tables in
* brad.c. Don't change this. */
#define BINARY_SCALE 8
#define BS_DENOMINATOR 1 << BINARY_SCALE
#define bs_to_int(n) ((n) >> BINARY_SCALE)
#define bs_of_int(i) ((i) << BINARY_SCALE)
bs_t bs_cos(brad_t angle);
bs_t bs_sin(brad_t angle);
bs_t bs_tan(brad_t angle);
brad_t bs_acos(bs_t angle);
brad_t bs_asin(bs_t angle);
bs_t bs_mul(bs_t a, bs_t b);
bs_t bs_div(bs_t a, bs_t b);
bs_t bs_approx_dist(bs_t dx, bs_t dy);
#endif /* __BRAD_H__ */

25
brad.py
View File

@ -1,25 +0,0 @@
#! /usr/bin/python
# Binary radians with B12 binary scaling (multiply floats by 256)
# pi radians = 128 brads
import math
brad2rad = math.pi/128
print "static bs_t bs_cos_table[] = {"
for i in range(129):
r = (i * math.pi) / 128.0
cos = math.sin(r)
bcos = int(round(cos * 256))
print ("%3d," % bcos),
print "}"
print "static brad_t bs_asin_table[] = {"
for i in range(257):
f = i / 256.0
acos = math.asin(f)
bacos = int(round(256 * acos / math.pi))
print ("%3d," % bacos),
print "}"

252
ctanks.c
View File

@ -1,6 +1,8 @@
#include <string.h>
#include <stdlib.h>
#include "brad.h"
#include <stdio.h>
#include <math.h>
#include "trig.h"
#include "ctanks.h"
void
@ -24,20 +26,20 @@ tank_fire(struct tank *tank)
}
void
tank_set_speed(struct tank *tank, bs_t left, bs_t right)
tank_set_speed(struct tank *tank, float left, float right)
{
tank->speed.desired[0] = left;
tank->speed.desired[1] = right;
}
brad_t
int
tank_get_turret(struct tank *tank)
{
return tank->turret.current;
}
void
tank_set_turret(struct tank *tank, brad_t angle)
tank_set_turret(struct tank *tank, int angle)
{
tank->turret.desired = angle;
}
@ -45,7 +47,7 @@ tank_set_turret(struct tank *tank, brad_t angle)
int
tank_get_sensor(struct tank *tank, int sensor_num)
{
if ((sensor < 0) || (sensor > MAX_SENSORS)) {
if ((sensor_num < 0) || (sensor_num > TANK_MAX_SENSORS)) {
return 0;
}
return tank->sensor[sensor_num].triggered;
@ -57,24 +59,96 @@ tank_set_led(struct tank *tank, int active)
tank->led = active;
}
/** Return distance^2 between tanks a and b.
*
* Comparing this against sensor_range^2 will tell you whether the tanks
* are within sensor range of one another. Similarly, comparing it
* against (2*tank_radius)^2 will tell you if they've collided.
*
*/
bs_t
tank_dist2(struct tanks_game *game, struct tank *a, struct tank *b)
static void
rotate_point(int angle, float point[2])
{
bs_t d[2];
int i;
float cos_, sin_;
float new[2];
cos_ = trig_cos(angle);
sin_ = trig_sin(angle);
new[0] = point[0]*cos_ + point[1]*sin_;
new[1] = point[0]*sin_ + point[1]*cos_;
}
static void
tank_sensor_calc(struct tanks_game *game,
struct tank *this,
struct tank *that,
float dist2)
{
float tpos[2];
int i;
/* Translate other tank's position to make us the origin */
for (i = 0; i < 2; i += 1) {
d[i] = abs(a.position[i] - b.position[i]);
d[i] = min(game.size[i] - dx[i], dx[i]);
tpos[i] = that->position[i] - this->position[i];
}
/* Did this shoot that? Rotate point by turret degrees, and if |y| <
TANK_RADIUS, we have a hit. */
if ((this->turret.firing) && (dist2 <= TANK_CANNON_RANGE)) {
int theta = this->angle + this->turret.current;
float rpos[2];
rpos[0] = tpos[0];
rpos[1] = tpos[1];
rotate_point(theta, rpos);
if (fabsf(rpos[1]) < TANK_RADIUS) {
that->killer = this;
that->cause_death = "shot";
}
}
/* Calculate sensors */
for (i = 0; i < this->num_sensors; i += 1) {
int theta;
float rpos[2];
float m_r, m_s;
/* No need to re-check this sensor if it's already firing */
if (this->sensor[i].triggered) {
continue;
}
/* If the tank is out of range, don't bother */
if (dist2 < this->sensor[i].range_adj2) {
continue;
}
/* What is the angle of our sensor? */
theta = this->angle;
if (this->sensor[i].turret) {
theta += this->turret.current;
}
/* Rotate tpos by theta */
rpos[0] = tpos[0];
rpos[1] = tpos[1];
rotate_point(theta, rpos);
/* Sensor is symmetrical, we can consider only first quadrant */
rpos[1] = fabsf(rpos[1]);
/* Compute slopes to tank and of our sensor */
m_s = tan(theta);
m_r = rpos[1] / rpos[0];
/* If their slope is greater than ours, they're inside the arc */
if (m_r >= m_s) {
this->sensor[i].triggered = 1;
continue;
}
/* Now check if the edge of the arc intersects the tank. Do this
just like with firing. */
rotate_point(this->sensor[i].width / 2, rpos);
if (fabsf(rpos[1]) < TANK_RADIUS) {
this->sensor[i].triggered = 1;
}
}
return ((d[0] * d[0]) + (d[1] * d[1]));
}
void
@ -82,9 +156,9 @@ do_shit_with(struct tanks_game *game,
struct tank *this,
struct tank *that)
{
bs_t vector[2];
float vector[2];
int dist2; /* Integer to avoid overflow! */
bs_t xpos; /* Translated position */
float tpos; /* Translated position */
int i;
/* Don't bother if one is dead */
@ -95,7 +169,7 @@ do_shit_with(struct tanks_game *game,
/* Establish shortest vector from center of this to center of that,
* taking wrapping into account */
for (i = 0; i < 2; i += 1) {
bs_t halfsize = game->size[i] / 2;
float halfsize = game->size[i] / 2;
/* XXX: is there a more elegant way to do this? */
vector[i] = that->position[i] - this->position[i];
@ -106,37 +180,133 @@ do_shit_with(struct tanks_game *game,
}
}
/* Compute distance^2 for range comparisons */
dist2 = ((bs_to_int(vector[0]) * bs_to_int(vector[0])) +
(bs_to_int(vector[1]) * bs_to_int(vector[1])));
dist2 = ((vector[0] * vector[0]) + (vector[1] * vector[1]));
/* If they're not within sensor range, there's nothing to do. */
if (dist2 > TANK_SENSOR_ADJ2) {
return;
}
/* Did they collide? Oh, goody! */
/* Did they collide? */
if (dist2 < TANK_COLLISION_ADJ2) {
/* XXX: kill both tanks */
this->killer = that;
this->cause_death = "collision";
that->killer = this;
this->cause_death = "collision";
return;
}
/* Has anybody been shot? */
/* Figure out who's whomin' whom */
tank_sensor_calc(game, this, that, dist2);
tank_sensor_calc(game, that, this, dist2);
}
/* Translate other tank's position to make us the origin */
void
tanks_print_tank(struct tanks_game *game,
struct tank *tank)
{
printf("%p\n", tank);
}
void
tanks_move_tank(struct tanks_game *game,
struct tank *tank)
{
int i;
float movement[2];
int angle;
/* Fakey acceleration */
for (i = 0; i < 2; i += 1) {
xpos[i] = that->position[i] - this->position[i];
}
/* Calculate sensors */
for (i = 0; i < this->num_sensors; i += 1) {
brad_t theta;
if (dist2 < this->sensor[i].range_adj2) {
continue;
if (tank->speed.current[i] == tank->speed.desired[i]) {
/* Do nothing */
} else if (tank->speed.current[i] < tank->speed.desired[i]) {
tank->speed.current[i] = max(tank->speed.current[i] - TANK_MAX_ACCEL,
tank->speed.desired[i]);
} else {
tank->speed.current[i] = min(tank->speed.current[i] + TANK_MAX_ACCEL,
tank->speed.desired[i]);
}
/* Translate other tank so that we're the origin */
}
/* The simple case */
if (tank->speed.current[0] == tank->speed.current[1]) {
movement[0] = tank->speed.current[0];
movement[1] = tank->speed.current[1];
angle = tank->angle;
} else {
/* pflarr's original comment:
*
* The tank drives around in a circle of radius r, which is some *
* offset on a line perpendicular to the tank. The distance it
* travels around the circle varies with the speed of each tread,
* and is such that each side of the tank moves an equal angle
* around the circle.
*
* Sounds good to me. pflarr's calculations here are fantastico,
* there's nothing whatsoever to change. */
float friction;
float v[2];
float So, Si;
float w, r;
int theta;
int dir;
/* The first thing Paul's code does is find "friction", which seems
to be a penalty for having the treads go in opposite directions.
This probably plays hell with precisely-planned tanks, which I
find very ha ha. */
friction = .25 * (fabsf(tank->speed.current[0] - tank->speed.current[1]) / 200);
v[0] = tank->speed.current[0] * friction;
v[1] = tank->speed.current[1] * friction;
/* Outside and inside speeds */
So = max(v[0], v[1]);
Si = min(v[0], v[1]);
dir = (v[0] > v[1]) ? 1 : -1;
/* Width of tank */
w = TANK_RADIUS * 2;
/* Radius of circle to outside tread (use similar triangles) */
r = So * w / (So - Si);
/* pflarr:
The fraction of the circle traveled is equal to the speed
of the outer tread over the circumference of the circle:
Ft = So/(2*pi*r)
The angle traveled is:
theta = Ft * 2*pi
This reduces to a simple
theta = So/r
We multiply it by dir to adjust for the direction of rotation
*/
theta = rad2deg(So/r) * dir;
/* Translate so the circle's center is 0,0, rotate the point by
theta, then add back in. */
v[0] = trig_cos(tank->angle + 90*dir) * (TANK_RADIUS - r);
v[1] = trig_sin(tank->angle + 90*dir) * (TANK_RADIUS - r);
movement[0] = v[0];
movement[1] = v[1];
rotate_point(theta, movement);
movement[0] -= v[0];
movement[1] -= v[1];
angle = theta;
}
/* Now move the tank */
for (i = 0; i < 2; i += 1) {
tank->position[i] = fmodf(tank->position[i] + movement[i] + game->size[i],
game->size[i]);
}
tank->angle = (tank->angle + angle + 360) % 360;
}
void
@ -145,10 +315,10 @@ tanks_run_turn(struct tanks_game *game, struct tank *tanks, int ntanks)
int i, j;
for (i = 0; i < ntanks; i += 1) {
for (j = i + 1; j < ntanks, j += 1) {
for (j = i + 1; j < ntanks; j += 1) {
do_shit_with(game, &(tanks[i]), &(tanks[j]));
}
tanks_print_tank(game, &(tanks[i]));
tanks_move(game, &(tanks[i]));
tanks_move_tank(game, &(tanks[i]));
}
}

View File

@ -1,57 +1,58 @@
#ifndef __CTANKS_H__
#define __CTANKS_H__
#include "brad.h"
/* Some useful constants */
#define TANK_MAX_SENSORS 10
#define TANK_RADIUS ((bs_t)1920)
#define TANK_SENSOR_RANGE ((int)100)
#define TANK_RADIUS 7.5
#define TANK_SENSOR_RANGE 100
#define TANK_CANNON_RANGE (TANK_SENSOR_RANGE / 2)
#define TANK_MAX_ACCEL 35
/* (tank radius + tank radius)^2 */
#define TANK_COLLISION_ADJ2 \
bs_to_int((TANK_RADIUS + TANK_RADIUS) * (TANK_RADIUS + TANK_RADIUS))
((TANK_RADIUS + TANK_RADIUS) * (TANK_RADIUS + TANK_RADIUS))
/* (Sensor range + tank radius)^2
* If the distance^2 to the center of a tank <= TANK_SENSOR_ADJ2,
* that tank is within sensor range. */
#define TANK_SENSOR_ADJ2 ((int)11556)
#define TANK_SENSOR_ADJ2 \
((TANK_SENSOR_RANGE + TANK_RADIUS) * (TANK_SENSOR_RANGE + TANK_RADIUS))
struct tanks_game {
bs_t size[2]; /* dimensions of playing field */
float size[2]; /* dimensions of playing field */
};
struct tank;
struct sensor {
brad_t angle;
brad_t width;
int range;
int range_adj2; /* (range + TANK_RADIUS)^2 */
int turret; /* Mounted to turret? */
int triggered;
int angle;
int width;
float range;
float range_adj2; /* (range + TANK_RADIUS)^2 */
int turret; /* Mounted to turret? */
int triggered;
};
typedef void tank_run_func(struct tank *, void *);
struct tank {
bs_t position[2]; /* Current position on the board */
brad_t angle; /* Current orientation */
float position[2]; /* Current position on the board */
int angle; /* Current orientation */
struct {
bs_t current[2]; /* Current tread speed */
bs_t desired[2]; /* Desired tread speed */
float current[2]; /* Current tread speed */
float desired[2]; /* Desired tread speed */
} speed;
struct {
brad_t current; /* Current turret angle */
brad_t desired; /* Desired turret angle */
int current; /* Current turret angle */
int desired; /* Desired turret angle */
int firing; /* True if firing this turn */
int recharge; /* Turns until gun is recharged */
} turret;
struct sensor sensor[MAX_SENSORS]; /* Sensor array */
int num_sensors; /* Number of sensors */
int led; /* State of the LED */
struct tank *killer; /* Killer, or NULL if alive */
char *cause_death; /* Cause of death */
struct sensor sensor[TANK_MAX_SENSORS]; /* Sensor array */
int num_sensors; /* Number of sensors */
int led; /* State of the LED */
struct tank *killer; /* Killer, or NULL if alive */
char *cause_death; /* Cause of death */
tank_run_func *run; /* Function to run a tank */
void *udata; /* Argument to pass to run */
@ -72,7 +73,7 @@ int tank_fire_ready(struct tank *tank);
void tank_fire(struct tank *tank);
/** Set desired speed */
void tank_set_speed(struct tank *tank, int left, int right);
void tank_set_speed(struct tank *tank, float left, float right);
/** Get the current turret angle */
int tank_get_turret(struct tank *tank);

93
trig.c Normal file
View File

@ -0,0 +1,93 @@
#include <stdlib.h>
#include <math.h>
#include "trig.h"
#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif
static float trig_cos_table[] = {
1.000000, 0.999848, 0.999391, 0.998630, 0.997564, 0.996195,
0.994522, 0.992546, 0.990268, 0.987688, 0.984808, 0.981627,
0.978148, 0.974370, 0.970296, 0.965926, 0.961262, 0.956305,
0.951057, 0.945519, 0.939693, 0.933580, 0.927184, 0.920505,
0.913545, 0.906308, 0.898794, 0.891007, 0.882948, 0.874620,
0.866025, 0.857167, 0.848048, 0.838671, 0.829038, 0.819152,
0.809017, 0.798636, 0.788011, 0.777146, 0.766044, 0.754710,
0.743145, 0.731354, 0.719340, 0.707107, 0.694658, 0.681998,
0.669131, 0.656059, 0.642788, 0.629320, 0.615661, 0.601815,
0.587785, 0.573576, 0.559193, 0.544639, 0.529919, 0.515038,
0.500000, 0.484810, 0.469472, 0.453990, 0.438371, 0.422618,
0.406737, 0.390731, 0.374607, 0.358368, 0.342020, 0.325568,
0.309017, 0.292372, 0.275637, 0.258819, 0.241922, 0.224951,
0.207912, 0.190809, 0.173648, 0.156434, 0.139173, 0.121869,
0.104528, 0.087156, 0.069756, 0.052336, 0.034899, 0.017452,
0.000000,
};
float
trig_cos(int angle)
{
int a;
float ret;
a = abs(angle) % 180;
if (a > 90) {
a = 180 - a;
}
ret = trig_cos_table[a];
a = abs(angle) % 360;
if ((a > 90) && (a < 270)) {
ret = -ret;
}
return ret;
}
float
trig_sin(int angle)
{
return trig_cos(90 - angle);
}
float
trig_tan(int angle)
{
return trig_sin(angle) / trig_cos(angle);
}
int
trig_asin(float x)
{
return rad2deg(asinf(x));
}
int
trig_acos(float x)
{
return rad2deg(acosf(x));
}
int
trig_atan2(float y, float x)
{
return rad2deg(atan2f(y, x));
}
#if 0
#include <stdio.h>
int
main()
{
int i, j;
float f;
int t;
for (i = 0; i < 600; i += 1) {
printf("%-04d %-0.6f %-0.6f\n", i, trig_cos(i), cosf(deg2rad(i)));
}
return 0;
}
#endif

25
trig.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef __TRIG_H__
#define __TRIG_H__
/** Trigonometry in degrees
*
* It's just a library to do discrete(-ish) trig, using degrees. It
* uses a lookup table to make sin, cos, and tan really fast.
* Everything is as slow as normal :)
*/
/* Just to make it clear what scale these functions are dealing with */
#define PI 3.14159265358979323846
#define rad2deg(rad) ((int)(rad * 180 / PI))
#define deg2rad(deg) ((float)(deg * PI / 180))
float trig_cos(int angle);
float trig_sin(int angle);
float trig_atan(int angle);
int trig_acos(float angle);
int trig_asin(float angle);
int trig_atan2(float y, float x);
#endif /* __TRIG_H__ */

12
trig.py Executable file
View File

@ -0,0 +1,12 @@
#! /usr/bin/python
import math
print "static float trig_cos_table[] = {"
for i in range(91):
r = (i * math.pi) / 180.0
cos = math.cos(r)
print ("%0f," % cos),
if 5 == i % 6:
print
print "};"