3abcd51db6202e4b695cc6837ed13867f6640417
[dragonfly.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  * @(#)save.c   8.1 (Berkeley) 5/31/93
37  * $FreeBSD: src/games/rogue/save.c,v 1.6 1999/11/30 03:49:27 billf Exp $
38  * $DragonFly: src/games/rogue/save.c,v 1.2 2003/06/17 04:25:25 dillon Exp $
39  */
40
41 /*
42  * save.c
43  *
44  * This source herein may be modified and/or distributed by anybody who
45  * so desires, with the following restrictions:
46  *    1.)  No portion of this notice shall be removed.
47  *    2.)  Credit shall not be taken for the creation of this source.
48  *    3.)  This code is not to be traded, sold, or used for personal
49  *         gain or profit.
50  *
51  */
52
53 #include <stdio.h>
54 #include "rogue.h"
55
56 short write_failed = 0;
57 char *save_file = (char *) 0;
58 static char save_name[80];
59
60 extern boolean detect_monster;
61 extern short cur_level, max_level;
62 extern char hunger_str[];
63 extern char login_name[];
64 extern short party_room;
65 extern short foods;
66 extern boolean is_wood[];
67 extern short cur_room;
68 extern boolean being_held;
69 extern short bear_trap;
70 extern short halluc;
71 extern short blind;
72 extern short confused;
73 extern short levitate;
74 extern short haste_self;
75 extern boolean see_invisible;
76 extern boolean detect_monster;
77 extern boolean wizard;
78 extern boolean score_only;
79 extern short m_moves;
80
81 extern boolean msg_cleared;
82
83 save_game()
84 {
85         char fname[64];
86
87         if (!get_input_line("file name?", save_file, fname, "game not saved",
88                         0, 1)) {
89                 return;
90         }
91         check_message();
92         message(fname, 0);
93         save_into_file(fname);
94 }
95
96 save_into_file(sfile)
97 const char *sfile;
98 {
99         FILE *fp;
100         int file_id;
101         char name_buffer[80];
102         char *hptr;
103         struct rogue_time rt_buf;
104
105         if (sfile[0] == '~') {
106                 if (hptr = md_getenv("HOME")) {
107                         (void) strcpy(name_buffer, hptr);
108                         (void) strcat(name_buffer, sfile+1);
109                         sfile = name_buffer;
110                 }
111         }
112         /* revoke */
113         setgid(getgid());
114         if (    ((fp = fopen(sfile, "w")) == NULL) ||
115                         ((file_id = md_get_file_id(sfile)) == -1)) {
116                 message("problem accessing the save file", 0);
117                 return;
118         }
119         md_ignore_signals();
120         write_failed = 0;
121         (void) xxx(1);
122         r_write(fp, (char *) &detect_monster, sizeof(detect_monster));
123         r_write(fp, (char *) &cur_level, sizeof(cur_level));
124         r_write(fp, (char *) &max_level, sizeof(max_level));
125         write_string(hunger_str, fp);
126         write_string(login_name, fp);
127         r_write(fp, (char *) &party_room, sizeof(party_room));
128         write_pack(&level_monsters, fp);
129         write_pack(&level_objects, fp);
130         r_write(fp, (char *) &file_id, sizeof(file_id));
131         rw_dungeon(fp, 1);
132         r_write(fp, (char *) &foods, sizeof(foods));
133         r_write(fp, (char *) &rogue, sizeof(fighter));
134         write_pack(&rogue.pack, fp);
135         rw_id(id_potions, fp, POTIONS, 1);
136         rw_id(id_scrolls, fp, SCROLS, 1);
137         rw_id(id_wands, fp, WANDS, 1);
138         rw_id(id_rings, fp, RINGS, 1);
139         r_write(fp, (char *) traps, (MAX_TRAPS * sizeof(trap)));
140         r_write(fp, (char *) is_wood, (WANDS * sizeof(boolean)));
141         r_write(fp, (char *) &cur_room, sizeof(cur_room));
142         rw_rooms(fp, 1);
143         r_write(fp, (char *) &being_held, sizeof(being_held));
144         r_write(fp, (char *) &bear_trap, sizeof(bear_trap));
145         r_write(fp, (char *) &halluc, sizeof(halluc));
146         r_write(fp, (char *) &blind, sizeof(blind));
147         r_write(fp, (char *) &confused, sizeof(confused));
148         r_write(fp, (char *) &levitate, sizeof(levitate));
149         r_write(fp, (char *) &haste_self, sizeof(haste_self));
150         r_write(fp, (char *) &see_invisible, sizeof(see_invisible));
151         r_write(fp, (char *) &detect_monster, sizeof(detect_monster));
152         r_write(fp, (char *) &wizard, sizeof(wizard));
153         r_write(fp, (char *) &score_only, sizeof(score_only));
154         r_write(fp, (char *) &m_moves, sizeof(m_moves));
155         md_gct(&rt_buf);
156         rt_buf.second += 10;            /* allow for some processing time */
157         r_write(fp, (char *) &rt_buf, sizeof(rt_buf));
158         fclose(fp);
159
160         if (write_failed) {
161                 (void) md_df(sfile);    /* delete file */
162         } else {
163                 if (strcmp(sfile, save_name) == 0)
164                         save_name[0] = 0;
165                 clean_up("");
166         }
167 }
168
169 static del_save_file()
170 {
171         if (!save_name[0])
172                 return;
173         /* revoke */
174         setgid(getgid());
175         md_df(save_name);
176 }
177
178 restore(fname)
179 const char *fname;
180 {
181         FILE *fp;
182         struct rogue_time saved_time, mod_time;
183         char buf[4];
184         char tbuf[40];
185         int new_file_id, saved_file_id;
186
187         if (    ((new_file_id = md_get_file_id(fname)) == -1) ||
188                         ((fp = fopen(fname, "r")) == NULL)) {
189                 clean_up("cannot open file");
190         }
191         if (md_link_count(fname) > 1) {
192                 clean_up("file has link");
193         }
194         (void) xxx(1);
195         r_read(fp, (char *) &detect_monster, sizeof(detect_monster));
196         r_read(fp, (char *) &cur_level, sizeof(cur_level));
197         r_read(fp, (char *) &max_level, sizeof(max_level));
198         read_string(hunger_str, fp);
199
200         (void) strcpy(tbuf, login_name);
201         read_string(login_name, fp);
202         if (strcmp(tbuf, login_name)) {
203                 clean_up("you're not the original player");
204         }
205
206         r_read(fp, (char *) &party_room, sizeof(party_room));
207         read_pack(&level_monsters, fp, 0);
208         read_pack(&level_objects, fp, 0);
209         r_read(fp, (char *) &saved_file_id, sizeof(saved_file_id));
210         if (new_file_id != saved_file_id) {
211                 clean_up("sorry, saved game is not in the same file");
212         }
213         rw_dungeon(fp, 0);
214         r_read(fp, (char *) &foods, sizeof(foods));
215         r_read(fp, (char *) &rogue, sizeof(fighter));
216         read_pack(&rogue.pack, fp, 1);
217         rw_id(id_potions, fp, POTIONS, 0);
218         rw_id(id_scrolls, fp, SCROLS, 0);
219         rw_id(id_wands, fp, WANDS, 0);
220         rw_id(id_rings, fp, RINGS, 0);
221         r_read(fp, (char *) traps, (MAX_TRAPS * sizeof(trap)));
222         r_read(fp, (char *) is_wood, (WANDS * sizeof(boolean)));
223         r_read(fp, (char *) &cur_room, sizeof(cur_room));
224         rw_rooms(fp, 0);
225         r_read(fp, (char *) &being_held, sizeof(being_held));
226         r_read(fp, (char *) &bear_trap, sizeof(bear_trap));
227         r_read(fp, (char *) &halluc, sizeof(halluc));
228         r_read(fp, (char *) &blind, sizeof(blind));
229         r_read(fp, (char *) &confused, sizeof(confused));
230         r_read(fp, (char *) &levitate, sizeof(levitate));
231         r_read(fp, (char *) &haste_self, sizeof(haste_self));
232         r_read(fp, (char *) &see_invisible, sizeof(see_invisible));
233         r_read(fp, (char *) &detect_monster, sizeof(detect_monster));
234         r_read(fp, (char *) &wizard, sizeof(wizard));
235         r_read(fp, (char *) &score_only, sizeof(score_only));
236         r_read(fp, (char *) &m_moves, sizeof(m_moves));
237         r_read(fp, (char *) &saved_time, sizeof(saved_time));
238
239         if (fread(buf, sizeof(char), 1, fp) > 0) {
240                 clear();
241                 clean_up("extra characters in file");
242         }
243
244         md_gfmt(fname, &mod_time);      /* get file modification time */
245
246         if (has_been_touched(&saved_time, &mod_time)) {
247                 clear();
248                 clean_up("sorry, file has been touched");
249         }
250         if ((!wizard)) {
251                 strcpy(save_name, fname);
252                 atexit(del_save_file);
253         }
254         msg_cleared = 0;
255         ring_stats(0);
256         fclose(fp);
257 }
258
259 write_pack(pack, fp)
260 const object *pack;
261 FILE *fp;
262 {
263         object t;
264
265         while (pack = pack->next_object) {
266                 r_write(fp, (const char *) pack, sizeof(object));
267         }
268         t.ichar = t.what_is = 0;
269         r_write(fp, (const char *) &t, sizeof(object));
270 }
271
272 read_pack(pack, fp, is_rogue)
273 object *pack;
274 FILE *fp;
275 boolean is_rogue;
276 {
277         object read_obj, *new_obj;
278
279         for (;;) {
280                 r_read(fp, (char *) &read_obj, sizeof(object));
281                 if (read_obj.ichar == 0) {
282                         pack->next_object = (object *) 0;
283                         break;
284                 }
285                 new_obj = alloc_object();
286                 *new_obj = read_obj;
287                 if (is_rogue) {
288                         if (new_obj->in_use_flags & BEING_WORN) {
289                                         do_wear(new_obj);
290                         } else if (new_obj->in_use_flags & BEING_WIELDED) {
291                                         do_wield(new_obj);
292                         } else if (new_obj->in_use_flags & (ON_EITHER_HAND)) {
293                                 do_put_on(new_obj,
294                                         ((new_obj->in_use_flags & ON_LEFT_HAND) ? 1 : 0));
295                         }
296                 }
297                 pack->next_object = new_obj;
298                 pack = new_obj;
299         }
300 }
301
302 rw_dungeon(fp, rw)
303 FILE *fp;
304 boolean rw;
305 {
306         short i, j;
307         char buf[DCOLS];
308
309         for (i = 0; i < DROWS; i++) {
310                 if (rw) {
311                         r_write(fp, (char *) dungeon[i], (DCOLS * sizeof(dungeon[0][0])));
312                         for (j = 0; j < DCOLS; j++) {
313                                 buf[j] = mvinch(i, j);
314                         }
315                         r_write(fp, buf, DCOLS);
316                 } else {
317                         r_read(fp, (char *) dungeon[i], (DCOLS * sizeof(dungeon[0][0])));
318                         r_read(fp, buf, DCOLS);
319                         for (j = 0; j < DCOLS; j++) {
320                                 mvaddch(i, j, buf[j]);
321                         }
322                 }
323         }
324 }
325
326 rw_id(id_table, fp, n, wr)
327 struct id id_table[];
328 FILE *fp;
329 int n;
330 boolean wr;
331 {
332         short i;
333
334         for (i = 0; i < n; i++) {
335                 if (wr) {
336                         r_write(fp, (const char *) &(id_table[i].value), sizeof(short));
337                         r_write(fp, (const char *) &(id_table[i].id_status),
338                                 sizeof(unsigned short));
339                         write_string(id_table[i].title, fp);
340                 } else {
341                         r_read(fp, (char *) &(id_table[i].value), sizeof(short));
342                         r_read(fp, (char *) &(id_table[i].id_status),
343                                 sizeof(unsigned short));
344                         read_string(id_table[i].title, fp);
345                 }
346         }
347 }
348
349 write_string(s, fp)
350 char *s;
351 FILE *fp;
352 {
353         short n;
354
355         n = strlen(s) + 1;
356         xxxx(s, n);
357         r_write(fp, (char *) &n, sizeof(short));
358         r_write(fp, s, n);
359 }
360
361 read_string(s, fp)
362 char *s;
363 FILE *fp;
364 {
365         short n;
366
367         r_read(fp, (char *) &n, sizeof(short));
368         r_read(fp, s, n);
369         xxxx(s, n);
370 }
371
372 rw_rooms(fp, rw)
373 FILE *fp;
374 boolean rw;
375 {
376         short i;
377
378         for (i = 0; i < MAXROOMS; i++) {
379                 rw ? r_write(fp, (char *) (rooms + i), sizeof(room)) :
380                         r_read(fp, (char *) (rooms + i), sizeof(room));
381         }
382 }
383
384 r_read(fp, buf, n)
385 FILE *fp;
386 char *buf;
387 int n;
388 {
389         if (fread(buf, sizeof(char), n, fp) != n) {
390                 clean_up("read() failed, don't know why");
391         }
392 }
393
394 r_write(fp, buf, n)
395 FILE *fp;
396 const char *buf;
397 int n;
398 {
399         if (!write_failed) {
400                 if (fwrite(buf, sizeof(char), n, fp) != n) {
401                         message("write() failed, don't know why", 0);
402                         sound_bell();
403                         write_failed = 1;
404                 }
405         }
406 }
407
408 boolean
409 has_been_touched(saved_time, mod_time)
410 const struct rogue_time *saved_time, *mod_time;
411 {
412         if (saved_time->year < mod_time->year) {
413                 return(1);
414         } else if (saved_time->year > mod_time->year) {
415                 return(0);
416         }
417         if (saved_time->month < mod_time->month) {
418                 return(1);
419         } else if (saved_time->month > mod_time->month) {
420                 return(0);
421         }
422         if (saved_time->day < mod_time->day) {
423                 return(1);
424         } else if (saved_time->day > mod_time->day) {
425                 return(0);
426         }
427         if (saved_time->hour < mod_time->hour) {
428                 return(1);
429         } else if (saved_time->hour > mod_time->hour) {
430                 return(0);
431         }
432         if (saved_time->minute < mod_time->minute) {
433                 return(1);
434         } else if (saved_time->minute > mod_time->minute) {
435                 return(0);
436         }
437         if (saved_time->second < mod_time->second) {
438                 return(1);
439         }
440         return(0);
441 }