Pārlūkot izejas kodu

Spawn aleatoire

Loquicom 6 gadi atpakaļ
vecāks
revīzija
ddbbfaee64
2 mainītis faili ar 30 papildinājumiem un 6 dzēšanām
  1. 4 4
      serveur/game.c
  2. 26 2
      serveur/handler.c

+ 4 - 4
serveur/game.c

@@ -969,12 +969,12 @@ int spawn_object(Game* g, int x, int y, JsonEncoder* json) {
             add_string(json, "type", "fire_power");
             break;
         case OBJ_SCOOTER:
-            res = 2;
-            add_string(json, "type", "scooter");
+            res = 1;
+            add_string(json, "type", "remote");
             break;
         case OBJ_BROKENLEG:
-            res = 2;
-            add_string(json, "type", "broken_legs");
+            res = 1;
+            add_string(json, "type", "mine");
             break;
         case OBJ_LIFEUP:
             res = 2;

+ 26 - 2
serveur/handler.c

@@ -223,6 +223,18 @@ int handler_game_create(int cliId, JsonParser* json) {
         clean_json_encoder(&reponse);
         return FAIL;
     }
+    //Cherche pos de depart random
+    int x = 0, y = 0;
+    srand(time(NULL));
+    do{
+        x = rand() % game[index].width;
+        y = rand() % game[index].height;
+    }while(game[index].map[x][y] != '_');
+    game[index].player[joueur]->x = x;
+    game[index].player[joueur]->y = y;
+    char pos[25];
+    memset(pos, 0, 25);
+    snprintf(pos, 25, "%d,%d", x, y);
     //Recup info
     describe_game(&game[index], joueur, &reponse);
     //Ajout infos
@@ -230,7 +242,7 @@ int handler_game_create(int cliId, JsonParser* json) {
     sprintf(msg, "Game created with %s", map);
     add_integer(&reponse, "status", 201);
     add_string(&reponse, "message", msg);
-    add_string(&reponse, "startPos", "1,1");
+    add_string(&reponse, "startPos", pos);
     free(msg);
     //Envoi
     if (!send_client(cliId, &reponse)) {
@@ -284,11 +296,23 @@ int handler_game_join(int cliId, JsonParser* json) {
         free(name);
         return FAIL;
     }
+    //Cherche pos de depart random
+    int x = 0, y = 0;
+    srand(time(NULL));
+    do{
+        x = rand() % game[index].width;
+        y = rand() % game[index].height;
+    }while(game[index].map[x][y] != '_');
+    game[index].player[joueur]->x = x;
+    game[index].player[joueur]->y = y;
+    char pos[25];
+    memset(pos, 0, 25);
+    snprintf(pos, 25, "%d,%d", x, y);
     //Recup info
     describe_game(&game[index], joueur, &reponse);
     //Ajout infos
     add_integer(&reponse, "status", 201);
-    add_string(&reponse, "startPos", "1,1");
+    add_string(&reponse, "startPos", pos);
     //Envoi
     if (!send_client(cliId, &reponse)) {
         adderror("Impossible de répondre au client");