rnd-20020331-2-src
[rocksndiamonds.git] / src / tape.c
index 01948ed9e7f14ddf55f27698cb9e171dbbca544d..963426033e02a9bb58a0013533e58364ff239137 100644 (file)
 #include "tools.h"
 #include "files.h"
 #include "network.h"
+#include "cartoons.h"
 
 /* tape button identifiers */
 #define TAPE_CTRL_ID_EJECT             0
-#define TAPE_CTRL_ID_STOP              1
-#define TAPE_CTRL_ID_PAUSE             2
-#define TAPE_CTRL_ID_RECORD            3
-#define TAPE_CTRL_ID_PLAY              4
+#define TAPE_CTRL_ID_INDEX             1
+#define TAPE_CTRL_ID_STOP              2
+#define TAPE_CTRL_ID_PAUSE             3
+#define TAPE_CTRL_ID_RECORD            4
+#define TAPE_CTRL_ID_PLAY              5
 
-#define NUM_TAPE_BUTTONS               5
+#define NUM_TAPE_BUTTONS               6
+
+/* values for tape handling */
+#define TAPE_PAUSE_SECONDS_BEFORE_DEATH                5
 
 /* forward declaration for internal use */
 static void HandleTapeButtons(struct GadgetInfo *);
@@ -77,7 +82,7 @@ static struct GadgetInfo *tape_gadget[NUM_TAPE_BUTTONS];
 #define VIDEO_TIME_YSIZE       16
 
 /* special */
-#define VIDEO_PBEND_LABEL_XPOS 6
+#define VIDEO_PBEND_LABEL_XPOS 5
 #define VIDEO_PBEND_LABEL_YPOS 220
 #define VIDEO_PBEND_LABEL_XSIZE        35
 #define VIDEO_PBEND_LABEL_YSIZE        30
@@ -304,6 +309,7 @@ void TapeStartRecording()
   tape.changed = TRUE;
   tape.date = 10000*(zeit2->tm_year%100) + 100*zeit2->tm_mon + zeit2->tm_mday;
   tape.random_seed = InitRND(NEW_RANDOMIZE);
+  tape.game_version = GAME_VERSION_ACTUAL;
 
   for(i=0; i<MAX_PLAYERS; i++)
     tape.player_participates[i] = FALSE;
@@ -311,27 +317,27 @@ void TapeStartRecording()
   DrawVideoDisplay(VIDEO_STATE_REC_ON, 0);
   DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
   DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
+  MapTapeIndexButton();
+
+  SetDrawDeactivationMask(REDRAW_NONE);
 }
 
 void TapeStopRecording()
 {
-  int i;
-
   if (!tape.recording)
     return;
 
-  for(i=0; i<MAX_PLAYERS; i++)
-    tape.pos[tape.counter].action[i] = 0;
-
   tape.counter++;
   tape.length = tape.counter;
   tape.length_seconds = GetTapeLength();
   tape.recording = FALSE;
   tape.pausing = FALSE;
+
   DrawVideoDisplay(VIDEO_STATE_REC_OFF, 0);
+  MapTapeEjectButton();
 }
 
-void TapeRecordAction(byte joy[MAX_PLAYERS])
+void TapeRecordAction(byte action[MAX_PLAYERS])
 {
   int i;
 
@@ -344,35 +350,29 @@ void TapeRecordAction(byte joy[MAX_PLAYERS])
     return;
   }
 
-  for(i=0; i<MAX_PLAYERS; i++)
-    tape.pos[tape.counter].action[i] = joy[i];
-
-  tape.counter++;
-  tape.pos[tape.counter].delay = 0;
-}
-
-void TapeRecordDelay()
-{
-  int i;
+  if (tape.pos[tape.counter].delay > 0)                /* already stored action */
+  {
+    boolean changed_events = FALSE;
 
-  if (!tape.recording || tape.pausing)
-    return;
+    for(i=0; i<MAX_PLAYERS; i++)
+      if (tape.pos[tape.counter].action[i] != action[i])
+       changed_events = TRUE;
 
-  if (tape.counter >= MAX_TAPELEN)
-  {
-    TapeStopRecording();
-    return;
+    if (changed_events || tape.pos[tape.counter].delay >= 255)
+    {
+      tape.counter++;
+      tape.pos[tape.counter].delay = 0;
+    }
+    else
+      tape.pos[tape.counter].delay++;
   }
 
-  tape.pos[tape.counter].delay++;
-
-  if (tape.pos[tape.counter].delay >= 255)
+  if (tape.pos[tape.counter].delay == 0)       /* store new action */
   {
     for(i=0; i<MAX_PLAYERS; i++)
-      tape.pos[tape.counter].action[i] = 0;
+      tape.pos[tape.counter].action[i] = action[i];
 
-    tape.counter++;
-    tape.pos[tape.counter].delay = 0;
+    tape.pos[tape.counter].delay++;
   }
 }
 
@@ -392,6 +392,20 @@ void TapeTogglePause()
     state |= VIDEO_STATE_PBEND_OFF;
 
   DrawVideoDisplay(state, 0);
+
+  if (tape.index_search)
+  {
+    SetDrawDeactivationMask(REDRAW_NONE);
+    RedrawPlayfield(TRUE, 0,0,0,0);
+  }
+
+  if (tape.index_search)
+  {
+    tape.index_search = FALSE;
+
+    SetDrawDeactivationMask(REDRAW_NONE);
+    RedrawPlayfield(TRUE, 0,0,0,0);
+  }
 }
 
 void TapeStartPlaying()
@@ -409,11 +423,16 @@ void TapeStartPlaying()
   tape.playing = TRUE;
   tape.pausing = FALSE;
   tape.fast_forward = FALSE;
+  tape.index_search = FALSE;
+
   InitRND(tape.random_seed);
 
   DrawVideoDisplay(VIDEO_STATE_PLAY_ON, 0);
   DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
   DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
+  MapTapeIndexButton();
+
+  SetDrawDeactivationMask(REDRAW_NONE);
 }
 
 void TapeStopPlaying()
@@ -423,44 +442,18 @@ void TapeStopPlaying()
 
   tape.playing = FALSE;
   tape.pausing = FALSE;
+
   DrawVideoDisplay(VIDEO_STATE_PLAY_OFF, 0);
+  MapTapeEjectButton();
 }
 
 byte *TapePlayAction()
 {
-  static byte joy[MAX_PLAYERS];
+  static byte action[MAX_PLAYERS];
   int i;
 
   if (!tape.playing || tape.pausing)
-    return(NULL);
-
-  if (tape.counter >= tape.length)
-  {
-    TapeStop();
-    return(NULL);
-  }
-
-  if (tape.delay_played == tape.pos[tape.counter].delay)
-  {
-    tape.delay_played = 0;
-    tape.counter++;
-
-    for(i=0; i<MAX_PLAYERS; i++)
-      joy[i] = tape.pos[tape.counter-1].action[i];
-  }
-  else
-  {
-    for(i=0; i<MAX_PLAYERS; i++)
-      joy[i] = 0;
-  }
-
-  return(joy);
-}
-
-boolean TapePlayDelay()
-{
-  if (!tape.playing || tape.pausing)
-    return(FALSE);
+    return NULL;
 
   if (tape.pause_before_death) /* STOP 10s BEFORE PLAYER GETS KILLED... */
   {
@@ -475,23 +468,31 @@ boolean TapePlayDelay()
     if (TimePlayed > tape.length_seconds - TAPE_PAUSE_SECONDS_BEFORE_DEATH)
     {
       TapeTogglePause();
-      return(FALSE);
+      return NULL;
     }
   }
 
-  if (tape.counter >= tape.length)
+  if (tape.counter >= tape.length)     /* end of tape reached */
   {
-    TapeStop();
-    return(TRUE);
+    if (tape.index_search)
+      TapeTogglePause();
+    else
+      TapeStop();
+
+    return NULL;
   }
 
-  if (tape.delay_played < tape.pos[tape.counter].delay)
+  for(i=0; i<MAX_PLAYERS; i++)
+    action[i] = tape.pos[tape.counter].action[i];
+
+  tape.delay_played++;
+  if (tape.delay_played >= tape.pos[tape.counter].delay)
   {
-    tape.delay_played++;
-    return(TRUE);
+    tape.counter++;
+    tape.delay_played = 0;
   }
-  else
-    return(FALSE);
+
+  return action;
 }
 
 void TapeStop()
@@ -535,6 +536,7 @@ unsigned int GetTapeLength()
 #define TAPE_BUTTON_YPOS       77
 
 #define TAPE_BUTTON_EJECT_XPOS (TAPE_BUTTON_XPOS + 0 * TAPE_BUTTON_XSIZE)
+#define TAPE_BUTTON_INDEX_XPOS (TAPE_BUTTON_XPOS + 0 * TAPE_BUTTON_XSIZE)
 #define TAPE_BUTTON_STOP_XPOS  (TAPE_BUTTON_XPOS + 1 * TAPE_BUTTON_XSIZE)
 #define TAPE_BUTTON_PAUSE_XPOS (TAPE_BUTTON_XPOS + 2 * TAPE_BUTTON_XSIZE)
 #define TAPE_BUTTON_RECORD_XPOS        (TAPE_BUTTON_XPOS + 3 * TAPE_BUTTON_XSIZE)
@@ -552,6 +554,11 @@ static struct
     TAPE_CTRL_ID_EJECT,
     "eject tape"
   },
+  {
+    TAPE_BUTTON_INDEX_XPOS,    TAPE_BUTTON_YPOS,
+    TAPE_CTRL_ID_INDEX,
+    "index mark"
+  },
   {
     TAPE_BUTTON_STOP_XPOS,     TAPE_BUTTON_YPOS,
     TAPE_CTRL_ID_STOP,
@@ -592,6 +599,12 @@ void CreateTapeButtons()
     gd_x2 = DOOR_GFX_PAGEX3 + gd_xoffset;
     gd_y  = DOOR_GFX_PAGEY2 + gd_yoffset;
 
+    if (i == TAPE_CTRL_ID_INDEX)
+    {
+      gd_x1 = DOOR_GFX_PAGEX6 + gd_xoffset;
+      gd_x2 = DOOR_GFX_PAGEX5 + gd_xoffset;
+    }
+
     gi = CreateGadget(GDI_CUSTOM_ID, id,
                      GDI_INFO_TEXT, tapebutton_info[i].infotext,
                      GDI_X, VX + gd_xoffset,
@@ -613,12 +626,28 @@ void CreateTapeButtons()
   }
 }
 
+void MapTapeEjectButton()
+{
+  UnmapGadget(tape_gadget[TAPE_CTRL_ID_INDEX]);
+  MapGadget(tape_gadget[TAPE_CTRL_ID_EJECT]);
+}
+
+void MapTapeIndexButton()
+{
+  UnmapGadget(tape_gadget[TAPE_CTRL_ID_EJECT]);
+  MapGadget(tape_gadget[TAPE_CTRL_ID_INDEX]);
+}
+
 void MapTapeButtons()
 {
   int i;
 
   for (i=0; i<NUM_TAPE_BUTTONS; i++)
-    MapGadget(tape_gadget[i]);
+    if (i != TAPE_CTRL_ID_INDEX)
+      MapGadget(tape_gadget[i]);
+
+  if (tape.recording || tape.playing)
+    MapTapeIndexButton();
 }
 
 void UnmapTapeButtons()
@@ -655,6 +684,20 @@ static void HandleTapeButtons(struct GadgetInfo *gi)
       DrawCompleteVideoDisplay();
       break;
 
+    case TAPE_CTRL_ID_INDEX:
+      if (tape.recording)
+       printf("Setting index mark ...\n");
+      else if (tape.playing)
+      {
+       printf("Going to index mark ...\n");
+
+       tape.index_search = TRUE;
+
+       if (!tape.fast_forward || tape.pause_before_death)
+         SetDrawDeactivationMask(REDRAW_FIELD);
+      }
+      break;
+
     case TAPE_CTRL_ID_STOP:
       TapeStop();
       break;
@@ -675,6 +718,7 @@ static void HandleTapeButtons(struct GadgetInfo *gi)
 #endif
        {
          game_status = PLAYING;
+         StopAnimation();
          InitGame();
        }
       }
@@ -682,10 +726,16 @@ static void HandleTapeButtons(struct GadgetInfo *gi)
       {
        if (tape.playing)       /* PLAYING -> PAUSING -> RECORDING */
        {
+         if (tape.game_version != GAME_VERSION_ACTUAL &&
+             !Request("This may break old version tape ! Append anyway ?",
+                      REQ_ASK))
+           break;
+
          tape.pos[tape.counter].delay = tape.delay_played;
          tape.playing = FALSE;
          tape.recording = TRUE;
          tape.changed = TRUE;
+         tape.game_version = GAME_VERSION_ACTUAL;
 
          DrawVideoDisplay(VIDEO_STATE_PLAY_OFF | VIDEO_STATE_REC_ON,0);
        }
@@ -703,6 +753,7 @@ static void HandleTapeButtons(struct GadgetInfo *gi)
        TapeStartPlaying();
 
        game_status = PLAYING;
+       StopAnimation();
        InitGame();
       }
       else if (tape.playing)