1 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2 /* hack.save.c - version 1.0.3 */
3 /* $FreeBSD: src/games/hack/hack.save.c,v 1.4 1999/11/16 10:26:37 marcel Exp $ */
4 /* $DragonFly: src/games/hack/hack.save.c,v 1.2 2003/06/17 04:25:24 dillon Exp $ */
7 extern char genocided[60]; /* defined in Decl.c */
8 extern char fut_geno[60]; /* idem */
12 extern char SAVEF[], nul[];
13 extern char pl_character[PL_CSIZ];
14 extern struct obj *restobjchn();
15 extern struct monst *restmonchn();
19 settty("Be seeing you ...\n");
27 #ifndef NOSAVEONHANGUP
34 /* returns 1 if save successful */
39 (void) signal(SIGHUP, SIG_IGN);
40 (void) signal(SIGINT, SIG_IGN);
41 if((fd = creat(SAVEF, FMASK)) < 0) {
42 if(!hu) pline("Cannot open save file. (Continue or Quit)");
43 (void) unlink(SAVEF); /* ab@unido */
46 if(flags.moonphase == FULL_MOON) /* ut-sally!fletcher */
47 u.uluck--; /* and unido!ab */
49 saveobjchn(fd, invent);
50 saveobjchn(fd, fcobj);
51 savemonchn(fd, fallen_down);
53 bwrite(fd, (char *) &tmp, sizeof tmp);
54 bwrite(fd, (char *) &flags, sizeof(struct flag));
55 bwrite(fd, (char *) &dlevel, sizeof dlevel);
56 bwrite(fd, (char *) &maxdlevel, sizeof maxdlevel);
57 bwrite(fd, (char *) &moves, sizeof moves);
58 bwrite(fd, (char *) &u, sizeof(struct you));
60 bwrite(fd, (char *) &(u.ustuck->m_id), sizeof u.ustuck->m_id);
61 bwrite(fd, (char *) pl_character, sizeof pl_character);
62 bwrite(fd, (char *) genocided, sizeof genocided);
63 bwrite(fd, (char *) fut_geno, sizeof fut_geno);
65 for(tmp = 1; tmp <= maxdlevel; tmp++) {
67 extern boolean level_exists[];
69 if(tmp == dlevel || !level_exists[tmp]) continue;
71 if((ofd = open(lock, 0)) < 0) {
72 if(!hu) pline("Error while saving: cannot read %s.", lock);
75 if(!hu) done("tricked");
78 getlev(ofd, hackpid, tmp);
80 bwrite(fd, (char *) &tmp, sizeof tmp); /* level number */
81 savelev(fd,tmp); /* actual level */
86 (void) unlink(lock); /* get rid of current level --jgm */
96 int tmp; /* not a ! */
97 unsigned mid; /* idem */
99 extern boolean restoring;
103 invent = restobjchn(fd);
104 for(otmp = invent; otmp; otmp = otmp->nobj)
106 setworn(otmp, otmp->owornmask);
107 fcobj = restobjchn(fd);
108 fallen_down = restmonchn(fd);
109 mread(fd, (char *) &tmp, sizeof tmp);
110 if(tmp != getuid()) { /* strange ... */
112 (void) unlink(SAVEF);
113 puts("Saved game was not yours.");
117 mread(fd, (char *) &flags, sizeof(struct flag));
118 mread(fd, (char *) &dlevel, sizeof dlevel);
119 mread(fd, (char *) &maxdlevel, sizeof maxdlevel);
120 mread(fd, (char *) &moves, sizeof moves);
121 mread(fd, (char *) &u, sizeof(struct you));
123 mread(fd, (char *) &mid, sizeof mid);
124 mread(fd, (char *) pl_character, sizeof pl_character);
125 mread(fd, (char *) genocided, sizeof genocided);
126 mread(fd, (char *) fut_geno, sizeof fut_geno);
129 if(read(fd, (char *) &tmp, sizeof tmp) != sizeof tmp)
133 if((nfd = creat(lock, FMASK)) < 0)
134 panic("Cannot open temp file %s!\n", lock);
138 (void) lseek(fd, (off_t)0, 0);
141 (void) unlink(SAVEF);
143 for(otmp = fobj; otmp; otmp = otmp->nobj)
144 if(otmp->olet == CHAIN_SYM) goto chainfnd;
145 panic("Cannot find the iron chain?");
149 for(otmp = fobj; otmp; otmp = otmp->nobj)
150 if(otmp->olet == BALL_SYM && otmp->spe)
152 panic("Cannot find the iron ball?");
160 for(mtmp = fmon; mtmp; mtmp = mtmp->nmon)
161 if(mtmp->m_id == mid) goto monfnd;
162 panic("Cannot find the monster ustuck.");
167 setsee(); /* only to recompute seelx etc. - these weren't saved */
178 struct obj *otmp, *otmp2;
179 struct obj *first = 0;
182 /* suppress "used before set" warning from lint */
186 mread(fd, (char *) &xl, sizeof(xl));
189 if(!first) first = otmp;
190 else otmp2->nobj = otmp;
191 mread(fd, (char *) otmp, (unsigned) xl + sizeof(struct obj));
192 if(!otmp->o_id) otmp->o_id = flags.ident++;
195 if(first && otmp2->nobj){
196 impossible("Restobjchn: error reading objchn.");
206 struct monst *mtmp, *mtmp2;
207 struct monst *first = 0;
210 struct permonst *monbegin;
213 mread(fd, (char *)&monbegin, sizeof(monbegin));
214 differ = (char *)(&mons[0]) - (char *)(monbegin);
217 /* suppress "used before set" warning from lint */
221 mread(fd, (char *) &xl, sizeof(xl));
224 if(!first) first = mtmp;
225 else mtmp2->nmon = mtmp;
226 mread(fd, (char *) mtmp, (unsigned) xl + sizeof(struct monst));
228 mtmp->m_id = flags.ident++;
229 mtmp->data = (struct permonst *)
230 ((char *) mtmp->data + differ);
232 mtmp->minvent = restobjchn(fd);
235 if(first && mtmp2->nmon){
236 impossible("Restmonchn: error reading monchn.");