rnd-19981016-1
[rocksndiamonds.git] / src / game.c
index c8519b390c7500c97d1d3b6507c04b63bd55e94d..7e678bfbd4f063ea04e37486abb4c7143f408f2d 100644 (file)
@@ -63,14 +63,22 @@ void InitGame()
   BOOL emulate_bd = TRUE;      /* unless non-BOULDERDASH elements found */
   BOOL emulate_sb = TRUE;      /* unless non-SOKOBAN     elements found */
 
+  /* don't play tapes over network */
+  network_playing = (network && !tape.playing);
+
   for(i=0; i<MAX_PLAYERS; i++)
   {
     struct PlayerInfo *player = &stored_player[i];
 
     player->index_nr = i;
     player->element_nr = EL_SPIELER1 + i;
+
+    player->present = FALSE;
     player->active = FALSE;
+
+    /*
     player->local = FALSE;
+    */
 
     player->score = 0;
     player->gems_still_needed = level.edelsteine;
@@ -126,23 +134,16 @@ void InitGame()
     player->GameOver = FALSE;
   }
 
+  /*
   local_player->active = TRUE;
   local_player->local = TRUE;
-
-  network_player_action_stored = FALSE;
-
-
-
-  /* initial null action */
-  SendToServer_MovePlayer(0,0);
-
-
-
-  /*
-  printf("BLURB\n");
   */
 
+  network_player_action_received = FALSE;
 
+  /* initial null action */
+  if (network_playing)
+    SendToServer_MovePlayer(MV_NO_MOVING);
 
   ZX = ZY = -1;
 
@@ -187,16 +188,30 @@ void InitGame()
        struct PlayerInfo *player = &stored_player[Feld[x][y] - EL_SPIELER1];
        int jx = player->jx, jy = player->jy;
 
-       /* remove duplicate players */
+       /*
+       player->active = TRUE;
+       */
+
+       player->present = TRUE;
+       if (player->connected)
+       {
+         player->active = TRUE;
+
+         printf("Player %d activated.\n", player->element_nr);
+         printf("[Local player is %d and currently %s.]\n",
+                local_player->element_nr,
+                local_player->active ? "active" : "not active");
+       }
+
+       /* remove potentially duplicate players */
        if (StorePlayer[jx][jy] == Feld[x][y])
          StorePlayer[jx][jy] = 0;
 
-       player->active = TRUE;
-
        StorePlayer[x][y] = Feld[x][y];
        Feld[x][y] = EL_LEERRAUM;
        player->jx = player->last_jx = x;
        player->jy = player->last_jy = y;
+
        break;
       }
       case EL_BADEWANNE:
@@ -274,6 +289,52 @@ void InitGame()
     }
   }
 
+  /* check if any connected player was not found in playfield */
+  for(i=0; i<MAX_PLAYERS; i++)
+  {
+    struct PlayerInfo *player = &stored_player[i];
+
+    if (player->connected && !player->present)
+    {
+      printf("Oops!\n");
+
+
+      for(j=0; j<MAX_PLAYERS; j++)
+      {
+       struct PlayerInfo *some_player = &stored_player[j];
+       int jx = some_player->jx, jy = some_player->jy;
+
+       /* assign first free player found that is present in the playfield */
+       if (some_player->present && !some_player->connected)
+       {
+         player->present = TRUE;
+         player->active = TRUE;
+         some_player->present = FALSE;
+
+         StorePlayer[jx][jy] = player->element_nr;
+         player->jx = player->last_jx = jx;
+         player->jy = player->last_jy = jy;
+
+         break;
+       }
+      }
+    }
+  }
+
+  for(i=0; i<MAX_PLAYERS; i++)
+  {
+    struct PlayerInfo *player = &stored_player[i];
+
+    printf("Player %d: present == %d, connected == %d, active == %d.\n",
+          i+1,
+          player->present,
+          player->connected,
+          player->active);
+    if (local_player == player)
+      printf("Player %d is local player.\n", i+1);
+  }
+
+
   game_emulation = (emulate_bd ? EMU_BOULDERDASH :
                    emulate_sb ? EMU_SOKOBAN : EMU_NONE);
 
@@ -330,6 +391,12 @@ void InitGame()
     PlaySoundLoop(background_loop[level_nr % num_bg_loops]);
 
   XAutoRepeatOff(display);
+
+
+  for (i=0;i<4;i++)
+    printf("Spieler %d %saktiv.\n",
+          i+1, (stored_player[i].active ? "" : "nicht "));
+
 }
 
 void InitMovDir(int x, int y)
@@ -2874,11 +2941,9 @@ void GameActions(byte player_action)
   int i, x,y, element;
   int *recorded_player_action;
 
-
   if (game_status != PLAYING)
     return;
 
-
 #ifdef DEBUG
   action_delay_value =
     (tape.playing && tape.fast_forward ? FFWD_FRAME_DELAY : GameFrameDelay);
@@ -2887,21 +2952,16 @@ void GameActions(byte player_action)
     (tape.playing && tape.fast_forward ? FFWD_FRAME_DELAY : GAME_FRAME_DELAY);
 #endif
 
-  /*
-  HandleNetworking();
-
-  if (game_status != PLAYING)
-    return;
-  */
-
   /* main game synchronization point */
   WaitUntilDelayReached(&action_delay, action_delay_value);
 
-  if (!network_player_action_stored)
+  if (network_playing && !network_player_action_received)
   {
+    /*
 #ifdef DEBUG
     printf("DEBUG: try to get network player actions in time\n");
 #endif
+    */
 
     /* last chance to get network player actions without main loop delay */
     HandleNetworking();
@@ -2909,27 +2969,45 @@ void GameActions(byte player_action)
     if (game_status != PLAYING)
       return;
 
-    if (!network_player_action_stored)
+    if (!network_player_action_received)
     {
+      /*
 #ifdef DEBUG
       printf("DEBUG: failed to get network player actions in time\n");
 #endif
+      */
       return;
     }
   }
 
+
+  if (tape.pausing || (tape.playing && !TapePlayDelay()))
+    return;
+  else if (tape.recording)
+    TapeRecordDelay();
+
+
   if (tape.playing)
     recorded_player_action = TapePlayAction();
   else
     recorded_player_action = NULL;
 
-
-  SendToServer_MovePlayer(player_action, FrameCounter);
-
+  if (network_playing)
+    SendToServer_MovePlayer(player_action);
 
   for(i=0; i<MAX_PLAYERS; i++)
   {
+    /*
+    int actual_player_action =
+      (network ? network_player_action[i] : player_action);
+      */
+
+    int actual_player_action =
+      (network_playing ? network_player_action[i] : player_action);
+
+    /*
     int actual_player_action = network_player_action[i];
+    */
 
     /*
     int actual_player_action = player_action;
@@ -2942,6 +3020,9 @@ void GameActions(byte player_action)
       actual_player_action = 0;
     */
 
+    if (!network && i != TestPlayer)
+      actual_player_action = 0;
+
     /* TEST TEST TEST */
 
     if (recorded_player_action)
@@ -2953,23 +3034,26 @@ void GameActions(byte player_action)
     network_player_action[i] = 0;
   }
 
-  network_player_action_stored = FALSE;
+  network_player_action_received = FALSE;
 
   ScrollScreen(NULL, SCROLL_GO_ON);
 
+  /*
   if (tape.pausing || (tape.playing && !TapePlayDelay()))
     return;
   else if (tape.recording)
     TapeRecordDelay();
+  */
 
   FrameCounter++;
   TimeFrames++;
 
+
   /*
-  printf("advancing FrameCounter to %d\n",
-        FrameCounter);
+  printf("FrameCounter == %d, RND(100) == %d\n", FrameCounter, RND(100));
   */
 
+
   for(y=0;y<lev_fieldy;y++) for(x=0;x<lev_fieldx;x++)
   {
     Stop[x][y] = FALSE;
@@ -3207,7 +3291,7 @@ BOOL MoveFigureOneStep(struct PlayerInfo *player,
   if (!IN_LEV_FIELD(new_jx,new_jy))
     return(MF_NO_ACTION);
 
-  if (standalone && !AllPlayersInSight(player, new_jx,new_jy))
+  if (!network && !AllPlayersInSight(player, new_jx,new_jy))
     return(MF_NO_ACTION);
 
   element = MovingOrBlocked2Element(new_jx,new_jy);
@@ -3280,7 +3364,7 @@ BOOL MoveFigure(struct PlayerInfo *player, int dx, int dy)
   */
 
   if (moved & MF_MOVING && !ScreenMovPos &&
-      (player == local_player || standalone))
+      (player == local_player || !network))
   {
     int old_scroll_x = scroll_x, old_scroll_y = scroll_y;
     int offset = (scroll_delay_on ? 3 : 0);
@@ -3350,7 +3434,7 @@ BOOL MoveFigure(struct PlayerInfo *player, int dx, int dy)
 
     if (scroll_x != old_scroll_x || scroll_y != old_scroll_y)
     {
-      if (standalone && !AllPlayersInVisibleScreen())
+      if (!network && !AllPlayersInVisibleScreen())
       {
        scroll_x = old_scroll_x;
        scroll_y = old_scroll_y;
@@ -4020,7 +4104,8 @@ BOOL PlaceBomb(struct PlayerInfo *player)
     player->dynamite--;
     DrawText(DX_DYNAMITE, DY_DYNAMITE, int2str(local_player->dynamite, 3),
             FS_SMALL, FC_YELLOW);
-    DrawGraphicThruMask(SCREENX(jx),SCREENY(jy),GFX_DYNAMIT);
+    if (IN_SCR_FIELD(SCREENX(jx),SCREENY(jy)))
+      DrawGraphicThruMask(SCREENX(jx),SCREENY(jy),GFX_DYNAMIT);
   }
   else
   {
@@ -4028,7 +4113,8 @@ BOOL PlaceBomb(struct PlayerInfo *player)
     Store2[jx][jy] = player->element_nr;       /* for DynaExplode() */
     MovDelay[jx][jy] = 96;
     player->dynabombs_left--;
-    DrawGraphicThruMask(SCREENX(jx),SCREENY(jy),GFX_DYNABOMB);
+    if (IN_SCR_FIELD(SCREENX(jx),SCREENY(jy)))
+      DrawGraphicThruMask(SCREENX(jx),SCREENY(jy),GFX_DYNABOMB);
   }
 
   return(TRUE);