X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;f=src%2Ffiles.c;h=a77b4463660c5821680defce31430e15ed4d09b7;hb=d7d97176a88ee47f893055480c76b9c05a253d05;hp=9a50079c3598bf5d9cea9dcbba0dd66bd7ecf271;hpb=4b0f1eb4220d2dbe4cffb288f745661b32c96a5b;p=rocksndiamonds.git diff --git a/src/files.c b/src/files.c index 9a50079c..a77b4463 100644 --- a/src/files.c +++ b/src/files.c @@ -1,27 +1,28 @@ /*********************************************************** * Rocks'n'Diamonds -- McDuffin Strikes Back! * *----------------------------------------------------------* -* ©1995 Artsoft Development * -* Holger Schemel * -* 33659 Bielefeld-Senne * -* Telefon: (0521) 493245 * -* eMail: aeglos@valinor.owl.de * -* aeglos@uni-paderborn.de * -* q99492@pbhrzx.uni-paderborn.de * +* (c) 1995-98 Artsoft Entertainment * +* Holger Schemel * +* Oststrasse 11a * +* 33604 Bielefeld * +* phone: ++49 +521 290471 * +* email: aeglos@valinor.owl.de * *----------------------------------------------------------* * files.h * ***********************************************************/ +#include + #include "files.h" #include "tools.h" #include "misc.h" #include "tape.h" #include "joystick.h" -BOOL CreateNewScoreFile() +boolean CreateNewScoreFile() { int i,j,k; - char filename[MAX_FILENAME]; + char filename[MAX_FILENAME_LEN]; char empty_alias[MAX_NAMELEN]; FILE *file; @@ -52,9 +53,9 @@ BOOL CreateNewScoreFile() return(TRUE); } -BOOL CreateNewNamesFile(int mode) +boolean CreateNewNamesFile(int mode) { - char filename[MAX_FILENAME]; + char filename[MAX_FILENAME_LEN]; FILE *file; if (mode==PLAYER_LEVEL) @@ -73,25 +74,25 @@ BOOL CreateNewNamesFile(int mode) return(TRUE); } -BOOL LoadLevelInfo() +boolean LoadLevelInfo() { int i; - char filename[MAX_FILENAME]; - char cookie[MAX_FILENAME]; + char filename[MAX_FILENAME_LEN]; + char cookie[MAX_FILENAME_LEN]; FILE *file; sprintf(filename,"%s/%s",level_directory,LEVDIR_FILENAME); if (!(file=fopen(filename,"r"))) { - fprintf(stderr,"%s: cannot load level info '%s'!\n",progname,filename); + Error(ERR_RETURN, "cannot read level info '%s'", filename); return(FALSE); } fscanf(file,"%s\n",cookie); if (strcmp(cookie,LEVELDIR_COOKIE)) /* ungültiges Format? */ { - fprintf(stderr,"%s: wrong format of level info file!\n",progname); + Error(ERR_RETURN, "wrong format of level info file"); fclose(file); return(FALSE); } @@ -112,7 +113,7 @@ BOOL LoadLevelInfo() if (!num_leveldirs) { - fprintf(stderr,"%s: empty level info '%s'!\n",progname,filename); + Error(ERR_RETURN, "empty level info '%s'", filename); return(FALSE); } @@ -122,19 +123,15 @@ BOOL LoadLevelInfo() void LoadLevel(int level_nr) { int i,x,y; - char filename[MAX_FILENAME]; - char cookie[MAX_FILENAME]; + char filename[MAX_FILENAME_LEN]; + char cookie[MAX_FILENAME_LEN]; FILE *file; sprintf(filename,"%s/%s/%d", level_directory,leveldir[leveldir_nr].filename,level_nr); - if (!(file=fopen(filename,"r"))) - { -/* - fprintf(stderr,"%s: cannot load level '%s'!\n",progname,filename); -*/ - } + if (!(file = fopen(filename,"r"))) + Error(ERR_RETURN, "cannot read level '%s' - creating new level", filename); else { fgets(cookie,LEVEL_COOKIE_LEN,file); @@ -142,8 +139,7 @@ void LoadLevel(int level_nr) if (strcmp(cookie,LEVEL_COOKIE)) /* ungültiges Format? */ { - fprintf(stderr,"%s: wrong format of level file '%s'!\n", - progname,filename); + Error(ERR_RETURN, "wrong format of level file '%s'", filename); fclose(file); file = NULL; } @@ -217,9 +213,10 @@ void LoadLevel(int level_nr) void LoadLevelTape(int level_nr) { int i; - char filename[MAX_FILENAME]; - char cookie[MAX_FILENAME]; + char filename[MAX_FILENAME_LEN]; + char cookie[MAX_FILENAME_LEN]; FILE *file; + boolean levelrec_10 = FALSE; #ifndef MSDOS sprintf(filename,"%s/%s/%d.tape", @@ -233,10 +230,11 @@ void LoadLevelTape(int level_nr) { fgets(cookie,LEVELREC_COOKIE_LEN,file); fgetc(file); - if (strcmp(cookie,LEVELREC_COOKIE)) /* ungültiges Format? */ + if (!strcmp(cookie,LEVELREC_COOKIE_10)) /* old 1.0 tape format */ + levelrec_10 = TRUE; + else if (strcmp(cookie,LEVELREC_COOKIE)) /* unknown tape format */ { - fprintf(stderr,"%s: wrong format of level recording file '%s'!\n", - progname,filename); + Error(ERR_RETURN, "wrong format of level recording file '%s'", filename); fclose(file); file = NULL; } @@ -262,10 +260,23 @@ void LoadLevelTape(int level_nr) for(i=0;i=MAX_TAPELEN) + int j; + + if (i >= MAX_TAPELEN) break; - tape.pos[i].joystickdata = fgetc(file); - tape.pos[i].delay = fgetc(file); + + for(j=0; j0) + { + tape.pos[i].action[j] = MV_NO_MOVING; + continue; + } + tape.pos[i].action[j] = fgetc(file); + } + + tape.pos[i].delay = fgetc(file); + if (feof(file)) break; } @@ -273,8 +284,7 @@ void LoadLevelTape(int level_nr) fclose(file); if (i != tape.length) - fprintf(stderr,"%s: level recording file '%s' corrupted!\n", - progname,filename); + Error(ERR_RETURN, "level recording file '%s' corrupted", filename); tape.length_seconds = GetTapeLength(); } @@ -282,25 +292,19 @@ void LoadLevelTape(int level_nr) void LoadScore(int level_nr) { int i,j; - char filename[MAX_FILENAME]; - char cookie[MAX_FILENAME]; + char filename[MAX_FILENAME_LEN]; + char cookie[MAX_FILENAME_LEN]; FILE *file; sprintf(filename,"%s/%s/%s", level_directory,leveldir[leveldir_nr].filename,SCORE_FILENAME); - if (!(file=fopen(filename,"r"))) + if (!(file = fopen(filename,"r"))) { if (!CreateNewScoreFile()) - { - fprintf(stderr,"%s: cannot create score file '%s'!\n", - progname,filename); - } - else if (!(file=fopen(filename,"r"))) - { - fprintf(stderr,"%s: cannot load score for level %d!\n", - progname,level_nr); - } + Error(ERR_RETURN, "cannot create score file '%s'", filename); + else if (!(file = fopen(filename,"r"))) + Error(ERR_RETURN, "cannot read score for level %d", level_nr); } if (file) @@ -308,7 +312,7 @@ void LoadScore(int level_nr) fgets(cookie,SCORE_COOKIE_LEN,file); if (strcmp(cookie,SCORE_COOKIE)) /* ungültiges Format? */ { - fprintf(stderr,"%s: wrong format of score file!\n",progname); + Error(ERR_RETURN, "wrong format of score file '%s'", filename); fclose(file); file = NULL; } @@ -340,8 +344,8 @@ void LoadScore(int level_nr) void LoadPlayerInfo(int mode) { int i; - char filename[MAX_FILENAME]; - char cookie[MAX_FILENAME]; + char filename[MAX_FILENAME_LEN]; + char cookie[MAX_FILENAME_LEN]; FILE *file; char *login_name = GetLoginName(); struct PlayerInfo default_player, new_player; @@ -364,18 +368,12 @@ void LoadPlayerInfo(int mode) new_player = default_player; - if (!(file=fopen(filename,"r"))) + if (!(file = fopen(filename,"r"))) { if (!CreateNewNamesFile(mode)) - { - fprintf(stderr,"%s: cannot create names file '%s'!\n", - progname,filename); - } - else if (!(file=fopen(filename,"r"))) - { - fprintf(stderr,"%s: cannot load player information '%s'!\n", - progname,filename); - } + Error(ERR_RETURN, "cannot create names file '%s'", filename); + else if (!(file = fopen(filename,"r"))) + Error(ERR_RETURN, "cannot read player information file '%s'", filename); } if (file) @@ -385,8 +383,7 @@ void LoadPlayerInfo(int mode) version_10_file = TRUE; else if (strcmp(cookie,NAMES_COOKIE)) /* ungültiges Format? */ { - fprintf(stderr,"%s: wrong format of names file '%s'!\n", - progname,filename); + Error(ERR_RETURN, "wrong format of names file '%s'", filename); fclose(file); file = NULL; } @@ -394,7 +391,7 @@ void LoadPlayerInfo(int mode) if (!file) { - player = default_player; + *local_player = default_player; level_nr = default_player.level_nr; return; } @@ -422,11 +419,9 @@ void LoadPlayerInfo(int mode) new_player = default_player; fclose(file); - if (!(file=fopen(filename,"a"))) - { - fprintf(stderr,"%s: cannot append new player to names file '%s'!\n", - progname,filename); - } + if (!(file = fopen(filename,"a"))) + Error(ERR_RETURN, "cannot append new player to names file '%s'", + filename); else { for(i=0;ileveldir_nr < num_leveldirs) + leveldir_nr = local_player->leveldir_nr; else leveldir_nr = 0; } else { - player.handicap = new_player.handicap; - player.level_nr = new_player.level_nr; + local_player->handicap = new_player.handicap; + local_player->level_nr = new_player.level_nr; } - level_nr = player.level_nr; - fclose(file); + level_nr = local_player->level_nr; + + if (file) + fclose(file); } void SaveLevel(int level_nr) { int i,x,y; - char filename[MAX_FILENAME]; + char filename[MAX_FILENAME_LEN]; FILE *file; sprintf(filename,"%s/%s/%d", @@ -480,7 +477,7 @@ void SaveLevel(int level_nr) if (!(file=fopen(filename,"w"))) { - fprintf(stderr,"%s: cannot save level file '%s'!\n",progname,filename); + Error(ERR_RETURN, "cannot save level file '%s'", filename); return; } @@ -522,9 +519,9 @@ void SaveLevel(int level_nr) void SaveLevelTape(int level_nr) { int i; - char filename[MAX_FILENAME]; + char filename[MAX_FILENAME_LEN]; FILE *file; - BOOL new_tape = TRUE; + boolean new_tape = TRUE; #ifndef MSDOS sprintf(filename,"%s/%s/%d.tape", @@ -540,14 +537,13 @@ void SaveLevelTape(int level_nr) new_tape = FALSE; fclose(file); - if (!AreYouSure("Replace old tape ?",AYS_ASK)) + if (!Request("Replace old tape ?",REQ_ASK)) return; } if (!(file=fopen(filename,"w"))) { - fprintf(stderr,"%s: cannot save level recording file '%s'!\n", - progname,filename); + Error(ERR_RETURN, "cannot save level recording file '%s'", filename); return; } @@ -571,7 +567,11 @@ void SaveLevelTape(int level_nr) for(i=0;ilogin_name, MAX_NAMELEN-1)) { fseek(file,-(2*MAX_NAMELEN+1+2+1+(version_10_file ? 0 : 11)),SEEK_CUR); break; } } - player.level_nr = level_nr; + local_player->level_nr = level_nr; for(i=0;ilogin_name[i],file); for(i=0;ialias_name[i],file); + fputc(local_player->handicap,file); + fputc(local_player->setup / 256,file); + fputc(local_player->setup % 256,file); + fputc(local_player->leveldir_nr,file); if (!version_10_file) { - fputc(player.level_nr,file); + fputc(local_player->level_nr,file); for(i=0;i<10;i++) /* currently unused bytes */ fputc(0,file); } @@ -711,7 +716,7 @@ void LoadJoystickData() fscanf(file,"%s",cookie); if (strcmp(cookie,JOYSTICK_COOKIE)) /* ungültiges Format? */ { - fprintf(stderr,"%s: wrong format of joystick file!\n",progname); + Error(ERR_RETURN, "wrong format of joystick file '%s'", JOYDAT_FILE); fclose(file); return; } @@ -745,7 +750,8 @@ void SaveJoystickData() if (!(file=fopen(JOYDAT_FILE,"w"))) { - fprintf(stderr,"%s: cannot save joystick calibration data!\n",progname); + Error(ERR_RETURN, "cannot save joystick calibration data to file '%s'", + JOYDAT_FILE); return; } @@ -766,3 +772,615 @@ void SaveJoystickData() #endif } + +/* ------------------------------------------------------------------------- */ +/* new setup functions */ +/* ------------------------------------------------------------------------- */ + +#define SETUP_TOKEN_SOUND 0 +#define SETUP_TOKEN_SOUND_LOOPS 1 +#define SETUP_TOKEN_SOUND_MUSIC 2 +#define SETUP_TOKEN_SOUND_SIMPLE 3 +#define SETUP_TOKEN_TOONS 4 +#define SETUP_TOKEN_DIRECT_DRAW 5 +#define SETUP_TOKEN_SCROLL_DELAY 6 +#define SETUP_TOKEN_SOFT_SCROLLING 7 +#define SETUP_TOKEN_FADING 8 +#define SETUP_TOKEN_AUTORECORD 9 +#define SETUP_TOKEN_QUICK_DOORS 10 +#define SETUP_TOKEN_USE_JOYSTICK 11 +#define SETUP_TOKEN_JOYSTICK_NR 12 +#define SETUP_TOKEN_JOY_SNAP 13 +#define SETUP_TOKEN_JOY_BOMB 14 +#define SETUP_TOKEN_KEY_LEFT 15 +#define SETUP_TOKEN_KEY_RIGHT 16 +#define SETUP_TOKEN_KEY_UP 17 +#define SETUP_TOKEN_KEY_DOWN 18 +#define SETUP_TOKEN_KEY_SNAP 19 +#define SETUP_TOKEN_KEY_BOMB 20 + +#define NUM_SETUP_TOKENS 21 + +#define SETUP_TOKEN_PLAYER_PREFIX "player_" + +static struct +{ + char *token, *value_true, *value_false; +} setup_info[] = +{ + { "sound", "on", "off" }, + { "repeating_sound_loops", "on", "off" }, + { "background_music", "on", "off" }, + { "simple_sound_effects", "on", "off" }, + { "toons", "on", "off" }, + { "double_buffering", "off", "on" }, + { "scroll_delay", "on", "off" }, + { "soft_scrolling", "on", "off" }, + { "screen_fading", "on", "off" }, + { "automatic_tape_recording", "on", "off" }, + { "quick_doors", "on", "off" }, + + /* for each player: */ + { ".use_joystick", "true", "false" }, + { ".joystick_device", "second", "first" }, + { ".joy.snap_field", "", "" }, + { ".joy.place_bomb", "", "" }, + { ".key.move_left", "", "" }, + { ".key.move_right", "", "" }, + { ".key.move_up", "", "" }, + { ".key.move_down", "", "" }, + { ".key.snap_field", "", "" }, + { ".key.place_bomb", "", "" } +}; + +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; itoken) + free(setup_file_info->token); + if (setup_file_info->value) + free(setup_file_info->value); + if (setup_file_info->next) + freeSetupFileInfo(setup_file_info->next); + free(setup_file_info); +} + +static struct SetupFileInfo *newSetupFileInfo(char *token, char *value) +{ + struct SetupFileInfo *new = checked_malloc(sizeof(struct SetupFileInfo)); + + 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; +} + +static char *lookupSetupFileValue(struct SetupFileInfo *setup_file_info, + char *token) +{ + if (!setup_file_info) + return NULL; + + if (strcmp(setup_file_info->token, token) == 0) + return setup_file_info->value; + else + return lookupSetupFileValue(setup_file_info->next, token); +} + +#ifdef DEBUG +static void printSetupFileInfo(struct SetupFileInfo *setup_file_info) +{ + if (!setup_file_info) + return; + + printf("token: '%s'\n", setup_file_info->token); + printf("value: '%s'\n", setup_file_info->value); + + printSetupFileInfo(setup_file_info->next); +} +#endif + +static void decodeSetupFileInfo(struct SetupFileInfo *setup_file_info) +{ + int i; + int token_nr = -1; + int player_nr = 0; + char *token; + char *token_value; + int token_integer_value; + boolean token_boolean_value; + int token_player_prefix_len; + + if (!setup_file_info) + return; + + token = setup_file_info->token; + token_value = setup_file_info->value; + token_integer_value = get_string_integer_value(token_value); + token_boolean_value = get_string_boolean_value(token_value); + + token_player_prefix_len = strlen(SETUP_TOKEN_PLAYER_PREFIX); + + if (strncmp(token, SETUP_TOKEN_PLAYER_PREFIX, token_player_prefix_len) == 0) + { + token += token_player_prefix_len; + + if (*token >= '0' && *token <= '9') + { + player_nr = ((int)(*token - '0') - 1 + MAX_PLAYERS) % MAX_PLAYERS; + token++; + } + } + + for (i=0; i 1) + token_integer_value = 1; + setup.input[player_nr].joystick_nr = token_integer_value - 1; + break; + case SETUP_TOKEN_JOY_SNAP: + setup.input[player_nr].joy.snap = getJoySymbolFromJoyName(token_value); + break; + case SETUP_TOKEN_JOY_BOMB : + setup.input[player_nr].joy.bomb = getJoySymbolFromJoyName(token_value); + break; + case SETUP_TOKEN_KEY_LEFT: + setup.input[player_nr].key.left = getKeySymFromX11KeyName(token_value); + break; + case SETUP_TOKEN_KEY_RIGHT: + setup.input[player_nr].key.right = getKeySymFromX11KeyName(token_value); + break; + case SETUP_TOKEN_KEY_UP: + setup.input[player_nr].key.up = getKeySymFromX11KeyName(token_value); + break; + case SETUP_TOKEN_KEY_DOWN: + setup.input[player_nr].key.down = getKeySymFromX11KeyName(token_value); + break; + case SETUP_TOKEN_KEY_SNAP: + setup.input[player_nr].key.snap = getKeySymFromX11KeyName(token_value); + break; + case SETUP_TOKEN_KEY_BOMB: + setup.input[player_nr].key.bomb = getKeySymFromX11KeyName(token_value); + break; + default: + break; + } + + decodeSetupFileInfo(setup_file_info->next); +} + +void LoadSetup() +{ + int line_len; + char filename[MAX_FILENAME_LEN]; + char line[MAX_LINE_LEN]; + char *token, *value, *line_ptr; + struct SetupFileInfo *setup_file_info, **next_entry = &setup_file_info; + FILE *file; + + + + printf("LoadSetup\n"); + + + + sprintf(filename, "%s/%s", SETUP_PATH, SETUP_FILENAME); + + if (!(file = fopen(filename, "r"))) + { + int i; + + Error(ERR_RETURN, "cannot open setup file '%s'", filename); + + /* use default values for setup */ + + setup.sound_on = TRUE; + setup.sound_loops_on = FALSE; + setup.sound_music_on = FALSE; + setup.sound_simple_on = FALSE; + setup.toons_on = TRUE; + setup.direct_draw_on = FALSE; + setup.scroll_delay_on = FALSE; + setup.soft_scrolling_on = TRUE; + setup.fading_on = FALSE; + setup.autorecord_on = FALSE; + setup.quick_doors = FALSE; + + for (i=0; i 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); + + + /* + printf("line: '%s'\n", 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; + + + /* + printf("token / value: '%s' / '%s'\n", token, value); + */ + + + if (*token && *value) + { + /* allocate new token/value pair */ + + *next_entry = newSetupFileInfo(token, value); + next_entry = &((*next_entry)->next); + } + } + + fclose(file); + +#if 0 + printf("Content of setup file info:\n"); + + printSetupFileInfo(setup_file_info); +#endif + + + + printf("decodeSetupFileInfo\n"); + + + + decodeSetupFileInfo(setup_file_info); + freeSetupFileInfo(setup_file_info); +} + +void SaveSetup() +{ + int i; + char filename[MAX_FILENAME_LEN]; + FILE *file; + + + + printf("SaveSetup\n"); + + + + sprintf(filename, "%s/%s", SETUP_PATH, SETUP_FILENAME); + + if (!(file = fopen(filename, "w"))) + { + Error(ERR_RETURN, "cannot write setup file '%s'", filename); + return; + } + + fprintf(file, "file_identifier: %s\n", + SETUP_COOKIE); + + fprintf(file, "\n"); + + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_SOUND, + setup.sound_on)); + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_SOUND_LOOPS, + setup.sound_loops_on)); + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_SOUND_MUSIC, + setup.sound_music_on)); + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_SOUND_SIMPLE, + setup.sound_simple_on)); + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_TOONS, + setup.toons_on)); + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_DIRECT_DRAW, + setup.direct_draw_on)); + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_SCROLL_DELAY, + setup.scroll_delay_on)); + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_SOFT_SCROLLING, + setup.soft_scrolling_on)); + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_FADING, + setup.fading_on)); + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_AUTORECORD, + setup.autorecord_on)); + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_QUICK_DOORS, + setup.quick_doors)); + + for (i=0; i