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