tanks

Blow up enemy tanks using code
git clone https://git.woozle.org/neale/tanks.git

Neale Pickett  ·  2024-12-04

ctanks.h

  1#pragma once
  2
  3/* τ = 2π */
  4#define TAU 6.28318530717958647692
  5#define PI 3.14159265358979323846
  6
  7/* Some in-game constants */
  8#define TANK_MAX_SENSORS 10
  9#define TANK_RADIUS 7.5
 10#define TANK_SENSOR_RANGE 100
 11#define TANK_CANNON_RECHARGE 20 /* Turns to recharge cannon */
 12#define TANK_CANNON_RANGE (TANK_SENSOR_RANGE / 2)
 13#define TANK_MAX_ACCEL 35
 14#define TANK_MAX_TURRET_ROT (TAU / 8)
 15#define TANK_TOP_SPEED 7
 16#define TANK_FRICTION 0.75
 17
 18/* (tank radius + tank radius)^2 */
 19#define TANK_COLLISION_ADJ2                                                    \
 20  ((TANK_RADIUS + TANK_RADIUS) * (TANK_RADIUS + TANK_RADIUS))
 21
 22/* (Sensor range + tank radius)^2
 23 * If the distance^2 to the center of a tank <= TANK_SENSOR_ADJ2,
 24 * that tank is within sensor range. */
 25#define TANK_SENSOR_ADJ2                                                       \
 26  ((TANK_SENSOR_RANGE + TANK_RADIUS) * (TANK_SENSOR_RANGE + TANK_RADIUS))
 27
 28#define TANK_CANNON_ADJ2                                                       \
 29  ((TANK_CANNON_RANGE + TANK_RADIUS) * (TANK_CANNON_RANGE + TANK_RADIUS))
 30
 31#ifndef rad2deg
 32#define rad2deg(r) ((int)(360 * (r) / TAU))
 33#define deg2rad(r) ((r * TAU) / 360)
 34#endif
 35
 36#ifndef max
 37#define max(a, b) (((a) > (b)) ? (a) : (b))
 38#define min(a, b) (((a) < (b)) ? (a) : (b))
 39#endif
 40
 41struct tanks_game {
 42  float size[2]; /* dimensions of playing field */
 43};
 44
 45struct tank;
 46
 47struct sensor {
 48  float angle;
 49  float width;
 50  int range;
 51  int turret; /* Mounted to turret? */
 52  int triggered;
 53};
 54
 55typedef void tank_run_func(struct tank *, void *);
 56
 57struct tank {
 58  float position[2]; /* Current position on the board */
 59  float angle;       /* Current orientation */
 60  struct {
 61    float current[2]; /* Current tread speed */
 62    float desired[2]; /* Desired tread speed */
 63  } speed;
 64  struct {
 65    float current; /* Current turret angle */
 66    float desired; /* Desired turret angle */
 67    int firing;    /* True if firing this turn */
 68    int recharge;  /* Turns until gun is recharged */
 69  } turret;
 70  struct sensor sensors[TANK_MAX_SENSORS]; /* Sensor array */
 71  int led;                                 /* State of the LED */
 72  struct tank *killer;                     /* Killer, or NULL if alive */
 73  char *cause_death;                       /* Cause of death */
 74
 75  tank_run_func *run; /* Function to run a tank */
 76  void *udata;        /* Argument to pass to run */
 77};
 78
 79void tank_init(struct tank *tank, tank_run_func *run, void *udata);
 80void tanks_run_turn(struct tanks_game *game, struct tank *tanks, int ntanks);
 81
 82/*
 83 *
 84 * Tanks API for scripts
 85 *
 86 */
 87
 88/** Has the turret recharged? */
 89int tank_fire_ready(struct tank *tank);
 90
 91/** Fire! */
 92void tank_fire(struct tank *tank);
 93
 94/** Set desired speed */
 95void tank_set_speed(struct tank *tank, float left, float right);
 96
 97/** Get the current turret angle */
 98float tank_get_turret(struct tank *tank);
 99
100/** Set the desired turret angle */
101void tank_set_turret(struct tank *tank, float angle);
102
103/** Is a sensor active? */
104int tank_get_sensor(struct tank *tank, int sensor_num);
105
106/** Set the LED state */
107void tank_set_led(struct tank *tank, int active);