comparison rogue4/state.c @ 12:9535a08ddc39

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