game.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  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. //Inie json
  343. ini_encoder(&object);
  344. ini_encoder(&notif);
  345. ini_array_encoder(&bomb);
  346. ini_array_encoder(&bonus);
  347. ini_array_encoder(&chain);
  348. //Mutex
  349. pthread_mutex_lock(&gameMutex[g->index]);
  350. if (playerIndex >= 0) pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + playerIndex]);
  351. //Determine le type de la bombe
  352. switch (g->map[x][y]) {
  353. case '1':
  354. add_string(json, "type", "classic");
  355. g->player[playerIndex]->classicBomb++;
  356. g->player[playerIndex]->nbBomb--;
  357. break;
  358. case '2':
  359. add_string(json, "type", "mine");
  360. g->player[playerIndex]->mine++;
  361. g->player[playerIndex]->nbBomb--;
  362. break;
  363. case '3':
  364. add_string(json, "type", "remote");
  365. g->player[playerIndex]->remoteBomb++;
  366. g->player[playerIndex]->nbBomb--;
  367. break;
  368. default:
  369. if (playerIndex >= 0) pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + playerIndex]);
  370. pthread_mutex_unlock(&gameMutex[g->index]);
  371. return false;
  372. }
  373. //Determine la position de la bombe
  374. char pos[20];
  375. memset(pos, 0, 20);
  376. snprintf(pos, 20, "%d,%d", x, y);
  377. add_string(json, "pos", pos);
  378. //Calcul l'explosion non mine
  379. if (g->map[x][y] != '2') {
  380. g->map[x][y] = '_';
  381. //Si le joueur à un bonus
  382. if (g->player[playerIndex]->firePower > 0) {
  383. add_boolean(json, "bonus", true);
  384. } else {
  385. add_boolean(json, "bonus", false);
  386. }
  387. //Vers le haut
  388. for (int i = 0; i < 2 + g->player[playerIndex]->firePower; i++) {
  389. //Si on est dans la map
  390. if (y - 1 - i >= 0) {
  391. //Si un mur destructible
  392. if (g->map[x][y - 1 - i] == '*') {
  393. g->map[x][y - 1 - i] = '_';
  394. res = spawn_object(g, x, y - 1 - i, &object);
  395. if (res == 1) { //Bombe
  396. cBomb++;
  397. add_array_object(&bomb, &object);
  398. clean_json_encoder(&object);
  399. } else if (res == 2) { //Bonus Malus
  400. cBonus++;
  401. add_array_object(&bonus, &object);
  402. clean_json_encoder(&object);
  403. }
  404. break;
  405. }
  406. //Si un joueur
  407. if (player_collision_index(g, x, y - 1 - i, &index)) {
  408. if (index != playerIndex) pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + index]);
  409. if (g->player[index]->major == 0) {
  410. //Baisse la vie
  411. g->player[index]->life = g->player[index]->life - (g->player[index]->maxLife * (DAMAGEBOMB - (0.1 * i)));
  412. //Notification du joueur
  413. describe_player(g->player[index], &notif);
  414. if (!notify_client(g->player[index]->cli, "POST", "attack/affect", &notif)) {
  415. adderror("Impossible de notifer le client");
  416. }
  417. clean_json_encoder(&notif);
  418. }
  419. if (index != playerIndex) pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + index]);
  420. }
  421. //Si une bombe
  422. if (g->map[x][y - 1 - i] == '1' || g->map[x][y - 1 - i] == '2' || g->map[x][y - 1 - i] == '3') {
  423. cChain++;
  424. bomb_chain(g, playerIndex, x, y - 1 - i, &chain);
  425. }
  426. //Si un mur solide
  427. if (g->map[x][y - 1 - i] == '-') {
  428. break;
  429. }
  430. } else {
  431. break;
  432. }
  433. }
  434. //Vers le bas
  435. for (int i = 0; i < 2 + g->player[playerIndex]->firePower; i++) {
  436. //Si on est dans la map
  437. if (y + 1 + i <= g->height) {
  438. //Si un mur destructible
  439. if (g->map[x][y + 1 + i] == '*') {
  440. g->map[x][y + 1 + i] = '_';
  441. res = spawn_object(g, x, y + 1 + i, &object);
  442. if (res == 1) { //Bombe
  443. cBomb++;
  444. add_array_object(&bomb, &object);
  445. clean_json_encoder(&object);
  446. } else if (res == 2) { //Bonus Malus
  447. cBonus++;
  448. add_array_object(&bonus, &object);
  449. clean_json_encoder(&object);
  450. }
  451. break;
  452. }
  453. //Si un joueur
  454. if (player_collision_index(g, x, y + 1 + i, &index)) {
  455. if (index != playerIndex) pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + index]);
  456. if (g->player[index]->major == 0) {
  457. //Baisse la vie
  458. g->player[index]->life = g->player[index]->life - (g->player[index]->maxLife * (DAMAGEBOMB - (0.1 * i)));
  459. //Notification du joueur
  460. describe_player(g->player[index], &notif);
  461. if (!notify_client(g->player[index]->cli, "POST", "attack/affect", &notif)) {
  462. adderror("Impossible de notifer le client");
  463. }
  464. clean_json_encoder(&notif);
  465. }
  466. if (index != playerIndex) pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + index]);
  467. }
  468. //Si une bombe
  469. if (g->map[x][y + 1 + i] == '1' || g->map[x][y + 1 + i] == '2' || g->map[x][y + 1 + i] == '3') {
  470. cChain++;
  471. bomb_chain(g, playerIndex, x, y + 1 + i, &chain);
  472. }
  473. //Si un mur solide
  474. if (g->map[x][y + 1 + i] == '-') {
  475. break;
  476. }
  477. } else {
  478. break;
  479. }
  480. }
  481. //Vers la gauche
  482. for (int i = 0; i < 2 + g->player[playerIndex]->firePower; i++) {
  483. //Si on est dans la map
  484. if (x - 1 - i >= 0) {
  485. //Si un mur destructible
  486. if (g->map[x - 1 - i][y] == '*') {
  487. g->map[x - 1 - i][y] = '_';
  488. res = spawn_object(g, x - 1 - i, y, &object);
  489. if (res == 1) { //Bombe
  490. cBomb++;
  491. add_array_object(&bomb, &object);
  492. clean_json_encoder(&object);
  493. } else if (res == 2) { //Bonus Malus
  494. cBonus++;
  495. add_array_object(&bonus, &object);
  496. clean_json_encoder(&object);
  497. }
  498. break;
  499. }
  500. //Si un joueur
  501. if (player_collision_index(g, x - 1 - i, y, &index)) {
  502. if (index != playerIndex) pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + index]);
  503. if (g->player[index]->major == 0) {
  504. //Baisse la vie
  505. g->player[index]->life = g->player[index]->life - (g->player[index]->maxLife * (DAMAGEBOMB - (0.1 * i)));
  506. //Notification du joueur
  507. describe_player(g->player[index], &notif);
  508. if (!notify_client(g->player[index]->cli, "POST", "attack/affect", &notif)) {
  509. adderror("Impossible de notifer le client");
  510. }
  511. clean_json_encoder(&notif);
  512. }
  513. if (index != playerIndex) pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + index]);
  514. }
  515. //Si une bombe
  516. if (g->map[x - 1 - i][y] == '1' || g->map[x - 1 - i][y] == '2' || g->map[x - 1 - i][y] == '3') {
  517. cChain++;
  518. bomb_chain(g, playerIndex, x - 1 - i, y, &chain);
  519. }
  520. //Si un mur solide
  521. if (g->map[x - 1 - i][y] == '-') {
  522. break;
  523. }
  524. } else {
  525. break;
  526. }
  527. }
  528. //Vers la droite
  529. for (int i = 0; i < 2 + g->player[playerIndex]->firePower; i++) {
  530. //Si on est dans la map
  531. if (x + 1 + i >= 0) {
  532. //Si un mur destructible
  533. if (g->map[x + 1 + i][y] == '*') {
  534. g->map[x + 1 + i][y] = '_';
  535. res = spawn_object(g, x + 1 + i, y, &object);
  536. if (res == 1) { //Bombe
  537. cBomb++;
  538. add_array_object(&bomb, &object);
  539. clean_json_encoder(&object);
  540. } else if (res == 2) { //Bonus Malus
  541. cBonus++;
  542. add_array_object(&bonus, &object);
  543. clean_json_encoder(&object);
  544. }
  545. break;
  546. }
  547. //Si un joueur
  548. if (player_collision_index(g, x + 1 + i, y, &index)) {
  549. if (index != playerIndex) pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + index]);
  550. if (g->player[index]->major == 0) {
  551. //Baisse la vie
  552. g->player[index]->life = g->player[index]->life - (g->player[index]->maxLife * (DAMAGEBOMB - (0.1 * i)));
  553. //Notification du joueur
  554. describe_player(g->player[index], &notif);
  555. if (!notify_client(g->player[index]->cli, "POST", "attack/affect", &notif)) {
  556. adderror("Impossible de notifer le client");
  557. }
  558. clean_json_encoder(&notif);
  559. }
  560. if (index != playerIndex) pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + index]);
  561. }
  562. //Si une bombe
  563. if (g->map[x + 1 + i][y] == '1' || g->map[x + 1 + i][y] == '2' || g->map[x + 1 + i][y] == '3') {
  564. cChain++;
  565. bomb_chain(g, playerIndex, x + 1 + i, y, &chain);
  566. }
  567. //Si un mur solide
  568. if (g->map[x + 1 + i][y] == '-') {
  569. break;
  570. }
  571. } else {
  572. break;
  573. }
  574. }
  575. }
  576. //Si c'est une mine
  577. else {
  578. g->map[x][y] = '_';
  579. //Calcul dommage
  580. if (g->player[playerIndex]->major == 0) {
  581. //Baisse la vie
  582. g->player[playerIndex]->life = g->player[playerIndex]->life - (g->player[playerIndex]->maxLife * DAMAGEMINE);
  583. //Notification du joueur
  584. describe_player(g->player[playerIndex], &notif);
  585. if (!notify_client(g->player[playerIndex]->cli, "POST", "attack/affect", &notif)) {
  586. adderror("Impossible de notifer le client");
  587. }
  588. clean_json_encoder(&notif);
  589. }
  590. //Bas de bonus si mine
  591. add_boolean(json, "bonus", false);
  592. }
  593. //Affichage de la map
  594. free(g->mapContent);
  595. g->mapContent = unparse_map(g->map, g->width, g->height);
  596. add_string(json, "map", g->mapContent);
  597. //Ajout des json
  598. add_array(json, "bomb", &bomb);
  599. add_array(json, "bonusMalus", &bonus);
  600. add_array(json, "chain", &chain);
  601. //Nettoyage
  602. if (playerIndex >= 0) pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + playerIndex]);
  603. pthread_mutex_unlock(&gameMutex[g->index]);
  604. clean_json_array(&bomb);
  605. clean_json_array(&bonus);
  606. clean_json_array(&chain);
  607. return true;
  608. }
  609. void bomb_chain(Game* g, int playerIndex, int x, int y, JsonArray* chain) {
  610. JsonEncoder json, notif;
  611. int index;
  612. Player* p;
  613. obj_node* objn = NULL;
  614. //Inie json
  615. ini_encoder(&json);
  616. ini_encoder(&notif);
  617. //Determine le type de la bombe
  618. switch (g->map[x][y]) {
  619. case '1':
  620. add_string(&json, "type", "classic");
  621. p = search_player_object(g, OBJ_BCLASSIC, x, y);
  622. if (p != NULL) {
  623. p->classicBomb++;
  624. p->nbBomb--;
  625. objn = object_search(p->bomb, OBJ_BCLASSIC, x, y);
  626. object_delete(p->bomb, objn);
  627. }
  628. break;
  629. case '2':
  630. add_string(&json, "type", "mine");
  631. p = search_player_object(g, OBJ_BMINE, x, y);
  632. if (p != NULL) {
  633. p->mine++;
  634. p->nbBomb--;
  635. objn = object_search(p->bomb, OBJ_BMINE, x, y);
  636. object_delete(p->bomb, objn);
  637. }
  638. break;
  639. case '3':
  640. add_string(&json, "type", "remote");
  641. p = search_player_object(g, OBJ_BREMOTE, x, y);
  642. if (p != NULL) {
  643. p->remoteBomb++;
  644. p->nbBomb--;
  645. objn = object_search(p->bomb, OBJ_BREMOTE, x, y);
  646. object_delete(p->bomb, objn);
  647. }
  648. break;
  649. default:
  650. return;
  651. }
  652. //Determine la position de la bombe
  653. char pos[20];
  654. memset(pos, 0, 20);
  655. snprintf(pos, 20, "%d,%d", x, y);
  656. add_string(&json, "pos", pos);
  657. //Calcul l'explosion non mine
  658. if (g->map[x][y] != '2') {
  659. g->map[x][y] = '_';
  660. //Vers le haut
  661. for (int i = 0; i < 2; i++) {
  662. //Si on est dans la map
  663. if (y - 1 - i >= 0) {
  664. //Si un mur destructible
  665. if (g->map[x][y - 1 - i] == '*') {
  666. g->map[x][y - 1 - i] = '_';
  667. break;
  668. }
  669. //Si un joueur
  670. if (player_collision_index(g, x, y - 1 - i, &index)) {
  671. if (index != playerIndex) pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + index]);
  672. if (g->player[index]->major == 0) {
  673. //Baisse la vie
  674. g->player[index]->life = g->player[index]->life - (g->player[index]->maxLife * (DAMAGEBOMB - (0.1 * i)));
  675. //Notification du joueur
  676. describe_player(g->player[index], &notif);
  677. if (!notify_client(g->player[index]->cli, "POST", "attack/affect", &notif)) {
  678. adderror("Impossible de notifer le client");
  679. }
  680. clean_json_encoder(&notif);
  681. }
  682. if (index != playerIndex) pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + index]);
  683. }
  684. //Si une bombe
  685. if (g->map[x][y - 1 - i] == '1' || g->map[x][y - 1 - i] == '2' || g->map[x][y - 1 - i] == '3') {
  686. bomb_chain(g, playerIndex, x, y - 1 - i, chain);
  687. }
  688. //Si un mur solide
  689. if (g->map[x][y - 1 - i] == '-') {
  690. break;
  691. }
  692. } else {
  693. break;
  694. }
  695. }
  696. //Vers le bas
  697. for (int i = 0; i < 2 + g->player[playerIndex]->firePower; i++) {
  698. //Si on est dans la map
  699. if (y + 1 + i <= g->height) {
  700. //Si un mur destructible
  701. if (g->map[x][y + 1 + i] == '*') {
  702. g->map[x][y + 1 + i] = '_';
  703. break;
  704. }
  705. //Si un joueur
  706. if (player_collision_index(g, x, y + 1 + i, &index)) {
  707. if (index != playerIndex) pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + index]);
  708. if (g->player[index]->major == 0) {
  709. //Baisse la vie
  710. g->player[index]->life = g->player[index]->life - (g->player[index]->maxLife * (DAMAGEBOMB - (0.1 * i)));
  711. //Notification du joueur
  712. describe_player(g->player[index], &notif);
  713. if (!notify_client(g->player[index]->cli, "POST", "attack/affect", &notif)) {
  714. adderror("Impossible de notifer le client");
  715. }
  716. clean_json_encoder(&notif);
  717. }
  718. if (index != playerIndex) pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + index]);
  719. }
  720. //Si une bombe
  721. if (g->map[x][y + 1 + i] == '1' || g->map[x][y + 1 + i] == '2' || g->map[x][y + 1 + i] == '3') {
  722. bomb_chain(g, playerIndex, x, y + 1 + i, chain);
  723. }
  724. //Si un mur solide
  725. if (g->map[x][y + 1 + i] == '-') {
  726. break;
  727. }
  728. } else {
  729. break;
  730. }
  731. }
  732. //Vers la gauche
  733. for (int i = 0; i < 2 + g->player[playerIndex]->firePower; i++) {
  734. //Si on est dans la map
  735. if (x - 1 - i >= 0) {
  736. //Si un mur destructible
  737. if (g->map[x - 1 - i][y] == '*') {
  738. g->map[x - 1 - i][y] = '_';
  739. break;
  740. }
  741. //Si un joueur
  742. if (player_collision_index(g, x - 1 - i, y, &index)) {
  743. if (index != playerIndex) pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + index]);
  744. if (g->player[index]->major == 0) {
  745. //Baisse la vie
  746. g->player[index]->life = g->player[index]->life - (g->player[index]->maxLife * (DAMAGEBOMB - (0.1 * i)));
  747. //Notification du joueur
  748. describe_player(g->player[index], &notif);
  749. if (!notify_client(g->player[index]->cli, "POST", "attack/affect", &notif)) {
  750. adderror("Impossible de notifer le client");
  751. }
  752. clean_json_encoder(&notif);
  753. }
  754. if (index != playerIndex) pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + index]);
  755. }
  756. //Si une bombe
  757. if (g->map[x - 1 - i][y] == '1' || g->map[x - 1 - i][y] == '2' || g->map[x - 1 - i][y] == '3') {
  758. bomb_chain(g, playerIndex, x - 1 - i, y, chain);
  759. }
  760. //Si un mur solide
  761. if (g->map[x - 1 - i][y] == '-') {
  762. break;
  763. }
  764. } else {
  765. break;
  766. }
  767. }
  768. //Vers la droite
  769. for (int i = 0; i < 2 + g->player[playerIndex]->firePower; i++) {
  770. //Si on est dans la map
  771. if (x + 1 + i >= 0) {
  772. //Si un mur destructible
  773. if (g->map[x + 1 + i][y] == '*') {
  774. g->map[x + 1 + i][y] = '_';
  775. break;
  776. }
  777. //Si un joueur
  778. if (player_collision_index(g, x + 1 + i, y, &index)) {
  779. if (index != playerIndex) pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + index]);
  780. if (g->player[index]->major == 0) {
  781. //Baisse la vie
  782. g->player[index]->life = g->player[index]->life - (g->player[index]->maxLife * (DAMAGEBOMB - (0.1 * i)));
  783. //Notification du joueur
  784. describe_player(g->player[index], &notif);
  785. if (!notify_client(g->player[index]->cli, "POST", "attack/affect", &notif)) {
  786. adderror("Impossible de notifer le client");
  787. }
  788. clean_json_encoder(&notif);
  789. }
  790. if (index != playerIndex) pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + index]);
  791. }
  792. //Si une bombe
  793. if (g->map[x + 1 + i][y] == '1' || g->map[x + 1 + i][y] == '2' || g->map[x + 1 + i][y] == '3') {
  794. bomb_chain(g, playerIndex, x + 1 + i, y, chain);
  795. }
  796. //Si un mur solide
  797. if (g->map[x + 1 + i][y] == '-') {
  798. break;
  799. }
  800. } else {
  801. break;
  802. }
  803. }
  804. }//Si c'est une mine
  805. else {
  806. g->map[x][y] = '_';
  807. //Recup le joueur
  808. if (player_collision_index(g, x, y, &index)) {
  809. if (index != playerIndex) pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + index]);
  810. if (g->player[index]->major == 0) {
  811. //Baisse la vie
  812. g->player[index]->life = g->player[index]->life - (g->player[index]->maxLife * DAMAGEMINE);
  813. //Notification du joueur
  814. describe_player(g->player[index], &notif);
  815. if (!notify_client(g->player[index]->cli, "POST", "attack/affect", &notif)) {
  816. adderror("Impossible de notifer le client");
  817. }
  818. clean_json_encoder(&notif);
  819. }
  820. if (index != playerIndex) pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + index]);
  821. }
  822. }
  823. //Ajoute json dans le tableau
  824. add_array_object(chain, &json);
  825. //Nettoyage
  826. clean_json_encoder(&json);
  827. }
  828. int spawn_object(Game* g, int x, int y, JsonEncoder* json) {
  829. int random, res = 0;
  830. //Generation random si spawn ou non
  831. srand(time(NULL));
  832. random = rand() % 100 + 1;
  833. if (random > SPAWNRATE) {
  834. return 0; //Pas de spawn
  835. }
  836. //Choisit l'objet
  837. random = rand() % 11;
  838. switch (random) {
  839. case OBJ_BCLASSIC:
  840. res = 1;
  841. add_string(json, "type", "classic");
  842. break;
  843. case OBJ_BMINE:
  844. res = 1;
  845. add_string(json, "type", "mine");
  846. break;
  847. case OBJ_BREMOTE:
  848. res = 1;
  849. add_string(json, "type", "remote");
  850. break;
  851. case OBJ_BOMBUP:
  852. res = 2;
  853. add_string(json, "type", "bomb_up");
  854. break;
  855. case OBJ_BOMBDOWN:
  856. res = 2;
  857. add_string(json, "type", "bomb_down");
  858. break;
  859. case OBJ_FIREPOWER:
  860. res = 2;
  861. add_string(json, "type", "fire_power");
  862. break;
  863. case OBJ_SCOOTER:
  864. res = 2;
  865. add_string(json, "type", "scooter");
  866. break;
  867. case OBJ_BROKENLEG:
  868. res = 2;
  869. add_string(json, "type", "broken_legs");
  870. break;
  871. case OBJ_LIFEUP:
  872. res = 2;
  873. add_string(json, "type", "life_up");
  874. break;
  875. case OBJ_LIFEMAX:
  876. res = 2;
  877. add_string(json, "type", "life_max");
  878. break;
  879. case OBJ_MAJOR:
  880. res = 2;
  881. add_string(json, "type", "major");
  882. break;
  883. }
  884. //Ajoute l'objet dans la partie
  885. object_add(g->object, random, x, y);
  886. //Ajoute postion au json
  887. char pos[20];
  888. memset(pos, 0, 20);
  889. snprintf(pos, 20, "%d,%d", x, y);
  890. add_string(json, "pos", pos);
  891. //Retourne type d'ajout
  892. return res;
  893. }
  894. void remove_player(Game* g, int playerIndex) {
  895. pthread_mutex_lock(&gameMutex[g->index]);
  896. pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + playerIndex]);
  897. g->nbPlayer--;
  898. delete_player(g->player[playerIndex]);
  899. pthread_mutex_unlock(&gameMutex[g->index]);
  900. pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + playerIndex]);
  901. }
  902. void stop_game(Game* g) {
  903. pthread_mutex_lock(&gameMutex[g->index]);
  904. //Indique comme inactive
  905. g->active = false;
  906. //Suppr les joueurs
  907. for (int i = 0; i < MAXPLAYER; i++) {
  908. //pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + i]);
  909. if (g->player[i]->ini) {
  910. delete_player(g->player[i]);
  911. }
  912. free(g->player[i]);
  913. //pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + i]);
  914. }
  915. //Libere la memoire
  916. object_clean(g->object);
  917. free(g->object);
  918. for (int i = 0; i < g->width; i++) {
  919. free(g->map[i]);
  920. }
  921. free(g->map);
  922. free(g->mapName);
  923. free(g->mapContent);
  924. pthread_mutex_unlock(&gameMutex[g->index]);
  925. //Retire une game
  926. nbGame--;
  927. }
  928. void clean_games() {
  929. for (int i = 0; i < MAXGAME; i++) {
  930. if (game[i].active) {
  931. pthread_mutex_unlock(&gameMutex[i]);
  932. stop_game(&game[i]);
  933. }
  934. }
  935. }