|
@@ -46,6 +46,26 @@ char** parse_map(char* mapContent, int width, int height) {
|
|
|
return map;
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * Transforme un tableau 2D en une chaine
|
|
|
+ * @param char** Le contenu du fichier map
|
|
|
+ * @param int Largeur de la map
|
|
|
+ * @param int Hauteur de la map
|
|
|
+ * @return char* La map
|
|
|
+ */
|
|
|
+char* unparse_map(char** map, int width, int height) {
|
|
|
+ int pos = 0;
|
|
|
+ char* mapContent = malloc(sizeof (char) * (width * height + 1));
|
|
|
+ memset(mapContent, 0, width * height + 1);
|
|
|
+ //Remplissage
|
|
|
+ for (int i = 0; i < height; i++) {
|
|
|
+ for (int j = 0; j < width; j++) {
|
|
|
+ mapContent[pos++] = map[j][i];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return mapContent;
|
|
|
+}
|
|
|
+
|
|
|
/* --- Fonctions publiques --- */
|
|
|
boolean ini_games() {
|
|
|
//Ini les games
|
|
@@ -316,6 +336,7 @@ boolean bomb_explode(Game* g, int playerIndex, int x, int y, JsonEncoder* json)
|
|
|
JsonEncoder object, notif;
|
|
|
JsonArray bomb, bonus, chain;
|
|
|
int res, index, cBomb = 0, cBonus = 0, cChain = 0;
|
|
|
+ char* map;
|
|
|
//Inie json
|
|
|
ini_encoder(&object);
|
|
|
ini_encoder(¬if);
|
|
@@ -324,7 +345,7 @@ boolean bomb_explode(Game* g, int playerIndex, int x, int y, JsonEncoder* json)
|
|
|
ini_array_encoder(&chain);
|
|
|
//Mutex
|
|
|
pthread_mutex_lock(&gameMutex[g->index]);
|
|
|
- pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + playerIndex]);
|
|
|
+ if(playerIndex >= 0) pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + playerIndex]);
|
|
|
//Determine le type de la bombe
|
|
|
switch (g->map[x][y]) {
|
|
|
case '1':
|
|
@@ -337,7 +358,7 @@ boolean bomb_explode(Game* g, int playerIndex, int x, int y, JsonEncoder* json)
|
|
|
add_string(json, "type", "remote");
|
|
|
break;
|
|
|
default:
|
|
|
- pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + playerIndex]);
|
|
|
+ if(playerIndex >= 0) pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + playerIndex]);
|
|
|
pthread_mutex_unlock(&gameMutex[g->index]);
|
|
|
return false;
|
|
|
}
|
|
@@ -346,15 +367,15 @@ boolean bomb_explode(Game* g, int playerIndex, int x, int y, JsonEncoder* json)
|
|
|
memset(pos, 0, 20);
|
|
|
snprintf(pos, 20, "%d,%d", x, y);
|
|
|
add_string(json, "pos", pos);
|
|
|
- //Si le joueur à un bonus
|
|
|
- if (g->player[playerIndex]->firePower > 0) {
|
|
|
- add_boolean(json, "bonus", true);
|
|
|
- } else {
|
|
|
- add_boolean(json, "bonus", false);
|
|
|
- }
|
|
|
//Calcul l'explosion non mine
|
|
|
if (g->map[x][y] != '2') {
|
|
|
g->map[x][y] = '_';
|
|
|
+ //Si le joueur à un bonus
|
|
|
+ if (g->player[playerIndex]->firePower > 0) {
|
|
|
+ add_boolean(json, "bonus", true);
|
|
|
+ } else {
|
|
|
+ add_boolean(json, "bonus", false);
|
|
|
+ }
|
|
|
//Vers le haut
|
|
|
for (int i = 0; i < 2 + g->player[playerIndex]->firePower; i++) {
|
|
|
//Si on est dans la map
|
|
@@ -519,6 +540,8 @@ boolean bomb_explode(Game* g, int playerIndex, int x, int y, JsonEncoder* json)
|
|
|
//Si c'est une mine
|
|
|
else {
|
|
|
g->map[x][y] = '_';
|
|
|
+ //Pas de bonus pour une mine
|
|
|
+ add_boolean(json, "bonus", false);
|
|
|
//Recup le joueur
|
|
|
if (player_collision_index(g, x, y, &index)) {
|
|
|
if (index != playerIndex) pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + index]);
|
|
@@ -536,14 +559,16 @@ boolean bomb_explode(Game* g, int playerIndex, int x, int y, JsonEncoder* json)
|
|
|
}
|
|
|
}
|
|
|
//Affichage de la map
|
|
|
-
|
|
|
+ map = unparse_map(g->map, g->width, g->height);
|
|
|
+ add_string(json, "map", map);
|
|
|
//Ajout des json
|
|
|
add_array(json, "bomb", &bomb);
|
|
|
add_array(json, "bonusMalus", &bonus);
|
|
|
add_array(json, "chain", &chain);
|
|
|
//Nettoyage
|
|
|
- pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + playerIndex]);
|
|
|
+ if(playerIndex >= 0) pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + playerIndex]);
|
|
|
pthread_mutex_unlock(&gameMutex[g->index]);
|
|
|
+ free(map);
|
|
|
return true;
|
|
|
}
|
|
|
|