game.c 33 KB

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