rnd-20050117-1-src
[rocksndiamonds.git] / src / libgame / misc.c
index 3612ba045fcd649f9b4ec9a52dcf4b1762ad7f9d..b72aa3c5608cd0e024b508f1e0cbb52c89faafdf 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                                                         */
 /* ------------------------------------------------------------------------- */
@@ -596,9 +615,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 +687,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 == '-')
     {
@@ -1208,7 +1240,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 +1504,9 @@ void dumpList(ListNode *node_first)
 
 boolean fileExists(char *filename)
 {
+  if (filename == NULL)
+    return FALSE;
+
 #if 0
   printf("checking file '%s'\n", filename);
 #endif
@@ -1675,7 +1710,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)
 {
@@ -1810,7 +1845,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 +1898,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)
 {
@@ -1912,7 +1947,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 +1990,43 @@ 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)
+  {
+    /* !!! make this dynamically configurable (init.c:InitArtworkConfig) !!! */
+    static char *map_token_prefix[][2] =
+    {  /* old prefix   ->      new prefix      */
+      { "char_procent",                "char_percent"  },
+      { NULL,                  NULL            }
+    };
+    char *token = HASH_ITERATION_TOKEN(itr);
+
+    for (i = 0; map_token_prefix[i][0] != NULL; i++)
+    {
+      int token_prefix_length = strlen(map_token_prefix[i][0]);
+
+      if (strncmp(token, map_token_prefix[i][0], token_prefix_length) == 0)
+      {
+       char *value = HASH_ITERATION_VALUE(itr);
+       char *mapped_token = getStringCat2(map_token_prefix[i][1],
+                                          &token[token_prefix_length]);
+
+       /* 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);
+
+       break;
+      }
+    }
+  }
+  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]);