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;
162 unsigned int RND(unsigned int max)
164 return (random_linux_libc() % max);
167 unsigned int InitRND(long seed)
169 struct timeval current_time;
171 if (seed == NEW_RANDOMIZE)
173 gettimeofday(¤t_time,NULL);
174 srandom_linux_libc((unsigned int) current_time.tv_usec);
175 return (unsigned int)current_time.tv_usec;
179 srandom_linux_libc((unsigned int) seed);
180 return (unsigned int)seed;
188 if (!(pwd = getpwuid(getuid())))
196 static char *home_dir = NULL;
200 if (!(home_dir = getenv("HOME")))
204 if ((pwd = getpwuid(getuid())))
205 home_dir = pwd->pw_dir;
214 char *getPath2(char *path1, char *path2)
216 char *complete_path = checked_malloc(strlen(path1) + strlen(path2) + 2);
218 sprintf(complete_path, "%s/%s", path1, path2);
219 return complete_path;
222 char *getStringCopy(char *s)
224 char *s_copy = checked_malloc(strlen(s) + 1);
230 char *getStringToLower(char *s)
232 char *s_copy = checked_malloc(strlen(s) + 1);
233 char *s_ptr = s_copy;
236 *s_ptr++ = tolower(*s++);
241 void MarkTileDirty(int x, int y)
243 int xx = redraw_x1 + x;
244 int yy = redraw_y1 + y;
249 redraw[xx][yy] = TRUE;
250 redraw_mask |= REDRAW_TILES;
253 void GetOptions(char *argv[])
255 char **options_left = &argv[1];
257 /* initialize global program options */
258 options.display_name = NULL;
259 options.server_host = NULL;
260 options.server_port = 0;
261 options.base_directory = BASE_PATH;
262 options.level_directory = BASE_PATH "/" LEVELS_DIRECTORY;
263 options.serveronly = FALSE;
264 options.network = FALSE;
265 options.verbose = FALSE;
267 while (*options_left)
269 char option_str[MAX_OPTION_LEN];
270 char *option = options_left[0];
271 char *next_option = options_left[1];
272 char *option_arg = NULL;
273 int option_len = strlen(option);
275 strcpy(option_str, option); /* copy argument into buffer */
278 if (strcmp(option, "--") == 0) /* stop scanning arguments */
281 if (option_len >= MAX_OPTION_LEN)
282 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option);
284 if (strncmp(option, "--", 2) == 0) /* treat '--' like '-' */
287 option_arg = strchr(option, '=');
288 if (option_arg == NULL) /* no '=' in option */
289 option_arg = next_option;
292 *option_arg++ = '\0'; /* cut argument from option */
293 if (*option_arg == '\0') /* no argument after '=' */
294 Error(ERR_EXIT_HELP, "option '%s' has invalid argument", option_str);
297 option_len = strlen(option);
299 if (strcmp(option, "-") == 0)
300 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option);
301 else if (strncmp(option, "-help", option_len) == 0)
303 printf("Usage: %s [options] [server.name [port]]\n"
305 " -d, --display machine:0 X server display\n"
306 " -b, --basepath directory alternative base directory\n"
307 " -l, --levels directory alternative level directory\n"
308 " -s, --serveronly only start network server\n"
309 " -n, --network network multiplayer game\n"
310 " -v, --verbose verbose mode\n",
314 else if (strncmp(option, "-display", option_len) == 0)
316 if (option_arg == NULL)
317 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
319 options.display_name = option_arg;
320 if (option_arg == next_option)
323 printf("--display == '%s'\n", options.display_name);
325 else if (strncmp(option, "-basepath", option_len) == 0)
327 if (option_arg == NULL)
328 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
330 options.base_directory = option_arg;
331 if (option_arg == next_option)
334 printf("--basepath == '%s'\n", options.base_directory);
336 /* adjust path for level directory accordingly */
337 options.level_directory = checked_malloc(strlen(options.base_directory) +
338 strlen(LEVELS_DIRECTORY) + 2);
339 sprintf(options.level_directory, "%s/%s",
340 options.base_directory, LEVELS_DIRECTORY);
342 else if (strncmp(option, "-levels", option_len) == 0)
344 if (option_arg == NULL)
345 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
347 options.level_directory = option_arg;
348 if (option_arg == next_option)
351 printf("--levels == '%s'\n", options.level_directory);
353 else if (strncmp(option, "-network", option_len) == 0)
355 printf("--network\n");
357 options.network = TRUE;
359 else if (strncmp(option, "-serveronly", option_len) == 0)
361 printf("--serveronly\n");
363 options.serveronly = TRUE;
365 else if (strncmp(option, "-verbose", option_len) == 0)
367 printf("--verbose\n");
369 options.verbose = TRUE;
371 else if (*option == '-')
372 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option_str);
373 else if (options.server_host == NULL)
375 options.server_host = *options_left;
377 printf("server.name == '%s'\n", options.server_host);
379 else if (options.server_port == 0)
381 options.server_port = atoi(*options_left);
382 if (options.server_port < 1024)
383 Error(ERR_EXIT_HELP, "bad port number '%d'", options.server_port);
385 printf("port == %d\n", options.server_port);
388 Error(ERR_EXIT_HELP, "too many arguments");
394 void Error(int mode, char *format, ...)
396 FILE *output_stream = stderr;
397 char *process_name = "";
399 if (mode & ERR_SOUNDSERVER)
400 process_name = " sound server";
406 fprintf(output_stream, "%s%s: ", program_name, process_name);
409 fprintf(output_stream, "warning: ");
411 va_start(ap, format);
412 vfprintf(output_stream, format, ap);
415 fprintf(output_stream, "\n");
419 fprintf(output_stream, "%s: Try option '--help' for more information.\n",
424 fprintf(output_stream, "%s%s: aborting\n", program_name, process_name);
429 void *checked_malloc(unsigned long size)
436 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
441 void *checked_calloc(unsigned long size)
445 ptr = calloc(1, size);
448 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
453 #define TRANSLATE_KEYSYM_TO_KEYNAME 0
454 #define TRANSLATE_KEYSYM_TO_X11KEYNAME 1
455 #define TRANSLATE_X11KEYNAME_TO_KEYSYM 2
457 void translate_keyname(KeySym *keysym, char **x11name, char **name, int mode)
466 /* normal cursor keys */
467 { XK_Left, "XK_Left", "cursor left" },
468 { XK_Right, "XK_Right", "cursor right" },
469 { XK_Up, "XK_Up", "cursor up" },
470 { XK_Down, "XK_Down", "cursor down" },
472 /* keypad cursor keys */
474 { XK_KP_Left, "XK_KP_Left", "keypad left" },
475 { XK_KP_Right, "XK_KP_Right", "keypad right" },
476 { XK_KP_Up, "XK_KP_Up", "keypad up" },
477 { XK_KP_Down, "XK_KP_Down", "keypad down" },
480 /* other keypad keys */
482 { XK_KP_Enter, "XK_KP_Enter", "keypad enter" },
483 { XK_KP_Add, "XK_KP_Add", "keypad +" },
484 { XK_KP_Subtract, "XK_KP_Subtract", "keypad -" },
485 { XK_KP_Multiply, "XK_KP_Multiply", "keypad mltply" },
486 { XK_KP_Divide, "XK_KP_Divide", "keypad /" },
487 { XK_KP_Separator, "XK_KP_Separator", "keypad ," },
491 { XK_Shift_L, "XK_Shift_L", "left shift" },
492 { XK_Shift_R, "XK_Shift_R", "right shift" },
493 { XK_Control_L, "XK_Control_L", "left control" },
494 { XK_Control_R, "XK_Control_R", "right control" },
495 { XK_Meta_L, "XK_Meta_L", "left meta" },
496 { XK_Meta_R, "XK_Meta_R", "right meta" },
497 { XK_Alt_L, "XK_Alt_L", "left alt" },
498 { XK_Alt_R, "XK_Alt_R", "right alt" },
499 { XK_Mode_switch, "XK_Mode_switch", "mode switch" },
500 { XK_Multi_key, "XK_Multi_key", "multi key" },
502 /* some special keys */
503 { XK_BackSpace, "XK_BackSpace", "backspace" },
504 { XK_Delete, "XK_Delete", "delete" },
505 { XK_Insert, "XK_Insert", "insert" },
506 { XK_Tab, "XK_Tab", "tab" },
507 { XK_Home, "XK_Home", "home" },
508 { XK_End, "XK_End", "end" },
509 { XK_Page_Up, "XK_Page_Up", "page up" },
510 { XK_Page_Down, "XK_Page_Down", "page down" },
511 { XK_space, "XK_space", "space" },
513 /* even more special keys */
514 { XK_adiaeresis, "XK_adiaeresis", "ä" },
515 { XK_odiaeresis, "XK_odiaeresis", "ö" },
516 { XK_udiaeresis, "XK_udiaeresis", "ü" },
517 { XK_apostrophe, "XK_apostrophe", "'" },
518 { XK_plus, "XK_plus", "+" },
519 { XK_minus, "XK_minus", "-" },
520 { XK_comma, "XK_comma", "," },
521 { XK_period, "XK_period", "." },
522 { XK_numbersign, "XK_numbersign", "#" },
523 { XK_less, "XK_less", "less" },
524 { XK_greater, "XK_greater", "greater" },
525 { XK_asciicircum, "XK_asciicircum", "circumflex" },
526 { XK_ssharp, "XK_ssharp", "sharp s" },
528 /* end-of-array identifier */
534 if (mode == TRANSLATE_KEYSYM_TO_KEYNAME)
536 static char name_buffer[30];
537 KeySym key = *keysym;
539 if (key >= XK_A && key <= XK_Z)
540 sprintf(name_buffer, "%c", 'A' + (char)(key - XK_A));
541 else if (key >= XK_a && key <= XK_z)
542 sprintf(name_buffer, "%c", 'a' + (char)(key - XK_a));
543 else if (key >= XK_0 && key <= XK_9)
544 sprintf(name_buffer, "%c", '0' + (char)(key - XK_0));
545 else if (key >= XK_KP_0 && key <= XK_KP_9)
546 sprintf(name_buffer, "keypad %c", '0' + (char)(key - XK_KP_0));
547 else if (key >= XK_F1 && key <= XK_F24)
548 sprintf(name_buffer, "function F%d", (int)(key - XK_F1 + 1));
549 else if (key == KEY_UNDEFINDED)
550 strcpy(name_buffer, "(undefined)");
557 if (key == translate_key[i].keysym)
559 strcpy(name_buffer, translate_key[i].name);
563 while (translate_key[++i].name);
565 if (!translate_key[i].name)
566 strcpy(name_buffer, "(unknown)");
571 else if (mode == TRANSLATE_KEYSYM_TO_X11KEYNAME)
573 static char name_buffer[30];
574 KeySym key = *keysym;
576 if (key >= XK_A && key <= XK_Z)
577 sprintf(name_buffer, "XK_%c", 'A' + (char)(key - XK_A));
578 else if (key >= XK_a && key <= XK_z)
579 sprintf(name_buffer, "XK_%c", 'a' + (char)(key - XK_a));
580 else if (key >= XK_0 && key <= XK_9)
581 sprintf(name_buffer, "XK_%c", '0' + (char)(key - XK_0));
582 else if (key >= XK_KP_0 && key <= XK_KP_9)
583 sprintf(name_buffer, "XK_KP_%c", '0' + (char)(key - XK_KP_0));
584 else if (key >= XK_F1 && key <= XK_F24)
585 sprintf(name_buffer, "XK_F%d", (int)(key - XK_F1 + 1));
586 else if (key == KEY_UNDEFINDED)
587 strcpy(name_buffer, "[undefined]");
594 if (key == translate_key[i].keysym)
596 strcpy(name_buffer, translate_key[i].x11name);
600 while (translate_key[++i].x11name);
602 if (!translate_key[i].x11name)
603 sprintf(name_buffer, "0x%04lx", (unsigned long)key);
606 *x11name = name_buffer;
608 else if (mode == TRANSLATE_X11KEYNAME_TO_KEYSYM)
610 KeySym key = XK_VoidSymbol;
611 char *name_ptr = *x11name;
613 if (strncmp(name_ptr, "XK_", 3) == 0 && strlen(name_ptr) == 4)
615 char c = name_ptr[3];
617 if (c >= 'A' && c <= 'Z')
618 key = XK_A + (KeySym)(c - 'A');
619 else if (c >= 'a' && c <= 'z')
620 key = XK_a + (KeySym)(c - 'a');
621 else if (c >= '0' && c <= '9')
622 key = XK_0 + (KeySym)(c - '0');
624 else if (strncmp(name_ptr, "XK_KP_", 6) == 0 && strlen(name_ptr) == 7)
626 char c = name_ptr[6];
628 if (c >= '0' && c <= '9')
629 key = XK_0 + (KeySym)(c - '0');
631 else if (strncmp(name_ptr, "XK_F", 4) == 0 && strlen(name_ptr) <= 6)
633 char c1 = name_ptr[4];
634 char c2 = name_ptr[5];
637 if ((c1 >= '0' && c1 <= '9') &&
638 ((c2 >= '0' && c1 <= '9') || c2 == '\0'))
639 d = atoi(&name_ptr[4]);
641 if (d >=1 && d <= 24)
642 key = XK_F1 + (KeySym)(d - 1);
644 else if (strncmp(name_ptr, "XK_", 3) == 0)
650 if (strcmp(name_ptr, translate_key[i].x11name) == 0)
652 key = translate_key[i].keysym;
656 while (translate_key[++i].x11name);
658 else if (strncmp(name_ptr, "0x", 2) == 0)
660 unsigned long value = 0;
666 char c = *name_ptr++;
669 if (c >= '0' && c <= '9')
671 else if (c >= 'a' && c <= 'f')
672 d = (int)(c - 'a' + 10);
673 else if (c >= 'A' && c <= 'F')
674 d = (int)(c - 'A' + 10);
682 value = value * 16 + d;
693 char *getKeyNameFromKeySym(KeySym keysym)
697 translate_keyname(&keysym, NULL, &name, TRANSLATE_KEYSYM_TO_KEYNAME);
701 char *getX11KeyNameFromKeySym(KeySym keysym)
705 translate_keyname(&keysym, &x11name, NULL, TRANSLATE_KEYSYM_TO_X11KEYNAME);
709 KeySym getKeySymFromX11KeyName(char *x11name)
713 translate_keyname(&keysym, &x11name, NULL, TRANSLATE_X11KEYNAME_TO_KEYSYM);
717 #define TRANSLATE_JOYSYMBOL_TO_JOYNAME 0
718 #define TRANSLATE_JOYNAME_TO_JOYSYMBOL 1
720 void translate_joyname(int *joysymbol, char **name, int mode)
728 { JOY_LEFT, "joystick_left" },
729 { JOY_RIGHT, "joystick_right" },
730 { JOY_UP, "joystick_up" },
731 { JOY_DOWN, "joystick_down" },
732 { JOY_BUTTON_1, "joystick_button_1" },
733 { JOY_BUTTON_2, "joystick_button_2" },
738 if (mode == TRANSLATE_JOYSYMBOL_TO_JOYNAME)
740 *name = "[undefined]";
744 if (*joysymbol == translate_joy[i].joysymbol)
746 *name = translate_joy[i].name;
751 else if (mode == TRANSLATE_JOYNAME_TO_JOYSYMBOL)
757 if (strcmp(*name, translate_joy[i].name) == 0)
759 *joysymbol = translate_joy[i].joysymbol;
766 char *getJoyNameFromJoySymbol(int joysymbol)
770 translate_joyname(&joysymbol, &name, TRANSLATE_JOYSYMBOL_TO_JOYNAME);
774 int getJoySymbolFromJoyName(char *name)
778 translate_joyname(&joysymbol, &name, TRANSLATE_JOYNAME_TO_JOYSYMBOL);
782 /* the following is only for debugging purpose and normally not used */
783 #define DEBUG_NUM_TIMESTAMPS 3
785 void debug_print_timestamp(int counter_nr, char *message)
787 static long counter[DEBUG_NUM_TIMESTAMPS][2];
789 if (counter_nr >= DEBUG_NUM_TIMESTAMPS)
790 Error(ERR_EXIT, "debugging: increase DEBUG_NUM_TIMESTAMPS in misc.c");
792 counter[counter_nr][0] = Counter();
795 printf("%s %.2f seconds\n", message,
796 (float)(counter[counter_nr][0] - counter[counter_nr][1]) / 1000);
798 counter[counter_nr][1] = Counter();