rnd-20020601-1-src
[rocksndiamonds.git] / src / libgame / misc.c
index b0244aa627ff6de461e6d99b72a2f43905bba53f..78a5d13e0938ea113457c11806326d41c19d9f95 100644 (file)
@@ -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;
 
@@ -159,12 +160,13 @@ boolean FrameReached(unsigned long *frame_counter_var,
 {
   unsigned long actual_frame_counter = FrameCounter;
 
-  if (actual_frame_counter < *frame_counter_var+frame_delay &&
+  if (actual_frame_counter < *frame_counter_var + frame_delay &&
       actual_frame_counter >= *frame_counter_var)
-    return(FALSE);
+    return FALSE;
 
   *frame_counter_var = actual_frame_counter;
-  return(TRUE);
+
+  return TRUE;
 }
 
 boolean DelayReached(unsigned long *counter_var,
@@ -174,10 +176,11 @@ boolean DelayReached(unsigned long *counter_var,
 
   if (actual_counter < *counter_var + delay &&
       actual_counter >= *counter_var)
-    return(FALSE);
+    return FALSE;
 
   *counter_var = actual_counter;
-  return(TRUE);
+
+  return TRUE;
 }
 
 void WaitUntilDelayReached(unsigned long *counter_var, unsigned long delay)
@@ -435,6 +438,7 @@ void GetOptions(char *argv[])
   options.network = FALSE;
   options.verbose = FALSE;
   options.debug = FALSE;
+  options.debug_command = NULL;
 
   while (*options_left)
   {
@@ -472,19 +476,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)
@@ -563,6 +571,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,7 +823,7 @@ void putFileChunk(FILE *file, char *chunk_name, int chunk_size,
 
 void ReadUnusedBytesFromFile(FILE *file, unsigned long bytes)
 {
-  while (bytes--)
+  while (bytes-- && !feof(file))
     fgetc(file);
 }
 
@@ -818,7 +835,8 @@ void WriteUnusedBytesToFile(FILE *file, unsigned long bytes)
 
 #define TRANSLATE_KEYSYM_TO_KEYNAME    0
 #define TRANSLATE_KEYSYM_TO_X11KEYNAME 1
-#define TRANSLATE_X11KEYNAME_TO_KEYSYM 2
+#define TRANSLATE_KEYNAME_TO_KEYSYM    2
+#define TRANSLATE_X11KEYNAME_TO_KEYSYM 3
 
 void translate_keyname(Key *keysym, char **x11name, char **name, int mode)
 {
@@ -1005,6 +1023,26 @@ void translate_keyname(Key *keysym, char **x11name, char **name, int mode)
 
     *x11name = name_buffer;
   }
+  else if (mode == TRANSLATE_KEYNAME_TO_KEYSYM)
+  {
+    Key key = KSYM_UNDEFINED;
+
+    i = 0;
+    do
+    {
+      if (strcmp(translate_key[i].name, *name) == 0)
+      {
+       key = translate_key[i].key;
+       break;
+      }
+    }
+    while (translate_key[++i].x11name);
+
+    if (key == KSYM_UNDEFINED)
+      Error(ERR_WARN, "getKeyFromKeyName(): not completely implemented");
+
+    *keysym = key;
+  }
   else if (mode == TRANSLATE_X11KEYNAME_TO_KEYSYM)
   {
     Key key = KSYM_UNDEFINED;
@@ -1106,6 +1144,14 @@ char *getX11KeyNameFromKey(Key key)
   return x11name;
 }
 
+Key getKeyFromKeyName(char *name)
+{
+  Key key;
+
+  translate_keyname(&key, NULL, &name, TRANSLATE_KEYNAME_TO_KEYSYM);
+  return key;
+}
+
 Key getKeyFromX11KeyName(char *x11name)
 {
   Key key;
@@ -1130,6 +1176,58 @@ char getCharFromKey(Key key)
 }
 
 
+/* ========================================================================= */
+/* functions for checking filenames                                          */
+/* ========================================================================= */
+
+boolean FileIsGraphic(char *filename)
+{
+  if (strlen(filename) > 4 &&
+      strcmp(&filename[strlen(filename) - 4], ".pcx") == 0)
+    return TRUE;
+
+  return FALSE;
+}
+
+boolean FileIsSound(char *basename)
+{
+  if (strlen(basename) > 4 &&
+      strcmp(&basename[strlen(basename) - 4], ".wav") == 0)
+    return TRUE;
+
+  return FALSE;
+}
+
+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 */
 /* ========================================================================= */