rnd-19990109-1
[rocksndiamonds.git] / src / files.c
index 2b089217ae9daba79e3c6d345ec153f5f8d9787e..87a60c1cc6112ad8546d4c8ad7587471b357d5d6 100644 (file)
@@ -58,7 +58,7 @@
 #define LEVELSETUP_FILENAME    "lvlsetup.cnf"
 #define LEVELINFO_FILENAME     "lvlinfo.cnf"
 #define LEVELFILE_EXTENSION    "lvl"
-#define TAPEFILE_EXTENSION     "rec"
+#define TAPEFILE_EXTENSION     "tap"
 #define SCOREFILE_EXTENSION    "sco"
 #define ERROR_FILENAME         "error.out"
 #endif
 #define TAPE_PERMS             LEVEL_PERMS
 #define SETUP_PERMS            LEVEL_PERMS
 
+/* sort priorities of level series (also used as level series classes) */
+#define LEVELCLASS_TUTORIAL_START      10
+#define LEVELCLASS_TUTORIAL_END                99
+#define LEVELCLASS_CLASSICS_START      100
+#define LEVELCLASS_CLASSICS_END                199
+#define LEVELCLASS_CONTRIBUTION_START  200
+#define LEVELCLASS_CONTRIBUTION_END    299
+#define LEVELCLASS_USER_START          300
+#define LEVELCLASS_USER_END            399
+#define LEVELCLASS_UNDEFINED           999
+
 static void SaveUserLevelInfo();               /* for 'InitUserLevelDir()' */
 static char *getSetupLine(char *, int);                /* for 'SaveUserLevelInfo()' */
 
@@ -155,7 +166,7 @@ static char *getScoreDir(char *level_subdir)
 static char *getLevelFilename(int nr)
 {
   static char *filename = NULL;
-  char basename[20 + strlen(LEVELFILE_EXTENSION)];
+  char basename[MAX_FILENAME_LEN];
 
   if (filename != NULL)
     free(filename);
@@ -173,7 +184,7 @@ static char *getLevelFilename(int nr)
 static char *getTapeFilename(int nr)
 {
   static char *filename = NULL;
-  char basename[20 + strlen(LEVELFILE_EXTENSION)];
+  char basename[MAX_FILENAME_LEN];
 
   if (filename != NULL)
     free(filename);
@@ -187,7 +198,7 @@ static char *getTapeFilename(int nr)
 static char *getScoreFilename(int nr)
 {
   static char *filename = NULL;
-  char basename[20 + strlen(LEVELFILE_EXTENSION)];
+  char basename[MAX_FILENAME_LEN];
 
   if (filename != NULL)
     free(filename);
@@ -242,8 +253,8 @@ static void setLevelInfoToDefaults()
   lev_fieldx = level.fieldx = STD_LEV_FIELDX;
   lev_fieldy = level.fieldy = STD_LEV_FIELDY;
 
-  for(x=0; x<MAX_LEV_FIELDX; x++) 
-    for(y=0; y<MAX_LEV_FIELDY; y++) 
+  for(x=0; x<MAX_LEV_FIELDX; x++)
+    for(y=0; y<MAX_LEV_FIELDY; y++)
       Feld[x][y] = Ur[x][y] = EL_ERDREICH;
 
   level.time = 100;
@@ -253,12 +264,15 @@ static void setLevelInfoToDefaults()
   level.dauer_ablenk = 10;
   level.amoebe_inhalt = EL_DIAMANT;
 
+  level.high_speed = FALSE;
+
   strcpy(level.name, "Nameless Level");
 
   for(i=0; i<LEVEL_SCORE_ELEMENTS; i++)
     level.score[i] = 10;
 
-  for(i=0; i<4; i++)
+  MampferMax = 4;
+  for(i=0; i<8; i++)
     for(x=0; x<3; x++)
       for(y=0; y<3; y++)
        level.mampfer_inhalt[i][x][y] = EL_FELSBROCKEN;
@@ -266,6 +280,8 @@ static void setLevelInfoToDefaults()
   Feld[0][0] = Ur[0][0] = EL_SPIELFIGUR;
   Feld[STD_LEV_FIELDX-1][STD_LEV_FIELDY-1] =
     Ur[STD_LEV_FIELDX-1][STD_LEV_FIELDY-1] = EL_AUSGANG_ZU;
+
+  BorderElement = EL_BETON;
 }
 
 void LoadLevel(int level_nr)
@@ -329,10 +345,20 @@ void LoadLevel(int level_nr)
   for(i=0; i<LEVEL_SCORE_ELEMENTS; i++)
     level.score[i]     = fgetc(file);
 
-  for(i=0; i<4; i++)
+  MampferMax = 4;
+  for(i=0; i<8; i++)
+  {
     for(y=0; y<3; y++)
+    {
       for(x=0; x<3; x++)
-       level.mampfer_inhalt[i][x][y] = fgetc(file);
+      {
+       if (i < 4)
+         level.mampfer_inhalt[i][x][y] = fgetc(file);
+       else
+         level.mampfer_inhalt[i][x][y] = EL_LEERRAUM;
+      }
+    }
+  }
 
   level.tempo_amoebe   = fgetc(file);
   level.dauer_sieb     = fgetc(file);
@@ -345,10 +371,29 @@ void LoadLevel(int level_nr)
   /* read chunk "BODY" */
   if (file_version >= FILE_VERSION_1_2)
   {
-    /* next check body chunk identifier and chunk length */
     fgets(chunk, CHUNK_ID_LEN + 1, file);
     chunk_length =
       (fgetc(file)<<24) | (fgetc(file)<<16) | (fgetc(file)<<8) | fgetc(file);
+
+    /* look for optional content chunk */
+    if (strcmp(chunk, "CONT") == 0 && chunk_length == 4 + 8 * 3 * 3)
+    {
+      fgetc(file);
+      MampferMax = fgetc(file);
+      fgetc(file);
+      fgetc(file);
+
+      for(i=0; i<8; i++)
+       for(y=0; y<3; y++)
+         for(x=0; x<3; x++)
+           level.mampfer_inhalt[i][x][y] = fgetc(file);
+
+      fgets(chunk, CHUNK_ID_LEN + 1, file);
+      chunk_length =
+       (fgetc(file)<<24) | (fgetc(file)<<16) | (fgetc(file)<<8) | fgetc(file);
+    }
+
+    /* next check body chunk identifier and chunk length */
     if (strcmp(chunk, "BODY") || chunk_length != lev_fieldx * lev_fieldy)
     {
       Error(ERR_WARN, "wrong 'BODY' chunk of level file '%s'", filename);
@@ -357,14 +402,25 @@ void LoadLevel(int level_nr)
     }
   }
 
-  for(y=0; y<lev_fieldy; y++) 
-    for(x=0; x<lev_fieldx; x++) 
+  for(y=0; y<lev_fieldy; y++)
+    for(x=0; x<lev_fieldx; x++)
       Feld[x][y] = Ur[x][y] = fgetc(file);
 
   fclose(file);
 
+#if 0
   if (level.time <= 10)                /* minimum playing time of each level */
     level.time = 10;
+#endif
+
+  if (file_version == FILE_VERSION_1_0 &&
+      leveldir[leveldir_nr].sort_priority >= LEVELCLASS_CONTRIBUTION_START &&
+      leveldir[leveldir_nr].sort_priority <= LEVELCLASS_CONTRIBUTION_END)
+  {
+    Error(ERR_WARN, "level file '%s' has version number 1.0", filename);
+    Error(ERR_WARN, "using high speed movement for player");
+    level.high_speed = TRUE;
+  }
 }
 
 void SaveLevel(int level_nr)
@@ -415,6 +471,25 @@ void SaveLevel(int level_nr)
   for(i=0; i<LEVEL_HEADER_UNUSED; i++) /* set unused header bytes to zero */
     fputc(0, file);
 
+  fputs("CONT", file);                 /* chunk identifier for contents */
+
+  chunk_length = 4 + 8 * 3 * 3;
+
+  fputc((chunk_length >>  24) & 0xff, file);
+  fputc((chunk_length >>  16) & 0xff, file);
+  fputc((chunk_length >>   8) & 0xff, file);
+  fputc((chunk_length >>   0) & 0xff, file);
+
+  fputc(EL_MAMPFER, file);
+  fputc(MampferMax, file);
+  fputc(0, file);
+  fputc(0, file);
+
+  for(i=0; i<8; i++)
+    for(y=0; y<3; y++)
+      for(x=0; x<3; x++)
+       fputc(level.mampfer_inhalt[i][x][y], file);
+
   fputs("BODY", file);                 /* chunk identifier for file body */
   chunk_length = lev_fieldx * lev_fieldy;
 
@@ -443,6 +518,17 @@ void LoadTape(int level_nr)
   int file_version = FILE_VERSION_1_2; /* last version of tape files */
   int chunk_length;
 
+  /* always start with reliable default values (empty tape) */
+  TapeErase();
+
+  /* default values (also for pre-1.2 tapes) with only the first player */
+  tape.player_participates[0] = TRUE;
+  for(i=1; i<MAX_PLAYERS; i++)
+    tape.player_participates[i] = FALSE;
+
+  /* at least one (default: the first) player participates in every tape */
+  num_participating_players = 1;
+
   if (!(file = fopen(filename, "r")))
     return;
 
@@ -491,7 +577,7 @@ void LoadTape(int level_nr)
     for(i=0; i<TAPE_HEADER_UNUSED; i++)                /* skip unused header bytes */
       fgetc(file);
 
-    /* check which players participate in this tape recording */
+    /* since version 1.2, tapes store which players participate in the tape */
     num_participating_players = 0;
     for(i=0; i<MAX_PLAYERS; i++)
     {
@@ -538,10 +624,6 @@ void LoadTape(int level_nr)
     {
       tape.pos[i].action[j] = MV_NO_MOVING;
 
-      /* pre-1.2 tapes store data for only one player */
-      if (file_version == FILE_VERSION_1_0 && j > 0)
-       continue;
-
       if (tape.player_participates[j])
        tape.pos[i].action[j] = fgetc(file);
     }
@@ -806,8 +888,9 @@ void SaveScore(int level_nr)
 /* level directory info */
 #define LEVELINFO_TOKEN_NAME           29
 #define LEVELINFO_TOKEN_LEVELS         30
-#define LEVELINFO_TOKEN_SORT_PRIORITY  31
-#define LEVELINFO_TOKEN_READONLY       32
+#define LEVELINFO_TOKEN_FIRST_LEVEL    31
+#define LEVELINFO_TOKEN_SORT_PRIORITY  32
+#define LEVELINFO_TOKEN_READONLY       33
 
 #define FIRST_GLOBAL_SETUP_TOKEN       SETUP_TOKEN_PLAYER_NAME
 #define LAST_GLOBAL_SETUP_TOKEN                SETUP_TOKEN_TEAM_MODE
@@ -870,6 +953,7 @@ static struct
   /* level directory info */
   { TYPE_STRING,  &ldi.name,           "name"                          },
   { TYPE_INTEGER, &ldi.levels,         "levels"                        },
+  { TYPE_INTEGER, &ldi.first_level,    "first_level"                   },
   { TYPE_INTEGER, &ldi.sort_priority,  "sort_priority"                 },
   { TYPE_BOOLEAN, &ldi.readonly,       "readonly"                      }
 };
@@ -1133,7 +1217,8 @@ static void setLevelDirInfoToDefaults(struct LevelDirInfo *ldi)
 {
   ldi->name = getStringCopy("non-existing");
   ldi->levels = 0;
-  ldi->sort_priority = 999;    /* default: least priority */
+  ldi->first_level = 0;
+  ldi->sort_priority = LEVELCLASS_UNDEFINED;   /* default: least priority */
   ldi->readonly = TRUE;
 }
 
@@ -1144,16 +1229,16 @@ static void setSetupInfoToDefaults(struct SetupInfo *si)
   si->player_name = getStringCopy(getLoginName());
 
   si->sound = TRUE;
-  si->sound_loops = FALSE;
-  si->sound_music = FALSE;
-  si->sound_simple = FALSE;
+  si->sound_loops = TRUE;
+  si->sound_music = TRUE;
+  si->sound_simple = TRUE;
   si->toons = TRUE;
   si->double_buffering = TRUE;
   si->direct_draw = !si->double_buffering;
-  si->scroll_delay = FALSE;
+  si->scroll_delay = TRUE;
   si->soft_scrolling = TRUE;
   si->fading = FALSE;
-  si->autorecord = FALSE;
+  si->autorecord = TRUE;
   si->quick_doors = FALSE;
 
   for (i=0; i<MAX_PLAYERS; i++)
@@ -1271,14 +1356,12 @@ int getLastPlayedLevelOfLevelSeries(char *level_series_name)
 
   if (token_value)
   {
-    int highest_level_nr = leveldir[level_series_nr].levels - 1;
-
     last_level_nr = atoi(token_value);
 
-    if (last_level_nr < 0)
-      last_level_nr = 0;
-    if (last_level_nr > highest_level_nr)
-      last_level_nr = highest_level_nr;
+    if (last_level_nr < leveldir[level_series_nr].first_level)
+      last_level_nr = leveldir[level_series_nr].first_level;
+    if (last_level_nr > leveldir[level_series_nr].last_level)
+      last_level_nr = leveldir[level_series_nr].last_level;
   }
 
   return last_level_nr;
@@ -1355,6 +1438,9 @@ static int LoadLevelInfoFromLevelDir(char *level_directory, int start_entry)
       leveldir[current_entry] = ldi;
 
       leveldir[current_entry].filename = getStringCopy(dir_entry->d_name);
+      leveldir[current_entry].last_level =
+       leveldir[current_entry].first_level +
+       leveldir[current_entry].levels - 1;
       leveldir[current_entry].user_defined =
        (level_directory == options.level_directory ? FALSE : TRUE);
 
@@ -1418,7 +1504,8 @@ static void SaveUserLevelInfo()
 
   ldi.name = getLoginName();
   ldi.levels = 100;
-  ldi.sort_priority = 300;
+  ldi.first_level = 0;
+  ldi.sort_priority = LEVELCLASS_USER_START;
   ldi.readonly = FALSE;
 
   fprintf(file, "%s\n\n",