rnd-20030313-1-src
[rocksndiamonds.git] / src / libgame / misc.c
index d68316d873777ba91f6c17e8e593d60b81606dc0..d94b37d55c174a63a3684ca2266ac19ed349dc12 100644 (file)
@@ -261,74 +261,66 @@ void WaitUntilDelayReached(unsigned long *counter_var, unsigned long delay)
 /* random generator functions                                                */
 /* ------------------------------------------------------------------------- */
 
+#if 0
 unsigned int SimpleRND(unsigned int max)
 {
-#if defined(TARGET_SDL)
-  static unsigned long root = 654321;
-  unsigned long current_ms;
+  return (random_linux_libc(RND_FREE) % max);
+}
 
-  current_ms = SDL_GetTicks();
-  root = root * 4253261 + current_ms;
-  return (root % max);
-#else
-  static unsigned long root = 654321;
-  struct timeval current_time;
+unsigned int InitSimpleRND(long seed)
+{
+  if (seed == NEW_RANDOMIZE)
+  {
+    struct timeval current_time;
 
-  gettimeofday(&current_time, NULL);
-  root = root * 4253261 + current_time.tv_sec + current_time.tv_usec;
-  return (root % max);
-#endif
-}
+    gettimeofday(&current_time, NULL);
+    seed = (long)current_time.tv_usec;
+  }
 
-#ifdef DEBUG
-static unsigned int last_RND_value = 0;
+  srandom_linux_libc(RND_FREE, (unsigned int) seed);
 
-unsigned int last_RND()
-{
-  return last_RND_value;
+  return (unsigned int) seed;
 }
-#endif
 
 unsigned int RND(unsigned int max)
 {
-#ifdef DEBUG
-  return (last_RND_value = random_linux_libc() % max);
-#else
-  return (random_linux_libc() % max);
-#endif
+  return (random_linux_libc(RND_GAME) % max);
 }
 
 unsigned int InitRND(long seed)
 {
-#if defined(TARGET_SDL)
-  unsigned long current_ms;
-
   if (seed == NEW_RANDOMIZE)
   {
-    current_ms = SDL_GetTicks();
-    srandom_linux_libc((unsigned int) current_ms);
-    return (unsigned int) current_ms;
-  }
-  else
-  {
-    srandom_linux_libc((unsigned int) seed);
-    return (unsigned int) seed;
+    struct timeval current_time;
+
+    gettimeofday(&current_time, NULL);
+    seed = (long)current_time.tv_usec;
   }
-#else
-  struct timeval current_time;
 
+  srandom_linux_libc(RND_GAME, (unsigned int) seed);
+
+  return (unsigned int) seed;
+}
+#endif
+
+unsigned int init_random_number(int nr, long seed)
+{
   if (seed == NEW_RANDOMIZE)
   {
+    struct timeval current_time;
+
     gettimeofday(&current_time, NULL);
-    srandom_linux_libc((unsigned int) current_time.tv_usec);
-    return (unsigned int) current_time.tv_usec;
+    seed = (long)current_time.tv_usec;
   }
-  else
-  {
-    srandom_linux_libc((unsigned int) seed);
-    return (unsigned int) seed;
-  }
-#endif
+
+  srandom_linux_libc(nr, (unsigned int) seed);
+
+  return (unsigned int) seed;
+}
+
+unsigned int get_random_number(int nr, unsigned int max)
+{
+  return (random_linux_libc(nr) % max);
 }
 
 
@@ -429,6 +421,7 @@ char *getPath2(char *path1, char *path2)
                                       strlen(path2) + 1);
 
   sprintf(complete_path, "%s/%s", path1, path2);
+
   return complete_path;
 }
 
@@ -439,6 +432,7 @@ char *getPath3(char *path1, char *path2, char *path3)
                                       strlen(path3) + 1);
 
   sprintf(complete_path, "%s/%s/%s", path1, path2, path3);
+
   return complete_path;
 }
 
@@ -447,6 +441,7 @@ char *getStringCat2(char *s1, char *s2)
   char *complete_string = checked_malloc(strlen(s1) + strlen(s2) + 1);
 
   sprintf(complete_string, "%s%s", s1, s2);
+
   return complete_string;
 }
 
@@ -458,8 +453,8 @@ char *getStringCopy(char *s)
     return NULL;
 
   s_copy = checked_malloc(strlen(s) + 1);
-
   strcpy(s_copy, s);
+
   return s_copy;
 }
 
@@ -1512,12 +1507,78 @@ boolean FileIsArtworkType(char *basename, int type)
 /* functions for loading artwork configuration information                   */
 /* ------------------------------------------------------------------------- */
 
-int get_parameter_value(int type, char *value)
+/* This function checks if a string <s> of the format "string1, string2, ..."
+   exactly contains a string <s_contained>. */
+
+static boolean string_has_parameter(char *s, char *s_contained)
+{
+  char *substring;
+
+  if (s == NULL || s_contained == NULL)
+    return FALSE;
+
+  if (strlen(s_contained) > strlen(s))
+    return FALSE;
+
+  if (strncmp(s, s_contained, strlen(s_contained)) == 0)
+  {
+    char next_char = s[strlen(s_contained)];
+
+    /* check if next character is delimiter or whitespace */
+    return (next_char == ',' || next_char == '\0' ||
+           next_char == ' ' || next_char == '\t' ? TRUE : FALSE);
+  }
+
+  /* check if string contains another parameter string after a comma */
+  substring = strchr(s, ',');
+  if (substring == NULL)       /* string does not contain a comma */
+    return FALSE;
+
+  /* advance string pointer to next character after the comma */
+  substring++;
+
+  /* skip potential whitespaces after the comma */
+  while (*substring == ' ' || *substring == '\t')
+    substring++;
+
+  return string_has_parameter(substring, s_contained);
+}
+
+int get_parameter_value(char *token, char *value_raw, int type)
 {
-  return (strcmp(value, ARG_UNDEFINED) == 0 ? ARG_UNDEFINED_VALUE :
-         type == TYPE_INTEGER ? get_integer_from_string(value) :
-         type == TYPE_BOOLEAN ? get_boolean_from_string(value) :
-         ARG_UNDEFINED_VALUE);
+  char *value = getStringToLower(value_raw);
+  int result = 0;      /* probably a save default value */
+
+  if (strcmp(token, ".direction") == 0)
+  {
+    result = (strcmp(value, "left")  == 0 ? MV_LEFT :
+             strcmp(value, "right") == 0 ? MV_RIGHT :
+             strcmp(value, "up")    == 0 ? MV_UP :
+             strcmp(value, "down")  == 0 ? MV_DOWN : MV_NO_MOVING);
+  }
+  else if (strcmp(token, ".anim_mode") == 0)
+  {
+    result = (string_has_parameter(value, "loop")      ? ANIM_LOOP :
+             string_has_parameter(value, "linear")    ? ANIM_LINEAR :
+             string_has_parameter(value, "pingpong")  ? ANIM_PINGPONG :
+             string_has_parameter(value, "pingpong2") ? ANIM_PINGPONG2 :
+             string_has_parameter(value, "random")    ? ANIM_RANDOM :
+             ANIM_LOOP);
+
+    if (string_has_parameter(value, "reverse"))
+      result |= ANIM_REVERSE;
+  }
+  else         /* generic parameter of type integer or boolean */
+  {
+    result = (strcmp(value, ARG_UNDEFINED) == 0 ? ARG_UNDEFINED_VALUE :
+             type == TYPE_INTEGER ? get_integer_from_string(value) :
+             type == TYPE_BOOLEAN ? get_boolean_from_string(value) :
+             ARG_UNDEFINED_VALUE);
+  }
+
+  free(value);
+
+  return result;
 }
 
 static void FreeCustomArtworkList(struct ArtworkListInfo *,
@@ -1525,6 +1586,7 @@ static void FreeCustomArtworkList(struct ArtworkListInfo *,
 
 struct FileInfo *getFileListFromConfigList(struct ConfigInfo *config_list,
                                           struct ConfigInfo *suffix_list,
+                                          char **ignore_tokens,
                                           int num_file_list_entries)
 {
   struct FileInfo *file_list;
@@ -1585,9 +1647,9 @@ struct FileInfo *getFileListFromConfigList(struct ConfigInfo *config_list,
     }
 
     /* the following tokens are no file definitions, but other config tokens */
-    if (strcmp(config_list[i].token, "global.num_toons") == 0 ||
-       strcmp(config_list[i].token, "menu.main.hide_static_text") == 0)
-      is_file_entry = FALSE;
+    for (j=0; ignore_tokens[j] != NULL; j++)
+      if (strcmp(config_list[i].token, ignore_tokens[j]) == 0)
+       is_file_entry = FALSE;
 
     if (is_file_entry)
     {