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: 2017-09-17 15:40:10 Functions: 5 5 100.0 %

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

Generated by: LCOV version 1.13