fixed a segFault (hpf) + overhaul to elo system + added noRenderer option

This commit is contained in:
Alexandre 2025-05-19 22:29:36 +02:00
parent 3334d62a3f
commit fcd20bcbd4
20 changed files with 134 additions and 47 deletions

View File

@ -5,19 +5,22 @@ LFLAGS = -lSDL2 -lSDL2_image -lm
all: bin/back all: bin/back
test: bin/back test: bin/back
bin/back levels/test.txt bots/dumb bots/dumb bots/dumb bots/dumb bin/back levels/test.txt bots/dumb bots/dumb2 bots/dumb3 bots/dumb4
testL: bin/back
bin/back levels/test.txt bots/dumb bots/dumb2 bots/dumb3 bots/dumb4 bots/dumb5 bots/dumb6
ez: bin/back ez: bin/back
bin/back levels/simple.txt bots/dumb bots/dumb bots/dumb bots/dumb bin/back levels/simple.txt bots/dumb bots/dumb2 bots/dumb3 bots/dumb4
spin: bin/back spin: bin/back
bin/back levels/turning.txt bots/dumb bots/dumb bots/dumb bots/dumb bin/back levels/turning.txt bots/dumb bots/dumb2 bots/dumb3 bots/dumb4
road: bin/back road: bin/back
bin/back levels/straight.txt bots/dumb bots/dumb bots/dumb bots/dumb bin/back levels/straight.txt bots/dumb bots/dumb2 bots/dumb3 bots/dumb4
mem: bin/back mem: bin/back
valgrind --leak-check=full ./bin/back levels/test.txt bots/dumb bots/dumb bots/dumb bots/dumb valgrind --leak-check=full ./bin/back levels/test.txt bots/dumb bots/dumb2 bots/dumb3 bots/dumb4
bin/back: obj/main.o obj/display.o obj/base.o obj/collisions.o obj/cars.o obj/rooms.o obj/structure.o bin/back: obj/main.o obj/display.o obj/base.o obj/collisions.o obj/cars.o obj/rooms.o obj/structure.o
mkdir -p bin mkdir -p bin

View File

@ -1 +1 @@
155 80 102 61

BIN
bin/back

Binary file not shown.

BIN
bots/dumb2 Executable file

Binary file not shown.

BIN
bots/dumb3 Executable file

Binary file not shown.

BIN
bots/dumb4 Executable file

Binary file not shown.

BIN
bots/dumb5 Executable file

Binary file not shown.

BIN
bots/dumb6 Executable file

Binary file not shown.

View File

@ -1,5 +1,7 @@
name, elo name, elo
./bots/dumb 2154 ./bots/dumb 551
./bots/dumb 1000 ./bots/dumb2 547
./bots/dumb 1500 ./bots/dumb3 512
./bots/dumb 501 ./bots/dumb4 512
./bots/dumb5 540
./bots/dumb6 562

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,12 +1,18 @@
4 4
0 (2 0) (32.28 87.09) 0 (6 4) (31.00 19.85)
1 (2 0) (83.34 49.89) 1 (4 1) (46.31 83.63)
2 (2 0) (81.67 74.71) 2 (2 1) (85.48 74.34)
3 (1 0) (79.36 63.40) 3 (2 1) (66.86 66.86)
[3] [3]
1 3 7 8
S1E ........
..31S...
..24....
...24...
...353E.
...215..
........
100 200 5 0.90 0.80 0.20 100 200 5 0.90 0.80 0.20

View File

@ -17,23 +17,23 @@
#include "collisions.h" #include "collisions.h"
// use after updating the position // use after updating the position
void updateChunk(int nPl) { void updateChunk(level* lvl, int nPl) {
car* c = players[nPl].c; car* c = players[nPl].c;
if(c->pos.fx < 0.0) { if(c->pos.fx < 0.0) {
c->pos.fx += 1.0*ROOM_SIZE; c->pos.fx += 1.0*ROOM_SIZE;
c->chx -= 1; if(c->chx > 0){c->chx -= 1;}
} }
if(c->pos.fx >= 1.0*ROOM_SIZE) { if(c->pos.fx >= 1.0*ROOM_SIZE) {
c->pos.fx -= 1.0*ROOM_SIZE; c->pos.fx -= 1.0*ROOM_SIZE;
c->chx += 1; if(c->chx < lvl->cols){c->chx += 1;}
} }
if(c->pos.fy < 0.0) { if(c->pos.fy < 0.0) {
c->pos.fy += 1.0*ROOM_SIZE; c->pos.fy += 1.0*ROOM_SIZE;
c->chy -= 1; if(c->chy > 0){c->chy -= 1;}
} }
if(c->pos.fy >= 1.0*ROOM_SIZE) { if(c->pos.fy >= 1.0*ROOM_SIZE) {
c->pos.fy -= 1.0*ROOM_SIZE; c->pos.fy -= 1.0*ROOM_SIZE;
c->chy += 1; if(c->chy < lvl->lines){c->chy += 1;}
} }
} }
@ -211,6 +211,15 @@ void bumpOtherCars(int nPl) {
players[nPl].c->vel.fx = -director.fx*RESTITUTION_PLAYER*(speed_P); players[nPl].c->vel.fx = -director.fx*RESTITUTION_PLAYER*(speed_P);
players[nPl].c->vel.fy = -director.fy*RESTITUTION_PLAYER*(speed_P); players[nPl].c->vel.fy = -director.fy*RESTITUTION_PLAYER*(speed_P);
// snap players away from each other (so they dont overlap)
while(distance(get_absolute_coords(p),get_absolute_coords(nPl)) <= 2*PLAYER_R) {
players[p].c->pos.fx += DT*players[p].c->vel.fx;
players[p].c->pos.fy += DT*players[p].c->vel.fy;
players[nPl].c->pos.fx += DT*players[nPl].c->vel.fx;
players[nPl].c->pos.fy += DT*players[nPl].c->vel.fy;
}
} }
} }
} }
@ -235,56 +244,56 @@ bool updateCar(level* lvl, int nPl) {
move_on_START(nPl); move_on_START(nPl);
bumpOtherCars(nPl); bumpOtherCars(nPl);
apply_friction(nPl); apply_friction(nPl);
updateChunk(nPl); updateChunk(lvl, nPl);
return true; return true;
case END: case END:
move_on_FINISH(nPl); move_on_FINISH(nPl);
bumpOtherCars(nPl); bumpOtherCars(nPl);
apply_friction(nPl); apply_friction(nPl);
updateChunk(nPl); updateChunk(lvl, nPl);
return true; return true;
case STR_V: case STR_V:
move_on_STR_V(nPl); move_on_STR_V(nPl);
bumpOtherCars(nPl); bumpOtherCars(nPl);
apply_friction(nPl); apply_friction(nPl);
updateChunk(nPl); updateChunk(lvl, nPl);
return true; return true;
case STR_H: case STR_H:
move_on_STR_H(nPl); move_on_STR_H(nPl);
bumpOtherCars(nPl); bumpOtherCars(nPl);
apply_friction(nPl); apply_friction(nPl);
updateChunk(nPl); updateChunk(lvl, nPl);
return true; return true;
case TURN_NE: case TURN_NE:
move_on_TURN(nPl, ROOM_SIZE, 0); move_on_TURN(nPl, ROOM_SIZE, 0);
bumpOtherCars(nPl); bumpOtherCars(nPl);
apply_friction(nPl); apply_friction(nPl);
updateChunk(nPl); updateChunk(lvl, nPl);
return true; return true;
case TURN_NW: case TURN_NW:
move_on_TURN(nPl, 0, 0); move_on_TURN(nPl, 0, 0);
bumpOtherCars(nPl); bumpOtherCars(nPl);
apply_friction(nPl); apply_friction(nPl);
updateChunk(nPl); updateChunk(lvl, nPl);
return true; return true;
case TURN_SE: case TURN_SE:
move_on_TURN(nPl, ROOM_SIZE, ROOM_SIZE); move_on_TURN(nPl, ROOM_SIZE, ROOM_SIZE);
bumpOtherCars(nPl); bumpOtherCars(nPl);
apply_friction(nPl); apply_friction(nPl);
updateChunk(nPl); updateChunk(lvl, nPl);
return true; return true;
case TURN_SW: case TURN_SW:
move_on_TURN(nPl, 0, ROOM_SIZE); move_on_TURN(nPl, 0, ROOM_SIZE);
bumpOtherCars(nPl); bumpOtherCars(nPl);
apply_friction(nPl); apply_friction(nPl);
updateChunk(nPl); updateChunk(lvl, nPl);
return true; return true;
default: default:

View File

@ -548,17 +548,19 @@ void renderPlayers(SDL_Renderer* renderer, int cx, int cy, int range, int rsize)
//printf("\n"); //printf("\n");
} }
void renderMap(SDL_Renderer* renderer, level* lvl, path* pth, int cx, int cy, int range, int rsize) { void renderMap(SDL_Renderer* renderer, level* lvl, path* pth, int cx, int cy, int range, int rsize, bool showM) {
renderCircles(renderer, lvl, cx, cy, range, rsize); if(showM) {
renderStraights(renderer, lvl, cx, cy, range, rsize); renderCircles(renderer, lvl, cx, cy, range, rsize);
for(int x = -range; x <= range; x++) { renderStraights(renderer, lvl, cx, cy, range, rsize);
for(int y = -range; y <= range; y++) { for(int x = -range; x <= range; x++) {
if((cx+x == START_CHX && cy+y == START_CHY) || (cx+x == FINISH_CHX && cy+y == FINISH_CHY)) { for(int y = -range; y <= range; y++) {
SDL_RenderDrawCircle(renderer, (WIDTH)/2+rsize*(y), (HEIGHT)/2+rsize*(x), (int)(rsize/3.5), 192, 192, 192, 255); if((cx+x == START_CHX && cy+y == START_CHY) || (cx+x == FINISH_CHX && cy+y == FINISH_CHY)) {
SDL_RenderDrawCircle(renderer, (WIDTH)/2+rsize*(y), (HEIGHT)/2+rsize*(x), (int)(rsize/3.5), 192, 192, 192, 255);
}
} }
} }
renderPlayers(renderer, cx, cy, range, rsize);
} }
renderPlayers(renderer, cx, cy, range, rsize);
renderProgress(renderer, lvl, pth); renderProgress(renderer, lvl, pth);
} }

View File

@ -18,7 +18,7 @@ void drawCircleToRenderer(SDL_Renderer * renderer, int32_t centreX, int32_t cent
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); 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); pt project(int p, int cx, int cy, int range, int rsize);
void renderMap(SDL_Renderer* renderer, level* lvl, path* pth, int cx, int cy, int range, int rsize); void renderMap(SDL_Renderer* renderer, level* lvl, path* pth, int cx, int cy, int range, int rsize, bool showM);
void import_digits(SDL_Renderer* renderer); void import_digits(SDL_Renderer* renderer);
void import_letters(SDL_Renderer* renderer); void import_letters(SDL_Renderer* renderer);

View File

@ -19,6 +19,7 @@
#include "cars.h" #include "cars.h"
bool WAIT = true; bool WAIT = true;
bool SHOW = true;
bool matchDone = false; bool matchDone = false;
void cameraActions(level* lvl, bool* halt, int* cx, int* cy, int* dezoom, int* sizeR) { void cameraActions(level* lvl, bool* halt, int* cx, int* cy, int* dezoom, int* sizeR) {
@ -63,6 +64,10 @@ void cameraActions(level* lvl, bool* halt, int* cx, int* cy, int* dezoom, int* s
WAIT = !WAIT; WAIT = !WAIT;
break; break;
case SDLK_LSHIFT:
SHOW = !SHOW;
break;
default: default:
break; break;
} }
@ -167,7 +172,7 @@ int main(int argc, char** argv) {
int count = 0; int count = 0;
while(!halt) { while(!halt) {
resetRenderer(rend); resetRenderer(rend);
renderMap(rend, stage, pth, cx, cy, dezoom, sizeR); renderMap(rend, stage, pth, cx, cy, dezoom, sizeR, SHOW);
for(int p = 0; p < N_PLAYERS; p++) { for(int p = 0; p < N_PLAYERS; p++) {
pt proj = project(p, cx, cy, dezoom, sizeR); pt proj = project(p, cx, cy, dezoom, sizeR);
drawStringToRenderer(rend, &(execs[p][1]), proj.ix, proj.iy, 75/(4+dezoom), 105/(4+dezoom)); drawStringToRenderer(rend, &(execs[p][1]), proj.ix, proj.iy, 75/(4+dezoom), 105/(4+dezoom));

View File

@ -79,9 +79,64 @@ bool str_equal(char* s1, char* s2) {
return str_equal(&s1[1], &s2[1]); return str_equal(&s1[1], &s2[1]);
} }
int gains(int elo, int rank) { int* get_elo_diffs(char** execs) {
FILE* ptr = fopen("leaderboard.txt", "r");
int* elos = malloc(sizeof(int)*nPlayers);
for(int p = 0; p < nPlayers; p++) {
elos[p] = 500;
}
// ignore first line
char c = fgetc(ptr);
while(c != EOF && c != '\n') {
c = fgetc(ptr);
}
char* name = malloc(sizeof(char)*64);
int elo;
int scanres = fscanf(ptr, "%s %d", name, &elo);
while(scanres > 0) {
for(int p = 0; p < nPlayers; p++) {
if(str_equal(name, execs[p])) {
elos[p] = elo;
}
}
scanres = fscanf(ptr, "%s %d", name, &elo);
}
fclose(ptr);
free(name);
int* diffs = malloc(sizeof(int)*nPlayers);
for(int p = 0; p < nPlayers; p++) {
diffs[p] = 0;
for(int q = 0; q < nPlayers; q++) {
if(p != q) {
diffs[p] += elos[q] - elos[p];
}
}
diffs[p] = diffs[p]/(nPlayers-1+((nPlayers==1)?(1):(0)));
}
free(elos);
return diffs;
}
int smax(int a, int b) {return (a>b)?(a):(b);}
int smin(int a, int b) {return (a<b)?(a):(b);}
double fsmax(double a, double b) {return (a>b)?(a):(b);}
double fsmin(double a, double b) {return (a<b)?(a):(b);}
// +20 for #1 no matter what
// up to -70 (past 2500) for last
// if diff < -250, no bonus
// if diff > 250, 2x bonus
int gains(int elo, int rank, int diff) {
return (int)( return (int)(
60*exp(-elo*((rank-1.0)/(nPlayers-1.0))/1200.0)-34 (fsmax(0.1, fsmin(2.0, (diff+250.0)/250.0)))*20-
(smax(40,smin(90, (90*elo)/3250)))*
(((rank-1)*1.0)/((nPlayers-1+((nPlayers==1)?(1):(0)))*1.0))
); );
} }
@ -97,6 +152,8 @@ void updateLeaderBoard(char** execs, int* ranks) {
} }
fprintf(ptrOut, "\n"); fprintf(ptrOut, "\n");
int* diffs = get_elo_diffs(execs);
bool* found = malloc(sizeof(bool)*nPlayers); bool* found = malloc(sizeof(bool)*nPlayers);
char* entry = malloc(sizeof(char)*64); char* entry = malloc(sizeof(char)*64);
int elo; int elo;
@ -106,7 +163,7 @@ void updateLeaderBoard(char** execs, int* ranks) {
if(!found[p] && str_equal(execs[p], entry)) { if(!found[p] && str_equal(execs[p], entry)) {
found[p] = true; found[p] = true;
printf("[%s - rank %d/%d] : %d -", execs[p], ranks[p], nPlayers, elo); printf("[%s - rank %d/%d] : %d -", execs[p], ranks[p], nPlayers, elo);
int de = gains(elo, ranks[p]); int de = gains(elo, ranks[p], diffs[p]);
printf("(%d)-> ", de); printf("(%d)-> ", de);
elo += de; elo += de;
printf("%d\n", elo); printf("%d\n", elo);
@ -130,7 +187,7 @@ void updateLeaderBoard(char** execs, int* ranks) {
} }
for(int k = 0; k < nPlayers; k++) { for(int k = 0; k < nPlayers; k++) {
if(!found[k]) { if(!found[k]) {
fprintf(mainWrite, "%s %d\n", execs[k], 500+gains(500, ranks[k])); fprintf(mainWrite, "%s %d\n", execs[k], 500+gains(500, ranks[k], diffs[k]));
} }
} }
@ -139,4 +196,5 @@ void updateLeaderBoard(char** execs, int* ranks) {
free(entry); free(entry);
free(found); free(found);
free(diffs);
} }

View File

@ -1,5 +1,7 @@
name, elo name, elo
./bots/dumb 2154 ./bots/dumb 551
./bots/dumb 1000 ./bots/dumb2 547
./bots/dumb 1500 ./bots/dumb3 512
./bots/dumb 501 ./bots/dumb4 512
./bots/dumb5 540
./bots/dumb6 562