1 /***********************************************************
2 * Rocks'n'Diamonds -- McDuffin Strikes Back! *
3 *----------------------------------------------------------*
4 * (c) 1995-98 Artsoft Entertainment *
8 * phone: ++49 +521 290471 *
9 * email: aeglos@valinor.owl.de *
10 *----------------------------------------------------------*
12 ***********************************************************/
18 #include <sys/param.h>
19 #include <sys/types.h>
32 volatile unsigned long counter = 0;
34 void increment_counter()
39 END_OF_FUNCTION(increment_counter);
44 /* maximal allowed length of a command line option */
45 #define MAX_OPTION_LEN 256
48 static unsigned long mainCounter(int mode)
50 static struct timeval base_time = { 0, 0 };
51 struct timeval current_time;
52 unsigned long counter_ms;
54 gettimeofday(¤t_time, NULL);
56 if (mode == INIT_COUNTER || current_time.tv_sec < base_time.tv_sec)
57 base_time = current_time;
59 counter_ms = (current_time.tv_sec - base_time.tv_sec) * 1000
60 + (current_time.tv_usec - base_time.tv_usec) / 1000;
62 return counter_ms; /* return milliseconds since last init */
66 void InitCounter() /* set counter back to zero */
69 mainCounter(INIT_COUNTER);
71 LOCK_VARIABLE(counter);
72 LOCK_FUNCTION(increment_counter);
73 install_int_ex(increment_counter, BPS_TO_TIMER(100));
77 unsigned long Counter() /* get milliseconds since last call of InitCounter() */
80 return mainCounter(READ_COUNTER);
82 return (counter * 10);
86 static void sleep_milliseconds(unsigned long milliseconds_delay)
88 boolean do_busy_waiting = (milliseconds_delay < 5 ? TRUE : FALSE);
91 /* donĂ¯t use select() to perform waiting operations under DOS/Windows
92 environment; always use a busy loop for waiting instead */
93 do_busy_waiting = TRUE;
98 /* we want to wait only a few ms -- if we assume that we have a
99 kernel timer resolution of 10 ms, we would wait far to long;
100 therefore it's better to do a short interval of busy waiting
101 to get our sleeping time more accurate */
103 unsigned long base_counter = Counter(), actual_counter = Counter();
105 while (actual_counter < base_counter + milliseconds_delay &&
106 actual_counter >= base_counter)
107 actual_counter = Counter();
111 struct timeval delay;
113 delay.tv_sec = milliseconds_delay / 1000;
114 delay.tv_usec = 1000 * (milliseconds_delay % 1000);
116 if (select(0, NULL, NULL, NULL, &delay) != 0)
117 Error(ERR_WARN, "sleep_milliseconds(): select() failed");
121 void Delay(unsigned long delay) /* Sleep specified number of milliseconds */
123 sleep_milliseconds(delay);
126 boolean FrameReached(unsigned long *frame_counter_var,
127 unsigned long frame_delay)
129 unsigned long actual_frame_counter = FrameCounter;
131 if (actual_frame_counter < *frame_counter_var+frame_delay &&
132 actual_frame_counter >= *frame_counter_var)
135 *frame_counter_var = actual_frame_counter;
139 boolean DelayReached(unsigned long *counter_var,
142 unsigned long actual_counter = Counter();
144 if (actual_counter < *counter_var + delay &&
145 actual_counter >= *counter_var)
148 *counter_var = actual_counter;
152 void WaitUntilDelayReached(unsigned long *counter_var, unsigned long delay)
154 unsigned long actual_counter;
158 actual_counter = Counter();
160 if (actual_counter < *counter_var + delay &&
161 actual_counter >= *counter_var)
162 sleep_milliseconds((*counter_var + delay - actual_counter) / 2);
167 *counter_var = actual_counter;
170 /* int2str() returns a number converted to a string;
171 the used memory is static, but will be overwritten by later calls,
172 so if you want to save the result, copy it to a private string buffer;
173 there can be 10 local calls of int2str() without buffering the result --
174 the 11th call will then destroy the result from the first call and so on.
177 char *int2str(int number, int size)
179 static char shift_array[10][40];
180 static int shift_counter = 0;
181 char *s = shift_array[shift_counter];
183 shift_counter = (shift_counter + 1) % 10;
190 sprintf(s, " %09d", number);
191 return &s[strlen(s) - size];
195 sprintf(s, "%d", number);
200 unsigned int SimpleRND(unsigned int max)
202 static unsigned long root = 654321;
203 struct timeval current_time;
205 gettimeofday(¤t_time,NULL);
206 root = root * 4253261 + current_time.tv_sec + current_time.tv_usec;
211 static unsigned int last_RND_value = 0;
213 unsigned int last_RND()
215 return last_RND_value;
219 unsigned int RND(unsigned int max)
222 return (last_RND_value = random_linux_libc() % max);
224 return (random_linux_libc() % max);
228 unsigned int InitRND(long seed)
230 struct timeval current_time;
232 if (seed == NEW_RANDOMIZE)
234 gettimeofday(¤t_time,NULL);
235 srandom_linux_libc((unsigned int) current_time.tv_usec);
236 return (unsigned int)current_time.tv_usec;
240 srandom_linux_libc((unsigned int) seed);
241 return (unsigned int)seed;
249 if ((pwd = getpwuid(getuid())) == NULL)
250 return ANONYMOUS_NAME;
260 if ((pwd = getpwuid(getuid())) == NULL || strlen(pwd->pw_gecos) == 0)
261 return ANONYMOUS_NAME;
264 static char real_name[1024];
265 char *from_ptr = pwd->pw_gecos, *to_ptr = real_name;
267 if (strchr(pwd->pw_gecos, 'ĂŸ') == NULL)
268 return pwd->pw_gecos;
270 /* the user's real name contains a 'ĂŸ' character (german sharp s),
271 which has no equivalent in upper case letters (which our fonts use) */
272 while (*from_ptr != '\0' && (long)(to_ptr - real_name) < 1024 - 2)
274 if (*from_ptr != 'ĂŸ')
275 *to_ptr++ = *from_ptr++;
288 return ANONYMOUS_NAME;
295 static char *home_dir = NULL;
299 if (!(home_dir = getenv("HOME")))
303 if ((pwd = getpwuid(getuid())))
304 home_dir = pwd->pw_dir;
316 char *getPath2(char *path1, char *path2)
318 char *complete_path = checked_malloc(strlen(path1) + 1 +
321 sprintf(complete_path, "%s/%s", path1, path2);
322 return complete_path;
325 char *getPath3(char *path1, char *path2, char *path3)
327 char *complete_path = checked_malloc(strlen(path1) + 1 +
331 sprintf(complete_path, "%s/%s/%s", path1, path2, path3);
332 return complete_path;
335 char *getStringCopy(char *s)
342 s_copy = checked_malloc(strlen(s) + 1);
348 char *getStringToLower(char *s)
350 char *s_copy = checked_malloc(strlen(s) + 1);
351 char *s_ptr = s_copy;
354 *s_ptr++ = tolower(*s++);
360 void MarkTileDirty(int x, int y)
362 int xx = redraw_x1 + x;
363 int yy = redraw_y1 + y;
368 redraw[xx][yy] = TRUE;
369 redraw_mask |= REDRAW_TILES;
372 void SetBorderElement()
376 BorderElement = EL_LEERRAUM;
378 for(y=0; y<lev_fieldy && BorderElement == EL_LEERRAUM; y++)
380 for(x=0; x<lev_fieldx; x++)
382 if (!IS_MASSIVE(Feld[x][y]))
383 BorderElement = EL_BETON;
385 if (y != 0 && y != lev_fieldy - 1 && x != lev_fieldx - 1)
391 void GetOptions(char *argv[])
393 char **options_left = &argv[1];
395 /* initialize global program options */
396 options.display_name = NULL;
397 options.server_host = NULL;
398 options.server_port = 0;
399 options.ro_base_directory = RO_BASE_PATH;
400 options.rw_base_directory = RW_BASE_PATH;
401 options.level_directory = RO_BASE_PATH "/" LEVELS_DIRECTORY;
402 options.serveronly = FALSE;
403 options.network = FALSE;
404 options.verbose = FALSE;
406 while (*options_left)
408 char option_str[MAX_OPTION_LEN];
409 char *option = options_left[0];
410 char *next_option = options_left[1];
411 char *option_arg = NULL;
412 int option_len = strlen(option);
414 if (option_len >= MAX_OPTION_LEN)
415 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option);
417 strcpy(option_str, option); /* copy argument into buffer */
420 if (strcmp(option, "--") == 0) /* stop scanning arguments */
423 if (strncmp(option, "--", 2) == 0) /* treat '--' like '-' */
426 option_arg = strchr(option, '=');
427 if (option_arg == NULL) /* no '=' in option */
428 option_arg = next_option;
431 *option_arg++ = '\0'; /* cut argument from option */
432 if (*option_arg == '\0') /* no argument after '=' */
433 Error(ERR_EXIT_HELP, "option '%s' has invalid argument", option_str);
436 option_len = strlen(option);
438 if (strcmp(option, "-") == 0)
439 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option);
440 else if (strncmp(option, "-help", option_len) == 0)
442 printf("Usage: %s [options] [server.name [port]]\n"
444 " -d, --display machine:0 X server display\n"
445 " -b, --basepath directory alternative base directory\n"
446 " -l, --level directory alternative level directory\n"
447 " -s, --serveronly only start network server\n"
448 " -n, --network network multiplayer game\n"
449 " -v, --verbose verbose mode\n",
453 else if (strncmp(option, "-display", option_len) == 0)
455 if (option_arg == NULL)
456 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
458 options.display_name = option_arg;
459 if (option_arg == next_option)
462 else if (strncmp(option, "-basepath", option_len) == 0)
464 if (option_arg == NULL)
465 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
467 /* this should be extended to separate options for ro and rw data */
468 options.ro_base_directory = option_arg;
469 options.rw_base_directory = option_arg;
470 if (option_arg == next_option)
473 /* adjust path for level directory accordingly */
474 options.level_directory =
475 getPath2(options.ro_base_directory, LEVELS_DIRECTORY);
477 else if (strncmp(option, "-levels", option_len) == 0)
479 if (option_arg == NULL)
480 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
482 options.level_directory = option_arg;
483 if (option_arg == next_option)
486 else if (strncmp(option, "-network", option_len) == 0)
488 options.network = TRUE;
490 else if (strncmp(option, "-serveronly", option_len) == 0)
492 options.serveronly = TRUE;
494 else if (strncmp(option, "-verbose", option_len) == 0)
496 options.verbose = TRUE;
498 else if (*option == '-')
500 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option_str);
502 else if (options.server_host == NULL)
504 options.server_host = *options_left;
506 else if (options.server_port == 0)
508 options.server_port = atoi(*options_left);
509 if (options.server_port < 1024)
510 Error(ERR_EXIT_HELP, "bad port number '%d'", options.server_port);
513 Error(ERR_EXIT_HELP, "too many arguments");
519 void Error(int mode, char *format, ...)
521 char *process_name = "";
522 FILE *error = stderr;
524 /* display warnings only when running in verbose mode */
525 if (mode & ERR_WARN && !options.verbose)
529 if ((error = openErrorFile()) == NULL)
531 printf("Cannot write to error output file!\n");
536 if (mode & ERR_SOUND_SERVER)
537 process_name = " sound server";
538 else if (mode & ERR_NETWORK_SERVER)
539 process_name = " network server";
540 else if (mode & ERR_NETWORK_CLIENT)
541 process_name = " network client **";
547 fprintf(error, "%s%s: ", program_name, process_name);
550 fprintf(error, "warning: ");
552 va_start(ap, format);
553 vfprintf(error, format, ap);
556 fprintf(error, "\n");
560 fprintf(error, "%s: Try option '--help' for more information.\n",
564 fprintf(error, "%s%s: aborting\n", program_name, process_name);
571 if (mode & ERR_FROM_SERVER)
572 exit(1); /* child process: normal exit */
574 CloseAllAndExit(1); /* main process: clean up stuff */
578 void *checked_malloc(unsigned long size)
585 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
590 void *checked_calloc(unsigned long size)
594 ptr = calloc(1, size);
597 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
602 short getFile16BitInteger(FILE *file, int byte_order)
604 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
605 return ((fgetc(file) << 8) |
607 else /* BYTE_ORDER_LITTLE_ENDIAN */
608 return ((fgetc(file) << 0) |
612 void putFile16BitInteger(FILE *file, short value, int byte_order)
614 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
616 fputc((value >> 8) & 0xff, file);
617 fputc((value >> 0) & 0xff, file);
619 else /* BYTE_ORDER_LITTLE_ENDIAN */
621 fputc((value >> 0) & 0xff, file);
622 fputc((value >> 8) & 0xff, file);
626 int getFile32BitInteger(FILE *file, int byte_order)
628 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
629 return ((fgetc(file) << 24) |
630 (fgetc(file) << 16) |
633 else /* BYTE_ORDER_LITTLE_ENDIAN */
634 return ((fgetc(file) << 0) |
636 (fgetc(file) << 16) |
637 (fgetc(file) << 24));
640 void putFile32BitInteger(FILE *file, int value, int byte_order)
642 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
644 fputc((value >> 24) & 0xff, file);
645 fputc((value >> 16) & 0xff, file);
646 fputc((value >> 8) & 0xff, file);
647 fputc((value >> 0) & 0xff, file);
649 else /* BYTE_ORDER_LITTLE_ENDIAN */
651 fputc((value >> 0) & 0xff, file);
652 fputc((value >> 8) & 0xff, file);
653 fputc((value >> 16) & 0xff, file);
654 fputc((value >> 24) & 0xff, file);
658 void getFileChunk(FILE *file, char *chunk_buffer, int *chunk_length,
661 const int chunk_identifier_length = 4;
663 /* read chunk identifier */
664 fgets(chunk_buffer, chunk_identifier_length + 1, file);
666 /* read chunk length */
667 *chunk_length = getFile32BitInteger(file, byte_order);
670 void putFileChunk(FILE *file, char *chunk_name, int chunk_length,
673 /* write chunk identifier */
674 fputs(chunk_name, file);
676 /* write chunk length */
677 putFile32BitInteger(file, chunk_length, byte_order);
680 #define TRANSLATE_KEY_TO_KEYNAME 0
681 #define TRANSLATE_KEY_TO_X11KEYNAME 1
682 #define TRANSLATE_X11KEYNAME_TO_KEY 2
684 void translate_keyname(Key *keysym, char **x11name, char **name, int mode)
693 /* normal cursor keys */
694 { KEY_Left, "XK_Left", "cursor left" },
695 { KEY_Right, "XK_Right", "cursor right" },
696 { KEY_Up, "XK_Up", "cursor up" },
697 { KEY_Down, "XK_Down", "cursor down" },
699 /* keypad cursor keys */
701 { KEY_KP_Left, "XK_KP_Left", "keypad left" },
702 { KEY_KP_Right, "XK_KP_Right", "keypad right" },
703 { KEY_KP_Up, "XK_KP_Up", "keypad up" },
704 { KEY_KP_Down, "XK_KP_Down", "keypad down" },
707 /* other keypad keys */
709 { KEY_KP_Enter, "XK_KP_Enter", "keypad enter" },
710 { KEY_KP_Add, "XK_KP_Add", "keypad +" },
711 { KEY_KP_Subtract, "XK_KP_Subtract", "keypad -" },
712 { KEY_KP_Multiply, "XK_KP_Multiply", "keypad mltply" },
713 { KEY_KP_Divide, "XK_KP_Divide", "keypad /" },
714 { KEY_KP_Separator, "XK_KP_Separator", "keypad ," },
718 { KEY_Shift_L, "XK_Shift_L", "left shift" },
719 { KEY_Shift_R, "XK_Shift_R", "right shift" },
720 { KEY_Control_L, "XK_Control_L", "left control" },
721 { KEY_Control_R, "XK_Control_R", "right control" },
722 { KEY_Meta_L, "XK_Meta_L", "left meta" },
723 { KEY_Meta_R, "XK_Meta_R", "right meta" },
724 { KEY_Alt_L, "XK_Alt_L", "left alt" },
725 { KEY_Alt_R, "XK_Alt_R", "right alt" },
726 { KEY_Mode_switch, "XK_Mode_switch", "mode switch" },
727 { KEY_Multi_key, "XK_Multi_key", "multi key" },
729 /* some special keys */
730 { KEY_BackSpace, "XK_BackSpace", "backspace" },
731 { KEY_Delete, "XK_Delete", "delete" },
732 { KEY_Insert, "XK_Insert", "insert" },
733 { KEY_Tab, "XK_Tab", "tab" },
734 { KEY_Home, "XK_Home", "home" },
735 { KEY_End, "XK_End", "end" },
736 { KEY_Page_Up, "XK_Page_Up", "page up" },
737 { KEY_Page_Down, "XK_Page_Down", "page down" },
740 /* ASCII 0x20 to 0x40 keys (except numbers) */
741 { KEY_space, "XK_space", "space" },
742 { KEY_exclam, "XK_exclam", "!" },
743 { KEY_quotedbl, "XK_quotedbl", "\"" },
744 { KEY_numbersign, "XK_numbersign", "#" },
745 { KEY_dollar, "XK_dollar", "$" },
746 { KEY_percent, "XK_percent", "%" },
747 { KEY_ampersand, "XK_ampersand", "&" },
748 { KEY_apostrophe, "XK_apostrophe", "'" },
749 { KEY_parenleft, "XK_parenleft", "(" },
750 { KEY_parenright, "XK_parenright", ")" },
751 { KEY_asterisk, "XK_asterisk", "*" },
752 { KEY_plus, "XK_plus", "+" },
753 { KEY_comma, "XK_comma", "," },
754 { KEY_minus, "XK_minus", "-" },
755 { KEY_period, "XK_period", "." },
756 { KEY_slash, "XK_slash", "/" },
757 { KEY_colon, "XK_colon", ":" },
758 { KEY_semicolon, "XK_semicolon", ";" },
759 { KEY_less, "XK_less", "<" },
760 { KEY_equal, "XK_equal", "=" },
761 { KEY_greater, "XK_greater", ">" },
762 { KEY_question, "XK_question", "?" },
763 { KEY_at, "XK_at", "@" },
765 /* more ASCII keys */
766 { KEY_bracketleft, "XK_bracketleft", "[" },
767 { KEY_backslash, "XK_backslash", "backslash" },
768 { KEY_bracketright, "XK_bracketright", "]" },
769 { KEY_asciicircum, "XK_asciicircum", "circumflex" },
770 { KEY_underscore, "XK_underscore", "_" },
771 { KEY_grave, "XK_grave", "grave" },
772 { KEY_quoteleft, "XK_quoteleft", "quote left" },
773 { KEY_braceleft, "XK_braceleft", "brace left" },
774 { KEY_bar, "XK_bar", "bar" },
775 { KEY_braceright, "XK_braceright", "brace right" },
776 { KEY_asciitilde, "XK_asciitilde", "ascii tilde" },
778 /* special (non-ASCII) keys */
779 { KEY_Adiaeresis, "XK_Adiaeresis", "Ă„" },
780 { KEY_Odiaeresis, "XK_Odiaeresis", "Ă–" },
781 { KEY_Udiaeresis, "XK_Udiaeresis", "Ăœ" },
782 { KEY_adiaeresis, "XK_adiaeresis", "ä" },
783 { KEY_odiaeresis, "XK_odiaeresis", "ö" },
784 { KEY_udiaeresis, "XK_udiaeresis", "Ă¼" },
785 { KEY_ssharp, "XK_ssharp", "sharp s" },
787 /* end-of-array identifier */
793 if (mode == TRANSLATE_KEY_TO_KEYNAME)
795 static char name_buffer[30];
798 if (key >= KEY_A && key <= KEY_Z)
799 sprintf(name_buffer, "%c", 'A' + (char)(key - KEY_A));
800 else if (key >= KEY_a && key <= KEY_z)
801 sprintf(name_buffer, "%c", 'a' + (char)(key - KEY_a));
802 else if (key >= KEY_0 && key <= KEY_9)
803 sprintf(name_buffer, "%c", '0' + (char)(key - KEY_0));
804 else if (key >= KEY_KP_0 && key <= KEY_KP_9)
805 sprintf(name_buffer, "keypad %c", '0' + (char)(key - KEY_KP_0));
806 else if (key >= KEY_F1 && key <= KEY_F24)
807 sprintf(name_buffer, "function F%d", (int)(key - KEY_F1 + 1));
808 else if (key == KEY_UNDEFINED)
809 strcpy(name_buffer, "(undefined)");
816 if (key == translate_key[i].key)
818 strcpy(name_buffer, translate_key[i].name);
822 while (translate_key[++i].name);
824 if (!translate_key[i].name)
825 strcpy(name_buffer, "(unknown)");
830 else if (mode == TRANSLATE_KEY_TO_X11KEYNAME)
832 static char name_buffer[30];
835 if (key >= KEY_A && key <= KEY_Z)
836 sprintf(name_buffer, "XK_%c", 'A' + (char)(key - KEY_A));
837 else if (key >= KEY_a && key <= KEY_z)
838 sprintf(name_buffer, "XK_%c", 'a' + (char)(key - KEY_a));
839 else if (key >= KEY_0 && key <= KEY_9)
840 sprintf(name_buffer, "XK_%c", '0' + (char)(key - KEY_0));
841 else if (key >= KEY_KP_0 && key <= KEY_KP_9)
842 sprintf(name_buffer, "XK_KP_%c", '0' + (char)(key - KEY_KP_0));
843 else if (key >= KEY_F1 && key <= KEY_F24)
844 sprintf(name_buffer, "XK_F%d", (int)(key - KEY_F1 + 1));
845 else if (key == KEY_UNDEFINED)
846 strcpy(name_buffer, "[undefined]");
853 if (key == translate_key[i].key)
855 strcpy(name_buffer, translate_key[i].x11name);
859 while (translate_key[++i].x11name);
861 if (!translate_key[i].x11name)
862 sprintf(name_buffer, "0x%04lx", (unsigned long)key);
865 *x11name = name_buffer;
867 else if (mode == TRANSLATE_X11KEYNAME_TO_KEY)
869 Key key = KEY_UNDEFINED;
870 char *name_ptr = *x11name;
872 if (strncmp(name_ptr, "XK_", 3) == 0 && strlen(name_ptr) == 4)
874 char c = name_ptr[3];
876 if (c >= 'A' && c <= 'Z')
877 key = KEY_A + (Key)(c - 'A');
878 else if (c >= 'a' && c <= 'z')
879 key = KEY_a + (Key)(c - 'a');
880 else if (c >= '0' && c <= '9')
881 key = KEY_0 + (Key)(c - '0');
883 else if (strncmp(name_ptr, "XK_KP_", 6) == 0 && strlen(name_ptr) == 7)
885 char c = name_ptr[6];
887 if (c >= '0' && c <= '9')
888 key = KEY_0 + (Key)(c - '0');
890 else if (strncmp(name_ptr, "XK_F", 4) == 0 && strlen(name_ptr) <= 6)
892 char c1 = name_ptr[4];
893 char c2 = name_ptr[5];
896 if ((c1 >= '0' && c1 <= '9') &&
897 ((c2 >= '0' && c1 <= '9') || c2 == '\0'))
898 d = atoi(&name_ptr[4]);
900 if (d >=1 && d <= 24)
901 key = KEY_F1 + (Key)(d - 1);
903 else if (strncmp(name_ptr, "XK_", 3) == 0)
909 if (strcmp(name_ptr, translate_key[i].x11name) == 0)
911 key = translate_key[i].key;
915 while (translate_key[++i].x11name);
917 else if (strncmp(name_ptr, "0x", 2) == 0)
919 unsigned long value = 0;
925 char c = *name_ptr++;
928 if (c >= '0' && c <= '9')
930 else if (c >= 'a' && c <= 'f')
931 d = (int)(c - 'a' + 10);
932 else if (c >= 'A' && c <= 'F')
933 d = (int)(c - 'A' + 10);
941 value = value * 16 + d;
952 char *getKeyNameFromKey(Key key)
956 translate_keyname(&key, NULL, &name, TRANSLATE_KEY_TO_KEYNAME);
960 char *getX11KeyNameFromKey(Key key)
964 translate_keyname(&key, &x11name, NULL, TRANSLATE_KEY_TO_X11KEYNAME);
968 Key getKeyFromX11KeyName(char *x11name)
972 translate_keyname(&key, &x11name, NULL, TRANSLATE_X11KEYNAME_TO_KEY);
976 char getCharFromKey(Key key)
978 char *keyname = getKeyNameFromKey(key);
981 if (strlen(keyname) == 1)
983 else if (strcmp(keyname, "space") == 0)
985 else if (strcmp(keyname, "circumflex") == 0)
991 #define TRANSLATE_JOYSYMBOL_TO_JOYNAME 0
992 #define TRANSLATE_JOYNAME_TO_JOYSYMBOL 1
994 void translate_joyname(int *joysymbol, char **name, int mode)
1002 { JOY_LEFT, "joystick_left" },
1003 { JOY_RIGHT, "joystick_right" },
1004 { JOY_UP, "joystick_up" },
1005 { JOY_DOWN, "joystick_down" },
1006 { JOY_BUTTON_1, "joystick_button_1" },
1007 { JOY_BUTTON_2, "joystick_button_2" },
1012 if (mode == TRANSLATE_JOYSYMBOL_TO_JOYNAME)
1014 *name = "[undefined]";
1018 if (*joysymbol == translate_joy[i].joysymbol)
1020 *name = translate_joy[i].name;
1025 else if (mode == TRANSLATE_JOYNAME_TO_JOYSYMBOL)
1031 if (strcmp(*name, translate_joy[i].name) == 0)
1033 *joysymbol = translate_joy[i].joysymbol;
1040 char *getJoyNameFromJoySymbol(int joysymbol)
1044 translate_joyname(&joysymbol, &name, TRANSLATE_JOYSYMBOL_TO_JOYNAME);
1048 int getJoySymbolFromJoyName(char *name)
1052 translate_joyname(&joysymbol, &name, TRANSLATE_JOYNAME_TO_JOYSYMBOL);
1056 int getJoystickNrFromDeviceName(char *device_name)
1059 int joystick_nr = 0;
1061 if (device_name == NULL || device_name[0] == '\0')
1064 c = device_name[strlen(device_name) - 1];
1066 if (c >= '0' && c <= '9')
1067 joystick_nr = (int)(c - '0');
1069 if (joystick_nr < 0 || joystick_nr >= MAX_PLAYERS)
1075 /* ------------------------------------------------------------------------- */
1076 /* some functions to handle lists of level directories */
1077 /* ------------------------------------------------------------------------- */
1079 struct LevelDirInfo *newLevelDirInfo()
1081 return checked_calloc(sizeof(struct LevelDirInfo));
1084 void pushLevelDirInfo(struct LevelDirInfo **node_first,
1085 struct LevelDirInfo *node_new)
1087 node_new->next = *node_first;
1088 *node_first = node_new;
1091 int numLevelDirInfo(struct LevelDirInfo *node)
1104 boolean validLevelSeries(struct LevelDirInfo *node)
1106 return (node != NULL && !node->node_group && !node->parent_link);
1109 struct LevelDirInfo *getFirstValidLevelSeries(struct LevelDirInfo *node)
1111 if (node == NULL) /* start with first level directory entry */
1112 return getFirstValidLevelSeries(leveldir_first);
1113 else if (node->node_group) /* enter level group (step down into tree) */
1114 return getFirstValidLevelSeries(node->node_group);
1115 else if (node->parent_link) /* skip start entry of level group */
1117 if (node->next) /* get first real level series entry */
1118 return getFirstValidLevelSeries(node->next);
1119 else /* leave empty level group and go on */
1120 return getFirstValidLevelSeries(node->node_parent->next);
1122 else /* this seems to be a regular level series */
1126 struct LevelDirInfo *getLevelDirInfoFirstGroupEntry(struct LevelDirInfo *node)
1131 if (node->node_parent == NULL) /* top level group */
1132 return leveldir_first;
1133 else /* sub level group */
1134 return node->node_parent->node_group;
1137 int numLevelDirInfoInGroup(struct LevelDirInfo *node)
1139 return numLevelDirInfo(getLevelDirInfoFirstGroupEntry(node));
1142 int posLevelDirInfo(struct LevelDirInfo *node)
1144 struct LevelDirInfo *node_cmp = getLevelDirInfoFirstGroupEntry(node);
1149 if (node_cmp == node)
1153 node_cmp = node_cmp->next;
1159 struct LevelDirInfo *getLevelDirInfoFromPos(struct LevelDirInfo *node, int pos)
1161 struct LevelDirInfo *node_default = node;
1173 return node_default;
1176 struct LevelDirInfo *getLevelDirInfoFromFilenameExt(struct LevelDirInfo *node,
1179 if (filename == NULL)
1184 if (node->node_group)
1186 struct LevelDirInfo *node_group;
1188 node_group = getLevelDirInfoFromFilenameExt(node->node_group, filename);
1193 else if (!node->parent_link)
1195 if (strcmp(filename, node->filename) == 0)
1205 struct LevelDirInfo *getLevelDirInfoFromFilename(char *filename)
1207 return getLevelDirInfoFromFilenameExt(leveldir_first, filename);
1210 void dumpLevelDirInfo(struct LevelDirInfo *node, int depth)
1216 for (i=0; i<depth * 3; i++)
1219 printf("filename == '%s'\n", node->filename);
1221 if (node->node_group != NULL)
1222 dumpLevelDirInfo(node->node_group, depth + 1);
1228 void sortLevelDirInfo(struct LevelDirInfo **node_first,
1229 int (*compare_function)(const void *, const void *))
1231 int num_nodes = numLevelDirInfo(*node_first);
1232 struct LevelDirInfo **sort_array;
1233 struct LevelDirInfo *node = *node_first;
1239 /* allocate array for sorting structure pointers */
1240 sort_array = checked_calloc(num_nodes * sizeof(struct LevelDirInfo *));
1242 /* writing structure pointers to sorting array */
1243 while (i < num_nodes && node) /* double boundary check... */
1245 sort_array[i] = node;
1251 /* sorting the structure pointers in the sorting array */
1252 qsort(sort_array, num_nodes, sizeof(struct LevelDirInfo *),
1255 /* update the linkage of list elements with the sorted node array */
1256 for (i=0; i<num_nodes - 1; i++)
1257 sort_array[i]->next = sort_array[i + 1];
1258 sort_array[num_nodes - 1]->next = NULL;
1260 /* update the linkage of the main list anchor pointer */
1261 *node_first = sort_array[0];
1265 /* now recursively sort the level group structures */
1269 if (node->node_group != NULL)
1270 sortLevelDirInfo(&node->node_group, compare_function);
1276 inline void swap_numbers(int *i1, int *i2)
1284 inline void swap_number_pairs(int *x1, int *y1, int *x2, int *y2)
1297 /* ------------------------------------------------------------------------- */
1298 /* the following is only for debugging purpose and normally not used */
1299 /* ------------------------------------------------------------------------- */
1301 #define DEBUG_NUM_TIMESTAMPS 3
1303 void debug_print_timestamp(int counter_nr, char *message)
1305 static long counter[DEBUG_NUM_TIMESTAMPS][2];
1307 if (counter_nr >= DEBUG_NUM_TIMESTAMPS)
1308 Error(ERR_EXIT, "debugging: increase DEBUG_NUM_TIMESTAMPS in misc.c");
1310 counter[counter_nr][0] = Counter();
1313 printf("%s %.2f seconds\n", message,
1314 (float)(counter[counter_nr][0] - counter[counter_nr][1]) / 1000);
1316 counter[counter_nr][1] = Counter();