save.c revision 1.3 1 1.3 cgd /* $NetBSD: save.c,v 1.3 1995/04/22 10:28:21 cgd Exp $ */
2 1.3 cgd
3 1.1 cgd /*
4 1.3 cgd * Copyright (c) 1988, 1993
5 1.3 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Timothy C. Stoehr.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.1 cgd * 3. All advertising materials mentioning features or use of this software
19 1.1 cgd * must display the following acknowledgement:
20 1.1 cgd * This product includes software developed by the University of
21 1.1 cgd * California, Berkeley and its contributors.
22 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
23 1.1 cgd * may be used to endorse or promote products derived from this software
24 1.1 cgd * without specific prior written permission.
25 1.1 cgd *
26 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 cgd * SUCH DAMAGE.
37 1.1 cgd */
38 1.1 cgd
39 1.1 cgd #ifndef lint
40 1.3 cgd #if 0
41 1.3 cgd static char sccsid[] = "@(#)save.c 8.1 (Berkeley) 5/31/93";
42 1.3 cgd #else
43 1.3 cgd static char rcsid[] = "$NetBSD: save.c,v 1.3 1995/04/22 10:28:21 cgd Exp $";
44 1.3 cgd #endif
45 1.1 cgd #endif /* not lint */
46 1.1 cgd
47 1.1 cgd /*
48 1.1 cgd * save.c
49 1.1 cgd *
50 1.1 cgd * This source herein may be modified and/or distributed by anybody who
51 1.1 cgd * so desires, with the following restrictions:
52 1.1 cgd * 1.) No portion of this notice shall be removed.
53 1.1 cgd * 2.) Credit shall not be taken for the creation of this source.
54 1.1 cgd * 3.) This code is not to be traded, sold, or used for personal
55 1.1 cgd * gain or profit.
56 1.1 cgd *
57 1.1 cgd */
58 1.1 cgd
59 1.1 cgd #include <stdio.h>
60 1.1 cgd #include "rogue.h"
61 1.1 cgd
62 1.1 cgd short write_failed = 0;
63 1.1 cgd char *save_file = (char *) 0;
64 1.1 cgd
65 1.1 cgd extern boolean detect_monster;
66 1.1 cgd extern short cur_level, max_level;
67 1.1 cgd extern char hunger_str[];
68 1.1 cgd extern char login_name[];
69 1.1 cgd extern short party_room;
70 1.1 cgd extern short foods;
71 1.1 cgd extern boolean is_wood[];
72 1.1 cgd extern short cur_room;
73 1.1 cgd extern boolean being_held;
74 1.1 cgd extern short bear_trap;
75 1.1 cgd extern short halluc;
76 1.1 cgd extern short blind;
77 1.1 cgd extern short confused;
78 1.1 cgd extern short levitate;
79 1.1 cgd extern short haste_self;
80 1.1 cgd extern boolean see_invisible;
81 1.1 cgd extern boolean detect_monster;
82 1.1 cgd extern boolean wizard;
83 1.1 cgd extern boolean score_only;
84 1.1 cgd extern short m_moves;
85 1.1 cgd
86 1.1 cgd extern boolean msg_cleared;
87 1.1 cgd
88 1.1 cgd save_game()
89 1.1 cgd {
90 1.1 cgd char fname[64];
91 1.1 cgd
92 1.1 cgd if (!get_input_line("file name?", save_file, fname, "game not saved",
93 1.1 cgd 0, 1)) {
94 1.1 cgd return;
95 1.1 cgd }
96 1.1 cgd check_message();
97 1.1 cgd message(fname, 0);
98 1.1 cgd save_into_file(fname);
99 1.1 cgd }
100 1.1 cgd
101 1.1 cgd save_into_file(sfile)
102 1.1 cgd char *sfile;
103 1.1 cgd {
104 1.1 cgd FILE *fp;
105 1.1 cgd int file_id;
106 1.1 cgd char name_buffer[80];
107 1.1 cgd char *hptr;
108 1.1 cgd struct rogue_time rt_buf;
109 1.1 cgd
110 1.1 cgd if (sfile[0] == '~') {
111 1.1 cgd if (hptr = md_getenv("HOME")) {
112 1.1 cgd (void) strcpy(name_buffer, hptr);
113 1.1 cgd (void) strcat(name_buffer, sfile+1);
114 1.1 cgd sfile = name_buffer;
115 1.1 cgd }
116 1.1 cgd }
117 1.1 cgd if ( ((fp = fopen(sfile, "w")) == NULL) ||
118 1.1 cgd ((file_id = md_get_file_id(sfile)) == -1)) {
119 1.1 cgd message("problem accessing the save file", 0);
120 1.1 cgd return;
121 1.1 cgd }
122 1.1 cgd md_ignore_signals();
123 1.1 cgd write_failed = 0;
124 1.1 cgd (void) xxx(1);
125 1.1 cgd r_write(fp, (char *) &detect_monster, sizeof(detect_monster));
126 1.1 cgd r_write(fp, (char *) &cur_level, sizeof(cur_level));
127 1.1 cgd r_write(fp, (char *) &max_level, sizeof(max_level));
128 1.1 cgd write_string(hunger_str, fp);
129 1.1 cgd write_string(login_name, fp);
130 1.1 cgd r_write(fp, (char *) &party_room, sizeof(party_room));
131 1.1 cgd write_pack(&level_monsters, fp);
132 1.1 cgd write_pack(&level_objects, fp);
133 1.1 cgd r_write(fp, (char *) &file_id, sizeof(file_id));
134 1.1 cgd rw_dungeon(fp, 1);
135 1.1 cgd r_write(fp, (char *) &foods, sizeof(foods));
136 1.1 cgd r_write(fp, (char *) &rogue, sizeof(fighter));
137 1.1 cgd write_pack(&rogue.pack, fp);
138 1.1 cgd rw_id(id_potions, fp, POTIONS, 1);
139 1.1 cgd rw_id(id_scrolls, fp, SCROLS, 1);
140 1.1 cgd rw_id(id_wands, fp, WANDS, 1);
141 1.1 cgd rw_id(id_rings, fp, RINGS, 1);
142 1.1 cgd r_write(fp, (char *) traps, (MAX_TRAPS * sizeof(trap)));
143 1.1 cgd r_write(fp, (char *) is_wood, (WANDS * sizeof(boolean)));
144 1.1 cgd r_write(fp, (char *) &cur_room, sizeof(cur_room));
145 1.1 cgd rw_rooms(fp, 1);
146 1.1 cgd r_write(fp, (char *) &being_held, sizeof(being_held));
147 1.1 cgd r_write(fp, (char *) &bear_trap, sizeof(bear_trap));
148 1.1 cgd r_write(fp, (char *) &halluc, sizeof(halluc));
149 1.1 cgd r_write(fp, (char *) &blind, sizeof(blind));
150 1.1 cgd r_write(fp, (char *) &confused, sizeof(confused));
151 1.1 cgd r_write(fp, (char *) &levitate, sizeof(levitate));
152 1.1 cgd r_write(fp, (char *) &haste_self, sizeof(haste_self));
153 1.1 cgd r_write(fp, (char *) &see_invisible, sizeof(see_invisible));
154 1.1 cgd r_write(fp, (char *) &detect_monster, sizeof(detect_monster));
155 1.1 cgd r_write(fp, (char *) &wizard, sizeof(wizard));
156 1.1 cgd r_write(fp, (char *) &score_only, sizeof(score_only));
157 1.1 cgd r_write(fp, (char *) &m_moves, sizeof(m_moves));
158 1.1 cgd md_gct(&rt_buf);
159 1.1 cgd rt_buf.second += 10; /* allow for some processing time */
160 1.1 cgd r_write(fp, (char *) &rt_buf, sizeof(rt_buf));
161 1.1 cgd fclose(fp);
162 1.1 cgd
163 1.1 cgd if (write_failed) {
164 1.1 cgd (void) md_df(sfile); /* delete file */
165 1.1 cgd } else {
166 1.1 cgd clean_up("");
167 1.1 cgd }
168 1.1 cgd }
169 1.1 cgd
170 1.1 cgd restore(fname)
171 1.1 cgd char *fname;
172 1.1 cgd {
173 1.1 cgd FILE *fp;
174 1.1 cgd struct rogue_time saved_time, mod_time;
175 1.1 cgd char buf[4];
176 1.1 cgd char tbuf[40];
177 1.1 cgd int new_file_id, saved_file_id;
178 1.1 cgd
179 1.1 cgd if ( ((new_file_id = md_get_file_id(fname)) == -1) ||
180 1.1 cgd ((fp = fopen(fname, "r")) == NULL)) {
181 1.1 cgd clean_up("cannot open file");
182 1.1 cgd }
183 1.1 cgd if (md_link_count(fname) > 1) {
184 1.1 cgd clean_up("file has link");
185 1.1 cgd }
186 1.1 cgd (void) xxx(1);
187 1.1 cgd r_read(fp, (char *) &detect_monster, sizeof(detect_monster));
188 1.1 cgd r_read(fp, (char *) &cur_level, sizeof(cur_level));
189 1.1 cgd r_read(fp, (char *) &max_level, sizeof(max_level));
190 1.1 cgd read_string(hunger_str, fp);
191 1.1 cgd
192 1.1 cgd (void) strcpy(tbuf, login_name);
193 1.1 cgd read_string(login_name, fp);
194 1.1 cgd if (strcmp(tbuf, login_name)) {
195 1.1 cgd clean_up("you're not the original player");
196 1.1 cgd }
197 1.1 cgd
198 1.1 cgd r_read(fp, (char *) &party_room, sizeof(party_room));
199 1.1 cgd read_pack(&level_monsters, fp, 0);
200 1.1 cgd read_pack(&level_objects, fp, 0);
201 1.1 cgd r_read(fp, (char *) &saved_file_id, sizeof(saved_file_id));
202 1.1 cgd if (new_file_id != saved_file_id) {
203 1.1 cgd clean_up("sorry, saved game is not in the same file");
204 1.1 cgd }
205 1.1 cgd rw_dungeon(fp, 0);
206 1.1 cgd r_read(fp, (char *) &foods, sizeof(foods));
207 1.1 cgd r_read(fp, (char *) &rogue, sizeof(fighter));
208 1.1 cgd read_pack(&rogue.pack, fp, 1);
209 1.1 cgd rw_id(id_potions, fp, POTIONS, 0);
210 1.1 cgd rw_id(id_scrolls, fp, SCROLS, 0);
211 1.1 cgd rw_id(id_wands, fp, WANDS, 0);
212 1.1 cgd rw_id(id_rings, fp, RINGS, 0);
213 1.1 cgd r_read(fp, (char *) traps, (MAX_TRAPS * sizeof(trap)));
214 1.1 cgd r_read(fp, (char *) is_wood, (WANDS * sizeof(boolean)));
215 1.1 cgd r_read(fp, (char *) &cur_room, sizeof(cur_room));
216 1.1 cgd rw_rooms(fp, 0);
217 1.1 cgd r_read(fp, (char *) &being_held, sizeof(being_held));
218 1.1 cgd r_read(fp, (char *) &bear_trap, sizeof(bear_trap));
219 1.1 cgd r_read(fp, (char *) &halluc, sizeof(halluc));
220 1.1 cgd r_read(fp, (char *) &blind, sizeof(blind));
221 1.1 cgd r_read(fp, (char *) &confused, sizeof(confused));
222 1.1 cgd r_read(fp, (char *) &levitate, sizeof(levitate));
223 1.1 cgd r_read(fp, (char *) &haste_self, sizeof(haste_self));
224 1.1 cgd r_read(fp, (char *) &see_invisible, sizeof(see_invisible));
225 1.1 cgd r_read(fp, (char *) &detect_monster, sizeof(detect_monster));
226 1.1 cgd r_read(fp, (char *) &wizard, sizeof(wizard));
227 1.1 cgd r_read(fp, (char *) &score_only, sizeof(score_only));
228 1.1 cgd r_read(fp, (char *) &m_moves, sizeof(m_moves));
229 1.1 cgd r_read(fp, (char *) &saved_time, sizeof(saved_time));
230 1.1 cgd
231 1.1 cgd if (fread(buf, sizeof(char), 1, fp) > 0) {
232 1.1 cgd clear();
233 1.1 cgd clean_up("extra characters in file");
234 1.1 cgd }
235 1.1 cgd
236 1.1 cgd md_gfmt(fname, &mod_time); /* get file modification time */
237 1.1 cgd
238 1.1 cgd if (has_been_touched(&saved_time, &mod_time)) {
239 1.1 cgd clear();
240 1.1 cgd clean_up("sorry, file has been touched");
241 1.1 cgd }
242 1.1 cgd if ((!wizard) && !md_df(fname)) {
243 1.1 cgd clean_up("cannot delete file");
244 1.1 cgd }
245 1.1 cgd msg_cleared = 0;
246 1.1 cgd ring_stats(0);
247 1.1 cgd fclose(fp);
248 1.1 cgd }
249 1.1 cgd
250 1.1 cgd write_pack(pack, fp)
251 1.1 cgd object *pack;
252 1.1 cgd FILE *fp;
253 1.1 cgd {
254 1.1 cgd object t;
255 1.1 cgd
256 1.1 cgd while (pack = pack->next_object) {
257 1.1 cgd r_write(fp, (char *) pack, sizeof(object));
258 1.1 cgd }
259 1.1 cgd t.ichar = t.what_is = 0;
260 1.1 cgd r_write(fp, (char *) &t, sizeof(object));
261 1.1 cgd }
262 1.1 cgd
263 1.1 cgd read_pack(pack, fp, is_rogue)
264 1.1 cgd object *pack;
265 1.1 cgd FILE *fp;
266 1.1 cgd boolean is_rogue;
267 1.1 cgd {
268 1.1 cgd object read_obj, *new_obj;
269 1.1 cgd
270 1.1 cgd for (;;) {
271 1.1 cgd r_read(fp, (char *) &read_obj, sizeof(object));
272 1.1 cgd if (read_obj.ichar == 0) {
273 1.1 cgd pack->next_object = (object *) 0;
274 1.1 cgd break;
275 1.1 cgd }
276 1.1 cgd new_obj = alloc_object();
277 1.1 cgd *new_obj = read_obj;
278 1.1 cgd if (is_rogue) {
279 1.1 cgd if (new_obj->in_use_flags & BEING_WORN) {
280 1.1 cgd do_wear(new_obj);
281 1.1 cgd } else if (new_obj->in_use_flags & BEING_WIELDED) {
282 1.1 cgd do_wield(new_obj);
283 1.1 cgd } else if (new_obj->in_use_flags & (ON_EITHER_HAND)) {
284 1.1 cgd do_put_on(new_obj,
285 1.1 cgd ((new_obj->in_use_flags & ON_LEFT_HAND) ? 1 : 0));
286 1.1 cgd }
287 1.1 cgd }
288 1.1 cgd pack->next_object = new_obj;
289 1.1 cgd pack = new_obj;
290 1.1 cgd }
291 1.1 cgd }
292 1.1 cgd
293 1.1 cgd rw_dungeon(fp, rw)
294 1.1 cgd FILE *fp;
295 1.1 cgd boolean rw;
296 1.1 cgd {
297 1.1 cgd short i, j;
298 1.1 cgd char buf[DCOLS];
299 1.1 cgd
300 1.1 cgd for (i = 0; i < DROWS; i++) {
301 1.1 cgd if (rw) {
302 1.1 cgd r_write(fp, (char *) dungeon[i], (DCOLS * sizeof(dungeon[0][0])));
303 1.1 cgd for (j = 0; j < DCOLS; j++) {
304 1.1 cgd buf[j] = mvinch(i, j);
305 1.1 cgd }
306 1.1 cgd r_write(fp, buf, DCOLS);
307 1.1 cgd } else {
308 1.1 cgd r_read(fp, (char *) dungeon[i], (DCOLS * sizeof(dungeon[0][0])));
309 1.1 cgd r_read(fp, buf, DCOLS);
310 1.1 cgd for (j = 0; j < DCOLS; j++) {
311 1.1 cgd mvaddch(i, j, buf[j]);
312 1.1 cgd }
313 1.1 cgd }
314 1.1 cgd }
315 1.1 cgd }
316 1.1 cgd
317 1.1 cgd rw_id(id_table, fp, n, wr)
318 1.1 cgd struct id id_table[];
319 1.1 cgd FILE *fp;
320 1.1 cgd int n;
321 1.1 cgd boolean wr;
322 1.1 cgd {
323 1.1 cgd short i;
324 1.1 cgd
325 1.1 cgd for (i = 0; i < n; i++) {
326 1.1 cgd if (wr) {
327 1.1 cgd r_write(fp, (char *) &(id_table[i].value), sizeof(short));
328 1.1 cgd r_write(fp, (char *) &(id_table[i].id_status),
329 1.1 cgd sizeof(unsigned short));
330 1.1 cgd write_string(id_table[i].title, fp);
331 1.1 cgd } else {
332 1.1 cgd r_read(fp, (char *) &(id_table[i].value), sizeof(short));
333 1.1 cgd r_read(fp, (char *) &(id_table[i].id_status),
334 1.1 cgd sizeof(unsigned short));
335 1.1 cgd read_string(id_table[i].title, fp);
336 1.1 cgd }
337 1.1 cgd }
338 1.1 cgd }
339 1.1 cgd
340 1.1 cgd write_string(s, fp)
341 1.1 cgd char *s;
342 1.1 cgd FILE *fp;
343 1.1 cgd {
344 1.1 cgd short n;
345 1.1 cgd
346 1.1 cgd n = strlen(s) + 1;
347 1.1 cgd xxxx(s, n);
348 1.1 cgd r_write(fp, (char *) &n, sizeof(short));
349 1.1 cgd r_write(fp, s, n);
350 1.1 cgd }
351 1.1 cgd
352 1.1 cgd read_string(s, fp)
353 1.1 cgd char *s;
354 1.1 cgd FILE *fp;
355 1.1 cgd {
356 1.1 cgd short n;
357 1.1 cgd
358 1.1 cgd r_read(fp, (char *) &n, sizeof(short));
359 1.1 cgd r_read(fp, s, n);
360 1.1 cgd xxxx(s, n);
361 1.1 cgd }
362 1.1 cgd
363 1.1 cgd rw_rooms(fp, rw)
364 1.1 cgd FILE *fp;
365 1.1 cgd boolean rw;
366 1.1 cgd {
367 1.1 cgd short i;
368 1.1 cgd
369 1.1 cgd for (i = 0; i < MAXROOMS; i++) {
370 1.1 cgd rw ? r_write(fp, (char *) (rooms + i), sizeof(room)) :
371 1.1 cgd r_read(fp, (char *) (rooms + i), sizeof(room));
372 1.1 cgd }
373 1.1 cgd }
374 1.1 cgd
375 1.1 cgd r_read(fp, buf, n)
376 1.1 cgd FILE *fp;
377 1.1 cgd char *buf;
378 1.1 cgd int n;
379 1.1 cgd {
380 1.1 cgd if (fread(buf, sizeof(char), n, fp) != n) {
381 1.1 cgd clean_up("read() failed, don't know why");
382 1.1 cgd }
383 1.1 cgd }
384 1.1 cgd
385 1.1 cgd r_write(fp, buf, n)
386 1.1 cgd FILE *fp;
387 1.1 cgd char *buf;
388 1.1 cgd int n;
389 1.1 cgd {
390 1.1 cgd if (!write_failed) {
391 1.1 cgd if (fwrite(buf, sizeof(char), n, fp) != n) {
392 1.1 cgd message("write() failed, don't know why", 0);
393 1.1 cgd sound_bell();
394 1.1 cgd write_failed = 1;
395 1.1 cgd }
396 1.1 cgd }
397 1.1 cgd }
398 1.1 cgd
399 1.1 cgd boolean
400 1.1 cgd has_been_touched(saved_time, mod_time)
401 1.1 cgd struct rogue_time *saved_time, *mod_time;
402 1.1 cgd {
403 1.1 cgd if (saved_time->year < mod_time->year) {
404 1.1 cgd return(1);
405 1.1 cgd } else if (saved_time->year > mod_time->year) {
406 1.1 cgd return(0);
407 1.1 cgd }
408 1.1 cgd if (saved_time->month < mod_time->month) {
409 1.1 cgd return(1);
410 1.1 cgd } else if (saved_time->month > mod_time->month) {
411 1.1 cgd return(0);
412 1.1 cgd }
413 1.1 cgd if (saved_time->day < mod_time->day) {
414 1.1 cgd return(1);
415 1.1 cgd } else if (saved_time->day > mod_time->day) {
416 1.1 cgd return(0);
417 1.1 cgd }
418 1.1 cgd if (saved_time->hour < mod_time->hour) {
419 1.1 cgd return(1);
420 1.1 cgd } else if (saved_time->hour > mod_time->hour) {
421 1.1 cgd return(0);
422 1.1 cgd }
423 1.1 cgd if (saved_time->minute < mod_time->minute) {
424 1.1 cgd return(1);
425 1.1 cgd } else if (saved_time->minute > mod_time->minute) {
426 1.1 cgd return(0);
427 1.1 cgd }
428 1.1 cgd if (saved_time->second < mod_time->second) {
429 1.1 cgd return(1);
430 1.1 cgd }
431 1.1 cgd return(0);
432 1.1 cgd }
433