comparison arogue7/state.c @ 125:adfa37e67084

Import Advanced Rogue 7.7 from the Roguelike Restoration Project (r1490)
author John "Elwin" Edwards
date Fri, 08 May 2015 15:24:40 -0400
parents
children b786053d2f37
comparison
equal deleted inserted replaced
124:d10fc4a065ac 125:adfa37e67084
1 /*
2 state.c - Portable Rogue Save State Code
3
4 Copyright (C) 1999, 2000, 2005 Nicholas J. Kisseberth
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
9 are met:
10 1. Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15 3. Neither the name(s) of the author(s) nor the names of other contributors
16 may be used to endorse or promote products derived from this software
17 without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTORS ``AS IS'' AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTORS BE LIABLE
23 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 SUCH DAMAGE.
30 */
31
32 /************************************************************************/
33 /* Save State Code */
34 /************************************************************************/
35
36 #define RSID_STATS 0xABCD0001
37 #define RSID_MSTATS 0xABCD0002
38 #define RSID_THING 0xABCD0003
39 #define RSID_OBJECT 0xABCD0004
40 #define RSID_MAGICITEMS 0xABCD0005
41 #define RSID_KNOWS 0xABCD0006
42 #define RSID_GUESSES 0xABCD0007
43 #define RSID_OBJECTLIST 0xABCD0008
44 #define RSID_BAGOBJECT 0xABCD0009
45 #define RSID_MONSTERLIST 0xABCD000A
46 #define RSID_MONSTERSTATS 0xABCD000B
47 #define RSID_MONSTERS 0xABCD000C
48 #define RSID_TRAP 0xABCD000D
49 #define RSID_WINDOW 0xABCD000E
50 #define RSID_DAEMONS 0xABCD000F
51 #define RSID_STICKS 0xABCD0010
52 #define RSID_IARMOR 0xABCD0011
53 #define RSID_SPELLS 0xABCD0012
54 #define RSID_ILIST 0xABCD0013
55 #define RSID_HLIST 0xABCD0014
56 #define RSID_DEATHTYPE 0xABCD0015
57 #define RSID_CTYPES 0XABCD0016
58 #define RSID_COORDLIST 0XABCD0017
59 #define RSID_ROOMS 0XABCD0018
60
61 #include <curses.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include "rogue.h"
65
66 #define READSTAT (format_error || read_error )
67 #define WRITESTAT (write_error)
68
69 static int read_error = FALSE;
70 static int write_error = FALSE;
71 static int format_error = FALSE;
72 static int endian = 0x01020304;
73 #define big_endian ( *((char *)&endian) == 0x01 )
74
75 int
76 rs_write(FILE *savef, void *ptr, size_t size)
77 {
78 if (write_error)
79 return(WRITESTAT);
80
81 if (encwrite(ptr, size, fileno(savef)) != size)
82 write_error = 1;
83
84 return(WRITESTAT);
85 }
86
87 int
88 rs_read(int inf, void *ptr, size_t size)
89 {
90 if (read_error || format_error)
91 return(READSTAT);
92
93 if (encread(ptr, size, inf) != size)
94 read_error = 1;
95
96 return(READSTAT);
97 }
98
99 int
100 rs_write_uchar(FILE *savef, unsigned char c)
101 {
102 if (write_error)
103 return(WRITESTAT);
104
105 rs_write(savef, &c, 1);
106
107 return(WRITESTAT);
108 }
109
110 int
111 rs_read_uchar(int inf, unsigned char *c)
112 {
113 if (read_error || format_error)
114 return(READSTAT);
115
116 rs_read(inf, c, 1);
117
118 return(READSTAT);
119 }
120
121 int
122 rs_write_char(FILE *savef, char c)
123 {
124 if (write_error)
125 return(WRITESTAT);
126
127 rs_write(savef, &c, 1);
128
129 return(WRITESTAT);
130 }
131
132 int
133 rs_read_char(int inf, char *c)
134 {
135 if (read_error || format_error)
136 return(READSTAT);
137
138 rs_read(inf, c, 1);
139
140 return(READSTAT);
141 }
142
143 int
144 rs_write_chars(FILE *savef, char *c, int count)
145 {
146 if (write_error)
147 return(WRITESTAT);
148
149 rs_write_int(savef, count);
150 rs_write(savef, c, count);
151
152 return(WRITESTAT);
153 }
154
155 int
156 rs_read_chars(int inf, char *i, int count)
157 {
158 int value = 0;
159
160 if (read_error || format_error)
161 return(READSTAT);
162
163 rs_read_int(inf, &value);
164
165 if (value != count)
166 format_error = TRUE;
167
168 rs_read(inf, i, count);
169
170 return(READSTAT);
171 }
172
173 int
174 rs_write_int(FILE *savef, int c)
175 {
176 unsigned char bytes[4];
177 unsigned char *buf = (unsigned char *) &c;
178
179 if (write_error)
180 return(WRITESTAT);
181
182 if (big_endian)
183 {
184 bytes[3] = buf[0];
185 bytes[2] = buf[1];
186 bytes[1] = buf[2];
187 bytes[0] = buf[3];
188 buf = bytes;
189 }
190
191 rs_write(savef, buf, 4);
192
193 return(WRITESTAT);
194 }
195
196 int
197 rs_read_int(int inf, int *i)
198 {
199 unsigned char bytes[4];
200 int input = 0;
201 unsigned char *buf = (unsigned char *)&input;
202
203 if (read_error || format_error)
204 return(READSTAT);
205
206 rs_read(inf, &input, 4);
207
208 if (big_endian)
209 {
210 bytes[3] = buf[0];
211 bytes[2] = buf[1];
212 bytes[1] = buf[2];
213 bytes[0] = buf[3];
214 buf = bytes;
215 }
216
217 *i = *((int *) buf);
218
219 return(READSTAT);
220 }
221
222 int
223 rs_write_ints(FILE *savef, int *c, int count)
224 {
225 int n = 0;
226
227 if (write_error)
228 return(WRITESTAT);
229
230 rs_write_int(savef, count);
231
232 for(n = 0; n < count; n++)
233 if( rs_write_int(savef,c[n]) != 0)
234 break;
235
236 return(WRITESTAT);
237 }
238
239 int
240 rs_read_ints(int inf, int *i, int count)
241 {
242 int n, value;
243
244 if (read_error || format_error)
245 return(READSTAT);
246
247 rs_read_int(inf,&value);
248
249 if (value != count)
250 format_error = TRUE;
251
252 for(n = 0; n < count; n++)
253 if (rs_read_int(inf, &i[n]) != 0)
254 break;
255
256 return(READSTAT);
257 }
258
259 int
260 rs_write_boolean(FILE *savef, bool c)
261 {
262 unsigned char buf = (c == 0) ? 0 : 1;
263
264 if (write_error)
265 return(WRITESTAT);
266
267 rs_write(savef, &buf, 1);
268
269 return(WRITESTAT);
270 }
271
272 int
273 rs_read_boolean(int inf, bool *i)
274 {
275 unsigned char buf = 0;
276
277 if (read_error || format_error)
278 return(READSTAT);
279
280 rs_read(inf, &buf, 1);
281
282 *i = (buf != 0);
283
284 return(READSTAT);
285 }
286
287 int
288 rs_write_booleans(FILE *savef, bool *c, int count)
289 {
290 int n = 0;
291
292 if (write_error)
293 return(WRITESTAT);
294
295 rs_write_int(savef, count);
296
297 for(n = 0; n < count; n++)
298 if (rs_write_boolean(savef, c[n]) != 0)
299 break;
300
301 return(WRITESTAT);
302 }
303
304 int
305 rs_read_booleans(int inf, bool *i, int count)
306 {
307 int n = 0, value = 0;
308
309 if (read_error || format_error)
310 return(READSTAT);
311
312 rs_read_int(inf,&value);
313
314 if (value != count)
315 format_error = TRUE;
316
317 for(n = 0; n < count; n++)
318 if (rs_read_boolean(inf, &i[n]) != 0)
319 break;
320
321 return(READSTAT);
322 }
323
324 int
325 rs_write_short(FILE *savef, short c)
326 {
327 unsigned char bytes[2];
328 unsigned char *buf = (unsigned char *) &c;
329
330 if (write_error)
331 return(WRITESTAT);
332
333 if (big_endian)
334 {
335 bytes[1] = buf[0];
336 bytes[0] = buf[1];
337 buf = bytes;
338 }
339
340 rs_write(savef, buf, 2);
341
342 return(WRITESTAT);
343 }
344
345 int
346 rs_read_short(int inf, short *i)
347 {
348 unsigned char bytes[2];
349 short input;
350 unsigned char *buf = (unsigned char *)&input;
351
352 if (read_error || format_error)
353 return(READSTAT);
354
355 rs_read(inf, &input, 2);
356
357 if (big_endian)
358 {
359 bytes[1] = buf[0];
360 bytes[0] = buf[1];
361 buf = bytes;
362 }
363
364 *i = *((short *) buf);
365
366 return(READSTAT);
367 }
368
369 int
370 rs_write_shorts(FILE *savef, short *c, int count)
371 {
372 int n = 0;
373
374 if (write_error)
375 return(WRITESTAT);
376
377 rs_write_int(savef, count);
378
379 for(n = 0; n < count; n++)
380 if (rs_write_short(savef, c[n]) != 0)
381 break;
382
383 return(WRITESTAT);
384 }
385
386 int
387 rs_read_shorts(int inf, short *i, int count)