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