mirror of https://github.com/dirtbags/tanks.git
clang-format
This commit is contained in:
parent
9d3cce9bf6
commit
ffc7d07214
129
ctanks.c
129
ctanks.c
|
@ -1,11 +1,13 @@
|
|||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include "ctanks.h"
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* 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)
|
||||
|
@ -18,64 +20,39 @@
|
|||
|
||||
#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];
|
||||
|
||||
|
@ -89,19 +66,13 @@ rotate_point(float angle, float point[2])
|
|||
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;
|
||||
}
|
||||
|
@ -138,13 +109,8 @@ 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)
|
||||
{
|
||||
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 */
|
||||
|
@ -211,13 +177,8 @@ 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)
|
||||
{
|
||||
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,
|
||||
|
@ -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,10 +198,7 @@ compute_vector(struct tanks_game *game,
|
|||
*dist2 = sq(vector[0]) + sq(vector[1]);
|
||||
}
|
||||
|
||||
void
|
||||
tanks_move_tank(struct tanks_game *game,
|
||||
struct tank *tank)
|
||||
{
|
||||
void tanks_move_tank(struct tanks_game *game, struct tank *tank) {
|
||||
int i;
|
||||
float movement;
|
||||
float angle;
|
||||
|
@ -268,11 +225,11 @@ 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]);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
||||
|
@ -345,15 +303,13 @@ 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)
|
||||
{
|
||||
void tanks_run_turn(struct tanks_game *game, struct tank *tanks, int ntanks) {
|
||||
int i, j;
|
||||
float vector[2];
|
||||
float dist2; /* distance squared */
|
||||
|
@ -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];
|
||||
|
|
1
ctanks.h
1
ctanks.h
|
@ -79,7 +79,6 @@ struct tank {
|
|||
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
|
||||
|
|
10
dump.h
10
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)
|
||||
|
@ -22,9 +24,11 @@
|
|||
#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)
|
||||
|
|
233
forf.c
233
forf.c
|
@ -27,13 +27,13 @@
|
|||
* and not new stack types.
|
||||
*/
|
||||
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include "forf.h"
|
||||
#include "dump.h"
|
||||
#include "forf.h"
|
||||
|
||||
#ifndef max
|
||||
#define max(a, b) (((a) > (b)) ? (a) : (b))
|
||||
|
@ -41,14 +41,8 @@
|
|||
#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)
|
||||
{
|
||||
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,19 +88,14 @@ 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;
|
||||
|
||||
|
@ -139,9 +108,7 @@ 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)) {
|
||||
|
@ -156,9 +123,7 @@ 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;
|
||||
|
@ -168,7 +133,6 @@ forf_push_num(struct forf_env *env, long i)
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/* Pop an entire stack
|
||||
*
|
||||
* DANGER WILL ROBINSON
|
||||
|
@ -177,9 +141,7 @@ 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 forf_pop_stack(struct forf_env *env) {
|
||||
struct forf_stack s = {0, 0, NULL};
|
||||
struct forf_value val;
|
||||
size_t depth = 1;
|
||||
|
@ -223,9 +185,7 @@ 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)) {
|
||||
|
@ -240,9 +200,7 @@ 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)
|
||||
{
|
||||
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,11 +217,8 @@ 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;
|
||||
|
||||
|
@ -296,10 +251,8 @@ forf_stack_move_value(struct forf_env *env,
|
|||
} while (depth > 0);
|
||||
|
||||
return 1;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
*
|
||||
* Procedures
|
||||
|
@ -307,45 +260,28 @@ forf_stack_move_value(struct forf_env *env,
|
|||
*/
|
||||
|
||||
#define unproc(name, op) \
|
||||
static void \
|
||||
forf_proc_ ## name(struct forf_env *env) \
|
||||
{ \
|
||||
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) \
|
||||
{ \
|
||||
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,9 +324,7 @@ forf_proc_exch(struct forf_env *env)
|
|||
forf_push_num(env, b);
|
||||
}
|
||||
|
||||
static void
|
||||
forf_proc_if(struct forf_env *env)
|
||||
{
|
||||
static void forf_proc_if(struct forf_env *env) {
|
||||
struct forf_stack ifclause = forf_pop_stack(env);
|
||||
long cond = forf_pop_num(env);
|
||||
|
||||
|
@ -411,9 +333,7 @@ forf_proc_if(struct forf_env *env)
|
|||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
@ -425,9 +345,7 @@ 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);
|
||||
|
||||
|
@ -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,8 +373,7 @@ forf_proc_memget(struct forf_env *env)
|
|||
* Lexical environment
|
||||
*
|
||||
*/
|
||||
struct forf_lexical_env forf_base_lexical_env[] = {
|
||||
{"~", forf_proc_inv},
|
||||
struct forf_lexical_env forf_base_lexical_env[] = {{"~", forf_proc_inv},
|
||||
{"!", forf_proc_not},
|
||||
{"+", forf_proc_add},
|
||||
{"-", forf_proc_sub},
|
||||
|
@ -484,18 +399,15 @@ struct forf_lexical_env forf_base_lexical_env[] = {
|
|||
{"ifelse", forf_proc_ifelse},
|
||||
{"mset", forf_proc_memset},
|
||||
{"mget", forf_proc_memget},
|
||||
{NULL, NULL}
|
||||
};
|
||||
{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 (base = 0; dest[base].name; base += 1)
|
||||
;
|
||||
for (i = 0; (base + i < size) && (src[i].name); i += 1) {
|
||||
dest[base + i] = src[i];
|
||||
}
|
||||
|
@ -508,15 +420,12 @@ forf_extend_lexical_env(struct forf_lexical_env *dest,
|
|||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
*
|
||||
* Parsing
|
||||
*
|
||||
*/
|
||||
static int
|
||||
forf_push_token(struct forf_env *env, char *token, size_t tokenlen)
|
||||
{
|
||||
static int forf_push_token(struct forf_env *env, char *token, size_t tokenlen) {
|
||||
long i;
|
||||
char s[MAX_TOKEN_LEN + 1];
|
||||
char *endptr;
|
||||
|
@ -558,11 +467,8 @@ 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 forf_parse_stream(struct forf_env *env, forf_getch_func *getch,
|
||||
void *datum) {
|
||||
int running = 1;
|
||||
long pos = 0;
|
||||
char token[MAX_TOKEN_LEN];
|
||||
|
@ -574,7 +480,8 @@ forf_parse_stream(struct forf_env *env,
|
|||
#define _tokenize() \
|
||||
do { \
|
||||
if (tokenlen) { \
|
||||
if (! forf_push_token(env, token, tokenlen)) return pos; \
|
||||
if (!forf_push_token(env, token, tokenlen)) \
|
||||
return pos; \
|
||||
tokenlen = 0; \
|
||||
} \
|
||||
} while (0)
|
||||
|
@ -657,20 +564,14 @@ struct forf_char_stream {
|
|||
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,35 +581,23 @@ 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)
|
||||
{
|
||||
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;
|
||||
|
@ -716,10 +605,7 @@ forf_env_init(struct forf_env *env,
|
|||
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)) {
|
||||
|
@ -731,7 +617,8 @@ forf_eval_once(struct forf_env *env)
|
|||
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;
|
||||
if (!forf_stack_move_value(env, env->data, env->command))
|
||||
return 0;
|
||||
break;
|
||||
case forf_type_proc:
|
||||
(val.v.p)(env);
|
||||
|
@ -743,9 +630,7 @@ forf_eval_once(struct forf_env *env)
|
|||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
forf_eval(struct forf_env *env)
|
||||
{
|
||||
int forf_eval(struct forf_env *env) {
|
||||
int ret;
|
||||
|
||||
env->error = forf_error_none;
|
||||
|
|
39
forf.h
39
forf.h
|
@ -1,7 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define MAX_TOKEN_LEN 20
|
||||
#define MAX_CMDSTACK 200
|
||||
|
@ -63,7 +63,6 @@ struct forf_env {
|
|||
void *udata;
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
*
|
||||
* Main entry points
|
||||
|
@ -71,13 +70,10 @@ 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,
|
||||
void forf_stack_init(struct forf_stack *s, struct forf_value *values,
|
||||
size_t size);
|
||||
|
||||
void forf_stack_reset(struct forf_stack *s);
|
||||
|
@ -94,26 +90,20 @@ 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 *);
|
||||
|
@ -123,22 +113,17 @@ typedef int (forf_getch_func)(void *);
|
|||
* 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,
|
||||
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);
|
||||
|
|
185
forftanks.c
185
forftanks.c
|
@ -1,13 +1,13 @@
|
|||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include "ctanks.h"
|
||||
#include "dump.h"
|
||||
#include "forf.h"
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include "ctanks.h"
|
||||
#include "forf.h"
|
||||
#include "dump.h"
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define MAX_TANKS 50
|
||||
#define ROUNDS 500
|
||||
|
@ -37,11 +37,8 @@ struct forftank {
|
|||
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,27 +78,21 @@ 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)
|
||||
{
|
||||
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);
|
||||
|
@ -114,9 +101,7 @@ forf_tank_set_speed(struct forf_env *env)
|
|||
}
|
||||
|
||||
/** Get the current turret angle */
|
||||
void
|
||||
forf_tank_get_turret(struct forf_env *env)
|
||||
{
|
||||
void forf_tank_get_turret(struct forf_env *env) {
|
||||
struct tank *tank = (struct tank *)env->udata;
|
||||
float angle = tank_get_turret(tank);
|
||||
|
||||
|
@ -124,9 +109,7 @@ forf_tank_get_turret(struct forf_env *env)
|
|||
}
|
||||
|
||||
/** Set the desired turret angle */
|
||||
void
|
||||
forf_tank_set_turret(struct forf_env *env)
|
||||
{
|
||||
void forf_tank_set_turret(struct forf_env *env) {
|
||||
struct tank *tank = (struct tank *)env->udata;
|
||||
long angle = forf_pop_num(env);
|
||||
|
||||
|
@ -134,9 +117,7 @@ forf_tank_set_turret(struct forf_env *env)
|
|||
}
|
||||
|
||||
/** Is a sensor active? */
|
||||
void
|
||||
forf_tank_get_sensor(struct forf_env *env)
|
||||
{
|
||||
void forf_tank_get_sensor(struct forf_env *env) {
|
||||
struct tank *tank = (struct tank *)env->udata;
|
||||
long sensor_num = forf_pop_num(env);
|
||||
|
||||
|
@ -144,9 +125,7 @@ forf_tank_get_sensor(struct forf_env *env)
|
|||
}
|
||||
|
||||
/** Set the LED state */
|
||||
void
|
||||
forf_tank_set_led(struct forf_env *env)
|
||||
{
|
||||
void forf_tank_set_led(struct forf_env *env) {
|
||||
struct tank *tank = (struct tank *)env->udata;
|
||||
long active = forf_pop_num(env);
|
||||
|
||||
|
@ -154,9 +133,7 @@ forf_tank_set_led(struct forf_env *env)
|
|||
}
|
||||
|
||||
/** 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) {
|
||||
|
@ -177,8 +154,7 @@ struct forf_lexical_env tanks_lenv_addons[] = {
|
|||
{"sensor?", forf_tank_get_sensor},
|
||||
{"set-led!", forf_tank_set_led},
|
||||
{"random", forf_proc_random},
|
||||
{NULL, NULL}
|
||||
};
|
||||
{NULL, NULL}};
|
||||
|
||||
/*
|
||||
*
|
||||
|
@ -186,9 +162,7 @@ struct forf_lexical_env tanks_lenv_addons[] = {
|
|||
*
|
||||
*/
|
||||
|
||||
int
|
||||
ft_read_file(char *ptr, size_t size, char *dir, char *fn)
|
||||
{
|
||||
int ft_read_file(char *ptr, size_t size, char *dir, char *fn) {
|
||||
char path[256];
|
||||
FILE *f = NULL;
|
||||
int ret;
|
||||
|
@ -197,31 +171,30 @@ ft_read_file(char *ptr, size_t size, char *dir, char *fn)
|
|||
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 (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 */
|
||||
|
@ -229,25 +202,21 @@ ft_run_tank(struct tank *tank, struct forftank *ftank)
|
|||
forf_stack_reset(&ftank->_data);
|
||||
ret = forf_eval(&ftank->env);
|
||||
if (!ret) {
|
||||
fprintf(stderr, "Error in %s: %s\n",
|
||||
ftank->path,
|
||||
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)
|
||||
{
|
||||
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,28 +234,18 @@ 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) {
|
||||
|
@ -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;
|
||||
|
@ -361,22 +316,14 @@ ft_read_tank(struct forftank *ftank,
|
|||
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");
|
||||
|
@ -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]);
|
||||
|
@ -474,10 +414,10 @@ print_standings(FILE *f,
|
|||
if (!s->range) {
|
||||
fprintf(f, " null");
|
||||
} else {
|
||||
fprintf(f, " {\"range\":%d,\"angle\":%.2f,\"width\":%.2f,\"turret\":%s}",
|
||||
(int)(s->range),
|
||||
s->angle,
|
||||
s->width,
|
||||
fprintf(f,
|
||||
" "
|
||||
"{\"range\":%d,\"angle\":%.2f,\"width\":%.2f,\"turret\":%s}",
|
||||
(int)(s->range), s->angle, s->width,
|
||||
s->turret ? "true" : "false");
|
||||
}
|
||||
}
|
||||
|
@ -488,16 +428,13 @@ 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[])
|
||||
{
|
||||
int main(int argc, char *argv[]) {
|
||||
struct tanks_game game;
|
||||
struct forftank myftanks[MAX_TANKS];
|
||||
struct tank mytanks[MAX_TANKS];
|
||||
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue