/***********************************************************
-* Rocks'n'Diamonds -- McDuffin Strikes Back! *
+* Artsoft Retro-Game Library *
*----------------------------------------------------------*
-* (c) 1995-98 Artsoft Entertainment *
-* Holger Schemel *
-* Oststrasse 11a *
-* 33604 Bielefeld *
-* phone: ++49 +521 290471 *
-* email: aeglos@valinor.owl.de *
+* (c) 1994-2002 Artsoft Entertainment *
+* Holger Schemel *
+* Detmolder Strasse 189 *
+* 33604 Bielefeld *
+* Germany *
+* e-mail: info@artsoft.org *
*----------------------------------------------------------*
-* misc.c *
+* misc.c *
***********************************************************/
#include <time.h>
#include <sys/types.h>
#include <stdarg.h>
#include <ctype.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "platform.h"
#if !defined(PLATFORM_WIN32)
#include <pwd.h>
#include <sys/param.h>
#endif
-#include "libgame.h"
-
-#include "main_TMP.h"
-
#include "misc.h"
+#include "setup.h"
+#include "random.h"
-#if 0
-#include "joystick_TMP.h"
-#endif
#if defined(PLATFORM_MSDOS)
volatile unsigned long counter = 0;
{
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)
{
#if defined(TARGET_SDL)
SDL_Delay(milliseconds_delay);
+#elif defined(TARGET_ALLEGRO)
+ rest(milliseconds_delay);
#else
struct timeval delay;
{
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,
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)
#if defined(PLATFORM_WIN32)
return ANONYMOUS_NAME;
#else
- struct passwd *pwd;
+ static char *login_name = NULL;
- if ((pwd = getpwuid(getuid())) == NULL)
- return ANONYMOUS_NAME;
- else
- return pwd->pw_name;
+ if (login_name == NULL)
+ {
+ struct passwd *pwd;
+
+ if ((pwd = getpwuid(getuid())) == NULL)
+ login_name = ANONYMOUS_NAME;
+ else
+ login_name = getStringCopy(pwd->pw_name);
+ }
+
+ return login_name;
#endif
}
#if defined(PLATFORM_UNIX)
static char *home_dir = NULL;
- if (!home_dir)
+ if (home_dir == NULL)
{
- if (!(home_dir = getenv("HOME")))
+ if ((home_dir = getenv("HOME")) == NULL)
{
struct passwd *pwd;
- if ((pwd = getpwuid(getuid())))
- home_dir = pwd->pw_dir;
- else
+ if ((pwd = getpwuid(getuid())) == NULL)
home_dir = ".";
+ else
+ home_dir = getStringCopy(pwd->pw_dir);
}
}
return s_copy;
}
-void MarkTileDirty(int x, int y)
-{
- int xx = redraw_x1 + x;
- int yy = redraw_y1 + y;
-
- if (!redraw[xx][yy])
- redraw_tiles++;
-
- redraw[xx][yy] = TRUE;
- redraw_mask |= REDRAW_TILES;
-}
-
-void SetBorderElement()
-{
- int x, y;
-
- BorderElement = EL_LEERRAUM;
-
- for(y=0; y<lev_fieldy && BorderElement == EL_LEERRAUM; y++)
- {
- for(x=0; x<lev_fieldx; x++)
- {
- if (!IS_MASSIVE(Feld[x][y]))
- BorderElement = EL_BETON;
-
- if (y != 0 && y != lev_fieldy - 1 && x != lev_fieldx - 1)
- x = lev_fieldx - 2;
- }
- }
-}
-
void GetOptions(char *argv[])
{
char **options_left = &argv[1];
options.ro_base_directory = RO_BASE_PATH;
options.rw_base_directory = RW_BASE_PATH;
options.level_directory = RO_BASE_PATH "/" LEVELS_DIRECTORY;
+ options.graphics_directory = RO_BASE_PATH "/" GRAPHICS_DIRECTORY;
+ options.sounds_directory = RO_BASE_PATH "/" SOUNDS_DIRECTORY;
+ options.music_directory = RO_BASE_PATH "/" MUSIC_DIRECTORY;
options.serveronly = FALSE;
options.network = FALSE;
options.verbose = FALSE;
options.debug = FALSE;
-
- /* initialize some more global variables */
- global.frames_per_second = 0;
- global.fps_slowdown = FALSE;
- global.fps_slowdown_factor = 1;
+ options.debug_command = NULL;
while (*options_left)
{
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 machine:0 X server display\n"
- " -b, --basepath directory alternative base directory\n"
- " -l, --level directory alternative level directory\n"
- " -s, --serveronly only start network server\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 sounds directory\n"
+ " -m, --music <directory> alternative music directory\n"
" -n, --network network multiplayer game\n"
- " -v, --verbose verbose mode\n",
- program_name);
+ " --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)
if (option_arg == next_option)
options_left++;
}
+ else if (strncmp(option, "-graphics", option_len) == 0)
+ {
+ if (option_arg == NULL)
+ Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
+
+ options.graphics_directory = option_arg;
+ if (option_arg == next_option)
+ options_left++;
+ }
+ else if (strncmp(option, "-sounds", option_len) == 0)
+ {
+ if (option_arg == NULL)
+ Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
+
+ options.sounds_directory = option_arg;
+ if (option_arg == next_option)
+ options_left++;
+ }
+ else if (strncmp(option, "-music", option_len) == 0)
+ {
+ if (option_arg == NULL)
+ Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
+
+ options.music_directory = option_arg;
+ if (option_arg == next_option)
+ options_left++;
+ }
else if (strncmp(option, "-network", option_len) == 0)
{
options.network = TRUE;
{
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);
}
}
+/* used by SetError() and GetError() to store internal error messages */
+static char internal_error[1024]; /* this is bad */
+
+void SetError(char *format, ...)
+{
+ va_list ap;
+
+ va_start(ap, format);
+ vsprintf(internal_error, format, ap);
+ va_end(ap);
+}
+
+char *GetError()
+{
+ return internal_error;
+}
+
void Error(int mode, char *format, ...)
{
char *process_name = "";
FILE *error = stderr;
+ char *newline = "\n";
/* display warnings only when running in verbose mode */
if (mode & ERR_WARN && !options.verbose)
return;
-#if !defined(PLATFORM_UNIX)
+#if defined(PLATFORM_MSDOS)
+ newline = "\r\n";
+
if ((error = openErrorFile()) == NULL)
{
- printf("Cannot write to error output file!\n");
- CloseAllAndExit(1);
+ printf("Cannot write to error output file!%s", newline);
+ program.exit_function(1);
}
#endif
{
va_list ap;
- fprintf(error, "%s%s: ", program_name, process_name);
+ fprintf(error, "%s%s: ", program.command_basename, process_name);
if (mode & ERR_WARN)
fprintf(error, "warning: ");
vfprintf(error, format, ap);
va_end(ap);
- fprintf(error, "\n");
+ fprintf(error, "%s", newline);
}
if (mode & ERR_HELP)
- fprintf(error, "%s: Try option '--help' for more information.\n",
- program_name);
+ fprintf(error, "%s: Try option '--help' for more information.%s",
+ program.command_basename, newline);
if (mode & ERR_EXIT)
- fprintf(error, "%s%s: aborting\n", program_name, process_name);
+ fprintf(error, "%s%s: aborting%s",
+ program.command_basename, process_name, newline);
if (error != stderr)
fclose(error);
if (mode & ERR_FROM_SERVER)
exit(1); /* child process: normal exit */
else
- CloseAllAndExit(1); /* main process: clean up stuff */
+ program.exit_function(1); /* main process: clean up stuff */
}
}
return ptr;
}
+void *checked_realloc(void *ptr, unsigned long size)
+{
+ ptr = realloc(ptr, size);
+
+ if (ptr == NULL)
+ Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
+
+ return ptr;
+}
+
+inline void swap_numbers(int *i1, int *i2)
+{
+ int help = *i1;
+
+ *i1 = *i2;
+ *i2 = help;
+}
+
+inline void swap_number_pairs(int *x1, int *y1, int *x2, int *y2)
+{
+ int help_x = *x1;
+ int help_y = *y1;
+
+ *x1 = *x2;
+ *x2 = help_x;
+
+ *y1 = *y2;
+ *y2 = help_y;
+}
+
short getFile16BitInteger(FILE *file, int byte_order)
{
if (byte_order == BYTE_ORDER_BIG_ENDIAN)
}
}
-void getFileChunk(FILE *file, char *chunk_buffer, int *chunk_length,
- int byte_order)
+boolean getFileChunk(FILE *file, char *chunk_name, int *chunk_size,
+ int byte_order)
{
- const int chunk_identifier_length = 4;
+ const int chunk_name_length = 4;
- /* read chunk identifier */
- fgets(chunk_buffer, chunk_identifier_length + 1, file);
+ /* read chunk name */
+ fgets(chunk_name, chunk_name_length + 1, file);
- /* read chunk length */
- *chunk_length = getFile32BitInteger(file, byte_order);
+ if (chunk_size != NULL)
+ {
+ /* read chunk size */
+ *chunk_size = getFile32BitInteger(file, byte_order);
+ }
+
+ return (feof(file) || ferror(file) ? FALSE : TRUE);
}
-void putFileChunk(FILE *file, char *chunk_name, int chunk_length,
+void putFileChunk(FILE *file, char *chunk_name, int chunk_size,
int byte_order)
{
- /* write chunk identifier */
+ /* write chunk name */
fputs(chunk_name, file);
- /* write chunk length */
- putFile32BitInteger(file, chunk_length, byte_order);
+ if (chunk_size >= 0)
+ {
+ /* write chunk size */
+ putFile32BitInteger(file, chunk_size, byte_order);
+ }
+}
+
+int getFileVersion(FILE *file)
+{
+ int version_major, version_minor, version_patch;
+
+ version_major = fgetc(file);
+ version_minor = fgetc(file);
+ version_patch = fgetc(file);
+ fgetc(file); /* not used */
+
+ return VERSION_IDENT(version_major, version_minor, version_patch);
+}
+
+void putFileVersion(FILE *file, int version)
+{
+ int version_major = VERSION_MAJOR(version);
+ int version_minor = VERSION_MINOR(version);
+ int version_patch = VERSION_PATCH(version);
+
+ fputc(version_major, file);
+ fputc(version_minor, file);
+ fputc(version_patch, file);
+ fputc(0, file); /* not used */
+}
+
+void ReadUnusedBytesFromFile(FILE *file, unsigned long bytes)
+{
+ while (bytes-- && !feof(file))
+ fgetc(file);
+}
+
+void WriteUnusedBytesToFile(FILE *file, unsigned long bytes)
+{
+ while (bytes--)
+ fputc(0, file);
}
+
+/* ------------------------------------------------------------------------- */
+/* functions to translate key identifiers between different format */
+/* ------------------------------------------------------------------------- */
+
#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)
{
sprintf(name_buffer, "%c", '0' + (char)(key - KSYM_0));
else if (key >= KSYM_KP_0 && key <= KSYM_KP_9)
sprintf(name_buffer, "keypad %c", '0' + (char)(key - KSYM_KP_0));
- else if (key >= KSYM_F1 && key <= KSYM_F24)
- sprintf(name_buffer, "function F%d", (int)(key - KSYM_F1 + 1));
+ else if (key >= KSYM_FKEY_FIRST && key <= KSYM_FKEY_LAST)
+ sprintf(name_buffer, "function F%d", (int)(key - KSYM_FKEY_FIRST + 1));
else if (key == KSYM_UNDEFINED)
strcpy(name_buffer, "(undefined)");
else
sprintf(name_buffer, "XK_%c", '0' + (char)(key - KSYM_0));
else if (key >= KSYM_KP_0 && key <= KSYM_KP_9)
sprintf(name_buffer, "XK_KP_%c", '0' + (char)(key - KSYM_KP_0));
- else if (key >= KSYM_F1 && key <= KSYM_F24)
- sprintf(name_buffer, "XK_F%d", (int)(key - KSYM_F1 + 1));
+ else if (key >= KSYM_FKEY_FIRST && key <= KSYM_FKEY_LAST)
+ sprintf(name_buffer, "XK_F%d", (int)(key - KSYM_FKEY_FIRST + 1));
else if (key == KSYM_UNDEFINED)
strcpy(name_buffer, "[undefined]");
else
*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;
((c2 >= '0' && c1 <= '9') || c2 == '\0'))
d = atoi(&name_ptr[4]);
- if (d >=1 && d <= 24)
+ if (d >= 1 && d <= KSYM_NUM_FKEYS)
key = KSYM_F1 + (Key)(d - 1);
}
else if (strncmp(name_ptr, "XK_", 3) == 0)
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;
return letter;
}
-/* ------------------------------------------------------------------------- */
-/* some functions to handle lists of level directories */
-/* ------------------------------------------------------------------------- */
-
-struct LevelDirInfo *newLevelDirInfo()
-{
- return checked_calloc(sizeof(struct LevelDirInfo));
-}
-void pushLevelDirInfo(struct LevelDirInfo **node_first,
- struct LevelDirInfo *node_new)
-{
- node_new->next = *node_first;
- *node_first = node_new;
-}
+/* ========================================================================= */
+/* functions for checking filenames */
+/* ========================================================================= */
-int numLevelDirInfo(struct LevelDirInfo *node)
+boolean FileIsGraphic(char *filename)
{
- int num = 0;
+ if (strlen(filename) > 4 &&
+ strcmp(&filename[strlen(filename) - 4], ".pcx") == 0)
+ return TRUE;
- while (node)
- {
- num++;
- node = node->next;
- }
-
- return num;
+ return FALSE;
}
-boolean validLevelSeries(struct LevelDirInfo *node)
+boolean FileIsSound(char *basename)
{
- return (node != NULL && !node->node_group && !node->parent_link);
-}
+ if (strlen(basename) > 4 &&
+ strcmp(&basename[strlen(basename) - 4], ".wav") == 0)
+ return TRUE;
-struct LevelDirInfo *getFirstValidLevelSeries(struct LevelDirInfo *node)
-{
- if (node == NULL)
- {
- if (leveldir_first) /* start with first level directory entry */
- return getFirstValidLevelSeries(leveldir_first);
- else
- return NULL;
- }
- else if (node->node_group) /* enter level group (step down into tree) */
- return getFirstValidLevelSeries(node->node_group);
- else if (node->parent_link) /* skip start entry of level group */
- {
- if (node->next) /* get first real level series entry */
- return getFirstValidLevelSeries(node->next);
- else /* leave empty level group and go on */
- return getFirstValidLevelSeries(node->node_parent->next);
- }
- else /* this seems to be a regular level series */
- return node;
-}
-
-struct LevelDirInfo *getLevelDirInfoFirstGroupEntry(struct LevelDirInfo *node)
-{
- if (node == NULL)
- return NULL;
-
- if (node->node_parent == NULL) /* top level group */
- return leveldir_first;
- else /* sub level group */
- return node->node_parent->node_group;
+ return FALSE;
}
-int numLevelDirInfoInGroup(struct LevelDirInfo *node)
+boolean FileIsMusic(char *basename)
{
- return numLevelDirInfo(getLevelDirInfoFirstGroupEntry(node));
-}
-
-int posLevelDirInfo(struct LevelDirInfo *node)
-{
- struct LevelDirInfo *node_cmp = getLevelDirInfoFirstGroupEntry(node);
- int pos = 0;
+ /* "music" can be a WAV (loop) file or (if compiled with SDL) a MOD file */
- while (node_cmp)
- {
- if (node_cmp == node)
- return pos;
+ if (FileIsSound(basename))
+ return TRUE;
- pos++;
- node_cmp = node_cmp->next;
- }
+#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 0;
+ return FALSE;
}
-struct LevelDirInfo *getLevelDirInfoFromPos(struct LevelDirInfo *node, int pos)
+boolean FileIsArtworkType(char *basename, int type)
{
- struct LevelDirInfo *node_default = node;
- int pos_cmp = 0;
+ if ((type == TREE_TYPE_GRAPHICS_DIR && FileIsGraphic(basename)) ||
+ (type == TREE_TYPE_SOUNDS_DIR && FileIsSound(basename)) ||
+ (type == TREE_TYPE_MUSIC_DIR && FileIsMusic(basename)))
+ return TRUE;
- while (node)
- {
- if (pos_cmp == pos)
- return node;
-
- pos_cmp++;
- node = node->next;
- }
-
- return node_default;
+ return FALSE;
}
-struct LevelDirInfo *getLevelDirInfoFromFilenameExt(struct LevelDirInfo *node,
- char *filename)
-{
- if (filename == NULL)
- return NULL;
- while (node)
- {
- if (node->node_group)
- {
- struct LevelDirInfo *node_group;
+/* ========================================================================= */
+/* functions only needed for non-Unix (non-command-line) systems */
+/* (MS-DOS only; SDL/Windows creates files "stdout.txt" and "stderr.txt") */
+/* ========================================================================= */
- node_group = getLevelDirInfoFromFilenameExt(node->node_group, filename);
-
- if (node_group)
- return node_group;
- }
- else if (!node->parent_link)
- {
- if (strcmp(filename, node->filename) == 0)
- return node;
- }
+#if defined(PLATFORM_MSDOS)
- node = node->next;
- }
+#define ERROR_FILENAME "stderr.txt"
- return NULL;
-}
-
-struct LevelDirInfo *getLevelDirInfoFromFilename(char *filename)
+void initErrorFile()
{
- return getLevelDirInfoFromFilenameExt(leveldir_first, filename);
+ unlink(ERROR_FILENAME);
}
-void dumpLevelDirInfo(struct LevelDirInfo *node, int depth)
+FILE *openErrorFile()
{
- int i;
-
- while (node)
- {
- for (i=0; i<depth * 3; i++)
- printf(" ");
-
- printf("filename == '%s'\n", node->filename);
-
- if (node->node_group != NULL)
- dumpLevelDirInfo(node->node_group, depth + 1);
-
- node = node->next;
- }
+ return fopen(ERROR_FILENAME, MODE_APPEND);
}
-void sortLevelDirInfo(struct LevelDirInfo **node_first,
- int (*compare_function)(const void *, const void *))
+void dumpErrorFile()
{
- int num_nodes = numLevelDirInfo(*node_first);
- struct LevelDirInfo **sort_array;
- struct LevelDirInfo *node = *node_first;
- int i = 0;
-
- if (num_nodes == 0)
- return;
-
- /* allocate array for sorting structure pointers */
- sort_array = checked_calloc(num_nodes * sizeof(struct LevelDirInfo *));
+ FILE *error_file = fopen(ERROR_FILENAME, MODE_READ);
- /* writing structure pointers to sorting array */
- while (i < num_nodes && node) /* double boundary check... */
+ if (error_file != NULL)
{
- sort_array[i] = node;
+ while (!feof(error_file))
+ fputc(fgetc(error_file), stderr);
- i++;
- node = node->next;
+ fclose(error_file);
}
-
- /* sorting the structure pointers in the sorting array */
- qsort(sort_array, num_nodes, sizeof(struct LevelDirInfo *),
- compare_function);
-
- /* update the linkage of list elements with the sorted node array */
- for (i=0; i<num_nodes - 1; i++)
- sort_array[i]->next = sort_array[i + 1];
- sort_array[num_nodes - 1]->next = NULL;
-
- /* update the linkage of the main list anchor pointer */
- *node_first = sort_array[0];
-
- free(sort_array);
-
- /* now recursively sort the level group structures */
- node = *node_first;
- while (node)
- {
- if (node->node_group != NULL)
- sortLevelDirInfo(&node->node_group, compare_function);
-
- node = node->next;
- }
-}
-
-inline void swap_numbers(int *i1, int *i2)
-{
- int help = *i1;
-
- *i1 = *i2;
- *i2 = help;
-}
-
-inline void swap_number_pairs(int *x1, int *y1, int *x2, int *y2)
-{
- int help_x = *x1;
- int help_y = *y1;
-
- *x1 = *x2;
- *x2 = help_x;
-
- *y1 = *y2;
- *y2 = help_y;
}
+#endif
-/* ------------------------------------------------------------------------- */
+/* ========================================================================= */
/* the following is only for debugging purpose and normally not used */
-/* ------------------------------------------------------------------------- */
+/* ========================================================================= */
#define DEBUG_NUM_TIMESTAMPS 3