game.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  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. //Pas de bonus pour une mine
  580. add_boolean(json, "bonus", false);
  581. //Recup le joueur
  582. if (player_collision_index(g, x, y, &index)) {
  583. if (index != playerIndex) pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + index]);
  584. if (g->player[index]->major == 0) {
  585. //Baisse la vie
  586. g->player[index]->life = g->player[index]->life - (g->player[index]->maxLife * DAMAGEMINE);
  587. //Notification du joueur
  588. describe_player(g->player[index], &notif);
  589. if (!notify_client(g->player[index]->cli, "POST", "attack/affect", &notif)) {
  590. adderror("Impossible de notifer le client");
  591. }
  592. clean_json_encoder(&notif);
  593. }
  594. if (index != playerIndex) pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + index]);
  595. }
  596. //Nettoyage
  597. if (playerIndex >= 0) pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + playerIndex]);
  598. pthread_mutex_unlock(&gameMutex[g->index]);
  599. clean_json_array(&bomb);
  600. clean_json_array(&bonus);
  601. clean_json_array(&chain);
  602. return true;
  603. }
  604. //Affichage de la map
  605. free(g->mapContent);
  606. g->mapContent = unparse_map(g->map, g->width, g->height);
  607. add_string(json, "map", g->mapContent);
  608. //Ajout des json
  609. add_array(json, "bomb", &bomb);
  610. add_array(json, "bonusMalus", &bonus);
  611. add_array(json, "chain", &chain);
  612. //Nettoyage
  613. if (playerIndex >= 0) pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + playerIndex]);
  614. pthread_mutex_unlock(&gameMutex[g->index]);
  615. clean_json_array(&bomb);
  616. clean_json_array(&bonus);
  617. clean_json_array(&chain);
  618. return true;
  619. }
  620. void bomb_chain(Game* g, int playerIndex, int x, int y, JsonArray* chain) {
  621. JsonEncoder json, notif;
  622. int index;
  623. Player* p;
  624. obj_node* objn = NULL;
  625. //Inie json
  626. ini_encoder(&json);
  627. ini_encoder(&notif);
  628. //Determine le type de la bombe
  629. switch (g->map[x][y]) {
  630. case '1':
  631. add_string(&json, "type", "classic");
  632. p = search_player_object(g, OBJ_BCLASSIC, x, y);
  633. if (p != NULL) {
  634. p->classicBomb++;
  635. p->nbBomb--;
  636. objn = object_search(p->bomb, OBJ_BCLASSIC, x, y);
  637. object_delete(p->bomb, objn);
  638. }
  639. break;
  640. case '2':
  641. add_string(&json, "type", "mine");
  642. p = search_player_object(g, OBJ_BMINE, x, y);
  643. if (p != NULL) {
  644. p->mine++;
  645. p->nbBomb--;
  646. objn = object_search(p->bomb, OBJ_BMINE, x, y);
  647. object_delete(p->bomb, objn);
  648. }
  649. break;
  650. case '3':
  651. add_string(&json, "type", "remote");
  652. p = search_player_object(g, OBJ_BREMOTE, x, y);
  653. if (p != NULL) {
  654. p->remoteBomb++;
  655. p->nbBomb--;
  656. objn = object_search(p->bomb, OBJ_BREMOTE, x, y);
  657. object_delete(p->bomb, objn);
  658. }
  659. break;
  660. default:
  661. return;
  662. }
  663. //Determine la position de la bombe
  664. char pos[20];
  665. memset(pos, 0, 20);
  666. snprintf(pos, 20, "%d,%d", x, y);
  667. add_string(&json, "pos", pos);
  668. //Calcul l'explosion non mine
  669. if (g->map[x][y] != '2') {
  670. g->map[x][y] = '_';
  671. //Vers le haut
  672. for (int i = 0; i < 2; i++) {
  673. //Si on est dans la map
  674. if (y - 1 - i >= 0) {
  675. //Si un mur destructible
  676. if (g->map[x][y - 1 - i] == '*') {
  677. g->map[x][y - 1 - i] = '_';
  678. break;
  679. }
  680. //Si un joueur
  681. if (player_collision_index(g, x, y - 1 - i, &index)) {
  682. if (index != playerIndex) pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + index]);
  683. if (g->player[index]->major == 0) {
  684. //Baisse la vie
  685. g->player[index]->life = g->player[index]->life - (g->player[index]->maxLife * (DAMAGEBOMB - (0.1 * i)));
  686. //Notification du joueur
  687. describe_player(g->player[index], &notif);
  688. if (!notify_client(g->player[index]->cli, "POST", "attack/affect", &notif)) {
  689. adderror("Impossible de notifer le client");
  690. }
  691. clean_json_encoder(&notif);
  692. }
  693. if (index != playerIndex) pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + index]);
  694. }
  695. //Si une bombe
  696. if (g->map[x][y - 1 - i] == '1' || g->map[x][y - 1 - i] == '2' || g->map[x][y - 1 - i] == '3') {
  697. bomb_chain(g, playerIndex, x, y - 1 - i, chain);
  698. }
  699. //Si un mur solide
  700. if (g->map[x][y - 1 - i] == '-') {
  701. break;
  702. }
  703. } else {
  704. break;
  705. }
  706. }
  707. //Vers le bas
  708. for (int i = 0; i < 2 + g->player[playerIndex]->firePower; i++) {
  709. //Si on est dans la map
  710. if (y + 1 + i <= g->height) {
  711. //Si un mur destructible
  712. if (g->map[x][y + 1 + i] == '*') {
  713. g->map[x][y + 1 + i] = '_';
  714. break;
  715. }
  716. //Si un joueur
  717. if (player_collision_index(g, x, y + 1 + i, &index)) {
  718. if (index != playerIndex) pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + index]);
  719. if (g->player[index]->major == 0) {
  720. //Baisse la vie
  721. g->player[index]->life = g->player[index]->life - (g->player[index]->maxLife * (DAMAGEBOMB - (0.1 * i)));
  722. //Notification du joueur
  723. describe_player(g->player[index], &notif);
  724. if (!notify_client(g->player[index]->cli, "POST", "attack/affect", &notif)) {
  725. adderror("Impossible de notifer le client");
  726. }
  727. clean_json_encoder(&notif);
  728. }
  729. if (index != playerIndex) pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + index]);
  730. }
  731. //Si une bombe
  732. if (g->map[x][y + 1 + i] == '1' || g->map[x][y + 1 + i] == '2' || g->map[x][y + 1 + i] == '3') {
  733. bomb_chain(g, playerIndex, x, y + 1 + i, chain);
  734. }
  735. //Si un mur solide
  736. if (g->map[x][y + 1 + i] == '-') {
  737. break;
  738. }
  739. } else {
  740. break;
  741. }
  742. }
  743. //Vers la gauche
  744. for (int i = 0; i < 2 + g->player[playerIndex]->firePower; i++) {
  745. //Si on est dans la map
  746. if (x - 1 - i >= 0) {
  747. //Si un mur destructible
  748. if (g->map[x - 1 - i][y] == '*') {
  749. g->map[x - 1 - i][y] = '_';
  750. break;
  751. }
  752. //Si un joueur
  753. if (player_collision_index(g, x - 1 - i, y, &index)) {
  754. if (index != playerIndex) pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + index]);
  755. if (g->player[index]->major == 0) {
  756. //Baisse la vie
  757. g->player[index]->life = g->player[index]->life - (g->player[index]->maxLife * (DAMAGEBOMB - (0.1 * i)));
  758. //Notification du joueur
  759. describe_player(g->player[index], &notif);
  760. if (!notify_client(g->player[index]->cli, "POST", "attack/affect", &notif)) {
  761. adderror("Impossible de notifer le client");
  762. }
  763. clean_json_encoder(&notif);
  764. }
  765. if (index != playerIndex) pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + index]);
  766. }
  767. //Si une bombe
  768. if (g->map[x - 1 - i][y] == '1' || g->map[x - 1 - i][y] == '2' || g->map[x - 1 - i][y] == '3') {
  769. bomb_chain(g, playerIndex, x - 1 - i, y, chain);
  770. }
  771. //Si un mur solide
  772. if (g->map[x - 1 - i][y] == '-') {
  773. break;
  774. }
  775. } else {
  776. break;
  777. }
  778. }
  779. //Vers la droite
  780. for (int i = 0; i < 2 + g->player[playerIndex]->firePower; i++) {
  781. //Si on est dans la map
  782. if (x + 1 + i >= 0) {
  783. //Si un mur destructible
  784. if (g->map[x + 1 + i][y] == '*') {
  785. g->map[x + 1 + i][y] = '_';
  786. break;
  787. }
  788. //Si un joueur
  789. if (player_collision_index(g, x + 1 + i, y, &index)) {
  790. if (index != playerIndex) pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + index]);
  791. if (g->player[index]->major == 0) {
  792. //Baisse la vie
  793. g->player[index]->life = g->player[index]->life - (g->player[index]->maxLife * (DAMAGEBOMB - (0.1 * i)));
  794. //Notification du joueur
  795. describe_player(g->player[index], &notif);
  796. if (!notify_client(g->player[index]->cli, "POST", "attack/affect", &notif)) {
  797. adderror("Impossible de notifer le client");
  798. }
  799. clean_json_encoder(&notif);
  800. }
  801. if (index != playerIndex) pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + index]);
  802. }
  803. //Si une bombe
  804. if (g->map[x + 1 + i][y] == '1' || g->map[x + 1 + i][y] == '2' || g->map[x + 1 + i][y] == '3') {
  805. bomb_chain(g, playerIndex, x + 1 + i, y, chain);
  806. }
  807. //Si un mur solide
  808. if (g->map[x + 1 + i][y] == '-') {
  809. break;
  810. }
  811. } else {
  812. break;
  813. }
  814. }
  815. } //Si c'est une mine
  816. else {
  817. g->map[x][y] = '_';
  818. //Recup le joueur
  819. if (player_collision_index(g, x, y, &index)) {
  820. if (index != playerIndex) pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + index]);
  821. if (g->player[index]->major == 0) {
  822. //Baisse la vie
  823. g->player[index]->life = g->player[index]->life - (g->player[index]->maxLife * DAMAGEMINE);
  824. //Notification du joueur
  825. describe_player(g->player[index], &notif);
  826. if (!notify_client(g->player[index]->cli, "POST", "attack/affect", &notif)) {
  827. adderror("Impossible de notifer le client");
  828. }
  829. clean_json_encoder(&notif);
  830. }
  831. if (index != playerIndex) pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + index]);
  832. }
  833. }
  834. //Ajoute json dans le tableau
  835. add_array_object(chain, &json);
  836. //Nettoyage
  837. clean_json_encoder(&json);
  838. }
  839. int spawn_object(Game* g, int x, int y, JsonEncoder* json) {
  840. int random, res = 0;
  841. //Generation random si spawn ou non
  842. srand(time(NULL));
  843. random = rand() % 100 + 1;
  844. if (random > SPAWNRATE) {
  845. return 0; //Pas de spawn
  846. }
  847. //Choisit l'objet
  848. random = rand() % 11;
  849. switch (random) {
  850. case OBJ_BCLASSIC:
  851. res = 1;
  852. add_string(json, "type", "classic");
  853. break;
  854. case OBJ_BMINE:
  855. res = 1;
  856. add_string(json, "type", "mine");
  857. break;
  858. case OBJ_BREMOTE:
  859. res = 1;
  860. add_string(json, "type", "remote");
  861. break;
  862. case OBJ_BOMBUP:
  863. res = 2;
  864. add_string(json, "type", "bomb_up");
  865. break;
  866. case OBJ_BOMBDOWN:
  867. res = 2;
  868. add_string(json, "type", "bomb_down");
  869. break;
  870. case OBJ_FIREPOWER:
  871. res = 2;
  872. add_string(json, "type", "fire_power");
  873. break;
  874. case OBJ_SCOOTER:
  875. res = 2;
  876. add_string(json, "type", "scooter");
  877. break;
  878. case OBJ_BROKENLEG:
  879. res = 2;
  880. add_string(json, "type", "broken_legs");
  881. break;
  882. case OBJ_LIFEUP:
  883. res = 2;
  884. add_string(json, "type", "life_up");
  885. break;
  886. case OBJ_LIFEMAX:
  887. res = 2;
  888. add_string(json, "type", "life_max");
  889. break;
  890. case OBJ_MAJOR:
  891. res = 2;
  892. add_string(json, "type", "major");
  893. break;
  894. }
  895. //Ajoute l'objet dans la partie
  896. object_add(g->object, random, x, y);
  897. //Ajoute postion au json
  898. char pos[20];
  899. memset(pos, 0, 20);
  900. snprintf(pos, 20, "%d,%d", x, y);
  901. add_string(json, "pos", pos);
  902. //Retourne type d'ajout
  903. return res;
  904. }
  905. void remove_player(Game* g, int playerIndex) {
  906. pthread_mutex_lock(&gameMutex[g->index]);
  907. pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + playerIndex]);
  908. g->nbPlayer--;
  909. delete_player(g->player[playerIndex]);
  910. pthread_mutex_unlock(&gameMutex[g->index]);
  911. pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + playerIndex]);
  912. }
  913. void stop_game(Game* g) {
  914. pthread_mutex_lock(&gameMutex[g->index]);
  915. //Indique comme inactive
  916. g->active = false;
  917. //Suppr les joueurs
  918. for (int i = 0; i < MAXPLAYER; i++) {
  919. //pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + i]);
  920. if (g->player[i]->ini) {
  921. delete_player(g->player[i]);
  922. }
  923. free(g->player[i]);
  924. //pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + i]);
  925. }
  926. //Libere la memoire
  927. object_clean(g->object);
  928. free(g->object);
  929. for (int i = 0; i < g->width; i++) {
  930. free(g->map[i]);
  931. }
  932. free(g->map);
  933. free(g->mapName);
  934. free(g->mapContent);
  935. pthread_mutex_unlock(&gameMutex[g->index]);
  936. //Retire une game
  937. nbGame--;
  938. }
  939. void clean_games() {
  940. for (int i = 0; i < MAXGAME; i++) {
  941. if (game[i].active) {
  942. pthread_mutex_unlock(&gameMutex[i]);
  943. stop_game(&game[i]);
  944. }
  945. }
  946. }