added player_interface and some code to test it

This commit is contained in:
Alexandre 2025-05-15 22:51:18 +02:00
parent 2a94a2a113
commit 4dab723255
93 changed files with 165 additions and 31 deletions

View File

@ -5,7 +5,7 @@ LFLAGS = -lSDL2 -lSDL2_image -lm
all: bin/back
test: bin/back
bin/back
bin/back levels/test.txt bots/dumb bots/dumb bots/dumb bots/dumb
mem: bin/back
valgrind --leak-check=full ./bin/back

BIN
bin/back

Binary file not shown.

1
bots/answer.txt Normal file
View File

@ -0,0 +1 @@
165 74

BIN
bots/dumb Executable file

Binary file not shown.

BIN
bots/dumb.cmi Normal file

Binary file not shown.

BIN
bots/dumb.cmx Normal file

Binary file not shown.

6
bots/dumb.ml Normal file
View File

@ -0,0 +1,6 @@
Random.self_init () ;;
let ptr = open_out "bots/answer.txt" in
Printf.fprintf ptr "%d %d" (Random.int 360) (60 + Random.int 40);
close_out ptr

BIN
bots/dumb.o Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,9 +1,9 @@
4
0 (2 0) (38.73 87.83)
1 (3 0) (12.86 46.67)
2 (2 0) (96.96 38.71)
3 (4 0) (55.58 91.72)
[2]
0 (3 0) (45.17 65.83)
1 (4 2) (65.68 34.72)
2 (3 3) (37.74 58.35)
3 (2 0) (19.47 35.72)
[3]
5 5
..S14
@ -11,3 +11,18 @@
...35
.E15.
.....
100
200
10
0.90
0.80
0.20
Room Size
Maximum speed
Player Radius
Friction
Restitution factor
Track Distance to Edge

Binary file not shown.

Before

Width:  |  Height:  |  Size: 244 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 312 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 127 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 181 KiB

BIN
res/digits/big-sign-sum.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 123 KiB

BIN
res/digits/digit-0.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/digits/digit-1.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/digits/digit-2.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/digits/digit-3.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/digits/digit-4.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/digits/digit-5.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/digits/digit-6.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/digits/digit-7.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/digits/digit-8.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/digits/digit-9.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/digits/sign-minus.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/digits/sign-plus.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/digits/sign-prod.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/digits/sum-k.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

BIN
res/letters/letter-a.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-b.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-c.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-d.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-e.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-f.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-g.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-h.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-i.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-j.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-k.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-l.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-m.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-n.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-o.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-p.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-q.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-r.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-s.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-t.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-u.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-v.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-w.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-x.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-y.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/letter-z.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
res/letters/ranking-A.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 62 KiB

BIN
res/letters/ranking-B.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 63 KiB

BIN
res/letters/ranking-C.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

BIN
res/letters/ranking-D.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

BIN
res/letters/ranking-S.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

BIN
res/letters/ranking-X.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 61 KiB

BIN
res/letters/symbol-percent.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 74 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 54 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.6 KiB

View File

@ -10,6 +10,8 @@ int convex_seg(int x1, int x2, double theta);
double to_double(int n);
int pw(int x, int n);
double distance_pt(int x1, int x2, int y1, int y2);
int max(int a, int b);
int min(int a, int b);
double norm(ptf p);
double distance(ptf p1, ptf p2);

View File

@ -34,9 +34,9 @@ car* init_car(int nPl, int nTotPl) {
res->lastPos = (ptf){.fx = res->pos.fx, .fy = res->pos.fy};
res->chx = START_CHY;
res->chy = START_CHX;
//res->vel = (ptf){.fx = 0.0, .fy = 0.0};
double theta = (2.0*MAX_THETA_SPAWN*nPl/((nTotPl-1==0)?(1):(nTotPl-1)) - 1.0*MAX_THETA_SPAWN);
res->vel = (ptf){.fx = (rand()%100)/100.0*2.0*MAX_SPEED, .fy = (rand()%100-50.0)/100.0*2.0*MAX_SPEED};
res->vel = (ptf){.fx = 0.0, .fy = 0.0};
//double theta = (2.0*MAX_THETA_SPAWN*nPl/((nTotPl-1==0)?(1):(nTotPl-1)) - 1.0*MAX_THETA_SPAWN);
//res->vel = (ptf){.fx = (rand()%100)/100.0*2.0*MAX_SPEED, .fy = (rand()%100-50.0)/100.0*2.0*MAX_SPEED};
return res;
}

View File

@ -501,6 +501,16 @@ void renderCircles(SDL_Renderer* renderer, level* lvl, int cx, int cy, int range
}
}
pt project(int p, int cx, int cy, int range, int rsize) {
if(players[p].c->chx >= cy-range && players[p].c->chx <= cy+range && players[p].c->chy >= cx-range && players[p].c->chy <= cx+range) {
int cox = (WIDTH-rsize)/2+rsize*(players[p].c->chx-cy) + (int)((players[p].c->pos.fx*1.0)/ROOM_SIZE*rsize);
int coy = (HEIGHT-rsize)/2+rsize*(players[p].c->chy-cx) + (int)((players[p].c->pos.fy*1.0)/ROOM_SIZE*rsize);
return (pt){.ix = cox, .iy = coy};
} else {
return (pt){.ix = -WIDTH, .iy = -HEIGHT};
}
}
void renderPlayers(SDL_Renderer* renderer, int cx, int cy, int range, int rsize) {
for(int p = 0; p < nPlayers; p++) {
if(players[p].c->chx >= cy-range && players[p].c->chx <= cy+range && players[p].c->chy >= cx-range && players[p].c->chy <= cx+range) {

View File

@ -17,6 +17,7 @@ void SDL_RenderFillCircle(SDL_Renderer * renderer, int x, int y, int radius, int
void drawCircleToRenderer(SDL_Renderer * renderer, int32_t centreX, int32_t centreY, int32_t radius, int r, int g, int b, int a);
void drawQuarterCircleToRenderer(SDL_Renderer * renderer, int32_t centreX, int32_t centreY, int32_t radius, bool TL, bool TR, bool BL, bool BR, int r, int g, int b, int a);
pt project(int p, int cx, int cy, int range, int rsize);
void renderMap(SDL_Renderer* renderer, level* lvl, int cx, int cy, int range, int rsize);
void import_digits(SDL_Renderer* renderer);

View File

@ -4,7 +4,7 @@
#include <math.h>
#include <stdbool.h>
#include <unistd.h>
#include <termios.h>
#include <string.h>
#include <limits.h>
#include <time.h>
#include <sys/time.h>
@ -18,11 +18,7 @@
#include "rooms.h"
#include "cars.h"
const int N_PLAYERS = 4;
double elapsed = 0.0;
bool halt = false;
void playerActions() {
void cameraActions(level* lvl, bool* halt, int* cx, int* cy, int* dezoom, int* sizeR) {
SDL_Event event;
while(SDL_PollEvent(&event)) {
switch (event.type) {
@ -31,7 +27,33 @@ void playerActions() {
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_SPACE:
halt = true;
*halt = true;
break;
case SDLK_q:
*cy = max(0, *cy-1);
break;
case SDLK_z:
*cx = max(0, *cx-1);
break;
case SDLK_d:
*cy = min(lvl->lines-1, *cy+1);
break;
case SDLK_s:
*cx = min(lvl->cols-1, *cx+1);
break;
case SDLK_p:
*dezoom = max(1, *dezoom-1);
*sizeR = min(WIDTH, HEIGHT)/(2*(*dezoom)+1)+1;
break;
case SDLK_m:
*dezoom = min(3, *dezoom+1);
*sizeR = min(WIDTH, HEIGHT)/(2*(*dezoom)+1)+1;
break;
default:
@ -41,7 +63,50 @@ void playerActions() {
}
}
int main() {
void player_turn(int id, char* exec) {
system(exec);
FILE* ptr = fopen("bots/answer.txt", "r");
int angle, mult;
fscanf(ptr, "%d %d", &angle, &mult);
fclose(ptr);
set_speed_car(players[id].c, MAX_SPEED*mult/100.0*cos(angle*3.14159/180.0), MAX_SPEED*mult/100.0*sin(angle*3.14159/180.0));
}
void parse_all_players(level* lvl, char** execs) {
currentTurn += 1;
for(int p = 0; p < nPlayers; p++) {
write_output("output.txt", lvl, p);
usleep(1000000/10);
player_turn(p, execs[p]);
}
}
int main(int argc, char** argv) {
if(argc < 2) {
fprintf(stderr, "usage : ./back <level_path> <P1 executable> <P2 executable> ...\n");
exit(1);
}
int N_PLAYERS = argc-2;
char** execs = malloc(sizeof(char*)*N_PLAYERS);
for(int k = 0; k < N_PLAYERS; k++) {
execs[k] = malloc(sizeof(char)*64);
execs[k][0] = '.';
execs[k][1] = '/';
int i = 2;
while(i-2 < 64 && argv[k+2][i-2] != '\0') {
execs[k][i] = argv[k+2][i-2];
i += 1;
}
execs[k][i] = '\0';
}
for(int k = 0; k < N_PLAYERS; k++) {
printf("[%d] %s\n", k, execs[k]);
}
double elapsed = 0.0;
bool halt = false;
srand(time(NULL));
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
printf("error initializing SDL: %s\n", SDL_GetError());
@ -58,8 +123,8 @@ int main() {
import_letters(rend);
import_digits(rend);
// ---------------------------- //
//test_file("levels/test.txt");
level* test = parse_level("levels/test.txt");
//stage_file("levels/stage.txt");
level* stage = parse_level(argv[1]);
init_all(N_PLAYERS);
printf("Starting direction : ");
@ -75,28 +140,54 @@ int main() {
printf("???\n");
}
int cx = 1;
int cy = 2;
int dezoom = 2;
int sizeR = 200;
while(!halt) {
resetRenderer(rend);
renderMap(rend, test, 1, 2, 2, 200);
renderMap(rend, stage, cx, cy, dezoom, sizeR);
for(int p = 0; p < N_PLAYERS; p++) {
pt proj = project(p, cx, cy, dezoom, sizeR);
drawStringToRenderer(rend, &(execs[p][1]), proj.ix, proj.iy, 75/(4+dezoom), 105/(4+dezoom));
}
if(elapsed <= 0.7) {
// just selected speeds
for(int p = 0; p < N_PLAYERS; p++) {
pt proj = project(p, cx, cy, dezoom, sizeR);
drawLineWithThicc(rend, 2,
proj.ix,
proj.ix+(int)(players[p].c->vel.fx/5.0),
proj.iy,
proj.iy+(int)(players[p].c->vel.fy/5.0),
players[p].rgb.red, players[p].rgb.green, players[p].rgb.blue, 255
);
}
placeRectToRenderer(rend, 0, 0, 50, 50, 255, 255, 32, 192);
} else if(updateCars(test)) {
} else if(updateCars(stage)) {
// movement
placeRectToRenderer(rend, 0, 0, 50, 50, 32, 255, 32, 192);
} else {
// no movement
placeRectToRenderer(rend, 0, 0, 50, 50, 255, 32, 32, 192);
parse_all_players(stage, execs);
elapsed = 0.0;
}
draw7SegNumberToRenderer(rend, currentTurn, WIDTH/2, 30, 25, 25, 3, 255, 255, 255, 255, 0, 0);
updateRenderer(rend);
elapsed += DT;
usleep((int)(1000000*DT));
playerActions();
cameraActions(stage, &halt, &cx, &cy, &dezoom, &sizeR);
}
write_output("output.txt", test, 2);
// ---------------------------- //
free_digits(digits);
free_digits(letters);
free_level(test);
free_level(stage);
for(int k = 0; k < N_PLAYERS; k++) {
free(execs[k]);
}
free(execs);
SDL_DestroyRenderer(rend);
SDL_DestroyWindow(win);
@ -106,7 +197,7 @@ int main() {
/* TODO :
- add level generation
- add player input parsing (and some dumb code to test it)
- add player input parsing (and some dumb code to stage it)
- add function that gives players the necessary data to play
- check seg faults
+ add items

View File

@ -7,12 +7,12 @@ const int WIDTH = 1000;
const int HEIGHT = 1000;
const int ROOM_SIZE = 100;
const int MAX_SPEED = 150; // m/s
const int MAX_SPEED = 200; // m/s
const double DIST_EDGE = 0.2;
const double START_EDGE = 0.05;
const double RESTITUTION_WALL = 0.8;
const double RESTITUTION_PLAYER = 0.8;
const double RESTITUTION_PLAYER = 0.8; // keep equal to prevouis constant
const int PLAYER_R = 10;
const int MAX_THETA_SPAWN = 80; // degrees
@ -20,12 +20,12 @@ const int MAX_THETA_SPAWN = 80; // degrees
const int BARRIERS = 1;
const double BARRIER_WIDTH = 0.05;
const double FRICTION = 0.75;
const double DV = 1.5; // m/s
const double FRICTION = 0.90;
const double DV = 2.2; // m/s
const double DT = 1.0/100.0;
const double EPSILON = 1.0/4096.0;
const double DELTA_V = 0.98; // m/s
const double DELTA_V = MAX_SPEED/30; // m/s
const double DELTA_THETA = 5; // degrees
// ------------------------------------------------------------------------------------------ //
@ -60,5 +60,13 @@ void write_output(char* stream, level* lvl, int nPl) {
}
fprintf(ptr, "\n");
}
fprintf(ptr, "\n");
fprintf(ptr, "%d\n", ROOM_SIZE);
fprintf(ptr, "%d\n", MAX_SPEED);
fprintf(ptr, "%d\n", PLAYER_R);
fprintf(ptr, "%.2lf\n", FRICTION);
fprintf(ptr, "%.2lf\n", RESTITUTION_WALL);
fprintf(ptr, "%.2lf\n", DIST_EDGE);
fprintf(ptr, "\n\nRoom Size\nMaximum speed\nPlayer Radius\nFriction\nRestitution factor\nTrack Distance to Edge\n");
fclose(ptr);
}