rnd-20030802-2-src
[rocksndiamonds.git] / src / libgame / setup.c
index 24e2471b723632f30eff5a570000d2c63973a6a8..20160fc7baed3682ea55cb71b019886cce0ec534 100644 (file)
@@ -98,25 +98,25 @@ static char *levelclass_desc[NUM_LEVELCLASS_DESC] =
 
 #define MAX_COOKIE_LEN                 256
 
-#define ARTWORKINFO_FILENAME(type)     ((type) == TREE_TYPE_GRAPHICS_DIR ? \
-                                        GRAPHICSINFO_FILENAME :            \
-                                        (type) == TREE_TYPE_SOUNDS_DIR ?   \
-                                        SOUNDSINFO_FILENAME :              \
-                                        (type) == TREE_TYPE_MUSIC_DIR ?    \
+#define ARTWORKINFO_FILENAME(type)     ((type) == ARTWORK_TYPE_GRAPHICS ? \
+                                        GRAPHICSINFO_FILENAME :           \
+                                        (type) == ARTWORK_TYPE_SOUNDS ?   \
+                                        SOUNDSINFO_FILENAME :             \
+                                        (type) == ARTWORK_TYPE_MUSIC ?    \
                                         MUSICINFO_FILENAME : "")
 
-#define ARTWORK_DIRECTORY(type)                ((type) == TREE_TYPE_GRAPHICS_DIR ? \
-                                        GRAPHICS_DIRECTORY :               \
-                                        (type) == TREE_TYPE_SOUNDS_DIR ?   \
-                                        SOUNDS_DIRECTORY :                 \
-                                        (type) == TREE_TYPE_MUSIC_DIR ?    \
+#define ARTWORK_DIRECTORY(type)                ((type) == ARTWORK_TYPE_GRAPHICS ? \
+                                        GRAPHICS_DIRECTORY :              \
+                                        (type) == ARTWORK_TYPE_SOUNDS ?   \
+                                        SOUNDS_DIRECTORY :                \
+                                        (type) == ARTWORK_TYPE_MUSIC ?    \
                                         MUSIC_DIRECTORY : "")
 
-#define OPTIONS_ARTWORK_DIRECTORY(type)        ((type) == TREE_TYPE_GRAPHICS_DIR ? \
-                                        options.graphics_directory :       \
-                                        (type) == TREE_TYPE_SOUNDS_DIR ?   \
-                                        options.sounds_directory :         \
-                                        (type) == TREE_TYPE_MUSIC_DIR ?    \
+#define OPTIONS_ARTWORK_DIRECTORY(type)        ((type) == ARTWORK_TYPE_GRAPHICS ? \
+                                        options.graphics_directory :      \
+                                        (type) == ARTWORK_TYPE_SOUNDS ?   \
+                                        options.sounds_directory :        \
+                                        (type) == ARTWORK_TYPE_MUSIC ?    \
                                         options.music_directory : "")
 
 
@@ -268,6 +268,13 @@ static char *getDefaultMusicDir(char *music_subdir)
   return music_dir;
 }
 
+static char *getDefaultArtworkSet(int type)
+{
+  return (type == TREE_TYPE_GRAPHICS_DIR ? GRAPHICS_SUBDIR :
+         type == TREE_TYPE_SOUNDS_DIR   ? SOUNDS_SUBDIR   :
+         type == TREE_TYPE_MUSIC_DIR    ? MUSIC_SUBDIR    : "");
+}
+
 static char *getDefaultArtworkDir(int type)
 {
   return (type == TREE_TYPE_GRAPHICS_DIR ?
@@ -322,27 +329,42 @@ static char *getSetupArtworkDir(TreeInfo *ti)
 
 void setLevelArtworkDir(TreeInfo *ti)
 {
-  char **artwork_path_ptr, *artwork_set;
+  char **artwork_path_ptr, **artwork_set_ptr;
   TreeInfo *level_artwork;
 
+  TreeInfo *tst1 = getTreeInfoFromIdentifier(leveldir_first, "jue1");
+  printf("::: XXX 0.1 '%s'\n", tst1->graphics_path);
+
   if (ti == NULL || leveldir_current == NULL)
     return;
 
-  artwork_path_ptr =
-    (ti->type == TREE_TYPE_GRAPHICS_DIR ? &leveldir_current->graphics_path :
-     ti->type == TREE_TYPE_SOUNDS_DIR   ? &leveldir_current->sounds_path :
-     &leveldir_current->music_path);
+  artwork_path_ptr = &(LEVELDIR_ARTWORK_PATH(leveldir_current, ti->type));
+  artwork_set_ptr  = &(LEVELDIR_ARTWORK_SET( leveldir_current, ti->type));
+
+  printf("::: ['%s', '%s']\n", tst1->identifier, leveldir_current->identifier);
 
-  artwork_set =
-    (ti->type == TREE_TYPE_GRAPHICS_DIR ? leveldir_current->graphics_set :
-     ti->type == TREE_TYPE_SOUNDS_DIR   ? leveldir_current->sounds_set :
-     leveldir_current->music_set);
+  printf("::: XXX 0.2 '%s' [%x]\n", tst1->graphics_path, tst1->graphics_path);
 
+#if 1
   if (*artwork_path_ptr != NULL)
+  {
+    if (ti->type == 0)
+      printf("::: free'ing '%s' [%x] [%x] [type %d] ...\n",
+            *artwork_path_ptr, *artwork_path_ptr,
+            leveldir_current->graphics_path, ti->type);
+
     free(*artwork_path_ptr);
+  }
+#endif
 
-  if ((level_artwork = getTreeInfoFromIdentifier(ti, artwork_set)))
+  printf("::: XXX 0.3 '%s' [%x]\n", tst1->graphics_path, tst1->graphics_path);
+
+  if ((level_artwork = getTreeInfoFromIdentifier(ti, *artwork_set_ptr)))
+  {
     *artwork_path_ptr = getStringCopy(getSetupArtworkDir(level_artwork));
+
+    printf(":1: setting to '%s' [type %d] ...\n", *artwork_path_ptr, ti->type);
+  }
   else
   {
     /* No (or non-existing) artwork configured in "levelinfo.conf". This would
@@ -354,29 +376,46 @@ void setLevelArtworkDir(TreeInfo *ti)
 
     char *dir = getPath2(getCurrentLevelDir(), ARTWORK_DIRECTORY(ti->type));
 
+    if (*artwork_set_ptr != NULL)
+      free(*artwork_set_ptr);
+
     if (fileExists(dir))
+    {
       *artwork_path_ptr = getStringCopy(getDefaultArtworkDir(ti->type));
+      *artwork_set_ptr = getStringCopy(getDefaultArtworkSet(ti->type));
+
+      if (ti->type == 0)
+       printf(":2: setting to '%s' [type %d] ...\n",
+              *artwork_path_ptr, ti->type);
+    }
     else
+    {
       *artwork_path_ptr = getStringCopy(UNDEFINED_FILENAME);
+      *artwork_set_ptr = NULL;
+
+      if (ti->type == 0)
+       printf(":3: setting to '%s' [type %d] ...\n",
+              *artwork_path_ptr, ti->type);
+    }
 
     free(dir);
   }
 }
 
-static char *getLevelArtworkDir(int type)
+inline static char *getLevelArtworkSet(int type)
 {
-  char *artwork_path;
+  if (leveldir_current == NULL)
+    return NULL;
+
+  return LEVELDIR_ARTWORK_SET(leveldir_current, type);
+}
 
+inline static char *getLevelArtworkDir(int type)
+{
   if (leveldir_current == NULL)
     return UNDEFINED_FILENAME;
 
-  artwork_path =
-    (type == TREE_TYPE_GRAPHICS_DIR ? leveldir_current->graphics_path :
-     type == TREE_TYPE_SOUNDS_DIR   ? leveldir_current->sounds_path :
-     type == TREE_TYPE_MUSIC_DIR    ? leveldir_current->music_path :
-     UNDEFINED_FILENAME);
-
-  return artwork_path;
+  return LEVELDIR_ARTWORK_PATH(leveldir_current, type);
 }
 
 char *getLevelFilename(int nr)
@@ -471,6 +510,7 @@ static char *getCorrectedImageBasename(char *basename)
 char *getCustomImageFilename(char *basename)
 {
   static char *filename = NULL;
+  boolean skip_setup_artwork = FALSE;
 
   if (filename != NULL)
     free(filename);
@@ -479,61 +519,40 @@ char *getCustomImageFilename(char *basename)
 
   if (!setup.override_level_graphics)
   {
-#if 0
-    /* 1st try: look for special artwork configured in level series config */
-    filename = getPath2(getLevelArtworkDir(TREE_TYPE_GRAPHICS_DIR), basename);
-#if 0
-    if (strcmp(basename, "RocksScreen.pcx") == 0)
-      printf("::: trying 1 '%s' ...\n", filename);
-#endif
+    /* 1st try: look for special artwork in current level series directory */
+    filename = getPath3(getCurrentLevelDir(), GRAPHICS_DIRECTORY, basename);
     if (fileExists(filename))
       return filename;
 
     free(filename);
-#endif
 
-    /* 2nd try: look for special artwork in current level series directory */
-    filename = getPath3(getCurrentLevelDir(), GRAPHICS_DIRECTORY, basename);
-#if 0
-    if (strcmp(basename, "RocksScreen.pcx") == 0)
-      printf("::: trying 2 '%s' ...\n", filename);
-#endif
-    if (fileExists(filename))
-      return filename;
+    /* check if there is special artwork configured in level series config */
+    if (getLevelArtworkSet(ARTWORK_TYPE_GRAPHICS) != NULL)
+    {
+      /* 2nd try: look for special artwork configured in level series config */
+      filename = getPath2(getLevelArtworkDir(ARTWORK_TYPE_GRAPHICS), basename);
+      if (fileExists(filename))
+       return filename;
 
-    free(filename);
+      free(filename);
 
-#if 1
-    /* 1st try: look for special artwork configured in level series config */
-    filename = getPath2(getLevelArtworkDir(TREE_TYPE_GRAPHICS_DIR), basename);
-#if 0
-    if (strcmp(basename, "RocksScreen.pcx") == 0)
-      printf("::: trying 2.1 '%s' ...\n", filename);
-#endif
+      /* take missing artwork configured in level set config from default */
+      skip_setup_artwork = TRUE;
+    }
+  }
+
+  if (!skip_setup_artwork)
+  {
+    /* 3rd try: look for special artwork in configured artwork directory */
+    filename = getPath2(getSetupArtworkDir(artwork.gfx_current), basename);
     if (fileExists(filename))
       return filename;
 
     free(filename);
-#endif
   }
 
-  /* 3rd try: look for special artwork in configured artwork directory */
-  filename = getPath2(getSetupArtworkDir(artwork.gfx_current), basename);
-#if 0
-  if (strcmp(basename, "RocksScreen.pcx") == 0)
-    printf("::: trying 3 '%s' ...\n", filename);
-#endif
-  if (fileExists(filename))
-    return filename;
-
-  free(filename);
-
   /* 4th try: look for default artwork in new default artwork directory */
   filename = getPath2(getDefaultGraphicsDir(GRAPHICS_SUBDIR), basename);
-#if 0
-  if (strcmp(basename, "RocksScreen.pcx") == 0)
-    printf("::: trying 4 '%s' ...\n", filename);
-#endif
   if (fileExists(filename))
     return filename;
 
@@ -541,10 +560,6 @@ char *getCustomImageFilename(char *basename)
 
   /* 5th try: look for default artwork in old default artwork directory */
   filename = getPath2(options.graphics_directory, basename);
-#if 0
-  if (strcmp(basename, "RocksScreen.pcx") == 0)
-    printf("::: trying 5 '%s' ...\n", filename);
-#endif
   if (fileExists(filename))
     return filename;
 
@@ -554,45 +569,45 @@ char *getCustomImageFilename(char *basename)
 char *getCustomSoundFilename(char *basename)
 {
   static char *filename = NULL;
+  boolean skip_setup_artwork = FALSE;
 
   if (filename != NULL)
     free(filename);
 
   if (!setup.override_level_sounds)
   {
-#if 0
-    /* 1st try: look for special artwork configured in level series config */
-    filename = getPath2(getLevelArtworkDir(TREE_TYPE_SOUNDS_DIR), basename);
+    /* 1st try: look for special artwork in current level series directory */
+    filename = getPath3(getCurrentLevelDir(), SOUNDS_DIRECTORY, basename);
     if (fileExists(filename))
       return filename;
 
     free(filename);
-#endif
 
-    /* 2nd try: look for special artwork in current level series directory */
-    filename = getPath3(getCurrentLevelDir(), SOUNDS_DIRECTORY, basename);
-    if (fileExists(filename))
-      return filename;
+    /* check if there is special artwork configured in level series config */
+    if (getLevelArtworkSet(ARTWORK_TYPE_SOUNDS) != NULL)
+    {
+      /* 2nd try: look for special artwork configured in level series config */
+      filename = getPath2(getLevelArtworkDir(TREE_TYPE_SOUNDS_DIR), basename);
+      if (fileExists(filename))
+       return filename;
 
-    free(filename);
+      free(filename);
 
-#if 1
-    /* 1st try: look for special artwork configured in level series config */
-    filename = getPath2(getLevelArtworkDir(TREE_TYPE_SOUNDS_DIR), basename);
+      /* take missing artwork configured in level set config from default */
+      skip_setup_artwork = TRUE;
+    }
+  }
+
+  if (!skip_setup_artwork)
+  {
+    /* 3rd try: look for special artwork in configured artwork directory */
+    filename = getPath2(getSetupArtworkDir(artwork.snd_current), basename);
     if (fileExists(filename))
       return filename;
 
     free(filename);
-#endif
   }
 
-  /* 3rd try: look for special artwork in configured artwork directory */
-  filename = getPath2(getSetupArtworkDir(artwork.snd_current), basename);
-  if (fileExists(filename))
-    return filename;
-
-  free(filename);
-
   /* 4th try: look for default artwork in new default artwork directory */
   filename = getPath2(getDefaultSoundsDir(SOUNDS_SUBDIR), basename);
   if (fileExists(filename))
@@ -638,45 +653,45 @@ char *getCustomArtworkLevelConfigFilename(int type)
 char *getCustomMusicDirectory(void)
 {
   static char *directory = NULL;
+  boolean skip_setup_artwork = FALSE;
 
   if (directory != NULL)
     free(directory);
 
   if (!setup.override_level_music)
   {
-#if 0
-    /* 1st try: look for special artwork configured in level series config */
-    directory = getStringCopy(getLevelArtworkDir(TREE_TYPE_MUSIC_DIR));
+    /* 1st try: look for special artwork in current level series directory */
+    directory = getPath2(getCurrentLevelDir(), MUSIC_DIRECTORY);
     if (fileExists(directory))
       return directory;
 
     free(directory);
-#endif
 
-    /* 2nd try: look for special artwork in current level series directory */
-    directory = getPath2(getCurrentLevelDir(), MUSIC_DIRECTORY);
-    if (fileExists(directory))
-      return directory;
+    /* check if there is special artwork configured in level series config */
+    if (getLevelArtworkSet(ARTWORK_TYPE_MUSIC) != NULL)
+    {
+      /* 2nd try: look for special artwork configured in level series config */
+      directory = getStringCopy(getLevelArtworkDir(TREE_TYPE_MUSIC_DIR));
+      if (fileExists(directory))
+       return directory;
 
-    free(directory);
+      free(directory);
 
-#if 1
-    /* 1st try: look for special artwork configured in level series config */
-    directory = getStringCopy(getLevelArtworkDir(TREE_TYPE_MUSIC_DIR));
+      /* take missing artwork configured in level set config from default */
+      skip_setup_artwork = TRUE;
+    }
+  }
+
+  if (!skip_setup_artwork)
+  {
+    /* 3rd try: look for special artwork in configured artwork directory */
+    directory = getStringCopy(getSetupArtworkDir(artwork.mus_current));
     if (fileExists(directory))
       return directory;
 
     free(directory);
-#endif
   }
 
-  /* 3rd try: look for special artwork in configured artwork directory */
-  directory = getStringCopy(getSetupArtworkDir(artwork.mus_current));
-  if (fileExists(directory))
-    return directory;
-
-  free(directory);
-
   /* 4th try: look for default artwork in new default artwork directory */
   directory = getStringCopy(getDefaultMusicDir(MUSIC_SUBDIR));
   if (fileExists(directory))
@@ -1512,12 +1527,14 @@ static void setTreeInfoToDefaults(TreeInfo *ldi, int type)
   if (ldi->type == TREE_TYPE_LEVEL_DIR)
   {
     ldi->imported_from = NULL;
+
     ldi->graphics_set = NULL;
     ldi->sounds_set = NULL;
     ldi->music_set = NULL;
     ldi->graphics_path = getStringCopy(UNDEFINED_FILENAME);
     ldi->sounds_path = getStringCopy(UNDEFINED_FILENAME);
     ldi->music_path = getStringCopy(UNDEFINED_FILENAME);
+
     ldi->levels = 0;
     ldi->first_level = 0;
     ldi->last_level = 0;
@@ -1533,10 +1550,60 @@ static void setTreeInfoToDefaultsFromParent(TreeInfo *ldi, TreeInfo *parent)
   {
     Error(ERR_WARN, "setTreeInfoToDefaultsFromParent(): parent == NULL");
 
-    setTreeInfoToDefaults(ldi, TREE_TYPE_GENERIC);
+    setTreeInfoToDefaults(ldi, TREE_TYPE_UNDEFINED);
+
     return;
   }
 
+#if 1
+  /* copy all values from the parent structure */
+
+  ldi->type = parent->type;
+
+  ldi->node_top = parent->node_top;
+  ldi->node_parent = parent;
+  ldi->node_group = NULL;
+  ldi->next = NULL;
+
+  ldi->cl_first = -1;
+  ldi->cl_cursor = -1;
+
+  ldi->filename = NULL;
+  ldi->fullpath = NULL;
+  ldi->basepath = NULL;
+  ldi->identifier = NULL;
+  ldi->name = getStringCopy(ANONYMOUS_NAME);
+  ldi->name_sorting = NULL;
+  ldi->author = getStringCopy(parent->author);
+
+  ldi->sort_priority = parent->sort_priority;
+  ldi->parent_link = FALSE;
+  ldi->user_defined = parent->user_defined;
+  ldi->color = parent->color;
+  ldi->class_desc = getStringCopy(parent->class_desc);
+
+  if (ldi->type == TREE_TYPE_LEVEL_DIR)
+  {
+    ldi->imported_from = getStringCopy(parent->imported_from);
+
+    ldi->graphics_set = NULL;
+    ldi->sounds_set = NULL;
+    ldi->music_set = NULL;
+    ldi->graphics_path = getStringCopy(UNDEFINED_FILENAME);
+    ldi->sounds_path = getStringCopy(UNDEFINED_FILENAME);
+    ldi->music_path = getStringCopy(UNDEFINED_FILENAME);
+
+    ldi->levels = 0;
+    ldi->first_level = 0;
+    ldi->last_level = 0;
+    ldi->level_group = FALSE;
+    ldi->handicap_level = 0;
+    ldi->readonly = TRUE;
+  }
+
+
+#else
+
   /* first copy all values from the parent structure ... */
   *ldi = *parent;
 
@@ -1554,7 +1621,16 @@ static void setTreeInfoToDefaultsFromParent(TreeInfo *ldi, TreeInfo *parent)
   ldi->name = getStringCopy(ANONYMOUS_NAME);
   ldi->name_sorting = NULL;
   ldi->author = getStringCopy(parent->author);
+
   ldi->imported_from = getStringCopy(parent->imported_from);
+  ldi->class_desc = getStringCopy(parent->class_desc);
+
+  ldi->graphics_set = NULL;
+  ldi->sounds_set = NULL;
+  ldi->music_set = NULL;
+  ldi->graphics_path = NULL;
+  ldi->sounds_path = NULL;
+  ldi->music_path = NULL;
 
   ldi->level_group = FALSE;
   ldi->parent_link = FALSE;
@@ -1563,6 +1639,8 @@ static void setTreeInfoToDefaultsFromParent(TreeInfo *ldi, TreeInfo *parent)
   ldi->node_parent = parent;
   ldi->node_group = NULL;
   ldi->next = NULL;
+
+#endif
 }
 
 void setSetupInfo(struct TokenInfo *token_info,
@@ -1844,6 +1922,13 @@ void LoadLevelInfo()
   LoadLevelInfoFromLevelDir(&leveldir_first, NULL, options.level_directory);
   LoadLevelInfoFromLevelDir(&leveldir_first, NULL, getUserLevelDir(NULL));
 
+  {
+    TreeInfo *tst1 = getTreeInfoFromIdentifier(leveldir_first, "jue1");
+    TreeInfo *tst2 = getTreeInfoFromIdentifier(leveldir_first, "demojue");
+    printf("::: ??? 1 '%s' [%x, %x]\n", tst1->graphics_path,
+          tst1->graphics_path, tst2->graphics_path);
+  }
+
   /* before sorting, the first entries will be from the user directory */
   leveldir_current = getFirstValidTreeInfoEntry(leveldir_first);
 
@@ -2278,6 +2363,18 @@ static void SaveUserLevelInfo()
   /* always start with reliable default values */
   setTreeInfoToDefaults(&ldi, TREE_TYPE_LEVEL_DIR);
 
+#if 0
+  /* !!! FIX ME !!! */
+  setString(&ldi.name, getLoginName());
+  setString(&ldi.author, getRealName());
+  ldi.levels = 100;
+  ldi.first_level = 1;
+  ldi.sort_priority = LEVELCLASS_USER_START;
+  ldi.readonly = FALSE;
+  setString(&ldi.graphics_set, GRAPHICS_SUBDIR);
+  setString(&ldi.sounds_set, SOUNDS_SUBDIR);
+  setString(&ldi.music_set, MUSIC_SUBDIR);
+#else
   ldi.name = getStringCopy(getLoginName());
   ldi.author = getStringCopy(getRealName());
   ldi.levels = 100;
@@ -2287,6 +2384,7 @@ static void SaveUserLevelInfo()
   ldi.graphics_set = getStringCopy(GRAPHICS_SUBDIR);
   ldi.sounds_set = getStringCopy(SOUNDS_SUBDIR);
   ldi.music_set = getStringCopy(MUSIC_SUBDIR);
+#endif
 
   fprintf(file, "%s\n\n", getFormattedSetupEntry(TOKEN_STR_FILE_IDENTIFIER,
                                                 getCookie("LEVELINFO")));