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