]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - rogue/save.c
1 /* $NetBSD: save.c,v 1.10 2006/03/17 23:04:01 abs 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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
38 static char sccsid
[] = "@(#)save.c 8.1 (Berkeley) 5/31/93";
40 __RCSID("$NetBSD: save.c,v 1.10 2006/03/17 23:04:01 abs Exp $");
47 * This source herein may be modified and/or distributed by anybody who
48 * so desires, with the following restrictions:
49 * 1.) No portion of this notice shall be removed.
50 * 2.) Credit shall not be taken for the creation of this source.
51 * 3.) This code is not to be traded, sold, or used for personal
59 short write_failed
= 0;
60 char *save_file
= (char *) 0;
67 if (!get_input_line("file name?", save_file
, fname
, "game not saved",
73 save_into_file(fname
);
85 struct rogue_time rt_buf
;
87 if (sfile
[0] == '~') {
88 if ((hptr
= md_getenv("HOME")) != NULL
) {
89 len
= strlen(hptr
) + strlen(sfile
);
90 name_buffer
= md_malloc(len
);
91 if (name_buffer
== NULL
) {
92 message("out of memory for save file name", 0);
95 (void) strcpy(name_buffer
, hptr
);
96 (void) strcat(name_buffer
, sfile
+1);
101 if (((fp
= fopen(sfile
, "w")) == NULL
) ||
102 ((file_id
= md_get_file_id(sfile
)) == -1)) {
105 message("problem accessing the save file", 0);
111 r_write(fp
, (char *) &detect_monster
, sizeof(detect_monster
));
112 r_write(fp
, (char *) &cur_level
, sizeof(cur_level
));
113 r_write(fp
, (char *) &max_level
, sizeof(max_level
));
114 write_string(hunger_str
, fp
);
115 write_string(login_name
, fp
);
116 r_write(fp
, (char *) &party_room
, sizeof(party_room
));
117 write_pack(&level_monsters
, fp
);
118 write_pack(&level_objects
, fp
);
119 r_write(fp
, (char *) &file_id
, sizeof(file_id
));
121 r_write(fp
, (char *) &foods
, sizeof(foods
));
122 r_write(fp
, (char *) &rogue
, sizeof(fighter
));
123 write_pack(&rogue
.pack
, fp
);
124 rw_id(id_potions
, fp
, POTIONS
, 1);
125 rw_id(id_scrolls
, fp
, SCROLS
, 1);
126 rw_id(id_wands
, fp
, WANDS
, 1);
127 rw_id(id_rings
, fp
, RINGS
, 1);
128 r_write(fp
, (char *) traps
, (MAX_TRAPS
* sizeof(trap
)));
129 r_write(fp
, (char *) is_wood
, (WANDS
* sizeof(boolean
)));
130 r_write(fp
, (char *) &cur_room
, sizeof(cur_room
));
132 r_write(fp
, (char *) &being_held
, sizeof(being_held
));
133 r_write(fp
, (char *) &bear_trap
, sizeof(bear_trap
));
134 r_write(fp
, (char *) &halluc
, sizeof(halluc
));
135 r_write(fp
, (char *) &blind
, sizeof(blind
));
136 r_write(fp
, (char *) &confused
, sizeof(confused
));
137 r_write(fp
, (char *) &levitate
, sizeof(levitate
));
138 r_write(fp
, (char *) &haste_self
, sizeof(haste_self
));
139 r_write(fp
, (char *) &see_invisible
, sizeof(see_invisible
));
140 r_write(fp
, (char *) &detect_monster
, sizeof(detect_monster
));
141 r_write(fp
, (char *) &wizard
, sizeof(wizard
));
142 r_write(fp
, (char *) &score_only
, sizeof(score_only
));
143 r_write(fp
, (char *) &m_moves
, sizeof(m_moves
));
145 rt_buf
.second
+= 10; /* allow for some processing time */
146 r_write(fp
, (char *) &rt_buf
, sizeof(rt_buf
));
150 (void) md_df(sfile
); /* delete file */
161 struct rogue_time saved_time
, mod_time
;
164 int new_file_id
, saved_file_id
;
167 if (((new_file_id
= md_get_file_id(fname
)) == -1) ||
168 ((fp
= fopen(fname
, "r")) == NULL
)) {
169 clean_up("cannot open file");
171 if (md_link_count(fname
) > 1) {
172 clean_up("file has link");
175 r_read(fp
, (char *) &detect_monster
, sizeof(detect_monster
));
176 r_read(fp
, (char *) &cur_level
, sizeof(cur_level
));
177 r_read(fp
, (char *) &max_level
, sizeof(max_level
));
178 read_string(hunger_str
, fp
, sizeof hunger_str
);
180 (void) strlcpy(tbuf
, login_name
, sizeof tbuf
);
181 read_string(login_name
, fp
, sizeof login_name
);
182 if (strcmp(tbuf
, login_name
)) {
183 clean_up("you're not the original player");
186 r_read(fp
, (char *) &party_room
, sizeof(party_room
));
187 read_pack(&level_monsters
, fp
, 0);
188 read_pack(&level_objects
, fp
, 0);
189 r_read(fp
, (char *) &saved_file_id
, sizeof(saved_file_id
));
190 if (new_file_id
!= saved_file_id
) {
191 clean_up("sorry, saved game is not in the same file");
194 r_read(fp
, (char *) &foods
, sizeof(foods
));
195 r_read(fp
, (char *) &rogue
, sizeof(fighter
));
196 read_pack(&rogue
.pack
, fp
, 1);
197 rw_id(id_potions
, fp
, POTIONS
, 0);
198 rw_id(id_scrolls
, fp
, SCROLS
, 0);
199 rw_id(id_wands
, fp
, WANDS
, 0);
200 rw_id(id_rings
, fp
, RINGS
, 0);
201 r_read(fp
, (char *) traps
, (MAX_TRAPS
* sizeof(trap
)));
202 r_read(fp
, (char *) is_wood
, (WANDS
* sizeof(boolean
)));
203 r_read(fp
, (char *) &cur_room
, sizeof(cur_room
));
205 r_read(fp
, (char *) &being_held
, sizeof(being_held
));
206 r_read(fp
, (char *) &bear_trap
, sizeof(bear_trap
));
207 r_read(fp
, (char *) &halluc
, sizeof(halluc
));
208 r_read(fp
, (char *) &blind
, sizeof(blind
));
209 r_read(fp
, (char *) &confused
, sizeof(confused
));
210 r_read(fp
, (char *) &levitate
, sizeof(levitate
));
211 r_read(fp
, (char *) &haste_self
, sizeof(haste_self
));
212 r_read(fp
, (char *) &see_invisible
, sizeof(see_invisible
));
213 r_read(fp
, (char *) &detect_monster
, sizeof(detect_monster
));
214 r_read(fp
, (char *) &wizard
, sizeof(wizard
));
215 r_read(fp
, (char *) &score_only
, sizeof(score_only
));
216 r_read(fp
, (char *) &m_moves
, sizeof(m_moves
));
217 r_read(fp
, (char *) &saved_time
, sizeof(saved_time
));
219 if (fread(buf
, sizeof(char), 1, fp
) > 0) {
221 clean_up("extra characters in file");
224 md_gfmt(fname
, &mod_time
); /* get file modification time */
226 if (has_been_touched(&saved_time
, &mod_time
)) {
228 clean_up("sorry, file has been touched");
230 if ((!wizard
) && !md_df(fname
)) {
231 clean_up("cannot delete file");
245 while ((pack
= pack
->next_object
) != NULL
) {
246 r_write(fp
, (const char *) pack
, sizeof(object
));
248 t
.ichar
= t
.what_is
= 0;
249 r_write(fp
, (const char *) &t
, sizeof(object
));
253 read_pack(pack
, fp
, is_rogue
)
258 object read_obj
, *new_obj
;
261 r_read(fp
, (char *) &read_obj
, sizeof(object
));
262 if (read_obj
.ichar
== 0) {
263 pack
->next_object
= (object
*) 0;
266 new_obj
= alloc_object();
269 if (new_obj
->in_use_flags
& BEING_WORN
) {
271 } else if (new_obj
->in_use_flags
& BEING_WIELDED
) {
273 } else if (new_obj
->in_use_flags
& (ON_EITHER_HAND
)) {
275 ((new_obj
->in_use_flags
& ON_LEFT_HAND
) ? 1 : 0));
278 pack
->next_object
= new_obj
;
291 for (i
= 0; i
< DROWS
; i
++) {
293 r_write(fp
, (char *) dungeon
[i
], (DCOLS
* sizeof(dungeon
[0][0])));
294 for (j
= 0; j
< DCOLS
; j
++) {
295 buf
[j
] = mvinch(i
, j
);
297 r_write(fp
, buf
, DCOLS
);
299 r_read(fp
, (char *) dungeon
[i
], (DCOLS
* sizeof(dungeon
[0][0])));
300 r_read(fp
, buf
, DCOLS
);
301 for (j
= 0; j
< DCOLS
; j
++) {
302 mvaddch(i
, j
, buf
[j
]);
309 rw_id(id_table
, fp
, n
, wr
)
310 struct id id_table
[];
317 for (i
= 0; i
< n
; i
++) {
319 r_write(fp
, (const char *) &(id_table
[i
].value
), sizeof(short));
320 r_write(fp
, (const char *) &(id_table
[i
].id_status
),
321 sizeof(unsigned short));
322 write_string(id_table
[i
].title
, fp
);
324 r_read(fp
, (char *) &(id_table
[i
].value
), sizeof(short));
325 r_read(fp
, (char *) &(id_table
[i
].id_status
),
326 sizeof(unsigned short));
327 read_string(id_table
[i
].title
, fp
, MAX_ID_TITLE_LEN
);
341 r_write(fp
, (char *) &n
, sizeof(short));
346 read_string(s
, fp
, len
)
353 r_read(fp
, (char *) &n
, sizeof(short));
355 clean_up("read_string: corrupt game file");
367 for (i
= 0; i
< MAXROOMS
; i
++) {
368 rw
? r_write(fp
, (char *) (rooms
+ i
), sizeof(room
)) :
369 r_read(fp
, (char *) (rooms
+ i
), sizeof(room
));
379 if (fread(buf
, sizeof(char), n
, fp
) != (size_t)n
) {
380 clean_up("read() failed, don't know why");
391 if (fwrite(buf
, sizeof(char), n
, fp
) != (size_t)n
) {
392 message("write() failed, don't know why", 0);
400 has_been_touched(saved_time
, mod_time
)
401 const struct rogue_time
*saved_time
, *mod_time
;
403 if (saved_time
->year
< mod_time
->year
) {
405 } else if (saved_time
->year
> mod_time
->year
) {
408 if (saved_time
->month
< mod_time
->month
) {
410 } else if (saved_time
->month
> mod_time
->month
) {
413 if (saved_time
->day
< mod_time
->day
) {
415 } else if (saved_time
->day
> mod_time
->day
) {
418 if (saved_time
->hour
< mod_time
->hour
) {
420 } else if (saved_time
->hour
> mod_time
->hour
) {
423 if (saved_time
->minute
< mod_time
->minute
) {
425 } else if (saved_time
->minute
> mod_time
->minute
) {
428 if (saved_time
->second
< mod_time
->second
) {