rnd-20050525-1-src
[rocksndiamonds.git] / src / libgame / misc.c
index c7b8eea400aead264705c4ac9be1b3aa6e678532..a502ec49026cd1baab8dc85ac4f267a8a1cd68c6 100644 (file)
@@ -52,6 +52,7 @@ void printf_line(char *line_string, int line_length)
   fprintf_line(stdout, line_string, line_length);
 }
 
+
 /* int2str() returns a number converted to a string;
    the used memory is static, but will be overwritten by later calls,
    so if you want to save the result, copy it to a private string buffer;
@@ -82,6 +83,7 @@ char *int2str(int number, int size)
   }
 }
 
+
 /* something similar to "int2str()" above, but allocates its own memory
    and has a different interface; we cannot use "itoa()", because this
    seems to be already defined when cross-compiling to the win32 target */
@@ -103,6 +105,23 @@ char *i_to_a(unsigned int i)
 }
 
 
+/* calculate base-2 logarithm of argument (rounded down to integer;
+   this function returns the number of the highest bit set in argument) */
+
+int log_2(unsigned int x)
+{
+  int e = 0;
+
+  while ((1 << e) < x)
+  {
+    x -= (1 << e);     /* for rounding down (rounding up: remove this line) */
+    e++;
+  }
+
+  return e;
+}
+
+
 /* ------------------------------------------------------------------------- */
 /* counter functions                                                         */
 /* ------------------------------------------------------------------------- */
@@ -318,26 +337,24 @@ static char *get_corrected_real_name(char *real_name)
   char *from_ptr = real_name;
   char *to_ptr   = real_name_new;
 
-  if (strchr(real_name, 'ß') == NULL)  /* name does not contain 'ß' */
-  {
-    strncpy(real_name_new, real_name, MAX_USERNAME_LEN);
-    real_name_new[MAX_USERNAME_LEN] = '\0';
-
-    return real_name_new;
-  }
-
-  /* the user's real name may contain a 'ß' character (german sharp s),
-     which has no equivalent in upper case letters (which our fonts use) */
+  /* copy the name string, but not more than MAX_USERNAME_LEN characters */
   while (*from_ptr && (long)(to_ptr - real_name_new) < MAX_USERNAME_LEN - 1)
   {
-    if (*from_ptr != 'ß')
-      *to_ptr++ = *from_ptr++;
-    else
+    /* the name field read from "passwd" file may also contain additional
+       user information, separated by commas, which will be removed here */
+    if (*from_ptr == ',')
+      break;
+
+    /* the user's real name may contain 'ß' characters (german sharp s),
+       which have no equivalent in upper case letters (used by our fonts) */
+    if (*from_ptr == 'ß')
     {
       from_ptr++;
       *to_ptr++ = 's';
       *to_ptr++ = 's';
     }
+    else
+      *to_ptr++ = *from_ptr++;
   }
 
   *to_ptr = '\0';
@@ -596,9 +613,17 @@ void GetOptions(char *argv[], void (*print_usage_function)(void))
       if (option_arg == next_option)
        options_left++;
 
-      /* adjust path for level directory accordingly */
+      /* adjust paths for sub-directories in base directory accordingly */
       options.level_directory =
        getPath2(options.ro_base_directory, LEVELS_DIRECTORY);
+      options.graphics_directory =
+       getPath2(options.ro_base_directory, GRAPHICS_DIRECTORY);
+      options.sounds_directory =
+       getPath2(options.ro_base_directory, SOUNDS_DIRECTORY);
+      options.music_directory =
+       getPath2(options.ro_base_directory, MUSIC_DIRECTORY);
+      options.docs_directory =
+       getPath2(options.ro_base_directory, DOCS_DIRECTORY);
     }
     else if (strncmp(option, "-levels", option_len) == 0)
     {
@@ -660,6 +685,11 @@ void GetOptions(char *argv[], void (*print_usage_function)(void))
       options.execute_command = option_arg;
       if (option_arg == next_option)
        options_left++;
+
+#if 1
+      /* when doing batch processing, always enable verbose mode (warnings) */
+      options.verbose = TRUE;
+#endif
     }
     else if (*option == '-')
     {
@@ -850,27 +880,27 @@ inline void swap_number_pairs(int *x1, int *y1, int *x2, int *y2)
   *y2 = help_y;
 }
 
-short getFile16BitInteger(FILE *file, int byte_order)
+int getFile16BitInteger(FILE *file, int byte_order)
 {
   if (byte_order == BYTE_ORDER_BIG_ENDIAN)
-    return ((fgetc(file) <<  8) |
-           (fgetc(file) <<  0));
+    return ((fgetc(file) << 8) |
+           (fgetc(file) << 0));
   else          /* BYTE_ORDER_LITTLE_ENDIAN */
-    return ((fgetc(file) <<  0) |
-           (fgetc(file) <<  8));
+    return ((fgetc(file) << 0) |
+           (fgetc(file) << 8));
 }
 
-void putFile16BitInteger(FILE *file, short value, int byte_order)
+void putFile16BitInteger(FILE *file, int value, int byte_order)
 {
   if (byte_order == BYTE_ORDER_BIG_ENDIAN)
   {
-    fputc((value >>  8) & 0xff, file);
-    fputc((value >>  0) & 0xff, file);
+    fputc((value >> 8) & 0xff, file);
+    fputc((value >> 0) & 0xff, file);
   }
   else          /* BYTE_ORDER_LITTLE_ENDIAN */
   {
-    fputc((value >>  0) & 0xff, file);
-    fputc((value >>  8) & 0xff, file);
+    fputc((value >> 0) & 0xff, file);
+    fputc((value >> 8) & 0xff, file);
   }
 }
 
@@ -1208,7 +1238,7 @@ void translate_keyname(Key *keysym, char **x11name, char **name, int mode)
       char c = name_ptr[6];
 
       if (c >= '0' && c <= '9')
-       key = KSYM_0 + (Key)(c - '0');
+       key = KSYM_KP_0 + (Key)(c - '0');
     }
     else if (strncmp(name_ptr, "XK_F", 4) == 0 && strlen(name_ptr) <= 6)
     {
@@ -1472,6 +1502,9 @@ void dumpList(ListNode *node_first)
 
 boolean fileExists(char *filename)
 {
+  if (filename == NULL)
+    return FALSE;
+
 #if 0
   printf("checking file '%s'\n", filename);
 #endif
@@ -1580,6 +1613,27 @@ boolean FileIsArtworkType(char *basename, int type)
 /* functions for loading artwork configuration information                   */
 /* ------------------------------------------------------------------------- */
 
+char *get_mapped_token(char *token)
+{
+  /* !!! make this dynamically configurable (init.c:InitArtworkConfig) !!! */
+  static char *map_token_prefix[][2] =
+  {
+    { "char_procent",          "char_percent"  },
+    { NULL,                                    }
+  };
+  int i;
+
+  for (i = 0; map_token_prefix[i][0] != NULL; i++)
+  {
+    int len_token_prefix = strlen(map_token_prefix[i][0]);
+
+    if (strncmp(token, map_token_prefix[i][0], len_token_prefix) == 0)
+      return getStringCat2(map_token_prefix[i][1], &token[len_token_prefix]);
+  }
+
+  return NULL;
+}
+
 /* This function checks if a string <s> of the format "string1, string2, ..."
    exactly contains a string <s_contained>. */
 
@@ -1675,7 +1729,7 @@ static void FreeCustomArtworkList(struct ArtworkListInfo *,
                                  struct ListNodeInfo ***, int *);
 
 struct FileInfo *getFileListFromConfigList(struct ConfigInfo *config_list,
-                                          struct ConfigInfo *suffix_list,
+                                          struct ConfigTypeInfo *suffix_list,
                                           char **ignore_tokens,
                                           int num_file_list_entries)
 {
@@ -1710,6 +1764,9 @@ struct FileInfo *getFileListFromConfigList(struct ConfigInfo *config_list,
        setString(&file_list[i].default_parameter[j], suffix_list[j].value);
        setString(&file_list[i].parameter[j], suffix_list[j].value);
       }
+
+      file_list[i].redefined = FALSE;
+      file_list[i].fallback_to_default = FALSE;
     }
   }
 
@@ -1810,7 +1867,7 @@ static boolean token_suffix_match(char *token, char *suffix, int start_pos)
 #define KNOWN_TOKEN_VALUE      "[KNOWN_TOKEN_VALUE]"
 
 static void read_token_parameters(SetupFileHash *setup_file_hash,
-                                 struct ConfigInfo *suffix_list,
+                                 struct ConfigTypeInfo *suffix_list,
                                  struct FileInfo *file_list_entry)
 {
   /* check for config token that is the base token without any suffixes */
@@ -1863,7 +1920,7 @@ static void read_token_parameters(SetupFileHash *setup_file_hash,
 static void add_dynamic_file_list_entry(struct FileInfo **list,
                                        int *num_list_entries,
                                        SetupFileHash *extra_file_hash,
-                                       struct ConfigInfo *suffix_list,
+                                       struct ConfigTypeInfo *suffix_list,
                                        int num_suffix_list_entries,
                                        char *token)
 {
@@ -1884,6 +1941,9 @@ static void add_dynamic_file_list_entry(struct FileInfo **list,
   new_list_entry->filename = NULL;
   new_list_entry->parameter = checked_calloc(parameter_array_size);
 
+  new_list_entry->redefined = FALSE;
+  new_list_entry->fallback_to_default = FALSE;
+
   read_token_parameters(extra_file_hash, suffix_list, new_list_entry);
 }
 
@@ -1912,7 +1972,7 @@ static void LoadArtworkConfigFromFilename(struct ArtworkListInfo *artwork_info,
                                          char *filename)
 {
   struct FileInfo *file_list = artwork_info->file_list;
-  struct ConfigInfo *suffix_list = artwork_info->suffix_list;
+  struct ConfigTypeInfo *suffix_list = artwork_info->suffix_list;
   char **base_prefixes = artwork_info->base_prefixes;
   char **ext1_suffixes = artwork_info->ext1_suffixes;
   char **ext2_suffixes = artwork_info->ext2_suffixes;
@@ -1955,6 +2015,29 @@ static void LoadArtworkConfigFromFilename(struct ArtworkListInfo *artwork_info,
   /* at this point, we do not need the setup file hash anymore -- free it */
   freeSetupFileHash(setup_file_hash);
 
+#if 1
+  /* map deprecated to current tokens (using prefix match and replace) */
+  BEGIN_HASH_ITERATION(valid_file_hash, itr)
+  {
+    char *token = HASH_ITERATION_TOKEN(itr);
+    char *mapped_token = get_mapped_token(token);
+
+    if (mapped_token != NULL)
+    {
+      char *value = HASH_ITERATION_VALUE(itr);
+
+      /* add mapped token */
+      setHashEntry(valid_file_hash, mapped_token, value);
+
+      /* ignore old token (by setting it to "known" keyword) */
+      setHashEntry(valid_file_hash, token, known_token_value);
+
+      free(mapped_token);
+    }
+  }
+  END_HASH_ITERATION(valid_file_hash, itr)
+#endif
+
   /* read parameters for all known config file tokens */
   for (i = 0; i < num_file_list_entries; i++)
     read_token_parameters(valid_file_hash, suffix_list, &file_list[i]);
@@ -2304,6 +2387,7 @@ void LoadArtworkConfig(struct ArtworkListInfo *artwork_info)
       setString(&file_list[i].parameter[j], file_list[i].default_parameter[j]);
 
     file_list[i].redefined = FALSE;
+    file_list[i].fallback_to_default = FALSE;
   }
 
   /* free previous dynamic artwork file array */
@@ -2372,6 +2456,116 @@ static void deleteArtworkListEntry(struct ArtworkListInfo *artwork_info,
   }
 }
 
+#if 1
+static void replaceArtworkListEntry(struct ArtworkListInfo *artwork_info,
+                                   struct ListNodeInfo **listnode,
+                                   struct FileInfo *file_list_entry)
+{
+  char *init_text[] =
+  {
+    "Loading graphics:",
+    "Loading sounds:",
+    "Loading music:"
+  };
+
+  ListNode *node;
+  char *basename = file_list_entry->filename;
+  char *filename = getCustomArtworkFilename(basename, artwork_info->type);
+
+  if (filename == NULL)
+  {
+    Error(ERR_WARN, "cannot find artwork file '%s'", basename);
+
+    basename = file_list_entry->default_filename;
+
+    /* dynamic artwork has no default filename / skip empty default artwork */
+    if (basename == NULL || strcmp(basename, UNDEFINED_FILENAME) == 0)
+      return;
+
+    file_list_entry->fallback_to_default = TRUE;
+
+    Error(ERR_WARN, "trying default artwork file '%s'", basename);
+
+    filename = getCustomArtworkFilename(basename, artwork_info->type);
+
+    if (filename == NULL)
+    {
+      int error_mode = ERR_WARN;
+
+      /* we can get away without sounds and music, but not without graphics */
+      if (*listnode == NULL && artwork_info->type == ARTWORK_TYPE_GRAPHICS)
+       error_mode = ERR_EXIT;
+
+      Error(error_mode, "cannot find default artwork file '%s'", basename);
+
+      return;
+    }
+  }
+
+  /* check if the old and the new artwork file are the same */
+  if (*listnode && strcmp((*listnode)->source_filename, filename) == 0)
+  {
+    /* The old and new artwork are the same (have the same filename and path).
+       This usually means that this artwork does not exist in this artwork set
+       and a fallback to the existing artwork is done. */
+
+#if 0
+    printf("[artwork '%s' already exists (same list entry)]\n", filename);
+#endif
+
+    return;
+  }
+
+  /* delete existing artwork file entry */
+  deleteArtworkListEntry(artwork_info, listnode);
+
+  /* check if the new artwork file already exists in the list of artworks */
+  if ((node = getNodeFromKey(artwork_info->content_list, filename)) != NULL)
+  {
+#if 0
+      printf("[artwork '%s' already exists (other list entry)]\n", filename);
+#endif
+
+      *listnode = (struct ListNodeInfo *)node->content;
+      (*listnode)->num_references++;
+
+      return;
+  }
+
+#if 0
+  printf("::: %s: '%s'\n", init_text[artwork_info->type], basename);
+#endif
+
+  DrawInitText(init_text[artwork_info->type], 120, FC_GREEN);
+  DrawInitText(basename, 150, FC_YELLOW);
+
+  if ((*listnode = artwork_info->load_artwork(filename)) != NULL)
+  {
+#if 0
+      printf("[adding new artwork '%s']\n", filename);
+#endif
+
+    (*listnode)->num_references = 1;
+    addNodeToList(&artwork_info->content_list, (*listnode)->source_filename,
+                 *listnode);
+  }
+  else
+  {
+    int error_mode = ERR_WARN;
+
+#if 1
+    /* we can get away without sounds and music, but not without graphics */
+    if (artwork_info->type == ARTWORK_TYPE_GRAPHICS)
+      error_mode = ERR_EXIT;
+#endif
+
+    Error(error_mode, "cannot load artwork file '%s'", basename);
+    return;
+  }
+}
+
+#else
+
 static void replaceArtworkListEntry(struct ArtworkListInfo *artwork_info,
                                    struct ListNodeInfo **listnode,
                                    char *basename)
@@ -2391,6 +2585,9 @@ static void replaceArtworkListEntry(struct ArtworkListInfo *artwork_info,
     int error_mode = ERR_WARN;
 
 #if 1
+    /* !!! NEW ARTWORK FALLBACK CODE !!! NEARLY UNTESTED !!! */
+    /* before failing, try fallback to default artwork */
+#else
     /* we can get away without sounds and music, but not without graphics */
     if (*listnode == NULL && artwork_info->type == ARTWORK_TYPE_GRAPHICS)
       error_mode = ERR_EXIT;
@@ -2461,6 +2658,27 @@ static void replaceArtworkListEntry(struct ArtworkListInfo *artwork_info,
     return;
   }
 }
+#endif
+
+#if 1
+static void LoadCustomArtwork(struct ArtworkListInfo *artwork_info,
+                             struct ListNodeInfo **listnode,
+                             struct FileInfo *file_list_entry)
+{
+#if 0
+  printf("GOT CUSTOM ARTWORK FILE '%s'\n", filename);
+#endif
+
+  if (strcmp(file_list_entry->filename, UNDEFINED_FILENAME) == 0)
+  {
+    deleteArtworkListEntry(artwork_info, listnode);
+    return;
+  }
+
+  replaceArtworkListEntry(artwork_info, listnode, file_list_entry);
+}
+
+#else
 
 static void LoadCustomArtwork(struct ArtworkListInfo *artwork_info,
                              struct ListNodeInfo **listnode,
@@ -2478,6 +2696,38 @@ static void LoadCustomArtwork(struct ArtworkListInfo *artwork_info,
 
   replaceArtworkListEntry(artwork_info, listnode, basename);
 }
+#endif
+
+#if 1
+static void LoadArtworkToList(struct ArtworkListInfo *artwork_info,
+                             struct ListNodeInfo **listnode,
+                             struct FileInfo *file_list_entry)
+{
+#if 0
+  if (artwork_info->artwork_list == NULL ||
+      list_pos >= artwork_info->num_file_list_entries)
+    return;
+#endif
+
+#if 0
+  printf("loading artwork '%s' ...  [%d]\n",
+        basename, getNumNodes(artwork_info->content_list));
+#endif
+
+#if 1
+  LoadCustomArtwork(artwork_info, listnode, file_list_entry);
+#else
+  LoadCustomArtwork(artwork_info, &artwork_info->artwork_list[list_pos],
+                   basename);
+#endif
+
+#if 0
+  printf("loading artwork '%s' done [%d]\n",
+        basename, getNumNodes(artwork_info->content_list));
+#endif
+}
+
+#else
 
 static void LoadArtworkToList(struct ArtworkListInfo *artwork_info,
                              struct ListNodeInfo **listnode,
@@ -2506,6 +2756,7 @@ static void LoadArtworkToList(struct ArtworkListInfo *artwork_info,
         basename, getNumNodes(artwork_info->content_list));
 #endif
 }
+#endif
 
 void ReloadCustomArtworkList(struct ArtworkListInfo *artwork_info)
 {
@@ -2528,18 +2779,34 @@ void ReloadCustomArtworkList(struct ArtworkListInfo *artwork_info)
       printf("::: '%s' -> '%s'\n", file_list[i].token, file_list[i].filename);
 #endif
 
+#if 1
+    LoadArtworkToList(artwork_info, &artwork_info->artwork_list[i],
+                     &file_list[i]);
+#else
     LoadArtworkToList(artwork_info, &artwork_info->artwork_list[i],
                      file_list[i].filename, i);
+#endif
 
 #if 0
+    /* !!! NEW ARTWORK FALLBACK CODE !!! NEARLY UNTESTED !!! */
     if (artwork_info->artwork_list[i] == NULL &&
-       strcmp(file_list[i].default_filename, file_list[i].filename) != 0)
+       strcmp(file_list[i].filename, UNDEFINED_FILENAME) != 0 &&
+       strcmp(file_list[i].default_filename, file_list[i].filename) != 0 &&
+       strcmp(file_list[i].default_filename, UNDEFINED_FILENAME) != 0)
     {
       Error(ERR_WARN, "trying default artwork file '%s'",
            file_list[i].default_filename);
 
       LoadArtworkToList(artwork_info, &artwork_info->artwork_list[i],
                        file_list[i].default_filename, i);
+
+      /* even the fallback to default artwork was not successful -- fail now */
+      if (artwork_info->artwork_list[i] == NULL &&
+         artwork_info->type == ARTWORK_TYPE_GRAPHICS)
+       Error(ERR_EXIT, "cannot find artwork file '%s' or default file '%s'",
+             file_list[i].filename, file_list[i].default_filename);
+
+      file_list[i].fallback_to_default = TRUE;
     }
 #endif
   }
@@ -2551,14 +2818,21 @@ void ReloadCustomArtworkList(struct ArtworkListInfo *artwork_info)
 
   for (i = 0; i < num_dynamic_file_list_entries; i++)
   {
+#if 1
+    LoadArtworkToList(artwork_info, &artwork_info->dynamic_artwork_list[i],
+                     &dynamic_file_list[i]);
+#else
     LoadArtworkToList(artwork_info, &artwork_info->dynamic_artwork_list[i],
                      dynamic_file_list[i].filename, i);
+#endif
 
 #if 0
     printf("::: '%s', '0x%08x'\n",
           dynamic_file_list[i].filename,
           dynamic_file_list[i].default_filename);
 #endif
+
+    /* dynamic artwork does not have default filename! */
   }
 
 #if 0