]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - rogue/save.c
1 /* $NetBSD: save.c,v 1.3 1995/04/22 10:28:21 cgd 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
41 static char sccsid
[] = "@(#)save.c 8.1 (Berkeley) 5/31/93";
43 static char rcsid
[] = "$NetBSD: save.c,v 1.3 1995/04/22 10:28:21 cgd Exp $";
50 * This source herein may be modified and/or distributed by anybody who
51 * so desires, with the following restrictions:
52 * 1.) No portion of this notice shall be removed.
53 * 2.) Credit shall not be taken for the creation of this source.
54 * 3.) This code is not to be traded, sold, or used for personal
62 short write_failed
= 0;
63 char *save_file
= (char *) 0;
65 extern boolean detect_monster
;
66 extern short cur_level
, max_level
;
67 extern char hunger_str
[];
68 extern char login_name
[];
69 extern short party_room
;
71 extern boolean is_wood
[];
72 extern short cur_room
;
73 extern boolean being_held
;
74 extern short bear_trap
;
77 extern short confused
;
78 extern short levitate
;
79 extern short haste_self
;
80 extern boolean see_invisible
;
81 extern boolean detect_monster
;
82 extern boolean wizard
;
83 extern boolean score_only
;
86 extern boolean msg_cleared
;
92 if (!get_input_line("file name?", save_file
, fname
, "game not saved",
98 save_into_file(fname
);
101 save_into_file(sfile
)
106 char name_buffer
[80];
108 struct rogue_time rt_buf
;
110 if (sfile
[0] == '~') {
111 if (hptr
= md_getenv("HOME")) {
112 (void) strcpy(name_buffer
, hptr
);
113 (void) strcat(name_buffer
, sfile
+1);
117 if ( ((fp
= fopen(sfile
, "w")) == NULL
) ||
118 ((file_id
= md_get_file_id(sfile
)) == -1)) {
119 message("problem accessing the save file", 0);
125 r_write(fp
, (char *) &detect_monster
, sizeof(detect_monster
));
126 r_write(fp
, (char *) &cur_level
, sizeof(cur_level
));
127 r_write(fp
, (char *) &max_level
, sizeof(max_level
));
128 write_string(hunger_str
, fp
);
129 write_string(login_name
, fp
);
130 r_write(fp
, (char *) &party_room
, sizeof(party_room
));
131 write_pack(&level_monsters
, fp
);
132 write_pack(&level_objects
, fp
);
133 r_write(fp
, (char *) &file_id
, sizeof(file_id
));
135 r_write(fp
, (char *) &foods
, sizeof(foods
));
136 r_write(fp
, (char *) &rogue
, sizeof(fighter
));
137 write_pack(&rogue
.pack
, fp
);
138 rw_id(id_potions
, fp
, POTIONS
, 1);
139 rw_id(id_scrolls
, fp
, SCROLS
, 1);
140 rw_id(id_wands
, fp
, WANDS
, 1);
141 rw_id(id_rings
, fp
, RINGS
, 1);
142 r_write(fp
, (char *) traps
, (MAX_TRAPS
* sizeof(trap
)));
143 r_write(fp
, (char *) is_wood
, (WANDS
* sizeof(boolean
)));
144 r_write(fp
, (char *) &cur_room
, sizeof(cur_room
));
146 r_write(fp
, (char *) &being_held
, sizeof(being_held
));
147 r_write(fp
, (char *) &bear_trap
, sizeof(bear_trap
));
148 r_write(fp
, (char *) &halluc
, sizeof(halluc
));
149 r_write(fp
, (char *) &blind
, sizeof(blind
));
150 r_write(fp
, (char *) &confused
, sizeof(confused
));
151 r_write(fp
, (char *) &levitate
, sizeof(levitate
));
152 r_write(fp
, (char *) &haste_self
, sizeof(haste_self
));
153 r_write(fp
, (char *) &see_invisible
, sizeof(see_invisible
));
154 r_write(fp
, (char *) &detect_monster
, sizeof(detect_monster
));
155 r_write(fp
, (char *) &wizard
, sizeof(wizard
));
156 r_write(fp
, (char *) &score_only
, sizeof(score_only
));
157 r_write(fp
, (char *) &m_moves
, sizeof(m_moves
));
159 rt_buf
.second
+= 10; /* allow for some processing time */
160 r_write(fp
, (char *) &rt_buf
, sizeof(rt_buf
));
164 (void) md_df(sfile
); /* delete file */
174 struct rogue_time saved_time
, mod_time
;
177 int new_file_id
, saved_file_id
;
179 if ( ((new_file_id
= md_get_file_id(fname
)) == -1) ||
180 ((fp
= fopen(fname
, "r")) == NULL
)) {
181 clean_up("cannot open file");
183 if (md_link_count(fname
) > 1) {
184 clean_up("file has link");
187 r_read(fp
, (char *) &detect_monster
, sizeof(detect_monster
));
188 r_read(fp
, (char *) &cur_level
, sizeof(cur_level
));
189 r_read(fp
, (char *) &max_level
, sizeof(max_level
));
190 read_string(hunger_str
, fp
);
192 (void) strcpy(tbuf
, login_name
);
193 read_string(login_name
, fp
);
194 if (strcmp(tbuf
, login_name
)) {
195 clean_up("you're not the original player");
198 r_read(fp
, (char *) &party_room
, sizeof(party_room
));
199 read_pack(&level_monsters
, fp
, 0);
200 read_pack(&level_objects
, fp
, 0);
201 r_read(fp
, (char *) &saved_file_id
, sizeof(saved_file_id
));
202 if (new_file_id
!= saved_file_id
) {
203 clean_up("sorry, saved game is not in the same file");
206 r_read(fp
, (char *) &foods
, sizeof(foods
));
207 r_read(fp
, (char *) &rogue
, sizeof(fighter
));
208 read_pack(&rogue
.pack
, fp
, 1);
209 rw_id(id_potions
, fp
, POTIONS
, 0);
210 rw_id(id_scrolls
, fp
, SCROLS
, 0);
211 rw_id(id_wands
, fp
, WANDS
, 0);
212 rw_id(id_rings
, fp
, RINGS
, 0);
213 r_read(fp
, (char *) traps
, (MAX_TRAPS
* sizeof(trap
)));
214 r_read(fp
, (char *) is_wood
, (WANDS
* sizeof(boolean
)));
215 r_read(fp
, (char *) &cur_room
, sizeof(cur_room
));
217 r_read(fp
, (char *) &being_held
, sizeof(being_held
));
218 r_read(fp
, (char *) &bear_trap
, sizeof(bear_trap
));
219 r_read(fp
, (char *) &halluc
, sizeof(halluc
));
220 r_read(fp
, (char *) &blind
, sizeof(blind
));
221 r_read(fp
, (char *) &confused
, sizeof(confused
));
222 r_read(fp
, (char *) &levitate
, sizeof(levitate
));
223 r_read(fp
, (char *) &haste_self
, sizeof(haste_self
));
224 r_read(fp
, (char *) &see_invisible
, sizeof(see_invisible
));
225 r_read(fp
, (char *) &detect_monster
, sizeof(detect_monster
));
226 r_read(fp
, (char *) &wizard
, sizeof(wizard
));
227 r_read(fp
, (char *) &score_only
, sizeof(score_only
));
228 r_read(fp
, (char *) &m_moves
, sizeof(m_moves
));
229 r_read(fp
, (char *) &saved_time
, sizeof(saved_time
));
231 if (fread(buf
, sizeof(char), 1, fp
) > 0) {
233 clean_up("extra characters in file");
236 md_gfmt(fname
, &mod_time
); /* get file modification time */
238 if (has_been_touched(&saved_time
, &mod_time
)) {
240 clean_up("sorry, file has been touched");
242 if ((!wizard
) && !md_df(fname
)) {
243 clean_up("cannot delete file");
256 while (pack
= pack
->next_object
) {
257 r_write(fp
, (char *) pack
, sizeof(object
));
259 t
.ichar
= t
.what_is
= 0;
260 r_write(fp
, (char *) &t
, sizeof(object
));
263 read_pack(pack
, fp
, is_rogue
)
268 object read_obj
, *new_obj
;
271 r_read(fp
, (char *) &read_obj
, sizeof(object
));
272 if (read_obj
.ichar
== 0) {
273 pack
->next_object
= (object
*) 0;
276 new_obj
= alloc_object();
279 if (new_obj
->in_use_flags
& BEING_WORN
) {
281 } else if (new_obj
->in_use_flags
& BEING_WIELDED
) {
283 } else if (new_obj
->in_use_flags
& (ON_EITHER_HAND
)) {
285 ((new_obj
->in_use_flags
& ON_LEFT_HAND
) ? 1 : 0));
288 pack
->next_object
= new_obj
;
300 for (i
= 0; i
< DROWS
; i
++) {
302 r_write(fp
, (char *) dungeon
[i
], (DCOLS
* sizeof(dungeon
[0][0])));
303 for (j
= 0; j
< DCOLS
; j
++) {
304 buf
[j
] = mvinch(i
, j
);
306 r_write(fp
, buf
, DCOLS
);
308 r_read(fp
, (char *) dungeon
[i
], (DCOLS
* sizeof(dungeon
[0][0])));
309 r_read(fp
, buf
, DCOLS
);
310 for (j
= 0; j
< DCOLS
; j
++) {
311 mvaddch(i
, j
, buf
[j
]);
317 rw_id(id_table
, fp
, n
, wr
)
318 struct id id_table
[];
325 for (i
= 0; i
< n
; i
++) {
327 r_write(fp
, (char *) &(id_table
[i
].value
), sizeof(short));
328 r_write(fp
, (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
);
348 r_write(fp
, (char *) &n
, sizeof(short));
358 r_read(fp
, (char *) &n
, sizeof(short));
369 for (i
= 0; i
< MAXROOMS
; i
++) {
370 rw
? r_write(fp
, (char *) (rooms
+ i
), sizeof(room
)) :
371 r_read(fp
, (char *) (rooms
+ i
), sizeof(room
));
380 if (fread(buf
, sizeof(char), n
, fp
) != n
) {
381 clean_up("read() failed, don't know why");
391 if (fwrite(buf
, sizeof(char), n
, fp
) != n
) {
392 message("write() failed, don't know why", 0);
400 has_been_touched(saved_time
, mod_time
)
401 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
) {