/***********************************************************
-* 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-2001 Artsoft Entertainment *
+* Holger Schemel *
+* Detmolder Strasse 189 *
+* 33604 Bielefeld *
+* Germany *
+* e-mail: info@artsoft.org *
*----------------------------------------------------------*
-* misc.c *
+* misc.c *
***********************************************************/
#include <time.h>
#include <sys/time.h>
#include <sys/types.h>
+#include <sys/stat.h>
#include <stdarg.h>
#include <ctype.h>
+#include <string.h>
+#include <unistd.h>
-#include "libgame.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 "random.h"
-#if 0
-#include "joystick_TMP.h"
-#endif
#if defined(PLATFORM_MSDOS)
volatile unsigned long counter = 0;
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.verbose = FALSE;
options.debug = FALSE;
- /* initialize some more global variables */
- global.frames_per_second = 0;
- global.fps_slowdown = FALSE;
- global.fps_slowdown_factor = 1;
-
while (*options_left)
{
char option_str[MAX_OPTION_LEN];
" -l, --level directory alternative level directory\n"
" -s, --serveronly only start network server\n"
" -n, --network network multiplayer game\n"
- " -v, --verbose verbose mode\n",
- program_name);
+ " -v, --verbose verbose mode\n"
+ " --debug display debugging information\n",
+ program.command_basename);
exit(0);
}
else if (strncmp(option, "-display", option_len) == 0)
{
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)
+ 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;
+}
+
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 name */
+ fgets(chunk_name, chunk_name_length + 1, file);
- /* read chunk identifier */
- fgets(chunk_buffer, chunk_identifier_length + 1, file);
+ if (chunk_size != NULL)
+ {
+ /* read chunk size */
+ *chunk_size = getFile32BitInteger(file, byte_order);
+ }
- /* read chunk length */
- *chunk_length = 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);
+ }
+}
+
+void ReadUnusedBytesFromFile(FILE *file, unsigned long bytes)
+{
+ while (bytes--)
+ fgetc(file);
+}
+
+void WriteUnusedBytesToFile(FILE *file, unsigned long bytes)
+{
+ while (bytes--)
+ fputc(0, file);
}
#define TRANSLATE_KEYSYM_TO_KEYNAME 0
}
+/* ========================================================================= */
+/* some stuff from "files.c" */
+/* ========================================================================= */
+
+#if defined(PLATFORM_WIN32)
+#ifndef S_IRGRP
+#define S_IRGRP S_IRUSR
+#endif
+#ifndef S_IROTH
+#define S_IROTH S_IRUSR
+#endif
+#ifndef S_IWGRP
+#define S_IWGRP S_IWUSR
+#endif
+#ifndef S_IWOTH
+#define S_IWOTH S_IWUSR
+#endif
+#ifndef S_IXGRP
+#define S_IXGRP S_IXUSR
+#endif
+#ifndef S_IXOTH
+#define S_IXOTH S_IXUSR
+#endif
+#endif /* PLATFORM_WIN32 */
+
+/* file permissions for newly written files */
+#define MODE_R_ALL (S_IRUSR | S_IRGRP | S_IROTH)
+#define MODE_W_ALL (S_IWUSR | S_IWGRP | S_IWOTH)
+#define MODE_X_ALL (S_IXUSR | S_IXGRP | S_IXOTH)
+
+#define MODE_W_PRIVATE (S_IWUSR)
+#define MODE_W_PUBLIC (S_IWUSR | S_IWGRP)
+#define MODE_W_PUBLIC_DIR (S_IWUSR | S_IWGRP | S_ISGID)
+
+#define DIR_PERMS_PRIVATE (MODE_R_ALL | MODE_X_ALL | MODE_W_PRIVATE)
+#define DIR_PERMS_PUBLIC (MODE_R_ALL | MODE_X_ALL | MODE_W_PUBLIC_DIR)
+
+#define FILE_PERMS_PRIVATE (MODE_R_ALL | MODE_W_PRIVATE)
+#define FILE_PERMS_PUBLIC (MODE_R_ALL | MODE_W_PUBLIC)
+
+char *getUserDataDir(void)
+{
+ static char *userdata_dir = NULL;
+
+ if (!userdata_dir)
+ {
+ char *home_dir = getHomeDir();
+ char *data_dir = program.userdata_directory;
+
+ userdata_dir = getPath2(home_dir, data_dir);
+ }
+
+ return userdata_dir;
+}
+
+char *getSetupDir()
+{
+ return getUserDataDir();
+}
+
+void createDirectory(char *dir, char *text, int permission_class)
+{
+#if defined(PLATFORM_UNIX)
+ /* leave "other" permissions in umask untouched, but ensure group parts
+ of USERDATA_DIR_MODE are not masked */
+ mode_t dir_mode = (permission_class == PERMS_PRIVATE ?
+ DIR_PERMS_PRIVATE : DIR_PERMS_PUBLIC);
+ mode_t normal_umask = umask(0);
+ mode_t group_umask = ~(dir_mode & S_IRWXG);
+ umask(normal_umask & group_umask);
+#endif
+
+ if (access(dir, F_OK) != 0)
+#if defined(PLATFORM_WIN32)
+ if (mkdir(dir) != 0)
+#else
+ if (mkdir(dir, dir_mode) != 0)
+#endif
+ Error(ERR_WARN, "cannot create %s directory '%s'", text, dir);
+
+#if defined(PLATFORM_UNIX)
+ umask(normal_umask); /* reset normal umask */
+#endif
+}
+
+void InitUserDataDirectory()
+{
+ createDirectory(getUserDataDir(), "user data", PERMS_PRIVATE);
+}
+
+void SetFilePermissions(char *filename, int permission_class)
+{
+ chmod(filename, (permission_class == PERMS_PRIVATE ?
+ FILE_PERMS_PRIVATE : FILE_PERMS_PUBLIC));
+}
+
+int getFileVersionFromCookieString(const char *cookie)
+{
+ const char *ptr_cookie1, *ptr_cookie2;
+ const char *pattern1 = "_FILE_VERSION_";
+ const char *pattern2 = "?.?";
+ const int len_cookie = strlen(cookie);
+ const int len_pattern1 = strlen(pattern1);
+ const int len_pattern2 = strlen(pattern2);
+ const int len_pattern = len_pattern1 + len_pattern2;
+ int version_major, version_minor;
+
+ if (len_cookie <= len_pattern)
+ return -1;
+
+ ptr_cookie1 = &cookie[len_cookie - len_pattern];
+ ptr_cookie2 = &cookie[len_cookie - len_pattern2];
+
+ if (strncmp(ptr_cookie1, pattern1, len_pattern1) != 0)
+ return -1;
+
+ if (ptr_cookie2[0] < '0' || ptr_cookie2[0] > '9' ||
+ ptr_cookie2[1] != '.' ||
+ ptr_cookie2[2] < '0' || ptr_cookie2[2] > '9')
+ return -1;
+
+ version_major = ptr_cookie2[0] - '0';
+ version_minor = ptr_cookie2[2] - '0';
+
+ return VERSION_IDENT(version_major, version_minor, 0);
+}
+
+boolean checkCookieString(const char *cookie, const char *template)
+{
+ const char *pattern = "_FILE_VERSION_?.?";
+ const int len_cookie = strlen(cookie);
+ const int len_template = strlen(template);
+ const int len_pattern = strlen(pattern);
+
+ if (len_cookie != len_template)
+ return FALSE;
+
+ if (strncmp(cookie, template, len_cookie - len_pattern) != 0)
+ return FALSE;
+
+ return TRUE;
+}
+
/* ------------------------------------------------------------------------- */
-/* the following is only for debugging purpose and normally not used */
+/* setup file stuff */
/* ------------------------------------------------------------------------- */
+static char *string_tolower(char *s)
+{
+ static char s_lower[100];
+ int i;
+
+ if (strlen(s) >= 100)
+ return s;
+
+ strcpy(s_lower, s);
+
+ for (i=0; i<strlen(s_lower); i++)
+ s_lower[i] = tolower(s_lower[i]);
+
+ return s_lower;
+}
+
+int get_string_integer_value(char *s)
+{
+ static char *number_text[][3] =
+ {
+ { "0", "zero", "null", },
+ { "1", "one", "first" },
+ { "2", "two", "second" },
+ { "3", "three", "third" },
+ { "4", "four", "fourth" },
+ { "5", "five", "fifth" },
+ { "6", "six", "sixth" },
+ { "7", "seven", "seventh" },
+ { "8", "eight", "eighth" },
+ { "9", "nine", "ninth" },
+ { "10", "ten", "tenth" },
+ { "11", "eleven", "eleventh" },
+ { "12", "twelve", "twelfth" },
+ };
+
+ int i, j;
+
+ for (i=0; i<13; i++)
+ for (j=0; j<3; j++)
+ if (strcmp(string_tolower(s), number_text[i][j]) == 0)
+ return i;
+
+ return atoi(s);
+}
+
+boolean get_string_boolean_value(char *s)
+{
+ if (strcmp(string_tolower(s), "true") == 0 ||
+ strcmp(string_tolower(s), "yes") == 0 ||
+ strcmp(string_tolower(s), "on") == 0 ||
+ get_string_integer_value(s) == 1)
+ return TRUE;
+ else
+ return FALSE;
+}
+
+char *getFormattedSetupEntry(char *token, char *value)
+{
+ int i;
+ static char entry[MAX_LINE_LEN];
+
+ sprintf(entry, "%s:", token);
+ for (i=strlen(entry); i<TOKEN_VALUE_POSITION; i++)
+ entry[i] = ' ';
+ entry[i] = '\0';
+
+ strcat(entry, value);
+
+ return entry;
+}
+
+void freeSetupFileList(struct SetupFileList *setup_file_list)
+{
+ if (!setup_file_list)
+ return;
+
+ if (setup_file_list->token)
+ free(setup_file_list->token);
+ if (setup_file_list->value)
+ free(setup_file_list->value);
+ if (setup_file_list->next)
+ freeSetupFileList(setup_file_list->next);
+ free(setup_file_list);
+}
+
+static struct SetupFileList *newSetupFileList(char *token, char *value)
+{
+ struct SetupFileList *new = checked_malloc(sizeof(struct SetupFileList));
+
+ new->token = checked_malloc(strlen(token) + 1);
+ strcpy(new->token, token);
+
+ new->value = checked_malloc(strlen(value) + 1);
+ strcpy(new->value, value);
+
+ new->next = NULL;
+
+ return new;
+}
+
+char *getTokenValue(struct SetupFileList *setup_file_list, char *token)
+{
+ if (!setup_file_list)
+ return NULL;
+
+ if (strcmp(setup_file_list->token, token) == 0)
+ return setup_file_list->value;
+ else
+ return getTokenValue(setup_file_list->next, token);
+}
+
+static void setTokenValue(struct SetupFileList *setup_file_list,
+ char *token, char *value)
+{
+ if (!setup_file_list)
+ return;
+
+ if (strcmp(setup_file_list->token, token) == 0)
+ {
+ free(setup_file_list->value);
+ setup_file_list->value = checked_malloc(strlen(value) + 1);
+ strcpy(setup_file_list->value, value);
+ }
+ else if (setup_file_list->next == NULL)
+ setup_file_list->next = newSetupFileList(token, value);
+ else
+ setTokenValue(setup_file_list->next, token, value);
+}
+
+#ifdef DEBUG
+static void printSetupFileList(struct SetupFileList *setup_file_list)
+{
+ if (!setup_file_list)
+ return;
+
+ printf("token: '%s'\n", setup_file_list->token);
+ printf("value: '%s'\n", setup_file_list->value);
+
+ printSetupFileList(setup_file_list->next);
+}
+#endif
+
+struct SetupFileList *loadSetupFileList(char *filename)
+{
+ int line_len;
+ char line[MAX_LINE_LEN];
+ char *token, *value, *line_ptr;
+ struct SetupFileList *setup_file_list = newSetupFileList("", "");
+ struct SetupFileList *first_valid_list_entry;
+
+ FILE *file;
+
+ if (!(file = fopen(filename, MODE_READ)))
+ {
+ Error(ERR_WARN, "cannot open configuration file '%s'", filename);
+ return NULL;
+ }
+
+ while(!feof(file))
+ {
+ /* read next line of input file */
+ if (!fgets(line, MAX_LINE_LEN, file))
+ break;
+
+ /* cut trailing comment or whitespace from input line */
+ for (line_ptr = line; *line_ptr; line_ptr++)
+ {
+ if (*line_ptr == '#' || *line_ptr == '\n' || *line_ptr == '\r')
+ {
+ *line_ptr = '\0';
+ break;
+ }
+ }
+
+ /* cut trailing whitespaces from input line */
+ for (line_ptr = &line[strlen(line)]; line_ptr > line; line_ptr--)
+ if ((*line_ptr == ' ' || *line_ptr == '\t') && line_ptr[1] == '\0')
+ *line_ptr = '\0';
+
+ /* ignore empty lines */
+ if (*line == '\0')
+ continue;
+
+ line_len = strlen(line);
+
+ /* cut leading whitespaces from token */
+ for (token = line; *token; token++)
+ if (*token != ' ' && *token != '\t')
+ break;
+
+ /* find end of token */
+ for (line_ptr = token; *line_ptr; line_ptr++)
+ {
+ if (*line_ptr == ' ' || *line_ptr == '\t' || *line_ptr == ':')
+ {
+ *line_ptr = '\0';
+ break;
+ }
+ }
+
+ if (line_ptr < line + line_len)
+ value = line_ptr + 1;
+ else
+ value = "\0";
+
+ /* cut leading whitespaces from value */
+ for (; *value; value++)
+ if (*value != ' ' && *value != '\t')
+ break;
+
+ if (*token && *value)
+ setTokenValue(setup_file_list, token, value);
+ }
+
+ fclose(file);
+
+ first_valid_list_entry = setup_file_list->next;
+
+ /* free empty list header */
+ setup_file_list->next = NULL;
+ freeSetupFileList(setup_file_list);
+
+ if (first_valid_list_entry == NULL)
+ Error(ERR_WARN, "configuration file '%s' is empty", filename);
+
+ return first_valid_list_entry;
+}
+
+void checkSetupFileListIdentifier(struct SetupFileList *setup_file_list,
+ char *identifier)
+{
+ if (!setup_file_list)
+ return;
+
+ if (strcmp(setup_file_list->token, TOKEN_STR_FILE_IDENTIFIER) == 0)
+ {
+ if (strcmp(setup_file_list->value, identifier) != 0)
+ {
+ Error(ERR_WARN, "configuration file has wrong version");
+ return;
+ }
+ else
+ return;
+ }
+
+ if (setup_file_list->next)
+ checkSetupFileListIdentifier(setup_file_list->next, identifier);
+ else
+ {
+ Error(ERR_WARN, "configuration file has no version information");
+ return;
+ }
+}
+
+
+/* ========================================================================= */
+/* functions only needed for non-Unix (non-command-line) systems */
+/* ========================================================================= */
+
+#if !defined(PLATFORM_UNIX)
+
+#define ERROR_FILENAME "error.out"
+
+void initErrorFile()
+{
+ char *filename;
+
+ InitUserDataDirectory();
+
+ filename = getPath2(getUserDataDir(), ERROR_FILENAME);
+ unlink(filename);
+ free(filename);
+}
+
+FILE *openErrorFile()
+{
+ char *filename;
+ FILE *error_file;
+
+ filename = getPath2(getUserDataDir(), ERROR_FILENAME);
+ error_file = fopen(filename, MODE_APPEND);
+ free(filename);
+
+ return error_file;
+}
+
+void dumpErrorFile()
+{
+ char *filename;
+ FILE *error_file;
+
+ filename = getPath2(getUserDataDir(), ERROR_FILENAME);
+ error_file = fopen(filename, MODE_READ);
+ free(filename);
+
+ if (error_file != NULL)
+ {
+ while (!feof(error_file))
+ fputc(fgetc(error_file), stderr);
+
+ fclose(error_file);
+ }
+}
+#endif
+
+
+/* ========================================================================= */
+/* the following is only for debugging purpose and normally not used */
+/* ========================================================================= */
+
#define DEBUG_NUM_TIMESTAMPS 3
void debug_print_timestamp(int counter_nr, char *message)