game.c 31 KB

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