tanks/ctanks.h

113 lines
3.1 KiB
C
Raw Normal View History

2010-06-25 16:15:09 -06:00
#ifndef __CTANKS_H__
#define __CTANKS_H__
2010-07-22 18:08:55 -06:00
/* τ = 2π */
#define TAU 6.28318530717958647692
#define PI 3.14159265358979323846
2010-07-06 13:53:17 -06:00
2010-07-22 18:08:55 -06:00
/* Some in-game constants */
#define TANK_MAX_SENSORS 10
#define TANK_RADIUS 7.5
#define TANK_SENSOR_RANGE 100
#define TANK_CANNON_RECHARGE 20 /* Turns to recharge cannon */
#define TANK_CANNON_RANGE (TANK_SENSOR_RANGE / 2)
#define TANK_MAX_ACCEL 35
2014-07-28 11:22:00 -06:00
#define TANK_MAX_TURRET_ROT (TAU/8)
#define TANK_TOP_SPEED 7
2014-07-28 14:32:24 -06:00
#define TANK_FRICTION 0.75
2010-06-25 16:15:09 -06:00
/* (tank radius + tank radius)^2 */
#define TANK_COLLISION_ADJ2 \
((TANK_RADIUS + TANK_RADIUS) * (TANK_RADIUS + TANK_RADIUS))
2010-06-25 16:15:09 -06:00
/* (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 \
((TANK_SENSOR_RANGE + TANK_RADIUS) * (TANK_SENSOR_RANGE + TANK_RADIUS))
2010-06-25 16:15:09 -06:00
#define TANK_CANNON_ADJ2 \
((TANK_CANNON_RANGE + TANK_RADIUS) * (TANK_CANNON_RANGE + TANK_RADIUS))
2010-07-06 13:53:17 -06:00
#ifndef rad2deg
#define rad2deg(r) ((int)(360*(r)/TAU))
#define deg2rad(r) ((r*TAU)/360)
2010-07-06 13:53:17 -06:00
#endif
#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif
2010-06-25 16:15:09 -06:00
struct tanks_game {
float size[2]; /* dimensions of playing field */
2010-06-25 16:15:09 -06:00
};
struct tank;
struct sensor {
2010-07-06 13:53:17 -06:00
float angle;
float width;
int range;
int turret; /* Mounted to turret? */
int triggered;
2010-06-25 16:15:09 -06:00
};
typedef void tank_run_func(struct tank *, void *);
struct tank {
float position[2]; /* Current position on the board */
2010-07-06 13:53:17 -06:00
float angle; /* Current orientation */
2010-06-25 16:15:09 -06:00
struct {
float current[2]; /* Current tread speed */
float desired[2]; /* Desired tread speed */
2010-06-25 16:15:09 -06:00
} speed;
struct {
2010-07-06 13:53:17 -06:00
float current; /* Current turret angle */
float desired; /* Desired turret angle */
2010-06-25 16:15:09 -06:00
int firing; /* True if firing this turn */
int recharge; /* Turns until gun is recharged */
} turret;
2010-07-06 13:53:17 -06:00
struct sensor sensors[TANK_MAX_SENSORS]; /* Sensor array */
int led; /* State of the LED */
struct tank *killer; /* Killer, or NULL if alive */
char *cause_death; /* Cause of death */
2010-06-25 16:15:09 -06:00
tank_run_func *run; /* Function to run a tank */
void *udata; /* Argument to pass to run */
};
void tank_init(struct tank *tank, tank_run_func *run, void *udata);
2010-07-19 17:23:30 -06:00
void tanks_run_turn(struct tanks_game *game, struct tank *tanks, int ntanks);
2010-06-25 16:15:09 -06:00
/*
*
* Tanks API for scripts
*
*/
/** Has the turret recharged? */
int tank_fire_ready(struct tank *tank);
/** Fire! */
void tank_fire(struct tank *tank);
/** Set desired speed */
void tank_set_speed(struct tank *tank, float left, float right);
2010-06-25 16:15:09 -06:00
/** Get the current turret angle */
2010-07-06 13:53:17 -06:00
float tank_get_turret(struct tank *tank);
2010-06-25 16:15:09 -06:00
/** Set the desired turret angle */
2010-07-06 13:53:17 -06:00
void tank_set_turret(struct tank *tank, float angle);
2010-06-25 16:15:09 -06:00
/** Is a sensor active? */
int tank_get_sensor(struct tank *tank, int sensor_num);
/** Set the LED state */
void tank_set_led(struct tank *tank, int active);
2010-07-19 17:23:30 -06:00
2010-06-25 16:15:09 -06:00
#endif /* __CTANKS_H__ */