LCOV - code coverage report
Current view: top level - open-adventure - saveresume.c (source / functions) Hit Total Coverage
Test: advent.info Lines: 88 99 88.9 %
Date: 2018-03-12 03:52:32 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Saving and resuming.
       3             :  *
       4             :  * (ESR) This replaces  a bunch of particularly nasty FORTRAN-derived code;
       5             :  * see the history.adoc file in the source distribution for discussion.
       6             :  *
       7             :  * Copyright (c) 1977, 2005 by Will Crowther and Don Woods
       8             :  * Copyright (c) 2017 by Eric S. Raymond
       9             :  * SPDX-License-Identifier: BSD-2-clause
      10             :  */
      11             : 
      12             : #include <stdlib.h>
      13             : #include <string.h>
      14             : #include <editline/readline.h>
      15             : #include <time.h>
      16             : #include <inttypes.h>
      17             : 
      18             : #include "advent.h"
      19             : #include "dungeon.h"
      20             : 
      21             : #define VRSION  28      /* bump on save format change */
      22             : 
      23             : /*
      24             :  * If you change the first three members, the resume function may not properly
      25             :  * reject saves from older versions.  Yes, this glues us to a hardware-
      26             :  * dependent length of long.  Later members can change, but bump the version
      27             :  * when you do that.
      28             :  */
      29             : struct save_t {
      30             :     int64_t savetime;
      31             :     int32_t mode;               /* not used, must be present for version detection */
      32             :     int32_t version;
      33             :     struct game_t game;
      34             : };
      35             : struct save_t save;
      36             : 
      37             : #define IGNORE(r) do{if (r){}}while(0)
      38             : 
      39           8 : int savefile(FILE *fp, int32_t version)
      40             : /* Save game to file. No input or output from user. */
      41             : {
      42           8 :     save.savetime = time(NULL);
      43           8 :     save.mode = -1;
      44           8 :     save.version = (version == 0) ? VRSION : version;
      45             : 
      46           8 :     save.game = game;
      47           8 :     IGNORE(fwrite(&save, sizeof(struct save_t), 1, fp));
      48           8 :     return (0);
      49             : }
      50             : 
      51             : /* Suspend and resume */
      52           4 : int suspend(void)
      53             : {
      54             :     /*  Suspend.  Offer to save things in a file, but charging
      55             :      *  some points (so can't win by using saved games to retry
      56             :      *  battles or to start over after learning zzword).
      57             :      *  If ADVENT_NOSAVE is defined, do nothing instead. */
      58             : 
      59             : #ifdef ADVENT_NOSAVE
      60             :     return GO_UNKNOWN;
      61             : #endif
      62           4 :     FILE *fp = NULL;
      63             : 
      64           4 :     rspeak(SUSPEND_WARNING);
      65           4 :     if (!yes(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN]))
      66           1 :         return GO_CLEAROBJ;
      67           3 :     game.saved = game.saved + 5;
      68             : 
      69           9 :     while (fp == NULL) {
      70           4 :         char* name = readline("\nFile name: ");
      71           4 :         if (name == NULL)
      72           1 :             return GO_TOP;
      73           3 :         fp = fopen(name, WRITE_MODE);
      74           3 :         if (fp == NULL)
      75           1 :             printf("Can't open file %s, try again.\n", name);
      76           3 :         free(name);
      77             :     }
      78             : 
      79           2 :     savefile(fp, VRSION);
      80           2 :     fclose(fp);
      81           2 :     rspeak(RESUME_HELP);
      82           2 :     exit(EXIT_SUCCESS);
      83             : }
      84             : 
      85           7 : int resume(void)
      86             : {
      87             :     /*  Resume.  Read a suspended game back from a file.
      88             :      *  If ADVENT_NOSAVE is defined, do nothing instead. */
      89             : 
      90             : #ifdef ADVENT_NOSAVE
      91             :     return GO_UNKNOWN;
      92             : #endif
      93           7 :     FILE *fp = NULL;
      94             : 
      95          12 :     if (game.loc != 1 ||
      96           5 :         game.abbrev[1] != 1) {
      97           3 :         rspeak(RESUME_ABANDON);
      98           3 :         if (!yes(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN]))
      99           1 :             return GO_CLEAROBJ;
     100             :     }
     101             : 
     102          20 :     while (fp == NULL) {
     103           9 :         char* name = readline("\nFile name: ");
     104           9 :         if (name == NULL)
     105           1 :             return GO_TOP;
     106           8 :         fp = fopen(name, READ_MODE);
     107           8 :         if (fp == NULL)
     108           3 :             printf("Can't open file %s, try again.\n", name);
     109           8 :         free(name);
     110             :     }
     111             : 
     112           5 :     return restore(fp);
     113             : }
     114             : 
     115             : bool is_valid(struct game_t*);
     116             : 
     117           7 : int restore(FILE* fp)
     118             : {
     119             :     /*  Read and restore game state from file, assuming
     120             :      *  sane initial state.
     121             :      *  If ADVENT_NOSAVE is defined, do nothing instead. */
     122             : #ifdef ADVENT_NOSAVE
     123             :     return GO_UNKNOWN;
     124             : #endif
     125             : 
     126           7 :     IGNORE(fread(&save, sizeof(struct save_t), 1, fp));
     127           7 :     fclose(fp);
     128           7 :     if (save.version != VRSION) {
     129           1 :         rspeak(VERSION_SKEW, save.version / 10, MOD(save.version, 10), VRSION / 10, MOD(VRSION, 10));
     130           6 :     } else if (is_valid(&save.game)) {
     131           6 :         game = save.game;
     132             :     }
     133           7 :     return GO_TOP;
     134             : }
     135             : 
     136           6 : bool is_valid(struct game_t* valgame)
     137             : {
     138             :     /*  Save files can be roughly grouped into three groups:
     139             :      *  With valid, reaceable state, with valid, but unreachable
     140             :      *  state and with invaild state. We check that state is
     141             :      *  valid: no states are outside minimal or maximal value
     142             :      */
     143             : 
     144             :     /* Prevent division by zero */
     145           6 :     if (valgame->abbnum == 0) {
     146           0 :         return false;
     147             :     }
     148             : 
     149             :     /* Check for RNG overflow. Truncate */
     150           6 :     if (valgame->lcg_x >= LCG_M) {
     151           0 :         valgame->lcg_x %= LCG_M;
     152             :     }
     153             : 
     154             :     /* Check for RNG underflow. Transpose */
     155           6 :     if (valgame->lcg_x < LCG_M) {
     156           6 :         valgame->lcg_x = LCG_M + (valgame->lcg_x % LCG_M);
     157             :     }
     158             : 
     159             :     /*  Bounds check for locations */
     160          12 :     if ( valgame->chloc  < -1 || valgame->chloc  > NLOCATIONS ||
     161          18 :          valgame->chloc2 < -1 || valgame->chloc2 > NLOCATIONS ||
     162          18 :          valgame->loc    <  0 || valgame->loc    > NLOCATIONS ||
     163          18 :          valgame->newloc <  0 || valgame->newloc > NLOCATIONS ||
     164          18 :          valgame->oldloc <  0 || valgame->oldloc > NLOCATIONS ||
     165          12 :          valgame->oldlc2 <  0 || valgame->oldlc2 > NLOCATIONS) {
     166           0 :         return false;
     167             :     }
     168             :     /*  Bounds check for location arrays */
     169          48 :     for (int i = 0; i <= NDWARVES; i++) {
     170          84 :         if (valgame->dloc[i]  < -1 || valgame->dloc[i]  > NLOCATIONS  ||
     171          84 :             valgame->odloc[i] < -1 || valgame->odloc[i] > NLOCATIONS) {
     172           0 :             return false;
     173             :         }
     174             :     }
     175             : 
     176         426 :     for (int i = 0; i <= NOBJECTS; i++) {
     177         840 :         if (valgame->place[i] < -1 || valgame->place[i] > NLOCATIONS  ||
     178         840 :             valgame->fixed[i] < -1 || valgame->fixed[i] > NLOCATIONS) {
     179           0 :             return false;
     180             :         }
     181             :     }
     182             : 
     183             :     /*  Bounds check for dwarves */
     184          12 :     if (valgame->dtotal < 0 || valgame->dtotal > NDWARVES ||
     185          12 :         valgame->dkill < 0  || valgame->dkill  > NDWARVES) {
     186           0 :         return false;
     187             :     }
     188             : 
     189             :     /*  Validate that we didn't die too many times in save */
     190           6 :     if (valgame->numdie >= NDEATHS) {
     191           0 :         return false;
     192             :     }
     193             : 
     194             :     /* Recalculate tally, throw the towel if in disagreement */
     195           6 :     long temp_tally = 0;
     196         420 :     for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
     197         414 :         if (objects[treasure].is_treasure) {
     198         120 :             if (valgame->prop[treasure] == STATE_NOTFOUND) {
     199         100 :                 ++temp_tally;
     200             :             }
     201             :         }
     202             :     }
     203           6 :     if (temp_tally != valgame->tally) {
     204           0 :         return false;
     205             :     }
     206             : 
     207             :     /* Check that properties of objects aren't beyond expected */
     208         426 :     for (obj_t obj = 0; obj <= NOBJECTS; obj++) {
     209         420 :         if (valgame->prop[obj] < STATE_NOTFOUND || valgame->prop[obj] > 1) {
     210           5 :             switch (obj) {
     211           4 :             case RUG:
     212             :             case DRAGON:
     213             :             case BIRD:
     214             :             case BOTTLE:
     215             :             case PLANT:
     216             :             case PLANT2:
     217             :             case TROLL:
     218             :             case URN:
     219             :             case EGGS:
     220             :             case VASE:
     221             :             case CHAIN:
     222           4 :                 if (valgame->prop[obj] == 2) // There are multiple different states, but it's convenient to clump them together
     223           4 :                     continue;
     224             :             /* FALLTHRU */
     225             :             case BEAR:
     226           1 :                 if (valgame->prop[BEAR] == CONTENTED_BEAR || valgame->prop[BEAR] == BEAR_DEAD)
     227           1 :                     continue;
     228             :             /* FALLTHRU */
     229             :             default:
     230           0 :                 return false;
     231             :             }
     232             :         }
     233             :     }
     234             : 
     235             :     /* Check that values in linked lists for objects in locations are inside bounds */
     236        1116 :     for (loc_t loc = LOC_NOWHERE; loc <= NLOCATIONS; loc++) {
     237        1110 :         if (valgame->atloc[loc] < NO_OBJECT || valgame->atloc[loc] > NOBJECTS * 2) {
     238           0 :             return false;
     239             :         }
     240             :     }
     241         840 :     for (obj_t obj = 0; obj <= NOBJECTS * 2; obj++ ) {
     242         834 :         if (valgame->link[obj] < NO_OBJECT || valgame->link[obj] > NOBJECTS * 2) {
     243           0 :             return false;
     244             :         }
     245             :     }
     246             : 
     247           6 :     return true;
     248             : }
     249             : 
     250             : /* end */

Generated by: LCOV version 1.13