tanks/ctanks.h

108 lines
3.0 KiB
C
Raw Normal View History

#pragma once
2010-06-25 16:15:09 -06:00
2010-07-22 18:08:55 -06:00
/* τ = 2π */
#define TAU 6.28318530717958647692
2024-12-04 18:40:53 -07:00
#define PI 3.14159265358979323846
2010-07-06 13:53:17 -06:00
2010-07-22 18:08:55 -06:00
/* Some in-game constants */
2024-12-04 18:40:53 -07:00
#define TANK_MAX_SENSORS 10
#define TANK_RADIUS 7.5
#define TANK_SENSOR_RANGE 100
#define TANK_CANNON_RECHARGE 20 /* Turns to recharge cannon */
2024-12-04 18:40:53 -07:00
#define TANK_CANNON_RANGE (TANK_SENSOR_RANGE / 2)
#define TANK_MAX_ACCEL 35
#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 */
2024-12-04 18:40:53 -07:00
#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. */
2024-12-04 18:40:53 -07:00
#define TANK_SENSOR_ADJ2 \
((TANK_SENSOR_RANGE + TANK_RADIUS) * (TANK_SENSOR_RANGE + TANK_RADIUS))
2010-06-25 16:15:09 -06:00
2024-12-04 18:40:53 -07: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
2024-12-04 18:40:53 -07:00
#define rad2deg(r) ((int)(360 * (r) / TAU))
#define deg2rad(r) ((r * TAU) / 360)
2010-07-06 13:53:17 -06:00
#endif
#ifndef max
2024-12-04 18:40:53 -07:00
#define max(a, b) (((a) > (b)) ? (a) : (b))
#define min(a, b) (((a) < (b)) ? (a) : (b))
2010-07-06 13:53:17 -06:00
#endif
2010-06-25 16:15:09 -06:00
struct tanks_game {
2024-12-04 18:40:53 -07:00
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;
2024-12-04 18:40:53 -07:00
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 {
2024-12-04 18:40:53 -07:00
float position[2]; /* Current position on the board */
float angle; /* Current orientation */
2010-06-25 16:15:09 -06:00
struct {
2024-12-04 18:40:53 -07:00
float current[2]; /* Current tread speed */
float desired[2]; /* Desired tread speed */
2010-06-25 16:15:09 -06:00
} speed;
struct {
2024-12-04 18:40:53 -07:00
float current; /* Current turret angle */
float desired; /* Desired turret angle */
int firing; /* True if firing this turn */
int recharge; /* Turns until gun is recharged */
2010-06-25 16:15:09 -06:00
} turret;
2024-12-04 18:40:53 -07: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
2024-12-04 18:40:53 -07:00
tank_run_func *run; /* Function to run a tank */
void *udata; /* Argument to pass to run */
2010-06-25 16:15:09 -06:00
};
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);