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>
30 static unsigned long mainCounter(int mode)
32 static struct timeval base_time = { 0, 0 };
33 struct timeval current_time;
34 unsigned long counter_ms;
36 gettimeofday(¤t_time, NULL);
38 if (mode == INIT_COUNTER || current_time.tv_sec < base_time.tv_sec)
39 base_time = current_time;
41 counter_ms = (current_time.tv_sec - base_time.tv_sec) * 1000
42 + (current_time.tv_usec - base_time.tv_usec) / 1000;
44 return counter_ms; /* return milliseconds since last init */
47 void InitCounter() /* set counter back to zero */
49 mainCounter(INIT_COUNTER);
52 unsigned long Counter() /* get milliseconds since last call of InitCounter() */
54 return(mainCounter(READ_COUNTER));
57 static void sleep_milliseconds(unsigned long milliseconds_delay)
59 if (milliseconds_delay < 5)
61 /* we want to wait only a few ms -- if we assume that we have a
62 kernel timer resolution of 10 ms, we would wait far to long;
63 therefore it's better to do a short interval of busy waiting
64 to get our sleeping time more accurate */
66 unsigned long base_counter = Counter(), actual_counter = Counter();
68 while (actual_counter < base_counter + milliseconds_delay &&
69 actual_counter >= base_counter)
70 actual_counter = Counter();
76 delay.tv_sec = milliseconds_delay / 1000;
77 delay.tv_usec = 1000 * (milliseconds_delay % 1000);
79 if (select(0, NULL, NULL, NULL, &delay) != 0)
80 Error(ERR_WARN, "sleep_milliseconds(): select() failed");
84 void Delay(unsigned long delay) /* Sleep specified number of milliseconds */
86 sleep_milliseconds(delay);
89 boolean FrameReached(unsigned long *frame_counter_var,
90 unsigned long frame_delay)
92 unsigned long actual_frame_counter = FrameCounter;
94 if (actual_frame_counter < *frame_counter_var+frame_delay &&
95 actual_frame_counter >= *frame_counter_var)
98 *frame_counter_var = actual_frame_counter;
102 boolean DelayReached(unsigned long *counter_var,
105 unsigned long actual_counter = Counter();
107 if (actual_counter < *counter_var + delay &&
108 actual_counter >= *counter_var)
111 *counter_var = actual_counter;
115 void WaitUntilDelayReached(unsigned long *counter_var, unsigned long delay)
117 unsigned long actual_counter;
121 actual_counter = Counter();
123 if (actual_counter < *counter_var + delay &&
124 actual_counter >= *counter_var)
125 sleep_milliseconds((*counter_var + delay - actual_counter) / 2);
130 *counter_var = actual_counter;
133 char *int2str(int number, int size)
142 sprintf(s, " %09d", number);
143 return &s[strlen(s) - size];
147 sprintf(s, "%d", number);
152 unsigned int SimpleRND(unsigned int max)
154 static unsigned long root = 654321;
155 struct timeval current_time;
157 gettimeofday(¤t_time,NULL);
158 root = root * 4253261 + current_time.tv_sec + current_time.tv_usec;
163 static unsigned int last_RND_value = 0;
165 unsigned int last_RND()
167 return last_RND_value;
171 unsigned int RND(unsigned int max)
174 return (last_RND_value = random_linux_libc() % max);
176 return (random_linux_libc() % max);
180 unsigned int InitRND(long seed)
182 struct timeval current_time;
184 if (seed == NEW_RANDOMIZE)
186 gettimeofday(¤t_time,NULL);
187 srandom_linux_libc((unsigned int) current_time.tv_usec);
188 return (unsigned int)current_time.tv_usec;
192 srandom_linux_libc((unsigned int) seed);
193 return (unsigned int)seed;
201 if (!(pwd = getpwuid(getuid())))
209 static char *home_dir = NULL;
213 if (!(home_dir = getenv("HOME")))
217 if ((pwd = getpwuid(getuid())))
218 home_dir = pwd->pw_dir;
227 char *getPath2(char *path1, char *path2)
229 char *complete_path = checked_malloc(strlen(path1) + strlen(path2) + 2);
231 sprintf(complete_path, "%s/%s", path1, path2);
232 return complete_path;
235 char *getStringCopy(char *s)
237 char *s_copy = checked_malloc(strlen(s) + 1);
243 char *getStringToLower(char *s)
245 char *s_copy = checked_malloc(strlen(s) + 1);
246 char *s_ptr = s_copy;
249 *s_ptr++ = tolower(*s++);
254 void MarkTileDirty(int x, int y)
256 int xx = redraw_x1 + x;
257 int yy = redraw_y1 + y;
262 redraw[xx][yy] = TRUE;
263 redraw_mask |= REDRAW_TILES;
266 void GetOptions(char *argv[])
268 char **options_left = &argv[1];
270 /* initialize global program options */
271 options.display_name = NULL;
272 options.server_host = NULL;
273 options.server_port = 0;
274 options.base_directory = BASE_PATH;
275 options.level_directory = BASE_PATH "/" LEVELS_DIRECTORY;
276 options.serveronly = FALSE;
277 options.network = FALSE;
278 options.verbose = FALSE;
280 while (*options_left)
282 char option_str[MAX_OPTION_LEN];
283 char *option = options_left[0];
284 char *next_option = options_left[1];
285 char *option_arg = NULL;
286 int option_len = strlen(option);
288 strcpy(option_str, option); /* copy argument into buffer */
291 if (strcmp(option, "--") == 0) /* stop scanning arguments */
294 if (option_len >= MAX_OPTION_LEN)
295 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option);
297 if (strncmp(option, "--", 2) == 0) /* treat '--' like '-' */
300 option_arg = strchr(option, '=');
301 if (option_arg == NULL) /* no '=' in option */
302 option_arg = next_option;
305 *option_arg++ = '\0'; /* cut argument from option */
306 if (*option_arg == '\0') /* no argument after '=' */
307 Error(ERR_EXIT_HELP, "option '%s' has invalid argument", option_str);
310 option_len = strlen(option);
312 if (strcmp(option, "-") == 0)
313 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option);
314 else if (strncmp(option, "-help", option_len) == 0)
316 printf("Usage: %s [options] [server.name [port]]\n"
318 " -d, --display machine:0 X server display\n"
319 " -b, --basepath directory alternative base directory\n"
320 " -l, --levels directory alternative level directory\n"
321 " -s, --serveronly only start network server\n"
322 " -n, --network network multiplayer game\n"
323 " -v, --verbose verbose mode\n",
327 else if (strncmp(option, "-display", option_len) == 0)
329 if (option_arg == NULL)
330 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
332 options.display_name = option_arg;
333 if (option_arg == next_option)
336 printf("--display == '%s'\n", options.display_name);
338 else if (strncmp(option, "-basepath", option_len) == 0)
340 if (option_arg == NULL)
341 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
343 options.base_directory = option_arg;
344 if (option_arg == next_option)
347 printf("--basepath == '%s'\n", options.base_directory);
349 /* adjust path for level directory accordingly */
350 options.level_directory = checked_malloc(strlen(options.base_directory) +
351 strlen(LEVELS_DIRECTORY) + 2);
352 sprintf(options.level_directory, "%s/%s",
353 options.base_directory, LEVELS_DIRECTORY);
355 else if (strncmp(option, "-levels", option_len) == 0)
357 if (option_arg == NULL)
358 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
360 options.level_directory = option_arg;
361 if (option_arg == next_option)
364 printf("--levels == '%s'\n", options.level_directory);
366 else if (strncmp(option, "-network", option_len) == 0)
368 printf("--network\n");
370 options.network = TRUE;
372 else if (strncmp(option, "-serveronly", option_len) == 0)
374 printf("--serveronly\n");
376 options.serveronly = TRUE;
378 else if (strncmp(option, "-verbose", option_len) == 0)
380 printf("--verbose\n");
382 options.verbose = TRUE;
384 else if (*option == '-')
385 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option_str);
386 else if (options.server_host == NULL)
388 options.server_host = *options_left;
390 printf("server.name == '%s'\n", options.server_host);
392 else if (options.server_port == 0)
394 options.server_port = atoi(*options_left);
395 if (options.server_port < 1024)
396 Error(ERR_EXIT_HELP, "bad port number '%d'", options.server_port);
398 printf("port == %d\n", options.server_port);
401 Error(ERR_EXIT_HELP, "too many arguments");
407 void Error(int mode, char *format, ...)
409 FILE *output_stream = stderr;
410 char *process_name = "";
412 if (mode & ERR_SOUNDSERVER)
413 process_name = " sound server";
419 fprintf(output_stream, "%s%s: ", program_name, process_name);
422 fprintf(output_stream, "warning: ");
424 va_start(ap, format);
425 vfprintf(output_stream, format, ap);
428 fprintf(output_stream, "\n");
432 fprintf(output_stream, "%s: Try option '--help' for more information.\n",
437 fprintf(output_stream, "%s%s: aborting\n", program_name, process_name);
442 void *checked_malloc(unsigned long size)
449 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
454 void *checked_calloc(unsigned long size)
458 ptr = calloc(1, size);
461 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
466 #define TRANSLATE_KEYSYM_TO_KEYNAME 0
467 #define TRANSLATE_KEYSYM_TO_X11KEYNAME 1
468 #define TRANSLATE_X11KEYNAME_TO_KEYSYM 2
470 void translate_keyname(KeySym *keysym, char **x11name, char **name, int mode)
479 /* normal cursor keys */
480 { XK_Left, "XK_Left", "cursor left" },
481 { XK_Right, "XK_Right", "cursor right" },
482 { XK_Up, "XK_Up", "cursor up" },
483 { XK_Down, "XK_Down", "cursor down" },
485 /* keypad cursor keys */
487 { XK_KP_Left, "XK_KP_Left", "keypad left" },
488 { XK_KP_Right, "XK_KP_Right", "keypad right" },
489 { XK_KP_Up, "XK_KP_Up", "keypad up" },
490 { XK_KP_Down, "XK_KP_Down", "keypad down" },
493 /* other keypad keys */
495 { XK_KP_Enter, "XK_KP_Enter", "keypad enter" },
496 { XK_KP_Add, "XK_KP_Add", "keypad +" },
497 { XK_KP_Subtract, "XK_KP_Subtract", "keypad -" },
498 { XK_KP_Multiply, "XK_KP_Multiply", "keypad mltply" },
499 { XK_KP_Divide, "XK_KP_Divide", "keypad /" },
500 { XK_KP_Separator, "XK_KP_Separator", "keypad ," },
504 { XK_Shift_L, "XK_Shift_L", "left shift" },
505 { XK_Shift_R, "XK_Shift_R", "right shift" },
506 { XK_Control_L, "XK_Control_L", "left control" },
507 { XK_Control_R, "XK_Control_R", "right control" },
508 { XK_Meta_L, "XK_Meta_L", "left meta" },
509 { XK_Meta_R, "XK_Meta_R", "right meta" },
510 { XK_Alt_L, "XK_Alt_L", "left alt" },
511 { XK_Alt_R, "XK_Alt_R", "right alt" },
512 { XK_Mode_switch, "XK_Mode_switch", "mode switch" },
513 { XK_Multi_key, "XK_Multi_key", "multi key" },
515 /* some special keys */
516 { XK_BackSpace, "XK_BackSpace", "backspace" },
517 { XK_Delete, "XK_Delete", "delete" },
518 { XK_Insert, "XK_Insert", "insert" },
519 { XK_Tab, "XK_Tab", "tab" },
520 { XK_Home, "XK_Home", "home" },
521 { XK_End, "XK_End", "end" },
522 { XK_Page_Up, "XK_Page_Up", "page up" },
523 { XK_Page_Down, "XK_Page_Down", "page down" },
524 { XK_space, "XK_space", "space" },
526 /* even more special keys */
527 { XK_adiaeresis, "XK_adiaeresis", "ä" },
528 { XK_odiaeresis, "XK_odiaeresis", "ö" },
529 { XK_udiaeresis, "XK_udiaeresis", "ü" },
530 { XK_apostrophe, "XK_apostrophe", "'" },
531 { XK_plus, "XK_plus", "+" },
532 { XK_minus, "XK_minus", "-" },
533 { XK_comma, "XK_comma", "," },
534 { XK_period, "XK_period", "." },
535 { XK_numbersign, "XK_numbersign", "#" },
536 { XK_less, "XK_less", "less" },
537 { XK_greater, "XK_greater", "greater" },
538 { XK_asciicircum, "XK_asciicircum", "circumflex" },
539 { XK_ssharp, "XK_ssharp", "sharp s" },
541 /* end-of-array identifier */
547 if (mode == TRANSLATE_KEYSYM_TO_KEYNAME)
549 static char name_buffer[30];
550 KeySym key = *keysym;
552 if (key >= XK_A && key <= XK_Z)
553 sprintf(name_buffer, "%c", 'A' + (char)(key - XK_A));
554 else if (key >= XK_a && key <= XK_z)
555 sprintf(name_buffer, "%c", 'a' + (char)(key - XK_a));
556 else if (key >= XK_0 && key <= XK_9)
557 sprintf(name_buffer, "%c", '0' + (char)(key - XK_0));
558 else if (key >= XK_KP_0 && key <= XK_KP_9)
559 sprintf(name_buffer, "keypad %c", '0' + (char)(key - XK_KP_0));
560 else if (key >= XK_F1 && key <= XK_F24)
561 sprintf(name_buffer, "function F%d", (int)(key - XK_F1 + 1));
562 else if (key == KEY_UNDEFINDED)
563 strcpy(name_buffer, "(undefined)");
570 if (key == translate_key[i].keysym)
572 strcpy(name_buffer, translate_key[i].name);
576 while (translate_key[++i].name);
578 if (!translate_key[i].name)
579 strcpy(name_buffer, "(unknown)");
584 else if (mode == TRANSLATE_KEYSYM_TO_X11KEYNAME)
586 static char name_buffer[30];
587 KeySym key = *keysym;
589 if (key >= XK_A && key <= XK_Z)
590 sprintf(name_buffer, "XK_%c", 'A' + (char)(key - XK_A));
591 else if (key >= XK_a && key <= XK_z)
592 sprintf(name_buffer, "XK_%c", 'a' + (char)(key - XK_a));
593 else if (key >= XK_0 && key <= XK_9)
594 sprintf(name_buffer, "XK_%c", '0' + (char)(key - XK_0));
595 else if (key >= XK_KP_0 && key <= XK_KP_9)
596 sprintf(name_buffer, "XK_KP_%c", '0' + (char)(key - XK_KP_0));
597 else if (key >= XK_F1 && key <= XK_F24)
598 sprintf(name_buffer, "XK_F%d", (int)(key - XK_F1 + 1));
599 else if (key == KEY_UNDEFINDED)
600 strcpy(name_buffer, "[undefined]");
607 if (key == translate_key[i].keysym)
609 strcpy(name_buffer, translate_key[i].x11name);
613 while (translate_key[++i].x11name);
615 if (!translate_key[i].x11name)
616 sprintf(name_buffer, "0x%04lx", (unsigned long)key);
619 *x11name = name_buffer;
621 else if (mode == TRANSLATE_X11KEYNAME_TO_KEYSYM)
623 KeySym key = XK_VoidSymbol;
624 char *name_ptr = *x11name;
626 if (strncmp(name_ptr, "XK_", 3) == 0 && strlen(name_ptr) == 4)
628 char c = name_ptr[3];
630 if (c >= 'A' && c <= 'Z')
631 key = XK_A + (KeySym)(c - 'A');
632 else if (c >= 'a' && c <= 'z')
633 key = XK_a + (KeySym)(c - 'a');
634 else if (c >= '0' && c <= '9')
635 key = XK_0 + (KeySym)(c - '0');
637 else if (strncmp(name_ptr, "XK_KP_", 6) == 0 && strlen(name_ptr) == 7)
639 char c = name_ptr[6];
641 if (c >= '0' && c <= '9')
642 key = XK_0 + (KeySym)(c - '0');
644 else if (strncmp(name_ptr, "XK_F", 4) == 0 && strlen(name_ptr) <= 6)
646 char c1 = name_ptr[4];
647 char c2 = name_ptr[5];
650 if ((c1 >= '0' && c1 <= '9') &&
651 ((c2 >= '0' && c1 <= '9') || c2 == '\0'))
652 d = atoi(&name_ptr[4]);
654 if (d >=1 && d <= 24)
655 key = XK_F1 + (KeySym)(d - 1);
657 else if (strncmp(name_ptr, "XK_", 3) == 0)
663 if (strcmp(name_ptr, translate_key[i].x11name) == 0)
665 key = translate_key[i].keysym;
669 while (translate_key[++i].x11name);
671 else if (strncmp(name_ptr, "0x", 2) == 0)
673 unsigned long value = 0;
679 char c = *name_ptr++;
682 if (c >= '0' && c <= '9')
684 else if (c >= 'a' && c <= 'f')
685 d = (int)(c - 'a' + 10);
686 else if (c >= 'A' && c <= 'F')
687 d = (int)(c - 'A' + 10);
695 value = value * 16 + d;
706 char *getKeyNameFromKeySym(KeySym keysym)
710 translate_keyname(&keysym, NULL, &name, TRANSLATE_KEYSYM_TO_KEYNAME);
714 char *getX11KeyNameFromKeySym(KeySym keysym)
718 translate_keyname(&keysym, &x11name, NULL, TRANSLATE_KEYSYM_TO_X11KEYNAME);
722 KeySym getKeySymFromX11KeyName(char *x11name)
726 translate_keyname(&keysym, &x11name, NULL, TRANSLATE_X11KEYNAME_TO_KEYSYM);
730 #define TRANSLATE_JOYSYMBOL_TO_JOYNAME 0
731 #define TRANSLATE_JOYNAME_TO_JOYSYMBOL 1
733 void translate_joyname(int *joysymbol, char **name, int mode)
741 { JOY_LEFT, "joystick_left" },
742 { JOY_RIGHT, "joystick_right" },
743 { JOY_UP, "joystick_up" },
744 { JOY_DOWN, "joystick_down" },
745 { JOY_BUTTON_1, "joystick_button_1" },
746 { JOY_BUTTON_2, "joystick_button_2" },
751 if (mode == TRANSLATE_JOYSYMBOL_TO_JOYNAME)
753 *name = "[undefined]";
757 if (*joysymbol == translate_joy[i].joysymbol)
759 *name = translate_joy[i].name;
764 else if (mode == TRANSLATE_JOYNAME_TO_JOYSYMBOL)
770 if (strcmp(*name, translate_joy[i].name) == 0)
772 *joysymbol = translate_joy[i].joysymbol;
779 char *getJoyNameFromJoySymbol(int joysymbol)
783 translate_joyname(&joysymbol, &name, TRANSLATE_JOYSYMBOL_TO_JOYNAME);
787 int getJoySymbolFromJoyName(char *name)
791 translate_joyname(&joysymbol, &name, TRANSLATE_JOYNAME_TO_JOYSYMBOL);
795 /* the following is only for debugging purpose and normally not used */
796 #define DEBUG_NUM_TIMESTAMPS 3
798 void debug_print_timestamp(int counter_nr, char *message)
800 static long counter[DEBUG_NUM_TIMESTAMPS][2];
802 if (counter_nr >= DEBUG_NUM_TIMESTAMPS)
803 Error(ERR_EXIT, "debugging: increase DEBUG_NUM_TIMESTAMPS in misc.c");
805 counter[counter_nr][0] = Counter();
808 printf("%s %.2f seconds\n", message,
809 (float)(counter[counter_nr][0] - counter[counter_nr][1]) / 1000);
811 counter[counter_nr][1] = Counter();