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