Monsters now have an orientation which can be inclined to like or dislike the player...
[rogue-pphs.git] / state.c
1 /*
2 state.c - Portable Rogue Save State Code
3
4 Copyright (C) 1999, 2000, 2005, 2006 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_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
60
61 #include <curses.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include "rogue.h"
66
67 #define READSTAT (format_error || read_error )
68 #define WRITESTAT (write_error)
69
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 )
75
76 int
77 rs_write(FILE *savef, void *ptr, size_t size)
78 {
79 if (write_error)
80 return(WRITESTAT);
81
82 if (encwrite(ptr, size, savef) != size)
83 write_error = 1;
84
85 return(WRITESTAT);
86 }
87
88 int
89 rs_read(int inf, void *ptr, size_t size)
90 {
91 if (read_error || format_error)
92 return(READSTAT);
93
94 if (encread(ptr, size, inf) != size)
95 read_error = 1;
96
97 return(READSTAT);
98 }
99
100 int
101 rs_write_char(FILE *savef, char c)
102 {
103 if (write_error)
104 return(WRITESTAT);
105
106 rs_write(savef, &c, 1);
107
108 return(WRITESTAT);
109 }
110
111 int
112 rs_read_char(int inf, char *c)
113 {
114 if (read_error || format_error)
115 return(READSTAT);
116
117 rs_read(inf, c, 1);
118
119 return(READSTAT);
120 }
121
122 int
123 rs_write_chars(FILE *savef, char *c, int count)
124 {
125 if (write_error)
126 return(WRITESTAT);
127
128 rs_write_int(savef, count);
129 rs_write(savef, c, count);
130
131 return(WRITESTAT);
132 }
133
134 int
135 rs_read_chars(int inf, char *i, int count)
136 {
137 int value = 0;
138
139 if (read_error || format_error)
140 return(READSTAT);
141
142 rs_read_int(inf, &value);
143
144 if (value != count)
145 format_error = TRUE;
146
147 rs_read(inf, i, count);
148
149 return(READSTAT);
150 }
151
152 int
153 rs_write_int(FILE *savef, int c)
154 {
155 unsigned char bytes[4];
156 unsigned char *buf = (unsigned char *) &c;
157
158 if (write_error)
159 return(WRITESTAT);
160
161 if (big_endian)
162 {
163 bytes[3] = buf[0];
164 bytes[2] = buf[1];
165 bytes[1] = buf[2];
166 bytes[0] = buf[3];
167 buf = bytes;
168 }
169
170 rs_write(savef, buf, 4);
171
172 return(WRITESTAT);
173 }
174
175 int
176 rs_read_int(int inf, int *i)
177 {
178 unsigned char bytes[4];
179 int input = 0;
180 unsigned char *buf = (unsigned char *)&input;
181
182 if (read_error || format_error)
183 return(READSTAT);
184
185 rs_read(inf, &input, 4);
186
187 if (big_endian)
188 {
189 bytes[3] = buf[0];
190 bytes[2] = buf[1];
191 bytes[1] = buf[2];
192 bytes[0] = buf[3];
193 buf = bytes;
194 }
195
196 *i = *((int *) buf);
197
198 return(READSTAT);
199 }
200
201 int
202 rs_write_ints(FILE *savef, int *c, int count)
203 {
204 int n = 0;
205
206 if (write_error)
207 return(WRITESTAT);
208
209 rs_write_int(savef, count);
210
211 for(n = 0; n < count; n++)
212 if( rs_write_int(savef,c[n]) != 0)
213 break;
214
215 return(WRITESTAT);
216 }
217
218 int
219 rs_read_ints(int inf, int *i, int count)
220 {
221 int n, value;
222
223 if (read_error || format_error)
224 return(READSTAT);
225
226 rs_read_int(inf,&value);
227
228 if (value != count)
229 format_error = TRUE;
230
231 for(n = 0; n < count; n++)
232 if (rs_read_int(inf, &i[n]) != 0)
233 break;
234
235 return(READSTAT);
236 }
237
238 int
239 rs_write_boolean(FILE *savef, bool c)
240 {
241 unsigned char buf = (c == 0) ? 0 : 1;
242
243 if (write_error)
244 return(WRITESTAT);
245
246 rs_write(savef, &buf, 1);
247
248 return(WRITESTAT);
249 }
250
251 int
252 rs_read_boolean(int inf, bool *i)
253 {
254 unsigned char buf = 0;
255
256 if (read_error || format_error)
257 return(READSTAT);
258
259 rs_read(inf, &buf, 1);
260
261 *i = (buf != 0);
262
263 return(READSTAT);
264 }
265
266 int
267 rs_write_booleans(FILE *savef, bool *c, int count)
268 {
269 int n = 0;
270
271 if (write_error)
272 return(WRITESTAT);
273
274 rs_write_int(savef, count);
275
276 for(n = 0; n < count; n++)
277 if (rs_write_boolean(savef, c[n]) != 0)
278 break;
279
280 return(WRITESTAT);
281 }
282
283 int
284 rs_read_booleans(int inf, bool *i, int count)
285 {
286 int n = 0, value = 0;
287
288 if (read_error || format_error)
289 return(READSTAT);
290
291 rs_read_int(inf,&value);
292
293 if (value != count)
294 format_error = TRUE;
295
296 for(n = 0; n < count; n++)
297 if (rs_read_boolean(inf, &i[n]) != 0)
298 break;
299
300 return(READSTAT);
301 }
302
303 int
304 rs_write_short(FILE *savef, short c)
305 {
306 unsigned char bytes[2];
307 unsigned char *buf = (unsigned char *) &c;
308
309 if (write_error)
310 return(WRITESTAT);
311
312 if (big_endian)
313 {
314 bytes[1] = buf[0];
315 bytes[0] = buf[1];
316 buf = bytes;
317 }
318
319 rs_write(savef, buf, 2);
320
321 return(WRITESTAT);
322 }
323
324 int
325 rs_read_short(int inf, short *i)
326 {
327 unsigned char bytes[2];
328 short input;
329 unsigned char *buf = (unsigned char *)&input;
330
331 if (read_error || format_error)
332 return(READSTAT);
333
334 rs_read(inf, &input, 2);
335
336 if (big_endian)
337 {
338 bytes[1] = buf[0];
339 bytes[0] = buf[1];
340 buf = bytes;
341 }
342
343 *i = *((short *) buf);
344
345 return(READSTAT);
346 }
347
348 int
349 rs_write_shorts(FILE *savef, short *c, int count)
350 {
351 int n = 0;
352
353 if (write_error)
354 return(WRITESTAT);
355
356 rs_write_int(savef, count);
357
358 for(n = 0; n < count; n++)
359 if (rs_write_short(savef, c[n]) != 0)
360 break;
361
362 return(WRITESTAT);
363 }
364
365 int
366 rs_read_shorts(int inf, short *i, int count)
367 {
368 int n = 0, value = 0;
369
370 if (read_error || format_error)
371 return(READSTAT);
372
373 rs_read_int(inf,&value);
374
375 if (value != count)
376 format_error = TRUE;
377
378 for(n = 0; n < value; n++)
379 if (rs_read_short(inf, &i[n]) != 0)
380 break;
381
382 return(READSTAT);
383 }
384
385 int
386 rs_write_ushort(FILE *savef, unsigned short c)
387 {
388 unsigned char bytes[2];
389 unsigned char *buf = (unsigned char *) &c;
390
391 if (write_error)
392 return(WRITESTAT);
393
394 if (big_endian)
395 {
396 bytes[1] = buf[0];
397 bytes[0] = buf[1];
398 buf = bytes;
399 }
400
401 rs_write(savef, buf, 2);
402
403 return(WRITESTAT);
404 }
405
406 int
407 rs_read_ushort(int inf, unsigned short *i)
408 {
409 unsigned char bytes[2];
410 unsigned short input;
411 unsigned char *buf = (unsigned char *)&input;
412
413 if (read_error || format_error)
414 return(READSTAT);
415
416 rs_read(inf, &input, 2);
417
418 if (big_endian)
419 {
420 bytes[1] = buf[0];
421 bytes[0] = buf[1];
422 buf = bytes;
423 }
424
425 *i = *((unsigned short *) buf);
426
427 return(READSTAT);
428 }
429
430 int
431 rs_write_uint(FILE *savef, unsigned int c)
432 {
433 unsigned char bytes[4];
434 unsigned char *buf = (unsigned char *) &c;
435
436 if (write_error)
437 return(WRITESTAT);
438
439 if (big_endian)
440 {
441 bytes[3] = buf[0];
442 bytes[2] = buf[1];
443 bytes[1] = buf[2];
444 bytes[0] = buf[3];
445 buf = bytes;
446 }
447
448 rs_write(savef, buf, 4);
449
450 return(WRITESTAT);
451 }
452
453 int
454 rs_read_uint(int inf, unsigned int *i)
455 {
456 unsigned char bytes[4];
457 int input;
458 unsigned char *buf = (unsigned char *)&input;
459
460 if (read_error || format_error)
461 return(READSTAT);
462
463 rs_read(inf, &input, 4);
464
465 if (big_endian)
466 {
467 bytes[3] = buf[0];
468 bytes[2] = buf[1];
469 bytes[1] = buf[2];
470 bytes[0] = buf[3];
471 buf = bytes;
472 }
473
474 *i = *((unsigned int *) buf);
475
476 return(READSTAT);
477 }
478
479 int
480 rs_write_long(FILE *savef, long c)
481 {
482 int c2;
483 unsigned char bytes[4];
484 unsigned char *buf = (unsigned char *)&c;
485
486 if (write_error)
487 return(WRITESTAT);
488
489 if (sizeof(long) == 8)
490 {
491 c2 = c;
492 buf = (unsigned char *) &c2;
493 }
494
495 if (big_endian)
496 {
497 bytes[3] = buf[0];
498 bytes[2] = buf[1];
499 bytes[1] = buf[2];
500 bytes[0] = buf[3];
501 buf = bytes;
502 }
503
504 rs_write(savef, buf, 4);
505
506 return(WRITESTAT);
507 }
508
509 int
510 rs_read_long(int inf, long *i)
511 {
512 unsigned char bytes[4];
513 long input;
514 unsigned char *buf = (unsigned char *) &input;
515
516 if (read_error || format_error)
517 return(READSTAT);
518
519 rs_read(inf, &input, 4);
520
521 if (big_endian)
522 {
523 bytes[3] = buf[0];
524 bytes[2] = buf[1];
525 bytes[1] = buf[2];
526 bytes[0] = buf[3];
527 buf = bytes;
528 }
529
530 *i = *((long *) buf);
531
532 return(READSTAT);
533 }
534
535 int
536 rs_write_longs(FILE *savef, long *c, int count)
537 {
538 int n = 0;
539
540 if (write_error)
541 return(WRITESTAT);
542
543 rs_write_int(savef,count);
544
545 for(n = 0; n < count; n++)
546 rs_write_long(savef, c[n]);
547
548 return(WRITESTAT);
549 }
550
551 int
552 rs_read_longs(int inf, long *i, int count)
553 {
554 int n = 0, value = 0;
555
556 if (read_error || format_error)
557 return(READSTAT);
558
559 rs_read_int(inf,&value);
560
561 if (value != count)
562 format_error = TRUE;
563
564 for(n = 0; n < value; n++)
565 if (rs_read_long(inf, &i[n]) != 0)
566 break;
567
568 return(READSTAT);
569 }
570
571 int
572 rs_write_ulong(FILE *savef, unsigned long c)
573 {
574 unsigned int c2;
575 unsigned char bytes[4];
576 unsigned char *buf = (unsigned char *)&c;
577
578 if (write_error)
579 return(WRITESTAT);
580
581 if ( (sizeof(long) == 8) && (sizeof(int) == 4) )
582 {
583 c2 = c;
584 buf = (unsigned char *) &c2;
585 }
586
587 if (big_endian)
588 {
589 bytes[3] = buf[0];
590 bytes[2] = buf[1];
591 bytes[1] = buf[2];
592 bytes[0] = buf[3];
593 buf = bytes;
594 }
595
596 rs_write(savef, buf, 4);
597
598 return(WRITESTAT);
599 }
600
601 int
602 rs_read_ulong(int inf, unsigned long *i)
603 {
604 unsigned char bytes[4];
605 unsigned long input;
606 unsigned char *buf = (unsigned char *) &input;
607
608 if (read_error || format_error)
609 return(READSTAT);
610
611 rs_read(inf, &input, 4);
612
613 if (big_endian)
614 {
615 bytes[3] = buf[0];
616 bytes[2] = buf[1];
617 bytes[1] = buf[2];
618 bytes[0] = buf[3];
619 buf = bytes;
620 }
621
622 *i = *((unsigned long *) buf);
623
624 return(READSTAT);
625 }
626
627 int
628 rs_write_ulongs(FILE *savef, unsigned long *c, int count)
629 {
630 int n = 0;
631
632 if (write_error)
633 return(WRITESTAT);
634
635 rs_write_int(savef,count);
636
637 for(n = 0; n < count; n++)
638 if (rs_write_ulong(savef,c[n]) != 0)
639 break;
640
641 return(WRITESTAT);
642 }
643
644 int
645 rs_read_ulongs(int inf, unsigned long *i, int count)
646 {
647 int n = 0, value = 0;
648
649 if (read_error || format_error)
650 return(READSTAT);
651
652 rs_read_int(inf,&value);
653
654 if (value != count)
655 format_error = TRUE;
656
657 for(n = 0; n < count; n++)
658 if (rs_read_ulong(inf, &i[n]) != 0)
659 break;
660
661 return(READSTAT);
662 }
663
664 int
665 rs_write_marker(FILE *savef, int id)
666 {
667 if (write_error)
668 return(WRITESTAT);
669
670 rs_write_int(savef, id);
671
672 return(WRITESTAT);
673 }
674
675 int
676 rs_read_marker(int inf, int id)
677 {
678 int nid;
679
680 if (read_error || format_error)
681 return(READSTAT);
682
683 if (rs_read_int(inf, &nid) == 0)
684 if (id != nid)
685 format_error = 1;
686
687 return(READSTAT);
688 }
689
690
691
692 /******************************************************************************/
693
694 int
695 rs_write_string(FILE *savef, char *s)
696 {
697 int len = 0;
698
699 if (write_error)
700 return(WRITESTAT);
701
702 len = (s == NULL) ? 0 : (int) strlen(s) + 1;
703
704 rs_write_int(savef, len);
705 rs_write_chars(savef, s, len);
706
707 return(WRITESTAT);
708 }
709
710 int
711 rs_read_string(int inf, char *s, int max)
712 {
713 int len = 0;
714
715 if (read_error || format_error)
716 return(READSTAT);
717
718 rs_read_int(inf, &len);
719
720 if (len > max)
721 format_error = TRUE;
722
723 rs_read_chars(inf, s, len);
724
725 return(READSTAT);
726 }
727
728 int
729 rs_read_new_string(int inf, char **s)
730 {
731 int len=0;
732 char *buf=0;
733
734 if (read_error || format_error)
735 return(READSTAT);
736
737 rs_read_int(inf, &len);
738
739 if (len == 0)
740 buf = NULL;
741 else
742 {
743 buf = malloc(len);
744
745 if (buf == NULL)
746 read_error = TRUE;
747 }
748
749 rs_read_chars(inf, buf, len);
750
751 *s = buf;
752
753 return(READSTAT);
754 }
755
756 int
757 rs_write_strings(FILE *savef, char *s[], int count)
758 {
759 int n = 0;
760
761 if (write_error)
762 return(WRITESTAT);
763
764 rs_write_int(savef, count);
765
766 for(n = 0; n < count; n++)
767 if (rs_write_string(savef, s[n]) != 0)
768 break;
769
770 return(WRITESTAT);
771 }
772
773 int
774 rs_read_strings(int inf, char **s, int count, int max)
775 {
776 int n = 0;
777 int value = 0;
778
779 if (read_error || format_error)
780 return(READSTAT);
781
782 rs_read_int(inf, &value);
783
784 if (value != count)
785 format_error = TRUE;
786
787 for(n = 0; n < count; n++)
788 if (rs_read_string(inf, s[n], max) != 0)
789 break;
790
791 return(READSTAT);
792 }
793
794 int
795 rs_read_new_strings(int inf, char **s, int count)
796 {
797 int n = 0;
798 int value = 0;
799
800 if (read_error || format_error)
801 return(READSTAT);
802
803 rs_read_int(inf, &value);
804
805 if (value != count)
806 format_error = TRUE;
807
808 for(n = 0; n < count; n++)
809 if (rs_read_new_string(inf, &s[n]) != 0)
810 break;
811
812 return(READSTAT);
813 }
814
815 int
816 rs_write_string_index(FILE *savef, char *master[], int max, const char *str)
817 {
818 int i;
819
820 if (write_error)
821 return(WRITESTAT);
822
823 for(i = 0; i < max; i++)
824 if (str == master[i])
825 return( rs_write_int(savef, i) );
826
827 return( rs_write_int(savef,-1) );
828 }
829
830 int
831 rs_read_string_index(int inf, char *master[], int maxindex, char **str)
832 {
833 int i;
834
835 if (read_error || format_error)
836 return(READSTAT);
837
838 rs_read_int(inf, &i);
839
840 if (i > maxindex)
841 format_error = TRUE;
842 else if (i >= 0)
843 *str = master[i];
844 else
845 *str = NULL;
846
847 return(READSTAT);
848 }
849
850 int
851 rs_write_str_t(FILE *savef, str_t st)
852 {
853 if (write_error)
854 return(WRITESTAT);
855
856 rs_write_short(savef,st.st_str);
857 rs_write_short(savef,st.st_add);
858
859 return(WRITESTAT);
860 }
861
862 int
863 rs_read_str_t(int inf, str_t *st)
864 {
865 if (read_error || format_error)
866 return(READSTAT);
867
868 rs_read_short(inf,&st->st_str);
869 rs_read_short(inf,&st->st_add);
870
871 return(READSTAT);
872 }
873
874 int
875 rs_write_coord(FILE *savef, coord c)
876 {
877 if (write_error)
878 return(WRITESTAT);
879
880 rs_write_int(savef, c.x);
881 rs_write_int(savef, c.y);
882
883 return(WRITESTAT);
884 }
885
886 int
887 rs_read_coord(int inf, coord *c)
888 {
889 coord in;
890
891 if (read_error || format_error)
892 return(READSTAT);
893
894 rs_read_int(inf,&in.x);
895 rs_read_int(inf,&in.y);
896
897 if (READSTAT == 0)
898 {
899 c->x = in.x;
900 c->y = in.y;
901 }
902
903 return(READSTAT);
904 }
905
906 int
907 rs_write_window(FILE *savef, WINDOW *win)
908 {
909 int row,col,height,width;
910
911 if (write_error)
912 return(WRITESTAT);
913
914 width = getmaxx(win);
915 height = getmaxy(win);
916
917 rs_write_marker(savef,RSID_WINDOW);
918 rs_write_int(savef,height);
919 rs_write_int(savef,width);
920
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)
924 return(WRITESTAT);
925
926 return(WRITESTAT);
927 }
928
929 int
930 rs_read_window(int inf, WINDOW *win)
931 {
932 int row,col,maxlines,maxcols,value,width,height;
933
934 if (read_error || format_error)
935 return(READSTAT);
936
937 width = getmaxx(win);
938 height = getmaxy(win);
939
940 rs_read_marker(inf, RSID_WINDOW);
941
942 rs_read_int(inf, &maxlines);
943 rs_read_int(inf, &maxcols);
944
945 for(row = 0; row < maxlines; row++)
946 for(col = 0; col < maxcols; col++)
947 {
948 if (rs_read_int(inf, &value) != 0)
949 return(READSTAT);
950
951 if ((row < height) && (col < width))
952 mvwaddch(win,row,col,value);
953 }
954
955 return(READSTAT);
956 }
957
958 /******************************************************************************/
959
960 void *
961 get_list_item(struct linked_list *l, int i)
962 {
963 int count;
964
965 for(count = 0; l != NULL; count++, l = l->l_next)
966 if (count == i)
967 return(l->l_data);
968
969 return(NULL);
970 }
971
972 int
973 find_list_ptr(struct linked_list *l, void *ptr)
974 {
975 int count;
976
977 for(count = 0; l != NULL; count++, l = l->l_next)
978 if (l->l_data == ptr)
979 return(count);
980
981 return(-1);
982 }
983
984 int
985 list_size(struct linked_list *l)
986 {
987 int count;
988
989 for(count = 0; l != NULL; count++, l = l->l_next)
990 if (l->l_data == NULL)
991 return(count);
992
993 return(count);
994 }
995
996 /******************************************************************************/
997
998 int
999 rs_write_stats(FILE *savef, struct stats *s)
1000 {
1001 if (write_error)
1002 return(WRITESTAT);
1003
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));
1011
1012 return(WRITESTAT);
1013 }
1014
1015 int
1016 rs_read_stats(int inf, struct stats *s)
1017 {
1018 if (read_error || format_error)
1019 return(READSTAT);
1020
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));
1028
1029 return(READSTAT);
1030 }
1031
1032 int
1033 rs_write_scrolls(FILE *savef)
1034 {
1035 int i;
1036
1037 if (write_error)
1038 return(WRITESTAT);
1039
1040 for(i = 0; i < MAXSCROLLS; i++)
1041 {
1042 rs_write_string(savef,s_names[i]);
1043 rs_write_boolean(savef,s_know[i]);
1044 rs_write_string(savef,s_guess[i]);
1045 }
1046 return(READSTAT);
1047 }
1048
1049 int
1050 rs_read_scrolls(int inf)
1051 {
1052 int i;
1053
1054 if (read_error || format_error)
1055 return(READSTAT);
1056
1057 for(i = 0; i < MAXSCROLLS; i++)
1058 {
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]);
1062 }
1063
1064 return(READSTAT);
1065 }
1066
1067 int
1068 rs_write_potions(FILE *savef)
1069 {
1070 int i;
1071
1072 if (write_error)
1073 return(WRITESTAT);
1074
1075 for(i = 0; i < MAXPOTIONS; i++)
1076 {
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]);
1080 }
1081
1082 return(WRITESTAT);
1083 }
1084
1085 int
1086 rs_read_potions(int inf)
1087 {
1088 int i;
1089
1090 if (read_error || format_error)
1091 return(READSTAT);
1092
1093 for(i = 0; i < MAXPOTIONS; i++)
1094 {
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]);
1098 }
1099
1100 return(READSTAT);
1101 }
1102
1103 int
1104 rs_write_rings(FILE *savef)
1105 {
1106 int i;
1107
1108 if (write_error)
1109 return(WRITESTAT);
1110
1111 for(i = 0; i < MAXRINGS; i++)
1112 {
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]);
1116 }
1117
1118 return(WRITESTAT);
1119 }
1120
1121 int
1122 rs_read_rings(int inf)
1123 {
1124 int i;
1125
1126 if (read_error || format_error)
1127 return(READSTAT);
1128
1129 for(i = 0; i < MAXRINGS; i++)
1130 {
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]);
1134 }
1135
1136 return(READSTAT);
1137 }
1138
1139 int
1140 rs_write_sticks(FILE *savef)
1141 {
1142 int i;
1143
1144 if (write_error)
1145 return(WRITESTAT);
1146
1147 for (i = 0; i < MAXSTICKS; i++)
1148 {
1149 if (strcmp(ws_type[i],"staff") == 0)
1150 {
1151 rs_write_int(savef,0);
1152 rs_write_string_index(savef, wood, cNWOOD, ws_made[i]);
1153 }
1154 else
1155 {
1156 rs_write_int(savef,1);
1157 rs_write_string_index(savef, metal, cNMETAL, ws_made[i]);
1158 }
1159 rs_write_boolean(savef, ws_know[i]);
1160 rs_write_string(savef, ws_guess[i]);
1161 }
1162
1163 return(WRITESTAT);
1164 }
1165
1166 int
1167 rs_read_sticks(int inf)
1168 {
1169 int i = 0, list = 0;
1170
1171 if (read_error || format_error)
1172 return(READSTAT);
1173
1174 for(i = 0; i < MAXSTICKS; i++)
1175 {
1176 rs_read_int(inf,&list);
1177
1178 if (list == 0)
1179 {
1180 rs_read_string_index(inf, wood, cNWOOD, &ws_made[i]);
1181 ws_type[i] = "staff";
1182 }
1183 else
1184 {
1185 rs_read_string_index(inf, metal, cNMETAL, &ws_made[i]);
1186 ws_type[i] = "wand";
1187 }
1188 rs_read_boolean(inf, &ws_know[i]);
1189 rs_read_new_string(inf, &ws_guess[i]);
1190 }
1191
1192 return(READSTAT);
1193 }
1194
1195 int
1196 rs_write_daemons(FILE *savef, struct delayed_action *d_list, int count)
1197 {
1198 int i = 0;
1199 int func = 0;
1200
1201 if (write_error)
1202 return(WRITESTAT);
1203
1204 rs_write_marker(savef, RSID_DAEMONS);
1205 rs_write_int(savef, count);
1206
1207 for(i = 0; i < count; i++)
1208 {
1209 if (d_list[i].d_func == rollwand)
1210 func = 1;
1211 else if (d_list[i].d_func == doctor)
1212 func = 2;
1213 else if (d_list[i].d_func == stomach)
1214 func = 3;
1215 else if (d_list[i].d_func == runners)
1216 func = 4;
1217 else if (d_list[i].d_func == swander)
1218 func = 5;
1219 else if (d_list[i].d_func == nohaste)
1220 func = 6;
1221 else if (d_list[i].d_func == unconfuse)
1222 func = 7;
1223 else if (d_list[i].d_func == unsee)
1224 func = 8;
1225 else if (d_list[i].d_func == sight)
1226 func = 9;
1227 else if (d_list[i].d_func == NULL)
1228 func = 0;
1229 else
1230 func = -1;
1231
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);
1236 }
1237
1238 return(WRITESTAT);
1239 }
1240
1241 int
1242 rs_read_daemons(int inf, struct delayed_action *d_list, int count)
1243 {
1244 int i = 0;
1245 int func = 0;
1246 int value = 0;
1247
1248 if (read_error || format_error)
1249 return(READSTAT);
1250
1251 rs_read_marker(inf, RSID_DAEMONS);
1252 rs_read_int(inf, &value);
1253
1254 if (value != count)
1255 format_error = TRUE;
1256
1257 for(i=0; i < count; i++)
1258 {
1259 func = 0;
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);
1264
1265 switch(func)
1266 {
1267 case 1: d_list[i].d_func = rollwand;
1268 break;
1269 case 2: d_list[i].d_func = doctor;
1270 break;
1271 case 3: d_list[i].d_func = stomach;
1272 break;
1273 case 4: d_list[i].d_func = runners;
1274 break;
1275 case 5: d_list[i].d_func = swander;
1276 break;
1277 case 6: d_list[i].d_func = nohaste;
1278 break;
1279 case 7: d_list[i].d_func = unconfuse;
1280 break;
1281 case 8: d_list[i].d_func = unsee;
1282 break;
1283 case 9: d_list[i].d_func = sight;
1284 break;
1285 default:d_list[i].d_func = NULL;
1286 break;
1287 }
1288 }
1289
1290 if (d_list[i].d_func == NULL)
1291 {
1292 d_list[i].d_type = 0;
1293 d_list[i].d_arg = 0;
1294 d_list[i].d_time = 0;
1295 }
1296
1297 return(READSTAT);
1298 }
1299
1300 int
1301 rs_write_trap(FILE *savef, struct trap *trap)
1302 {
1303 if (write_error)
1304 return(WRITESTAT);
1305
1306 rs_write_coord(savef, trap->tr_pos);
1307 rs_write_char(savef, trap->tr_type);
1308 rs_write_int(savef, trap->tr_flags);
1309
1310 return(WRITESTAT);
1311 }
1312
1313 int
1314 rs_read_trap(int inf, struct trap *trap)
1315 {
1316 if (read_error || format_error)
1317 return(READSTAT);
1318
1319 rs_read_coord(inf,&trap->tr_pos);
1320 rs_read_char(inf,&trap->tr_type);
1321 rs_read_int(inf,&trap->tr_flags);
1322
1323 return(READSTAT);
1324 }
1325
1326 int
1327 rs_write_traps(FILE *savef, struct trap t[], int count)
1328 {
1329 int n = 0;
1330
1331 if (write_error)
1332 return(WRITESTAT);
1333
1334 rs_write_marker(savef, RSID_MONSTERS);
1335 rs_write_int(savef, count);
1336
1337 for(n = 0; n < count; n++)
1338 rs_write_trap(savef, &t[n]);
1339
1340 return(WRITESTAT);
1341 }
1342
1343 int
1344 rs_read_traps(int inf, struct trap *t, int count)
1345 {
1346 int value = 0, n = 0;
1347
1348 if (read_error || format_error)
1349 return(READSTAT);
1350
1351 rs_read_marker(inf, RSID_MONSTERS);
1352
1353 rs_read_int(inf,&value);
1354
1355 if (value > count)
1356 format_error = TRUE;
1357
1358 for(n = 0; n < value; n++)
1359 rs_read_trap(inf,&t[n]);
1360
1361 return(READSTAT);
1362 }
1363
1364 int
1365 rs_write_room(FILE *savef, struct room *r)
1366 {
1367 if (write_error)
1368 return(WRITESTAT);
1369
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]);
1380
1381 return(WRITESTAT);
1382 }
1383
1384 int
1385 rs_read_room(int inf, struct room *r)
1386 {
1387 if (read_error || format_error)
1388 return(READSTAT);
1389
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]);
1400
1401 return(READSTAT);
1402 }
1403
1404 int
1405 rs_write_rooms(FILE *savef, struct room r[], int count)
1406 {
1407 int n = 0;
1408
1409 if (write_error)
1410 return(WRITESTAT);
1411
1412 rs_write_int(savef, count);
1413
1414 for(n = 0; n < count; n++)
1415 rs_write_room(savef, &r[n]);
1416
1417 return(WRITESTAT);
1418 }
1419
1420 int
1421 rs_read_rooms(int inf, struct room *r, int count)
1422 {
1423 int value = 0, n = 0;
1424
1425 if (read_error || format_error)
1426 return(READSTAT);
1427
1428 rs_read_int(inf,&value);
1429
1430 if (value > count)
1431 format_error = TRUE;
1432
1433 for(n = 0; n < value; n++)
1434 rs_read_room(inf,&r[n]);
1435
1436 return(READSTAT);
1437 }
1438
1439 int
1440 rs_write_room_reference(FILE *savef, struct room *rp)
1441 {
1442 int i, room = -1;
1443
1444 if (write_error)
1445 return(WRITESTAT);
1446
1447 for (i = 0; i < MAXROOMS; i++)
1448 if (&rooms[i] == rp)
1449 room = i;
1450
1451 rs_write_int(savef, room);
1452
1453 return(WRITESTAT);
1454 }
1455
1456 int
1457 rs_read_room_reference(int inf, struct room **rp)
1458 {
1459 int i;
1460
1461 if (read_error || format_error)
1462 return(READSTAT);
1463
1464 rs_read_int(inf, &i);
1465
1466 *rp = &rooms[i];
1467
1468 return(READSTAT);
1469 }
1470
1471 int
1472 rs_write_object(FILE *savef, struct object *o)
1473 {
1474 if (write_error)
1475 return(WRITESTAT);
1476
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);
1490 return(WRITESTAT);
1491 }
1492
1493 int
1494 rs_read_object(int inf, struct object *o)
1495 {
1496 if (read_error || format_error)
1497 return(READSTAT);
1498
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);
1512
1513 return(READSTAT);
1514 }
1515
1516 int
1517 rs_write_object_list(FILE *savef, struct linked_list *l)
1518 {
1519 if (write_error)
1520 return(WRITESTAT);
1521
1522 rs_write_marker(savef, RSID_OBJECTLIST);
1523 rs_write_int(savef, list_size(l));
1524
1525 for( ;l != NULL; l = l->l_next)
1526 rs_write_object(savef, (struct object *) l->l_data);
1527
1528 return(WRITESTAT);
1529 }
1530
1531 int
1532 rs_read_object_list(int inf, struct linked_list **list)
1533 {
1534 int i, cnt;
1535 struct linked_list *l = NULL, *previous = NULL, *head = NULL;
1536
1537 if (read_error || format_error)
1538 return(READSTAT);
1539
1540 rs_read_marker(inf, RSID_OBJECTLIST);
1541 rs_read_int(inf, &cnt);
1542
1543 for (i = 0; i < cnt; i++)
1544 {
1545 l = new_item(sizeof(struct object));
1546
1547 memset(l->l_data,0,sizeof(struct object));
1548
1549 l->l_prev = previous;
1550
1551 if (previous != NULL)
1552 previous->l_next = l;
1553
1554 rs_read_object(inf,(struct object *) l->l_data);
1555
1556 if (previous == NULL)
1557 head = l;
1558
1559 previous = l;
1560 }
1561
1562 if (l != NULL)
1563 l->l_next = NULL;
1564
1565 *list = head;
1566
1567 return(READSTAT);
1568 }
1569
1570 int
1571 rs_write_object_reference(FILE *savef, struct linked_list *list,
1572 struct object *item)
1573 {
1574 int i;
1575
1576 if (write_error)
1577 return(WRITESTAT);
1578
1579 i = find_list_ptr(list, item);
1580
1581 rs_write_int(savef, i);
1582
1583 return(WRITESTAT);
1584 }
1585
1586 int
1587 rs_read_object_reference(int inf, struct linked_list *list,
1588 struct object **item)
1589 {
1590 int i;
1591
1592 if (read_error || format_error)
1593 return(READSTAT);
1594
1595 rs_read_int(inf, &i);
1596
1597 *item = get_list_item(list,i);
1598
1599 return(READSTAT);
1600 }
1601
1602 int
1603 find_room_coord(struct room *rmlist, coord *c, int n)
1604 {
1605 int i = 0;
1606
1607 for(i = 0; i < n; i++)
1608 if(&rmlist[i].r_gold == c)
1609 return(i);
1610
1611 return(-1);
1612 }
1613
1614 int
1615 find_thing_coord(struct linked_list *monlist, coord *c)
1616 {
1617 struct linked_list *mitem;
1618 struct thing *tp;
1619 int i = 0;
1620
1621 for(mitem = monlist; mitem != NULL; mitem = mitem->l_next)
1622 {
1623 tp = THINGPTR(mitem);
1624
1625 if (c == &tp->t_pos)
1626 return(i);
1627
1628 i++;
1629 }
1630
1631 return(-1);
1632 }
1633
1634 int
1635 find_object_coord(struct linked_list *objlist, coord *c)
1636 {
1637 struct linked_list *oitem;
1638 struct object *obj;
1639 int i = 0;
1640
1641 for(oitem = objlist; oitem != NULL; oitem = oitem->l_next)
1642 {
1643 obj = OBJPTR(oitem);
1644
1645 if (c == &obj->o_pos)
1646 return(i);
1647
1648 i++;
1649 }
1650
1651 return(-1);
1652 }
1653
1654 int
1655 rs_write_thing(FILE *savef, struct thing *t)
1656 {
1657 int i = -1;
1658
1659 if (write_error)
1660 return(WRITESTAT);
1661
1662 rs_write_marker(savef, RSID_THING);
1663
1664 if (t == NULL)
1665 {
1666 rs_write_int(savef, 0);
1667 return(WRITESTAT);
1668 }
1669
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);
1676
1677 /*
1678 t_dest can be:
1679 0,0: NULL
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
1684
1685 We need to remember what we are chasing rather than
1686 the current location of what we are chasing.
1687 */
1688
1689 if (t->t_dest == &hero)
1690 {
1691 rs_write_int(savef,0);
1692 rs_write_int(savef,1);
1693 }
1694 else if (t->t_dest != NULL)
1695 {
1696 i = find_thing_coord(mlist, t->t_dest);
1697
1698 if (i >=0 )
1699 {
1700 rs_write_int(savef,1);
1701 rs_write_int(savef,i);
1702 }
1703 else
1704 {
1705 i = find_object_coord(lvl_obj, t->t_dest);
1706
1707 if (i >= 0)
1708 {
1709 rs_write_int(savef,2);
1710 rs_write_int(savef,i);
1711 }
1712 else
1713 {
1714 i = find_room_coord(rooms, t->t_dest, MAXROOMS);
1715
1716 if (i >= 0)
1717 {
1718 rs_write_int(savef,3);
1719 rs_write_int(savef,i);
1720 }
1721 else
1722 {
1723 rs_write_int(savef, 0);
1724 rs_write_int(savef,1); /* chase the hero anyway */
1725 }
1726 }
1727 }
1728 }
1729 else
1730 {
1731 rs_write_int(savef,0);
1732 rs_write_int(savef,0);
1733 }
1734
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);
1738
1739 return(WRITESTAT);
1740 }
1741
1742 int
1743 rs_read_thing(int inf, struct thing *t)
1744 {
1745 int listid = 0, index = -1;
1746 struct linked_list *item;
1747
1748 if (read_error || format_error)
1749 return(READSTAT);
1750
1751 rs_read_marker(inf, RSID_THING);
1752
1753 rs_read_int(inf, &index);
1754
1755 if (index == 0)
1756 return(READSTAT);
1757
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);
1763
1764 /*
1765 t_dest can be (listid,index):
1766 0,0: NULL
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
1771
1772 We need to remember what we are chasing rather than
1773 the current location of what we are chasing.
1774 */
1775
1776 rs_read_int(inf, &listid);
1777 rs_read_int(inf, &index);
1778 t->t_reserved = -1;
1779
1780 if (listid == 0) /* hero or NULL */
1781 {
1782 if (index == 1)
1783 t->t_dest = &hero;
1784 else
1785 t->t_dest = NULL;
1786 }
1787 else if (listid == 1) /* monster/thing */
1788 {
1789 t->t_dest = NULL;
1790 t->t_reserved = index;
1791 }
1792 else if (listid == 2) /* object */
1793 {
1794 struct object *obj;
1795
1796 item = get_list_item(lvl_obj,index);
1797
1798 if (item != NULL)
1799 {
1800 obj = OBJPTR(item);
1801 t->t_dest = &obj->o_pos;
1802 }
1803 }
1804 else if (listid == 3) /* gold */
1805 {
1806 t->t_dest = &rooms[index].r_gold;
1807 }
1808 else
1809 t->t_dest = NULL;
1810
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);
1814
1815 return(READSTAT);
1816 }
1817
1818 int
1819 rs_fix_thing(struct thing *t)
1820 {
1821 struct linked_list *item;
1822 struct thing *tp;
1823
1824 if (t->t_reserved < 0)
1825 return;
1826
1827 item = get_list_item(mlist,t->t_reserved);
1828
1829 if (item != NULL)
1830 {
1831 tp = THINGPTR(item);
1832 t->t_dest = &tp->t_pos;
1833 }
1834 }
1835
1836 int
1837 rs_write_thing_list(FILE *savef, struct linked_list *l)
1838 {
1839 int cnt = 0;
1840
1841 if (write_error)
1842 return(WRITESTAT);
1843
1844 rs_write_marker(savef, RSID_MONSTERLIST);
1845
1846 cnt = list_size(l);
1847
1848 rs_write_int(savef, cnt);
1849
1850 if (cnt < 1)
1851 return(WRITESTAT);
1852
1853 while (l != NULL) {
1854 rs_write_thing(savef, (struct thing *)l->l_data);
1855 l = l->l_next;
1856 }
1857
1858 return(WRITESTAT);
1859 }
1860
1861 int
1862 rs_read_thing_list(int inf, struct linked_list **list)
1863 {
1864 int i, cnt;
1865 struct linked_list *l = NULL, *previous = NULL, *head = NULL;
1866
1867 if (read_error || format_error)
1868 return(READSTAT);
1869
1870 rs_read_marker(inf, RSID_MONSTERLIST);
1871
1872 rs_read_int(inf, &cnt);
1873
1874 for (i = 0; i < cnt; i++)
1875 {
1876 l = new_item(sizeof(struct thing));
1877
1878 l->l_prev = previous;
1879
1880 if (previous != NULL)
1881 previous->l_next = l;
1882
1883 rs_read_thing(inf,(struct thing *)l->l_data);
1884
1885 if (previous == NULL)
1886 head = l;
1887
1888 previous = l;
1889 }
1890
1891 if (l != NULL)
1892 l->l_next = NULL;
1893
1894 *list = head;
1895
1896 return(READSTAT);
1897 }
1898
1899 int
1900 rs_fix_thing_list(struct linked_list *list)
1901 {
1902 struct linked_list *item;
1903
1904 for(item = list; item != NULL; item = item->l_next)
1905 rs_fix_thing(THINGPTR(item));
1906 }
1907
1908 int
1909 rs_fix_magic_items(struct magic_item *mi, int count)
1910 {
1911 int i;
1912
1913 for (i = 0; i < count; i++)
1914 if (i > 0)
1915 mi[i].mi_prob += mi[i-1].mi_prob;
1916 }
1917
1918 int
1919 rs_fix_monsters(struct monster monsters[26])
1920 {
1921 sprintf(monsters['F'-'A'].m_stats.s_dmg,"%dd1",fung_hit);
1922 }
1923
1924 int
1925 rs_save_file(FILE *savef)
1926 {
1927 if (write_error)
1928 return(WRITESTAT);
1929
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 */
1995
1996 return(WRITESTAT);
1997 }
1998
1999 int
2000 rs_restore_file(int inf)
2001 {
2002 if (read_error || format_error)
2003 return(READSTAT);
2004
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);
2047 rs_read_rings(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, &notify);
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);
2078 return(READSTAT);
2079 }