rocksndiamonds-3.3.1.0
[rocksndiamonds.git] / src / libgame / misc.c
index f3e191047b59bd4f79e657ae6d76b6f507ecdf5e..60dbcf068c20e9d84a29a6d4de15356d1ce15892 100644 (file)
@@ -14,6 +14,7 @@
 #include <time.h>
 #include <sys/time.h>
 #include <sys/types.h>
+#include <sys/stat.h>
 #include <stdarg.h>
 #include <ctype.h>
 #include <string.h>
@@ -167,7 +168,7 @@ boolean getTokenValueFromString(char *string, char **token, char **value)
 /* ------------------------------------------------------------------------- */
 
 #if defined(PLATFORM_MSDOS)
-volatile unsigned long counter = 0;
+volatile unsigned int counter = 0;
 
 void increment_counter()
 {
@@ -181,12 +182,52 @@ END_OF_FUNCTION(increment_counter);
 /* maximal allowed length of a command line option */
 #define MAX_OPTION_LEN         256
 
+#if 1
+
+#ifdef TARGET_SDL
+static unsigned int getCurrentMS()
+{
+  return SDL_GetTicks();
+}
+
+#else /* !TARGET_SDL */
+
+#if defined(PLATFORM_UNIX)
+static unsigned int getCurrentMS()
+{
+  struct timeval current_time;
+
+  gettimeofday(&current_time, NULL);
+
+  return current_time.tv_sec * 1000 + current_time.tv_usec / 1000;
+}
+#endif /* PLATFORM_UNIX */
+#endif /* !TARGET_SDL */
+
+static unsigned int mainCounter(int mode)
+{
+  static unsigned int base_ms = 0;
+  unsigned int current_ms;
+
+  /* get current system milliseconds */
+  current_ms = getCurrentMS();
+
+  /* reset base timestamp in case of counter reset or wrap-around */
+  if (mode == INIT_COUNTER || current_ms < base_ms)
+    base_ms = current_ms;
+
+  /* return milliseconds since last counter reset */
+  return current_ms - base_ms;
+}
+
+#else
+
 #ifdef TARGET_SDL
-static unsigned long mainCounter(int mode)
+static unsigned int mainCounter(int mode)
 {
-  static unsigned long base_ms = 0;
-  unsigned long current_ms;
-  unsigned long counter_ms;
+  static unsigned int base_ms = 0;
+  unsigned int current_ms;
+  unsigned int counter_ms;
 
   current_ms = SDL_GetTicks();
 
@@ -202,11 +243,11 @@ static unsigned long mainCounter(int mode)
 #else /* !TARGET_SDL */
 
 #if defined(PLATFORM_UNIX)
-static unsigned long mainCounter(int mode)
+static unsigned int mainCounter(int mode)
 {
   static struct timeval base_time = { 0, 0 };
   struct timeval current_time;
-  unsigned long counter_ms;
+  unsigned int counter_ms;
 
   gettimeofday(&current_time, NULL);
 
@@ -222,6 +263,8 @@ static unsigned long mainCounter(int mode)
 #endif /* PLATFORM_UNIX */
 #endif /* !TARGET_SDL */
 
+#endif
+
 void InitCounter()             /* set counter back to zero */
 {
 #if !defined(PLATFORM_MSDOS)
@@ -233,7 +276,7 @@ void InitCounter()          /* set counter back to zero */
 #endif
 }
 
-unsigned long Counter()        /* get milliseconds since last call of InitCounter() */
+unsigned int Counter() /* get milliseconds since last call of InitCounter() */
 {
 #if !defined(PLATFORM_MSDOS)
   return mainCounter(READ_COUNTER);
@@ -242,7 +285,7 @@ unsigned long Counter()     /* get milliseconds since last call of InitCounter() */
 #endif
 }
 
-static void sleep_milliseconds(unsigned long milliseconds_delay)
+static void sleep_milliseconds(unsigned int milliseconds_delay)
 {
   boolean do_busy_waiting = (milliseconds_delay < 5 ? TRUE : FALSE);
 
@@ -253,7 +296,7 @@ static void sleep_milliseconds(unsigned long milliseconds_delay)
        therefore it's better to do a short interval of busy waiting
        to get our sleeping time more accurate */
 
-    unsigned long base_counter = Counter(), actual_counter = Counter();
+    unsigned int base_counter = Counter(), actual_counter = Counter();
 
     while (actual_counter < base_counter + milliseconds_delay &&
           actual_counter >= base_counter)
@@ -277,15 +320,15 @@ static void sleep_milliseconds(unsigned long milliseconds_delay)
   }
 }
 
-void Delay(unsigned long delay)        /* Sleep specified number of milliseconds */
+void Delay(unsigned int delay) /* Sleep specified number of milliseconds */
 {
   sleep_milliseconds(delay);
 }
 
-boolean FrameReached(unsigned long *frame_counter_var,
-                    unsigned long frame_delay)
+boolean FrameReached(unsigned int *frame_counter_var,
+                    unsigned int frame_delay)
 {
-  unsigned long actual_frame_counter = FrameCounter;
+  unsigned int actual_frame_counter = FrameCounter;
 
   if (actual_frame_counter >= *frame_counter_var &&
       actual_frame_counter < *frame_counter_var + frame_delay)
@@ -296,10 +339,10 @@ boolean FrameReached(unsigned long *frame_counter_var,
   return TRUE;
 }
 
-boolean DelayReached(unsigned long *counter_var,
-                    unsigned long delay)
+boolean DelayReached(unsigned int *counter_var,
+                    unsigned int delay)
 {
-  unsigned long actual_counter = Counter();
+  unsigned int actual_counter = Counter();
 
   if (actual_counter >= *counter_var &&
       actual_counter < *counter_var + delay)
@@ -310,9 +353,9 @@ boolean DelayReached(unsigned long *counter_var,
   return TRUE;
 }
 
-void WaitUntilDelayReached(unsigned long *counter_var, unsigned long delay)
+void WaitUntilDelayReached(unsigned int *counter_var, unsigned int delay)
 {
-  unsigned long actual_counter;
+  unsigned int actual_counter;
 
   while (1)
   {
@@ -333,12 +376,12 @@ void WaitUntilDelayReached(unsigned long *counter_var, unsigned long delay)
 /* random generator functions                                                */
 /* ------------------------------------------------------------------------- */
 
-unsigned int init_random_number(int nr, long seed)
+unsigned int init_random_number(int nr, int seed)
 {
   if (seed == NEW_RANDOMIZE)
   {
     /* default random seed */
-    seed = (long)time(NULL);                   // seconds since the epoch
+    seed = (int)time(NULL);                    // seconds since the epoch
 
 #if !defined(PLATFORM_WIN32)
     /* add some more randomness */
@@ -346,12 +389,12 @@ unsigned int init_random_number(int nr, long seed)
 
     gettimeofday(&current_time, NULL);
 
-    seed += (long)current_time.tv_usec;                // microseconds since the epoch
+    seed += (int)current_time.tv_usec;         // microseconds since the epoch
 #endif
 
 #if defined(TARGET_SDL)
     /* add some more randomness */
-    seed += (long)SDL_GetTicks();              // milliseconds since SDL init
+    seed += (int)SDL_GetTicks();               // milliseconds since SDL init
 #endif
 
 #if 1
@@ -383,7 +426,7 @@ static char *get_corrected_real_name(char *real_name)
   char *to_ptr   = real_name_new;
 
   /* 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)
+  while (*from_ptr && (int)(to_ptr - real_name_new) < MAX_USERNAME_LEN - 1)
   {
     /* the name field read from "passwd" file may also contain additional
        user information, separated by commas, which will be removed here */
@@ -468,6 +511,16 @@ char *getRealName()
   return real_name;
 }
 
+time_t getFileTimestampEpochSeconds(char *filename)
+{
+  struct stat file_status;
+
+  if (stat(filename, &file_status) != 0)       /* cannot stat file */
+    return 0;
+
+  return file_status.st_mtime;
+}
+
 
 /* ------------------------------------------------------------------------- */
 /* path manipulation functions                                               */
@@ -685,6 +738,7 @@ void GetOptions(char *argv[], void (*print_usage_function)(void))
   options.display_name = NULL;
   options.server_host = NULL;
   options.server_port = 0;
+
   options.ro_base_directory = ro_base_path;
   options.rw_base_directory = rw_base_path;
   options.level_directory    = getPath2(ro_base_path, LEVELS_DIRECTORY);
@@ -692,7 +746,10 @@ void GetOptions(char *argv[], void (*print_usage_function)(void))
   options.sounds_directory   = getPath2(ro_base_path, SOUNDS_DIRECTORY);
   options.music_directory    = getPath2(ro_base_path, MUSIC_DIRECTORY);
   options.docs_directory     = getPath2(ro_base_path, DOCS_DIRECTORY);
+
   options.execute_command = NULL;
+  options.special_flags = NULL;
+
   options.serveronly = FALSE;
   options.network = FALSE;
   options.verbose = FALSE;
@@ -827,6 +884,25 @@ void GetOptions(char *argv[], void (*print_usage_function)(void))
     {
       options.debug_x11_sync = TRUE;
     }
+    else if (strPrefix(option, "-D"))
+    {
+#if 1
+      options.special_flags = getStringCopy(&option[2]);
+#else
+      char *flags_string = &option[2];
+      unsigned int flags_value;
+
+      if (*flags_string == '\0')
+       Error(ERR_EXIT_HELP, "empty flag ignored");
+
+      flags_value = get_special_flags_function(flags_string);
+
+      if (flags_value == 0)
+       Error(ERR_EXIT_HELP, "unknown flag '%s'", flags_string);
+
+      options.special_flags |= flags_value;
+#endif
+    }
     else if (strncmp(option, "-execute", option_len) == 0)
     {
       if (option_arg == NULL)
@@ -948,7 +1024,7 @@ void Error(int mode, char *format, ...)
 /* checked memory allocation and freeing functions                           */
 /* ------------------------------------------------------------------------- */
 
-void *checked_malloc(unsigned long size)
+void *checked_malloc(unsigned int size)
 {
   void *ptr;
 
@@ -960,7 +1036,7 @@ void *checked_malloc(unsigned long size)
   return ptr;
 }
 
-void *checked_calloc(unsigned long size)
+void *checked_calloc(unsigned int size)
 {
   void *ptr;
 
@@ -972,7 +1048,7 @@ void *checked_calloc(unsigned long size)
   return ptr;
 }
 
-void *checked_realloc(void *ptr, unsigned long size)
+void *checked_realloc(void *ptr, unsigned int size)
 {
   ptr = realloc(ptr, size);
 
@@ -988,7 +1064,7 @@ void checked_free(void *ptr)
     free(ptr);
 }
 
-void clear_mem(void *ptr, unsigned long size)
+void clear_mem(void *ptr, unsigned int size)
 {
 #if defined(PLATFORM_WIN32)
   /* for unknown reason, memset() sometimes crashes when compiled with MinGW */
@@ -1117,7 +1193,8 @@ boolean getFileChunk(FILE *file, char *chunk_name, int *chunk_size,
   const int chunk_name_length = 4;
 
   /* read chunk name */
-  fgets(chunk_name, chunk_name_length + 1, file);
+  if (fgets(chunk_name, chunk_name_length + 1, file) == NULL)
+    return FALSE;
 
   if (chunk_size != NULL)
   {
@@ -1180,7 +1257,7 @@ int putFileVersion(FILE *file, int version)
   return 4;
 }
 
-void ReadBytesFromFile(FILE *file, byte *buffer, unsigned long bytes)
+void ReadBytesFromFile(FILE *file, byte *buffer, unsigned int bytes)
 {
   int i;
 
@@ -1188,7 +1265,7 @@ void ReadBytesFromFile(FILE *file, byte *buffer, unsigned long bytes)
     buffer[i] = fgetc(file);
 }
 
-void WriteBytesToFile(FILE *file, byte *buffer, unsigned long bytes)
+void WriteBytesToFile(FILE *file, byte *buffer, unsigned int bytes)
 {
   int i;
 
@@ -1196,13 +1273,13 @@ void WriteBytesToFile(FILE *file, byte *buffer, unsigned long bytes)
     fputc(buffer[i], file);
 }
 
-void ReadUnusedBytesFromFile(FILE *file, unsigned long bytes)
+void ReadUnusedBytesFromFile(FILE *file, unsigned int bytes)
 {
   while (bytes-- && !feof(file))
     fgetc(file);
 }
 
-void WriteUnusedBytesToFile(FILE *file, unsigned long bytes)
+void WriteUnusedBytesToFile(FILE *file, unsigned int bytes)
 {
   while (bytes--)
     fputc(0, file);
@@ -1399,7 +1476,7 @@ void translate_keyname(Key *keysym, char **x11name, char **name, int mode)
       while (translate_key[++i].x11name);
 
       if (!translate_key[i].x11name)
-       sprintf(name_buffer, "0x%04lx", (unsigned long)key);
+       sprintf(name_buffer, "0x%04x", (unsigned int)key);
     }
 
     *x11name = name_buffer;
@@ -1476,7 +1553,7 @@ void translate_keyname(Key *keysym, char **x11name, char **name, int mode)
     }
     else if (strPrefix(name_ptr, "0x"))
     {
-      unsigned long value = 0;
+      unsigned int value = 0;
 
       name_ptr += 2;
 
@@ -1797,7 +1874,7 @@ boolean FileIsMusic(char *filename)
     return TRUE;
 
 #if defined(TARGET_SDL)
-  if (fileHasPrefix(basename, "mod") ||
+  if ((fileHasPrefix(basename, "mod") && !fileHasSuffix(basename, "txt")) ||
       fileHasSuffix(basename, "mod") ||
       fileHasSuffix(basename, "s3m") ||
       fileHasSuffix(basename, "it") ||
@@ -1935,6 +2012,20 @@ int get_parameter_value(char *value_raw, char *suffix, int type)
     if (string_has_parameter(value, "static_panel"))
       result |= ANIM_STATIC_PANEL;
   }
+  else if (strEqual(suffix, ".class"))
+  {
+    result = get_hash_from_key(value);
+  }
+  else if (strEqual(suffix, ".style"))
+  {
+    result = STYLE_DEFAULT;
+
+    if (string_has_parameter(value, "accurate_borders"))
+      result |= STYLE_ACCURATE_BORDERS;
+
+    if (string_has_parameter(value, "inner_corners"))
+      result |= STYLE_INNER_CORNERS;
+  }
   else if (strEqual(suffix, ".fade_mode"))
   {
     result = (string_has_parameter(value, "none")      ? FADE_MODE_NONE :
@@ -3058,7 +3149,7 @@ void debug_print_timestamp(int counter_nr, char *message)
 
   counter[counter_nr][0] = Counter_Microseconds();
 #else
-  static long counter[DEBUG_NUM_TIMESTAMPS][2];
+  static int counter[DEBUG_NUM_TIMESTAMPS][2];
   char *unit = "s";
 
   counter[counter_nr][0] = Counter();