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