123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- /* =============================================================================
- * PROGRAM: ularn
- * FILENAME: savegame.c
- *
- * DESCRIPTION:
- * This module contains functions for saving and loading the game.
- *
- * =============================================================================
- * EXPORTED VARIABLES
- *
- * None
- *
- * =============================================================================
- * EXPORTED FUNCTIONS
- *
- * savegame : Function to save the game
- * restoregame : Function to load the game
- *
- * =============================================================================
- */
- #include <fcntl.h>
- #include <stdio.h>
- #include "dungeon.h"
- #include "header.h"
- #include "itm.h"
- #include "monster.h"
- #include "player.h"
- #include "savegame.h"
- #include "saveutils.h"
- #include "scores.h"
- #include "spell.h"
- #include "sphere.h"
- #include "store.h"
- #include "ularn_game.h"
- #include "ularn_win.h"
- /* =============================================================================
- * Local functions
- */
- /* =============================================================================
- * FUNCTION: greedy
- *
- * DESCRIPTION:
- * Subroutine to not allow greedy cheaters.
- * Displays a cheater message and quits the game.
- *
- * PARAMETERS:
- *
- * None
- *
- * RETURN VALUE:
- *
- * None.
- */
- static void greedy(void) {
- if (wizard)
- return;
- Print("\n\nI am so sorry but your character is a little TOO good! Since "
- "this\n");
- Print("cannot normally happen from an honest game, I must assume that you "
- "cheated.\n");
- Print(
- "Since you are GREEDY as well as a CHEATER, I cannot allow this game\n");
- Print("to continue.\n");
- nap(5000);
- cheat = 1;
- c[GOLD] = c[BANKACCOUNT] = 0;
- died(DIED_GREEDY_CHEATER, 0);
- return;
- }
- /* =============================================================================
- * FUNCTION: fsorry
- *
- * DESCRIPTION:
- * Subroutine to not allow altered save files and terminate the attempted
- * restart.
- * Sets the cheat flag to disallow scoring for this game.
- *
- * PARAMETERS:
- *
- * None.
- *
- * RETURN VALUE:
- *
- * None.
- */
- static void fsorry(void) {
- if (cheat)
- return;
- Print("\nSorry but your savefile has been altered.\n");
- Print("However, since I am a good sport, I will let you play.\n");
- Print("Be advised, though, that you won't be placed on the scoreboard.");
- cheat = 1;
- nap(4000);
- }
- /* =============================================================================
- * FUNCTION: fcheat
- *
- * DESCRIPTION:
- * Subroutine to not allow game if save file can't be deleted.
- * Displays a message and quits the game.
- *
- * PARAMETERS:
- *
- * None
- *
- * RETURN VALUE:
- *
- * None.
- */
- static void fcheat(void) {
- if (wizard)
- return;
- if (cheat)
- return;
- Print("\nSorry but your savefile can't be deleted. This can only mean\n");
- Print("that you tried to CHEAT by protecting the directory the savefile\n");
- Print("is in. Since this is unfair to the rest of the VLarn community, I\n");
- Print("cannot let you play this game.\n");
- nap(5000);
- c[GOLD] = c[BANKACCOUNT] = 0;
- died(DIED_PROTECTED_SAVE_FILE, 0);
- return;
- }
- /* =============================================================================
- * Exported functions
- */
- /* =============================================================================
- * FUNCTION: savegame
- */
- int savegame(char *fname) {
- FILE *fp;
- nosignal = 1;
- /* Save the current level to storage */
- savelevel();
- /* make sure the interest on bank deposits is up to date */
- ointerest();
- /*
- * try and create the save file
- */
- fp = fopen(fname, "wb");
- if (fp == NULL) {
- Printf("Can't open file <%s> to save game\n", fname);
- nosignal = 0;
- return -1;
- }
- FileSum = 0;
- write_player(fp);
- write_levels(fp);
- write_store(fp);
- write_monster_data(fp);
- write_spheres(fp);
- /* file sum */
- bwrite(fp, (char *)&FileSum, sizeof(FileSum));
- fclose(fp);
- nosignal = 0;
- return 0;
- }
- /* =============================================================================
- * FUNCTION: restoregame
- */
- void restoregame(char *fname) {
- int i;
- unsigned int thesum;
- unsigned int asum;
- int TotalAttr;
- FILE *fp;
- fp = fopen(fname, "rb");
- if (fp == NULL) {
- Printf("Can't open file <%s> to restore game\n", fname);
- nap(4000);
- c[GOLD] = c[BANKACCOUNT] = 0;
- died(DIED_MISSING_SAVE_FILE, 0);
- return;
- }
- Printf(" Reading data...");
- init_cells();
- FileSum = 0;
- read_player(fp);
- read_levels(fp);
- read_store(fp);
- read_monster_data(fp);
- read_spheres(fp);
- /* sum of everything so far */
- thesum = FileSum;
- bread(fp, (char *)&asum, sizeof(asum));
- if (asum != thesum)
- fsorry();
- fclose(fp);
- lastpx = 0;
- lastpy = 0;
- if (strcmp(fname, ckpfile) == 0) {
- fp = fopen(fname, "ab+");
- if (fp == NULL)
- /*
- * Hmmm. We should be able to write to this file, something fishy
- * is going on.
- */
- fcheat();
- else
- fclose(fp);
- } else if (unlink(fname) == -1)
- /* can't unlink save file */
- fcheat();
- /* for the greedy cheater checker */
- TotalAttr = 0;
- for (i = ABILITY_FIRST; i < ABILITY_LAST; i++) {
- TotalAttr += c[i];
- if (c[i] > 300)
- greedy();
- }
- if (TotalAttr > 600)
- greedy();
- if ((c[HPMAX] > 999) || (c[SPELLMAX] > 125))
- greedy();
- /* XP has been boosted in the save file, so fix character level */
- if ((c[LEVEL] == 25) && (c[EXPERIENCE] > skill[24])) {
- long tmp_xp = c[EXPERIENCE] - skill[24]; /* amount to go up */
- c[EXPERIENCE] = skill[24];
- raiseexperience((long)tmp_xp);
- }
- /* Get the current dungeon level from storage */
- getlevel();
- }
|