diff --git a/.vscode/settings.json b/.vscode/settings.json index 502ca39..e6f0ad5 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -8,6 +8,8 @@ "base.h": "c", "cars.h": "c", "sdl_image.h": "c", - "collisions.h": "c" + "collisions.h": "c", + "stdbool.h": "c", + "music.h": "c" } } \ No newline at end of file diff --git a/Makefile b/Makefile index 43bbf94..25fa789 100644 --- a/Makefile +++ b/Makefile @@ -25,7 +25,7 @@ road: bin/back mem: bin/back 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/music.o obj/cars.o obj/rooms.o obj/structure.o mkdir -p bin $(CC) $(FLAGS) $^ $(LFLAGS) -o $@ @@ -37,6 +37,7 @@ obj/main.o: src/main.c obj/display.o: src/display.c obj/base.o: src/base.c obj/cars.o: src/cars.c +obj/music.o: src/music.c obj/rooms.o: src/rooms.c obj/collisions.o: src/collisions.c obj/structure.o: src/structure.c diff --git a/answer.txt b/answer.txt index 337a31e..32b7faf 100644 --- a/answer.txt +++ b/answer.txt @@ -1 +1 @@ -288 75 \ No newline at end of file +271 90 \ No newline at end of file diff --git a/bin/back b/bin/back index 0158b64..2d9c046 100755 Binary files a/bin/back and b/bin/back differ diff --git a/lastActions.txt b/lastActions.txt index 8745157..6e8e1b5 100644 --- a/lastActions.txt +++ b/lastActions.txt @@ -1,22 +1,53 @@ -83.689682 166.568590 -161.856726 -68.371906 --21.799328 154.573210 -167.141051 -112.643060 -23.867801 -137.063781 --134.600006 141.659189 --131.665050 -68.540697 --188.330352 56.382417 --44.777939 -151.693916 --157.607132 -25.718204 -141.568869 35.871573 --11.482366 -141.473584 --161.353467 -52.403609 -118.974318 -60.269145 -142.449106 80.225227 --156.434592 120.574661 -130.379231 139.672870 --107.308604 -82.900739 -69.679734 -172.732818 -150.627007 71.542801 -46.785402 -141.274814 -61.850213 -128.983670 +-91.598146 124.327144 +142.266681 -23.083685 +80.023571 -110.291019 +-209.326753 22.630339 +68.505747 119.501330 +107.060380 -114.069708 +-70.233012 -101.577163 +147.160673 -72.086463 +-140.110322 106.226989 +135.086455 98.172637 +56.107652 105.827274 +35.854738 -152.398792 +132.320996 -67.163737 +176.181947 66.489059 +130.017074 -31.088727 +66.243545 115.114005 +145.260991 -91.403980 +-69.982160 -182.672565 +35.887790 -183.222296 +108.756771 95.947835 +-49.680253 -115.091637 +58.894910 114.835148 +99.088389 -156.633565 +-187.977088 -51.719528 +-104.449063 -91.836921 +-107.925240 138.542331 +-94.113722 79.680583 +-97.214088 -68.731116 +-100.166986 -61.714426 +-115.272316 110.207498 +-96.395609 -136.982500 +-43.753892 -133.340136 +-66.862857 105.898787 +91.865015 121.247073 +-95.201198 111.796966 +37.801519 -111.152427 +53.162045 174.621969 +-83.439530 168.654458 +86.164970 159.092863 +-62.380427 -152.204942 +17.433048 133.218210 +-105.950486 88.326322 +-47.503687 -175.362014 +-63.992977 156.620065 +-155.337407 -113.318321 +114.800901 120.609239 +-11.129331 -159.843313 +79.955484 139.639897 +-150.338137 27.027579 +-165.852701 9.876618 +-166.712365 85.606507 +102.833309 109.163228 +-16.456985 -187.091529 diff --git a/leaderboard.txt b/leaderboard.txt index f5e586e..d67608d 100644 --- a/leaderboard.txt +++ b/leaderboard.txt @@ -1,7 +1,7 @@ name, elo -./bots/dumb 1637 -./bots/dumb2 1592 -./bots/dumb3 1611 -./bots/dumb4 1519 +./bots/dumb 1656 +./bots/dumb2 1542 +./bots/dumb3 1633 +./bots/dumb4 1481 ./bots/dumb5 540 ./bots/dumb6 562 diff --git a/obj/collisions.o b/obj/collisions.o index c1badde..f4bd036 100644 Binary files a/obj/collisions.o and b/obj/collisions.o differ diff --git a/obj/main.o b/obj/main.o index f9453a9..bac39e1 100644 Binary files a/obj/main.o and b/obj/main.o differ diff --git a/obj/music.o b/obj/music.o new file mode 100644 index 0000000..0935dde Binary files /dev/null and b/obj/music.o differ diff --git a/output.txt b/output.txt index 835a842..3e7e920 100644 --- a/output.txt +++ b/output.txt @@ -1,12 +1,16 @@ 4 -0 (2 0) (10.04 51.32) -1 (2 0) (32.42 40.43) -2 (2 0) (72.81 52.36) -3 (0 0) (89.50 92.97) +0 (3 0) (22.26 39.65) +1 (1 3) (20.14 43.41) +2 (2 0) (27.37 58.70) +3 (2 0) (73.15 83.07) [3] -1 3 -S1E +5 5 +..S14 +....0 +...35 +.E15. +..... 100 200 5 0.90 0.80 0.20 diff --git a/sound/audio/kahoot-gong.wav b/sound/audio/kahoot-gong.wav new file mode 100644 index 0000000..bbca277 Binary files /dev/null and b/sound/audio/kahoot-gong.wav differ diff --git a/sound/audio/kahoot_10s.mp3 b/sound/audio/kahoot_10s.mp3 new file mode 100644 index 0000000..104d28e Binary files /dev/null and b/sound/audio/kahoot_10s.mp3 differ diff --git a/sound/audio/kahoot_10s.wav b/sound/audio/kahoot_10s.wav new file mode 100644 index 0000000..e6aac9f Binary files /dev/null and b/sound/audio/kahoot_10s.wav differ diff --git a/sound/audio/kahoot_20s.mp3 b/sound/audio/kahoot_20s.mp3 new file mode 100644 index 0000000..9139061 Binary files /dev/null and b/sound/audio/kahoot_20s.mp3 differ diff --git a/sound/audio/kahoot_20s.wav b/sound/audio/kahoot_20s.wav new file mode 100644 index 0000000..974301c Binary files /dev/null and b/sound/audio/kahoot_20s.wav differ diff --git a/sound/audio/kahoot_30s.mp3 b/sound/audio/kahoot_30s.mp3 new file mode 100644 index 0000000..42d7c92 Binary files /dev/null and b/sound/audio/kahoot_30s.mp3 differ diff --git a/sound/audio/kahoot_30s.wav b/sound/audio/kahoot_30s.wav new file mode 100644 index 0000000..85eefd5 Binary files /dev/null and b/sound/audio/kahoot_30s.wav differ diff --git a/sound/audio/kahoot_5s.mp3 b/sound/audio/kahoot_5s.mp3 new file mode 100644 index 0000000..3987d88 Binary files /dev/null and b/sound/audio/kahoot_5s.mp3 differ diff --git a/sound/audio/kahoot_5s.wav b/sound/audio/kahoot_5s.wav new file mode 100644 index 0000000..6317c31 Binary files /dev/null and b/sound/audio/kahoot_5s.wav differ diff --git a/sound/audio/kahoot_60s.mp3 b/sound/audio/kahoot_60s.mp3 new file mode 100644 index 0000000..3ee6aa0 Binary files /dev/null and b/sound/audio/kahoot_60s.mp3 differ diff --git a/sound/audio/kahoot_60s.wav b/sound/audio/kahoot_60s.wav new file mode 100644 index 0000000..65c2c83 Binary files /dev/null and b/sound/audio/kahoot_60s.wav differ diff --git a/sound/audio/smw_coin.wav b/sound/audio/smw_coin.wav new file mode 100644 index 0000000..09f9ca0 Binary files /dev/null and b/sound/audio/smw_coin.wav differ diff --git a/sound/audio/tetris_2.wav b/sound/audio/tetris_2.wav new file mode 100755 index 0000000..ae56c39 Binary files /dev/null and b/sound/audio/tetris_2.wav differ diff --git a/sound/compilation.sh b/sound/compilation.sh new file mode 100644 index 0000000..6f6a086 --- /dev/null +++ b/sound/compilation.sh @@ -0,0 +1 @@ +gcc sound/playsound.c -lSDL2 -o sound/music diff --git a/sound/music b/sound/music new file mode 100755 index 0000000..18f642f Binary files /dev/null and b/sound/music differ diff --git a/sound/playsound.c b/sound/playsound.c new file mode 100644 index 0000000..46bdec9 --- /dev/null +++ b/sound/playsound.c @@ -0,0 +1,37 @@ +#include +#include +#include +#include +#include +#include + +int can_pass(char* filename) { + FILE* ptr = fopen(filename, "r"); + char c = fgetc(ptr); + fclose(ptr); + return (c != '0'); +} + +int main(int argc, char** argv) { + assert(argc == 2); + if(SDL_Init(SDL_INIT_AUDIO)) {fprintf(stderr, "cannot initialize audio");exit(1);} + SDL_AudioSpec wavSpec; + Uint32 wavLength; + Uint8 *wavBuffer; + SDL_LoadWAV(argv[1], &wavSpec, &wavBuffer, &wavLength); + SDL_AudioDeviceID deviceId = SDL_OpenAudioDevice(NULL, 0, &wavSpec, NULL, 0); + int success = SDL_QueueAudio(deviceId, wavBuffer, wavLength); + SDL_PauseAudioDevice(deviceId, 0); + clock_t start = clock(); + clock_t finish = clock(); + while((double)finish - (double)start < (wavLength)/44100.0*100000.0 && can_pass("sound/sem.txt")) { + usleep(10000); + finish = clock(); + } + //usleep((int)((wavLength)/44100.0*100000.0)); + SDL_CloseAudioDevice(deviceId); + SDL_FreeWAV(wavBuffer); + SDL_Quit(); + return 0; +} +// yummy \ No newline at end of file diff --git a/sound/sem.txt b/sound/sem.txt new file mode 100644 index 0000000..56a6051 --- /dev/null +++ b/sound/sem.txt @@ -0,0 +1 @@ +1 \ No newline at end of file diff --git a/src/collisions.c b/src/collisions.c index ddffc79..dc61eef 100644 --- a/src/collisions.c +++ b/src/collisions.c @@ -12,8 +12,9 @@ #include #include "structure.h" -#include "cars.h" #include "base.h" +#include "cars.h" +#include "music.h" #include "collisions.h" // use after updating the position @@ -311,6 +312,7 @@ void hasWon(int nPl, int* ranks, int* incr) { if(winners[nPl]==1 && ranks[nPl] == nPlayers) { ranks[nPl] = nextRank; *incr += 1; + play_sound("sound/audio/kahoot_10s.wav"); } if(winners[nPl]==1 && remainingTurns == -1) { remainingTurns = 4; diff --git a/src/main.c b/src/main.c index 0d81aa3..4a495a1 100644 --- a/src/main.c +++ b/src/main.c @@ -13,6 +13,7 @@ #include "structure.h" #include "base.h" +#include "music.h" #include "display.h" #include "collisions.h" #include "rooms.h" @@ -166,6 +167,7 @@ int main(int argc, char** argv) { import_letters(rend); import_digits(rend); + init_music(); // ---------------------------- // //stage_file("levels/stage.txt"); level* stage = parse_level(argv[1]); @@ -255,6 +257,7 @@ int main(int argc, char** argv) { free_level(stage); freePath(pth); fclose(save); + end_music(); for(int k = 0; k < N_PLAYERS; k++) { free(execs[k]); } diff --git a/src/music.c b/src/music.c new file mode 100644 index 0000000..fbcb359 --- /dev/null +++ b/src/music.c @@ -0,0 +1,133 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "structure.h" +#include "base.h" +#include "music.h" + +static pthread_t threads[32]; +static bool threadIsFree[32]; +static int threadID; + +static int thrID; + +pthread_mutex_t lock; + +void init_music() { + pthread_mutex_init(&lock, NULL); + threadID = 0; + for(int k = 0; k < 32; k++) { + threadIsFree[k] = true; + } +} + +void end_music() { + pthread_mutex_destroy(&lock); + + FILE* endptr = fopen("sound/sem.txt", "w"); + fprintf(endptr, "0"); + fclose(endptr); + + usleep(100000); + + FILE* stptr = fopen("sound/sem.txt", "w"); + fprintf(stptr, "1"); + fclose(stptr); + + for(int k = 0; k < 32; k++) { + if(!threadIsFree[k]) { + printf("[pthread] killing %d\n", k); + pthread_cancel(threads[k]); + } + } +} + +void reset_music() { + FILE* endptr = fopen("sound/sem.txt", "w"); + fprintf(endptr, "0"); + fclose(endptr); + + usleep(100000); + + FILE* stptr = fopen("sound/sem.txt", "w"); + fprintf(stptr, "1"); + fclose(stptr); +} + +char* get_name(char* filename) { + char* res = malloc(sizeof(char)*100); + res[0] = '.'; + res[1] = '/'; + res[2] = 's'; + res[3] = 'o'; + res[4] = 'u'; + res[5] = 'n'; + res[6] = 'd'; + res[7] = '/'; + res[8] = 'm'; + res[9] = 'u'; + res[10] = 's'; + res[11] = 'i'; + res[12] = 'c'; + res[13] = ' '; + int k = 14; + while(k < 99 &&filename[k-14] != '\0') { + res[k] = filename[k-14]; + k += 1; + } + res[k] = '\0'; + return res; +} + +void* mainMusic(void* arg) { + int localThreadID = thrID; + char* name = get_name((char*)arg); + int i=0; + //while(name[i] != '\0') { + // printf("%c", name[i]); + // i +=1; + //} + system(name); + free(name); + threadIsFree[localThreadID] = true; + //printf("thread %d is done\n", localThreadID); + return NULL; +} + +bool get_id(int* res) { + int lp = 0; + while(lp < 32) { + if(threadIsFree[(lp+threadID)%32]) { + *res = (lp+threadID)%32; + return true; + } + lp += 1; + } + return false; +} + +void play_sound(char* filename) { + pthread_mutex_lock(&lock); + int id; + if(get_id(&id)) { + //printf("[music] %d\n"); + threadIsFree[id] = false; + thrID = id; + threadID += 1; + pthread_create(&threads[id], NULL, mainMusic, filename); + } else { + fprintf(stderr, "WARNING : cannot play audio %s (out of space)\n", filename); + } + pthread_mutex_unlock(&lock); +} \ No newline at end of file diff --git a/src/music.h b/src/music.h new file mode 100644 index 0000000..5497de0 --- /dev/null +++ b/src/music.h @@ -0,0 +1,10 @@ +#ifndef MUSIC_H +#define MUSIC_H + +void init_music(); +void reset_music(); +void end_music(); + +void play_sound(char* filename); + +#endif \ No newline at end of file diff --git a/tempLeader.txt b/tempLeader.txt index f5e586e..d67608d 100644 --- a/tempLeader.txt +++ b/tempLeader.txt @@ -1,7 +1,7 @@ name, elo -./bots/dumb 1637 -./bots/dumb2 1592 -./bots/dumb3 1611 -./bots/dumb4 1519 +./bots/dumb 1656 +./bots/dumb2 1542 +./bots/dumb3 1633 +./bots/dumb4 1481 ./bots/dumb5 540 ./bots/dumb6 562