added command to automatically test tapes and try to fix if broken
[rocksndiamonds.git] / src / init.c
index 683bacdb44dcf328f2ce18389100ffe4a809de46..be7447666d29dc90c4e29ca326d51a365bb2c84d 100644 (file)
@@ -4,7 +4,7 @@
 // (c) 1995-2014 by Artsoft Entertainment
 //                         Holger Schemel
 //                 info@artsoft.org
-//                 http://www.artsoft.org/
+//                 https://www.artsoft.org/
 // ----------------------------------------------------------------------------
 // init.c
 // ============================================================================
@@ -77,6 +77,11 @@ static int copy_properties[][5] =
     EL_MOLE_LEFT,              EL_MOLE_RIGHT,
     EL_MOLE_UP,                        EL_MOLE_DOWN
   },
+  {
+    EL_SPRING,
+    EL_SPRING_LEFT,            EL_SPRING_RIGHT,
+    EL_SPRING_LEFT,            EL_SPRING_RIGHT,        // (to match array size)
+  },
   {
     -1,
     -1, -1, -1, -1
@@ -275,6 +280,19 @@ static void InitBitmapPointers(void)
 
 void InitImageTextures(void)
 {
+  static int texture_graphics[] =
+  {
+    IMG_GFX_REQUEST_BUTTON_TOUCH_YES,
+    IMG_GFX_REQUEST_BUTTON_TOUCH_NO,
+    IMG_GFX_REQUEST_BUTTON_TOUCH_CONFIRM,
+    IMG_GFX_GAME_BUTTON_TOUCH_STOP,
+    IMG_GFX_GAME_BUTTON_TOUCH_PAUSE,
+    IMG_MENU_BUTTON_TOUCH_BACK,
+    IMG_MENU_BUTTON_TOUCH_NEXT,
+    IMG_MENU_BUTTON_TOUCH_BACK2,
+    IMG_MENU_BUTTON_TOUCH_NEXT2,
+    -1
+  };
   int i, j, k;
 
   FreeAllImageTextures();
@@ -300,24 +318,10 @@ void InitImageTextures(void)
       }
     }
   }
-}
 
-#if 1
-// !!! FIX THIS (CHANGE TO USING NORMAL ELEMENT GRAPHIC DEFINITIONS) !!!
-void SetBitmaps_EM(Bitmap **em_bitmap)
-{
-  em_bitmap[0] = graphic_info[IMG_EMC_OBJECT].bitmap;
-  em_bitmap[1] = graphic_info[IMG_EMC_SPRITE].bitmap;
+  for (i = 0; texture_graphics[i] > -1; i++)
+    CreateImageTextures(texture_graphics[i]);
 }
-#endif
-
-#if 0
-// !!! FIX THIS (CHANGE TO USING NORMAL ELEMENT GRAPHIC DEFINITIONS) !!!
-void SetBitmaps_SP(Bitmap **sp_bitmap)
-{
-  *sp_bitmap = graphic_info[IMG_SP_OBJECTS].bitmap;
-}
-#endif
 
 static int getFontBitmapID(int font_nr)
 {
@@ -1297,12 +1301,15 @@ static void set_graphic_parameters_ext(int graphic, int *parameter,
   g->clone_from = -1;                  // do not use clone graphic
   g->init_delay_fixed = 0;
   g->init_delay_random = 0;
+  g->init_delay_action = -1;
   g->anim_delay_fixed = 0;
   g->anim_delay_random = 0;
+  g->anim_delay_action = -1;
   g->post_delay_fixed = 0;
   g->post_delay_random = 0;
-  g->init_event = ANIM_EVENT_DEFAULT;
-  g->anim_event = ANIM_EVENT_DEFAULT;
+  g->post_delay_action = -1;
+  g->init_event = ANIM_EVENT_UNDEFINED;
+  g->anim_event = ANIM_EVENT_UNDEFINED;
   g->init_event_action = -1;
   g->anim_event_action = -1;
   g->draw_masked = FALSE;
@@ -1311,6 +1318,7 @@ static void set_graphic_parameters_ext(int graphic, int *parameter,
   g->fade_delay = -1;
   g->post_delay = -1;
   g->auto_delay = -1;
+  g->auto_delay_unit = AUTO_DELAY_UNIT_DEFAULT;
   g->align = ALIGN_CENTER;             // default for title screens
   g->valign = VALIGN_MIDDLE;           // default for title screens
   g->sort_priority = 0;                        // default for title screens
@@ -1472,7 +1480,7 @@ static void set_graphic_parameters_ext(int graphic, int *parameter,
   else
     g->anim_frames = 1;
 
-  if (g->anim_frames == 0)             // frames must be at least 1
+  if (g->anim_frames < 1)              // frames must be at least 1
     g->anim_frames = 1;
 
   g->anim_frames_per_line =
@@ -1480,7 +1488,7 @@ static void set_graphic_parameters_ext(int graphic, int *parameter,
      parameter[GFX_ARG_FRAMES_PER_LINE] : anim_frames_per_line);
 
   g->anim_delay = parameter[GFX_ARG_DELAY];
-  if (g->anim_delay == 0)              // delay must be at least 1
+  if (g->anim_delay < 1)               // delay must be at least 1
     g->anim_delay = 1;
 
   g->anim_mode = parameter[GFX_ARG_ANIM_MODE];
@@ -1531,6 +1539,12 @@ static void set_graphic_parameters_ext(int graphic, int *parameter,
     g->init_event_action = parameter[GFX_ARG_INIT_EVENT_ACTION];
   if (parameter[GFX_ARG_ANIM_EVENT_ACTION] != ARG_UNDEFINED_VALUE)
     g->anim_event_action = parameter[GFX_ARG_ANIM_EVENT_ACTION];
+  if (parameter[GFX_ARG_INIT_DELAY_ACTION] != ARG_UNDEFINED_VALUE)
+    g->init_delay_action = parameter[GFX_ARG_INIT_DELAY_ACTION];
+  if (parameter[GFX_ARG_ANIM_DELAY_ACTION] != ARG_UNDEFINED_VALUE)
+    g->anim_delay_action = parameter[GFX_ARG_ANIM_DELAY_ACTION];
+  if (parameter[GFX_ARG_POST_DELAY_ACTION] != ARG_UNDEFINED_VALUE)
+    g->post_delay_action = parameter[GFX_ARG_POST_DELAY_ACTION];
 
   // used for toon animations and global animations
   g->step_offset  = parameter[GFX_ARG_STEP_OFFSET];
@@ -1542,6 +1556,9 @@ static void set_graphic_parameters_ext(int graphic, int *parameter,
   g->x            = parameter[GFX_ARG_X];      // (may be uninitialized,
   g->y            = parameter[GFX_ARG_Y];      // unlike src_x and src_y)
 
+  if (g->step_delay < 1)                       // delay must be at least 1
+    g->step_delay = 1;
+
   // this is only used for drawing font characters
   g->draw_xoffset = parameter[GFX_ARG_DRAW_XOFFSET];
   g->draw_yoffset = parameter[GFX_ARG_DRAW_YOFFSET];
@@ -1572,6 +1589,8 @@ static void set_graphic_parameters_ext(int graphic, int *parameter,
     g->post_delay = parameter[GFX_ARG_POST_DELAY];
   if (parameter[GFX_ARG_AUTO_DELAY] != ARG_UNDEFINED_VALUE)
     g->auto_delay = parameter[GFX_ARG_AUTO_DELAY];
+  if (parameter[GFX_ARG_AUTO_DELAY_UNIT] != ARG_UNDEFINED_VALUE)
+    g->auto_delay_unit = parameter[GFX_ARG_AUTO_DELAY_UNIT];
   if (parameter[GFX_ARG_ALIGN] != ARG_UNDEFINED_VALUE)
     g->align = parameter[GFX_ARG_ALIGN];
   if (parameter[GFX_ARG_VALIGN] != ARG_UNDEFINED_VALUE)
@@ -1723,6 +1742,8 @@ static void InitGraphicInfo(void)
     -1
   };
 
+  FreeGlobalAnimEventInfo();
+
   checked_free(graphic_info);
 
   graphic_info = checked_calloc(num_images * sizeof(struct GraphicInfo));
@@ -2341,6 +2362,8 @@ static int get_special_property_bit(int element, int property_bit_nr)
     { EL_SP_ELECTRON,          15      },
     { EL_BALLOON,              16      },
     { EL_SPRING,               17      },
+    { EL_SPRING_LEFT,          17      },
+    { EL_SPRING_RIGHT,         17      },
     { EL_EMC_ANDROID,          18      },
 
     { -1,                      -1      },
@@ -2846,6 +2869,7 @@ void InitElementPropertiesStatic(void)
     EL_BOMB,
     EL_NUT,
     EL_AMOEBA_DROP,
+    EL_AMOEBA_DROPPING,
     EL_QUICKSAND_FULL,
     EL_QUICKSAND_FAST_FULL,
     EL_MAGIC_WALL_FULL,
@@ -4876,6 +4900,7 @@ static void InitGlobal(void)
   }
 
   global.autoplay_leveldir = NULL;
+  global.patchtapes_leveldir = NULL;
   global.convert_leveldir = NULL;
   global.create_images_dir = NULL;
 
@@ -5007,18 +5032,20 @@ static void Execute_Command(char *command)
 
     exit(0);
   }
-  else if (strPrefix(command, "autotest ") ||
-          strPrefix(command, "autoplay ") ||
+  else if (strPrefix(command, "autoplay ") ||
           strPrefix(command, "autoffwd ") ||
-          strPrefix(command, "autowarp "))
+          strPrefix(command, "autowarp ") ||
+          strPrefix(command, "autotest ") ||
+          strPrefix(command, "autofix "))
   {
-    char *str_ptr = getStringCopy(&command[9]);        // read command parameters
+    char *str_ptr = getStringCopy(&command[8]);        // read command parameters
 
     global.autoplay_mode =
-      (strPrefix(command, "autotest") ? AUTOPLAY_MODE_TEST :
-       strPrefix(command, "autoplay") ? AUTOPLAY_MODE_PLAY :
+      (strPrefix(command, "autoplay") ? AUTOPLAY_MODE_PLAY :
        strPrefix(command, "autoffwd") ? AUTOPLAY_MODE_FFWD :
        strPrefix(command, "autowarp") ? AUTOPLAY_MODE_WARP :
+       strPrefix(command, "autotest") ? AUTOPLAY_MODE_TEST :
+       strPrefix(command, "autofix")  ? AUTOPLAY_MODE_FIX :
        AUTOPLAY_MODE_NONE);
 
     while (*str_ptr != '\0')                   // continue parsing string
@@ -5053,9 +5080,68 @@ static void Execute_Command(char *command)
        str_ptr++;
     }
 
-    if (global.autoplay_mode == AUTOPLAY_MODE_TEST)
+    if (global.autoplay_mode & AUTOPLAY_MODE_WARP_NO_DISPLAY)
       program.headless = TRUE;
   }
+  else if (strPrefix(command, "patch tapes "))
+  {
+    char *str_ptr = getStringCopy(&command[12]); // read command parameters
+
+    // skip leading whitespace
+    while (*str_ptr == ' ' || *str_ptr == '\t')
+      str_ptr++;
+
+    if (*str_ptr == '\0')
+      Error(ERR_EXIT, "cannot find MODE in command '%s'", command);
+
+    global.patchtapes_mode = str_ptr;          // store patch mode
+
+    // advance to next whitespace (or end of string)
+    while (*str_ptr != ' ' && *str_ptr != '\t' && *str_ptr != '\0')
+      str_ptr++;
+
+    while (*str_ptr != '\0')                   // continue parsing string
+    {
+      // cut leading whitespace from string, replace it by string terminator
+      while (*str_ptr == ' ' || *str_ptr == '\t')
+       *str_ptr++ = '\0';
+
+      if (*str_ptr == '\0')                    // end of string reached
+       break;
+
+      if (global.patchtapes_leveldir == NULL)  // read level set string
+      {
+       global.patchtapes_leveldir = str_ptr;
+       global.patchtapes_all = TRUE;           // default: patch all tapes
+
+       for (i = 0; i < MAX_TAPES_PER_SET; i++)
+         global.patchtapes_level[i] = FALSE;
+      }
+      else                                     // read level number string
+      {
+       int level_nr = atoi(str_ptr);           // get level_nr value
+
+       if (level_nr >= 0 && level_nr < MAX_TAPES_PER_SET)
+         global.patchtapes_level[level_nr] = TRUE;
+
+       global.patchtapes_all = FALSE;
+      }
+
+      // advance string pointer to the next whitespace (or end of string)
+      while (*str_ptr != ' ' && *str_ptr != '\t' && *str_ptr != '\0')
+       str_ptr++;
+    }
+
+    if (global.patchtapes_leveldir == NULL)
+    {
+      if (strEqual(global.patchtapes_mode, "help"))
+       global.patchtapes_leveldir = UNDEFINED_LEVELSET;
+      else
+       Error(ERR_EXIT, "cannot find LEVELDIR in command '%s'", command);
+    }
+
+    program.headless = TRUE;
+  }
   else if (strPrefix(command, "convert "))
   {
     char *str_copy = getStringCopy(strchr(command, ' ') + 1);
@@ -5892,6 +5978,7 @@ void ReloadCustomArtwork(int force_reload)
   InitOverrideArtwork();
 
   force_reload_gfx |= AdjustGraphicsForEMC();
+  force_reload_snd |= AdjustSoundsForEMC();
 
   gfx_new_identifier = getNewArtworkIdentifier(ARTWORK_TYPE_GRAPHICS);
   snd_new_identifier = getNewArtworkIdentifier(ARTWORK_TYPE_SOUNDS);
@@ -6097,6 +6184,8 @@ void OpenAll(void)
   InitVideoBuffer(WIN_XSIZE, WIN_YSIZE, DEFAULT_DEPTH, setup.fullscreen);
   InitVideoOverlay();
 
+  InitEventFilter(FilterMouseMotionEvents);
+
   print_timestamp_time("[init video stuff]");
 
   InitElementPropertiesStatic();
@@ -6137,7 +6226,12 @@ void OpenAll(void)
 
   if (global.autoplay_leveldir)
   {
-    AutoPlayTape();
+    AutoPlayTapes();
+    return;
+  }
+  else if (global.patchtapes_leveldir)
+  {
+    PatchTapes();
     return;
   }
   else if (global.convert_leveldir)