diff --git a/Makefile b/Makefile index 11146ee..b53367c 100644 --- a/Makefile +++ b/Makefile @@ -5,19 +5,22 @@ LFLAGS = -lSDL2 -lSDL2_image -lm all: 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 - 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 - 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 - 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 - 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 mkdir -p bin diff --git a/answer.txt b/answer.txt index 5c406b3..41f3620 100644 --- a/answer.txt +++ b/answer.txt @@ -1 +1 @@ -155 80 \ No newline at end of file +102 61 \ No newline at end of file diff --git a/bin/back b/bin/back index 5baf2ce..d4a04e2 100755 Binary files a/bin/back and b/bin/back differ diff --git a/bots/dumb2 b/bots/dumb2 new file mode 100755 index 0000000..0bbf42a Binary files /dev/null and b/bots/dumb2 differ diff --git a/bots/dumb3 b/bots/dumb3 new file mode 100755 index 0000000..0bbf42a Binary files /dev/null and b/bots/dumb3 differ diff --git a/bots/dumb4 b/bots/dumb4 new file mode 100755 index 0000000..0bbf42a Binary files /dev/null and b/bots/dumb4 differ diff --git a/bots/dumb5 b/bots/dumb5 new file mode 100755 index 0000000..0bbf42a Binary files /dev/null and b/bots/dumb5 differ diff --git a/bots/dumb6 b/bots/dumb6 new file mode 100755 index 0000000..0bbf42a Binary files /dev/null and b/bots/dumb6 differ diff --git a/leaderboard.txt b/leaderboard.txt index 5dd5ff8..43f7a20 100644 --- a/leaderboard.txt +++ b/leaderboard.txt @@ -1,5 +1,7 @@ name, elo -./bots/dumb 2154 -./bots/dumb 1000 -./bots/dumb 1500 -./bots/dumb 501 +./bots/dumb 551 +./bots/dumb2 547 +./bots/dumb3 512 +./bots/dumb4 512 +./bots/dumb5 540 +./bots/dumb6 562 diff --git a/obj/collisions.o b/obj/collisions.o index edf7bbe..c1badde 100644 Binary files a/obj/collisions.o and b/obj/collisions.o differ diff --git a/obj/display.o b/obj/display.o index 64ee9bb..5a2ca59 100644 Binary files a/obj/display.o and b/obj/display.o differ diff --git a/obj/main.o b/obj/main.o index 7025d55..03bb60d 100644 Binary files a/obj/main.o and b/obj/main.o differ diff --git a/obj/structure.o b/obj/structure.o index 8d3e3c9..20ef4c8 100644 Binary files a/obj/structure.o and b/obj/structure.o differ diff --git a/output.txt b/output.txt index bacaecb..d458f93 100644 --- a/output.txt +++ b/output.txt @@ -1,12 +1,18 @@ 4 -0 (2 0) (32.28 87.09) -1 (2 0) (83.34 49.89) -2 (2 0) (81.67 74.71) -3 (1 0) (79.36 63.40) +0 (6 4) (31.00 19.85) +1 (4 1) (46.31 83.63) +2 (2 1) (85.48 74.34) +3 (2 1) (66.86 66.86) [3] -1 3 -S1E +7 8 +........ +..31S... +..24.... +...24... +...353E. +...215.. +........ 100 200 5 0.90 0.80 0.20 diff --git a/src/collisions.c b/src/collisions.c index a7c4819..ddffc79 100644 --- a/src/collisions.c +++ b/src/collisions.c @@ -17,23 +17,23 @@ #include "collisions.h" // use after updating the position -void updateChunk(int nPl) { +void updateChunk(level* lvl, int nPl) { car* c = players[nPl].c; if(c->pos.fx < 0.0) { 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) { c->pos.fx -= 1.0*ROOM_SIZE; - c->chx += 1; + if(c->chx < lvl->cols){c->chx += 1;} } if(c->pos.fy < 0.0) { 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) { 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.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); bumpOtherCars(nPl); apply_friction(nPl); - updateChunk(nPl); + updateChunk(lvl, nPl); return true; case END: move_on_FINISH(nPl); bumpOtherCars(nPl); apply_friction(nPl); - updateChunk(nPl); + updateChunk(lvl, nPl); return true; case STR_V: move_on_STR_V(nPl); bumpOtherCars(nPl); apply_friction(nPl); - updateChunk(nPl); + updateChunk(lvl, nPl); return true; case STR_H: move_on_STR_H(nPl); bumpOtherCars(nPl); apply_friction(nPl); - updateChunk(nPl); + updateChunk(lvl, nPl); return true; case TURN_NE: move_on_TURN(nPl, ROOM_SIZE, 0); bumpOtherCars(nPl); apply_friction(nPl); - updateChunk(nPl); + updateChunk(lvl, nPl); return true; case TURN_NW: move_on_TURN(nPl, 0, 0); bumpOtherCars(nPl); apply_friction(nPl); - updateChunk(nPl); + updateChunk(lvl, nPl); return true; case TURN_SE: move_on_TURN(nPl, ROOM_SIZE, ROOM_SIZE); bumpOtherCars(nPl); apply_friction(nPl); - updateChunk(nPl); + updateChunk(lvl, nPl); return true; case TURN_SW: move_on_TURN(nPl, 0, ROOM_SIZE); bumpOtherCars(nPl); apply_friction(nPl); - updateChunk(nPl); + updateChunk(lvl, nPl); return true; default: diff --git a/src/display.c b/src/display.c index 3f51e54..72d8e2d 100644 --- a/src/display.c +++ b/src/display.c @@ -548,17 +548,19 @@ void renderPlayers(SDL_Renderer* renderer, int cx, int cy, int range, int rsize) //printf("\n"); } -void renderMap(SDL_Renderer* renderer, level* lvl, path* pth, int cx, int cy, int range, int rsize) { - renderCircles(renderer, lvl, cx, cy, range, rsize); - renderStraights(renderer, lvl, cx, cy, range, rsize); - for(int x = -range; x <= range; x++) { - for(int y = -range; y <= range; y++) { - 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); +void renderMap(SDL_Renderer* renderer, level* lvl, path* pth, int cx, int cy, int range, int rsize, bool showM) { + if(showM) { + renderCircles(renderer, lvl, cx, cy, range, rsize); + renderStraights(renderer, lvl, cx, cy, range, rsize); + for(int x = -range; x <= range; x++) { + for(int y = -range; y <= range; y++) { + 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); } diff --git a/src/display.h b/src/display.h index 28956b0..9e8db85 100644 --- a/src/display.h +++ b/src/display.h @@ -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); 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_letters(SDL_Renderer* renderer); diff --git a/src/main.c b/src/main.c index 18ae0c9..d210860 100644 --- a/src/main.c +++ b/src/main.c @@ -19,6 +19,7 @@ #include "cars.h" bool WAIT = true; +bool SHOW = true; bool matchDone = false; 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; break; + case SDLK_LSHIFT: + SHOW = !SHOW; + break; + default: break; } @@ -167,7 +172,7 @@ int main(int argc, char** argv) { int count = 0; while(!halt) { 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++) { pt proj = project(p, cx, cy, dezoom, sizeR); drawStringToRenderer(rend, &(execs[p][1]), proj.ix, proj.iy, 75/(4+dezoom), 105/(4+dezoom)); diff --git a/src/structure.c b/src/structure.c index 02b83f1..52c7bcb 100644 --- a/src/structure.c +++ b/src/structure.c @@ -79,9 +79,64 @@ bool str_equal(char* s1, char* s2) { 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 (ab)?(a):(b);} +double fsmin(double a, double b) {return (a 250, 2x bonus +int gains(int elo, int rank, int diff) { 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"); + int* diffs = get_elo_diffs(execs); + bool* found = malloc(sizeof(bool)*nPlayers); char* entry = malloc(sizeof(char)*64); int elo; @@ -106,7 +163,7 @@ void updateLeaderBoard(char** execs, int* ranks) { if(!found[p] && str_equal(execs[p], entry)) { found[p] = true; 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); elo += de; printf("%d\n", elo); @@ -130,7 +187,7 @@ void updateLeaderBoard(char** execs, int* ranks) { } for(int k = 0; k < nPlayers; 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(found); + free(diffs); } \ No newline at end of file diff --git a/tempLeader.txt b/tempLeader.txt index 5dd5ff8..43f7a20 100644 --- a/tempLeader.txt +++ b/tempLeader.txt @@ -1,5 +1,7 @@ name, elo -./bots/dumb 2154 -./bots/dumb 1000 -./bots/dumb 1500 -./bots/dumb 501 +./bots/dumb 551 +./bots/dumb2 547 +./bots/dumb3 512 +./bots/dumb4 512 +./bots/dumb5 540 +./bots/dumb6 562