-
Notifications
You must be signed in to change notification settings - Fork 2
/
state.c
1450 lines (1193 loc) · 32.2 KB
/
state.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
state.c - Portable Rogue Save State Code
Copyright (C) 1999, 2000, 2005 Nicholas J. Kisseberth
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name(s) of the author(s) nor the names of other contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
#include <stdlib.h>
#include <string.h>
#include <curses.h>
#include <errno.h>
#include "rogue.h"
/************************************************************************/
/* Save State Code */
/************************************************************************/
#define RSID_STATS 0xABCD0001
#define RSID_THING 0xABCD0002
#define RSID_THING_NULL 0xDEAD0002
#define RSID_OBJECT 0xABCD0003
#define RSID_MAGICITEMS 0xABCD0004
#define RSID_KNOWS 0xABCD0005
#define RSID_GUESSES 0xABCD0006
#define RSID_OBJECTLIST 0xABCD0007
#define RSID_BAGOBJECT 0xABCD0008
#define RSID_MONSTERLIST 0xABCD0009
#define RSID_MONSTERSTATS 0xABCD000A
#define RSID_MONSTERS 0xABCD000B
#define RSID_TRAP 0xABCD000C
#define RSID_WINDOW 0xABCD000D
#define RSID_DAEMONS 0xABCD000E
#define RSID_IWEAPS 0xABCD000F
#define RSID_IARMOR 0xABCD0010
#define RSID_SPELLS 0xABCD0011
#define RSID_ILIST 0xABCD0012
#define RSID_HLIST 0xABCD0013
#define RSID_DEATHTYPE 0xABCD0014
#define RSID_CTYPES 0XABCD0015
#define RSID_COORDLIST 0XABCD0016
#define RSID_ROOMS 0XABCD0017
#define READSTAT (format_error || read_error )
#define WRITESTAT (write_error)
static int read_error = FALSE;
static int write_error = FALSE;
static int format_error = FALSE;
static int endian = 0x01020304;
#define big_endian ( *((char *)&endian) == 0x01 )
void
rs_write(FILE *savef, const void *ptr, size_t size)
{
encwrite(ptr, size, savef);
}
void
rs_read(FILE *savef, void *ptr, size_t size)
{
encread(ptr, size, savef);
}
void
rs_write_int(FILE *savef, int c)
{
char bytes[4];
char *buf = (char *) &c;
if (big_endian)
{
bytes[3] = buf[0];
bytes[2] = buf[1];
bytes[1] = buf[2];
bytes[0] = buf[3];
buf = bytes;
}
rs_write(savef, buf, 4);
}
void
rs_read_int(FILE *savef, int *i)
{
char bytes[4];
int input = 0;
char *buf = (char *)&input;
rs_read(savef, &input, 4);
if (encerror())
return;
if (big_endian)
{
bytes[3] = buf[0];
bytes[2] = buf[1];
bytes[1] = buf[2];
bytes[0] = buf[3];
buf = bytes;
}
*i = *((int *) buf);
}
void
rs_write_uint(FILE *savef, unsigned int c)
{
char bytes[4];
char *buf = (char *) &c;
if (big_endian)
{
bytes[3] = buf[0];
bytes[2] = buf[1];
bytes[1] = buf[2];
bytes[0] = buf[3];
buf = bytes;
}
rs_write(savef, buf, 4);
}
void
rs_read_uint(FILE *savef, unsigned int *i)
{
char bytes[4];
int input = 0;
char *buf = (char *)&input;
rs_read(savef, &input, 4);
if (encerror())
return;
if (big_endian)
{
bytes[3] = buf[0];
bytes[2] = buf[1];
bytes[1] = buf[2];
bytes[0] = buf[3];
buf = bytes;
}
*i = *((unsigned int *) buf);
}
void
rs_write_chars(FILE *savef, const char *c, int cnt)
{
rs_write_int(savef, cnt);
rs_write(savef, c, cnt);
}
void
rs_read_chars(FILE *savef, char *i, int cnt)
{
int value = 0;
rs_read_int(savef, &value);
if (!encerror() && (value != cnt))
encseterr(EILSEQ);
rs_read(savef, i, cnt);
}
void
rs_write_ints(FILE *savef, int *c, int cnt)
{
int n = 0;
rs_write_int(savef, cnt);
for(n = 0; n < cnt; n++)
rs_write_int(savef,c[n]);
}
void
rs_read_ints(FILE *savef, int *i, int cnt)
{
int n, value;
rs_read_int(savef,&value);
if (!encerror() && (value != cnt))
encseterr(EILSEQ);
for(n = 0; n < cnt; n++)
rs_read_int(savef, &i[n]);
}
void
rs_write_marker(FILE *savef, int id)
{
rs_write_int(savef, id);
}
void
rs_read_marker(FILE *savef, int id)
{
int nid;
rs_read_int(savef, &nid);
if (!encerror() && (id != nid))
encseterr(EILSEQ);
}
/******************************************************************************/
void
rs_write_string(FILE *savef, const char *s)
{
int len = 0;
len = (s == NULL) ? 0 : (int) strlen(s) + 1;
rs_write_int(savef, len);
rs_write_chars(savef, s, len);
}
void
rs_read_string(FILE *savef, char *s, int max)
{
int len = 0;
rs_read_int(savef, &len);
if (!encerror() && (len > max))
encseterr(EILSEQ);
rs_read_chars(savef, s, len);
}
void
rs_read_new_string(FILE *savef, char **s)
{
int len=0;
char *buf=0;
rs_read_int(savef, &len);
if (encerror())
return;
if (len == 0)
buf = NULL;
else
{
buf = malloc(len);
if (buf == NULL)
encseterr(ENOMEM);
}
rs_read_chars(savef, buf, len);
*s = buf;
}
void
rs_write_string_index(FILE *savef, const char *master[], int max, const char *str)
{
int i;
for(i = 0; i < max; i++)
if (str == master[i])
{
rs_write_int(savef, i);
return;
}
rs_write_int(savef,-1);
}
void
rs_read_string_index(FILE *savef, const char *master[], int maxindex, const char **str)
{
int i;
rs_read_int(savef, &i);
if (!encerror() && (i > maxindex))
encseterr(EILSEQ);
else if (i >= 0)
*str = master[i];
else
*str = NULL;
}
void
rs_write_coord(FILE *savef, coord c)
{
rs_write_int(savef, c.x);
rs_write_int(savef, c.y);
}
void
rs_read_coord(FILE *savef, coord *c)
{
coord in;
rs_read_int(savef,&in.x);
rs_read_int(savef,&in.y);
if (!encerror())
{
c->x = in.x;
c->y = in.y;
}
}
void
rs_write_window(FILE *savef, WINDOW *win)
{
int row,col,height,width;
width = getmaxx(win);
height = getmaxy(win);
rs_write_marker(savef,RSID_WINDOW);
rs_write_int(savef,height);
rs_write_int(savef,width);
for(row=0;row<height;row++)
for(col=0;col<width;col++)
rs_write_int(savef, mvwinch(win,row,col));
}
void
rs_read_window(FILE *savef, WINDOW *win)
{
int row,col,maxlines,maxcols,value,width,height;
width = getmaxx(win);
height = getmaxy(win);
rs_read_marker(savef, RSID_WINDOW);
rs_read_int(savef, &maxlines);
rs_read_int(savef, &maxcols);
if (encerror())
return;
for(row = 0; row < maxlines; row++)
for(col = 0; col < maxcols; col++)
{
rs_read_int(savef, &value);
if ((row < height) && (col < width))
mvwaddch(win,row,col,value);
}
}
/******************************************************************************/
void *
get_list_item(THING *l, int i)
{
int cnt;
for(cnt = 0; l != NULL; cnt++, l = l->l_next)
if (cnt == i)
return(l);
return(NULL);
}
int
find_list_ptr(THING *l, void *ptr)
{
int cnt;
for(cnt = 0; l != NULL; cnt++, l = l->l_next)
if (l == ptr)
return(cnt);
return(-1);
}
int
list_size(THING *l)
{
int cnt;
for(cnt = 0; l != NULL; cnt++, l = l->l_next)
;
return(cnt);
}
/******************************************************************************/
void
rs_write_stats(FILE *savef, struct stats *s)
{
rs_write_marker(savef, RSID_STATS);
rs_write_int(savef, s->s_str);
rs_write_int(savef, s->s_exp);
rs_write_int(savef, s->s_lvl);
rs_write_int(savef, s->s_arm);
rs_write_int(savef, s->s_hpt);
rs_write_chars(savef, s->s_dmg, sizeof(s->s_dmg));
rs_write_int(savef,s->s_maxhp);
}
void
rs_read_stats(FILE *savef, struct stats *s)
{
rs_read_marker(savef, RSID_STATS);
rs_read_int(savef,&s->s_str);
rs_read_int(savef,&s->s_exp);
rs_read_int(savef,&s->s_lvl);
rs_read_int(savef,&s->s_arm);
rs_read_int(savef,&s->s_hpt);
rs_read_chars(savef,s->s_dmg,sizeof(s->s_dmg));
rs_read_int(savef,&s->s_maxhp);
}
void
rs_write_stone_index(FILE *savef, const STONE master[], int max, const char *str)
{
int i;
for(i = 0; i < max; i++)
if (str == master[i].st_name)
{
rs_write_int(savef,i);
return;
}
rs_write_int(savef,-1);
}
void
rs_read_stone_index(FILE *savef, const STONE master[], int maxindex, const char **str)
{
int i = 0;
rs_read_int(savef,&i);
if (!encerror() && (i > maxindex))
encseterr(EILSEQ);
else if (i >= 0)
*str = master[i].st_name;
else
*str = NULL;
}
void
rs_write_scrolls(FILE *savef)
{
int i;
for(i = 0; i < MAXSCROLLS; i++)
rs_write_string(savef, s_names[i]);
}
void
rs_read_scrolls(FILE *savef)
{
int i;
for(i = 0; i < MAXSCROLLS; i++)
rs_read_new_string(savef, &s_names[i]);
}
void
rs_write_potions(FILE *savef)
{
int i;
for(i = 0; i < MAXPOTIONS; i++)
rs_write_string_index(savef, rainbow, cNCOLORS, p_colors[i]);
}
void
rs_read_potions(FILE *savef)
{
int i;
for(i = 0; i < MAXPOTIONS; i++)
rs_read_string_index(savef, rainbow, cNCOLORS, &p_colors[i]);
}
void
rs_write_rings(FILE *savef)
{
int i;
for(i = 0; i < MAXRINGS; i++)
rs_write_stone_index(savef, stones, cNSTONES, r_stones[i]);
}
void
rs_read_rings(FILE *savef)
{
int i;
for(i = 0; i < MAXRINGS; i++)
rs_read_stone_index(savef, stones, cNSTONES, &r_stones[i]);
}
void
rs_write_sticks(FILE *savef)
{
int i;
for (i = 0; i < MAXSTICKS; i++)
{
if (strcmp(ws_type[i],"staff") == 0)
{
rs_write_int(savef,0);
rs_write_string_index(savef, wood, cNWOOD, ws_made[i]);
}
else
{
rs_write_int(savef,1);
rs_write_string_index(savef, metal, cNMETAL, ws_made[i]);
}
}
}
void
rs_read_sticks(FILE *savef)
{
int i = 0, list = 0;
for(i = 0; i < MAXSTICKS; i++)
{
rs_read_int(savef,&list);
if (list == 0)
{
rs_read_string_index(savef, wood, cNWOOD, &ws_made[i]);
ws_type[i] = "staff";
}
else
{
rs_read_string_index(savef, metal, cNMETAL, &ws_made[i]);
ws_type[i] = "wand";
}
}
}
void
rs_write_daemons(FILE *savef, struct delayed_action *dlist, int cnt)
{
int i = 0;
int func = 0;
rs_write_marker(savef, RSID_DAEMONS);
rs_write_int(savef, cnt);
for(i = 0; i < cnt; i++)
{
if (dlist[i].d_func == rollwand)
func = 1;
else if (dlist[i].d_func == doctor)
func = 2;
else if (dlist[i].d_func == stomach)
func = 3;
else if (dlist[i].d_func == runners)
func = 4;
else if (dlist[i].d_func == swander)
func = 5;
else if (dlist[i].d_func == nohaste)
func = 6;
else if (dlist[i].d_func == unconfuse)
func = 7;
else if (dlist[i].d_func == unsee)
func = 8;
else if (dlist[i].d_func == sight)
func = 9;
else if (dlist[i].d_func == NULL)
func = 0;
else
func = -1;
rs_write_int(savef, dlist[i].d_type);
rs_write_int(savef, func);
rs_write_int(savef, dlist[i].d_arg);
rs_write_int(savef, dlist[i].d_time);
}
}
void
rs_read_daemons(FILE *savef, struct delayed_action *dlist, int cnt)
{
int i = 0;
int func = 0;
int value = 0;
rs_read_marker(savef, RSID_DAEMONS);
rs_read_int(savef, &value);
if (!encerror() && (value > cnt))
{
encseterr(EILSEQ);
return;
}
for(i=0; i < cnt; i++)
{
func = 0;
rs_read_int(savef, &dlist[i].d_type);
rs_read_int(savef, &func);
rs_read_int(savef, &dlist[i].d_arg);
rs_read_int(savef, &dlist[i].d_time);
if (encerror())
return;
switch(func)
{
case 1: dlist[i].d_func = rollwand;
break;
case 2: dlist[i].d_func = doctor;
break;
case 3: dlist[i].d_func = stomach;
break;
case 4: dlist[i].d_func = runners;
break;
case 5: dlist[i].d_func = swander;
break;
case 6: dlist[i].d_func = nohaste;
break;
case 7: dlist[i].d_func = unconfuse;
break;
case 8: dlist[i].d_func = unsee;
break;
case 9: dlist[i].d_func = sight;
break;
default:dlist[i].d_func = NULL;
break;
}
}
if (dlist[i].d_func == NULL)
{
dlist[i].d_type = 0;
dlist[i].d_arg = 0;
dlist[i].d_time = 0;
}
}
void
rs_write_obj_info(FILE *savef, struct obj_info *i, int cnt)
{
int n;
rs_write_marker(savef, RSID_MAGICITEMS);
rs_write_int(savef, cnt);
for(n = 0; n < cnt; n++)
{
/* oi_name is constant, defined at compile time in all cases */
rs_write_int(savef,i[n].oi_prob);
rs_write_int(savef,i[n].oi_worth);
rs_write_string(savef,i[n].oi_guess);
rs_write_int(savef,i[n].oi_know);
}
}
void
rs_read_obj_info(FILE *savef, struct obj_info *mi, int cnt)
{
int n;
int value;
rs_read_marker(savef, RSID_MAGICITEMS);
rs_read_int(savef, &value);
if (!encerror() && (value > cnt))
{
encseterr(EILSEQ);
return;
}
for(n = 0; n < value; n++)
{
/* mi_name is const, defined at compile time in all cases */
rs_read_int(savef,&mi[n].oi_prob);
rs_read_int(savef,&mi[n].oi_worth);
rs_read_new_string(savef,&mi[n].oi_guess);
rs_read_int(savef,&mi[n].oi_know);
}
}
void
rs_write_room(FILE *savef, struct room *r)
{
rs_write_coord(savef, r->r_pos);
rs_write_coord(savef, r->r_max);
rs_write_coord(savef, r->r_gold);
rs_write_int(savef, r->r_goldval);
rs_write_int(savef, r->r_flags);
rs_write_int(savef, r->r_nexits);
rs_write_coord(savef, r->r_exit[0]);
rs_write_coord(savef, r->r_exit[1]);
rs_write_coord(savef, r->r_exit[2]);
rs_write_coord(savef, r->r_exit[3]);
rs_write_coord(savef, r->r_exit[4]);
rs_write_coord(savef, r->r_exit[5]);
rs_write_coord(savef, r->r_exit[6]);
rs_write_coord(savef, r->r_exit[7]);
rs_write_coord(savef, r->r_exit[8]);
rs_write_coord(savef, r->r_exit[9]);
rs_write_coord(savef, r->r_exit[10]);
rs_write_coord(savef, r->r_exit[11]);
}
void
rs_read_room(FILE *savef, struct room *r)
{
rs_read_coord(savef,&r->r_pos);
rs_read_coord(savef,&r->r_max);
rs_read_coord(savef,&r->r_gold);
rs_read_int(savef,&r->r_goldval);
rs_read_int(savef,&r->r_flags);
rs_read_int(savef,&r->r_nexits);
rs_read_coord(savef,&r->r_exit[0]);
rs_read_coord(savef,&r->r_exit[1]);
rs_read_coord(savef,&r->r_exit[2]);
rs_read_coord(savef,&r->r_exit[3]);
rs_read_coord(savef,&r->r_exit[4]);
rs_read_coord(savef,&r->r_exit[5]);
rs_read_coord(savef,&r->r_exit[6]);
rs_read_coord(savef,&r->r_exit[7]);
rs_read_coord(savef,&r->r_exit[8]);
rs_read_coord(savef,&r->r_exit[9]);
rs_read_coord(savef,&r->r_exit[10]);
rs_read_coord(savef,&r->r_exit[11]);
}
void
rs_write_rooms(FILE *savef, struct room r[], int cnt)
{
int n = 0;
rs_write_int(savef, cnt);
for(n = 0; n < cnt; n++)
rs_write_room(savef, &r[n]);
}
void
rs_read_rooms(FILE *savef, struct room *r, int cnt)
{
int value = 0, n = 0;
rs_read_int(savef,&value);
if (!encerror() && (value > cnt))
encseterr(EILSEQ);
else
for(n = 0; n < value; n++)
rs_read_room(savef,&r[n]);
}
void
rs_write_room_reference(FILE *savef, struct room *rp)
{
int i, room = -1;
for (i = 0; i < MAXROOMS; i++)
if (&rooms[i] == rp)
room = i;
rs_write_int(savef, room);
}
void
rs_read_room_reference(FILE *savef, struct room **rp)
{
int i;
rs_read_int(savef, &i);
if (!encerror())
*rp = &rooms[i];
}
void
rs_write_monsters(FILE *savef, struct monster *m, int cnt)
{
int n;
rs_write_marker(savef, RSID_MONSTERS);
rs_write_int(savef, cnt);
for(n=0;n<cnt;n++)
rs_write_stats(savef, &m[n].m_stats);
}
void
rs_read_monsters(FILE *savef, struct monster *m, int cnt)
{
int value = 0, n = 0;
rs_read_marker(savef, RSID_MONSTERS);
rs_read_int(savef, &value);
if (!encerror() && (value != cnt))
encseterr(EILSEQ);
else
for(n = 0; n < cnt; n++)
rs_read_stats(savef, &m[n].m_stats);
}
void
rs_write_object(FILE *savef, THING *o)
{
rs_write_marker(savef, RSID_OBJECT);
rs_write_int(savef, o->_o._o_type);
rs_write_coord(savef, o->_o._o_pos);
rs_write_int(savef, o->_o._o_launch);
rs_write_int(savef, o->_o._o_packch);
rs_write_chars(savef, o->_o._o_damage, sizeof(o->_o._o_damage));
rs_write_chars(savef, o->_o._o_hurldmg, sizeof(o->_o._o_hurldmg));
rs_write_int(savef, o->_o._o_count);
rs_write_int(savef, o->_o._o_which);
rs_write_int(savef, o->_o._o_hplus);
rs_write_int(savef, o->_o._o_dplus);
rs_write_int(savef, o->_o._o_arm);
rs_write_int(savef, o->_o._o_flags);
rs_write_int(savef, o->_o._o_group);
rs_write_string(savef, o->_o._o_label);
}
void
rs_read_object(FILE *savef, THING *o)
{
rs_read_marker(savef, RSID_OBJECT);
rs_read_int(savef, &o->_o._o_type);
rs_read_coord(savef, &o->_o._o_pos);
rs_read_int(savef, &o->_o._o_launch);
rs_read_int(savef, &o->_o._o_packch);
rs_read_chars(savef, o->_o._o_damage, sizeof(o->_o._o_damage));
rs_read_chars(savef, o->_o._o_hurldmg, sizeof(o->_o._o_hurldmg));
rs_read_int(savef, &o->_o._o_count);
rs_read_int(savef, &o->_o._o_which);
rs_read_int(savef, &o->_o._o_hplus);
rs_read_int(savef, &o->_o._o_dplus);
rs_read_int(savef, &o->_o._o_arm);
rs_read_int(savef, &o->_o._o_flags);
rs_read_int(savef, &o->_o._o_group);
rs_read_new_string(savef, &o->_o._o_label);
}
void
rs_write_object_list(FILE *savef, THING *l)
{
rs_write_marker(savef, RSID_OBJECTLIST);
rs_write_int(savef, list_size(l));
for( ;l != NULL; l = l->l_next)
rs_write_object(savef, l);
}
void
rs_read_object_list(FILE *savef, THING **list)
{
int i, cnt;
THING *l = NULL, *previous = NULL, *head = NULL;
rs_read_marker(savef, RSID_OBJECTLIST);
rs_read_int(savef, &cnt);
if (encerror())
return;
for (i = 0; i < cnt; i++)
{
l = new_item();
l->l_prev = previous;
if (previous != NULL)
previous->l_next = l;
rs_read_object(savef,l);
if (previous == NULL)
head = l;
previous = l;
}
if (l != NULL)
l->l_next = NULL;
*list = head;
}
void
rs_write_object_reference(FILE *savef, THING *list, THING *item)
{
int i;
i = find_list_ptr(list, item);
rs_write_int(savef, i);
}
void
rs_read_object_reference(FILE *savef, THING *list, THING **item)
{
int i;
rs_read_int(savef, &i);
if (!encerror())
*item = get_list_item(list,i);
else
*item = NULL;
}
int
find_room_coord(const struct room *rmlist,const coord *c, int n)
{
int i = 0;
for(i = 0; i < n; i++)
if(&rmlist[i].r_gold == c)
return(i);
return(-1);
}
int
find_thing_coord(THING *monlist, const coord *c)
{
THING *mitem;
THING *tp;
int i = 0;
for(mitem = monlist; mitem != NULL; mitem = mitem->l_next)
{
tp = mitem;
if (c == &tp->t_pos)
return(i);
i++;
}
return(-1);
}
int
find_object_coord(THING *objlist, const coord *c)
{
THING *oitem;
THING *obj;
int i = 0;
for(oitem = objlist; oitem != NULL; oitem = oitem->l_next)
{
obj = oitem;
if (c == &obj->o_pos)
return(i);
i++;
}
return(-1);
}
void
rs_write_thing(FILE *savef, THING *t)