rnd-20020402-3-src
[rocksndiamonds.git] / src / libgame / setup.c
index edfa4d35502891929c4a6cdbb1d50a515688b0bc..d9b62634ec488e870a60ef8b0e0af699da91e493 100644 (file)
@@ -462,7 +462,7 @@ TreeInfo *getTreeInfoFirstGroupEntry(TreeInfo *node)
     return NULL;
 
   if (node->node_parent == NULL)               /* top level group */
-    return leveldir_first;
+    return *node->node_top;
   else                                         /* sub level group */
     return node->node_parent->node_group;
 }
@@ -534,9 +534,9 @@ TreeInfo *getTreeInfoFromFilenameExt(TreeInfo *node, char *filename)
   return NULL;
 }
 
-TreeInfo *getTreeInfoFromFilename(char *filename)
+TreeInfo *getTreeInfoFromFilename(TreeInfo *ti, char *filename)
 {
-  return getTreeInfoFromFilenameExt(leveldir_first, filename);
+  return getTreeInfoFromFilenameExt(ti, filename);
 }
 
 void dumpTreeInfo(TreeInfo *node, int depth)
@@ -1076,6 +1076,17 @@ static struct TokenInfo levelinfo_tokens[] =
 
 static void setTreeInfoToDefaults(TreeInfo *ldi)
 {
+  /* ldi->type is expected to be already set! */
+
+  if (ldi->type == 0)
+    Error(ERR_EXIT, "ldi->type == 0");
+
+  ldi->node_top = (ldi->type == TREE_TYPE_LEVEL_DIR ? &leveldir_first :
+                  ldi->type == TREE_TYPE_GRAPHICS_DIR ? &artwork.gfx_first :
+                  ldi->type == TREE_TYPE_SOUNDS_DIR ? &artwork.snd_first :
+                  ldi->type == TREE_TYPE_MUSIC_DIR ? &artwork.mus_first :
+                  NULL);
+
   ldi->node_parent = NULL;
   ldi->node_group = NULL;
   ldi->next = NULL;
@@ -1083,8 +1094,6 @@ static void setTreeInfoToDefaults(TreeInfo *ldi)
   ldi->cl_first = -1;
   ldi->cl_cursor = -1;
 
-  ldi->type = TREE_TYPE_GENERIC;
-
   ldi->filename = NULL;
   ldi->fullpath = NULL;
   ldi->basepath = NULL;
@@ -1092,22 +1101,26 @@ static void setTreeInfoToDefaults(TreeInfo *ldi)
   ldi->name_short = NULL;
   ldi->name_sorting = NULL;
   ldi->author = getStringCopy(ANONYMOUS_NAME);
-  ldi->imported_from = NULL;
-  ldi->levels = 0;
-  ldi->first_level = 0;
-  ldi->last_level = 0;
+
   ldi->sort_priority = LEVELCLASS_UNDEFINED;   /* default: least priority */
-  ldi->level_group = FALSE;
   ldi->parent_link = FALSE;
   ldi->user_defined = FALSE;
-  ldi->readonly = TRUE;
   ldi->color = 0;
   ldi->class_desc = NULL;
-  ldi->handicap_level = 0;
+
+  if (ldi->type == TREE_TYPE_LEVEL_DIR)
+  {
+    ldi->imported_from = NULL;
+    ldi->levels = 0;
+    ldi->first_level = 0;
+    ldi->last_level = 0;
+    ldi->level_group = FALSE;
+    ldi->handicap_level = 0;
+    ldi->readonly = TRUE;
+  }
 }
 
-static void setTreeInfoToDefaultsFromParent(TreeInfo *ldi,
-                                           TreeInfo *parent)
+static void setTreeInfoToDefaultsFromParent(TreeInfo *ldi, TreeInfo *parent)
 {
   if (parent == NULL)
   {
@@ -1137,6 +1150,7 @@ static void setTreeInfoToDefaultsFromParent(TreeInfo *ldi,
   ldi->level_group = FALSE;
   ldi->parent_link = FALSE;
 
+  ldi->node_top = parent->node_top;
   ldi->node_parent = parent;
   ldi->node_group = NULL;
   ldi->next = NULL;
@@ -1210,24 +1224,30 @@ static int compareTreeInfoEntries(const void *object1, const void *object2)
 
 static void createParentTreeInfoNode(TreeInfo *node_parent)
 {
-  TreeInfo *leveldir_new = newTreeInfo();
+  TreeInfo *ti_new;
+
+  if (node_parent == NULL)
+    return;
 
-  setTreeInfoToDefaults(leveldir_new);
+  ti_new = newTreeInfo();
+  ti_new->type = node_parent->type;
 
-  leveldir_new->node_parent = node_parent;
-  leveldir_new->parent_link = TRUE;
+  setTreeInfoToDefaults(ti_new);
 
-  leveldir_new->name = ".. (parent directory)";
-  leveldir_new->name_short = getStringCopy(leveldir_new->name);
-  leveldir_new->name_sorting = getStringCopy(leveldir_new->name);
+  ti_new->node_parent = node_parent;
+  ti_new->parent_link = TRUE;
 
-  leveldir_new->filename = "..";
-  leveldir_new->fullpath = getStringCopy(node_parent->fullpath);
+  ti_new->name = ".. (parent directory)";
+  ti_new->name_short = getStringCopy(ti_new->name);
+  ti_new->name_sorting = getStringCopy(ti_new->name);
 
-  leveldir_new->sort_priority = node_parent->sort_priority;
-  leveldir_new->class_desc = getLevelClassDescription(leveldir_new);
+  ti_new->filename = "..";
+  ti_new->fullpath = getStringCopy(node_parent->fullpath);
+
+  ti_new->sort_priority = node_parent->sort_priority;
+  ti_new->class_desc = getLevelClassDescription(ti_new);
 
-  pushTreeInfo(&node_parent->node_group, leveldir_new);
+  pushTreeInfo(&node_parent->node_group, ti_new);
 }
 
 /* forward declaration for recursive call by "LoadLevelInfoFromLevelDir()" */
@@ -1255,6 +1275,7 @@ static boolean LoadLevelInfoFromLevelConf(TreeInfo **node_first,
   }
 
   leveldir_new = newTreeInfo();
+  leveldir_new->type = TREE_TYPE_LEVEL_DIR;
 
   checkSetupFileListIdentifier(setup_file_list, getCookie("LEVELINFO"));
   setTreeInfoToDefaultsFromParent(leveldir_new, node_parent);
@@ -1402,9 +1423,6 @@ void LoadLevelInfo()
 #endif
 }
 
-/* declaration for recursive call by "LoadArtworkInfoFromArtworkConf()" */
-static void LoadArtworkInfoFromArtworkDir(TreeInfo **, TreeInfo *, char *,int);
-
 static boolean LoadArtworkInfoFromArtworkConf(TreeInfo **node_first,
                                              TreeInfo *node_parent,
                                              char *base_directory,
@@ -1460,6 +1478,8 @@ static boolean LoadArtworkInfoFromArtworkConf(TreeInfo **node_first,
   }
 
   artwork_new = newTreeInfo();
+  artwork_new->type = type;
+
   setTreeInfoToDefaultsFromParent(artwork_new, node_parent);
 
   artwork_new->filename = getStringCopy(directory_name);
@@ -1575,6 +1595,7 @@ static void LoadArtworkInfoFromArtworkDir(TreeInfo **node_first,
 
     free(directory_path);
 
+    /* check if this directory contains artwork with or without config file */
     valid_entry_found |= LoadArtworkInfoFromArtworkConf(node_first,node_parent,
                                                        base_directory,
                                                        directory_name, type);
@@ -1582,14 +1603,10 @@ static void LoadArtworkInfoFromArtworkDir(TreeInfo **node_first,
 
   closedir(dir);
 
-  if (!valid_entry_found)
-  {
-    /* check if this directory directly contains an artwork config file */
-    valid_entry_found |= LoadArtworkInfoFromArtworkConf(node_first,node_parent,
-                                                       base_directory, ".",
-                                                       type);
-  }
-
+  /* check if this directory directly contains artwork itself */
+  valid_entry_found |= LoadArtworkInfoFromArtworkConf(node_first,node_parent,
+                                                     base_directory, ".",
+                                                     type);
   if (!valid_entry_found)
     Error(ERR_WARN, "cannot find any valid artwork in directory '%s'",
          base_directory);
@@ -1773,7 +1790,8 @@ void LoadLevelSetup_LastSeries()
     char *last_level_series =
       getTokenValue(level_setup_list, TOKEN_STR_LAST_LEVEL_SERIES);
 
-    leveldir_current = getTreeInfoFromFilename(last_level_series);
+    leveldir_current = getTreeInfoFromFilename(leveldir_first,
+                                              last_level_series);
     if (leveldir_current == NULL)
       leveldir_current = leveldir_first;