2 state.c - Portable Rogue Save State Code
4 Copyright (C) 1999, 2000, 2005, 2006 Nicholas J. Kisseberth
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
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.
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
32 /************************************************************************/
34 /************************************************************************/
36 #define RSID_STATS 0xABCD0001
37 #define RSID_THING 0xABCD0002
38 #define RSID_THING_NULL 0xDEAD0002
39 #define RSID_OBJECT 0xABCD0003
40 #define RSID_MAGICITEMS 0xABCD0004
41 #define RSID_KNOWS 0xABCD0005
42 #define RSID_GUESSES 0xABCD0006
43 #define RSID_OBJECTLIST 0xABCD0007
44 #define RSID_BAGOBJECT 0xABCD0008
45 #define RSID_MONSTERLIST 0xABCD0009
46 #define RSID_MONSTERSTATS 0xABCD000A
47 #define RSID_MONSTERS 0xABCD000B
48 #define RSID_TRAP 0xABCD000C
49 #define RSID_WINDOW 0xABCD000D
50 #define RSID_DAEMONS 0xABCD000E
51 #define RSID_IWEAPS 0xABCD000F
52 #define RSID_IARMOR 0xABCD0010
53 #define RSID_SPELLS 0xABCD0011
54 #define RSID_ILIST 0xABCD0012
55 #define RSID_HLIST 0xABCD0013
56 #define RSID_DEATHTYPE 0xABCD0014
57 #define RSID_CTYPES 0XABCD0015
58 #define RSID_COORDLIST 0XABCD0016
59 #define RSID_ROOMS 0XABCD0017
67 #define READSTAT (format_error || read_error )
68 #define WRITESTAT (write_error)
70 static int read_error
= FALSE
;
71 static int write_error
= FALSE
;
72 static int format_error
= FALSE
;
73 static int endian
= 0x01020304;
74 #define big_endian ( *((char *)&endian) == 0x01 )
77 rs_write(FILE *savef
, void *ptr
, size_t size
)
82 if (encwrite(ptr
, size
, savef
) != size
)
89 rs_read(int inf
, void *ptr
, size_t size
)
91 if (read_error
|| format_error
)
94 if (encread(ptr
, size
, inf
) != size
)
101 rs_write_char(FILE *savef
, char c
)
106 rs_write(savef
, &c
, 1);
112 rs_read_char(int inf
, char *c
)
114 if (read_error
|| format_error
)
123 rs_write_chars(FILE *savef
, char *c
, int count
)
128 rs_write_int(savef
, count
);
129 rs_write(savef
, c
, count
);
135 rs_read_chars(int inf
, char *i
, int count
)
139 if (read_error
|| format_error
)
142 rs_read_int(inf
, &value
);
147 rs_read(inf
, i
, count
);
153 rs_write_int(FILE *savef
, int c
)
155 unsigned char bytes
[4];
156 unsigned char *buf
= (unsigned char *) &c
;
170 rs_write(savef
, buf
, 4);
176 rs_read_int(int inf
, int *i
)
178 unsigned char bytes
[4];
180 unsigned char *buf
= (unsigned char *)&input
;
182 if (read_error
|| format_error
)
185 rs_read(inf
, &input
, 4);
202 rs_write_ints(FILE *savef
, int *c
, int count
)
209 rs_write_int(savef
, count
);
211 for(n
= 0; n
< count
; n
++)
212 if( rs_write_int(savef
,c
[n
]) != 0)
219 rs_read_ints(int inf
, int *i
, int count
)
223 if (read_error
|| format_error
)
226 rs_read_int(inf
,&value
);
231 for(n
= 0; n
< count
; n
++)
232 if (rs_read_int(inf
, &i
[n
]) != 0)
239 rs_write_boolean(FILE *savef
, bool c
)
241 unsigned char buf
= (c
== 0) ? 0 : 1;
246 rs_write(savef
, &buf
, 1);
252 rs_read_boolean(int inf
, bool *i
)
254 unsigned char buf
= 0;
256 if (read_error
|| format_error
)
259 rs_read(inf
, &buf
, 1);
267 rs_write_booleans(FILE *savef
, bool *c
, int count
)
274 rs_write_int(savef
, count
);
276 for(n
= 0; n
< count
; n
++)
277 if (rs_write_boolean(savef
, c
[n
]) != 0)
284 rs_read_booleans(int inf
, bool *i
, int count
)
286 int n
= 0, value
= 0;
288 if (read_error
|| format_error
)
291 rs_read_int(inf
,&value
);
296 for(n
= 0; n
< count
; n
++)
297 if (rs_read_boolean(inf
, &i
[n
]) != 0)
304 rs_write_short(FILE *savef
, short c
)
306 unsigned char bytes
[2];
307 unsigned char *buf
= (unsigned char *) &c
;
319 rs_write(savef
, buf
, 2);
325 rs_read_short(int inf
, short *i
)
327 unsigned char bytes
[2];
329 unsigned char *buf
= (unsigned char *)&input
;
331 if (read_error
|| format_error
)
334 rs_read(inf
, &input
, 2);
343 *i
= *((short *) buf
);
349 rs_write_shorts(FILE *savef
, short *c
, int count
)
356 rs_write_int(savef
, count
);
358 for(n
= 0; n
< count
; n
++)
359 if (rs_write_short(savef
, c
[n
]) != 0)
366 rs_read_shorts(int inf
, short *i
, int count
)
368 int n
= 0, value
= 0;
370 if (read_error
|| format_error
)
373 rs_read_int(inf
,&value
);
378 for(n
= 0; n
< value
; n
++)
379 if (rs_read_short(inf
, &i
[n
]) != 0)
386 rs_write_ushort(FILE *savef
, unsigned short c
)
388 unsigned char bytes
[2];
389 unsigned char *buf
= (unsigned char *) &c
;
401 rs_write(savef
, buf
, 2);
407 rs_read_ushort(int inf
, unsigned short *i
)
409 unsigned char bytes
[2];
410 unsigned short input
;
411 unsigned char *buf
= (unsigned char *)&input
;
413 if (read_error
|| format_error
)
416 rs_read(inf
, &input
, 2);
425 *i
= *((unsigned short *) buf
);
431 rs_write_uint(FILE *savef
, unsigned int c
)
433 unsigned char bytes
[4];
434 unsigned char *buf
= (unsigned char *) &c
;
448 rs_write(savef
, buf
, 4);
454 rs_read_uint(int inf
, unsigned int *i
)
456 unsigned char bytes
[4];
458 unsigned char *buf
= (unsigned char *)&input
;
460 if (read_error
|| format_error
)
463 rs_read(inf
, &input
, 4);
474 *i
= *((unsigned int *) buf
);
480 rs_write_long(FILE *savef
, long c
)
483 unsigned char bytes
[4];
484 unsigned char *buf
= (unsigned char *)&c
;
489 if (sizeof(long) == 8)
492 buf
= (unsigned char *) &c2
;
504 rs_write(savef
, buf
, 4);
510 rs_read_long(int inf
, long *i
)
512 unsigned char bytes
[4];
514 unsigned char *buf
= (unsigned char *) &input
;
516 if (read_error
|| format_error
)
519 rs_read(inf
, &input
, 4);
530 *i
= *((long *) buf
);
536 rs_write_longs(FILE *savef
, long *c
, int count
)
543 rs_write_int(savef
,count
);
545 for(n
= 0; n
< count
; n
++)
546 rs_write_long(savef
, c
[n
]);
552 rs_read_longs(int inf
, long *i
, int count
)
554 int n
= 0, value
= 0;
556 if (read_error
|| format_error
)
559 rs_read_int(inf
,&value
);
564 for(n
= 0; n
< value
; n
++)
565 if (rs_read_long(inf
, &i
[n
]) != 0)
572 rs_write_ulong(FILE *savef
, unsigned long c
)
575 unsigned char bytes
[4];
576 unsigned char *buf
= (unsigned char *)&c
;
581 if ( (sizeof(long) == 8) && (sizeof(int) == 4) )
584 buf
= (unsigned char *) &c2
;
596 rs_write(savef
, buf
, 4);
602 rs_read_ulong(int inf
, unsigned long *i
)
604 unsigned char bytes
[4];
606 unsigned char *buf
= (unsigned char *) &input
;
608 if (read_error
|| format_error
)
611 rs_read(inf
, &input
, 4);
622 *i
= *((unsigned long *) buf
);
628 rs_write_ulongs(FILE *savef
, unsigned long *c
, int count
)
635 rs_write_int(savef
,count
);
637 for(n
= 0; n
< count
; n
++)
638 if (rs_write_ulong(savef
,c
[n
]) != 0)
645 rs_read_ulongs(int inf
, unsigned long *i
, int count
)
647 int n
= 0, value
= 0;
649 if (read_error
|| format_error
)
652 rs_read_int(inf
,&value
);
657 for(n
= 0; n
< count
; n
++)
658 if (rs_read_ulong(inf
, &i
[n
]) != 0)
665 rs_write_marker(FILE *savef
, int id
)
670 rs_write_int(savef
, id
);
676 rs_read_marker(int inf
, int id
)
680 if (read_error
|| format_error
)
683 if (rs_read_int(inf
, &nid
) == 0)
692 /******************************************************************************/
695 rs_write_string(FILE *savef
, char *s
)
702 len
= (s
== NULL
) ? 0 : (int) strlen(s
) + 1;
704 rs_write_int(savef
, len
);
705 rs_write_chars(savef
, s
, len
);
711 rs_read_string(int inf
, char *s
, int max
)
715 if (read_error
|| format_error
)
718 rs_read_int(inf
, &len
);
723 rs_read_chars(inf
, s
, len
);
729 rs_read_new_string(int inf
, char **s
)
734 if (read_error
|| format_error
)
737 rs_read_int(inf
, &len
);
749 rs_read_chars(inf
, buf
, len
);
757 rs_write_strings(FILE *savef
, char *s
[], int count
)
764 rs_write_int(savef
, count
);
766 for(n
= 0; n
< count
; n
++)
767 if (rs_write_string(savef
, s
[n
]) != 0)
774 rs_read_strings(int inf
, char **s
, int count
, int max
)
779 if (read_error
|| format_error
)
782 rs_read_int(inf
, &value
);
787 for(n
= 0; n
< count
; n
++)
788 if (rs_read_string(inf
, s
[n
], max
) != 0)
795 rs_read_new_strings(int inf
, char **s
, int count
)
800 if (read_error
|| format_error
)
803 rs_read_int(inf
, &value
);
808 for(n
= 0; n
< count
; n
++)
809 if (rs_read_new_string(inf
, &s
[n
]) != 0)
816 rs_write_string_index(FILE *savef
, char *master
[], int max
, const char *str
)
823 for(i
= 0; i
< max
; i
++)
824 if (str
== master
[i
])
825 return( rs_write_int(savef
, i
) );
827 return( rs_write_int(savef
,-1) );
831 rs_read_string_index(int inf
, char *master
[], int maxindex
, char **str
)
835 if (read_error
|| format_error
)
838 rs_read_int(inf
, &i
);
851 rs_write_str_t(FILE *savef
, str_t st
)
856 rs_write_short(savef
,st
.st_str
);
857 rs_write_short(savef
,st
.st_add
);
863 rs_read_str_t(int inf
, str_t
*st
)
865 if (read_error
|| format_error
)
868 rs_read_short(inf
,&st
->st_str
);
869 rs_read_short(inf
,&st
->st_add
);
875 rs_write_coord(FILE *savef
, coord c
)
880 rs_write_int(savef
, c
.x
);
881 rs_write_int(savef
, c
.y
);
887 rs_read_coord(int inf
, coord
*c
)
891 if (read_error
|| format_error
)
894 rs_read_int(inf
,&in
.x
);
895 rs_read_int(inf
,&in
.y
);
907 rs_write_window(FILE *savef
, WINDOW
*win
)
909 int row
,col
,height
,width
;
914 width
= getmaxx(win
);
915 height
= getmaxy(win
);
917 rs_write_marker(savef
,RSID_WINDOW
);
918 rs_write_int(savef
,height
);
919 rs_write_int(savef
,width
);
921 for(row
=0;row
<height
;row
++)
922 for(col
=0;col
<width
;col
++)
923 if (rs_write_int(savef
, mvwinch(win
,row
,col
)) != 0)
930 rs_read_window(int inf
, WINDOW
*win
)
932 int row
,col
,maxlines
,maxcols
,value
,width
,height
;
934 if (read_error
|| format_error
)
937 width
= getmaxx(win
);
938 height
= getmaxy(win
);
940 rs_read_marker(inf
, RSID_WINDOW
);
942 rs_read_int(inf
, &maxlines
);
943 rs_read_int(inf
, &maxcols
);
945 for(row
= 0; row
< maxlines
; row
++)
946 for(col
= 0; col
< maxcols
; col
++)
948 if (rs_read_int(inf
, &value
) != 0)
951 if ((row
< height
) && (col
< width
))
952 mvwaddch(win
,row
,col
,value
);
958 /******************************************************************************/
961 get_list_item(struct linked_list
*l
, int i
)
965 for(count
= 0; l
!= NULL
; count
++, l
= l
->l_next
)
973 find_list_ptr(struct linked_list
*l
, void *ptr
)
977 for(count
= 0; l
!= NULL
; count
++, l
= l
->l_next
)
978 if (l
->l_data
== ptr
)
985 list_size(struct linked_list
*l
)
989 for(count
= 0; l
!= NULL
; count
++, l
= l
->l_next
)
990 if (l
->l_data
== NULL
)
996 /******************************************************************************/
999 rs_write_stats(FILE *savef
, struct stats
*s
)
1004 rs_write_marker(savef
, RSID_STATS
);
1005 rs_write_str_t(savef
, s
->s_str
);
1006 rs_write_long(savef
, s
->s_exp
);
1007 rs_write_int(savef
, s
->s_lvl
);
1008 rs_write_int(savef
, s
->s_arm
);
1009 rs_write_int(savef
, s
->s_hpt
);
1010 rs_write_chars(savef
, s
->s_dmg
, sizeof(s
->s_dmg
));
1016 rs_read_stats(int inf
, struct stats
*s
)
1018 if (read_error
|| format_error
)
1021 rs_read_marker(inf
, RSID_STATS
);
1022 rs_read_str_t(inf
,&s
->s_str
);
1023 rs_read_long(inf
,&s
->s_exp
);
1024 rs_read_int(inf
,&s
->s_lvl
);
1025 rs_read_int(inf
,&s
->s_arm
);
1026 rs_read_int(inf
,&s
->s_hpt
);
1027 rs_read_chars(inf
,s
->s_dmg
,sizeof(s
->s_dmg
));
1033 rs_write_scrolls(FILE *savef
)
1040 for(i
= 0; i
< MAXSCROLLS
; i
++)
1042 rs_write_string(savef
,s_names
[i
]);
1043 rs_write_boolean(savef
,s_know
[i
]);
1044 rs_write_string(savef
,s_guess
[i
]);
1050 rs_read_scrolls(int inf
)
1054 if (read_error
|| format_error
)
1057 for(i
= 0; i
< MAXSCROLLS
; i
++)
1059 rs_read_new_string(inf
,&s_names
[i
]);
1060 rs_read_boolean(inf
,&s_know
[i
]);
1061 rs_read_new_string(inf
,&s_guess
[i
]);
1068 rs_write_potions(FILE *savef
)
1075 for(i
= 0; i
< MAXPOTIONS
; i
++)
1077 rs_write_string_index(savef
, rainbow
, cNCOLORS
, p_colors
[i
]);
1078 rs_write_boolean(savef
,p_know
[i
]);
1079 rs_write_string(savef
,p_guess
[i
]);
1086 rs_read_potions(int inf
)
1090 if (read_error
|| format_error
)
1093 for(i
= 0; i
< MAXPOTIONS
; i
++)
1095 rs_read_string_index(inf
, rainbow
, cNCOLORS
, &p_colors
[i
]);
1096 rs_read_boolean(inf
,&p_know
[i
]);
1097 rs_read_new_string(inf
,&p_guess
[i
]);
1104 rs_write_rings(FILE *savef
)
1111 for(i
= 0; i
< MAXRINGS
; i
++)
1113 rs_write_string_index(savef
, stones
, cNSTONES
, r_stones
[i
]);
1114 rs_write_boolean(savef
,r_know
[i
]);
1115 rs_write_string(savef
,r_guess
[i
]);
1122 rs_read_rings(int inf
)
1126 if (read_error
|| format_error
)
1129 for(i
= 0; i
< MAXRINGS
; i
++)
1131 rs_read_string_index(inf
, stones
, cNSTONES
, &r_stones
[i
]);
1132 rs_read_boolean(inf
,&r_know
[i
]);
1133 rs_read_new_string(inf
,&r_guess
[i
]);
1140 rs_write_sticks(FILE *savef
)
1147 for (i
= 0; i
< MAXSTICKS
; i
++)
1149 if (strcmp(ws_type
[i
],"staff") == 0)
1151 rs_write_int(savef
,0);
1152 rs_write_string_index(savef
, wood
, cNWOOD
, ws_made
[i
]);
1156 rs_write_int(savef
,1);
1157 rs_write_string_index(savef
, metal
, cNMETAL
, ws_made
[i
]);
1159 rs_write_boolean(savef
, ws_know
[i
]);
1160 rs_write_string(savef
, ws_guess
[i
]);
1167 rs_read_sticks(int inf
)
1169 int i
= 0, list
= 0;
1171 if (read_error
|| format_error
)
1174 for(i
= 0; i
< MAXSTICKS
; i
++)
1176 rs_read_int(inf
,&list
);
1180 rs_read_string_index(inf
, wood
, cNWOOD
, &ws_made
[i
]);
1181 ws_type
[i
] = "staff";
1185 rs_read_string_index(inf
, metal
, cNMETAL
, &ws_made
[i
]);
1186 ws_type
[i
] = "wand";
1188 rs_read_boolean(inf
, &ws_know
[i
]);
1189 rs_read_new_string(inf
, &ws_guess
[i
]);
1196 rs_write_daemons(FILE *savef
, struct delayed_action
*d_list
, int count
)
1204 rs_write_marker(savef
, RSID_DAEMONS
);
1205 rs_write_int(savef
, count
);
1207 for(i
= 0; i
< count
; i
++)
1209 if (d_list
[i
].d_func
== rollwand
)
1211 else if (d_list
[i
].d_func
== doctor
)
1213 else if (d_list
[i
].d_func
== stomach
)
1215 else if (d_list
[i
].d_func
== runners
)
1217 else if (d_list
[i
].d_func
== swander
)
1219 else if (d_list
[i
].d_func
== nohaste
)
1221 else if (d_list
[i
].d_func
== unconfuse
)
1223 else if (d_list
[i
].d_func
== unsee
)
1225 else if (d_list
[i
].d_func
== sight
)
1227 else if (d_list
[i
].d_func
== NULL
)
1232 rs_write_int(savef
, d_list
[i
].d_type
);
1233 rs_write_int(savef
, func
);
1234 rs_write_int(savef
, d_list
[i
].d_arg
);
1235 rs_write_int(savef
, d_list
[i
].d_time
);
1242 rs_read_daemons(int inf
, struct delayed_action
*d_list
, int count
)
1248 if (read_error
|| format_error
)
1251 rs_read_marker(inf
, RSID_DAEMONS
);
1252 rs_read_int(inf
, &value
);
1255 format_error
= TRUE
;
1257 for(i
=0; i
< count
; i
++)
1260 rs_read_int(inf
, &d_list
[i
].d_type
);
1261 rs_read_int(inf
, &func
);
1262 rs_read_int(inf
, &d_list
[i
].d_arg
);
1263 rs_read_int(inf
, &d_list
[i
].d_time
);
1267 case 1: d_list
[i
].d_func
= rollwand
;
1269 case 2: d_list
[i
].d_func
= doctor
;
1271 case 3: d_list
[i
].d_func
= stomach
;
1273 case 4: d_list
[i
].d_func
= runners
;
1275 case 5: d_list
[i
].d_func
= swander
;
1277 case 6: d_list
[i
].d_func
= nohaste
;
1279 case 7: d_list
[i
].d_func
= unconfuse
;
1281 case 8: d_list
[i
].d_func
= unsee
;
1283 case 9: d_list
[i
].d_func
= sight
;
1285 default:d_list
[i
].d_func
= NULL
;
1290 if (d_list
[i
].d_func
== NULL
)
1292 d_list
[i
].d_type
= 0;
1293 d_list
[i
].d_arg
= 0;
1294 d_list
[i
].d_time
= 0;
1301 rs_write_trap(FILE *savef
, struct trap
*trap
)
1306 rs_write_coord(savef
, trap
->tr_pos
);
1307 rs_write_char(savef
, trap
->tr_type
);
1308 rs_write_int(savef
, trap
->tr_flags
);
1314 rs_read_trap(int inf
, struct trap
*trap
)
1316 if (read_error
|| format_error
)
1319 rs_read_coord(inf
,&trap
->tr_pos
);
1320 rs_read_char(inf
,&trap
->tr_type
);
1321 rs_read_int(inf
,&trap
->tr_flags
);
1327 rs_write_traps(FILE *savef
, struct trap t
[], int count
)
1334 rs_write_marker(savef
, RSID_MONSTERS
);
1335 rs_write_int(savef
, count
);
1337 for(n
= 0; n
< count
; n
++)
1338 rs_write_trap(savef
, &t
[n
]);
1344 rs_read_traps(int inf
, struct trap
*t
, int count
)
1346 int value
= 0, n
= 0;
1348 if (read_error
|| format_error
)
1351 rs_read_marker(inf
, RSID_MONSTERS
);
1353 rs_read_int(inf
,&value
);
1356 format_error
= TRUE
;
1358 for(n
= 0; n
< value
; n
++)
1359 rs_read_trap(inf
,&t
[n
]);
1365 rs_write_room(FILE *savef
, struct room
*r
)
1370 rs_write_coord(savef
, r
->r_pos
);
1371 rs_write_coord(savef
, r
->r_max
);
1372 rs_write_coord(savef
, r
->r_gold
);
1373 rs_write_int(savef
, r
->r_goldval
);
1374 rs_write_int(savef
, r
->r_flags
);
1375 rs_write_int(savef
, r
->r_nexits
);
1376 rs_write_coord(savef
, r
->r_exit
[0]);
1377 rs_write_coord(savef
, r
->r_exit
[1]);
1378 rs_write_coord(savef
, r
->r_exit
[2]);
1379 rs_write_coord(savef
, r
->r_exit
[3]);
1385 rs_read_room(int inf
, struct room
*r
)
1387 if (read_error
|| format_error
)
1390 rs_read_coord(inf
,&r
->r_pos
);
1391 rs_read_coord(inf
,&r
->r_max
);
1392 rs_read_coord(inf
,&r
->r_gold
);
1393 rs_read_int(inf
,&r
->r_goldval
);
1394 rs_read_int(inf
,&r
->r_flags
);
1395 rs_read_int(inf
,&r
->r_nexits
);
1396 rs_read_coord(inf
,&r
->r_exit
[0]);
1397 rs_read_coord(inf
,&r
->r_exit
[1]);
1398 rs_read_coord(inf
,&r
->r_exit
[2]);
1399 rs_read_coord(inf
,&r
->r_exit
[3]);
1405 rs_write_rooms(FILE *savef
, struct room r
[], int count
)
1412 rs_write_int(savef
, count
);
1414 for(n
= 0; n
< count
; n
++)
1415 rs_write_room(savef
, &r
[n
]);
1421 rs_read_rooms(int inf
, struct room
*r
, int count
)
1423 int value
= 0, n
= 0;
1425 if (read_error
|| format_error
)
1428 rs_read_int(inf
,&value
);
1431 format_error
= TRUE
;
1433 for(n
= 0; n
< value
; n
++)
1434 rs_read_room(inf
,&r
[n
]);
1440 rs_write_room_reference(FILE *savef
, struct room
*rp
)
1447 for (i
= 0; i
< MAXROOMS
; i
++)
1448 if (&rooms
[i
] == rp
)
1451 rs_write_int(savef
, room
);
1457 rs_read_room_reference(int inf
, struct room
**rp
)
1461 if (read_error
|| format_error
)
1464 rs_read_int(inf
, &i
);
1472 rs_write_object(FILE *savef
, struct object
*o
)
1477 rs_write_marker(savef
, RSID_OBJECT
);
1478 rs_write_int(savef
, o
->o_type
);
1479 rs_write_coord(savef
, o
->o_pos
);
1480 rs_write_char(savef
, o
->o_launch
);
1481 rs_write_chars(savef
, o
->o_damage
, sizeof(o
->o_damage
));
1482 rs_write_chars(savef
, o
->o_hurldmg
, sizeof(o
->o_damage
));
1483 rs_write_int(savef
, o
->o_count
);
1484 rs_write_int(savef
, o
->o_which
);
1485 rs_write_int(savef
, o
->o_hplus
);
1486 rs_write_int(savef
, o
->o_dplus
);
1487 rs_write_int(savef
, o
->o_ac
);
1488 rs_write_int(savef
, o
->o_flags
);
1489 rs_write_int(savef
, o
->o_group
);
1494 rs_read_object(int inf
, struct object
*o
)
1496 if (read_error
|| format_error
)
1499 rs_read_marker(inf
, RSID_OBJECT
);
1500 rs_read_int(inf
, &o
->o_type
);
1501 rs_read_coord(inf
, &o
->o_pos
);
1502 rs_read_char(inf
, &o
->o_launch
);
1503 rs_read_chars(inf
, o
->o_damage
, sizeof(o
->o_damage
));
1504 rs_read_chars(inf
, o
->o_hurldmg
, sizeof(o
->o_hurldmg
));
1505 rs_read_int(inf
, &o
->o_count
);
1506 rs_read_int(inf
, &o
->o_which
);
1507 rs_read_int(inf
, &o
->o_hplus
);
1508 rs_read_int(inf
, &o
->o_hplus
);
1509 rs_read_int(inf
,&o
->o_ac
);
1510 rs_read_int(inf
,&o
->o_flags
);
1511 rs_read_int(inf
,&o
->o_group
);
1517 rs_write_object_list(FILE *savef
, struct linked_list
*l
)
1522 rs_write_marker(savef
, RSID_OBJECTLIST
);
1523 rs_write_int(savef
, list_size(l
));
1525 for( ;l
!= NULL
; l
= l
->l_next
)
1526 rs_write_object(savef
, (struct object
*) l
->l_data
);
1532 rs_read_object_list(int inf
, struct linked_list
**list
)
1535 struct linked_list
*l
= NULL
, *previous
= NULL
, *head
= NULL
;
1537 if (read_error
|| format_error
)
1540 rs_read_marker(inf
, RSID_OBJECTLIST
);
1541 rs_read_int(inf
, &cnt
);
1543 for (i
= 0; i
< cnt
; i
++)
1545 l
= new_item(sizeof(struct object
));
1547 memset(l
->l_data
,0,sizeof(struct object
));
1549 l
->l_prev
= previous
;
1551 if (previous
!= NULL
)
1552 previous
->l_next
= l
;
1554 rs_read_object(inf
,(struct object
*) l
->l_data
);
1556 if (previous
== NULL
)
1571 rs_write_object_reference(FILE *savef
, struct linked_list
*list
,
1572 struct object
*item
)
1579 i
= find_list_ptr(list
, item
);
1581 rs_write_int(savef
, i
);
1587 rs_read_object_reference(int inf
, struct linked_list
*list
,
1588 struct object
**item
)
1592 if (read_error
|| format_error
)
1595 rs_read_int(inf
, &i
);
1597 *item
= get_list_item(list
,i
);
1603 find_room_coord(struct room
*rmlist
, coord
*c
, int n
)
1607 for(i
= 0; i
< n
; i
++)
1608 if(&rmlist
[i
].r_gold
== c
)
1615 find_thing_coord(struct linked_list
*monlist
, coord
*c
)
1617 struct linked_list
*mitem
;
1621 for(mitem
= monlist
; mitem
!= NULL
; mitem
= mitem
->l_next
)
1623 tp
= THINGPTR(mitem
);
1625 if (c
== &tp
->t_pos
)
1635 find_object_coord(struct linked_list
*objlist
, coord
*c
)
1637 struct linked_list
*oitem
;
1641 for(oitem
= objlist
; oitem
!= NULL
; oitem
= oitem
->l_next
)
1643 obj
= OBJPTR(oitem
);
1645 if (c
== &obj
->o_pos
)
1655 rs_write_thing(FILE *savef
, struct thing
*t
)
1662 rs_write_marker(savef
, RSID_THING
);
1666 rs_write_int(savef
, 0);
1670 rs_write_int(savef
, 1);
1671 rs_write_coord(savef
, t
->t_pos
);
1672 rs_write_boolean(savef
, t
->t_turn
);
1673 rs_write_char(savef
, t
->t_type
);
1674 rs_write_char(savef
, t
->t_disguise
);
1675 rs_write_char(savef
, t
->t_oldch
);
1680 0,1: location of hero
1681 1,i: location of a thing (monster)
1682 2,i: location of an object
1683 3,i: location of gold in a room
1685 We need to remember what we are chasing rather than
1686 the current location of what we are chasing.
1689 if (t
->t_dest
== &hero
)
1691 rs_write_int(savef
,0);
1692 rs_write_int(savef
,1);
1694 else if (t
->t_dest
!= NULL
)
1696 i
= find_thing_coord(mlist
, t
->t_dest
);
1700 rs_write_int(savef
,1);
1701 rs_write_int(savef
,i
);
1705 i
= find_object_coord(lvl_obj
, t
->t_dest
);
1709 rs_write_int(savef
,2);
1710 rs_write_int(savef
,i
);
1714 i
= find_room_coord(rooms
, t
->t_dest
, MAXROOMS
);
1718 rs_write_int(savef
,3);
1719 rs_write_int(savef
,i
);
1723 rs_write_int(savef
, 0);
1724 rs_write_int(savef
,1); /* chase the hero anyway */
1731 rs_write_int(savef
,0);
1732 rs_write_int(savef
,0);
1735 rs_write_short(savef
, t
->t_flags
);
1736 rs_write_stats(savef
, &t
->t_stats
);
1737 rs_write_object_list(savef
, t
->t_pack
);
1743 rs_read_thing(int inf
, struct thing
*t
)
1745 int listid
= 0, index
= -1;
1746 struct linked_list
*item
;
1748 if (read_error
|| format_error
)
1751 rs_read_marker(inf
, RSID_THING
);
1753 rs_read_int(inf
, &index
);
1758 rs_read_coord(inf
,&t
->t_pos
);
1759 rs_read_boolean(inf
,&t
->t_turn
);
1760 rs_read_char(inf
,&t
->t_type
);
1761 rs_read_char(inf
,&t
->t_disguise
);
1762 rs_read_char(inf
,&t
->t_oldch
);
1765 t_dest can be (listid,index):
1767 0,1: location of hero
1768 1,i: location of a thing (monster)
1769 2,i: location of an object
1770 3,i: location of gold in a room
1772 We need to remember what we are chasing rather than
1773 the current location of what we are chasing.
1776 rs_read_int(inf
, &listid
);
1777 rs_read_int(inf
, &index
);
1780 if (listid
== 0) /* hero or NULL */
1787 else if (listid
== 1) /* monster/thing */
1790 t
->t_reserved
= index
;
1792 else if (listid
== 2) /* object */
1796 item
= get_list_item(lvl_obj
,index
);
1801 t
->t_dest
= &obj
->o_pos
;
1804 else if (listid
== 3) /* gold */
1806 t
->t_dest
= &rooms
[index
].r_gold
;
1811 rs_read_short(inf
,&t
->t_flags
);
1812 rs_read_stats(inf
,&t
->t_stats
);
1813 rs_read_object_list(inf
,&t
->t_pack
);
1819 rs_fix_thing(struct thing
*t
)
1821 struct linked_list
*item
;
1824 if (t
->t_reserved
< 0)
1827 item
= get_list_item(mlist
,t
->t_reserved
);
1831 tp
= THINGPTR(item
);
1832 t
->t_dest
= &tp
->t_pos
;
1837 rs_write_thing_list(FILE *savef
, struct linked_list
*l
)
1844 rs_write_marker(savef
, RSID_MONSTERLIST
);
1848 rs_write_int(savef
, cnt
);
1854 rs_write_thing(savef
, (struct thing
*)l
->l_data
);
1862 rs_read_thing_list(int inf
, struct linked_list
**list
)
1865 struct linked_list
*l
= NULL
, *previous
= NULL
, *head
= NULL
;
1867 if (read_error
|| format_error
)
1870 rs_read_marker(inf
, RSID_MONSTERLIST
);
1872 rs_read_int(inf
, &cnt
);
1874 for (i
= 0; i
< cnt
; i
++)
1876 l
= new_item(sizeof(struct thing
));
1878 l
->l_prev
= previous
;
1880 if (previous
!= NULL
)
1881 previous
->l_next
= l
;
1883 rs_read_thing(inf
,(struct thing
*)l
->l_data
);
1885 if (previous
== NULL
)
1900 rs_fix_thing_list(struct linked_list
*list
)
1902 struct linked_list
*item
;
1904 for(item
= list
; item
!= NULL
; item
= item
->l_next
)
1905 rs_fix_thing(THINGPTR(item
));
1909 rs_fix_magic_items(struct magic_item
*mi
, int count
)
1913 for (i
= 0; i
< count
; i
++)
1915 mi
[i
].mi_prob
+= mi
[i
-1].mi_prob
;
1919 rs_fix_monsters(struct monster monsters
[26])
1921 sprintf(monsters
['F'-'A'].m_stats
.s_dmg
,"%dd1",fung_hit
);
1925 rs_save_file(FILE *savef
)
1930 rs_write_thing(savef
, &player
);
1931 rs_write_object_list(savef
, lvl_obj
);
1932 rs_write_thing_list(savef
, mlist
);
1933 rs_write_traps(savef
, traps
, MAXTRAPS
);
1934 rs_write_rooms(savef
, rooms
, MAXROOMS
);
1935 rs_write_room_reference(savef
, oldrp
);
1936 rs_write_stats(savef
,&max_stats
);
1937 rs_write_object_reference(savef
, player
.t_pack
, cur_weapon
);
1938 rs_write_object_reference(savef
, player
.t_pack
, cur_armor
);
1939 rs_write_object_reference(savef
, player
.t_pack
, cur_ring
[0]);
1940 rs_write_object_reference(savef
, player
.t_pack
, cur_ring
[1]);
1941 rs_write_int(savef
, level
);
1942 rs_write_int(savef
, purse
);
1943 rs_write_int(savef
, mpos
);
1944 rs_write_int(savef
, ntraps
);
1945 rs_write_int(savef
, no_move
);
1946 rs_write_int(savef
, no_command
);
1947 rs_write_int(savef
, inpack
);
1948 rs_write_int(savef
, max_hp
);
1949 rs_write_int(savef
, total
);
1950 rs_write_int(savef
, lastscore
);
1951 rs_write_int(savef
, no_food
);
1952 rs_write_int(savef
, seed
);
1953 rs_write_int(savef
, count
);
1954 rs_write_int(savef
, dnum
);
1955 rs_write_int(savef
, fung_hit
);
1956 rs_write_int(savef
, quiet
);
1957 rs_write_int(savef
, max_level
);
1958 rs_write_int(savef
, food_left
);
1959 rs_write_int(savef
, group
);
1960 rs_write_int(savef
, hungry_state
);
1961 rs_write_char(savef
, take
);
1962 rs_write_char(savef
, runch
);
1963 rs_write_scrolls(savef
);
1964 rs_write_potions(savef
);
1965 rs_write_rings(savef
);
1966 rs_write_sticks(savef
);
1967 rs_write_chars(savef
,whoami
,80);
1968 rs_write_chars(savef
,fruit
,80);
1969 rs_write_window(savef
, cw
);
1970 rs_write_window(savef
, mw
);
1971 rs_write_window(savef
, stdscr
);
1972 rs_write_boolean(savef
, running
);
1973 rs_write_boolean(savef
, playing
);
1974 rs_write_boolean(savef
, wizard
);
1975 rs_write_boolean(savef
, after
);
1976 rs_write_boolean(savef
, notify
);
1977 rs_write_boolean(savef
, fight_flush
);
1978 rs_write_boolean(savef
, terse
);
1979 rs_write_boolean(savef
, door_stop
);
1980 rs_write_boolean(savef
, jump
);
1981 rs_write_boolean(savef
, slow_invent
);
1982 rs_write_boolean(savef
, firstmove
);
1983 rs_write_boolean(savef
, waswizard
);
1984 rs_write_boolean(savef
, askme
);
1985 rs_write_boolean(savef
, amulet
);
1986 rs_write_boolean(savef
, in_shell
);
1987 rs_write_coord(savef
, oldpos
);
1988 rs_write_coord(savef
, delta
);
1989 rs_write_coord(savef
, ch_ret
); /* chase.c */
1990 rs_write_daemons(savef
, &d_list
[0], 20); /* daemon.c */
1991 rs_write_int(savef
,between
); /* daemons.c */
1992 rs_write_int(savef
,num_checks
); /* main.c */
1993 rs_write_chars(savef
,lvl_mons
,sizeof(lvl_mons
)); /* monsters.c */
1994 rs_write_chars(savef
,wand_mons
,sizeof(wand_mons
)); /* monsters.c */
2000 rs_restore_file(int inf
)
2002 if (read_error
|| format_error
)
2005 rs_read_thing(inf
, &player
);
2006 rs_read_object_list(inf
, &lvl_obj
);
2007 rs_read_thing_list(inf
, &mlist
);
2008 rs_fix_thing(&player
);
2009 rs_fix_thing_list(mlist
);
2010 rs_read_traps(inf
, traps
, MAXTRAPS
);
2011 rs_read_rooms(inf
, rooms
, MAXROOMS
);
2012 rs_read_room_reference(inf
, &oldrp
);
2013 rs_read_stats(inf
,&max_stats
);
2014 rs_read_object_reference(inf
, player
.t_pack
, &cur_weapon
);
2015 rs_read_object_reference(inf
, player
.t_pack
, &cur_armor
);
2016 rs_read_object_reference(inf
, player
.t_pack
, &cur_ring
[0]);
2017 rs_read_object_reference(inf
, player
.t_pack
, &cur_ring
[1]);
2018 rs_fix_magic_items(things
,NUMTHINGS
);
2019 rs_fix_magic_items(s_magic
,MAXSCROLLS
);
2020 rs_fix_magic_items(p_magic
,MAXPOTIONS
);
2021 rs_fix_magic_items(r_magic
,MAXRINGS
);
2022 rs_fix_magic_items(ws_magic
,MAXSTICKS
);
2023 rs_read_int(inf
, &level
);
2024 rs_read_int(inf
, &purse
);
2025 rs_read_int(inf
, &mpos
);
2026 rs_read_int(inf
, &ntraps
);
2027 rs_read_int(inf
, &no_move
);
2028 rs_read_int(inf
, &no_command
);
2029 rs_read_int(inf
, &inpack
);
2030 rs_read_int(inf
, &max_hp
);
2031 rs_read_int(inf
, &total
);
2032 rs_read_int(inf
, &lastscore
);
2033 rs_read_int(inf
, &no_food
);
2034 rs_read_int(inf
, &seed
);
2035 rs_read_int(inf
, &count
);
2036 rs_read_int(inf
, &dnum
);
2037 rs_read_int(inf
, &fung_hit
);
2038 rs_read_int(inf
, &quiet
);
2039 rs_read_int(inf
, &max_level
);
2040 rs_read_int(inf
, &food_left
);
2041 rs_read_int(inf
, &group
);
2042 rs_read_int(inf
, &hungry_state
);
2043 rs_read_char(inf
, &take
);
2044 rs_read_char(inf
, &runch
);
2045 rs_read_scrolls(inf
);
2046 rs_read_potions(inf
);
2048 rs_read_sticks(inf
);
2049 rs_read_chars(inf
,whoami
,80);
2050 rs_read_chars(inf
,fruit
,80);
2051 rs_read_window(inf
, cw
);
2052 rs_read_window(inf
, mw
);
2053 rs_read_window(inf
, stdscr
);
2054 rs_read_boolean(inf
, &running
);
2055 rs_read_boolean(inf
, &playing
);
2056 rs_read_boolean(inf
, &wizard
);
2057 rs_read_boolean(inf
, &after
);
2058 rs_read_boolean(inf
, ¬ify
);
2059 rs_read_boolean(inf
, &fight_flush
);
2060 rs_read_boolean(inf
, &terse
);
2061 rs_read_boolean(inf
, &door_stop
);
2062 rs_read_boolean(inf
, &jump
);
2063 rs_read_boolean(inf
, &slow_invent
);
2064 rs_read_boolean(inf
, &firstmove
);
2065 rs_read_boolean(inf
, &waswizard
);
2066 rs_read_boolean(inf
, &askme
);
2067 rs_read_boolean(inf
, &amulet
);
2068 rs_read_boolean(inf
, &in_shell
);
2069 rs_read_coord(inf
,&oldpos
);
2070 rs_read_coord(inf
,&delta
);
2071 rs_read_coord(inf
, &ch_ret
); /* chase.c */
2072 rs_read_daemons(inf
, d_list
, 20); /* daemon.c */
2073 rs_read_int(inf
,&between
); /* daemons.c */
2074 rs_read_int(inf
,&num_checks
); /* main.c */
2075 rs_read_chars(inf
, lvl_mons
, sizeof(lvl_mons
)); /* monsters.c */
2076 rs_read_chars(inf
, wand_mons
, sizeof(wand_mons
)); /* monsters.c */
2077 rs_fix_monsters(monsters
);