game.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  1. /*
  2. * File: game.c
  3. * Author: Arthur Brandao
  4. *
  5. * Created on 28 novembre 2018
  6. */
  7. #define _XOPEN_SOURCE 500
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include "error.h"
  11. #include "str.h"
  12. #include "file.h"
  13. #include "game.h"
  14. #include "bomberstudent_server.h"
  15. /* --- Extern --- */
  16. Game game[MAXGAME];
  17. int nbGame = 0;
  18. pthread_mutex_t gameMutex[MAXGAME];
  19. pthread_mutex_t playerMutex[MAXGAME * MAXPLAYER];
  20. /* --- Fonctions privées --- */
  21. /**
  22. * Transforme le fichier map en un tableau 2D
  23. * @param char* Le contenu du fichier map
  24. * @param int Largeur de la map
  25. * @param int Hauteur de la map
  26. * @return char** La map parser
  27. */
  28. char** parse_map(char* mapContent, int width, int height) {
  29. char** map = malloc(sizeof (char*) * width);
  30. //Creation des colonnes
  31. for (int i = 0; i < width; i++) {
  32. map[i] = malloc(sizeof (char) * height);
  33. }
  34. //Remplissage
  35. for (int i = 0; i < height; i++) {
  36. for (int j = 0; j < width; j++) {
  37. map[j][i] = *mapContent;
  38. mapContent++;
  39. }
  40. mapContent++;
  41. }
  42. return map;
  43. }
  44. /**
  45. * Transforme un tableau 2D en une chaine
  46. * @param char** Le contenu du fichier map
  47. * @param int Largeur de la map
  48. * @param int Hauteur de la map
  49. * @return char* La map
  50. */
  51. char* unparse_map(char** map, int width, int height) {
  52. int pos = 0;
  53. char* mapContent = malloc(sizeof (char) * (width * height + 1));
  54. memset(mapContent, 0, width * height + 1);
  55. //Remplissage
  56. for (int i = 0; i < height; i++) {
  57. for (int j = 0; j < width; j++) {
  58. mapContent[pos++] = map[j][i];
  59. }
  60. }
  61. return mapContent;
  62. }
  63. /**
  64. * Cherche le joueur à qui appartient un objet
  65. * @param Game* La game du joueur
  66. * @param int Le type de l'objet
  67. * @param int X de l'objet
  68. * @param int Y de l'objet
  69. * @return Player* Le joueur trouvé ou NULL
  70. */
  71. Player* search_player_object(Game* g, int type, int x, int y){
  72. obj_node* objn;
  73. for(int i = 0; i < MAXPLAYER; i++){
  74. if(g->player[i]->ini){
  75. objn = object_search(g->player[i]->bomb, type, x, y);
  76. if(objn != NULL){
  77. return g->player[i];
  78. }
  79. }
  80. }
  81. return NULL;
  82. }
  83. /* --- Fonctions publiques --- */
  84. boolean ini_games() {
  85. //Ini les games
  86. for (int i = 0; i < MAXGAME; i++) {
  87. game[i].active = false;
  88. game[i].index = i;
  89. }
  90. //Ini les mutex des games
  91. for (int i = 0; i < MAXGAME; i++) {
  92. if (pthread_mutex_init(&gameMutex[i], NULL) != 0) {
  93. adderror("Impossible d'initialiser les mutex des games");
  94. return false;
  95. }
  96. }
  97. //Ini les mutex des joueurs des games
  98. for (int i = 0; i < MAXGAME * MAXPLAYER; i++) {
  99. if (pthread_mutex_init(&playerMutex[i], NULL) != 0) {
  100. adderror("Impossible d'initialiser les mutex des joueurs");
  101. return false;
  102. }
  103. }
  104. return true;
  105. }
  106. void list_map(JsonArray* res) {
  107. char** result;
  108. int nbResult;
  109. //Regarde les fichiers dans le dossier
  110. result = file_list(MAPDIR, &nbResult);
  111. for (int i = 0; i < nbResult; i++) {
  112. add_array_string(res, result[i]);
  113. free(result[i]);
  114. }
  115. free(result);
  116. }
  117. int list_game(JsonArray* res) {
  118. JsonEncoder gameJson;
  119. int compteur = 0, i = 0;
  120. //Si il n' y a aucune game
  121. if (nbGame == 0) {
  122. return 0;
  123. }
  124. //Initialisation json
  125. ini_encoder(&gameJson);
  126. //Ajoute chaque game
  127. while (compteur < nbGame && i < MAXGAME) {
  128. pthread_mutex_lock(&gameMutex[i]);
  129. if (!game[i].active) {
  130. pthread_mutex_unlock(&gameMutex[i]);
  131. i++;
  132. continue;
  133. }
  134. //Creation objet json
  135. add_string(&gameJson, "name", game[i].name);
  136. add_integer(&gameJson, "nbPlayer", game[i].nbPlayer);
  137. add_string(&gameJson, "map", game[i].mapName);
  138. //Ajout dans le tableau
  139. add_array_object(res, &gameJson);
  140. //Nettoyage encoder objet
  141. clean_json_encoder(&gameJson);
  142. //Incremente
  143. pthread_mutex_unlock(&gameMutex[i]);
  144. i++;
  145. compteur++;
  146. }
  147. return nbGame;
  148. }
  149. int* map_size(char* map) {
  150. int* res;
  151. res = malloc(sizeof (int) * 2);
  152. res[WIDTH] = 0;
  153. res[HEIGHT] = 1;
  154. //Parcours la 1er ligne pour compter le nombre de caractère
  155. while (*map != '\n' && *map != '\0') {
  156. res[WIDTH]++;
  157. map++;
  158. }
  159. if (*map == '\0') {
  160. return res;
  161. }
  162. //Compte les lignes
  163. map++;
  164. while (*map != '\0') {
  165. if (*map == '\n') {
  166. res[HEIGHT]++;
  167. }
  168. map++;
  169. }
  170. return res;
  171. }
  172. int create_game(char* name, char* map) {
  173. int length, index = 0;
  174. char* path;
  175. int* size;
  176. //Regarde si il reste une game de disponnible
  177. if (nbGame == MAXGAME) {
  178. return ERR;
  179. }
  180. //Cherche une game libre
  181. while (index < MAXGAME) {
  182. pthread_mutex_lock(&gameMutex[index]);
  183. if (game[index].active == false) {
  184. break;
  185. }
  186. pthread_mutex_unlock(&gameMutex[index]);
  187. index++;
  188. }
  189. //Recup la map
  190. length = strlen(MAPDIR) + strlen(map);
  191. path = new_string(length);
  192. snprintf(path, length + 1, "%s%s", MAPDIR, map);
  193. game[index].mapContent = file_get_content(path);
  194. //Calcul taille de la map
  195. size = map_size(game[index].mapContent);
  196. //Set Up
  197. game[index].active = true;
  198. game[index].name = string_copy(name);
  199. game[index].nbPlayer = 0;
  200. game[index].mapName = string_copy(map);
  201. game[index].width = size[WIDTH];
  202. game[index].height = size[HEIGHT];
  203. game[index].map = parse_map(game[index].mapContent, size[WIDTH], size[HEIGHT]);
  204. for (int i = 0; i < MAXPLAYER; i++) {
  205. game[index].player[i] = malloc(sizeof (Player));
  206. game[index].player[i]->ini = false;
  207. }
  208. game[index].object = malloc(sizeof (Object));
  209. object_ini(game[index].object);
  210. //object_add(game[index].object, OBJ_MAJOR, 1, 1);
  211. pthread_mutex_unlock(&gameMutex[index]);
  212. //Retourne la position de la partie dans le tableau
  213. nbGame++;
  214. free(path);
  215. free(size);
  216. return index;
  217. }
  218. int add_player(Game* g, int cliId) {
  219. int index = 0;
  220. Client* cli;
  221. //Verif que la game n'est pas deja pleine
  222. pthread_mutex_lock(&gameMutex[g->index]);
  223. if (g->nbPlayer >= MAXPLAYER) {
  224. pthread_mutex_unlock(&gameMutex[g->index]);
  225. return ERR;
  226. }
  227. //Cherche un joueur non initialisé
  228. while (index < MAXPLAYER) {
  229. pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + index]);
  230. if (!g->player[index]->ini) {
  231. break;
  232. }
  233. pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + index]);
  234. index++;
  235. }
  236. //Recup du client
  237. cli = get_client(cliId);
  238. if (cli == NULL) {
  239. pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + index]);
  240. pthread_mutex_unlock(&gameMutex[g->index]);
  241. return ERR;
  242. }
  243. //Creation du joueur
  244. create_player(g->player[index], cli);
  245. g->nbPlayer++;
  246. pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + index]);
  247. pthread_mutex_unlock(&gameMutex[g->index]);
  248. return index;
  249. }
  250. void describe_game(Game* g, int playerIndex, JsonEncoder* desc) {
  251. JsonEncoder player;
  252. JsonEncoder map;
  253. JsonArray players;
  254. char* content;
  255. //Initialisation
  256. ini_encoder(&player);
  257. ini_encoder(&map);
  258. ini_array_encoder(&players);
  259. //Info map
  260. pthread_mutex_lock(&gameMutex[g->index]);
  261. content = remove_char(g->mapContent, '\n'); //La map sans \n car interdit en JSON
  262. add_integer(&map, "width", g->width);
  263. add_integer(&map, "height", g->height);
  264. add_string(&map, "content", content);
  265. free(content);
  266. //Ajout info
  267. add_integer(desc, "nbPlayers", g->nbPlayer);
  268. add_object(desc, "map", &map);
  269. //Ajout info courte joueur
  270. //printf("Add players\n");
  271. for (int i = 0; i < MAXPLAYER; i++) {
  272. pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + i]);
  273. if (g->player[i]->ini) {
  274. describe_short_player(g->player[i], &player);
  275. add_array_object(&players, &player);
  276. clean_json_encoder(&player);
  277. }
  278. pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + i]);
  279. }
  280. //printf("Fin Add players\n");
  281. add_array(desc, "players", &players);
  282. //Si besoins ajout un joueur
  283. if (playerIndex >= 0 && playerIndex < MAXPLAYER) {
  284. pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + playerIndex]);
  285. if (g->player[playerIndex]->ini) {
  286. describe_player(g->player[playerIndex], &player);
  287. add_object(desc, "player", &player);
  288. clean_json_encoder(&player);
  289. }
  290. pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + playerIndex]);
  291. }
  292. //Nettoyage
  293. pthread_mutex_unlock(&gameMutex[g->index]);
  294. clean_json_encoder(&map);
  295. clean_json_array(&players);
  296. }
  297. boolean notify_player(Game* g, char* method, char* ressource, JsonEncoder* param, int excludePlayerId) {
  298. boolean res = true;
  299. pthread_mutex_lock(&gameMutex[g->index]);
  300. //Regarde si la game est active
  301. if (!g->active) {
  302. pthread_mutex_unlock(&gameMutex[g->index]);
  303. return false;
  304. }
  305. //Parcours les joeurs de la game
  306. for (int i = 0; i < MAXPLAYER; i++) {
  307. //Si joueur actif
  308. pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + i]);
  309. if (g->player[i]->ini && g->player[i]->id != excludePlayerId) {
  310. res = res && notify_client(g->player[i]->cli, method, ressource, param);
  311. }
  312. pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + i]);
  313. }
  314. pthread_mutex_unlock(&gameMutex[g->index]);
  315. return res;
  316. }
  317. boolean player_collision(Game* g, int x, int y) {
  318. for (int i = 0; i < MAXPLAYER; i++) {
  319. if (g->player[i]->ini) {
  320. if (g->player[i]->x == x && g->player[i]->y == y) {
  321. return true;
  322. }
  323. }
  324. }
  325. return false;
  326. }
  327. boolean player_collision_index(Game* g, int x, int y, int* index) {
  328. for (int i = 0; i < MAXPLAYER; i++) {
  329. if (g->player[i]->ini) {
  330. if (g->player[i]->x == x && g->player[i]->y == y) {
  331. *index = i;
  332. return true;
  333. }
  334. }
  335. }
  336. return false;
  337. }
  338. boolean bomb_explode(Game* g, int playerIndex, int x, int y, JsonEncoder* json) {
  339. JsonEncoder object, notif;
  340. JsonArray bomb, bonus, chain;
  341. int res, index, cBomb = 0, cBonus = 0, cChain = 0;
  342. char* map;
  343. //Inie json
  344. ini_encoder(&object);
  345. ini_encoder(&notif);
  346. ini_array_encoder(&bomb);
  347. ini_array_encoder(&bonus);
  348. ini_array_encoder(&chain);
  349. //Mutex
  350. pthread_mutex_lock(&gameMutex[g->index]);
  351. if(playerIndex >= 0) pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + playerIndex]);
  352. //Determine le type de la bombe
  353. switch (g->map[x][y]) {
  354. case '1':
  355. add_string(json, "type", "classic");
  356. g->player[playerIndex]->classicBomb++;
  357. g->player[playerIndex]->nbBomb--;
  358. break;
  359. case '2':
  360. add_string(json, "type", "mine");
  361. g->player[playerIndex]->mine++;
  362. g->player[playerIndex]->nbBomb--;
  363. break;
  364. case '3':
  365. add_string(json, "type", "remote");
  366. g->player[playerIndex]->remoteBomb++;
  367. g->player[playerIndex]->nbBomb--;
  368. break;
  369. default:
  370. if(playerIndex >= 0) pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + playerIndex]);
  371. pthread_mutex_unlock(&gameMutex[g->index]);
  372. return false;
  373. }
  374. //Determine la position de la bombe
  375. char pos[20];
  376. memset(pos, 0, 20);
  377. snprintf(pos, 20, "%d,%d", x, y);
  378. add_string(json, "pos", pos);
  379. //Calcul l'explosion non mine
  380. if (g->map[x][y] != '2') {
  381. g->map[x][y] = '_';
  382. //Si le joueur à un bonus
  383. if (g->player[playerIndex]->firePower > 0) {
  384. add_boolean(json, "bonus", true);
  385. } else {
  386. add_boolean(json, "bonus", false);
  387. }
  388. //Vers le haut
  389. for (int i = 0; i < 2 + g->player[playerIndex]->firePower; i++) {
  390. //Si on est dans la map
  391. if (y - 1 - i >= 0) {
  392. //Si un mur destructible
  393. if (g->map[x][y - 1 - i] == '*') {
  394. g->map[x][y - 1 - i] = '_';
  395. res = spawn_object(g, x, y - 1 - i, &object);
  396. if (res == 1) { //Bombe
  397. cBomb++;
  398. add_array_object(&bomb, &object);
  399. clean_json_encoder(&object);
  400. } else if (res == 2) { //Bonus Malus
  401. cBonus++;
  402. add_array_object(&bonus, &object);
  403. clean_json_encoder(&object);
  404. }
  405. break;
  406. }
  407. //Si un joueur
  408. if (player_collision_index(g, x, y - 1 - i, &index)) {
  409. if (index != playerIndex) pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + index]);
  410. if (g->player[index]->major == 0) {
  411. //Baisse la vie
  412. g->player[index]->life = g->player[index]->life - (g->player[index]->maxLife * (DAMAGEBOMB - (0.1 * i)));
  413. //Notification du joueur
  414. describe_player(g->player[index], &notif);
  415. if (!notify_client(g->player[index]->cli, "POST", "attack/affect", &notif)) {
  416. adderror("Impossible de notifer le client");
  417. }
  418. clean_json_encoder(&notif);
  419. }
  420. if (index != playerIndex) pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + index]);
  421. }
  422. //Si une bombe
  423. if (g->map[x][y - 1 - i] == '1' || g->map[x][y - 1 - i] == '2' || g->map[x][y - 1 - i] == '3') {
  424. cChain++;
  425. bomb_chain(g, playerIndex, x, y - 1 - i, &chain);
  426. }
  427. //Si un mur solide
  428. if(g->map[x][y - 1 - i] == '-'){
  429. break;
  430. }
  431. } else {
  432. break;
  433. }
  434. }
  435. //Vers le bas
  436. for (int i = 0; i < 2 + g->player[playerIndex]->firePower; i++) {
  437. //Si on est dans la map
  438. if (y + 1 + i <= g->height) {
  439. //Si un mur destructible
  440. if (g->map[x][y + 1 + i] == '*') {
  441. g->map[x][y + 1 + i] = '_';
  442. res = spawn_object(g, x, y + 1 + i, &object);
  443. if (res == 1) { //Bombe
  444. cBomb++;
  445. add_array_object(&bomb, &object);
  446. clean_json_encoder(&object);
  447. } else if (res == 2) { //Bonus Malus
  448. cBonus++;
  449. add_array_object(&bonus, &object);
  450. clean_json_encoder(&object);
  451. }
  452. break;
  453. }
  454. //Si un joueur
  455. if (player_collision_index(g, x, y + 1 + i, &index)) {
  456. if (index != playerIndex) pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + index]);
  457. if (g->player[index]->major == 0) {
  458. //Baisse la vie
  459. g->player[index]->life = g->player[index]->life - (g->player[index]->maxLife * (DAMAGEBOMB - (0.1 * i)));
  460. //Notification du joueur
  461. describe_player(g->player[index], &notif);
  462. if (!notify_client(g->player[index]->cli, "POST", "attack/affect", &notif)) {
  463. adderror("Impossible de notifer le client");
  464. }
  465. clean_json_encoder(&notif);
  466. }
  467. if (index != playerIndex) pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + index]);
  468. }
  469. //Si une bombe
  470. if (g->map[x][y + 1 + i] == '1' || g->map[x][y + 1 + i] == '2' || g->map[x][y + 1 + i] == '3') {
  471. cChain++;
  472. bomb_chain(g, playerIndex, x, y + 1 + i, &chain);
  473. }
  474. //Si un mur solide
  475. if(g->map[x][y + 1 + i] == '-'){
  476. break;
  477. }
  478. } else {
  479. break;
  480. }
  481. }
  482. //Vers la gauche
  483. for (int i = 0; i < 2 + g->player[playerIndex]->firePower; i++) {
  484. //Si on est dans la map
  485. if (x - 1 - i >= 0) {
  486. //Si un mur destructible
  487. if (g->map[x - 1 - i][y] == '*') {
  488. g->map[x - 1 - i][y] = '_';
  489. res = spawn_object(g, x - 1 - i, y, &object);
  490. if (res == 1) { //Bombe
  491. cBomb++;
  492. add_array_object(&bomb, &object);
  493. clean_json_encoder(&object);
  494. } else if (res == 2) { //Bonus Malus
  495. cBonus++;
  496. add_array_object(&bonus, &object);
  497. clean_json_encoder(&object);
  498. }
  499. break;
  500. }
  501. //Si un joueur
  502. if (player_collision_index(g, x - 1 - i, y, &index)) {
  503. if (index != playerIndex) pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + index]);
  504. if (g->player[index]->major == 0) {
  505. //Baisse la vie
  506. g->player[index]->life = g->player[index]->life - (g->player[index]->maxLife * (DAMAGEBOMB - (0.1 * i)));
  507. //Notification du joueur
  508. describe_player(g->player[index], &notif);
  509. if (!notify_client(g->player[index]->cli, "POST", "attack/affect", &notif)) {
  510. adderror("Impossible de notifer le client");
  511. }
  512. clean_json_encoder(&notif);
  513. }
  514. if (index != playerIndex) pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + index]);
  515. }
  516. //Si une bombe
  517. if (g->map[x - 1 - i][y] == '1' || g->map[x - 1 - i][y] == '2' || g->map[x - 1 - i][y] == '3') {
  518. cChain++;
  519. bomb_chain(g, playerIndex, x - 1 - i, y, &chain);
  520. }
  521. //Si un mur solide
  522. if(g->map[x - 1 - i][y] == '-'){
  523. break;
  524. }
  525. } else {
  526. break;
  527. }
  528. }
  529. //Vers la droite
  530. for (int i = 0; i < 2 + g->player[playerIndex]->firePower; i++) {
  531. //Si on est dans la map
  532. if (x + 1 + i >= 0) {
  533. //Si un mur destructible
  534. if (g->map[x + 1 + i][y] == '*') {
  535. g->map[x + 1 + i][y] = '_';
  536. res = spawn_object(g, x + 1 + i, y, &object);
  537. if (res == 1) { //Bombe
  538. cBomb++;
  539. add_array_object(&bomb, &object);
  540. clean_json_encoder(&object);
  541. } else if (res == 2) { //Bonus Malus
  542. cBonus++;
  543. add_array_object(&bonus, &object);
  544. clean_json_encoder(&object);
  545. }
  546. break;
  547. }
  548. //Si un joueur
  549. if (player_collision_index(g, x + 1 + i, y, &index)) {
  550. if (index != playerIndex) pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + index]);
  551. if (g->player[index]->major == 0) {
  552. //Baisse la vie
  553. g->player[index]->life = g->player[index]->life - (g->player[index]->maxLife * (DAMAGEBOMB - (0.1 * i)));
  554. //Notification du joueur
  555. describe_player(g->player[index], &notif);
  556. if (!notify_client(g->player[index]->cli, "POST", "attack/affect", &notif)) {
  557. adderror("Impossible de notifer le client");
  558. }
  559. clean_json_encoder(&notif);
  560. }
  561. if (index != playerIndex) pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + index]);
  562. }
  563. //Si une bombe
  564. if (g->map[x + 1 + i][y] == '1' || g->map[x + 1 + i][y] == '2' || g->map[x + 1 + i][y] == '3') {
  565. cChain++;
  566. bomb_chain(g, playerIndex, x + 1 + i, y, &chain);
  567. }
  568. //Si un mur solide
  569. if(g->map[x + 1 + i][y] == '-'){
  570. break;
  571. }
  572. } else {
  573. break;
  574. }
  575. }
  576. }
  577. //Si c'est une mine
  578. else {
  579. g->map[x][y] = '_';
  580. //Pas de bonus pour une mine
  581. add_boolean(json, "bonus", false);
  582. //Recup le joueur
  583. if (player_collision_index(g, x, y, &index)) {
  584. if (index != playerIndex) pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + index]);
  585. if (g->player[index]->major == 0) {
  586. //Baisse la vie
  587. g->player[index]->life = g->player[index]->life - (g->player[index]->maxLife * DAMAGEMINE);
  588. //Notification du joueur
  589. describe_player(g->player[index], &notif);
  590. if (!notify_client(g->player[index]->cli, "POST", "attack/affect", &notif)) {
  591. adderror("Impossible de notifer le client");
  592. }
  593. clean_json_encoder(&notif);
  594. }
  595. if (index != playerIndex) pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + index]);
  596. }
  597. }
  598. //Affichage de la map
  599. map = unparse_map(g->map, g->width, g->height);
  600. add_string(json, "map", map);
  601. //Ajout des json
  602. add_array(json, "bomb", &bomb);
  603. add_array(json, "bonusMalus", &bonus);
  604. add_array(json, "chain", &chain);
  605. //Nettoyage
  606. if(playerIndex >= 0) pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + playerIndex]);
  607. pthread_mutex_unlock(&gameMutex[g->index]);
  608. free(map);
  609. return true;
  610. }
  611. void bomb_chain(Game* g, int playerIndex, int x, int y, JsonArray* chain) {
  612. JsonEncoder json, notif;
  613. int index;
  614. Player* p;
  615. obj_node* objn = NULL;
  616. //Inie json
  617. ini_encoder(&json);
  618. ini_encoder(&notif);
  619. //Determine le type de la bombe
  620. switch (g->map[x][y]) {
  621. case '1':
  622. add_string(&json, "type", "classic");
  623. p = search_player_object(g, OBJ_BCLASSIC, x, y);
  624. if(p != NULL){
  625. p->classicBomb++;
  626. p->nbBomb--;
  627. objn = object_search(p->bomb, OBJ_BCLASSIC, x, y);
  628. object_delete(p->bomb, objn);
  629. }
  630. break;
  631. case '2':
  632. add_string(&json, "type", "mine");
  633. p = search_player_object(g, OBJ_BMINE, x, y);
  634. if(p != NULL){
  635. p->mine++;
  636. p->nbBomb--;
  637. objn = object_search(p->bomb, OBJ_BMINE, x, y);
  638. object_delete(p->bomb, objn);
  639. }
  640. break;
  641. case '3':
  642. add_string(&json, "type", "remote");
  643. p = search_player_object(g, OBJ_BREMOTE, x, y);
  644. if(p != NULL){
  645. p->remoteBomb++;
  646. p->nbBomb--;
  647. objn = object_search(p->bomb, OBJ_BREMOTE, x, y);
  648. object_delete(p->bomb, objn);
  649. }
  650. break;
  651. default:
  652. return;
  653. }
  654. //Determine la position de la bombe
  655. char pos[20];
  656. memset(pos, 0, 20);
  657. snprintf(pos, 20, "%d,%d", x, y);
  658. add_string(&json, "pos", pos);
  659. //Calcul l'explosion non mine
  660. if (g->map[x][y] != '2') {
  661. g->map[x][y] = '_';
  662. //Vers le haut
  663. for (int i = 0; i < 2; i++) {
  664. //Si on est dans la map
  665. if (y - 1 - i >= 0) {
  666. //Si un mur destructible
  667. if (g->map[x][y - 1 - i] == '*') {
  668. g->map[x][y - 1 - i] = '_';
  669. break;
  670. }
  671. //Si un joueur
  672. if (player_collision_index(g, x, y - 1 - i, &index)) {
  673. if (index != playerIndex) pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + index]);
  674. if (g->player[index]->major == 0) {
  675. //Baisse la vie
  676. g->player[index]->life = g->player[index]->life - (g->player[index]->maxLife * (DAMAGEBOMB - (0.1 * i)));
  677. //Notification du joueur
  678. describe_player(g->player[index], &notif);
  679. if (!notify_client(g->player[index]->cli, "POST", "attack/affect", &notif)) {
  680. adderror("Impossible de notifer le client");
  681. }
  682. clean_json_encoder(&notif);
  683. }
  684. if (index != playerIndex) pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + index]);
  685. }
  686. //Si une bombe
  687. if (g->map[x][y - 1 - i] == '1' || g->map[x][y - 1 - i] == '2' || g->map[x][y - 1 - i] == '3') {
  688. bomb_chain(g, playerIndex, x, y - 1 - i, chain);
  689. }
  690. //Si un mur solide
  691. if(g->map[x][y - 1 - i] == '-'){
  692. break;
  693. }
  694. } else {
  695. break;
  696. }
  697. }
  698. //Vers le bas
  699. for (int i = 0; i < 2 + g->player[playerIndex]->firePower; i++) {
  700. //Si on est dans la map
  701. if (y + 1 + i <= g->height) {
  702. //Si un mur destructible
  703. if (g->map[x][y + 1 + i] == '*') {
  704. g->map[x][y + 1 + i] = '_';
  705. break;
  706. }
  707. //Si un joueur
  708. if (player_collision_index(g, x, y + 1 + i, &index)) {
  709. if (index != playerIndex) pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + index]);
  710. if (g->player[index]->major == 0) {
  711. //Baisse la vie
  712. g->player[index]->life = g->player[index]->life - (g->player[index]->maxLife * (DAMAGEBOMB - (0.1 * i)));
  713. //Notification du joueur
  714. describe_player(g->player[index], &notif);
  715. if (!notify_client(g->player[index]->cli, "POST", "attack/affect", &notif)) {
  716. adderror("Impossible de notifer le client");
  717. }
  718. clean_json_encoder(&notif);
  719. }
  720. if (index != playerIndex) pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + index]);
  721. }
  722. //Si une bombe
  723. if (g->map[x][y + 1 + i] == '1' || g->map[x][y + 1 + i] == '2' || g->map[x][y + 1 + i] == '3') {
  724. bomb_chain(g, playerIndex, x, y + 1 + i, chain);
  725. }
  726. //Si un mur solide
  727. if(g->map[x][y + 1 + i] == '-'){
  728. break;
  729. }
  730. } else {
  731. break;
  732. }
  733. }
  734. //Vers la gauche
  735. for (int i = 0; i < 2 + g->player[playerIndex]->firePower; i++) {
  736. //Si on est dans la map
  737. if (x - 1 - i >= 0) {
  738. //Si un mur destructible
  739. if (g->map[x - 1 - i][y] == '*') {
  740. g->map[x - 1 - i][y] = '_';
  741. break;
  742. }
  743. //Si un joueur
  744. if (player_collision_index(g, x - 1 - i, y, &index)) {
  745. if (index != playerIndex) pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + index]);
  746. if (g->player[index]->major == 0) {
  747. //Baisse la vie
  748. g->player[index]->life = g->player[index]->life - (g->player[index]->maxLife * (DAMAGEBOMB - (0.1 * i)));
  749. //Notification du joueur
  750. describe_player(g->player[index], &notif);
  751. if (!notify_client(g->player[index]->cli, "POST", "attack/affect", &notif)) {
  752. adderror("Impossible de notifer le client");
  753. }
  754. clean_json_encoder(&notif);
  755. }
  756. if (index != playerIndex) pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + index]);
  757. }
  758. //Si une bombe
  759. if (g->map[x - 1 - i][y] == '1' || g->map[x - 1 - i][y] == '2' || g->map[x - 1 - i][y] == '3') {
  760. bomb_chain(g, playerIndex, x - 1 - i, y, chain);
  761. }
  762. //Si un mur solide
  763. if(g->map[x - 1 - i][y] == '-'){
  764. break;
  765. }
  766. } else {
  767. break;
  768. }
  769. }
  770. //Vers la droite
  771. for (int i = 0; i < 2 + g->player[playerIndex]->firePower; i++) {
  772. //Si on est dans la map
  773. if (x + 1 + i >= 0) {
  774. //Si un mur destructible
  775. if (g->map[x + 1 + i][y] == '*') {
  776. g->map[x + 1 + i][y] = '_';
  777. break;
  778. }
  779. //Si un joueur
  780. if (player_collision_index(g, x + 1 + i, y, &index)) {
  781. if (index != playerIndex) pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + index]);
  782. if (g->player[index]->major == 0) {
  783. //Baisse la vie
  784. g->player[index]->life = g->player[index]->life - (g->player[index]->maxLife * (DAMAGEBOMB - (0.1 * i)));
  785. //Notification du joueur
  786. describe_player(g->player[index], &notif);
  787. if (!notify_client(g->player[index]->cli, "POST", "attack/affect", &notif)) {
  788. adderror("Impossible de notifer le client");
  789. }
  790. clean_json_encoder(&notif);
  791. }
  792. if (index != playerIndex) pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + index]);
  793. }
  794. //Si une bombe
  795. if (g->map[x + 1 + i][y] == '1' || g->map[x + 1 + i][y] == '2' || g->map[x + 1 + i][y] == '3') {
  796. bomb_chain(g, playerIndex, x + 1 + i, y, chain);
  797. }
  798. //Si un mur solide
  799. if(g->map[x + 1 + i][y] == '-'){
  800. break;
  801. }
  802. } else {
  803. break;
  804. }
  805. }
  806. }
  807. //Si c'est une mine
  808. else {
  809. g->map[x][y] = '_';
  810. //Recup le joueur
  811. if (player_collision_index(g, x, y, &index)) {
  812. if (index != playerIndex) pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + index]);
  813. if (g->player[index]->major == 0) {
  814. //Baisse la vie
  815. g->player[index]->life = g->player[index]->life - (g->player[index]->maxLife * DAMAGEMINE);
  816. //Notification du joueur
  817. describe_player(g->player[index], &notif);
  818. if (!notify_client(g->player[index]->cli, "POST", "attack/affect", &notif)) {
  819. adderror("Impossible de notifer le client");
  820. }
  821. clean_json_encoder(&notif);
  822. }
  823. if (index != playerIndex) pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + index]);
  824. }
  825. }
  826. //Ajoute json dans le tableau
  827. add_array_object(chain, &json);
  828. //Nettoyage
  829. clean_json_encoder(&json);
  830. }
  831. int spawn_object(Game* g, int x, int y, JsonEncoder* json) {
  832. int random, res = 0;
  833. //Generation random si spawn ou non
  834. srand(time(NULL));
  835. random = rand() % 100 + 1;
  836. if (random > SPAWNRATE) {
  837. return 0; //Pas de spawn
  838. }
  839. //Choisit l'objet
  840. random = rand() % 11;
  841. switch (random) {
  842. case OBJ_BCLASSIC:
  843. res = 1;
  844. add_string(json, "type", "classic");
  845. break;
  846. case OBJ_BMINE:
  847. res = 1;
  848. add_string(json, "type", "mine");
  849. break;
  850. case OBJ_BREMOTE:
  851. res = 1;
  852. add_string(json, "type", "remote");
  853. break;
  854. case OBJ_BOMBUP:
  855. res = 2;
  856. add_string(json, "type", "bomb_up");
  857. break;
  858. case OBJ_BOMBDOWN:
  859. res = 2;
  860. add_string(json, "type", "bomb_down");
  861. break;
  862. case OBJ_FIREPOWER:
  863. res = 2;
  864. add_string(json, "type", "fire_power");
  865. break;
  866. case OBJ_SCOOTER:
  867. res = 2;
  868. add_string(json, "type", "scooter");
  869. break;
  870. case OBJ_BROKENLEG:
  871. res = 2;
  872. add_string(json, "type", "broken_legs");
  873. break;
  874. case OBJ_LIFEUP:
  875. res = 2;
  876. add_string(json, "type", "life_up");
  877. break;
  878. case OBJ_LIFEMAX:
  879. res = 2;
  880. add_string(json, "type", "life_max");
  881. break;
  882. case OBJ_MAJOR:
  883. res = 2;
  884. add_string(json, "type", "major");
  885. break;
  886. }
  887. //Ajoute l'objet dans la partie
  888. object_add(g->object, random, x, y);
  889. //Ajoute postion au json
  890. char pos[20];
  891. memset(pos, 0, 20);
  892. snprintf(pos, 20, "%d,%d", x, y);
  893. add_string(json, "pos", pos);
  894. //Retourne type d'ajout
  895. return res;
  896. }
  897. void remove_player(Game* g, int playerIndex) {
  898. pthread_mutex_lock(&gameMutex[g->index]);
  899. pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + playerIndex]);
  900. g->nbPlayer--;
  901. delete_player(g->player[playerIndex]);
  902. pthread_mutex_unlock(&gameMutex[g->index]);
  903. pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + playerIndex]);
  904. }
  905. void stop_game(Game* g) {
  906. pthread_mutex_lock(&gameMutex[g->index]);
  907. //Indique comme inactive
  908. g->active = false;
  909. //Suppr les joueurs
  910. for (int i = 0; i < MAXPLAYER; i++) {
  911. //pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + i]);
  912. if (g->player[i]->ini) {
  913. delete_player(g->player[i]);
  914. }
  915. free(g->player[i]);
  916. //pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + i]);
  917. }
  918. //Libere la memoire
  919. object_clean(g->object);
  920. free(g->object);
  921. for (int i = 0; i < g->width; i++) {
  922. free(g->map[i]);
  923. }
  924. free(g->map);
  925. free(g->mapName);
  926. free(g->mapContent);
  927. pthread_mutex_unlock(&gameMutex[g->index]);
  928. //Retire une game
  929. nbGame--;
  930. }
  931. void clean_games() {
  932. for (int i = 0; i < MAXGAME; i++) {
  933. if (game[i].active) {
  934. pthread_mutex_unlock(&gameMutex[i]);
  935. stop_game(&game[i]);
  936. }
  937. }
  938. }