2 * Copyright (c) 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)save.c 8.1 (Berkeley) 5/31/93
33 * $FreeBSD: src/games/rogue/save.c,v 1.6 1999/11/30 03:49:27 billf Exp $
39 * This source herein may be modified and/or distributed by anybody who
40 * so desires, with the following restrictions:
41 * 1.) No portion of this notice shall be removed.
42 * 2.) Credit shall not be taken for the creation of this source.
43 * 3.) This code is not to be traded, sold, or used for personal
52 static boolean
has_been_touched(const struct rogue_time
*,
53 const struct rogue_time
*);
54 static void del_save_file(void);
55 static void r_read(FILE *, char *, int);
56 static void r_write(FILE *, const char *, int);
57 static void read_pack(object
*, FILE *, boolean
);
58 static void read_string(char *, FILE *, size_t);
59 static void rw_dungeon(FILE *, boolean
);
60 static void rw_id(struct id
*, FILE *, int, boolean
);
61 static void rw_rooms(FILE *, boolean
);
62 static void write_pack(const object
*, FILE *);
63 static void write_string(char *, FILE *);
65 static short write_failed
= 0;
66 static char save_name
[80];
68 char *save_file
= NULL
;
75 if (!get_input_line("file name?", save_file
, fname
,
76 "game not saved", 0, 1)) {
81 save_into_file(fname
);
85 save_into_file(const char *sfile
)
92 struct rogue_time rt_buf
;
94 if (sfile
[0] == '~') {
95 if ((hptr
= md_getenv("HOME")) != NULL
) {
96 len
= strlen(hptr
) + strlen(sfile
);
97 name_buffer
= md_malloc(len
);
98 if (name_buffer
== NULL
) {
99 message("out of memory for save file name", 0);
102 strcpy(name_buffer
, hptr
);
103 strcat(name_buffer
, sfile
+1);
111 if ( ((fp
= fopen(sfile
, "w")) == NULL
) ||
112 ((file_id
= md_get_file_id(sfile
)) == -1)) {
113 message("problem accessing the save file", 0);
119 r_write(fp
, (char *) &detect_monster
, sizeof(detect_monster
));
120 r_write(fp
, (char *) &cur_level
, sizeof(cur_level
));
121 r_write(fp
, (char *) &max_level
, sizeof(max_level
));
122 write_string(hunger_str
, fp
);
123 write_string(login_name
, fp
);
124 r_write(fp
, (char *) &party_room
, sizeof(party_room
));
125 write_pack(&level_monsters
, fp
);
126 write_pack(&level_objects
, fp
);
127 r_write(fp
, (char *) &file_id
, sizeof(file_id
));
129 r_write(fp
, (char *) &foods
, sizeof(foods
));
130 r_write(fp
, (char *) &rogue
, sizeof(fighter
));
131 write_pack(&rogue
.pack
, fp
);
132 rw_id(id_potions
, fp
, POTIONS
, 1);
133 rw_id(id_scrolls
, fp
, SCROLS
, 1);
134 rw_id(id_wands
, fp
, WANDS
, 1);
135 rw_id(id_rings
, fp
, RINGS
, 1);
136 r_write(fp
, (char *) traps
, (MAX_TRAPS
* sizeof(trap
)));
137 r_write(fp
, (char *) is_wood
, (WANDS
* sizeof(boolean
)));
138 r_write(fp
, (char *) &cur_room
, sizeof(cur_room
));
140 r_write(fp
, (char *) &being_held
, sizeof(being_held
));
141 r_write(fp
, (char *) &bear_trap
, sizeof(bear_trap
));
142 r_write(fp
, (char *) &halluc
, sizeof(halluc
));
143 r_write(fp
, (char *) &blind
, sizeof(blind
));
144 r_write(fp
, (char *) &confused
, sizeof(confused
));
145 r_write(fp
, (char *) &levitate
, sizeof(levitate
));
146 r_write(fp
, (char *) &haste_self
, sizeof(haste_self
));
147 r_write(fp
, (char *) &see_invisible
, sizeof(see_invisible
));
148 r_write(fp
, (char *) &detect_monster
, sizeof(detect_monster
));
149 r_write(fp
, (char *) &wizard
, sizeof(wizard
));
150 r_write(fp
, (char *) &score_only
, sizeof(score_only
));
151 r_write(fp
, (char *) &m_moves
, sizeof(m_moves
));
153 rt_buf
.second
+= 10; /* allow for some processing time */
154 r_write(fp
, (char *) &rt_buf
, sizeof(rt_buf
));
158 md_df(sfile
); /* delete file */
160 if (strcmp(sfile
, save_name
) == 0)
177 restore(const char *fname
)
180 struct rogue_time saved_time
, mod_time
;
183 int new_file_id
, saved_file_id
;
185 if ( ((new_file_id
= md_get_file_id(fname
)) == -1) ||
186 ((fp
= fopen(fname
, "r")) == NULL
)) {
187 clean_up("cannot open file");
189 if (md_link_count(fname
) > 1) {
190 clean_up("file has link");
193 r_read(fp
, (char *) &detect_monster
, sizeof(detect_monster
));
194 r_read(fp
, (char *) &cur_level
, sizeof(cur_level
));
195 r_read(fp
, (char *) &max_level
, sizeof(max_level
));
196 read_string(hunger_str
, fp
, sizeof hunger_str
);
198 strlcpy(tbuf
, login_name
, sizeof tbuf
);
199 read_string(login_name
, fp
, sizeof login_name
);
200 if (strcmp(tbuf
, login_name
)) {
201 clean_up("you're not the original player");
204 r_read(fp
, (char *) &party_room
, sizeof(party_room
));
205 read_pack(&level_monsters
, fp
, 0);
206 read_pack(&level_objects
, fp
, 0);
207 r_read(fp
, (char *) &saved_file_id
, sizeof(saved_file_id
));
208 if (new_file_id
!= saved_file_id
) {
209 clean_up("sorry, saved game is not in the same file");
212 r_read(fp
, (char *) &foods
, sizeof(foods
));
213 r_read(fp
, (char *) &rogue
, sizeof(fighter
));
214 read_pack(&rogue
.pack
, fp
, 1);
215 rw_id(id_potions
, fp
, POTIONS
, 0);
216 rw_id(id_scrolls
, fp
, SCROLS
, 0);
217 rw_id(id_wands
, fp
, WANDS
, 0);
218 rw_id(id_rings
, fp
, RINGS
, 0);
219 r_read(fp
, (char *) traps
, (MAX_TRAPS
* sizeof(trap
)));
220 r_read(fp
, (char *) is_wood
, (WANDS
* sizeof(boolean
)));
221 r_read(fp
, (char *) &cur_room
, sizeof(cur_room
));
223 r_read(fp
, (char *) &being_held
, sizeof(being_held
));
224 r_read(fp
, (char *) &bear_trap
, sizeof(bear_trap
));
225 r_read(fp
, (char *) &halluc
, sizeof(halluc
));
226 r_read(fp
, (char *) &blind
, sizeof(blind
));
227 r_read(fp
, (char *) &confused
, sizeof(confused
));
228 r_read(fp
, (char *) &levitate
, sizeof(levitate
));
229 r_read(fp
, (char *) &haste_self
, sizeof(haste_self
));
230 r_read(fp
, (char *) &see_invisible
, sizeof(see_invisible
));
231 r_read(fp
, (char *) &detect_monster
, sizeof(detect_monster
));
232 r_read(fp
, (char *) &wizard
, sizeof(wizard
));
233 r_read(fp
, (char *) &score_only
, sizeof(score_only
));
234 r_read(fp
, (char *) &m_moves
, sizeof(m_moves
));
235 r_read(fp
, (char *) &saved_time
, sizeof(saved_time
));
237 if (fread(buf
, sizeof(char), 1, fp
) > 0) {
239 clean_up("extra characters in file");
242 md_gfmt(fname
, &mod_time
); /* get file modification time */
244 if (has_been_touched(&saved_time
, &mod_time
)) {
246 clean_up("sorry, file has been touched");
249 strcpy(save_name
, fname
);
250 atexit(del_save_file
);
258 write_pack(const object
*pack
, FILE *fp
)
262 while ((pack
= pack
->next_object
) != NULL
) {
263 r_write(fp
, (const char *) pack
, sizeof(object
));
265 t
.ichar
= t
.what_is
= 0;
266 r_write(fp
, (const char *) &t
, sizeof(object
));
270 read_pack(object
*pack
, FILE *fp
, boolean is_rogue
)
272 object read_obj
, *new_obj
;
275 r_read(fp
, (char *) &read_obj
, sizeof(object
));
276 if (read_obj
.ichar
== 0) {
277 pack
->next_object
= NULL
;
280 new_obj
= alloc_object();
283 if (new_obj
->in_use_flags
& BEING_WORN
) {
285 } else if (new_obj
->in_use_flags
& BEING_WIELDED
) {
287 } else if (new_obj
->in_use_flags
& (ON_EITHER_HAND
)) {
289 ((new_obj
->in_use_flags
& ON_LEFT_HAND
) ? 1 : 0));
292 pack
->next_object
= new_obj
;
298 rw_dungeon(FILE *fp
, boolean rw
)
303 for (i
= 0; i
< DROWS
; i
++) {
305 r_write(fp
, (char *) dungeon
[i
], (DCOLS
* sizeof(dungeon
[0][0])));
306 for (j
= 0; j
< DCOLS
; j
++) {
307 buf
[j
] = mvinch(i
, j
);
309 r_write(fp
, buf
, DCOLS
);
311 r_read(fp
, (char *) dungeon
[i
], (DCOLS
* sizeof(dungeon
[0][0])));
312 r_read(fp
, buf
, DCOLS
);
313 for (j
= 0; j
< DCOLS
; j
++) {
314 mvaddch(i
, j
, buf
[j
]);
321 rw_id(struct id id_table
[], FILE *fp
, int n
, boolean wr
)
325 for (i
= 0; i
< n
; i
++) {
327 r_write(fp
, (const char *) &(id_table
[i
].value
), sizeof(short));
328 r_write(fp
, (const char *) &(id_table
[i
].id_status
),
329 sizeof(unsigned short));
330 write_string(id_table
[i
].title
, fp
);
332 r_read(fp
, (char *) &(id_table
[i
].value
), sizeof(short));
333 r_read(fp
, (char *) &(id_table
[i
].id_status
),
334 sizeof(unsigned short));
335 read_string(id_table
[i
].title
, fp
, MAX_ID_TITLE_LEN
);
341 write_string(char *s
, FILE *fp
)
347 r_write(fp
, (char *) &n
, sizeof(short));
352 read_string(char *s
, FILE *fp
, size_t len
)
356 r_read(fp
, (char *) &n
, sizeof(short));
357 if (n
< 0 || n
> (short)len
) {
358 clean_up("read_string: corrupt game file");
362 /* ensure null termination */
367 rw_rooms(FILE *fp
, boolean rw
)
371 for (i
= 0; i
< MAXROOMS
; i
++) {
372 rw
? r_write(fp
, (char *) (rooms
+ i
), sizeof(room
)) :
373 r_read(fp
, (char *) (rooms
+ i
), sizeof(room
));
378 r_read(FILE *fp
, char *buf
, int n
)
380 if (fread(buf
, sizeof(char), n
, fp
) != (unsigned)n
) {
381 clean_up("read() failed, don't know why");
386 r_write(FILE *fp
, const char *buf
, int n
)
389 if (fwrite(buf
, sizeof(char), n
, fp
) != (unsigned)n
) {
390 message("write() failed, don't know why", 0);
398 has_been_touched(const struct rogue_time
*saved_time
,
399 const struct rogue_time
*mod_time
)
401 if (saved_time
->year
< mod_time
->year
) {
403 } else if (saved_time
->year
> mod_time
->year
) {
406 if (saved_time
->month
< mod_time
->month
) {
408 } else if (saved_time
->month
> mod_time
->month
) {
411 if (saved_time
->day
< mod_time
->day
) {
413 } else if (saved_time
->day
> mod_time
->day
) {
416 if (saved_time
->hour
< mod_time
->hour
) {
418 } else if (saved_time
->hour
> mod_time
->hour
) {
421 if (saved_time
->minute
< mod_time
->minute
) {
423 } else if (saved_time
->minute
> mod_time
->minute
) {
426 if (saved_time
->second
< mod_time
->second
) {