Mercurial > hg > early-roguelike
comparison srogue/state.c @ 36:2128c7dc8a40
Import Super-Rogue 9.0 from the Roguelike Restoration Project (r1490)
| author | elwin |
|---|---|
| date | Thu, 25 Nov 2010 12:21:41 +0000 |
| parents | |
| children | 3aa87373c908 |
comparison
equal
deleted
inserted
replaced
| 35:05018c63a721 | 36:2128c7dc8a40 |
|---|---|
| 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 #include <curses.h> | |
| 62 #include <sys/stat.h> | |
| 63 #include <stdio.h> | |
| 64 #include <stdlib.h> | |
| 65 #include <assert.h> | |
| 66 #include "rogue.h" | |
| 67 #include "rogue.ext" | |
| 68 | |
| 69 #define READSTAT ((format_error == 0) && (read_error == 0)) | |
| 70 #define WRITESTAT (write_error == 0) | |
| 71 | |
| 72 int read_error = FALSE; | |
| 73 int write_error = FALSE; | |
| 74 int format_error = FALSE; | |
| 75 int end_of_file = FALSE; | |
| 76 int big_endian = 0; | |
| 77 const char *fmterr = ""; | |
| 78 | |
| 79 char encstr[] = "\354\251\243\332A\201|\301\321p\210\251\327\"\257\365t\341%3\271^`~\203z{\341};\f\341\231\222e\234\351]\321"; | |
| 80 | |
| 81 /* | |
| 82 * perform an encrypted write | |
| 83 */ | |
| 84 encwrite(starta, size, outf) | |
| 85 register void *starta; | |
| 86 unsigned int size; | |
| 87 register FILE *outf; | |
| 88 { | |
| 89 register char *ep; | |
| 90 register char *start = starta; | |
| 91 | |
| 92 ep = encstr; | |
| 93 | |
| 94 while (size--) | |
| 95 { | |
| 96 putc(*start++ ^ *ep++, outf); | |
| 97 if (*ep == '\0') | |
| 98 ep = encstr; | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 /* | |
| 103 * perform an encrypted read | |
| 104 */ | |
| 105 encread(starta, size, inf) | |
| 106 register void *starta; | |
| 107 unsigned int size; | |
| 108 register int inf; | |
| 109 { | |
| 110 register char *ep; | |
| 111 register int read_size; | |
| 112 register char *start = starta; | |
| 113 | |
| 114 if ((read_size = read(inf, start, size)) == -1 || read_size == 0) | |
| 115 return read_size; | |
| 116 | |
| 117 ep = encstr; | |
| 118 | |
| 119 while (size--) | |
| 120 { | |
| 121 *start++ ^= *ep++; | |
| 122 if (*ep == '\0') | |
| 123 ep = encstr; | |
| 124 } | |
| 125 return read_size; | |
| 126 } | |
| 127 | |
| 128 void * | |
| 129 get_list_item(struct linked_list *l, int i) | |
| 130 { | |
| 131 int count = 0; | |
| 132 | |
| 133 while(l != NULL) | |
| 134 { | |
| 135 if (count == i) | |
| 136 return(l->l_data); | |
| 137 | |
| 138 l = l->l_next; | |
| 139 | |
| 140 count++; | |
| 141 } | |
| 142 | |
| 143 return(NULL); | |
| 144 } | |
| 145 | |
| 146 int | |
| 147 find_list_ptr(struct linked_list *l, void *ptr) | |
| 148 { | |
| 149 int count = 0; | |
| 150 | |
| 151 while(l != NULL) | |
| 152 { | |
| 153 if (l->l_data == ptr) | |
| 154 return(count); | |
| 155 | |
| 156 l = l->l_next; | |
| 157 count++; | |
| 158 } | |
| 159 | |
| 160 return(-1); | |
| 161 } | |
| 162 | |
| 163 int | |
| 164 list_size(struct linked_list *l) | |
| 165 { | |
| 166 int count = 0; | |
| 167 | |
| 168 while(l != NULL) | |
| 169 { | |
| 170 if (l->l_data == NULL) | |
| 171 return(count); | |
| 172 | |
| 173 count++; | |
| 174 | |
| 175 l = l->l_next; | |
| 176 } | |
| 177 | |
| 178 return(count); | |
| 179 } | |
| 180 | |
| 181 int | |
| 182 rs_write(FILE *savef, void *ptr, int size) | |
| 183 { | |
| 184 if (!write_error) | |
| 185 encwrite(ptr,size,savef); | |
| 186 | |
| 187 if (0) | |
| 188 write_error = TRUE; | |
| 189 | |
| 190 assert(write_error == 0); | |
| 191 | |
| 192 return(WRITESTAT); | |
| 193 } | |
| 194 | |
| 195 int | |
| 196 rs_write_char(FILE *savef, char c) | |
| 197 { | |
| 198 rs_write(savef, &c, 1); | |
| 199 | |
| 200 return(WRITESTAT); | |
| 201 } | |
| 202 | |
| 203 int | |
| 204 rs_write_boolean(FILE *savef, bool c) | |
| 205 { | |
| 206 unsigned char buf = (c == 0) ? 0 : 1; | |
| 207 | |
| 208 rs_write(savef, &buf, 1); | |
| 209 | |
| 210 return(WRITESTAT); | |
| 211 } | |
| 212 | |
| 213 int | |
| 214 rs_write_booleans(FILE *savef, bool *c, int count) | |
| 215 { | |
| 216 int n = 0; | |
| 217 | |
| 218 rs_write_int(savef,count); | |
| 219 | |
| 220 for(n = 0; n < count; n++) | |
| 221 rs_write_boolean(savef,c[n]); | |
| 222 | |
| 223 return(WRITESTAT); | |
| 224 } | |
| 225 | |
| 226 int | |
| 227 rs_write_shint(FILE *savef, unsigned char c) | |
| 228 { | |
| 229 unsigned char buf = c; | |
| 230 | |
| 231 rs_write(savef, &buf, 1); | |
| 232 | |
| 233 return(WRITESTAT); | |
| 234 } | |
| 235 | |
| 236 int | |
| 237 rs_write_short(FILE *savef, short c) | |
| 238 { | |
| 239 unsigned char bytes[2]; | |
| 240 unsigned char *buf = (unsigned char *) &c; | |
| 241 | |
| 242 if (big_endian) | |
| 243 { | |
| 244 bytes[1] = buf[0]; | |
| 245 bytes[0] = buf[1]; | |
| 246 buf = bytes; | |
| 247 } | |
| 248 | |
| 249 rs_write(savef, buf, 2); | |
| 250 | |
| 251 return(WRITESTAT); | |
| 252 } | |
| 253 | |
| 254 int | |
| 255 rs_write_shorts(FILE *savef, short *c, int count) | |
| 256 { | |
| 257 int n = 0; | |
| 258 | |
| 259 rs_write_int(savef,count); | |
| 260 | |
| 261 for(n = 0; n < count; n++) | |
| 262 rs_write_short(savef,c[n]); | |
| 263 | |
| 264 return(WRITESTAT); | |
| 265 } | |
| 266 | |
| 267 int | |
| 268 rs_write_ushort(FILE *savef, unsigned short c) | |
| 269 { | |
| 270 unsigned char bytes[2]; | |
| 271 unsigned char *buf = (unsigned char *) &c; | |
| 272 | |
| 273 if (big_endian) | |
| 274 { | |
| 275 bytes[1] = buf[0]; | |
| 276 bytes[0] = buf[1]; | |
| 277 buf = bytes; | |
| 278 } | |
| 279 | |
| 280 rs_write(savef, buf, 2); | |
| 281 | |
| 282 return(WRITESTAT); | |
| 283 } | |
| 284 | |
| 285 int | |
| 286 rs_write_int(FILE *savef, int c) | |
| 287 { | |
| 288 unsigned char bytes[4]; | |
| 289 unsigned char *buf = (unsigned char *) &c; | |
| 290 | |
| 291 if (big_endian) | |
| 292 { | |
| 293 bytes[3] = buf[0]; | |
| 294 bytes[2] = buf[1]; | |
| 295 bytes[1] = buf[2]; | |
| 296 bytes[0] = buf[3]; | |
| 297 buf = bytes; | |
| 298 } | |
| 299 | |
| 300 rs_write(savef, buf, 4); | |
| 301 | |
| 302 return(WRITESTAT); | |
| 303 } | |
| 304 | |
| 305 int | |
| 306 rs_write_ints(FILE *savef, int *c, int count) | |
| 307 { | |
| 308 int n = 0; | |
| 309 | |
| 310 rs_write_int(savef,count); | |
| 311 | |
| 312 for(n = 0; n < count; n++) | |
| 313 rs_write_int(savef,c[n]); | |
| 314 | |
| 315 return(WRITESTAT); | |
| 316 } | |
| 317 | |
| 318 int | |
| 319 rs_write_uint(FILE *savef, unsigned int c) | |
| 320 { | |
| 321 unsigned char bytes[4]; | |
| 322 unsigned char *buf = (unsigned char *) &c; | |
| 323 | |
| 324 if (big_endian) | |
| 325 { | |
| 326 bytes[3] = buf[0]; | |
| 327 bytes[2] = buf[1]; | |
| 328 bytes[1] = buf[2]; | |
| 329 bytes[0] = buf[3]; | |
| 330 buf = bytes; | |
| 331 } | |
| 332 | |
| 333 rs_write(savef, buf, 4); | |
| 334 | |
| 335 return(WRITESTAT); | |
| 336 } | |
| 337 | |
| 338 int | |
| 339 rs_write_long(FILE *savef, long c) | |
| 340 { | |
| 341 int c2; | |
| 342 unsigned char bytes[4]; | |
| 343 unsigned char *buf = (unsigned char *)&c; | |
| 344 | |
| 345 if (sizeof(long) == 8) | |
| 346 { | |
| 347 c2 = c; | |
| 348 buf = (unsigned char *) &c2; | |
| 349 } | |
| 350 | |
| 351 if (big_endian) | |
| 352 { | |
| 353 bytes[3] = buf[0]; | |
| 354 bytes[2] = buf[1]; | |
| 355 bytes[1] = buf[2]; | |
| 356 bytes[0] = buf[3]; | |
| 357 buf = bytes; | |
| 358 } | |
| 359 | |
| 360 rs_write(savef, buf, 4); | |
| 361 | |
| 362 return(WRITESTAT); | |
| 363 } | |
| 364 | |
| 365 int | |
| 366 rs_write_longs(FILE *savef, long *c, int count) | |
| 367 { | |
| 368 int n = 0; | |
| 369 | |
| 370 rs_write_int(savef,count); | |
| 371 | |
| 372 for(n = 0; n < count; n++) | |
| 373 rs_write_long(savef,c[n]); | |
| 374 | |
| 375 return(WRITESTAT); | |
| 376 } | |
| 377 | |
| 378 int | |
| 379 rs_write_ulong(FILE *savef, unsigned long c) | |
| 380 { | |
| 381 unsigned int c2; | |
| 382 unsigned char bytes[4]; | |
| 383 unsigned char *buf = (unsigned char *)&c; | |
| 384 | |
| 385 if ( (sizeof(long) == 8) && (sizeof(int) == 4) ) | |
| 386 { | |
| 387 c2 = c; |
