diff --git a/ctanks.c b/ctanks.c index 47d6bd9..3d90f50 100644 --- a/ctanks.c +++ b/ctanks.c @@ -1,11 +1,13 @@ -#include -#include -#include -#include #include "ctanks.h" +#include +#include +#include +#include /* Debugging help */ -#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_d(v) DUMPf("%s = %d", #v, v) #define DUMP_x(v) DUMPf("%s = 0x%x", #v, v) @@ -14,94 +16,63 @@ #define DUMP_f(v) DUMPf("%s = %f", #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_angle(v) DUMPf("%s = %.3fτ", #v, (v/TAU)); +#define DUMP_angle(v) DUMPf("%s = %.3fτ", #v, (v / TAU)); #define sq(x) ((x) * (x)) - -void -tank_init(struct tank *tank, tank_run_func *run, void *udata) -{ +void tank_init(struct tank *tank, tank_run_func *run, void *udata) { memset(tank, 0, sizeof(*tank)); tank->run = run; tank->udata = udata; } -int -tank_fire_ready(struct tank *tank) -{ - return (! tank->turret.recharge); -} +int tank_fire_ready(struct tank *tank) { return (!tank->turret.recharge); } -void -tank_fire(struct tank *tank) -{ +void tank_fire(struct tank *tank) { tank->turret.firing = tank_fire_ready(tank); } -void -tank_set_speed(struct tank *tank, float left, float right) -{ +void tank_set_speed(struct tank *tank, float left, float right) { tank->speed.desired[0] = min(max(left, -100), 100); tank->speed.desired[1] = min(max(right, -100), 100); } -float -tank_get_turret(struct tank *tank) -{ - return tank->turret.current; -} +float tank_get_turret(struct tank *tank) { return tank->turret.current; } -void -tank_set_turret(struct tank *tank, float angle) -{ +void tank_set_turret(struct tank *tank, float angle) { tank->turret.desired = fmodf(angle, TAU); } -int -tank_get_sensor(struct tank *tank, int sensor_num) -{ +int tank_get_sensor(struct tank *tank, int sensor_num) { if ((sensor_num < 0) || (sensor_num > TANK_MAX_SENSORS)) { return 0; } return tank->sensors[sensor_num].triggered; } -void -tank_set_led(struct tank *tank, int active) -{ - tank->led = active; -} +void tank_set_led(struct tank *tank, int active) { tank->led = active; } -static void -rotate_point(float angle, float point[2]) -{ +static void rotate_point(float angle, float point[2]) { float cos_, sin_; float new[2]; cos_ = cosf(angle); sin_ = sinf(angle); - new[0] = point[0]*cos_ - point[1]*sin_; - new[1] = point[0]*sin_ + point[1]*cos_; + new[0] = point[0] * cos_ - point[1] * sin_; + new[1] = point[0] * sin_ + point[1] * cos_; point[0] = new[0]; point[1] = new[1]; } - -static void -tanks_fire_cannon(struct tanks_game *game, - struct tank *this, - struct tank *that, - float vector[2], - float dist2) -{ +static void tanks_fire_cannon(struct tanks_game *game, struct tank *this, + struct tank *that, float vector[2], float dist2) { float theta = this->angle + this->turret.current; float rpos[2]; - /* If someone's a crater, this is easy (unless we were just killed by the other one, in which case - we have to check the other direction) */ + /* If someone's a crater, this is easy (unless we were just killed by the + other one, in which case we have to check the other direction) */ if ((this->killer && this->killer != that) || that->killer) { return; } @@ -118,7 +89,7 @@ tanks_fire_cannon(struct tanks_game *game, } /* No need to check if it's not even firing */ - if (! this->turret.firing) { + if (!this->turret.firing) { return; } @@ -138,14 +109,9 @@ tanks_fire_cannon(struct tanks_game *game, } } -static void -tanks_sensor_calc(struct tanks_game *game, - struct tank *this, - struct tank *that, - float vector[2], - float dist2) -{ - int i; +static void tanks_sensor_calc(struct tanks_game *game, struct tank *this, + struct tank *that, float vector[2], float dist2) { + int i; /* If someone's a crater, this is easy */ if (this->killer || that->killer) { @@ -211,14 +177,9 @@ tanks_sensor_calc(struct tanks_game *game, } } -void -compute_vector(struct tanks_game *game, - float vector[2], - float *dist2, - struct tank *this, - struct tank *that) -{ - int i; +void compute_vector(struct tanks_game *game, float vector[2], float *dist2, + struct tank *this, struct tank *that) { + int i; /* Establish shortest vector from center of this to center of that, * taking wrapping into account */ @@ -228,8 +189,7 @@ compute_vector(struct tanks_game *game, vector[i] = that->position[i] - this->position[i]; if (vector[i] > halfsize) { vector[i] = vector[i] - game->size[i]; - } - else if (vector[i] < -halfsize) { + } else if (vector[i] < -halfsize) { vector[i] = game->size[i] + vector[i]; } } @@ -238,18 +198,15 @@ compute_vector(struct tanks_game *game, *dist2 = sq(vector[0]) + sq(vector[1]); } -void -tanks_move_tank(struct tanks_game *game, - struct tank *tank) -{ - int i; +void tanks_move_tank(struct tanks_game *game, struct tank *tank) { + int i; float movement; float angle; - int dir = 1; + int dir = 1; /* Rotate the turret */ { - float rot_angle; /* Quickest way there */ + float rot_angle; /* Quickest way there */ /* Constrain rot_angle to between -PI and PI */ rot_angle = tank->turret.desired - tank->turret.current; @@ -268,18 +225,18 @@ tanks_move_tank(struct tanks_game *game, 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] = min(tank->speed.current[i] + TANK_MAX_ACCEL, - tank->speed.desired[i]); + tank->speed.current[i] = + min(tank->speed.current[i] + TANK_MAX_ACCEL, tank->speed.desired[i]); } else { - tank->speed.current[i] = max(tank->speed.current[i] - TANK_MAX_ACCEL, - tank->speed.desired[i]); + tank->speed.current[i] = + max(tank->speed.current[i] - TANK_MAX_ACCEL, tank->speed.desired[i]); } } /* The simple case */ if (tank->speed.current[0] == tank->speed.current[1]) { movement = tank->speed.current[0] * (TANK_TOP_SPEED / 100.0); - angle = 0; + angle = 0; } else { /* pflarr's original comment: * @@ -301,7 +258,8 @@ tanks_move_tank(struct tanks_game *game, 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 = TANK_FRICTION * (fabsf(tank->speed.current[0] - tank->speed.current[1]) / 200); + friction = TANK_FRICTION * + (fabsf(tank->speed.current[0] - tank->speed.current[1]) / 200); v[0] = tank->speed.current[0] * (1 - friction) * (TANK_TOP_SPEED / 100.0); v[1] = tank->speed.current[1] * (1 - friction) * (TANK_TOP_SPEED / 100.0); @@ -330,7 +288,7 @@ tanks_move_tank(struct tanks_game *game, theta = So/r We multiply it by dir to adjust for the direction of rotation */ - theta = So/r * dir; + theta = So / r * dir; movement = r * tanf(theta); angle = theta; @@ -345,18 +303,16 @@ tanks_move_tank(struct tanks_game *game, m[1] = sinf(tank->angle) * movement * dir; for (i = 0; i < 2; i += 1) { - tank->position[i] = fmodf(tank->position[i] + m[i] + game->size[i], - game->size[i]); + tank->position[i] = + fmodf(tank->position[i] + m[i] + game->size[i], game->size[i]); } } } -void -tanks_run_turn(struct tanks_game *game, struct tank *tanks, int ntanks) -{ - int i, j; +void tanks_run_turn(struct tanks_game *game, struct tank *tanks, int ntanks) { + int i, j; float vector[2]; - float dist2; /* distance squared */ + float dist2; /* distance squared */ /* It takes (at least) two to tank-o */ if (ntanks < 2) { @@ -369,7 +325,8 @@ tanks_run_turn(struct tanks_game *game, struct tank *tanks, int ntanks) tanks[i].turret.firing = 0; tanks[i].turret.recharge = TANK_CANNON_RECHARGE; } - if (tanks[i].killer) continue; + if (tanks[i].killer) + continue; if (tanks[i].turret.recharge) { tanks[i].turret.recharge -= 1; } @@ -380,13 +337,15 @@ tanks_run_turn(struct tanks_game *game, struct tank *tanks, int ntanks) /* Move tanks */ for (i = 0; i < ntanks; i += 1) { - if (tanks[i].killer) continue; + if (tanks[i].killer) + continue; tanks_move_tank(game, &(tanks[i])); } /* Probe sensors */ for (i = 0; i < ntanks; i += 1) { - if (tanks[i].killer) continue; + if (tanks[i].killer) + continue; for (j = i + 1; j < ntanks; j += 1) { struct tank *this = &tanks[i]; struct tank *that = &tanks[j]; @@ -401,13 +360,15 @@ tanks_run_turn(struct tanks_game *game, struct tank *tanks, int ntanks) /* Run programs */ for (i = 0; i < ntanks; i += 1) { - if (tanks[i].killer) continue; + if (tanks[i].killer) + continue; tanks[i].run(&tanks[i], tanks[i].udata); } /* Fire cannons and check for crashes */ for (i = 0; i < ntanks; i += 1) { - if (tanks[i].killer) continue; + if (tanks[i].killer) + continue; for (j = i + 1; j < ntanks; j += 1) { struct tank *this = &tanks[i]; struct tank *that = &tanks[j]; diff --git a/ctanks.h b/ctanks.h index cbab3cf..1200d28 100644 --- a/ctanks.h +++ b/ctanks.h @@ -2,44 +2,44 @@ /* τ = 2π */ #define TAU 6.28318530717958647692 -#define PI 3.14159265358979323846 +#define PI 3.14159265358979323846 /* Some in-game constants */ -#define TANK_MAX_SENSORS 10 -#define TANK_RADIUS 7.5 -#define TANK_SENSOR_RANGE 100 +#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 -#define TANK_MAX_TURRET_ROT (TAU/8) -#define TANK_TOP_SPEED 7 +#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 #define TANK_FRICTION 0.75 /* (tank radius + tank radius)^2 */ -#define TANK_COLLISION_ADJ2 \ +#define TANK_COLLISION_ADJ2 \ ((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 \ +#define TANK_SENSOR_ADJ2 \ ((TANK_SENSOR_RANGE + TANK_RADIUS) * (TANK_SENSOR_RANGE + TANK_RADIUS)) -#define TANK_CANNON_ADJ2 \ +#define TANK_CANNON_ADJ2 \ ((TANK_CANNON_RANGE + TANK_RADIUS) * (TANK_CANNON_RANGE + TANK_RADIUS)) #ifndef rad2deg -#define rad2deg(r) ((int)(360*(r)/TAU)) -#define deg2rad(r) ((r*TAU)/360) +#define rad2deg(r) ((int)(360 * (r) / TAU)) +#define deg2rad(r) ((r * TAU) / 360) #endif #ifndef max -#define max(a,b) (((a) > (b)) ? (a) : (b)) -#define min(a,b) (((a) < (b)) ? (a) : (b)) +#define max(a, b) (((a) > (b)) ? (a) : (b)) +#define min(a, b) (((a) < (b)) ? (a) : (b)) #endif struct tanks_game { - float size[2]; /* dimensions of playing field */ + float size[2]; /* dimensions of playing field */ }; struct tank; @@ -47,39 +47,38 @@ struct tank; struct sensor { float angle; float width; - int range; - int turret; /* Mounted to turret? */ - int triggered; + int range; + int turret; /* Mounted to turret? */ + int triggered; }; typedef void tank_run_func(struct tank *, void *); struct tank { - float position[2]; /* Current position on the board */ - float angle; /* Current orientation */ + float position[2]; /* Current position on the board */ + float angle; /* Current orientation */ struct { - float current[2]; /* Current tread speed */ - float desired[2]; /* Desired tread speed */ + float current[2]; /* Current tread speed */ + float desired[2]; /* Desired tread speed */ } speed; struct { - float current; /* Current turret angle */ - float desired; /* Desired turret angle */ - int firing; /* True if firing this turn */ - int recharge; /* Turns until gun is recharged */ + float current; /* Current turret angle */ + float desired; /* Desired turret angle */ + int firing; /* True if firing this turn */ + int recharge; /* Turns until gun is recharged */ } turret; - 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 */ + 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 */ - tank_run_func *run; /* Function to run a tank */ - void *udata; /* Argument to pass to run */ + 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); void tanks_run_turn(struct tanks_game *game, struct tank *tanks, int ntanks); - /* * * Tanks API for scripts diff --git a/dump.h b/dump.h index 7cdfb2a..1a36db7 100644 --- a/dump.h +++ b/dump.h @@ -7,7 +7,9 @@ #endif /* 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_d(v) DUMPf("%s = %d", #v, v) #define DUMP_x(v) DUMPf("%s = 0x%x", #v, v) @@ -16,15 +18,17 @@ #define DUMP_f(v) DUMPf("%s = %f", #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_angle(v) DUMPf("%s = %.3fτ", #v, (v/TAU)); +#define DUMP_angle(v) DUMPf("%s = %.3fτ", #v, (v / TAU)); /* 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_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_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) diff --git a/forf.c b/forf.c index b85de82..45e1ffc 100644 --- a/forf.c +++ b/forf.c @@ -27,28 +27,22 @@ * and not new stack types. */ +#include #include #include #include -#include -#include "forf.h" #include "dump.h" +#include "forf.h" #ifndef max -#define max(a,b) (((a) > (b)) ? (a) : (b)) -#define min(a,b) (((a) < (b)) ? (a) : (b)) +#define max(a, b) (((a) > (b)) ? (a) : (b)) +#define min(a, b) (((a) < (b)) ? (a) : (b)) #endif char *forf_error_str[] = { - "None", - "Runtime", - "Parse", - "Underflow", - "Overflow", - "Type", - "No such procedure", - "Divide by zero", + "None", "Runtime", "Parse", "Underflow", "Overflow", + "Type", "No such procedure", "Divide by zero", }; /* @@ -56,47 +50,29 @@ char *forf_error_str[] = { * Memory manipulation * */ -void -forf_memory_init(struct forf_memory *m, - long *values, - size_t size) -{ - m->mem = values; +void forf_memory_init(struct forf_memory *m, long *values, size_t size) { + m->mem = values; m->size = size; } - /* * * Stack manipulation * */ -void -forf_stack_init(struct forf_stack *s, - struct forf_value *values, - size_t size) -{ +void forf_stack_init(struct forf_stack *s, struct forf_value *values, + size_t size) { s->stack = values; s->size = size; s->top = 0; } -void -forf_stack_reset(struct forf_stack *s) -{ - s->top = 0; -} +void forf_stack_reset(struct forf_stack *s) { s->top = 0; } -size_t -forf_stack_len(struct forf_stack *s) -{ - return s->top; -} +size_t forf_stack_len(struct forf_stack *s) { return s->top; } -int -forf_stack_push(struct forf_stack *s, struct forf_value *v) -{ +int forf_stack_push(struct forf_stack *s, struct forf_value *v) { if (s->top == s->size) { return 0; } @@ -104,9 +80,7 @@ forf_stack_push(struct forf_stack *s, struct forf_value *v) return 1; } -int -forf_stack_pop(struct forf_stack *s, struct forf_value *v) -{ +int forf_stack_pop(struct forf_stack *s, struct forf_value *v) { if (0 == s->top) { return 0; } @@ -114,23 +88,18 @@ forf_stack_pop(struct forf_stack *s, struct forf_value *v) return 1; } -void -forf_stack_copy(struct forf_stack *dst, struct forf_stack *src) -{ +void forf_stack_copy(struct forf_stack *dst, struct forf_stack *src) { int top = min(dst->size, src->top); dst->top = top; memcpy(dst->stack, src->stack, sizeof(*dst->stack) * top); } - -void -forf_stack_reverse(struct forf_stack *s) -{ +void forf_stack_reverse(struct forf_stack *s) { struct forf_value val; size_t pos; - for (pos = 0; pos < (s->top)/2; pos += 1) { + for (pos = 0; pos < (s->top) / 2; pos += 1) { size_t qos = s->top - pos - 1; val = s->stack[pos]; @@ -139,12 +108,10 @@ forf_stack_reverse(struct forf_stack *s) } } -long -forf_pop_num(struct forf_env *env) -{ +long forf_pop_num(struct forf_env *env) { struct forf_value val; - if (! forf_stack_pop(env->data, &val)) { + if (!forf_stack_pop(env->data, &val)) { env->error = forf_error_underflow; return 0; } @@ -156,19 +123,16 @@ forf_pop_num(struct forf_env *env) return val.v.i; } -void -forf_push_num(struct forf_env *env, long i) -{ +void forf_push_num(struct forf_env *env, long i) { struct forf_value val; val.type = forf_type_number; val.v.i = i; - if (! forf_stack_push(env->data, &val)) { + if (!forf_stack_push(env->data, &val)) { env->error = forf_error_overflow; } } - /* Pop an entire stack * * DANGER WILL ROBINSON @@ -177,14 +141,12 @@ forf_push_num(struct forf_env *env, long i) * finished with this stack before you push anything onto the data * stack, otherwise your returned stack will be corrupted. */ -struct forf_stack -forf_pop_stack(struct forf_env *env) -{ - struct forf_stack s = { 0, 0, NULL }; +struct forf_stack forf_pop_stack(struct forf_env *env) { + struct forf_stack s = {0, 0, NULL}; struct forf_value val; - size_t depth = 1; + size_t depth = 1; - if (! forf_stack_pop(env->data, &val)) { + if (!forf_stack_pop(env->data, &val)) { env->error = forf_error_underflow; return s; } @@ -198,7 +160,7 @@ forf_pop_stack(struct forf_env *env) s.size = -1; while (depth) { s.size += 1; - if (! forf_stack_pop(env->data, &val)) { + if (!forf_stack_pop(env->data, &val)) { /* You should never underflow here, there should at least be a stack begin marker. */ env->error = forf_error_runtime; @@ -206,14 +168,14 @@ forf_pop_stack(struct forf_env *env) return s; } switch (val.type) { - case forf_type_stack_end: - depth += 1; - break; - case forf_type_stack_begin: - depth -= 1; - break; - default: - break; + case forf_type_stack_end: + depth += 1; + break; + case forf_type_stack_begin: + depth -= 1; + break; + default: + break; } } s.top = s.size; @@ -223,13 +185,11 @@ forf_pop_stack(struct forf_env *env) /* Push an entire stack onto another stack. */ -int -forf_push_stack(struct forf_stack *dst, struct forf_stack *src) -{ +int forf_push_stack(struct forf_stack *dst, struct forf_stack *src) { struct forf_value val; while (forf_stack_pop(src, &val)) { - if (! forf_stack_push(dst, &val)) { + if (!forf_stack_push(dst, &val)) { return 0; } } @@ -240,10 +200,8 @@ forf_push_stack(struct forf_stack *dst, struct forf_stack *src) * * This is meant to work with the return value from forf_pop_stack. */ -int -forf_push_to_command_stack(struct forf_env *env, struct forf_stack *src) -{ - if (! forf_push_stack(env->command, src)) { +int forf_push_to_command_stack(struct forf_env *env, struct forf_stack *src) { + if (!forf_push_stack(env->command, src)) { env->error = forf_error_overflow; return 0; } @@ -259,24 +217,21 @@ forf_push_to_command_stack(struct forf_env *env, struct forf_stack *src) * always have "reversed" substacks, and everything else will have them * in the right order. */ -int -forf_stack_move_value(struct forf_env *env, - struct forf_stack *dst, - struct forf_stack *src) -{ +int forf_stack_move_value(struct forf_env *env, struct forf_stack *dst, + struct forf_stack *src) { struct forf_value val; - size_t depth = 0; + size_t depth = 0; do { /* Pop from src */ - if (! forf_stack_pop(env->command, &val)) { + if (!forf_stack_pop(env->command, &val)) { env->error = forf_error_underflow; return 0; } /* Push to dst (or discard if dst is NULL) */ if (dst) { - if (! forf_stack_push(env->data, &val)) { + if (!forf_stack_push(env->data, &val)) { env->error = forf_error_overflow; return 0; } @@ -284,68 +239,49 @@ forf_stack_move_value(struct forf_env *env, /* Deal with it being a substack marker */ switch (val.type) { - case forf_type_stack_begin: - depth += 1; - break; - case forf_type_stack_end: - depth -= 1; - break; - default: - break; + case forf_type_stack_begin: + depth += 1; + break; + case forf_type_stack_end: + depth -= 1; + break; + default: + break; } } while (depth > 0); return 1; - } - /* * * Procedures * */ -#define unproc(name, op) \ - static void \ - forf_proc_ ## name(struct forf_env *env) \ - { \ - long a = forf_pop_num(env); \ - \ - forf_push_num(env, op a); \ +#define unproc(name, op) \ + static void forf_proc_##name(struct forf_env *env) { \ + long a = forf_pop_num(env); \ + \ + forf_push_num(env, op a); \ } -unproc(inv, ~) -unproc(not, !) +unproc(inv, ~) unproc(not, !) -#define binproc(name, op) \ - static void \ - forf_proc_ ## name(struct forf_env *env) \ - { \ - long a = forf_pop_num(env); \ - long b = forf_pop_num(env); \ - \ - forf_push_num(env, b op a); \ +#define binproc(name, op) \ + static void forf_proc_##name(struct forf_env *env) { \ + long a = forf_pop_num(env); \ + long b = forf_pop_num(env); \ + \ + forf_push_num(env, b op a); \ } -binproc(add, +) -binproc(sub, -) -binproc(mul, *) -binproc(and, &) -binproc(or, |) -binproc(xor, ^) -binproc(lshift, <<) -binproc(rshift, >>) -binproc(gt, >) -binproc(ge, >=) -binproc(lt, <) -binproc(le, <=) -binproc(eq, ==) -binproc(ne, !=) + binproc(add, +) binproc(sub, -) binproc(mul, *) binproc(and, &) + binproc(or, |) binproc(xor, ^) binproc(lshift, <<) binproc(rshift, >>) + binproc(gt, >) binproc(ge, >=) binproc(lt, <) binproc(le, <=) + binproc(eq, ==) binproc(ne, !=) -static void -forf_proc_div(struct forf_env *env) -{ + static void forf_proc_div(struct forf_env *env) { long a = forf_pop_num(env); long b = forf_pop_num(env); @@ -356,9 +292,7 @@ forf_proc_div(struct forf_env *env) forf_push_num(env, b / a); } -static void -forf_proc_mod(struct forf_env *env) -{ +static void forf_proc_mod(struct forf_env *env) { long a = forf_pop_num(env); long b = forf_pop_num(env); @@ -369,30 +303,20 @@ forf_proc_mod(struct forf_env *env) forf_push_num(env, b % a); } -static void -forf_proc_abs(struct forf_env *env) -{ +static void forf_proc_abs(struct forf_env *env) { forf_push_num(env, abs(forf_pop_num(env))); } -static void -forf_proc_dup(struct forf_env *env) -{ +static void forf_proc_dup(struct forf_env *env) { long a = forf_pop_num(env); forf_push_num(env, a); forf_push_num(env, a); } -static void -forf_proc_pop(struct forf_env *env) -{ - forf_pop_num(env); -} +static void forf_proc_pop(struct forf_env *env) { forf_pop_num(env); } -static void -forf_proc_exch(struct forf_env *env) -{ +static void forf_proc_exch(struct forf_env *env) { long a = forf_pop_num(env); long b = forf_pop_num(env); @@ -400,23 +324,19 @@ forf_proc_exch(struct forf_env *env) forf_push_num(env, b); } -static void -forf_proc_if(struct forf_env *env) -{ - struct forf_stack ifclause = forf_pop_stack(env); - long cond = forf_pop_num(env); +static void forf_proc_if(struct forf_env *env) { + struct forf_stack ifclause = forf_pop_stack(env); + long cond = forf_pop_num(env); if (cond) { forf_push_to_command_stack(env, &ifclause); } } -static void -forf_proc_ifelse(struct forf_env *env) -{ +static void forf_proc_ifelse(struct forf_env *env) { struct forf_stack elseclause = forf_pop_stack(env); - struct forf_stack ifclause = forf_pop_stack(env); - long cond = forf_pop_num(env); + struct forf_stack ifclause = forf_pop_stack(env); + long cond = forf_pop_num(env); if (cond) { forf_push_to_command_stack(env, &ifclause); @@ -425,11 +345,9 @@ forf_proc_ifelse(struct forf_env *env) } } -static void -forf_proc_memset(struct forf_env *env) -{ +static void forf_proc_memset(struct forf_env *env) { long pos = forf_pop_num(env); - long a = forf_pop_num(env); + long a = forf_pop_num(env); if (pos >= env->memory->size) { env->error = forf_error_overflow; @@ -439,9 +357,7 @@ forf_proc_memset(struct forf_env *env) env->memory->mem[pos] = a; } -static void -forf_proc_memget(struct forf_env *env) -{ +static void forf_proc_memget(struct forf_env *env) { long pos = forf_pop_num(env); if (pos >= env->memory->size) { @@ -457,70 +373,63 @@ forf_proc_memget(struct forf_env *env) * Lexical environment * */ -struct forf_lexical_env forf_base_lexical_env[] = { - {"~", forf_proc_inv}, - {"!", forf_proc_not}, - {"+", forf_proc_add}, - {"-", forf_proc_sub}, - {"*", forf_proc_mul}, - {"/", forf_proc_div}, - {"%", forf_proc_mod}, - {"&", forf_proc_and}, - {"|", forf_proc_or}, - {"^", forf_proc_xor}, - {"<<", forf_proc_lshift}, - {">>", forf_proc_rshift}, - {">", forf_proc_gt}, - {">=", forf_proc_ge}, - {"<", forf_proc_lt}, - {"<=", forf_proc_le}, - {"=", forf_proc_eq}, - {"<>", forf_proc_ne}, - {"abs", forf_proc_abs}, - {"dup", forf_proc_dup}, - {"pop", forf_proc_pop}, - {"exch", forf_proc_exch}, - {"if", forf_proc_if}, - {"ifelse", forf_proc_ifelse}, - {"mset", forf_proc_memset}, - {"mget", forf_proc_memget}, - {NULL, NULL} -}; +struct forf_lexical_env forf_base_lexical_env[] = {{"~", forf_proc_inv}, + {"!", forf_proc_not}, + {"+", forf_proc_add}, + {"-", forf_proc_sub}, + {"*", forf_proc_mul}, + {"/", forf_proc_div}, + {"%", forf_proc_mod}, + {"&", forf_proc_and}, + {"|", forf_proc_or}, + {"^", forf_proc_xor}, + {"<<", forf_proc_lshift}, + {">>", forf_proc_rshift}, + {">", forf_proc_gt}, + {">=", forf_proc_ge}, + {"<", forf_proc_lt}, + {"<=", forf_proc_le}, + {"=", forf_proc_eq}, + {"<>", forf_proc_ne}, + {"abs", forf_proc_abs}, + {"dup", forf_proc_dup}, + {"pop", forf_proc_pop}, + {"exch", forf_proc_exch}, + {"if", forf_proc_if}, + {"ifelse", forf_proc_ifelse}, + {"mset", forf_proc_memset}, + {"mget", forf_proc_memget}, + {NULL, NULL}}; /** Extend a lexical environment */ -int -forf_extend_lexical_env(struct forf_lexical_env *dest, - struct forf_lexical_env *src, - size_t size) -{ +int forf_extend_lexical_env(struct forf_lexical_env *dest, + struct forf_lexical_env *src, size_t size) { int base, i; - for (base = 0; dest[base].name; base += 1); - for (i = 0; (base+i < size) && (src[i].name); i += 1) { - dest[base+i] = src[i]; + for (base = 0; dest[base].name; base += 1) + ; + for (i = 0; (base + i < size) && (src[i].name); i += 1) { + dest[base + i] = src[i]; } if (base + i == size) { /* Not enough room */ return 0; } - dest[base+i].name = NULL; - dest[base+i].proc = NULL; + dest[base + i].name = NULL; + dest[base + i].proc = NULL; return 1; } - /* * * Parsing * */ -static int -forf_push_token(struct forf_env *env, char *token, size_t tokenlen) -{ - long i; - char s[MAX_TOKEN_LEN + 1]; - char *endptr; - struct forf_value val; +static int forf_push_token(struct forf_env *env, char *token, size_t tokenlen) { + long i; + char s[MAX_TOKEN_LEN + 1]; + char *endptr; + struct forf_value val; /* Zero-length token yields int:0 from strtol */ @@ -549,7 +458,7 @@ forf_push_token(struct forf_env *env, char *token, size_t tokenlen) } } - if (! forf_stack_push(env->command, &val)) { + if (!forf_stack_push(env->command, &val)) { env->error = forf_error_overflow; return 0; } @@ -558,25 +467,23 @@ forf_push_token(struct forf_env *env, char *token, size_t tokenlen) } /* Parse an input stream onto the command stack */ -int -forf_parse_stream(struct forf_env *env, - forf_getch_func *getch, - void *datum) -{ - int running = 1; - long pos = 0; - char token[MAX_TOKEN_LEN]; - size_t tokenlen = 0; +int forf_parse_stream(struct forf_env *env, forf_getch_func *getch, + void *datum) { + int running = 1; + long pos = 0; + char token[MAX_TOKEN_LEN]; + size_t tokenlen = 0; struct forf_value val; - size_t stack_depth = 0; - int comment = 0; + size_t stack_depth = 0; + int comment = 0; -#define _tokenize() \ - do { \ - if (tokenlen) { \ - if (! forf_push_token(env, token, tokenlen)) return pos; \ - tokenlen = 0; \ - } \ +#define _tokenize() \ + do { \ + if (tokenlen) { \ + if (!forf_push_token(env, token, tokenlen)) \ + return pos; \ + tokenlen = 0; \ + } \ } while (0) while (running) { @@ -588,54 +495,54 @@ forf_parse_stream(struct forf_env *env, /* Handle comments */ if (comment) { switch (c) { - case EOF: - env->error = forf_error_parse; - return pos; - case ')': - comment = 0; - break; + case EOF: + env->error = forf_error_parse; + return pos; + case ')': + comment = 0; + break; } continue; } switch (c) { - case EOF: - running = 0; - break; - case '(': - comment = 1; - break; - case ' ': - case '\f': - case '\n': - case '\r': - case '\t': - case '\v': - _tokenize(); - break; - case '{': - _tokenize(); - val.type = forf_type_stack_begin; - if (! forf_stack_push(env->command, &val)) { - env->error = forf_error_overflow; - return pos; - } - stack_depth += 1; - break; - case '}': - _tokenize(); - val.type = forf_type_stack_end; - if (! forf_stack_push(env->command, &val)) { - env->error = forf_error_overflow; - return pos; - } - stack_depth -= 1; - break; - default: - if (tokenlen < sizeof(token)) { - token[tokenlen++] = c; - } - break; + case EOF: + running = 0; + break; + case '(': + comment = 1; + break; + case ' ': + case '\f': + case '\n': + case '\r': + case '\t': + case '\v': + _tokenize(); + break; + case '{': + _tokenize(); + val.type = forf_type_stack_begin; + if (!forf_stack_push(env->command, &val)) { + env->error = forf_error_overflow; + return pos; + } + stack_depth += 1; + break; + case '}': + _tokenize(); + val.type = forf_type_stack_end; + if (!forf_stack_push(env->command, &val)) { + env->error = forf_error_overflow; + return pos; + } + stack_depth -= 1; + break; + default: + if (tokenlen < sizeof(token)) { + token[tokenlen++] = c; + } + break; } } _tokenize(); @@ -652,25 +559,19 @@ forf_parse_stream(struct forf_env *env, } struct forf_char_stream { - char *buf; - size_t len; - size_t pos; + char *buf; + size_t len; + size_t pos; }; -static int -forf_string_getch(struct forf_char_stream *stream) -{ +static int forf_string_getch(struct forf_char_stream *stream) { if (stream->pos >= stream->len) { return EOF; } return stream->buf[stream->pos++]; } -int -forf_parse_buffer(struct forf_env *env, - char *buf, - size_t len) -{ +int forf_parse_buffer(struct forf_env *env, char *buf, size_t len) { struct forf_char_stream stream; stream.buf = buf; @@ -680,78 +581,62 @@ forf_parse_buffer(struct forf_env *env, return forf_parse_stream(env, (forf_getch_func *)forf_string_getch, &stream); } -int -forf_parse_string(struct forf_env *env, - char *str) -{ +int forf_parse_string(struct forf_env *env, char *str) { return forf_parse_buffer(env, str, strlen(str)); } -int -forf_parse_file(struct forf_env *env, - FILE *f) -{ +int forf_parse_file(struct forf_env *env, FILE *f) { return forf_parse_stream(env, (forf_getch_func *)fgetc, f); } - /* * * Forf environment * */ -void -forf_env_init(struct forf_env *env, - struct forf_lexical_env *lenv, - struct forf_stack *data, - struct forf_stack *cmd, - struct forf_memory *mem, - void *udata) -{ - env->lenv = lenv; - env->data = data; +void forf_env_init(struct forf_env *env, struct forf_lexical_env *lenv, + struct forf_stack *data, struct forf_stack *cmd, + struct forf_memory *mem, void *udata) { + env->lenv = lenv; + env->data = data; env->command = cmd; - env->memory = mem; - env->udata = udata; + env->memory = mem; + env->udata = udata; } - -int -forf_eval_once(struct forf_env *env) -{ +int forf_eval_once(struct forf_env *env) { struct forf_value val; - if (! forf_stack_pop(env->command, &val)) { + if (!forf_stack_pop(env->command, &val)) { env->error = forf_error_underflow; return 0; } switch (val.type) { - case forf_type_number: - case forf_type_stack_begin: - // Push back on command stack, then move it - forf_stack_push(env->command, &val); - if (! forf_stack_move_value(env, env->data, env->command)) return 0; - break; - case forf_type_proc: - (val.v.p)(env); - break; - default: - env->error = forf_error_runtime; + case forf_type_number: + case forf_type_stack_begin: + // Push back on command stack, then move it + forf_stack_push(env->command, &val); + if (!forf_stack_move_value(env, env->data, env->command)) return 0; + break; + case forf_type_proc: + (val.v.p)(env); + break; + default: + env->error = forf_error_runtime; + return 0; } return 1; } -int -forf_eval(struct forf_env *env) -{ +int forf_eval(struct forf_env *env) { int ret; env->error = forf_error_none; while (env->command->top) { ret = forf_eval_once(env); - if ((! ret) || (env->error)) { + if ((!ret) || (env->error)) { return 0; } } diff --git a/forf.h b/forf.h index d231086..69745ac 100644 --- a/forf.h +++ b/forf.h @@ -1,7 +1,7 @@ #pragma once -#include #include +#include #define MAX_TOKEN_LEN 20 #define MAX_CMDSTACK 200 @@ -28,42 +28,41 @@ enum forf_error_type { extern char *forf_error_str[]; -typedef void (forf_proc)(struct forf_env *); +typedef void(forf_proc)(struct forf_env *); struct forf_value { - enum forf_value_type type; + enum forf_value_type type; union { - forf_proc *p; - long i; + forf_proc *p; + long i; } v; }; struct forf_stack { - size_t size; - size_t top; + size_t size; + size_t top; struct forf_value *stack; }; struct forf_memory { - size_t size; - long *mem; + size_t size; + long *mem; }; struct forf_lexical_env { - char *name; + char *name; forf_proc *proc; }; struct forf_env { - enum forf_error_type error; + enum forf_error_type error; struct forf_lexical_env *lenv; - struct forf_stack *data; - struct forf_stack *command; - struct forf_memory *memory; - void *udata; + struct forf_stack *data; + struct forf_stack *command; + struct forf_memory *memory; + void *udata; }; - /* * * Main entry points @@ -71,14 +70,11 @@ struct forf_env { */ /** Initialize a memory structure, given an array of longs */ -void forf_memory_init(struct forf_memory *m, - long *values, - size_t size); +void forf_memory_init(struct forf_memory *m, long *values, size_t size); /** Initialize a stack, given an array of values */ -void forf_stack_init(struct forf_stack *s, - struct forf_value *values, - size_t size); +void forf_stack_init(struct forf_stack *s, struct forf_value *values, + size_t size); void forf_stack_reset(struct forf_stack *s); void forf_stack_copy(struct forf_stack *dst, struct forf_stack *src); @@ -94,51 +90,40 @@ void forf_push_num(struct forf_env *env, long i); /** Pop a whole stack */ struct forf_stack forf_pop_stack(struct forf_env *env); - /** The base lexical environment */ extern struct forf_lexical_env forf_base_lexical_env[]; /** Extend a lexical environment */ -int -forf_extend_lexical_env(struct forf_lexical_env *dest, - struct forf_lexical_env *src, - size_t size); +int forf_extend_lexical_env(struct forf_lexical_env *dest, + struct forf_lexical_env *src, size_t size); /** Initialize a forf runtime environment. * * data, cmd, and mem should have already been initialized */ -void forf_env_init(struct forf_env *env, - struct forf_lexical_env *lenv, - struct forf_stack *data, - struct forf_stack *cmd, - struct forf_memory *mem, - void *udata); +void forf_env_init(struct forf_env *env, struct forf_lexical_env *lenv, + struct forf_stack *data, struct forf_stack *cmd, + struct forf_memory *mem, void *udata); /** The type of a getch function (used for parsing) */ -typedef int (forf_getch_func)(void *); +typedef int(forf_getch_func)(void *); /** Parse something by calling getch(datum) * * Returns the character at which an error was encountered, or * 0 for successful parse. */ -int forf_parse_stream(struct forf_env *env, - forf_getch_func *getch, - void *datum); +int forf_parse_stream(struct forf_env *env, forf_getch_func *getch, + void *datum); /** Parse a buffer */ -int forf_parse_buffer(struct forf_env *env, - char *buf, - size_t len); +int forf_parse_buffer(struct forf_env *env, char *buf, size_t len); /** Parse a string */ -int forf_parse_string(struct forf_env *env, - char *str); +int forf_parse_string(struct forf_env *env, char *str); /** Parse a FILE * */ -int forf_parse_file(struct forf_env *env, - FILE *f); +int forf_parse_file(struct forf_env *env, FILE *f); /** Evaluate the topmost value on the command stack */ int forf_eval_once(struct forf_env *env); diff --git a/forftanks.c b/forftanks.c index 9bc305a..34a40f8 100644 --- a/forftanks.c +++ b/forftanks.c @@ -1,13 +1,13 @@ -#include -#include -#include -#include +#include "ctanks.h" +#include "dump.h" +#include "forf.h" +#include #include #include -#include -#include "ctanks.h" -#include "forf.h" -#include "dump.h" +#include +#include +#include +#include #define MAX_TANKS 50 #define ROUNDS 500 @@ -20,28 +20,25 @@ #define MEMORY_SIZE 10 struct forftank { - struct forf_env env; - int error_pos; - char color[8]; /* "#ff0088" */ - char name[50]; - char *path; + struct forf_env env; + int error_pos; + char color[8]; /* "#ff0088" */ + char name[50]; + char *path; unsigned int uid; - struct forf_stack _prog; - struct forf_value _progvals[CSTACK_SIZE]; - struct forf_stack _cmd; - struct forf_value _cmdvals[CSTACK_SIZE]; - struct forf_stack _data; - struct forf_value _datavals[DSTACK_SIZE]; + struct forf_stack _prog; + struct forf_value _progvals[CSTACK_SIZE]; + struct forf_stack _cmd; + struct forf_value _cmdvals[CSTACK_SIZE]; + struct forf_stack _data; + struct forf_value _datavals[DSTACK_SIZE]; struct forf_memory _mem; - long _memvals[MEMORY_SIZE]; + long _memvals[MEMORY_SIZE]; }; - #ifndef NODEBUG -void -forf_print_val(struct forf_value *val) -{ +void forf_print_val(struct forf_value *val) { switch (val->type) { case forf_type_number: printf("%ld", val->v.i); @@ -58,9 +55,7 @@ forf_print_val(struct forf_value *val) } } -void -forf_print_stack(struct forf_stack *s) -{ +void forf_print_stack(struct forf_stack *s) { size_t pos; for (pos = 0; pos < s->top; pos += 1) { @@ -69,9 +64,7 @@ forf_print_stack(struct forf_stack *s) } } -void -forf_dump_stack(struct forf_stack *s) -{ +void forf_dump_stack(struct forf_stack *s) { printf("Stack at %p: ", s); forf_print_stack(s); printf("\n"); @@ -85,80 +78,64 @@ forf_dump_stack(struct forf_stack *s) */ /** Has the turret recharged? */ -void -forf_tank_fire_ready(struct forf_env *env) -{ +void forf_tank_fire_ready(struct forf_env *env) { struct tank *tank = (struct tank *)env->udata; forf_push_num(env, tank_fire_ready(tank)); } /** Fire! */ -void -forf_tank_fire(struct forf_env *env) -{ +void forf_tank_fire(struct forf_env *env) { struct tank *tank = (struct tank *)env->udata; tank_fire(tank); } /** Set desired speed */ -void -forf_tank_set_speed(struct forf_env *env) -{ - struct tank *tank = (struct tank *)env->udata; - long right = forf_pop_num(env); - long left = forf_pop_num(env); +void forf_tank_set_speed(struct forf_env *env) { + struct tank *tank = (struct tank *)env->udata; + long right = forf_pop_num(env); + long left = forf_pop_num(env); tank_set_speed(tank, left, right); } /** Get the current turret angle */ -void -forf_tank_get_turret(struct forf_env *env) -{ - struct tank *tank = (struct tank *)env->udata; - float angle = tank_get_turret(tank); +void forf_tank_get_turret(struct forf_env *env) { + struct tank *tank = (struct tank *)env->udata; + float angle = tank_get_turret(tank); forf_push_num(env, rad2deg(angle)); } /** Set the desired turret angle */ -void -forf_tank_set_turret(struct forf_env *env) -{ - struct tank *tank = (struct tank *)env->udata; - long angle = forf_pop_num(env); +void forf_tank_set_turret(struct forf_env *env) { + struct tank *tank = (struct tank *)env->udata; + long angle = forf_pop_num(env); tank_set_turret(tank, deg2rad(angle)); } /** Is a sensor active? */ -void -forf_tank_get_sensor(struct forf_env *env) -{ - struct tank *tank = (struct tank *)env->udata; - long sensor_num = forf_pop_num(env); +void forf_tank_get_sensor(struct forf_env *env) { + struct tank *tank = (struct tank *)env->udata; + long sensor_num = forf_pop_num(env); forf_push_num(env, tank_get_sensor(tank, sensor_num)); } /** Set the LED state */ -void -forf_tank_set_led(struct forf_env *env) -{ - struct tank *tank = (struct tank *)env->udata; - long active = forf_pop_num(env); +void forf_tank_set_led(struct forf_env *env) { + struct tank *tank = (struct tank *)env->udata; + long active = forf_pop_num(env); tank_set_led(tank, active); } /** Pick a random number */ -void -forf_proc_random(struct forf_env *env) -{ +void forf_proc_random(struct forf_env *env) { long max = forf_pop_num(env); - + if (max < 1) { forf_push_num(env, 0); return; @@ -169,16 +146,15 @@ forf_proc_random(struct forf_env *env) /* Tanks lexical environment */ struct forf_lexical_env tanks_lenv_addons[] = { - {"fire-ready?", forf_tank_fire_ready}, - {"fire!", forf_tank_fire}, - {"set-speed!", forf_tank_set_speed}, - {"get-turret", forf_tank_get_turret}, - {"set-turret!", forf_tank_set_turret}, - {"sensor?", forf_tank_get_sensor}, - {"set-led!", forf_tank_set_led}, - {"random", forf_proc_random}, - {NULL, NULL} -}; + {"fire-ready?", forf_tank_fire_ready}, + {"fire!", forf_tank_fire}, + {"set-speed!", forf_tank_set_speed}, + {"get-turret", forf_tank_get_turret}, + {"set-turret!", forf_tank_set_turret}, + {"sensor?", forf_tank_get_sensor}, + {"set-led!", forf_tank_set_led}, + {"random", forf_proc_random}, + {NULL, NULL}}; /* * @@ -186,68 +162,61 @@ struct forf_lexical_env tanks_lenv_addons[] = { * */ -int -ft_read_file(char *ptr, size_t size, char *dir, char *fn) -{ - char path[256]; - FILE *f = NULL; - int ret; - int success = 0; +int ft_read_file(char *ptr, size_t size, char *dir, char *fn) { + char path[256]; + FILE *f = NULL; + int ret; + int success = 0; do { snprintf(path, sizeof(path), "%s/%s", dir, fn); f = fopen(path, "r"); - if (! f) break; + if (!f) + break; ret = fread(ptr, 1, size - 1, f); ptr[ret] = '\0'; - if (! ret) break; + if (!ret) + break; success = 1; } while (0); - if (f) fclose(f); - if (! success) { + if (f) + fclose(f); + if (!success) { return 0; } return 1; } -void -ft_bricked_tank(struct tank *tank, void *ignored) -{ +void ft_bricked_tank(struct tank *tank, void *ignored) { /* Do nothing, the tank is comatose */ } -void -ft_run_tank(struct tank *tank, struct forftank *ftank) -{ +void ft_run_tank(struct tank *tank, struct forftank *ftank) { int ret; /* Copy program into command stack */ forf_stack_copy(&ftank->_cmd, &ftank->_prog); forf_stack_reset(&ftank->_data); ret = forf_eval(&ftank->env); - if (! ret) { - fprintf(stderr, "Error in %s: %s\n", - ftank->path, + if (!ret) { + fprintf(stderr, "Error in %s: %s\n", ftank->path, forf_error_str[ftank->env.error]); } } -int -ft_read_program(struct forftank *ftank, - struct tank *tank, - struct forf_lexical_env *lenv, - char *path) -{ - char progpath[256]; +int ft_read_program(struct forftank *ftank, struct tank *tank, + struct forf_lexical_env *lenv, char *path) { + char progpath[256]; FILE *f; /* Open program */ snprintf(progpath, sizeof(progpath), "%s/program", path); f = fopen(progpath, "r"); - if (! f) return 0; + if (!f) + return 0; /* Parse program */ ftank->error_pos = forf_parse_file(&ftank->env, f); @@ -265,43 +234,33 @@ ft_read_program(struct forftank *ftank, return 1; } -void -ft_tank_init(struct forftank *ftank, - struct tank *tank, - struct forf_lexical_env *lenv) -{ +void ft_tank_init(struct forftank *ftank, struct tank *tank, + struct forf_lexical_env *lenv) { /* Set up forf environment */ forf_stack_init(&ftank->_prog, ftank->_progvals, CSTACK_SIZE); forf_stack_init(&ftank->_cmd, ftank->_cmdvals, CSTACK_SIZE); forf_stack_init(&ftank->_data, ftank->_datavals, DSTACK_SIZE); forf_memory_init(&ftank->_mem, ftank->_memvals, MEMORY_SIZE); - forf_env_init(&ftank->env, - lenv, - &ftank->_data, - &ftank->_cmd, - &ftank->_mem, + forf_env_init(&ftank->env, lenv, &ftank->_data, &ftank->_cmd, &ftank->_mem, tank); } -void -ft_read_sensors(struct tank *tank, - char *path) -{ +void ft_read_sensors(struct tank *tank, char *path) { int i; for (i = 0; i < TANK_MAX_SENSORS; i += 1) { - int ret; - char fn[10]; - char s[20]; + int ret; + char fn[10]; + char s[20]; char *p = s; - long range; - long angle; - long width; - long turret; + long range; + long angle; + long width; + long turret; snprintf(fn, sizeof(fn), "sensor%d", i); ret = ft_read_file(s, sizeof(s), path, fn); - if (! ret) { + if (!ret) { s[0] = 0; } range = strtol(p, &p, 0); @@ -316,12 +275,8 @@ ft_read_sensors(struct tank *tank, } } -int -ft_read_tank(struct forftank *ftank, - struct tank *tank, - struct forf_lexical_env *lenv, - char *path) -{ +int ft_read_tank(struct forftank *ftank, struct tank *tank, + struct forf_lexical_env *lenv, char *path) { int ret; ftank->path = path; @@ -338,7 +293,7 @@ ft_read_tank(struct forftank *ftank, /* What is your name? */ ret = ft_read_file(ftank->name, sizeof(ftank->name), path, "name"); - if (! ret) { + if (!ret) { snprintf(ftank->name, sizeof(ftank->name), "i:%x", ftank->uid); } @@ -354,29 +309,21 @@ ft_read_tank(struct forftank *ftank, /* What is your favorite color? */ ret = ft_read_file(ftank->color, sizeof(ftank->color), path, "color"); - if (! ret) { + if (!ret) { strncpy(ftank->color, "#808080", sizeof(ftank->color)); } return 1; } -void -print_header(FILE *f, - struct tanks_game *game, - int seed) -{ +void print_header(FILE *f, struct tanks_game *game, int seed) { fprintf(f, "{\n"); fprintf(f, " \"seed\": %d,\n", seed); fprintf(f, " \"field\": [%d,%d],\n", (int)game->size[0], (int)game->size[1]); } -void -print_rounds(FILE *f, - struct tanks_game *game, - struct tank *tanks, - int ntanks) -{ +void print_rounds(FILE *f, struct tanks_game *game, struct tank *tanks, + int ntanks) { int alive; fprintf(f, " \"rounds\": [\n"); @@ -395,7 +342,7 @@ print_rounds(FILE *f, struct tank *t = &(tanks[j]); int k; - int flags = 0; + int flags = 0; int sensors = 0; if (j > 0) { @@ -417,12 +364,8 @@ print_rounds(FILE *f, alive -= 1; flags |= 4; } - fprintf(f, "[%d,%d,%.2f,%.2f,%d,%d]", - (int)t->position[0], - (int)(t->position[1]), - t->angle, - t->turret.current, - flags, + fprintf(f, "[%d,%d,%.2f,%.2f,%d,%d]", (int)t->position[0], + (int)(t->position[1]), t->angle, t->turret.current, flags, sensors); } fprintf(f, "]"); @@ -431,12 +374,8 @@ print_rounds(FILE *f, fprintf(f, "\n ],\n"); } -void -print_standings(FILE *f, - struct forftank *ftanks, - struct tank *tanks, - int ntanks) -{ +void print_standings(FILE *f, struct forftank *ftanks, struct tank *tanks, + int ntanks) { fprintf(f, " \"tanks\": [\n"); for (int i = 0; i < ntanks; i += 1) { @@ -462,7 +401,8 @@ print_standings(FILE *f, fprintf(f, " \"killer\": %d,\n", killer); fprintf(f, " \"kills\": %d,\n", kills); fprintf(f, " \"errorPos\": %d,\n", ftanks[i].error_pos); - fprintf(f, " \"error\": \"%s\",\n", forf_error_str[ftanks[i].env.error]); + fprintf(f, " \"error\": \"%s\",\n", + forf_error_str[ftanks[i].env.error]); fprintf(f, " \"sensors\": [\n"); for (int j = 0; j < TANK_MAX_SENSORS; j += 1) { struct sensor *s = &(tanks[i].sensors[j]); @@ -470,15 +410,15 @@ print_standings(FILE *f, if (j > 0) { fprintf(f, ",\n"); } - - if (! s->range) { + + if (!s->range) { fprintf(f, " null"); } else { - fprintf(f, " {\"range\":%d,\"angle\":%.2f,\"width\":%.2f,\"turret\":%s}", - (int)(s->range), - s->angle, - s->width, - s->turret?"true":"false"); + fprintf(f, + " " + "{\"range\":%d,\"angle\":%.2f,\"width\":%.2f,\"turret\":%s}", + (int)(s->range), s->angle, s->width, + s->turret ? "true" : "false"); } } fprintf(f, "\n ]"); @@ -488,39 +428,36 @@ print_standings(FILE *f, fprintf(f, "\n ],\n"); } -void -print_footer(FILE *f) -{ - fprintf(f, " \"\": null\n"); // sentry, so everything prior can end with a comma +void print_footer(FILE *f) { + fprintf(f, + " \"\": null\n"); // sentry, so everything prior can end with a comma fprintf(f, "}\n"); } -int -main(int argc, char *argv[]) -{ - struct tanks_game game; - struct forftank myftanks[MAX_TANKS]; - struct tank mytanks[MAX_TANKS]; +int main(int argc, char *argv[]) { + struct tanks_game game; + struct forftank myftanks[MAX_TANKS]; + struct tank mytanks[MAX_TANKS]; struct forf_lexical_env lenv[LENV_SIZE]; - int order[MAX_TANKS]; - int ntanks = 0; - int i; + int order[MAX_TANKS]; + int ntanks = 0; + int i; int seed; lenv[0].name = NULL; lenv[0].proc = NULL; - if ((! forf_extend_lexical_env(lenv, forf_base_lexical_env, LENV_SIZE)) || - (! forf_extend_lexical_env(lenv, tanks_lenv_addons, LENV_SIZE))) { + if ((!forf_extend_lexical_env(lenv, forf_base_lexical_env, LENV_SIZE)) || + (!forf_extend_lexical_env(lenv, tanks_lenv_addons, LENV_SIZE))) { fprintf(stderr, "Unable to initialize lexical environment.\n"); return 1; } /* We only need slightly random numbers */ { - char *s = getenv("SEED"); - seed = atoi(s?s:""); + char *s = getenv("SEED"); + seed = atoi(s ? s : ""); - if (! seed) { + if (!seed) { seed = getpid(); } @@ -534,10 +471,7 @@ main(int argc, char *argv[]) /* Every argument is a tank directory */ for (i = 1; ntanks < MAX_TANKS && i < argc; i += 1) { - if (ft_read_tank(&myftanks[ntanks], - &mytanks[ntanks], - lenv, - argv[i])) { + if (ft_read_tank(&myftanks[ntanks], &mytanks[ntanks], lenv, argv[i])) { ntanks += 1; } } @@ -546,7 +480,8 @@ main(int argc, char *argv[]) { int x, y; - for (x = 1; x * x < ntanks; x += 1); + for (x = 1; x * x < ntanks; x += 1) + ; y = ntanks / x; if (ntanks % x) { y += 1; @@ -570,8 +505,8 @@ main(int argc, char *argv[]) /* Position tanks */ { - int x = SPACING/2; - int y = SPACING/2; + int x = SPACING / 2; + int y = SPACING / 2; for (i = 0; i < ntanks; i += 1) { mytanks[order[i]].position[0] = (float)x;