/***************************** save_game.c ******************************/ #include #include "sdi.h" /* * Copyright 1987 by Mark Weiser. * Permission to reproduce and use in any manner whatsoever on Suns is granted * so long as this copyright and other identifying marks of authorship * in the code and the game remain intact and visible. Use of this code * in other products is reserved to me--I'm working on Mac and IBM versions. */ /* * Code to save and restore games. */ /* If the save format is changed, change the version number. */ #define SAVE_VERSION 6 extern int time_to_play, gamemaster, cursor_type; extern char save_file_name[]; extern Panel_item cursor_item, cycle_time_item; extern char *strcpy(); void save_game() { char tmpbuf[128]; int control_x, control_y; FILE *Savefile; if ((Savefile = fopen(SAVE_FILE_NAME, "r")) != NULL) { sprintf(tmpbuf, "Save file '%s' already exists. Overwrite?", SAVE_FILE_NAME); if (! easy_warn(tmpbuf)) return; fclose(Savefile); } if ((Savefile = fopen(SAVE_FILE_NAME, "w")) == NULL) { sprintf(tmpbuf, "Cannot open save file '%s'.", SAVE_FILE_NAME); easy_pop(tmpbuf); } fprintf(Savefile, "version = %d\n", SAVE_VERSION); fprintf(Savefile, "level = %s\n",(char *)xv_get(level_item, PANEL_VALUE)); fprintf(Savefile, "skill = %d\n", xv_get(skill_item, PANEL_VALUE)); fprintf(Savefile, "interceptor_val = %d\n", xv_get(interceptor_item, PANEL_VALUE)); fprintf(Savefile, "laser_val = %d\n", xv_get(laser_item, PANEL_VALUE)); fprintf(Savefile, "rock_val = %d\n", xv_get(rock_item, PANEL_VALUE)); fprintf(Savefile, "game_master = %d\n", gamemaster); fprintf(Savefile, "score = %s\n", xv_get(score_item, PANEL_VALUE)); fprintf(Savefile, "name = '%s'\n", USER_NAME); fprintf(Savefile, "playing field size = %d, %d\n", xv_get(cityframe, XV_WIDTH), xv_get(cityframe, XV_HEIGHT)); fprintf(Savefile, "control panel position = %d, %d\n", (control_x = (int)xv_get(controlframe, WIN_X)), (control_y = (int)xv_get(controlframe, WIN_Y))); /* * Kludge around a "feature" of Sunview in which negative * positions are ignored. */ xv_set(controlframe, WIN_X, 0, WIN_Y, 0, 0); fprintf(Savefile, "city field position = %d, %d\n", xv_get(cityframe, WIN_X), xv_get(cityframe, WIN_Y)); fprintf(Savefile, "launch field position = %d, %d\n", xv_get(launchframe, WIN_X), xv_get(launchframe, WIN_Y)); xv_set(controlframe, WIN_X, control_x, WIN_Y, control_y, 0); fprintf(Savefile, "blast_delay = %d\n", blast_delay); fprintf(Savefile, "cursor_type = %d\n", cursor_type); fclose(Savefile); } void restore_game() { int version; int tmpint, w, h, x, y; int control_x, control_y; char tmpbuf[256]; FILE *Savefile; if ((Savefile = fopen(SAVE_FILE_NAME, "r")) == NULL) { sprintf(tmpbuf, "Can't open restore file '%s'.", SAVE_FILE_NAME); easy_pop(tmpbuf); return; } fscanf(Savefile, "version = %d\n", &version); if (version != SAVE_VERSION) { easy_pop("The save file has the wrong version."); return; } fscanf(Savefile, "level = %s\n", tmpbuf); xv_set(level_item, PANEL_VALUE, tmpbuf, 0); fscanf(Savefile, "skill = %d\n", &tmpint); xv_set(skill_item, PANEL_VALUE, tmpint, 0); fscanf(Savefile, "interceptor_val = %d\n", &tmpint); xv_set(interceptor_item, PANEL_VALUE, tmpint, PANEL_MAX_VALUE, tmpint, 0); fscanf(Savefile, "laser_val = %d\n", &tmpint); xv_set(laser_item, PANEL_VALUE, tmpint, PANEL_MAX_VALUE, tmpint, 0); fscanf(Savefile, "rock_val = %d\n", &tmpint); xv_set(rock_item, PANEL_VALUE, tmpint, PANEL_MAX_VALUE, tmpint, 0); fscanf(Savefile, "game_master = %d\n", &gamemaster); fscanf(Savefile, "score = %s\n", tmpbuf); xv_set(score_item, PANEL_VALUE, tmpbuf); fscanf(Savefile, "name = '%[^']'\n", tmpbuf); if (user_name[0] == '\0') xv_set(user_name_item, PANEL_VALUE, tmpbuf); strcpy(user_name, tmpbuf); fscanf(Savefile, "playing field size = %d, %d\n", &w, &h); /* Only set one, they track each other. */ xv_set(cityframe, XV_WIDTH, w, XV_HEIGHT, h, 0); fscanf(Savefile, "control panel position = %d, %d\n",&control_x,&control_y); /* * Kludge around a "feature" of Sunview in which negative * positions are ignored. */ xv_set(controlframe, WIN_X, 0, WIN_Y, 0, 0); fscanf(Savefile, "city field position = %d, %d\n",&x,&y); xv_set(cityframe, WIN_X, x, WIN_Y, y, 0); fscanf(Savefile, "launch field position = %d, %d\n",&x,&y); xv_set(launchframe, WIN_X, x, WIN_Y, y, 0); xv_set(controlframe, WIN_X, control_x, WIN_Y, control_y, 0); fscanf(Savefile, "blast_delay = %d\n", &blast_delay); xv_set(cycle_time_item, PANEL_VALUE, blast_delay); fscanf(Savefile, "cursor_type = %d\n", &tmpint); cursor_type = tmpint; fclose(Savefile); } FILE * getsavefile(s) char *s; { char *filename; FILE *stream; filename = SAVE_FILE_NAME; if (((stream = fopen(filename, s)) == NULL)) { easy_pop("Can't open the save file."); } return stream; }