Initial import from FreeBSD RELENG_4:
[games.git] / games / rogue / save.c
1 /*
2  * Copyright (c) 1988, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Timothy C. Stoehr.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)save.c      8.1 (Berkeley) 5/31/93";
40 #endif
41 static const char rcsid[] =
42  "$FreeBSD: src/games/rogue/save.c,v 1.6 1999/11/30 03:49:27 billf Exp $";
43 #endif /* not lint */
44
45 /*
46  * save.c
47  *
48  * This source herein may be modified and/or distributed by anybody who
49  * so desires, with the following restrictions:
50  *    1.)  No portion of this notice shall be removed.
51  *    2.)  Credit shall not be taken for the creation of this source.
52  *    3.)  This code is not to be traded, sold, or used for personal
53  *         gain or profit.
54  *
55  */
56
57 #include <stdio.h>
58 #include "rogue.h"
59
60 short write_failed = 0;
61 char *save_file = (char *) 0;
62 static char save_name[80];
63
64 extern boolean detect_monster;
65 extern short cur_level, max_level;
66 extern char hunger_str[];
67 extern char login_name[];
68 extern short party_room;
69 extern short foods;
70 extern boolean is_wood[];
71 extern short cur_room;
72 extern boolean being_held;
73 extern short bear_trap;
74 extern short halluc;
75 extern short blind;
76 extern short confused;
77 extern short levitate;
78 extern short haste_self;
79 extern boolean see_invisible;
80 extern boolean detect_monster;
81 extern boolean wizard;
82 extern boolean score_only;
83 extern short m_moves;
84
85 extern boolean msg_cleared;
86
87 save_game()
88 {
89         char fname[64];
90
91         if (!get_input_line("file name?", save_file, fname, "game not saved",
92                         0, 1)) {
93                 return;
94         }
95         check_message();
96         message(fname, 0);
97         save_into_file(fname);
98 }
99
100 save_into_file(sfile)
101 const char *sfile;
102 {
103         FILE *fp;
104         int file_id;
105         char name_buffer[80];
106         char *hptr;
107         struct rogue_time rt_buf;
108
109         if (sfile[0] == '~') {
110                 if (hptr = md_getenv("HOME")) {
111                         (void) strcpy(name_buffer, hptr);
112                         (void) strcat(name_buffer, sfile+1);
113                         sfile = name_buffer;
114                 }
115         }
116         /* revoke */
117         setgid(getgid());
118         if (    ((fp = fopen(sfile, "w")) == NULL) ||
119                         ((file_id = md_get_file_id(sfile)) == -1)) {
120                 message("problem accessing the save file", 0);
121                 return;
122         }
123         md_ignore_signals();
124         write_failed = 0;
125         (void) xxx(1);
126         r_write(fp, (char *) &detect_monster, sizeof(detect_monster));
127         r_write(fp, (char *) &cur_level, sizeof(cur_level));
128         r_write(fp, (char *) &max_level, sizeof(max_level));
129         write_string(hunger_str, fp);
130         write_string(login_name, fp);
131         r_write(fp, (char *) &party_room, sizeof(party_room));
132         write_pack(&level_monsters, fp);
133         write_pack(&level_objects, fp);
134         r_write(fp, (char *) &file_id, sizeof(file_id));
135         rw_dungeon(fp, 1);
136         r_write(fp, (char *) &foods, sizeof(foods));
137         r_write(fp, (char *) &rogue, sizeof(fighter));
138         write_pack(&rogue.pack, fp);
139         rw_id(id_potions, fp, POTIONS, 1);
140         rw_id(id_scrolls, fp, SCROLS, 1);
141         rw_id(id_wands, fp, WANDS, 1);
142         rw_id(id_rings, fp, RINGS, 1);
143         r_write(fp, (char *) traps, (MAX_TRAPS * sizeof(trap)));
144         r_write(fp, (char *) is_wood, (WANDS * sizeof(boolean)));
145         r_write(fp, (char *) &cur_room, sizeof(cur_room));
146         rw_rooms(fp, 1);
147         r_write(fp, (char *) &being_held, sizeof(being_held));
148         r_write(fp, (char *) &bear_trap, sizeof(bear_trap));
149         r_write(fp, (char *) &halluc, sizeof(halluc));
150         r_write(fp, (char *) &blind, sizeof(blind));
151         r_write(fp, (char *) &confused, sizeof(confused));
152         r_write(fp, (char *) &levitate, sizeof(levitate));
153         r_write(fp, (char *) &haste_self, sizeof(haste_self));
154         r_write(fp, (char *) &see_invisible, sizeof(see_invisible));
155         r_write(fp, (char *) &detect_monster, sizeof(detect_monster));
156         r_write(fp, (char *) &wizard, sizeof(wizard));
157         r_write(fp, (char *) &score_only, sizeof(score_only));
158         r_write(fp, (char *) &m_moves, sizeof(m_moves));
159         md_gct(&rt_buf);
160         rt_buf.second += 10;            /* allow for some processing time */
161         r_write(fp, (char *) &rt_buf, sizeof(rt_buf));
162         fclose(fp);
163
164         if (write_failed) {
165                 (void) md_df(sfile);    /* delete file */
166         } else {
167                 if (strcmp(sfile, save_name) == 0)
168                         save_name[0] = 0;
169                 clean_up("");
170         }
171 }
172
173 static del_save_file()
174 {
175         if (!save_name[0])
176                 return;
177         /* revoke */
178         setgid(getgid());
179         md_df(save_name);
180 }
181
182 restore(fname)
183 const char *fname;
184 {
185         FILE *fp;
186         struct rogue_time saved_time, mod_time;
187         char buf[4];
188         char tbuf[40];
189         int new_file_id, saved_file_id;
190
191         if (    ((new_file_id = md_get_file_id(fname)) == -1) ||
192                         ((fp = fopen(fname, "r")) == NULL)) {
193                 clean_up("cannot open file");
194         }
195         if (md_link_count(fname) > 1) {
196                 clean_up("file has link");
197         }
198         (void) xxx(1);
199         r_read(fp, (char *) &detect_monster, sizeof(detect_monster));
200         r_read(fp, (char *) &cur_level, sizeof(cur_level));
201         r_read(fp, (char *) &max_level, sizeof(max_level));
202         read_string(hunger_str, fp);
203
204         (void) strcpy(tbuf, login_name);
205         read_string(login_name, fp);
206         if (strcmp(tbuf, login_name)) {
207                 clean_up("you're not the original player");
208         }
209
210         r_read(fp, (char *) &party_room, sizeof(party_room));
211         read_pack(&level_monsters, fp, 0);
212         read_pack(&level_objects, fp, 0);
213         r_read(fp, (char *) &saved_file_id, sizeof(saved_file_id));
214         if (new_file_id != saved_file_id) {
215                 clean_up("sorry, saved game is not in the same file");
216         }
217         rw_dungeon(fp, 0);
218         r_read(fp, (char *) &foods, sizeof(foods));
219         r_read(fp, (char *) &rogue, sizeof(fighter));
220         read_pack(&rogue.pack, fp, 1);
221         rw_id(id_potions, fp, POTIONS, 0);
222         rw_id(id_scrolls, fp, SCROLS, 0);
223         rw_id(id_wands, fp, WANDS, 0);
224         rw_id(id_rings, fp, RINGS, 0);
225         r_read(fp, (char *) traps, (MAX_TRAPS * sizeof(trap)));
226         r_read(fp, (char *) is_wood, (WANDS * sizeof(boolean)));
227         r_read(fp, (char *) &cur_room, sizeof(cur_room));
228         rw_rooms(fp, 0);
229         r_read(fp, (char *) &being_held, sizeof(being_held));
230         r_read(fp, (char *) &bear_trap, sizeof(bear_trap));
231         r_read(fp, (char *) &halluc, sizeof(halluc));
232         r_read(fp, (char *) &blind, sizeof(blind));
233         r_read(fp, (char *) &confused, sizeof(confused));
234         r_read(fp, (char *) &levitate, sizeof(levitate));
235         r_read(fp, (char *) &haste_self, sizeof(haste_self));
236         r_read(fp, (char *) &see_invisible, sizeof(see_invisible));
237         r_read(fp, (char *) &detect_monster, sizeof(detect_monster));
238         r_read(fp, (char *) &wizard, sizeof(wizard));
239         r_read(fp, (char *) &score_only, sizeof(score_only));
240         r_read(fp, (char *) &m_moves, sizeof(m_moves));
241         r_read(fp, (char *) &saved_time, sizeof(saved_time));
242
243         if (fread(buf, sizeof(char), 1, fp) > 0) {
244                 clear();
245                 clean_up("extra characters in file");
246         }
247
248         md_gfmt(fname, &mod_time);      /* get file modification time */
249
250         if (has_been_touched(&saved_time, &mod_time)) {
251                 clear();
252                 clean_up("sorry, file has been touched");
253         }
254         if ((!wizard)) {
255                 strcpy(save_name, fname);
256                 atexit(del_save_file);
257         }
258         msg_cleared = 0;
259         ring_stats(0);
260         fclose(fp);
261 }
262
263 write_pack(pack, fp)
264 const object *pack;
265 FILE *fp;
266 {
267         object t;
268
269         while (pack = pack->next_object) {
270                 r_write(fp, (const char *) pack, sizeof(object));
271         }
272         t.ichar = t.what_is = 0;
273         r_write(fp, (const char *) &t, sizeof(object));
274 }
275
276 read_pack(pack, fp, is_rogue)
277 object *pack;
278 FILE *fp;
279 boolean is_rogue;
280 {
281         object read_obj, *new_obj;
282
283         for (;;) {
284                 r_read(fp, (char *) &read_obj, sizeof(object));
285                 if (read_obj.ichar == 0) {
286                         pack->next_object = (object *) 0;
287                         break;
288                 }
289                 new_obj = alloc_object();
290                 *new_obj = read_obj;
291                 if (is_rogue) {
292                         if (new_obj->in_use_flags & BEING_WORN) {
293                                         do_wear(new_obj);
294                         } else if (new_obj->in_use_flags & BEING_WIELDED) {
295                                         do_wield(new_obj);
296                         } else if (new_obj->in_use_flags & (ON_EITHER_HAND)) {
297                                 do_put_on(new_obj,
298                                         ((new_obj->in_use_flags & ON_LEFT_HAND) ? 1 : 0));
299                         }
300                 }
301                 pack->next_object = new_obj;
302                 pack = new_obj;
303         }
304 }
305
306 rw_dungeon(fp, rw)
307 FILE *fp;
308 boolean rw;
309 {
310         short i, j;
311         char buf[DCOLS];
312
313         for (i = 0; i < DROWS; i++) {
314                 if (rw) {
315                         r_write(fp, (char *) dungeon[i], (DCOLS * sizeof(dungeon[0][0])));
316                         for (j = 0; j < DCOLS; j++) {
317                                 buf[j] = mvinch(i, j);
318                         }
319                         r_write(fp, buf, DCOLS);
320                 } else {
321                         r_read(fp, (char *) dungeon[i], (DCOLS * sizeof(dungeon[0][0])));
322                         r_read(fp, buf, DCOLS);
323                         for (j = 0; j < DCOLS; j++) {
324                                 mvaddch(i, j, buf[j]);
325                         }
326                 }
327         }
328 }
329
330 rw_id(id_table, fp, n, wr)
331 struct id id_table[];
332 FILE *fp;
333 int n;
334 boolean wr;
335 {
336         short i;
337
338         for (i = 0; i < n; i++) {
339                 if (wr) {
340                         r_write(fp, (const char *) &(id_table[i].value), sizeof(short));
341                         r_write(fp, (const char *) &(id_table[i].id_status),
342                                 sizeof(unsigned short));
343                         write_string(id_table[i].title, fp);
344                 } else {
345                         r_read(fp, (char *) &(id_table[i].value), sizeof(short));
346                         r_read(fp, (char *) &(id_table[i].id_status),
347                                 sizeof(unsigned short));
348                         read_string(id_table[i].title, fp);
349                 }
350         }
351 }
352
353 write_string(s, fp)
354 char *s;
355 FILE *fp;
356 {
357         short n;
358
359         n = strlen(s) + 1;
360         xxxx(s, n);
361         r_write(fp, (char *) &n, sizeof(short));
362         r_write(fp, s, n);
363 }
364
365 read_string(s, fp)
366 char *s;
367 FILE *fp;
368 {
369         short n;
370
371         r_read(fp, (char *) &n, sizeof(short));
372         r_read(fp, s, n);
373         xxxx(s, n);
374 }
375
376 rw_rooms(fp, rw)
377 FILE *fp;
378 boolean rw;
379 {
380         short i;
381
382         for (i = 0; i < MAXROOMS; i++) {
383                 rw ? r_write(fp, (char *) (rooms + i), sizeof(room)) :
384                         r_read(fp, (char *) (rooms + i), sizeof(room));
385         }
386 }
387
388 r_read(fp, buf, n)
389 FILE *fp;
390 char *buf;
391 int n;
392 {
393         if (fread(buf, sizeof(char), n, fp) != n) {
394                 clean_up("read() failed, don't know why");
395         }
396 }
397
398 r_write(fp, buf, n)
399 FILE *fp;
400 const char *buf;
401 int n;
402 {
403         if (!write_failed) {
404                 if (fwrite(buf, sizeof(char), n, fp) != n) {
405                         message("write() failed, don't know why", 0);
406                         sound_bell();
407                         write_failed = 1;
408                 }
409         }
410 }
411
412 boolean
413 has_been_touched(saved_time, mod_time)
414 const struct rogue_time *saved_time, *mod_time;
415 {
416         if (saved_time->year < mod_time->year) {
417                 return(1);
418         } else if (saved_time->year > mod_time->year) {
419                 return(0);
420         }
421         if (saved_time->month < mod_time->month) {
422                 return(1);
423         } else if (saved_time->month > mod_time->month) {
424                 return(0);
425         }
426         if (saved_time->day < mod_time->day) {
427                 return(1);
428         } else if (saved_time->day > mod_time->day) {
429                 return(0);
430         }
431         if (saved_time->hour < mod_time->hour) {
432                 return(1);
433         } else if (saved_time->hour > mod_time->hour) {
434                 return(0);
435         }
436         if (saved_time->minute < mod_time->minute) {
437                 return(1);
438         } else if (saved_time->minute > mod_time->minute) {
439                 return(0);
440         }
441         if (saved_time->second < mod_time->second) {
442                 return(1);
443         }
444         return(0);
445 }