rnd-20020824-1-src
[rocksndiamonds.git] / src / libgame / setup.c
index 27c05e55d16e00d5ba87069a21b43701355da1e2..add159b92932fe9cce781c31b748c1523bb30366 100644 (file)
@@ -11,8 +11,9 @@
 * setup.c                                                  *
 ***********************************************************/
 
-#include <dirent.h>
+#include <sys/types.h>
 #include <sys/stat.h>
+#include <dirent.h>
 #include <string.h>
 #include <unistd.h>
 
@@ -79,6 +80,18 @@ static char *levelclass_desc[NUM_LEVELCLASS_DESC] =
                         IS_LEVELCLASS_USER(n) ?                7 : \
                         9)
 
+#define ARTWORKCOLOR(n)        (IS_ARTWORKCLASS_CLASSICS(n) ?          FC_RED : \
+                        IS_ARTWORKCLASS_CONTRIBUTION(n) ?      FC_YELLOW : \
+                        IS_ARTWORKCLASS_LEVEL(n) ?             FC_GREEN : \
+                        IS_ARTWORKCLASS_USER(n) ?              FC_RED : \
+                        FC_BLUE)
+
+#define ARTWORKSORTING(n) (IS_ARTWORKCLASS_CLASSICS(n) ?       0 : \
+                        IS_ARTWORKCLASS_CONTRIBUTION(n) ?      1 : \
+                        IS_ARTWORKCLASS_LEVEL(n) ?             2 : \
+                        IS_ARTWORKCLASS_USER(n) ?              3 : \
+                        9)
+
 #define TOKEN_VALUE_POSITION           40
 #define TOKEN_COMMENT_POSITION         60
 
@@ -516,50 +529,6 @@ void InitLevelSetupDirectory(char *level_subdir)
   createDirectory(getLevelSetupDir(level_subdir), "level setup",PERMS_PRIVATE);
 }
 
-void ReadChunk_VERS(FILE *file, int *file_version, int *game_version)
-{
-  int file_version_major, file_version_minor, file_version_patch;
-  int game_version_major, game_version_minor, game_version_patch;
-
-  file_version_major = fgetc(file);
-  file_version_minor = fgetc(file);
-  file_version_patch = fgetc(file);
-  fgetc(file);         /* not used */
-
-  game_version_major = fgetc(file);
-  game_version_minor = fgetc(file);
-  game_version_patch = fgetc(file);
-  fgetc(file);         /* not used */
-
-  *file_version = VERSION_IDENT(file_version_major,
-                               file_version_minor,
-                               file_version_patch);
-
-  *game_version = VERSION_IDENT(game_version_major,
-                               game_version_minor,
-                               game_version_patch);
-}
-
-void WriteChunk_VERS(FILE *file, int file_version, int game_version)
-{
-  int file_version_major = VERSION_MAJOR(file_version);
-  int file_version_minor = VERSION_MINOR(file_version);
-  int file_version_patch = VERSION_PATCH(file_version);
-  int game_version_major = VERSION_MAJOR(game_version);
-  int game_version_minor = VERSION_MINOR(game_version);
-  int game_version_patch = VERSION_PATCH(game_version);
-
-  fputc(file_version_major, file);
-  fputc(file_version_minor, file);
-  fputc(file_version_patch, file);
-  fputc(0, file);      /* not used */
-
-  fputc(game_version_major, file);
-  fputc(game_version_minor, file);
-  fputc(game_version_patch, file);
-  fputc(0, file);      /* not used */
-}
-
 
 /* ------------------------------------------------------------------------- */
 /* some functions to handle lists of level directories                       */
@@ -597,13 +566,9 @@ boolean validLevelSeries(TreeInfo *node)
 TreeInfo *getFirstValidTreeInfoEntry(TreeInfo *node)
 {
   if (node == NULL)
-  {
-    if (node->node_top)                /* start with first tree entry */
-      return getFirstValidTreeInfoEntry(*node->node_top);
-    else
-      return NULL;
-  }
-  else if (node->node_group)   /* enter level group (step down into tree) */
+    return NULL;
+
+  if (node->node_group)                /* enter level group (step down into tree) */
     return getFirstValidTreeInfoEntry(node->node_group);
   else if (node->parent_link)  /* skip start entry of level group */
   {
@@ -666,9 +631,9 @@ TreeInfo *getTreeInfoFromPos(TreeInfo *node, int pos)
   return node_default;
 }
 
-TreeInfo *getTreeInfoFromFilenameExt(TreeInfo *node, char *filename)
+TreeInfo *getTreeInfoFromIdentifier(TreeInfo *node, char *identifier)
 {
-  if (filename == NULL)
+  if (identifier == NULL)
     return NULL;
 
   while (node)
@@ -677,14 +642,14 @@ TreeInfo *getTreeInfoFromFilenameExt(TreeInfo *node, char *filename)
     {
       TreeInfo *node_group;
 
-      node_group = getTreeInfoFromFilenameExt(node->node_group, filename);
+      node_group = getTreeInfoFromIdentifier(node->node_group, identifier);
 
       if (node_group)
        return node_group;
     }
     else if (!node->parent_link)
     {
-      if (strcmp(filename, node->filename) == 0)
+      if (strcmp(identifier, node->identifier) == 0)
        return node;
     }
 
@@ -694,11 +659,6 @@ TreeInfo *getTreeInfoFromFilenameExt(TreeInfo *node, char *filename)
   return NULL;
 }
 
-TreeInfo *getTreeInfoFromFilename(TreeInfo *ti, char *filename)
-{
-  return getTreeInfoFromFilenameExt(ti, filename);
-}
-
 void dumpTreeInfo(TreeInfo *node, int depth)
 {
   int i;
@@ -710,8 +670,8 @@ void dumpTreeInfo(TreeInfo *node, int depth)
     for (i=0; i<(depth + 1) * 3; i++)
       printf(" ");
 
-    printf("filename == '%s' (%s) [%s]\n",
-          node->filename, node->name, node->name_short);
+    printf("filename == '%s' (%s) [%s] (%d)\n",
+          node->filename, node->name, node->identifier, node->sort_priority);
 
     if (node->node_group != NULL)
       dumpTreeInfo(node->node_group, depth + 1);
@@ -1208,8 +1168,8 @@ void checkSetupFileListIdentifier(struct SetupFileList *setup_file_list,
 #define TOKEN_STR_HANDICAP_LEVEL       "handicap_level"
 
 /* level directory info */
-#define LEVELINFO_TOKEN_NAME           0
-#define LEVELINFO_TOKEN_NAME_SHORT     1
+#define LEVELINFO_TOKEN_IDENTIFIER     0
+#define LEVELINFO_TOKEN_NAME           1
 #define LEVELINFO_TOKEN_NAME_SORTING   2
 #define LEVELINFO_TOKEN_AUTHOR         3
 #define LEVELINFO_TOKEN_IMPORTED_FROM  4
@@ -1226,8 +1186,8 @@ static LevelDirTree ldi;
 static struct TokenInfo levelinfo_tokens[] =
 {
   /* level directory info */
+  { TYPE_STRING,  &ldi.identifier,     "identifier"    },
   { TYPE_STRING,  &ldi.name,           "name"          },
-  { TYPE_STRING,  &ldi.name_short,     "name_short"    },
   { TYPE_STRING,  &ldi.name_sorting,   "name_sorting"  },
   { TYPE_STRING,  &ldi.author,         "author"        },
   { TYPE_STRING,  &ldi.imported_from,  "imported_from" },
@@ -1258,8 +1218,8 @@ static void setTreeInfoToDefaults(TreeInfo *ldi, int type)
   ldi->filename = NULL;
   ldi->fullpath = NULL;
   ldi->basepath = NULL;
+  ldi->identifier = NULL;
   ldi->name = getStringCopy(ANONYMOUS_NAME);
-  ldi->name_short = NULL;
   ldi->name_sorting = NULL;
   ldi->author = getStringCopy(ANONYMOUS_NAME);
 
@@ -1304,8 +1264,8 @@ static void setTreeInfoToDefaultsFromParent(TreeInfo *ldi, TreeInfo *parent)
   ldi->filename = NULL;
   ldi->fullpath = NULL;
   ldi->basepath = NULL;
+  ldi->identifier = NULL;
   ldi->name = getStringCopy(ANONYMOUS_NAME);
-  ldi->name_short = NULL;
   ldi->name_sorting = NULL;
   ldi->author = getStringCopy(parent->author);
   ldi->imported_from = getStringCopy(parent->imported_from);
@@ -1363,8 +1323,20 @@ static int compareTreeInfoEntries(const void *object1, const void *object2)
 {
   const TreeInfo *entry1 = *((TreeInfo **)object1);
   const TreeInfo *entry2 = *((TreeInfo **)object2);
+  int class_sorting1, class_sorting2;
   int compare_result;
 
+  if (entry1->type == TREE_TYPE_LEVEL_DIR)
+  {
+    class_sorting1 = LEVELSORTING(entry1);
+    class_sorting2 = LEVELSORTING(entry2);
+  }
+  else
+  {
+    class_sorting1 = ARTWORKSORTING(entry1);
+    class_sorting2 = ARTWORKSORTING(entry2);
+  }
+
   if (entry1->parent_link || entry2->parent_link)
     compare_result = (entry1->parent_link ? -1 : +1);
   else if (entry1->sort_priority == entry2->sort_priority)
@@ -1377,10 +1349,10 @@ static int compareTreeInfoEntries(const void *object1, const void *object2)
     free(name1);
     free(name2);
   }
-  else if (LEVELSORTING(entry1) == LEVELSORTING(entry2))
+  else if (class_sorting1 == class_sorting2)
     compare_result = entry1->sort_priority - entry2->sort_priority;
   else
-    compare_result = LEVELSORTING(entry1) - LEVELSORTING(entry2);
+    compare_result = class_sorting1 - class_sorting2;
 
   return compare_result;
 }
@@ -1398,8 +1370,8 @@ static void createParentTreeInfoNode(TreeInfo *node_parent)
   ti_new->node_parent = node_parent;
   ti_new->parent_link = TRUE;
 
+  ti_new->identifier = getStringCopy(node_parent->identifier);
   ti_new->name = ".. (parent directory)";
-  ti_new->name_short = getStringCopy(ti_new->name);
   ti_new->name_sorting = getStringCopy(ti_new->name);
 
   ti_new->filename = "..";
@@ -1461,8 +1433,8 @@ static boolean LoadLevelInfoFromLevelConf(TreeInfo **node_first,
 
   DrawInitText(leveldir_new->name, 150, FC_YELLOW);
 
-  if (leveldir_new->name_short == NULL)
-    leveldir_new->name_short = getStringCopy(leveldir_new->name);
+  if (leveldir_new->identifier == NULL)
+    leveldir_new->identifier = getStringCopy(leveldir_new->filename);
 
   if (leveldir_new->name_sorting == NULL)
     leveldir_new->name_sorting = getStringCopy(leveldir_new->name);
@@ -1675,10 +1647,12 @@ static boolean LoadArtworkInfoFromArtworkConf(TreeInfo **node_first,
       artwork_new->name = getStringCopy(artwork_new->filename);
     }
 
+#if 0
     DrawInitText(artwork_new->name, 150, FC_YELLOW);
+#endif
 
-    if (artwork_new->name_short == NULL)
-      artwork_new->name_short = getStringCopy(artwork_new->name);
+    if (artwork_new->identifier == NULL)
+      artwork_new->identifier = getStringCopy(artwork_new->filename);
 
     if (artwork_new->name_sorting == NULL)
       artwork_new->name_sorting = getStringCopy(artwork_new->name);
@@ -1699,7 +1673,7 @@ static boolean LoadArtworkInfoFromArtworkConf(TreeInfo **node_first,
     (artwork_new->basepath == OPTIONS_ARTWORK_DIRECTORY(type) ? FALSE : TRUE);
 
   /* (may use ".sort_priority" from "setup_file_list" above) */
-  artwork_new->color = LEVELCOLOR(artwork_new);
+  artwork_new->color = ARTWORKCOLOR(artwork_new);
   artwork_new->class_desc = getLevelClassDescription(artwork_new);
 
   if (setup_file_list == NULL) /* (after determining ".user_defined") */
@@ -1711,25 +1685,30 @@ static boolean LoadArtworkInfoFromArtworkConf(TreeInfo **node_first,
     {
       if (artwork_new->user_defined)
       {
-       artwork_new->name = getStringCopy("private");
-       artwork_new->sort_priority = LEVELCLASS_USER;
+       artwork_new->identifier = getStringCopy("private");
+       artwork_new->sort_priority = ARTWORKCLASS_USER;
       }
       else
       {
-       artwork_new->name = getStringCopy("classic");
-       artwork_new->sort_priority = LEVELCLASS_CLASSICS;
+       artwork_new->identifier = getStringCopy("classic");
+       artwork_new->sort_priority = ARTWORKCLASS_CLASSICS;
       }
 
-      artwork_new->color = LEVELCOLOR(artwork_new);
+      /* set to new values after changing ".sort_priority" */
+      artwork_new->color = ARTWORKCOLOR(artwork_new);
       artwork_new->class_desc = getLevelClassDescription(artwork_new);
     }
     else
-      artwork_new->name = getStringCopy(artwork_new->filename);
+    {
+      artwork_new->identifier = getStringCopy(artwork_new->filename);
+    }
 
-    artwork_new->name_short = getStringCopy(artwork_new->name);
+    artwork_new->name = getStringCopy(artwork_new->identifier);
     artwork_new->name_sorting = getStringCopy(artwork_new->name);
   }
 
+  DrawInitText(artwork_new->name, 150, FC_YELLOW);
+
   pushTreeInfo(node_first, artwork_new);
 
   freeSetupFileList(setup_file_list);
@@ -1796,6 +1775,27 @@ static void LoadArtworkInfoFromArtworkDir(TreeInfo **node_first,
          base_directory);
 }
 
+static TreeInfo *getDummyArtworkInfo(int type)
+{
+  /* this is only needed when there is completely no artwork available */
+  TreeInfo *artwork_new = newTreeInfo();
+
+  setTreeInfoToDefaults(artwork_new, type);
+
+  artwork_new->filename = getStringCopy(NOT_AVAILABLE);
+  artwork_new->fullpath = getStringCopy(NOT_AVAILABLE);
+  artwork_new->basepath = getStringCopy(NOT_AVAILABLE);
+
+  if (artwork_new->name != NULL)
+    free(artwork_new->name);
+
+  artwork_new->identifier   = getStringCopy(NOT_AVAILABLE);
+  artwork_new->name         = getStringCopy(NOT_AVAILABLE);
+  artwork_new->name_sorting = getStringCopy(NOT_AVAILABLE);
+
+  return artwork_new;
+}
+
 void LoadArtworkInfo()
 {
   DrawInitText("Looking for custom artwork:", 120, FC_GREEN);
@@ -1821,30 +1821,37 @@ void LoadArtworkInfo()
                                getUserMusicDir(),
                                TREE_TYPE_MUSIC_DIR);
 
+  if (artwork.gfx_first == NULL)
+    artwork.gfx_first = getDummyArtworkInfo(TREE_TYPE_GRAPHICS_DIR);
+  if (artwork.snd_first == NULL)
+    artwork.snd_first = getDummyArtworkInfo(TREE_TYPE_SOUNDS_DIR);
+  if (artwork.mus_first == NULL)
+    artwork.mus_first = getDummyArtworkInfo(TREE_TYPE_MUSIC_DIR);
+
   /* before sorting, the first entries will be from the user directory */
   artwork.gfx_current =
-    getTreeInfoFromFilename(artwork.gfx_first, setup.graphics_set);
+    getTreeInfoFromIdentifier(artwork.gfx_first, setup.graphics_set);
   if (artwork.gfx_current == NULL)
     artwork.gfx_current = getFirstValidTreeInfoEntry(artwork.gfx_first);
 
   artwork.snd_current =
-    getTreeInfoFromFilename(artwork.snd_first, setup.sounds_set);
+    getTreeInfoFromIdentifier(artwork.snd_first, setup.sounds_set);
   if (artwork.snd_current == NULL)
-  artwork.snd_current = getFirstValidTreeInfoEntry(artwork.snd_first);
+    artwork.snd_current = getFirstValidTreeInfoEntry(artwork.snd_first);
 
   artwork.mus_current =
-    getTreeInfoFromFilename(artwork.mus_first, setup.music_set);
+    getTreeInfoFromIdentifier(artwork.mus_first, setup.music_set);
   if (artwork.mus_current == NULL)
-  artwork.mus_current = getFirstValidTreeInfoEntry(artwork.mus_first);
+    artwork.mus_current = getFirstValidTreeInfoEntry(artwork.mus_first);
 
-  artwork.graphics_set_current_name = artwork.gfx_current->name;
-  artwork.sounds_set_current_name = artwork.snd_current->name;
-  artwork.music_set_current_name = artwork.mus_current->name;
+  artwork.gfx_current_identifier = artwork.gfx_current->identifier;
+  artwork.snd_current_identifier = artwork.snd_current->identifier;
+  artwork.mus_current_identifier = artwork.mus_current->identifier;
 
 #if 0
-  printf("graphics set == %s\n\n", artwork.graphics_set_current_name);
-  printf("sounds set == %s\n\n", artwork.sounds_set_current_name);
-  printf("music set == %s\n\n", artwork.music_set_current_name);
+  printf("graphics set == %s\n\n", artwork.gfx_current_identifier);
+  printf("sounds set == %s\n\n", artwork.snd_current_identifier);
+  printf("music set == %s\n\n", artwork.mus_current_identifier);
 #endif
 
   sortTreeInfo(&artwork.gfx_first, compareTreeInfoEntries);
@@ -1883,13 +1890,13 @@ void LoadArtworkInfoFromLevelInfo(ArtworkDirTree **artwork_node,
 
       if (topnode_last != *artwork_node)
       {
+       free((*artwork_node)->identifier);
        free((*artwork_node)->name);
        free((*artwork_node)->name_sorting);
-       free((*artwork_node)->name_short);
 
+       (*artwork_node)->identifier   = getStringCopy(level_node->filename);
        (*artwork_node)->name         = getStringCopy(level_node->name);
        (*artwork_node)->name_sorting = getStringCopy(level_node->name);
-       (*artwork_node)->name_short   = getStringCopy(level_node->filename);
 
        (*artwork_node)->sort_priority = level_node->sort_priority;
        (*artwork_node)->color = LEVELCOLOR((*artwork_node));
@@ -1913,11 +1920,36 @@ void LoadLevelArtworkInfo()
   LoadArtworkInfoFromLevelInfo(&artwork.snd_first, leveldir_first);
   LoadArtworkInfoFromLevelInfo(&artwork.mus_first, leveldir_first);
 
+  /* needed for reloading level artwork not known at ealier stage */
+  if (strcmp(artwork.gfx_current_identifier, setup.graphics_set) != 0)
+  {
+    artwork.gfx_current =
+      getTreeInfoFromIdentifier(artwork.gfx_first, setup.graphics_set);
+    if (artwork.gfx_current == NULL)
+      artwork.gfx_current = getFirstValidTreeInfoEntry(artwork.gfx_first);
+  }
+
+  if (strcmp(artwork.snd_current_identifier, setup.sounds_set) != 0)
+  {
+    artwork.snd_current =
+      getTreeInfoFromIdentifier(artwork.snd_first, setup.sounds_set);
+    if (artwork.snd_current == NULL)
+      artwork.snd_current = getFirstValidTreeInfoEntry(artwork.snd_first);
+  }
+
+  if (strcmp(artwork.mus_current_identifier, setup.music_set) != 0)
+  {
+    artwork.mus_current =
+      getTreeInfoFromIdentifier(artwork.mus_first, setup.music_set);
+    if (artwork.mus_current == NULL)
+      artwork.mus_current = getFirstValidTreeInfoEntry(artwork.mus_first);
+  }
+
   sortTreeInfo(&artwork.gfx_first, compareTreeInfoEntries);
   sortTreeInfo(&artwork.snd_first, compareTreeInfoEntries);
   sortTreeInfo(&artwork.mus_first, compareTreeInfoEntries);
 
-#if 1
+#if 0
   dumpTreeInfo(artwork.gfx_first, 0);
   dumpTreeInfo(artwork.snd_first, 0);
   dumpTreeInfo(artwork.mus_first, 0);
@@ -1942,8 +1974,8 @@ static void SaveUserLevelInfo()
   /* always start with reliable default values */
   setTreeInfoToDefaults(&ldi, TREE_TYPE_LEVEL_DIR);
 
-  ldi.name = getLoginName();
-  ldi.author = getRealName();
+  ldi.name = getStringCopy(getLoginName());
+  ldi.author = getStringCopy(getRealName());
   ldi.levels = 100;
   ldi.first_level = 1;
   ldi.sort_priority = LEVELCLASS_USER_START;
@@ -1953,7 +1985,7 @@ static void SaveUserLevelInfo()
                                                 getCookie("LEVELINFO")));
 
   for (i=0; i<NUM_LEVELINFO_TOKENS; i++)
-    if (i != LEVELINFO_TOKEN_NAME_SHORT &&
+    if (i != LEVELINFO_TOKEN_IDENTIFIER &&
        i != LEVELINFO_TOKEN_NAME_SORTING &&
        i != LEVELINFO_TOKEN_IMPORTED_FROM)
       fprintf(file, "%s\n", getSetupLine(levelinfo_tokens, "", i));
@@ -2066,8 +2098,8 @@ void LoadLevelSetup_LastSeries()
     char *last_level_series =
       getTokenValue(level_setup_list, TOKEN_STR_LAST_LEVEL_SERIES);
 
-    leveldir_current = getTreeInfoFromFilename(leveldir_first,
-                                              last_level_series);
+    leveldir_current = getTreeInfoFromIdentifier(leveldir_first,
+                                                last_level_series);
     if (leveldir_current == NULL)
       leveldir_current = getFirstValidTreeInfoEntry(leveldir_first);