]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - rogue/save.c
1 /* $NetBSD: save.c,v 1.6 1999/09/13 17:19:55 jsm Exp $ */
4 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <sys/cdefs.h>
42 static char sccsid
[] = "@(#)save.c 8.1 (Berkeley) 5/31/93";
44 __RCSID("$NetBSD: save.c,v 1.6 1999/09/13 17:19:55 jsm Exp $");
51 * This source herein may be modified and/or distributed by anybody who
52 * so desires, with the following restrictions:
53 * 1.) No portion of this notice shall be removed.
54 * 2.) Credit shall not be taken for the creation of this source.
55 * 3.) This code is not to be traded, sold, or used for personal
63 short write_failed
= 0;
64 char *save_file
= (char *) 0;
71 if (!get_input_line("file name?", save_file
, fname
, "game not saved",
77 save_into_file(fname
);
89 struct rogue_time rt_buf
;
91 if (sfile
[0] == '~') {
92 if ((hptr
= md_getenv("HOME")) != NULL
) {
93 len
= strlen(hptr
) + strlen(sfile
);
94 name_buffer
= md_malloc(len
);
95 if (name_buffer
== NULL
) {
96 message("out of memory for save file name", 0);
99 (void) strcpy(name_buffer
, hptr
);
100 (void) strcat(name_buffer
, sfile
+1);
105 if ( ((fp
= fopen(sfile
, "w")) == NULL
) ||
106 ((file_id
= md_get_file_id(sfile
)) == -1)) {
107 message("problem accessing the save file", 0);
113 r_write(fp
, (char *) &detect_monster
, sizeof(detect_monster
));
114 r_write(fp
, (char *) &cur_level
, sizeof(cur_level
));
115 r_write(fp
, (char *) &max_level
, sizeof(max_level
));
116 write_string(hunger_str
, fp
);
117 write_string(login_name
, fp
);
118 r_write(fp
, (char *) &party_room
, sizeof(party_room
));
119 write_pack(&level_monsters
, fp
);
120 write_pack(&level_objects
, fp
);
121 r_write(fp
, (char *) &file_id
, sizeof(file_id
));
123 r_write(fp
, (char *) &foods
, sizeof(foods
));
124 r_write(fp
, (char *) &rogue
, sizeof(fighter
));
125 write_pack(&rogue
.pack
, fp
);
126 rw_id(id_potions
, fp
, POTIONS
, 1);
127 rw_id(id_scrolls
, fp
, SCROLS
, 1);
128 rw_id(id_wands
, fp
, WANDS
, 1);
129 rw_id(id_rings
, fp
, RINGS
, 1);
130 r_write(fp
, (char *) traps
, (MAX_TRAPS
* sizeof(trap
)));
131 r_write(fp
, (char *) is_wood
, (WANDS
* sizeof(boolean
)));
132 r_write(fp
, (char *) &cur_room
, sizeof(cur_room
));
134 r_write(fp
, (char *) &being_held
, sizeof(being_held
));
135 r_write(fp
, (char *) &bear_trap
, sizeof(bear_trap
));
136 r_write(fp
, (char *) &halluc
, sizeof(halluc
));
137 r_write(fp
, (char *) &blind
, sizeof(blind
));
138 r_write(fp
, (char *) &confused
, sizeof(confused
));
139 r_write(fp
, (char *) &levitate
, sizeof(levitate
));
140 r_write(fp
, (char *) &haste_self
, sizeof(haste_self
));
141 r_write(fp
, (char *) &see_invisible
, sizeof(see_invisible
));
142 r_write(fp
, (char *) &detect_monster
, sizeof(detect_monster
));
143 r_write(fp
, (char *) &wizard
, sizeof(wizard
));
144 r_write(fp
, (char *) &score_only
, sizeof(score_only
));
145 r_write(fp
, (char *) &m_moves
, sizeof(m_moves
));
147 rt_buf
.second
+= 10; /* allow for some processing time */
148 r_write(fp
, (char *) &rt_buf
, sizeof(rt_buf
));
152 (void) md_df(sfile
); /* delete file */
163 struct rogue_time saved_time
, mod_time
;
166 int new_file_id
, saved_file_id
;
169 if ( ((new_file_id
= md_get_file_id(fname
)) == -1) ||
170 ((fp
= fopen(fname
, "r")) == NULL
)) {
171 clean_up("cannot open file");
173 if (md_link_count(fname
) > 1) {
174 clean_up("file has link");
177 r_read(fp
, (char *) &detect_monster
, sizeof(detect_monster
));
178 r_read(fp
, (char *) &cur_level
, sizeof(cur_level
));
179 r_read(fp
, (char *) &max_level
, sizeof(max_level
));
180 read_string(hunger_str
, fp
);
182 (void) strcpy(tbuf
, login_name
);
183 read_string(login_name
, fp
);
184 if (strcmp(tbuf
, login_name
)) {
185 clean_up("you're not the original player");
188 r_read(fp
, (char *) &party_room
, sizeof(party_room
));
189 read_pack(&level_monsters
, fp
, 0);
190 read_pack(&level_objects
, fp
, 0);
191 r_read(fp
, (char *) &saved_file_id
, sizeof(saved_file_id
));
192 if (new_file_id
!= saved_file_id
) {
193 clean_up("sorry, saved game is not in the same file");
196 r_read(fp
, (char *) &foods
, sizeof(foods
));
197 r_read(fp
, (char *) &rogue
, sizeof(fighter
));
198 read_pack(&rogue
.pack
, fp
, 1);
199 rw_id(id_potions
, fp
, POTIONS
, 0);
200 rw_id(id_scrolls
, fp
, SCROLS
, 0);
201 rw_id(id_wands
, fp
, WANDS
, 0);
202 rw_id(id_rings
, fp
, RINGS
, 0);
203 r_read(fp
, (char *) traps
, (MAX_TRAPS
* sizeof(trap
)));
204 r_read(fp
, (char *) is_wood
, (WANDS
* sizeof(boolean
)));
205 r_read(fp
, (char *) &cur_room
, sizeof(cur_room
));
207 r_read(fp
, (char *) &being_held
, sizeof(being_held
));
208 r_read(fp
, (char *) &bear_trap
, sizeof(bear_trap
));
209 r_read(fp
, (char *) &halluc
, sizeof(halluc
));
210 r_read(fp
, (char *) &blind
, sizeof(blind
));
211 r_read(fp
, (char *) &confused
, sizeof(confused
));
212 r_read(fp
, (char *) &levitate
, sizeof(levitate
));
213 r_read(fp
, (char *) &haste_self
, sizeof(haste_self
));
214 r_read(fp
, (char *) &see_invisible
, sizeof(see_invisible
));
215 r_read(fp
, (char *) &detect_monster
, sizeof(detect_monster
));
216 r_read(fp
, (char *) &wizard
, sizeof(wizard
));
217 r_read(fp
, (char *) &score_only
, sizeof(score_only
));
218 r_read(fp
, (char *) &m_moves
, sizeof(m_moves
));
219 r_read(fp
, (char *) &saved_time
, sizeof(saved_time
));
221 if (fread(buf
, sizeof(char), 1, fp
) > 0) {
223 clean_up("extra characters in file");
226 md_gfmt(fname
, &mod_time
); /* get file modification time */
228 if (has_been_touched(&saved_time
, &mod_time
)) {
230 clean_up("sorry, file has been touched");
232 if ((!wizard
) && !md_df(fname
)) {
233 clean_up("cannot delete file");
247 while ((pack
= pack
->next_object
) != NULL
) {
248 r_write(fp
, (const char *) pack
, sizeof(object
));
250 t
.ichar
= t
.what_is
= 0;
251 r_write(fp
, (const char *) &t
, sizeof(object
));
255 read_pack(pack
, fp
, is_rogue
)
260 object read_obj
, *new_obj
;
263 r_read(fp
, (char *) &read_obj
, sizeof(object
));
264 if (read_obj
.ichar
== 0) {
265 pack
->next_object
= (object
*) 0;
268 new_obj
= alloc_object();
271 if (new_obj
->in_use_flags
& BEING_WORN
) {
273 } else if (new_obj
->in_use_flags
& BEING_WIELDED
) {
275 } else if (new_obj
->in_use_flags
& (ON_EITHER_HAND
)) {
277 ((new_obj
->in_use_flags
& ON_LEFT_HAND
) ? 1 : 0));
280 pack
->next_object
= new_obj
;
293 for (i
= 0; i
< DROWS
; i
++) {
295 r_write(fp
, (char *) dungeon
[i
], (DCOLS
* sizeof(dungeon
[0][0])));
296 for (j
= 0; j
< DCOLS
; j
++) {
297 buf
[j
] = mvinch(i
, j
);
299 r_write(fp
, buf
, DCOLS
);
301 r_read(fp
, (char *) dungeon
[i
], (DCOLS
* sizeof(dungeon
[0][0])));
302 r_read(fp
, buf
, DCOLS
);
303 for (j
= 0; j
< DCOLS
; j
++) {
304 mvaddch(i
, j
, buf
[j
]);
311 rw_id(id_table
, fp
, n
, wr
)
312 struct id id_table
[];
319 for (i
= 0; i
< n
; i
++) {
321 r_write(fp
, (const char *) &(id_table
[i
].value
), sizeof(short));
322 r_write(fp
, (const char *) &(id_table
[i
].id_status
),
323 sizeof(unsigned short));
324 write_string(id_table
[i
].title
, fp
);
326 r_read(fp
, (char *) &(id_table
[i
].value
), sizeof(short));
327 r_read(fp
, (char *) &(id_table
[i
].id_status
),
328 sizeof(unsigned short));
329 read_string(id_table
[i
].title
, fp
);
343 r_write(fp
, (char *) &n
, sizeof(short));
354 r_read(fp
, (char *) &n
, sizeof(short));
366 for (i
= 0; i
< MAXROOMS
; i
++) {
367 rw
? r_write(fp
, (char *) (rooms
+ i
), sizeof(room
)) :
368 r_read(fp
, (char *) (rooms
+ i
), sizeof(room
));
378 if (fread(buf
, sizeof(char), n
, fp
) != n
) {
379 clean_up("read() failed, don't know why");
390 if (fwrite(buf
, sizeof(char), n
, fp
) != n
) {
391 message("write() failed, don't know why", 0);
399 has_been_touched(saved_time
, mod_time
)
400 const struct rogue_time
*saved_time
, *mod_time
;
402 if (saved_time
->year
< mod_time
->year
) {
404 } else if (saved_time
->year
> mod_time
->year
) {
407 if (saved_time
->month
< mod_time
->month
) {
409 } else if (saved_time
->month
> mod_time
->month
) {
412 if (saved_time
->day
< mod_time
->day
) {
414 } else if (saved_time
->day
> mod_time
->day
) {
417 if (saved_time
->hour
< mod_time
->hour
) {
419 } else if (saved_time
->hour
> mod_time
->hour
) {
422 if (saved_time
->minute
< mod_time
->minute
) {
424 } else if (saved_time
->minute
> mod_time
->minute
) {
427 if (saved_time
->second
< mod_time
->second
) {