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>
28 static unsigned long mainCounter(int mode)
30 static struct timeval base_time = { 0, 0 };
31 struct timeval current_time;
32 unsigned long counter_ms;
34 gettimeofday(¤t_time, NULL);
36 if (mode == INIT_COUNTER || current_time.tv_sec < base_time.tv_sec)
37 base_time = current_time;
39 counter_ms = (current_time.tv_sec - base_time.tv_sec) * 1000
40 + (current_time.tv_usec - base_time.tv_usec) / 1000;
42 return counter_ms; /* return milliseconds since last init */
45 void InitCounter() /* set counter back to zero */
47 mainCounter(INIT_COUNTER);
50 unsigned long Counter() /* get milliseconds since last call of InitCounter() */
52 return(mainCounter(READ_COUNTER));
55 static void sleep_milliseconds(unsigned long milliseconds_delay)
57 if (milliseconds_delay < 5)
59 /* we want to wait only a few ms -- if we assume that we have a
60 kernel timer resolution of 10 ms, we would wait far to long;
61 therefore it's better to do a short interval of busy waiting
62 to get our sleeping time more accurate */
64 unsigned long base_counter = Counter(), actual_counter = Counter();
66 while (actual_counter < base_counter + milliseconds_delay &&
67 actual_counter >= base_counter)
68 actual_counter = Counter();
74 delay.tv_sec = milliseconds_delay / 1000;
75 delay.tv_usec = 1000 * (milliseconds_delay % 1000);
77 if (select(0, NULL, NULL, NULL, &delay) != 0)
78 Error(ERR_RETURN, "sleep_milliseconds(): select() failed");
82 void Delay(unsigned long delay) /* Sleep specified number of milliseconds */
84 sleep_milliseconds(delay);
87 boolean FrameReached(unsigned long *frame_counter_var,
88 unsigned long frame_delay)
90 unsigned long actual_frame_counter = FrameCounter;
92 if (actual_frame_counter < *frame_counter_var+frame_delay &&
93 actual_frame_counter >= *frame_counter_var)
96 *frame_counter_var = actual_frame_counter;
100 boolean DelayReached(unsigned long *counter_var,
103 unsigned long actual_counter = Counter();
105 if (actual_counter < *counter_var + delay &&
106 actual_counter >= *counter_var)
109 *counter_var = actual_counter;
113 void WaitUntilDelayReached(unsigned long *counter_var, unsigned long delay)
115 unsigned long actual_counter;
119 actual_counter = Counter();
121 if (actual_counter < *counter_var + delay &&
122 actual_counter >= *counter_var)
123 sleep_milliseconds((*counter_var + delay - actual_counter) / 2);
128 *counter_var = actual_counter;
131 char *int2str(int ct, int nr)
135 sprintf(str,"%09d",ct);
136 return(&str[strlen(str)-nr]);
139 unsigned int SimpleRND(unsigned int max)
141 static unsigned long root = 654321;
142 struct timeval current_time;
144 gettimeofday(¤t_time,NULL);
145 root = root * 4253261 + current_time.tv_sec + current_time.tv_usec;
149 unsigned int RND(unsigned int max)
151 return(random_linux_libc() % max);
154 unsigned int InitRND(long seed)
156 struct timeval current_time;
158 if (seed==NEW_RANDOMIZE)
160 gettimeofday(¤t_time,NULL);
161 srandom_linux_libc((unsigned int) current_time.tv_usec);
162 return((unsigned int) current_time.tv_usec);
166 srandom_linux_libc((unsigned int) seed);
167 return((unsigned int) seed);
175 if (!(pwd=getpwuid(getuid())))
178 return(pwd->pw_name);
181 void MarkTileDirty(int x, int y)
183 int xx = redraw_x1 + x;
184 int yy = redraw_y1 + y;
188 redraw[xx][yy] = TRUE;
190 redraw_mask |= REDRAW_TILES;
194 void GetOptions(char *argv[])
196 char **options_left = &argv[1];
198 /* initialize global program options */
199 options.display_name = NULL;
200 options.server_host = NULL;
201 options.server_port = 0;
202 options.serveronly = FALSE;
203 options.network = FALSE;
204 options.verbose = FALSE;
206 while (*options_left)
208 char option_str[MAX_OPTION_LEN];
209 char *option = options_left[0];
210 char *next_option = options_left[1];
211 char *option_arg = NULL;
212 int option_len = strlen(option);
214 strcpy(option_str, option); /* copy argument into buffer */
217 if (strcmp(option, "--") == 0) /* stop scanning arguments */
220 if (option_len >= MAX_OPTION_LEN)
221 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option);
223 if (strncmp(option, "--", 2) == 0) /* treat '--' like '-' */
226 option_arg = strchr(option, '=');
227 if (option_arg == NULL) /* no '=' in option */
228 option_arg = next_option;
231 *option_arg++ = '\0'; /* cut argument from option */
232 if (*option_arg == '\0') /* no argument after '=' */
233 Error(ERR_EXIT_HELP, "option '%s' has invalid argument", option_str);
236 option_len = strlen(option);
238 if (strcmp(option, "-") == 0)
239 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option);
240 else if (strncmp(option, "-help", option_len) == 0)
242 printf("Usage: %s [options] [server.name [port]]\n"
244 " -d, --display machine:0 X server display\n"
245 " -l, --levels directory alternative level directory\n"
246 " -s, --serveronly only start network server\n"
247 " -n, --network network multiplayer game\n"
248 " -v, --verbose verbose mode\n",
252 else if (strncmp(option, "-display", option_len) == 0)
254 if (option_arg == NULL)
255 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
257 options.display_name = option_arg;
258 if (option_arg == next_option)
261 printf("--display == '%s'\n", options.display_name);
263 else if (strncmp(option, "-levels", option_len) == 0)
265 if (option_arg == NULL)
266 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
268 level_directory = option_arg;
269 if (option_arg == next_option)
272 printf("--levels == '%s'\n", level_directory);
274 else if (strncmp(option, "-network", option_len) == 0)
276 printf("--network\n");
278 options.network = TRUE;
280 else if (strncmp(option, "-serveronly", option_len) == 0)
282 printf("--serveronly\n");
284 options.serveronly = TRUE;
286 else if (strncmp(option, "-verbose", option_len) == 0)
288 printf("--verbose\n");
290 options.verbose = TRUE;
292 else if (*option == '-')
293 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option_str);
294 else if (options.server_host == NULL)
296 options.server_host = *options_left;
298 printf("server.name == '%s'\n", options.server_host);
300 else if (options.server_port == 0)
302 options.server_port = atoi(*options_left);
303 if (options.server_port < 1024)
304 Error(ERR_EXIT_HELP, "bad port number '%d'", options.server_port);
306 printf("port == %d\n", options.server_port);
309 Error(ERR_EXIT_HELP, "too many arguments");
315 void Error(int mode, char *format_str, ...)
317 FILE *output_stream = stderr;
318 char *process_name = "";
320 if (mode == ERR_EXIT_SOUNDSERVER)
321 process_name = " sound server";
331 fprintf(output_stream, "%s%s: ", program_name, process_name);
333 va_start(ap, format_str); /* ap points to first unnamed argument */
335 for(format_ptr=format_str; *format_ptr; format_ptr++)
337 if (*format_ptr != '%')
339 fprintf(output_stream, "%c", *format_ptr);
343 switch(*++format_ptr)
346 i_value = va_arg(ap, int);
347 fprintf(output_stream, "%d", i_value);
351 d_value = va_arg(ap, double);
352 fprintf(output_stream, "%f", d_value);
356 s_value = va_arg(ap, char *);
357 fprintf(output_stream, "%s", s_value);
361 fprintf(stderr, "\n%s: Error(): invalid format string: %s\n",
362 program_name, format_str);
369 fprintf(output_stream, "\n");
372 if (mode == ERR_EXIT_HELP)
373 fprintf(output_stream, "%s: Try option '--help' for more information.\n",
376 if (mode != ERR_RETURN)
378 fprintf(output_stream, "%s%s: aborting\n", program_name, process_name);
383 void *checked_malloc(unsigned long size)
390 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
395 char *getKeySymName(KeySym key)
397 static char key_name[20];
404 /* normal cursor keys */
405 { XK_Left, "cursor left" },
406 { XK_Right, "cursor right" },
407 { XK_Up, "cursor up" },
408 { XK_Down, "cursor down" },
410 /* keypad cursor keys */
412 { XK_KP_Left, "keypad left" },
413 { XK_KP_Right, "keypad right" },
414 { XK_KP_Up, "keypad up" },
415 { XK_KP_Down, "keypad down" },
418 /* other keypad keys */
420 { XK_KP_Enter, "keypad enter" },
421 { XK_KP_Add, "keypad +" },
422 { XK_KP_Subtract, "keypad -" },
423 { XK_KP_Multiply, "keypad mltply" },
424 { XK_KP_Divide, "keypad /" },
425 { XK_KP_Separator, "keypad ," },
429 { XK_Shift_L, "left shift" },
430 { XK_Shift_R, "right shift" },
431 { XK_Control_L, "left control" },
432 { XK_Control_R, "right control" },
433 { XK_Meta_L, "left meta" },
434 { XK_Meta_R, "right meta" },
435 { XK_Alt_L, "left alt" },
436 { XK_Alt_R, "right alt" },
437 { XK_Mode_switch, "mode switch" },
438 { XK_Multi_key, "multi key" },
440 /* some special keys */
441 { XK_BackSpace, "backspace" },
442 { XK_Delete, "delete" },
443 { XK_Insert, "insert" },
447 { XK_Page_Up, "page up" },
448 { XK_Page_Down, "page down" },
449 { XK_space, "space" },
451 /* even more special keys */
452 { XK_adiaeresis, "ä" },
453 { XK_odiaeresis, "ö" },
454 { XK_udiaeresis, "ü" },
455 { XK_apostrophe, "'" },
460 { XK_numbersign, "#" },
462 { XK_greater, "greater" },
463 { XK_asciicircum, "circumflex" },
464 { XK_ssharp, "sharp s" },
466 /* end-of-array identifier */
470 if (key >= XK_A && key <= XK_Z)
472 sprintf(key_name, "%c", 'A' + (char)(key - XK_A));
475 else if (key >= XK_a && key <= XK_z)
477 sprintf(key_name, "%c", 'a' + (char)(key - XK_a));
480 else if (key >= XK_0 && key <= XK_9)
482 sprintf(key_name, "%c", '0' + (char)(key - XK_0));
485 else if (key >= XK_KP_0 && key <= XK_KP_9)
487 sprintf(key_name, "keypad %c", '0' + (char)(key - XK_KP_0));
490 else if (key >= XK_F1 && key <= XK_F24)
492 sprintf(key_name, "function F%d", (int)(key - XK_F1 + 1));
501 if (key == translate[i].keysym)
502 return translate[i].name;
504 while (translate[++i].name);
506 sprintf(key_name, "(unknown)");