rnd-19981108-2
[rocksndiamonds.git] / src / game.c
index 6d4c3da63f690c1609a5312b8785d0f0ff949774..017adc470abf7a353bd45c16c5738af65bff89cc 100644 (file)
@@ -59,7 +59,7 @@ void InitGame()
     player->active = FALSE;
 
     player->action = 0;
-    player->potential_action = 0;
+    player->effective_action = 0;
 
     player->score = 0;
     player->gems_still_needed = level.edelsteine;
@@ -156,7 +156,12 @@ void InitGame()
        int jx = player->jx, jy = player->jy;
 
        player->present = TRUE;
+
+       /*
        if (!network_playing || player->connected)
+       */
+
+       if (!options.network || player->connected)
        {
          player->active = TRUE;
 
@@ -260,9 +265,6 @@ void InitGame()
 
     if (player->connected && !player->present)
     {
-      printf("Oops!\n");
-
-
       for(j=0; j<MAX_PLAYERS; j++)
       {
        struct PlayerInfo *some_player = &stored_player[j];
@@ -285,6 +287,28 @@ void InitGame()
     }
   }
 
+  /* when in single player mode, eliminate all but the first active player */
+  if (!options.network && !setup.team_mode)
+  {
+    for(i=0; i<MAX_PLAYERS; i++)
+    {
+      if (stored_player[i].active)
+      {
+       for(j=i+1; j<MAX_PLAYERS; j++)
+       {
+         struct PlayerInfo *player = &stored_player[j];
+         int jx = player->jx, jy = player->jy;
+
+         if (player->active)
+         {
+           player->active = FALSE;
+           StorePlayer[jx][jy] = 0;
+           Feld[jx][jy] = EL_LEERRAUM;
+         }
+       }
+      }
+    }
+  }
 
   for(i=0; i<MAX_PLAYERS; i++)
   {
@@ -477,6 +501,9 @@ void GameWon()
   int hi_pos;
   int bumplevel = FALSE;
 
+  if (local_player->MovPos)
+    return;
+
   local_player->LevelSolved = FALSE;
 
   if (TimeLeft)
@@ -517,7 +544,7 @@ void GameWon()
   if (tape.recording)
   {
     TapeStop();
-    SaveLevelTape(tape.level_nr);      /* Ask to save tape */
+    SaveTape(tape.level_nr);           /* Ask to save tape */
   }
 
   if ((hi_pos=NewHiScore()) >= 0) 
@@ -2805,10 +2832,11 @@ void CheckForDragon(int x, int y)
   }
 }
 
-void PlayerActions(struct PlayerInfo *player, byte player_action)
+static void PlayerActions(struct PlayerInfo *player, byte player_action)
 {
   static byte stored_player_action[MAX_PLAYERS];
   static int num_stored_actions = 0;
+  static boolean save_tape_entry = FALSE;
   boolean moved = FALSE, snapped = FALSE, bombed = FALSE;
   int jx = player->jx, jy = player->jy;
   int left     = player_action & JOY_LEFT;
@@ -2828,6 +2856,7 @@ void PlayerActions(struct PlayerInfo *player, byte player_action)
 
   if (player_action)
   {
+    save_tape_entry = TRUE;
     player->frame_reset_delay = 0;
 
     if (button1)
@@ -2846,12 +2875,15 @@ void PlayerActions(struct PlayerInfo *player, byte player_action)
 
       stored_player_action[player->index_nr] = player_action;
 
+#if 0
       /* this allows cycled sequences of PlayerActions() */
       if (num_stored_actions >= MAX_PLAYERS)
       {
        TapeRecordAction(stored_player_action);
        num_stored_actions = 0;
       }
+#endif
+
     }
     else if (tape.playing && snapped)
       SnapField(player, 0,0);                  /* stop snapping */
@@ -2864,13 +2896,21 @@ void PlayerActions(struct PlayerInfo *player, byte player_action)
       player->Frame = 0;
   }
 
+  if (tape.recording && num_stored_actions >= MAX_PLAYERS && save_tape_entry)
+  {
+    TapeRecordAction(stored_player_action);
+    num_stored_actions = 0;
+    save_tape_entry = FALSE;
+  }
+
   if (tape.playing && !tape.pausing && !player_action &&
       tape.counter < tape.length)
   {
     int next_joy =
       tape.pos[tape.counter].action[player->index_nr] & (JOY_LEFT|JOY_RIGHT);
 
-    if (next_joy == JOY_LEFT || next_joy == JOY_RIGHT)
+    if ((next_joy == JOY_LEFT || next_joy == JOY_RIGHT) &&
+       (player->MovDir != JOY_UP && player->MovDir != JOY_DOWN))
     {
       int dx = (next_joy == JOY_LEFT ? -1 : +1);
 
@@ -2897,6 +2937,7 @@ void GameActions()
   int sieb_x = 0, sieb_y = 0;
   int i, x,y, element;
   byte *recorded_player_action;
+  byte summarized_player_action = 0;
 
   if (game_status != PLAYING)
     return;
@@ -2933,19 +2974,40 @@ void GameActions()
   }
 
 
+  /*
   if (tape.pausing || (tape.playing && !TapePlayDelay()))
     return;
   else if (tape.recording)
     TapeRecordDelay();
+  */
+
+  if (tape.pausing)
+    return;
+
+  if (tape.playing)
+    TapePlayDelay();
+  else if (tape.recording)
+    TapeRecordDelay();
 
   recorded_player_action = (tape.playing ? TapePlayAction() : NULL);
 
+  for(i=0; i<MAX_PLAYERS; i++)
+  {
+    summarized_player_action |= stored_player[i].action;
+
+    if (!network_playing)
+      stored_player[i].effective_action = stored_player[i].action;
+  }
+
   if (network_playing)
-    SendToServer_MovePlayer(local_player->potential_action);
+    SendToServer_MovePlayer(summarized_player_action);
+
+  if (!options.network && !setup.team_mode)
+    local_player->effective_action = summarized_player_action;
 
   for(i=0; i<MAX_PLAYERS; i++)
   {
-    int actual_player_action = stored_player[i].action;
+    int actual_player_action = stored_player[i].effective_action;
 
     if (recorded_player_action)
       actual_player_action = recorded_player_action[i];
@@ -2958,6 +3020,15 @@ void GameActions()
 
   ScrollScreen(NULL, SCROLL_GO_ON);
 
+
+  /*
+  if (tape.pausing || (tape.playing && !TapePlayDelay()))
+    return;
+  else if (tape.recording)
+    TapeRecordDelay();
+  */
+
+
   FrameCounter++;
   TimeFrames++;
 
@@ -3246,6 +3317,25 @@ boolean MoveFigure(struct PlayerInfo *player, int dx, int dy)
   if (!FrameReached(&player->move_delay,MoveSpeed) && !tape.playing)
     return(FALSE);
 
+  if (player->MovPos)
+  {
+    /* should only happen if pre-1.0 tape recordings are played */
+    /* this is only for backward compatibility */
+
+#if DEBUG
+    printf("THIS SHOULD ONLY HAPPEN WITH PRE-1.0 LEVEL TAPES.\n");
+#endif
+
+    while (player->MovPos)
+    {
+      ScrollFigure(player, SCROLL_GO_ON);
+      ScrollScreen(NULL, SCROLL_GO_ON);
+      FrameCounter++;
+      DrawAllPlayers();
+      BackToFront();
+    }
+  }
+
   if (player->last_move_dir & (MV_LEFT | MV_RIGHT))
   {
     if (!(moved |= MoveFigureOneStep(player, 0,dy, dx,dy)))
@@ -3283,8 +3373,8 @@ boolean MoveFigure(struct PlayerInfo *player, int dx, int dy)
          scroll_x = jx-MIDPOSX + (scroll_x < jx-MIDPOSX ? -offset : +offset);
 
        /* don't scroll over playfield boundaries */
-       if (scroll_x < -1 || scroll_x > lev_fieldx - SCR_FIELDX + 2)
-         scroll_x = (scroll_x < -1 ? -1 : lev_fieldx - SCR_FIELDX + 2);
+       if (scroll_x < -1 || scroll_x > lev_fieldx - SCR_FIELDX + 1)
+         scroll_x = (scroll_x < -1 ? -1 : lev_fieldx - SCR_FIELDX + 1);
 
        /* don't scroll more than one field at a time */
        scroll_x = old_scroll_x + SIGN(scroll_x - old_scroll_x);
@@ -3301,8 +3391,8 @@ boolean MoveFigure(struct PlayerInfo *player, int dx, int dy)
          scroll_y = jy-MIDPOSY + (scroll_y < jy-MIDPOSY ? -offset : +offset);
 
        /* don't scroll over playfield boundaries */
-       if (scroll_y < -1 || scroll_y > lev_fieldy - SCR_FIELDY + 2)
-         scroll_y = (scroll_y < -1 ? -1 : lev_fieldy - SCR_FIELDY + 2);
+       if (scroll_y < -1 || scroll_y > lev_fieldy - SCR_FIELDY + 1)
+         scroll_y = (scroll_y < -1 ? -1 : lev_fieldy - SCR_FIELDY + 1);
 
        /* don't scroll more than one field at a time */
        scroll_y = old_scroll_y + SIGN(scroll_y - old_scroll_y);