]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - rogue/save.c
1 /* $NetBSD: save.c,v 1.5 1998/11/10 13:01:32 hubertf 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.5 1998/11/10 13:01:32 hubertf 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
);
88 struct rogue_time rt_buf
;
90 if (sfile
[0] == '~') {
91 if ((hptr
= md_getenv("HOME")) != NULL
) {
92 (void) strcpy(name_buffer
, hptr
);
93 (void) strcat(name_buffer
, sfile
+1);
97 if ( ((fp
= fopen(sfile
, "w")) == NULL
) ||
98 ((file_id
= md_get_file_id(sfile
)) == -1)) {
99 message("problem accessing the save file", 0);
105 r_write(fp
, (char *) &detect_monster
, sizeof(detect_monster
));
106 r_write(fp
, (char *) &cur_level
, sizeof(cur_level
));
107 r_write(fp
, (char *) &max_level
, sizeof(max_level
));
108 write_string(hunger_str
, fp
);
109 write_string(login_name
, fp
);
110 r_write(fp
, (char *) &party_room
, sizeof(party_room
));
111 write_pack(&level_monsters
, fp
);
112 write_pack(&level_objects
, fp
);
113 r_write(fp
, (char *) &file_id
, sizeof(file_id
));
115 r_write(fp
, (char *) &foods
, sizeof(foods
));
116 r_write(fp
, (char *) &rogue
, sizeof(fighter
));
117 write_pack(&rogue
.pack
, fp
);
118 rw_id(id_potions
, fp
, POTIONS
, 1);
119 rw_id(id_scrolls
, fp
, SCROLS
, 1);
120 rw_id(id_wands
, fp
, WANDS
, 1);
121 rw_id(id_rings
, fp
, RINGS
, 1);
122 r_write(fp
, (char *) traps
, (MAX_TRAPS
* sizeof(trap
)));
123 r_write(fp
, (char *) is_wood
, (WANDS
* sizeof(boolean
)));
124 r_write(fp
, (char *) &cur_room
, sizeof(cur_room
));
126 r_write(fp
, (char *) &being_held
, sizeof(being_held
));
127 r_write(fp
, (char *) &bear_trap
, sizeof(bear_trap
));
128 r_write(fp
, (char *) &halluc
, sizeof(halluc
));
129 r_write(fp
, (char *) &blind
, sizeof(blind
));
130 r_write(fp
, (char *) &confused
, sizeof(confused
));
131 r_write(fp
, (char *) &levitate
, sizeof(levitate
));
132 r_write(fp
, (char *) &haste_self
, sizeof(haste_self
));
133 r_write(fp
, (char *) &see_invisible
, sizeof(see_invisible
));
134 r_write(fp
, (char *) &detect_monster
, sizeof(detect_monster
));
135 r_write(fp
, (char *) &wizard
, sizeof(wizard
));
136 r_write(fp
, (char *) &score_only
, sizeof(score_only
));
137 r_write(fp
, (char *) &m_moves
, sizeof(m_moves
));
139 rt_buf
.second
+= 10; /* allow for some processing time */
140 r_write(fp
, (char *) &rt_buf
, sizeof(rt_buf
));
144 (void) md_df(sfile
); /* delete file */
155 struct rogue_time saved_time
, mod_time
;
158 int new_file_id
, saved_file_id
;
161 if ( ((new_file_id
= md_get_file_id(fname
)) == -1) ||
162 ((fp
= fopen(fname
, "r")) == NULL
)) {
163 clean_up("cannot open file");
165 if (md_link_count(fname
) > 1) {
166 clean_up("file has link");
169 r_read(fp
, (char *) &detect_monster
, sizeof(detect_monster
));
170 r_read(fp
, (char *) &cur_level
, sizeof(cur_level
));
171 r_read(fp
, (char *) &max_level
, sizeof(max_level
));
172 read_string(hunger_str
, fp
);
174 (void) strcpy(tbuf
, login_name
);
175 read_string(login_name
, fp
);
176 if (strcmp(tbuf
, login_name
)) {
177 clean_up("you're not the original player");
180 r_read(fp
, (char *) &party_room
, sizeof(party_room
));
181 read_pack(&level_monsters
, fp
, 0);
182 read_pack(&level_objects
, fp
, 0);
183 r_read(fp
, (char *) &saved_file_id
, sizeof(saved_file_id
));
184 if (new_file_id
!= saved_file_id
) {
185 clean_up("sorry, saved game is not in the same file");
188 r_read(fp
, (char *) &foods
, sizeof(foods
));
189 r_read(fp
, (char *) &rogue
, sizeof(fighter
));
190 read_pack(&rogue
.pack
, fp
, 1);
191 rw_id(id_potions
, fp
, POTIONS
, 0);
192 rw_id(id_scrolls
, fp
, SCROLS
, 0);
193 rw_id(id_wands
, fp
, WANDS
, 0);
194 rw_id(id_rings
, fp
, RINGS
, 0);
195 r_read(fp
, (char *) traps
, (MAX_TRAPS
* sizeof(trap
)));
196 r_read(fp
, (char *) is_wood
, (WANDS
* sizeof(boolean
)));
197 r_read(fp
, (char *) &cur_room
, sizeof(cur_room
));
199 r_read(fp
, (char *) &being_held
, sizeof(being_held
));
200 r_read(fp
, (char *) &bear_trap
, sizeof(bear_trap
));
201 r_read(fp
, (char *) &halluc
, sizeof(halluc
));
202 r_read(fp
, (char *) &blind
, sizeof(blind
));
203 r_read(fp
, (char *) &confused
, sizeof(confused
));
204 r_read(fp
, (char *) &levitate
, sizeof(levitate
));
205 r_read(fp
, (char *) &haste_self
, sizeof(haste_self
));
206 r_read(fp
, (char *) &see_invisible
, sizeof(see_invisible
));
207 r_read(fp
, (char *) &detect_monster
, sizeof(detect_monster
));
208 r_read(fp
, (char *) &wizard
, sizeof(wizard
));
209 r_read(fp
, (char *) &score_only
, sizeof(score_only
));
210 r_read(fp
, (char *) &m_moves
, sizeof(m_moves
));
211 r_read(fp
, (char *) &saved_time
, sizeof(saved_time
));
213 if (fread(buf
, sizeof(char), 1, fp
) > 0) {
215 clean_up("extra characters in file");
218 md_gfmt(fname
, &mod_time
); /* get file modification time */
220 if (has_been_touched(&saved_time
, &mod_time
)) {
222 clean_up("sorry, file has been touched");
224 if ((!wizard
) && !md_df(fname
)) {
225 clean_up("cannot delete file");
239 while ((pack
= pack
->next_object
) != NULL
) {
240 r_write(fp
, (const char *) pack
, sizeof(object
));
242 t
.ichar
= t
.what_is
= 0;
243 r_write(fp
, (const char *) &t
, sizeof(object
));
247 read_pack(pack
, fp
, is_rogue
)
252 object read_obj
, *new_obj
;
255 r_read(fp
, (char *) &read_obj
, sizeof(object
));
256 if (read_obj
.ichar
== 0) {
257 pack
->next_object
= (object
*) 0;
260 new_obj
= alloc_object();
263 if (new_obj
->in_use_flags
& BEING_WORN
) {
265 } else if (new_obj
->in_use_flags
& BEING_WIELDED
) {
267 } else if (new_obj
->in_use_flags
& (ON_EITHER_HAND
)) {
269 ((new_obj
->in_use_flags
& ON_LEFT_HAND
) ? 1 : 0));
272 pack
->next_object
= new_obj
;
285 for (i
= 0; i
< DROWS
; i
++) {
287 r_write(fp
, (char *) dungeon
[i
], (DCOLS
* sizeof(dungeon
[0][0])));
288 for (j
= 0; j
< DCOLS
; j
++) {
289 buf
[j
] = mvinch(i
, j
);
291 r_write(fp
, buf
, DCOLS
);
293 r_read(fp
, (char *) dungeon
[i
], (DCOLS
* sizeof(dungeon
[0][0])));
294 r_read(fp
, buf
, DCOLS
);
295 for (j
= 0; j
< DCOLS
; j
++) {
296 mvaddch(i
, j
, buf
[j
]);
303 rw_id(id_table
, fp
, n
, wr
)
304 struct id id_table
[];
311 for (i
= 0; i
< n
; i
++) {
313 r_write(fp
, (const char *) &(id_table
[i
].value
), sizeof(short));
314 r_write(fp
, (const char *) &(id_table
[i
].id_status
),
315 sizeof(unsigned short));
316 write_string(id_table
[i
].title
, fp
);
318 r_read(fp
, (char *) &(id_table
[i
].value
), sizeof(short));
319 r_read(fp
, (char *) &(id_table
[i
].id_status
),
320 sizeof(unsigned short));
321 read_string(id_table
[i
].title
, fp
);
335 r_write(fp
, (char *) &n
, sizeof(short));
346 r_read(fp
, (char *) &n
, sizeof(short));
358 for (i
= 0; i
< MAXROOMS
; i
++) {
359 rw
? r_write(fp
, (char *) (rooms
+ i
), sizeof(room
)) :
360 r_read(fp
, (char *) (rooms
+ i
), sizeof(room
));
370 if (fread(buf
, sizeof(char), n
, fp
) != n
) {
371 clean_up("read() failed, don't know why");
382 if (fwrite(buf
, sizeof(char), n
, fp
) != n
) {
383 message("write() failed, don't know why", 0);
391 has_been_touched(saved_time
, mod_time
)
392 const struct rogue_time
*saved_time
, *mod_time
;
394 if (saved_time
->year
< mod_time
->year
) {
396 } else if (saved_time
->year
> mod_time
->year
) {
399 if (saved_time
->month
< mod_time
->month
) {
401 } else if (saved_time
->month
> mod_time
->month
) {
404 if (saved_time
->day
< mod_time
->day
) {
406 } else if (saved_time
->day
> mod_time
->day
) {
409 if (saved_time
->hour
< mod_time
->hour
) {
411 } else if (saved_time
->hour
> mod_time
->hour
) {
414 if (saved_time
->minute
< mod_time
->minute
) {
416 } else if (saved_time
->minute
> mod_time
->minute
) {
419 if (saved_time
->second
< mod_time
->second
) {