rnd-20020803-1-src
[rocksndiamonds.git] / src / libgame / misc.c
index 4f201d97fd015712dafd2cd555fe8a03edd44623..acada2472e9754f5e15b775b0e74fae3832823f7 100644 (file)
@@ -1,7 +1,7 @@
 /***********************************************************
 * Artsoft Retro-Game Library                               *
 *----------------------------------------------------------*
-* (c) 1994-2001 Artsoft Entertainment                      *
+* (c) 1994-2002 Artsoft Entertainment                      *
 *               Holger Schemel                             *
 *               Detmolder Strasse 189                      *
 *               33604 Bielefeld                            *
@@ -14,9 +14,6 @@
 #include <time.h>
 #include <sys/time.h>
 #include <sys/types.h>
-/*
-#include <sys/stat.h>
-*/
 #include <stdarg.h>
 #include <ctype.h>
 #include <string.h>
@@ -114,10 +111,12 @@ static void sleep_milliseconds(unsigned long milliseconds_delay)
 {
   boolean do_busy_waiting = (milliseconds_delay < 5 ? TRUE : FALSE);
 
+#if 0
 #if defined(PLATFORM_MSDOS)
-  /* don't use select() to perform waiting operations under DOS/Windows
+  /* don't use select() to perform waiting operations under DOS
      environment; always use a busy loop for waiting instead */
   do_busy_waiting = TRUE;
+#endif
 #endif
 
   if (do_busy_waiting)
@@ -137,6 +136,8 @@ static void sleep_milliseconds(unsigned long milliseconds_delay)
   {
 #if defined(TARGET_SDL)
     SDL_Delay(milliseconds_delay);
+#elif defined(TARGET_ALLEGRO)
+    rest(milliseconds_delay);
 #else
     struct timeval delay;
 
@@ -305,12 +306,19 @@ char *getLoginName()
 #if defined(PLATFORM_WIN32)
   return ANONYMOUS_NAME;
 #else
-  struct passwd *pwd;
+  static char *login_name = NULL;
 
-  if ((pwd = getpwuid(getuid())) == NULL)
-    return ANONYMOUS_NAME;
-  else
-    return pwd->pw_name;
+  if (login_name == NULL)
+  {
+    struct passwd *pwd;
+
+    if ((pwd = getpwuid(getuid())) == NULL)
+      login_name = ANONYMOUS_NAME;
+    else
+      login_name = getStringCopy(pwd->pw_name);
+  }
+
+  return login_name;
 #endif
 }
 
@@ -356,16 +364,16 @@ char *getHomeDir()
 #if defined(PLATFORM_UNIX)
   static char *home_dir = NULL;
 
-  if (!home_dir)
+  if (home_dir == NULL)
   {
-    if (!(home_dir = getenv("HOME")))
+    if ((home_dir = getenv("HOME")) == NULL)
     {
       struct passwd *pwd;
 
-      if ((pwd = getpwuid(getuid())))
-       home_dir = pwd->pw_dir;
-      else
+      if ((pwd = getpwuid(getuid())) == NULL)
        home_dir = ".";
+      else
+       home_dir = getStringCopy(pwd->pw_dir);
     }
   }
 
@@ -437,6 +445,7 @@ void GetOptions(char *argv[])
   options.network = FALSE;
   options.verbose = FALSE;
   options.debug = FALSE;
+  options.debug_command = NULL;
 
   while (*options_left)
   {
@@ -474,19 +483,23 @@ void GetOptions(char *argv[])
       Error(ERR_EXIT_HELP, "unrecognized option '%s'", option);
     else if (strncmp(option, "-help", option_len) == 0)
     {
-      printf("Usage: %s [options] [server.name [port]]\n"
+      printf("Usage: %s [options] [<server host> [<server port>]]\n"
             "Options:\n"
             "  -d, --display <host>[:<scr>]  X server display\n"
             "  -b, --basepath <directory>    alternative base directory\n"
             "  -l, --level <directory>       alternative level directory\n"
             "  -g, --graphics <directory>    alternative graphics directory\n"
-            "  -s, --sounds <directory>      alternative graphics directory\n"
-            "  -m, --music <directory>       alternative graphics directory\n"
+            "  -s, --sounds <directory>      alternative sounds directory\n"
+            "  -m, --music <directory>       alternative music directory\n"
             "  -n, --network                 network multiplayer game\n"
             "      --serveronly              only start network server\n"
             "  -v, --verbose                 verbose mode\n"
             "      --debug                   display debugging information\n",
             program.command_basename);
+
+      if (options.debug)
+       printf("      --debug-command <command> execute special command\n");
+
       exit(0);
     }
     else if (strncmp(option, "-display", option_len) == 0)
@@ -565,6 +578,15 @@ void GetOptions(char *argv[])
     {
       options.debug = TRUE;
     }
+    else if (strncmp(option, "-debug-command", option_len) == 0)
+    {
+      if (option_arg == NULL)
+       Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
+
+      options.debug_command = option_arg;
+      if (option_arg == next_option)
+       options_left++;
+    }
     else if (*option == '-')
     {
       Error(ERR_EXIT_HELP, "unrecognized option '%s'", option_str);
@@ -806,6 +828,30 @@ void putFileChunk(FILE *file, char *chunk_name, int chunk_size,
   }
 }
 
+int getFileVersion(FILE *file)
+{
+  int version_major, version_minor, version_patch;
+
+  version_major = fgetc(file);
+  version_minor = fgetc(file);
+  version_patch = fgetc(file);
+  fgetc(file);         /* not used */
+
+  return VERSION_IDENT(version_major, version_minor, version_patch);
+}
+
+void putFileVersion(FILE *file, int version)
+{
+  int version_major = VERSION_MAJOR(version);
+  int version_minor = VERSION_MINOR(version);
+  int version_patch = VERSION_PATCH(version);
+
+  fputc(version_major, file);
+  fputc(version_minor, file);
+  fputc(version_patch, file);
+  fputc(0, file);      /* not used */
+}
+
 void ReadUnusedBytesFromFile(FILE *file, unsigned long bytes)
 {
   while (bytes-- && !feof(file))
@@ -818,6 +864,11 @@ void WriteUnusedBytesToFile(FILE *file, unsigned long bytes)
     fputc(0, file);
 }
 
+
+/* ------------------------------------------------------------------------- */
+/* functions to translate key identifiers between different format           */
+/* ------------------------------------------------------------------------- */
+
 #define TRANSLATE_KEYSYM_TO_KEYNAME    0
 #define TRANSLATE_KEYSYM_TO_X11KEYNAME 1
 #define TRANSLATE_KEYNAME_TO_KEYSYM    2
@@ -947,8 +998,8 @@ void translate_keyname(Key *keysym, char **x11name, char **name, int mode)
       sprintf(name_buffer, "%c", '0' + (char)(key - KSYM_0));
     else if (key >= KSYM_KP_0 && key <= KSYM_KP_9)
       sprintf(name_buffer, "keypad %c", '0' + (char)(key - KSYM_KP_0));
-    else if (key >= KSYM_F1 && key <= KSYM_F24)
-      sprintf(name_buffer, "function F%d", (int)(key - KSYM_F1 + 1));
+    else if (key >= KSYM_FKEY_FIRST && key <= KSYM_FKEY_LAST)
+      sprintf(name_buffer, "function F%d", (int)(key - KSYM_FKEY_FIRST + 1));
     else if (key == KSYM_UNDEFINED)
       strcpy(name_buffer, "(undefined)");
     else
@@ -984,8 +1035,8 @@ void translate_keyname(Key *keysym, char **x11name, char **name, int mode)
       sprintf(name_buffer, "XK_%c", '0' + (char)(key - KSYM_0));
     else if (key >= KSYM_KP_0 && key <= KSYM_KP_9)
       sprintf(name_buffer, "XK_KP_%c", '0' + (char)(key - KSYM_KP_0));
-    else if (key >= KSYM_F1 && key <= KSYM_F24)
-      sprintf(name_buffer, "XK_F%d", (int)(key - KSYM_F1 + 1));
+    else if (key >= KSYM_FKEY_FIRST && key <= KSYM_FKEY_LAST)
+      sprintf(name_buffer, "XK_F%d", (int)(key - KSYM_FKEY_FIRST + 1));
     else if (key == KSYM_UNDEFINED)
       strcpy(name_buffer, "[undefined]");
     else
@@ -1061,7 +1112,7 @@ void translate_keyname(Key *keysym, char **x11name, char **name, int mode)
          ((c2 >= '0' && c1 <= '9') || c2 == '\0'))
        d = atoi(&name_ptr[4]);
 
-      if (d >=1 && d <= 24)
+      if (d >= 1 && d <= KSYM_NUM_FKEYS)
        key = KSYM_F1 + (Key)(d - 1);
     }
     else if (strncmp(name_ptr, "XK_", 3) == 0)
@@ -1185,16 +1236,34 @@ boolean FileIsSound(char *basename)
 
 boolean FileIsMusic(char *basename)
 {
+  /* "music" can be a WAV (loop) file or (if compiled with SDL) a MOD file */
+
+  if (FileIsSound(basename))
+    return TRUE;
+
+#if defined(TARGET_SDL)
   if (strlen(basename) > 4 &&
       (strcmp(&basename[strlen(basename) - 4], ".mod") == 0 ||
        strcmp(&basename[strlen(basename) - 4], ".MOD") == 0 ||
        strncmp(basename, "mod.", 4) == 0 ||
        strncmp(basename, "MOD.", 4) == 0))
     return TRUE;
+#endif
+
+  return FALSE;
+}
+
+boolean FileIsArtworkType(char *basename, int type)
+{
+  if ((type == TREE_TYPE_GRAPHICS_DIR && FileIsGraphic(basename)) ||
+      (type == TREE_TYPE_SOUNDS_DIR && FileIsSound(basename)) ||
+      (type == TREE_TYPE_MUSIC_DIR && FileIsMusic(basename)))
+    return TRUE;
 
   return FALSE;
 }
 
+
 /* ========================================================================= */
 /* functions only needed for non-Unix (non-command-line) systems */
 /* ========================================================================= */