1 /* 2 * Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. 3 */ 4 5 #ifndef lint 6 static char rcsid[] = "$Id: hack.save.c,v 1.4 1994/03/28 11:21:12 cgd Exp $"; 7 #endif /* not lint */ 8 9 #include "hack.h" 10 extern char genocided[60]; /* defined in Decl.c */ 11 extern char fut_geno[60]; /* idem */ 12 #include <signal.h> 13 14 extern char SAVEF[], nul[]; 15 extern char pl_character[PL_CSIZ]; 16 extern struct obj *restobjchn(); 17 extern struct monst *restmonchn(); 18 19 dosave(){ 20 if(dosave0(0)) { 21 settty("Be seeing you ...\n"); 22 exit(0); 23 } 24 #ifdef lint 25 return(0); 26 #endif lint 27 } 28 29 #ifndef NOSAVEONHANGUP 30 hangup(){ 31 (void) dosave0(1); 32 exit(1); 33 } 34 #endif NOSAVEONHANGUP 35 36 /* returns 1 if save successful */ 37 dosave0(hu) int hu; { 38 register fd, ofd; 39 int tmp; /* not register ! */ 40 41 (void) signal(SIGHUP, SIG_IGN); 42 (void) signal(SIGINT, SIG_IGN); 43 if((fd = creat(SAVEF, FMASK)) < 0) { 44 if(!hu) pline("Cannot open save file. (Continue or Quit)"); 45 (void) unlink(SAVEF); /* ab@unido */ 46 return(0); 47 } 48 if(flags.moonphase == FULL_MOON) /* ut-sally!fletcher */ 49 u.uluck--; /* and unido!ab */ 50 savelev(fd,dlevel); 51 saveobjchn(fd, invent); 52 saveobjchn(fd, fcobj); 53 savemonchn(fd, fallen_down); 54 tmp = getuid(); 55 bwrite(fd, (char *) &tmp, sizeof tmp); 56 bwrite(fd, (char *) &flags, sizeof(struct flag)); 57 bwrite(fd, (char *) &dlevel, sizeof dlevel); 58 bwrite(fd, (char *) &maxdlevel, sizeof maxdlevel); 59 bwrite(fd, (char *) &moves, sizeof moves); 60 bwrite(fd, (char *) &u, sizeof(struct you)); 61 if(u.ustuck) 62 bwrite(fd, (char *) &(u.ustuck->m_id), sizeof u.ustuck->m_id); 63 bwrite(fd, (char *) pl_character, sizeof pl_character); 64 bwrite(fd, (char *) genocided, sizeof genocided); 65 bwrite(fd, (char *) fut_geno, sizeof fut_geno); 66 savenames(fd); 67 for(tmp = 1; tmp <= maxdlevel; tmp++) { 68 extern int hackpid; 69 extern boolean level_exists[]; 70 71 if(tmp == dlevel || !level_exists[tmp]) continue; 72 glo(tmp); 73 if((ofd = open(lock, 0)) < 0) { 74 if(!hu) pline("Error while saving: cannot read %s.", lock); 75 (void) close(fd); 76 (void) unlink(SAVEF); 77 if(!hu) done("tricked"); 78 return(0); 79 } 80 getlev(ofd, hackpid, tmp); 81 (void) close(ofd); 82 bwrite(fd, (char *) &tmp, sizeof tmp); /* level number */ 83 savelev(fd,tmp); /* actual level */ 84 (void) unlink(lock); 85 } 86 (void) close(fd); 87 glo(dlevel); 88 (void) unlink(lock); /* get rid of current level --jgm */ 89 glo(0); 90 (void) unlink(lock); 91 return(1); 92 } 93 94 dorecover(fd) 95 register fd; 96 { 97 register nfd; 98 int tmp; /* not a register ! */ 99 unsigned mid; /* idem */ 100 struct obj *otmp; 101 extern boolean restoring; 102 103 restoring = TRUE; 104 getlev(fd, 0, 0); 105 invent = restobjchn(fd); 106 for(otmp = invent; otmp; otmp = otmp->nobj) 107 if(otmp->owornmask) 108 setworn(otmp, otmp->owornmask); 109 fcobj = restobjchn(fd); 110 fallen_down = restmonchn(fd); 111 mread(fd, (char *) &tmp, sizeof tmp); 112 if(tmp != getuid()) { /* strange ... */ 113 (void) close(fd); 114 (void) unlink(SAVEF); 115 puts("Saved game was not yours."); 116 restoring = FALSE; 117 return(0); 118 } 119 mread(fd, (char *) &flags, sizeof(struct flag)); 120 mread(fd, (char *) &dlevel, sizeof dlevel); 121 mread(fd, (char *) &maxdlevel, sizeof maxdlevel); 122 mread(fd, (char *) &moves, sizeof moves); 123 mread(fd, (char *) &u, sizeof(struct you)); 124 if(u.ustuck) 125 mread(fd, (char *) &mid, sizeof mid); 126 mread(fd, (char *) pl_character, sizeof pl_character); 127 mread(fd, (char *) genocided, sizeof genocided); 128 mread(fd, (char *) fut_geno, sizeof fut_geno); 129 restnames(fd); 130 while(1) { 131 if(read(fd, (char *) &tmp, sizeof tmp) != sizeof tmp) 132 break; 133 getlev(fd, 0, tmp); 134 glo(tmp); 135 if((nfd = creat(lock, FMASK)) < 0) 136 panic("Cannot open temp file %s!\n", lock); 137 savelev(nfd,tmp); 138 (void) close(nfd); 139 } 140 (void) lseek(fd, 0, 0); 141 getlev(fd, 0, 0); 142 (void) close(fd); 143 (void) unlink(SAVEF); 144 if(Punished) { 145 for(otmp = fobj; otmp; otmp = otmp->nobj) 146 if(otmp->olet == CHAIN_SYM) goto chainfnd; 147 panic("Cannot find the iron chain?"); 148 chainfnd: 149 uchain = otmp; 150 if(!uball){ 151 for(otmp = fobj; otmp; otmp = otmp->nobj) 152 if(otmp->olet == BALL_SYM && otmp->spe) 153 goto ballfnd; 154 panic("Cannot find the iron ball?"); 155 ballfnd: 156 uball = otmp; 157 } 158 } 159 if(u.ustuck) { 160 register struct monst *mtmp; 161 162 for(mtmp = fmon; mtmp; mtmp = mtmp->nmon) 163 if(mtmp->m_id == mid) goto monfnd; 164 panic("Cannot find the monster ustuck."); 165 monfnd: 166 u.ustuck = mtmp; 167 } 168 #ifndef QUEST 169 setsee(); /* only to recompute seelx etc. - these weren't saved */ 170 #endif QUEST 171 docrt(); 172 restoring = FALSE; 173 return(1); 174 } 175 176 struct obj * 177 restobjchn(fd) 178 register fd; 179 { 180 register struct obj *otmp, *otmp2; 181 register struct obj *first = 0; 182 int xl; 183 #ifdef lint 184 /* suppress "used before set" warning from lint */ 185 otmp2 = 0; 186 #endif lint 187 while(1) { 188 mread(fd, (char *) &xl, sizeof(xl)); 189 if(xl == -1) break; 190 otmp = newobj(xl); 191 if(!first) first = otmp; 192 else otmp2->nobj = otmp; 193 mread(fd, (char *) otmp, (unsigned) xl + sizeof(struct obj)); 194 if(!otmp->o_id) otmp->o_id = flags.ident++; 195 otmp2 = otmp; 196 } 197 if(first && otmp2->nobj){ 198 impossible("Restobjchn: error reading objchn."); 199 otmp2->nobj = 0; 200 } 201 return(first); 202 } 203 204 struct monst * 205 restmonchn(fd) 206 register fd; 207 { 208 register struct monst *mtmp, *mtmp2; 209 register struct monst *first = 0; 210 int xl; 211 212 struct permonst *monbegin; 213 long differ; 214 215 mread(fd, (char *)&monbegin, sizeof(monbegin)); 216 differ = (char *)(&mons[0]) - (char *)(monbegin); 217 218 #ifdef lint 219 /* suppress "used before set" warning from lint */ 220 mtmp2 = 0; 221 #endif lint 222 while(1) { 223 mread(fd, (char *) &xl, sizeof(xl)); 224 if(xl == -1) break; 225 mtmp = newmonst(xl); 226 if(!first) first = mtmp; 227 else mtmp2->nmon = mtmp; 228 mread(fd, (char *) mtmp, (unsigned) xl + sizeof(struct monst)); 229 if(!mtmp->m_id) 230 mtmp->m_id = flags.ident++; 231 mtmp->data = (struct permonst *) 232 ((char *) mtmp->data + differ); 233 if(mtmp->minvent) 234 mtmp->minvent = restobjchn(fd); 235 mtmp2 = mtmp; 236 } 237 if(first && mtmp2->nmon){ 238 impossible("Restmonchn: error reading monchn."); 239 mtmp2->nmon = 0; 240 } 241 return(first); 242 } 243