]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - rogue/save.c
1 /* $NetBSD: save.c,v 1.9 2003/08/07 09:37:40 agc 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.9 2003/08/07 09:37:40 agc 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)) {
103 message("problem accessing the save file", 0);
109 r_write(fp
, (char *) &detect_monster
, sizeof(detect_monster
));
110 r_write(fp
, (char *) &cur_level
, sizeof(cur_level
));
111 r_write(fp
, (char *) &max_level
, sizeof(max_level
));
112 write_string(hunger_str
, fp
);
113 write_string(login_name
, fp
);
114 r_write(fp
, (char *) &party_room
, sizeof(party_room
));
115 write_pack(&level_monsters
, fp
);
116 write_pack(&level_objects
, fp
);
117 r_write(fp
, (char *) &file_id
, sizeof(file_id
));
119 r_write(fp
, (char *) &foods
, sizeof(foods
));
120 r_write(fp
, (char *) &rogue
, sizeof(fighter
));
121 write_pack(&rogue
.pack
, fp
);
122 rw_id(id_potions
, fp
, POTIONS
, 1);
123 rw_id(id_scrolls
, fp
, SCROLS
, 1);
124 rw_id(id_wands
, fp
, WANDS
, 1);
125 rw_id(id_rings
, fp
, RINGS
, 1);
126 r_write(fp
, (char *) traps
, (MAX_TRAPS
* sizeof(trap
)));
127 r_write(fp
, (char *) is_wood
, (WANDS
* sizeof(boolean
)));
128 r_write(fp
, (char *) &cur_room
, sizeof(cur_room
));
130 r_write(fp
, (char *) &being_held
, sizeof(being_held
));
131 r_write(fp
, (char *) &bear_trap
, sizeof(bear_trap
));
132 r_write(fp
, (char *) &halluc
, sizeof(halluc
));
133 r_write(fp
, (char *) &blind
, sizeof(blind
));
134 r_write(fp
, (char *) &confused
, sizeof(confused
));
135 r_write(fp
, (char *) &levitate
, sizeof(levitate
));
136 r_write(fp
, (char *) &haste_self
, sizeof(haste_self
));
137 r_write(fp
, (char *) &see_invisible
, sizeof(see_invisible
));
138 r_write(fp
, (char *) &detect_monster
, sizeof(detect_monster
));
139 r_write(fp
, (char *) &wizard
, sizeof(wizard
));
140 r_write(fp
, (char *) &score_only
, sizeof(score_only
));
141 r_write(fp
, (char *) &m_moves
, sizeof(m_moves
));
143 rt_buf
.second
+= 10; /* allow for some processing time */
144 r_write(fp
, (char *) &rt_buf
, sizeof(rt_buf
));
148 (void) md_df(sfile
); /* delete file */
159 struct rogue_time saved_time
, mod_time
;
162 int new_file_id
, saved_file_id
;
165 if (((new_file_id
= md_get_file_id(fname
)) == -1) ||
166 ((fp
= fopen(fname
, "r")) == NULL
)) {
167 clean_up("cannot open file");
169 if (md_link_count(fname
) > 1) {
170 clean_up("file has link");
173 r_read(fp
, (char *) &detect_monster
, sizeof(detect_monster
));
174 r_read(fp
, (char *) &cur_level
, sizeof(cur_level
));
175 r_read(fp
, (char *) &max_level
, sizeof(max_level
));
176 read_string(hunger_str
, fp
, sizeof hunger_str
);
178 (void) strlcpy(tbuf
, login_name
, sizeof tbuf
);
179 read_string(login_name
, fp
, sizeof login_name
);
180 if (strcmp(tbuf
, login_name
)) {
181 clean_up("you're not the original player");
184 r_read(fp
, (char *) &party_room
, sizeof(party_room
));
185 read_pack(&level_monsters
, fp
, 0);
186 read_pack(&level_objects
, fp
, 0);
187 r_read(fp
, (char *) &saved_file_id
, sizeof(saved_file_id
));
188 if (new_file_id
!= saved_file_id
) {
189 clean_up("sorry, saved game is not in the same file");
192 r_read(fp
, (char *) &foods
, sizeof(foods
));
193 r_read(fp
, (char *) &rogue
, sizeof(fighter
));
194 read_pack(&rogue
.pack
, fp
, 1);
195 rw_id(id_potions
, fp
, POTIONS
, 0);
196 rw_id(id_scrolls
, fp
, SCROLS
, 0);
197 rw_id(id_wands
, fp
, WANDS
, 0);
198 rw_id(id_rings
, fp
, RINGS
, 0);
199 r_read(fp
, (char *) traps
, (MAX_TRAPS
* sizeof(trap
)));
200 r_read(fp
, (char *) is_wood
, (WANDS
* sizeof(boolean
)));
201 r_read(fp
, (char *) &cur_room
, sizeof(cur_room
));
203 r_read(fp
, (char *) &being_held
, sizeof(being_held
));
204 r_read(fp
, (char *) &bear_trap
, sizeof(bear_trap
));
205 r_read(fp
, (char *) &halluc
, sizeof(halluc
));
206 r_read(fp
, (char *) &blind
, sizeof(blind
));
207 r_read(fp
, (char *) &confused
, sizeof(confused
));
208 r_read(fp
, (char *) &levitate
, sizeof(levitate
));
209 r_read(fp
, (char *) &haste_self
, sizeof(haste_self
));
210 r_read(fp
, (char *) &see_invisible
, sizeof(see_invisible
));
211 r_read(fp
, (char *) &detect_monster
, sizeof(detect_monster
));
212 r_read(fp
, (char *) &wizard
, sizeof(wizard
));
213 r_read(fp
, (char *) &score_only
, sizeof(score_only
));
214 r_read(fp
, (char *) &m_moves
, sizeof(m_moves
));
215 r_read(fp
, (char *) &saved_time
, sizeof(saved_time
));
217 if (fread(buf
, sizeof(char), 1, fp
) > 0) {
219 clean_up("extra characters in file");
222 md_gfmt(fname
, &mod_time
); /* get file modification time */
224 if (has_been_touched(&saved_time
, &mod_time
)) {
226 clean_up("sorry, file has been touched");
228 if ((!wizard
) && !md_df(fname
)) {
229 clean_up("cannot delete file");
243 while ((pack
= pack
->next_object
) != NULL
) {
244 r_write(fp
, (const char *) pack
, sizeof(object
));
246 t
.ichar
= t
.what_is
= 0;
247 r_write(fp
, (const char *) &t
, sizeof(object
));
251 read_pack(pack
, fp
, is_rogue
)
256 object read_obj
, *new_obj
;
259 r_read(fp
, (char *) &read_obj
, sizeof(object
));
260 if (read_obj
.ichar
== 0) {
261 pack
->next_object
= (object
*) 0;
264 new_obj
= alloc_object();
267 if (new_obj
->in_use_flags
& BEING_WORN
) {
269 } else if (new_obj
->in_use_flags
& BEING_WIELDED
) {
271 } else if (new_obj
->in_use_flags
& (ON_EITHER_HAND
)) {
273 ((new_obj
->in_use_flags
& ON_LEFT_HAND
) ? 1 : 0));
276 pack
->next_object
= new_obj
;
289 for (i
= 0; i
< DROWS
; i
++) {
291 r_write(fp
, (char *) dungeon
[i
], (DCOLS
* sizeof(dungeon
[0][0])));
292 for (j
= 0; j
< DCOLS
; j
++) {
293 buf
[j
] = mvinch(i
, j
);
295 r_write(fp
, buf
, DCOLS
);
297 r_read(fp
, (char *) dungeon
[i
], (DCOLS
* sizeof(dungeon
[0][0])));
298 r_read(fp
, buf
, DCOLS
);
299 for (j
= 0; j
< DCOLS
; j
++) {
300 mvaddch(i
, j
, buf
[j
]);
307 rw_id(id_table
, fp
, n
, wr
)
308 struct id id_table
[];
315 for (i
= 0; i
< n
; i
++) {
317 r_write(fp
, (const char *) &(id_table
[i
].value
), sizeof(short));
318 r_write(fp
, (const char *) &(id_table
[i
].id_status
),
319 sizeof(unsigned short));
320 write_string(id_table
[i
].title
, fp
);
322 r_read(fp
, (char *) &(id_table
[i
].value
), sizeof(short));
323 r_read(fp
, (char *) &(id_table
[i
].id_status
),
324 sizeof(unsigned short));
325 read_string(id_table
[i
].title
, fp
, MAX_ID_TITLE_LEN
);
339 r_write(fp
, (char *) &n
, sizeof(short));
344 read_string(s
, fp
, len
)
351 r_read(fp
, (char *) &n
, sizeof(short));
353 clean_up("read_string: corrupt game file");
365 for (i
= 0; i
< MAXROOMS
; i
++) {
366 rw
? r_write(fp
, (char *) (rooms
+ i
), sizeof(room
)) :
367 r_read(fp
, (char *) (rooms
+ i
), sizeof(room
));
377 if (fread(buf
, sizeof(char), n
, fp
) != (size_t)n
) {
378 clean_up("read() failed, don't know why");
389 if (fwrite(buf
, sizeof(char), n
, fp
) != (size_t)n
) {
390 message("write() failed, don't know why", 0);
398 has_been_touched(saved_time
, mod_time
)
399 const struct rogue_time
*saved_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
) {