-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworld.c
More file actions
1454 lines (1422 loc) · 49 KB
/
world.c
File metadata and controls
1454 lines (1422 loc) · 49 KB
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
#if 0
gcc $CFLAGS -c -Wno-unused-result world.c `sdl-config --cflags`
exit
#endif
#define USING_RW_DATA
#include "common.h"
#include <time.h>
Uint32 item_random_key;
static int check_feature(const ASN1_Value*v) {
// Returns 0 if feature is valid, nonzero if feature is not valid.
// Will also enable the feature if necessary (currently this has no effect)
// (Note: This is not really tested properly yet, but is partally tested)
int i,j;
if(v->type==ASN1_RELATIVE_OID && v->length<=version_rel_oid_length && !memcmp(v->data,version_rel_oid,v->length)) return 0;
if(v->type==ASN1_RELATIVE_OID && v->length>2 && !v->data[0] && !version_rel_oid[0]) {
// (This currently assumes that the pieces of the version number cannot exceed 16383, but it is unlikely to exceed 16383 anyways)
i=j=(v->data[1]>127?3:2);
// Major
if(v->data[1]!=version_rel_oid[1]) return 1;
if(v->data[1]>127 && v->data[2]!=version_rel_oid[2]) return 1;
// Minor, patch, others
if(v->length>i) {
if(version_rel_oid_length-i<=0) return 1;
if(version_rel_oid_length>=v->length && memcmp(v->data+i,version_rel_oid+i,v->length-i)>0) return 1;
if(version_rel_oid_length<v->length && memcmp(v->data+i,version_rel_oid+i,version_rel_oid_length-i)>=0) return 1;
}
// OK
return 0;
}
if(v->type==ASN1_RELATIVE_OID && v->length==3 && !memcmp(v->data,"\x04\x00\x0E",3)) return 0;
if(v->type==ASN1_RELATIVE_OID && v->length==3 && !memcmp(v->data,"\x04\x03",2) && v->data[2]==version_is_release) return 0;
return -1;
}
static int do_joystick_config(const ASN1_Value*v) {
char buf[256];
ASN1_Value a,b;
ASN1_Iterator it;
int c,d,i,q;
Sint32 z;
asn1_foreach(q,&it,v,&a) {
if(!a.class && a.type==ASN1_SEQUENCE && a.constructed) {
if(asn1_first_of(&b,&a) || b.class || b.type!=ASN1_PC_STRING || b.length>255) return 1;
memcpy(buf,b.data,b.length);
buf[b.length]=0;
if(asn1_next_of(&b,&a) || b.class || b.type!=ASN1_OCTET_STRING) return 1;
z=configure_joystick(1,buf);
if(z<0) continue;
for(d=i=q=0;i<b.length && q<16;) {
switch(c=b.data[i++]) {
case 3: joystat->map[z].a[q++]=0; if(q<16)
case 2: joystat->map[z].a[q++]=0; if(q<16)
case 1: joystat->map[z].a[q++]=0; if(q<16)
case 0: joystat->map[z].a[q++]=0; break;
case 7: joystat->map[z].a[q++]=d; if(q<16)
case 6: joystat->map[z].a[q++]=d; if(q<16)
case 5: joystat->map[z].a[q++]=d; if(q<16)
case 4: joystat->map[z].a[q++]=d; if(q<16)
/* */ joystat->map[z].a[q++]=d; break;
case 8 ... 9: case 13: case 16 ... 17: case 24 ... 27: case 30 ... 126:
case 8+128 ... 9+128: case 13+128: case 16+128 ... 17+128: case 24+128 ... 27+128: case 30+128 ... 126+128:
joystat->map[z].a[q++]=d=c; break;
case 12: if(i<b.length && q<16) joystat->map[z].a[q++]=d=b.data[i++]+0x100; //
case 11: if(i<b.length && q<16) joystat->map[z].a[q++]=d=b.data[i++]+0x100; //
case 10: if(i<b.length && q<16) joystat->map[z].a[q++]=d=b.data[i++]+0x100; break;
case 128 ... 135: joystat->map[z].a[q++]=d=c+0x181; break;
default: fprintf(stderr,"Incorrect byte (%02X) in joystick configuration in GENERAL.DER",c); return 1;
}
}
for(i=16;i<24;i++) if(joystat->map[z].a[i]&0x8000) joystat->map[z].a[i]=0x300;
}
}
return q!=ASN1_DONE;
}
#define X(aa) if(v1.constructed || v1.class || (v1.type!=ASN1_NULL && v1.type!=ASN1_INTEGER) || (v1.type==ASN1_INTEGER && asn1_decode_number(&v1,ASN1_INTEGER,&config.aa))) return 1;
#define Y(aa) if(v1.constructed || v1.class || (v1.type!=ASN1_NULL && v1.type!=ASN1_ENUMERATED) || (v1.type==ASN1_ENUMERATED && asn1_decode_number(&v1,ASN1_INTEGER,&config.aa))) return 1;
static int do_override_option(const ASN1_Value*v0) {
ASN1_Value v1;
if(asn1_first_of(&v1,v0)) return 0; X(speed)
if(asn1_next_of(&v1,v0)) return 0; X(speed_fast)
if(asn1_next_of(&v1,v0)) return 0; X(message_timer)
if(asn1_next_of(&v1,v0)) return 0; X(menu_x)
if(asn1_next_of(&v1,v0)) return 0; X(menu_y)
if(asn1_next_of(&v1,v0)) return 0; Y(game_key_repeat)
return 0;
}
#undef X
#undef Y
static int do_font_palette(const ASN1_Value*v) {
char m[12];
ASN1_Value a;
if(asn1_first_of(&a,v) || a.class!=ASN1_UNIVERSAL || a.constructed || a.length>8) return 1;
if(a.type==ASN1_VISIBLE_STRING && a.length>0) {
memcpy(m,a.data,a.length);
m[a.length]=0;
if(!load_font(m,LOADFONT_BASE)) return 1;
} else if(a.type!=ASN1_NULL) {
return 1;
}
if(asn1_next_of(&a,v) || a.class!=ASN1_UNIVERSAL || a.constructed || a.length>8) return 1;
if(a.type==ASN1_VISIBLE_STRING && a.length>0) {
memcpy(m,a.data,a.length);
m[a.length]=0;
if(!load_palette(m,LOADPAL_BASE)) return 1;
} else if(a.type!=ASN1_NULL) {
return 1;
}
return 0;
}
static const char*load_item_definitions(FILE*f) {
FILE*of;
size_t ofs=0;
FILE*nf;
size_t nfs=0;
Uint32 nfi=1;
ASN1_Value v={};
ASN1_Value vv={};
ASN1_Value v1;
ItemDef id;
const char*e=0;
if(asn1_read(f,&v.constructed,&v.class,&v.type,&v.length,0)) return "ASN.1 error in ITEM.DER";
if(!v.constructed || v.type!=ASN1_SEQUENCE || v.class!=ASN1_UNIVERSAL) return "Wrong ASN.1 type in ITEM.DER";
of=open_memstream((char**)&itemdefs,&ofs);
nf=open_memstream((char**)&itemnames,&nfs);
if(!of || !nf) err(1,"Allocation failed");
fputc(0,nf);
nitemdefs=0;
while(!e && !asn1_read_item(f,&vv,0)) {
nitemdefs++;
id=(ItemDef){.weight=0xFFFFFFFFUL,.class=255};
if(vv.class==ASN1_UNIVERSAL && vv.type==ASN1_SEQUENCE) {
id.weight=0; id.maxheap=0xFFFFFFFFUL;
if(asn1_first_of(&v,&vv)) goto error;
if(v.class || (v.type!=ASN1_PC_STRING && v.type!=ASN1_OCTET_STRING) || v.length<1 || v.length>79) goto wrongtype;
id.name=nfi; fwrite(v.data,1,v.length,nf); fputc(0,nf); nfi+=v.length+1;
if(asn1_next_of(&v,&vv) || v.class || v.type!=ASN1_INTEGER || v.length!=1 || asn1_decode_number(&v,ASN1_INTEGER,&id.class) || (id.class&0x80)) goto error;
if(asn1_next_of(&v,&vv) || v.class || v.type!=ASN1_BIT_STRING || v.length<1 || v.length>5) goto error;
if(v.length>1) id.flag|=v.data[1]<<000;
if(v.length>2) id.flag|=v.data[1]<<010;
if(v.length>3) id.flag|=v.data[1]<<020;
if(v.length>4) id.flag|=v.data[1]<<030;
while(!asn1_next_of(&v,&vv)) if(v.class==ASN1_CONTEXT_SPECIFIC) switch(v.type) {
case 0: if(asn1_first_of(&v1,&v) || v1.class || v1.type!=ASN1_INTEGER || asn1_decode_number(&v1,ASN1_INTEGER,&id.weight)) goto error; break;
case 1: case 4: case 5:
if(asn1_first_of(&v1,&v) || v1.class || (v1.type!=ASN1_PC_STRING && v1.type!=ASN1_OCTET_STRING) || v1.constructed) goto error;
if(!v1.length) break;
if(v.type==1) id.script=nfi; else if(v.type==4) id.desc=nfi; else if(v.type==5) id.appearance=nfi;
fwrite(v1.data,1,v1.length,nf); fputc(0,nf); nfi+=v1.length+1;
break;
case 2: if(asn1_first_of(&v1,&v) || v1.class || v1.type!=ASN1_INTEGER || asn1_decode_number(&v1,ASN1_INTEGER,&id.maxheap)) goto error; break;
case 3: if(asn1_first_of(&v1,&v) || v1.class || v1.type!=ASN1_INTEGER || asn1_decode_number(&v1,ASN1_INTEGER,&id.element)) goto error; break;
case 6: if(asn1_first_of(&v1,&v) || v1.class || v1.type!=ASN1_INTEGER || asn1_decode_number(&v1,ASN1_INTEGER,&id.price)) goto error; break;
case 7: if(asn1_first_of(&v1,&v) || v1.class || v1.type!=ASN1_INTEGER || asn1_decode_number(&v1,ASN1_INTEGER,&id.ext3)) goto error; break;
case 8: if(asn1_first_of(&v1,&v) || v1.class || v1.type!=ASN1_INTEGER || asn1_decode_number(&v1,ASN1_INTEGER,&id.ext4)) goto error; break;
case 9: if(asn1_first_of(&v1,&v) || v1.class || v1.type!=ASN1_INTEGER || asn1_decode_number(&v1,ASN1_INTEGER,&id.ext5)) goto error; break;
case 10:
if(asn1_first_of(&v1,&v) || v1.class || v1.type!=ASN1_OCTET_STRING || v1.length<1 || v1.length>2) goto error;
id.color=v1.data[0]; if(v1.length==2) id.parameter=v1.data[1];
break;
case 11: if(asn1_first_of(&v1,&v) || v1.class || v1.type!=ASN1_ENUMERATED || asn1_decode_number(&v1,ASN1_INTEGER,&id.special)) goto error; break;
default: e="Unexpected field in ITEM.DER";
}
} else if(vv.class!=ASN1_UNIVERSAL || vv.type!=ASN1_NULL) {
wrongtype: e="Wrong ASN.1 type in ITEM.DER";
}
fwrite(&id,1,sizeof(ItemDef),of);
if(0) error: e="Error in ITEM.DER";
asn1_free(&vv);
}
fclose(of); fclose(nf);
if((nitemdefs && !itemdefs) || !itemnames || nfi!=nfs) err(1,"Allocation failed");
if(!e && !editor) e=randomize_itemdefs(0);
return e;
}
const char*init_world(void) {
// Returns 0 if OK, error message if error
int i,j;
Uint32 u,v;
FILE*fp;
// "!SZ0"
if(config.version_check) {
fp=open_lump("!SZ0","r");
if(!fp) return "Cannot open !SZ0 lump";
if(lump_size!=7) {
fclose(fp);
return "Wrong size of !SZ0 lump";
}
i=fgetc(fp);
fclose(fp);
if((i^1)&0xF1) return "Wrong file type";
}
item_random_key=time(0)^42;
// "MEMORY"
if(!editor) {
fp=open_lump("MEMORY","r");
if(!fp) return "Cannot open MEMORY lump";
u=lump_size>>1;
if(u>0x10000) u=0x10000;
for(i=0;i<u;i++) memory[i]=read16(fp);
for(;i<0x10000;i++) memory[i]=0;
fclose(fp);
} else if(fp=open_lump("MEMORY.ED","r")) {
for(i=0;i<0x10000;i++) memory[i]=0;
i=0x100;
while(i<0x10000) {
j=fgetc(fp);
if(j==EOF) break;
switch(j>>6) {
case 0: i+=j+1; break;
case 1: j=(j&0x3F)+1; while(j-- && i<0x10000) memory[i++]=read8(fp); break;
case 2: j=(j&0x3F)+1; while(j-- && i<0x10000) memory[i++]=read16(fp); break;
case 3: return "Improper file format";
}
}
fclose(fp);
}
// "START"
fp=open_lump("START","r");
if(!fp) return "Cannot open START lump";
start_mode=u=read16(fp);
if(!(u&0x0001)) config.pause|=128;
if((u&0x0002) && !editor) v_mode&=~VIDEO_80COLUMNS;
if((u&0x0004) && !editor) v_mode|=VIDEO_SMZX; else v_mode&=~VIDEO_SMZX;
if(u&~0x0007) return "Unrecognized data in START lump";
cur_screen.message_l=222;
cur_board_id=read16(fp);
v=read32(fp);
if((v&~0xFFFF) || read32(fp)) return "Unrecognized data in START lump";
for(i=0;i<16;i++) status_vars[i]=(v&(1<<i))?0:read32(fp);
fclose(fp);
for(i=0;i<16;i++) namedflag[i].name[0]=0;
for(i=0;i<4;i++) asn1reg[i]=(ASN1_Value){.type=ASN1_NULL};
// "GENERAL.DER"
if(fp=open_lump("GENERAL.DER","r")) {
// (Note: This is not really tested properly yet)
ASN1_Value a1,a2,a3,a4;
ASN1_Iterator i1,i2,i3;
if(asn1_read_item(fp,&a1,0)) {
fclose(fp);
return "ASN.1 error in GENERAL.DER lump";
}
fclose(fp);
asn1_rewind(&i1,&a1);
// Mandatory features
if(asn1_next(&i1,&a2)) return "ASN.1 error in GENERAL.DER lump";
if(a2.class || a2.type!=ASN1_SET || !a2.constructed) return "Improper type in GENERAL.DER lump";
asn1_foreach(j,&i2,&a2,&a3) {
if(a3.class || (a3.type!=ASN1_OID && a3.type!=ASN1_RELATIVE_OID && a3.type!=ASN1_SEQUENCE)) return "Improper type in GENERAL.DER lump";
if(a3.type==ASN1_SEQUENCE) {
i=0;
asn1_foreach(j,&i3,&a3,&a4) {
if(a4.class || (a4.type!=ASN1_OID && a4.type!=ASN1_RELATIVE_OID)) return "Improper type in GENERAL.DER lump";
if(!(i=check_feature(&a4))) break;
}
if(i && (config.version_check || config.version_warn || !editor)) {
fprintf(stderr,"Unrecognized sequence of OIDs in mandatory set: ");
asn1_foreach(j,&i3,&a3,&a4) fputc(' ',stderr),asn1_print_decimal_oid(&a4,ASN1_AUTO,stderr);
goto gen1;
}
} else if(check_feature(&a3) && (config.version_check || config.version_warn || !editor)) {
fprintf(stderr,"Unrecognized OID in mandatory set: ");
asn1_print_decimal_oid(&a3,ASN1_AUTO,stderr);
gen1:
fputc('\n',stderr);
config.version_warn|=4;
if(config.version_check && !editor) return "Unimplemented feature in mandatory set";
}
}
// Optional features
if(asn1_next(&i1,&a2)) return "ASN.1 error in GENERAL.DER lump";
if(a2.class || a2.type!=ASN1_SET || !a2.constructed) return "Improper type in GENERAL.DER lump";
asn1_foreach(j,&i2,&a2,&a3) {
if(a3.class || (a3.type!=ASN1_OID && a3.type!=ASN1_RELATIVE_OID && a3.type!=ASN1_SEQUENCE)) return "Improper type in GENERAL.DER lump";
if(a3.type==ASN1_SEQUENCE) {
i=0;
asn1_foreach(j,&i3,&a3,&a4) {
if(a4.class || (a4.type!=ASN1_OID && a4.type!=ASN1_RELATIVE_OID)) return "Improper type in GENERAL.DER lump";
if(!(i=check_feature(&a4))) break;
}
if(i && config.version_warn) {
fprintf(stderr,"Unrecognized sequence of OIDs in optional set:");
asn1_foreach(j,&i3,&a3,&a4) fputc(' ',stderr),asn1_print_decimal_oid(&a4,ASN1_AUTO,stderr);
goto gen2;
}
} else if(check_feature(&a3) && config.version_warn) {
fprintf(stderr,"Unrecognized OID in optional set: ");
asn1_print_decimal_oid(&a3,ASN1_AUTO,stderr);
gen2:
fputc('\n',stderr);
config.version_warn|=2;
}
}
// Others
while(!asn1_next(&i1,&a2)) if(a2.class==ASN1_CONTEXT_SPECIFIC && a2.constructed) switch(a2.type) {
case 0: // Joystick configuration
if(joystat && do_joystick_config(&a2)) return "Error in joystick configuration in world file";
break;
case 1: // Font/palette
if(do_font_palette(&a2) && !editor) return "World specification of font/palette is incorrect";
break;
case 2: // Override option
if(!editor && config.override_option && do_override_option(&a2)) return "Override option is incorrect";
break;
}
// Done
asn1_free(&a1);
}
// "NUMFORM"
if(fp=open_lump("NUMFORM","r")) {
for(i=0;i<16;i++) {
num_format[i].code=read8(fp);
num_format[i].lead=read8(fp);
num_format[i].mark=read8(fp);
num_format[i].div=read8(fp);
}
fclose(fp);
} else {
num_format[0].code=num_format[1].code='d';
num_format[0].lead=' '; num_format[1].lead='0';
num_format[0].div=num_format[1].div=1;
}
// "ELEMENT"
memset(elem_def,0,sizeof(elem_def));
fp=open_lump("ELEMENT","r");
if(!fp) return "Cannot open ELEMENT lump";
fread(appearance_mapping,1,128,fp);
for(i=0;i<4;i++) {
animation[i].mode=read8(fp);
fread(animation[i].step,1,4,fp);
}
for(i=0;i<256;i++) {
u=read8(fp);
if(u&15) {
fread(elem_def[i].name,1,u&15,fp);
if(u&0x80) elem_def[i].app[0]=fgetc(fp);
if(u&0x40) elem_def[i].app[1]=fgetc(fp);
if(!(u&0xC0)) elem_def[i].app[0]=AP_PARAM;
if(u&0x20) elem_def[i].attrib=read32(fp); else elem_def[i].attrib=i?elem_def[i-1].attrib:0;
if(u&0x10) {
u=read16(fp);
for(j=0;j<16;j++) if(u&(1<<j)) elem_def[i].event[j]=read16(fp);
}
} else {
i+=u>>4;
}
}
fclose(fp);
// "TEXT"
free(vgtext),vgtext=0;
free(gtext),gtext=0;
if(fp=open_lump(editor?"TEXT.ED":"TEXT","r")) {
Uint8*p;
vgtext=malloc(lump_size+1);
if(!vgtext) err(1,"Allocation failed");
fread(vgtext,1,lump_size,fp);
vgtext[lump_size]=0;
ngtext=1;
for(i=0;i<lump_size;i++) if(!vgtext[i]) ++ngtext;
gtext=malloc(ngtext*sizeof(Uint8*));
if(!gtext) err(1,"Allocation failed");
for(i=1,u=0,p=vgtext;i<ngtext;i++) {
gtext[i]=p;
p+=strlen(p)+1;
}
fclose(fp);
} else {
gtext=malloc(sizeof(Uint8*));
if(!gtext) err(1,"Allocation failed");
ngtext=1;
}
*gtext="";
// "BRD.NAM"
if(boardnames) {
free(*boardnames);
if(editor) for(u=1;u<=maxboard;u++) free(boardnames[u]);
}
free(boardnames),boardnames=0;
if(fp=open_lump("BRD.NAM","r")) {
Uint8*s;
Uint8*ss;
maxboard=read16(fp);
boardnames=calloc(maxboard+1,sizeof(Uint8*));
if(!boardnames) err(1,"Allocation failed");
if(editor) {
size_t z;
for(j=0;j<=maxboard;j++) {
s=0; z=0; if(getdelim((char**)&s,&z,0,fp)<=0) break;
boardnames[j]=s;
}
} else {
u=(lump_size<3?1:lump_size-2);
s=malloc(u+1);
if(!s) err(1,"Allocation failed");
fread(s,1,u,fp);
s[u]=0;
ss=s+u;
for(j=0;j<=maxboard && s<ss;j++) {
i=strlen(s);
boardnames[j]=s;
s+=i+1;
}
}
fclose(fp);
}
// "SCR.NAM"
if(screennames) {
free(*screennames);
if(editor) for(u=1;u<=maxscreen;u++) free(screennames[u]);
}
free(screennames),screennames=0;
if(editor && (fp=open_lump("SCR.NAM","r"))) {
Uint8*s;
size_t z;
maxscreen=read16(fp);
screennames=calloc(maxscreen+1,sizeof(Uint8*));
for(j=0;j<=maxscreen;j++) {
s=0; z=0; if(getdelim((char**)&s,&z,0,fp)<=0) break;
screennames[j]=s;
}
fclose(fp);
}
// "GLOBAL"
if(global_text) free(global_text);
global_text=0;
global_length=0;
if(!editor && (fp=open_lump("GLOBAL","r"))) {
if(lump_size>=0xFFFE) errx(1,"Global script is too long");
global_length=lump_size;
global_text=malloc(lump_size+1);
if(!global_text) err(1,"Allocation failed");
fread(global_text,1,lump_size,fp);
global_text[lump_size]=0;
fclose(fp);
}
// "ITEM.DER"
free(itemdefs);
free(itemnames);
itemdefs=0;
nitemdefs=0;
itemnames=0;
if(!editor && (fp=open_lump("ITEM.DER","r"))) {
const char*e=load_item_definitions(fp);
fclose(fp);
if(e) return e;
}
// "DYNASTR"
free(dynastr);
dynastr=0;
ndynastr=0;
if(fp=open_lump("DYNASTR","r")) {
warnx("This world file contains a DYNASTR lump but it is not supposed to");
config.version_warn|=4;
fclose(fp);
if(fp=open_lump("DYNASTR","w")) fclose(fp);
}
// "X?.INV"
if(!editor) {
const char*e;
char buf[]="X0.INV";
for(i=0;i<8;i++) {
buf[1]=i+'0';
e=load_inventory(fp=open_lump(buf,"r"),inventory+i);
if(fp) fclose(fp);
if(e) return e;
}
}
// done
return 0;
}
static void load_varproperties(FILE*fp,Uint32 on,VarPropertyList*vp) {
int i;
free(vp->item);
vp->item=0;
vp->count=0;
if(!on) return;
vp->count=fgetc(fp);
if(!vp->count) return;
vp->item=calloc(vp->count,sizeof(VarProperty));
if(!vp->item) err(1,"Allocation failed");
for(i=0;i<vp->count;i++) if((vp->item[i].type=fgetc(fp))&15) fread(vp->item[i].data,1,vp->item[i].type&15,fp);
}
static void save_varproperties(FILE*fp,const VarPropertyList*vp) {
int i;
fputc(vp->count,fp);
for(i=0;i<vp->count;i++) fputc(vp->item[i].type,fp),fwrite(vp->item[i].data,1,vp->item[i].type&15,fp);
}
static void load_zones(FILE*fp) {
UnordZone*u;
OrdZone*o;
Uint32 at,n,x,y;
Uint16 zo,v,f,ex;
Uint8 c,w;
// Unordered zones
zo=read16(fp);
for(w=0;w<16;w++) if(zo&(1<<w)) {
f=read16(fp); ex=read16(fp); v=board_info.height>256?read16(fp):read8(fp);
u=uzone[w]=calloc(1,(y=((v+1)*board_info.width)/8+1)+sizeof(UnordZone));
if(!u) err(1,"Allocation failed");
u->flag=f; u->extra=ex; u->maxy=v;
for(x=0;x<y;) {
u->data[x++]=c=fgetc(fp);
if(c==0 || c==255) {
n=fgetc(fp)&255;
while(n-- && x<y) u->data[x++]=c;
}
}
}
// Ordered zones
zo=read16(fp);
for(w=0;w<16;w++) if(zo&(1<<w)) {
f=read16(fp); ex=read16(fp); n=read16(fp);
o=ozone[w]=malloc(sizeof(OrdZone)+n*sizeof(OrdZoneXY));
if(!o) err(1,"Allocation failed");
o->flag=f; o->extra=ex; o->ncells=n;
x=y=1;
for(v=0;v<n;) {
c=fgetc(fp);
for(;;) {
f=(c>>4)&3;
if(!f) x=board_info.width>256?read16(fp):read8(fp); else x+=f-2;
if(x>=board_info.width) x=0;
f=(c>>6)&3;
if(!f) y=board_info.height>256?read16(fp):read8(fp); else y+=f-2;
if(y>=board_info.height) y=0;
o->xy[v].x=x; o->xy[v].y=y; v++;
if(!(c&15)) break;
c--;
}
}
}
}
static void save_zones(FILE*fp) {
UnordZone*u;
OrdZone*o;
Uint32 at,n,x,y;
Uint16 zo,v;
Uint8 c,d,w;
// Unordered zones
for(zo=w=0;w<16;w++) if(uzone[w]) zo|=1<<w;
write16(fp,zo);
for(w=0;w<16;w++) if(u=uzone[w]) {
write16(fp,u->flag); write16(fp,u->extra);
if(board_info.height>256) write16(fp,u->maxy); else write8(fp,u->maxy);
y=((u->maxy+1)*board_info.width)/8+1;
for(x=0;x<y;) {
fputc(c=u->data[x++],fp);
if(c==0 || c==255) {
for(n=0;n<255 && x+n<y && u->data[x+n]==c;n++);
fputc(n,fp);
x+=n;
}
}
}
// Ordered zones
for(zo=w=0;w<16;w++) if(ozone[w]) zo|=1<<w;
write16(fp,zo);
for(w=0;w<16;w++) if(o=ozone[w]) {
write16(fp,o->flag); write16(fp,o->extra); write16(fp,o->ncells);
x=y=1;
for(n=v=d=0;n<=o->ncells;n++) {
if(n!=o->ncells) {
c=(o->xy[n].x==x-1?0x10:o->xy[n].x==x?0x20:o->xy[n].x==x+1?0x30:0x00)+(o->xy[n].y==y-1?0x40:o->xy[n].y==y?0x80:o->xy[n].y==y+1?0xC0:0x00);
x=o->xy[n].x; y=o->xy[n].y;
}
if(n!=v && (c!=d || n==o->ncells || n==v+16)) {
fputc(d+n-v-1,fp);
while(v<n) {
if(!(d&0x30)) board_info.width>256?write16(fp,o->xy[v].x):write8(fp,o->xy[v].x);
if(!(d&0xC0)) board_info.height>256?write16(fp,o->xy[v].y):write8(fp,o->xy[v].y);
v++;
}
}
d=c;
}
}
}
static inline void fill_layer(Tile*p,Tile t,Uint32 c) {
while(c--) *p++=t;
}
static void layer_inversion_stat(Uint32 at,Uint8 lay) {
Uint32 x=at%board_info.width;
Uint32 y=at/board_info.width;
Uint32 n=b_under[at].stat|b_main[at].stat;
Stat*s=stats+n-1;
if(n>maxstat) return;
for(n=0;n<s->count;n++) if(s->xy[n].x==x && s->xy[n].y==y && (s->xy[n].layer&3)==lay) {
s->xy[n].layer^=3;
return;
}
}
static void layer_inversion(void) {
Uint32 tc=board_info.width*board_info.height;
Uint32 i;
Tile t;
for(i=0;i<tc;i++) {
if(b_under[i].kind && b_main[i].kind) {
if(b_under[i].stat && !b_main[i].stat) layer_inversion_stat(i,1); else if(!b_under[i].stat && b_main[i].stat) layer_inversion_stat(i,2);
t=b_under[i];
b_under[i]=b_main[i];
b_main[i]=t;
}
}
}
static inline Uint8 make_guess(const Tile*pt,Uint8 v,Uint8*g) {
return v?g[pt->kind]:pt->stat?*g:0;
}
static inline void update_guess(const Tile*pt,Uint8 v,Uint8*g) {
// If the guess is correct then this does not actually make any changes.
if(v) g[pt->kind]=pt->values[v]; else if(pt->stat) *g=pt->kind;
}
static Uint32 load_board_run(FILE*fp,Tile*pt,Tile*end,Uint8 v,Uint8*g) {
Uint8 c=fgetc(fp);
Uint8 m=c>>6;
Uint16 r=c&63;
Uint16 n=0;
if(!r) {
r=127+fgetc(fp);
if(r>254) r+=(fgetc(fp)<<7)+63;
}
if(r>end-pt) r=end-pt;
if(pt==b_under && m>1) m=0;
if(pt<b_under+board_info.width && m==3) m=0;
while(n<r) {
pt[n].values[v]=(m==0?make_guess(pt+n,v,g):m==1?fgetc(fp):m==2?pt[-1].values[v]:pt[n-board_info.width].values[v]);
update_guess(pt+n,v,g);
n++;
}
return r;
}
static inline void hetero_board_run(FILE*fp,const Tile*pt,Uint16 n,Uint8 v) {
Uint16 i;
for(i=0;i<n;i++) fputc(pt[i].values[v],fp);
}
static Uint32 save_board_run(FILE*fp,const Tile*pt,const Tile*end,Uint8 v,Uint8*g) {
Uint8 g0[256];
Uint8 m;
Uint16 w=board_info.width;
Uint16 r0,r2,r3,n,i;
for(r0=r2=r3=n=0;n<end-pt && (n==r0 || n==r2 || n==r3) && n<33085;n++) {
if(n==r0 && pt[n].values[v]==make_guess(pt+n,v,g)) r0++;
if(n==r2 && pt>b_under && pt[n-1].values[v]==pt[n].values[v]) r2++;
if(n==r3 && pt>=b_under+w && pt[n-w].values[v]==pt[n].values[v]) r3++;
}
if(!r0 && !r2 && !r3) {
memcpy(g0,g,256);
for(n=1,i=0;n<end-pt && n<33085;n++) {
if(pt>b_under && pt[n-1].values[v]==pt[n].values[v]) i|=010;
if(pt>=b_under+w && pt[n-w].values[v]==pt[n].values[v]) i|=020;
if(pt[n].values[v]==make_guess(pt+n,v,g0)) i|=040;
if(i&(i>>3)) {
n--;
break;
}
i>>=3;
update_guess(pt+n,v,g0);
}
m=1;
} else if(r0>r2 && r0>r3) {
m=0; n=r0;
} else if(r2>=r3) {
m=2; n=r2;
} else {
m=3; n=r3;
}
if(n>63 && n<127) n=63;
if(n>254 && n<445) n=254;
if(m) for(i=0;i<n;i++) update_guess(pt+i,v,g);
if(n<64) {
fputc(n|(m<<6),fp);
} else if(n<255) {
fputc(m<<6,fp);
fputc(n-127,fp);
} else {
fputc(m<<6,fp);
fputc((n-318)|128,fp);
fputc((n-318)>>7,fp);
}
if(m==1) hetero_board_run(fp,pt,n,v);
return n;
}
const char*load_board(FILE*fp) {
Uint8 guess[256];
Uint8 c,sf;
Uint16 ef=read16(fp);
Uint32 at,tc,n;
Tile*pt;
Tile*end;
StatXY*r;
int i,j;
if(ef&0x7040) return "Unrecognized file format";
if(feof(fp)) return "Input past end of file";
free(b_under);
b_under=b_main=b_over=0;
for(i=0;i<maxstat;i++) {
free(stats[i].text);
free(stats[i].xy);
}
free(stats);
stats=0;
maxstat=0;
for(i=0;i<16;i++) {
free(ozone[i]); ozone[i]=0;
free(uzone[i]); uzone[i]=0;
}
free(board_info.varprop.item);
memset(&board_info,0,sizeof(BoardInfo));
board_info.flag=(ef&0x100?read16(fp):read8(fp));
board_info.screen=read16(fp);
for(i=0;i<4;i++) if(ef&(1<<i)) board_info.exits[i]=read16(fp);
if(ef&0x10) {
board_info.width=read16(fp);
board_info.height=read16(fp);
if(!board_info.width || !board_info.height) return "Board size is zero";
} else {
board_info.width=read8(fp)+1;
board_info.height=read8(fp)+1;
}
if(ef&0x20) board_info.userdata=read16(fp);
maxstat=read8(fp);
// Variable property list
load_varproperties(fp,ef&0x800,&board_info.varprop);
// Board grid
if(board_info.width*(unsigned long long)board_info.height>0x100000) return "Board size is too big";
b_under=calloc(3*sizeof(Tile),tc=board_info.width*board_info.height);
if(!b_under) err(1,"Allocation failed");
b_main=b_under+tc;
b_over=b_main+tc;
end=b_over+tc;
// Stats
if(!maxstat) goto nostats;
stats=calloc(maxstat,sizeof(Stat));
if(!stats) err(1,"Allocation failed");
for(i=0;i<maxstat;i++) {
sf=(ef&0x0200?read8(fp):0x0F);
stats[i].misc1=(sf&0x02)?read16(fp):0;
stats[i].misc2=(sf&0x04)?read16(fp):0;
stats[i].misc3=(sf&0x08)?read16(fp):0;
if(stats[i].length=(sf&0x01?read16(fp):0)) {
stats[i].text=malloc(stats[i].length+1);
if(!stats[i].text) err(1,"Allocation failed");
fread(stats[i].text,1,stats[i].length,fp);
stats[i].text[stats[i].length]=0;
} else {
stats[i].text=0;
}
stats[i].speed=read8(fp);
stats[i].frame=(sf&0x10)?read16(fp):0;
stats[i].mode=(sf&0x40)?read8(fp):0;
stats[i].zone=(stats[i].mode&STAT_ZONERESTRICT)?read8(fp):0;
if(stats[i].frame && stats[i].frame>stats[i].length-4) return "Incorrect frame offset";
if(stats[i].count=read16(fp)) {
r=stats[i].xy=calloc(stats[i].count,sizeof(StatXY));
if(!r) err(1,"Allocation failed");
for(j=0;j<stats[i].count;j++) {
c=read8(fp);
if(!j && (c&15)!=15) return "File format error";
r[j].x=((c&3)==3?((board_info.width>256 || (ef&0x8000) || (stats[i].mode&STAT_INDEPENDENT))?read16(fp):read8(fp)):r[j-1].x+(c&3)-1);
r[j].y=(((c>>2)&3)==3?((board_info.height>256 || (ef&0x8000) || (stats[i].mode&STAT_INDEPENDENT))?read16(fp):read8(fp)):r[j-1].y+((c>>2)&3)-1);
r[j].instptr=(((c>>4)&3)==0?0:((c>>4)&3)==1?65535:((c>>4)&3)==2?r[j?j-1:0].instptr:read16(fp));
r[j].layer=(c&0x40?read8(fp):j?r[j-1].layer:2);
r[j].delay=(c&0x80?read8(fp):j?r[j-1].delay:0);
r[j].sensor=(Tile){};
r[j].frame=r[j].extra=0;
c=r[j].layer&0x23;
if(c && c<4 && r[j].x<board_info.width && r[j].y<board_info.height) (c==1?b_under:c==2?b_main:b_over)[r[j].y*board_info.width+r[j].x].stat=i+1;
if(sf&0x20) {
c=read8(fp);
if(c&0x01) r[j].sensor.kind=read8(fp);
if(c&0x02) r[j].sensor.color=read8(fp);
if(c&0x04) r[j].sensor.param=read8(fp);
if(c&0x08) r[j].sensor.stat=read8(fp);
if(c&0x10) {
r[j].frame=read16(fp);
if(r[j].frame>stats[i].length || !stats[i].frame) return "Improper frame pointer";
}
if(c&0x20) r[j].extra=read16(fp);
}
}
} else {
stats[i].xy=0;
}
}
nostats:
// Stat grid
if(ef&0x8000) {
pt=b_under;
while(pt<end) {
pt++->stat=c=read8(fp);
if(!c) {
n=read16(fp);
while(n-- && pt<end) pt++->stat=0;
}
}
}
// Board grid
*guess=1;
for(pt=b_under;pt<end;) pt+=load_board_run(fp,pt,end,0,guess);
memset(guess,0,256);
for(pt=b_under;pt<end;) pt+=load_board_run(fp,pt,end,1,guess);
memset(guess,0,256);
for(pt=b_under;pt<end;) pt+=load_board_run(fp,pt,end,2,guess);
if(ef&0x0400) layer_inversion();
// Zones
if(ef&0x80) load_zones(fp);
return 0;
}
const char*save_board(FILE*fp,int m) {
Uint8 guess[256];
Uint16 ef=(m?0x8600:0x0600);
Uint16 w=board_info.width;
Uint32 at;
Uint32 tc=board_info.width*board_info.height;
Tile*pt=b_under;
Tile*end=pt+tc*3;
int i,j;
Uint8 c,sf;
StatXY*r;
Stat savedstat1={};
Uint16 stat1frame=0;
// Header
if(board_info.flag&~255) ef|=0x100;
if(board_info.exits[0]) ef|=1;
if(board_info.exits[1]) ef|=2;
if(board_info.exits[2]) ef|=4;
if(board_info.exits[3]) ef|=8;
if(board_info.width>256 || board_info.height>256) ef|=0x10;
if(board_info.userdata) ef|=0x20;
if(board_info.varprop.count) ef|=0x800;
for(i=0;i<16 && !(ef&0x80);i++) if(ozone[i] || uzone[i]) ef|=0x80;
write16(fp,ef);
if(ef&0x100) write16(fp,board_info.flag); else write8(fp,board_info.flag);
write16(fp,board_info.screen);
if(ef&1) write16(fp,board_info.exits[0]);
if(ef&2) write16(fp,board_info.exits[1]);
if(ef&4) write16(fp,board_info.exits[2]);
if(ef&8) write16(fp,board_info.exits[3]);
if(ef&0x10) {
write16(fp,board_info.width);
write16(fp,board_info.height);
} else {
write8(fp,board_info.width-1);
write8(fp,board_info.height-1);
}
if(ef&0x20) write16(fp,board_info.userdata);
write8(fp,maxstat);
if(ef&0x0400) layer_inversion();
// Variable property list
if(ef&0x0800) save_varproperties(fp,&board_info.varprop);
// Stats
if(!editor && global_text && maxstat && stats->text==global_text) {
savedstat1=*stats;
if(stats->count) stat1frame=stats->xy->frame;
stats->text=0;
stats->length=0;
stats->frame=0;
if(stats->count) stats->xy->frame=0;
}
for(i=0;i<maxstat;i++) {
if(ef&0x200) {
sf=0;
if(stats[i].length) sf|=0x01;
if(stats[i].misc1) sf|=0x02;
if(stats[i].misc2) sf|=0x04;
if(stats[i].misc3) sf|=0x08;
if(stats[i].frame) sf|=0x10;
if(stats[i].mode) sf|=0x40;
for(j=0;j<stats[i].count;j++) if(stats[i].xy[j].sensor.kind || stats[i].xy[j].sensor.color || stats[i].xy[j].sensor.param || stats[i].xy[j].sensor.stat || stats[i].xy[j].frame || stats[i].xy[j].extra) {
sf|=0x20;
break;
}
write8(fp,sf);
} else {
sf=0x0F;
}
if(sf&0x02) write16(fp,stats[i].misc1);
if(sf&0x04) write16(fp,stats[i].misc2);
if(sf&0x08) write16(fp,stats[i].misc3);
if(sf&0x01) {
write16(fp,stats[i].length);
if(stats[i].length) fwrite(stats[i].text,1,stats[i].length,fp);
}
write8(fp,stats[i].speed);
if(sf&0x10) write16(fp,stats[i].frame);
if(sf&0x40) write8(fp,stats[i].mode);
if(stats[i].mode&STAT_ZONERESTRICT) write8(fp,stats[i].zone);
write16(fp,stats[i].count);
r=stats[i].xy;
for(j=0;j<stats[i].count;j++) {
if(j) {
if(r[j].x==r[j-1].x) c=1; else if(r[j].x==r[j-1].x-1) c=0; else if(r[j].x==r[j-1].x+1) c=2; else c=3;
if(r[j].y==r[j-1].y) c+=4; else if(r[j].y==r[j-1].y-1) c+=0; else if(r[j].y==r[j-1].y+1) c+=8; else c+=12;
if(r[j].layer!=r[j-1].layer) c|=0x40;
if(r[j].delay!=r[j-1].delay) c|=0x80;
} else {
c=15;
if(r[j].layer!=2) c|=0x40;
if(r[j].delay) c|=0x80;
}
if(r[j].instptr==65535) c+=0x10; else if(j && r[j].instptr==r[j-1].instptr) c+=0x20; else if(r[j].instptr) c+=0x30;
write8(fp,c);
if((c&0x03)==0x03) (board_info.width>256 || (ef&0x8000) || (stats[i].mode&STAT_INDEPENDENT))?write16(fp,r[j].x):write8(fp,r[j].x);
if((c&0x0C)==0x0C) (board_info.height>256 || (ef&0x8000) || (stats[i].mode&STAT_INDEPENDENT))?write16(fp,r[j].y):write8(fp,r[j].y);
if((c&0x30)==0x30) write16(fp,r[j].instptr);
if(c&0x40) write8(fp,r[j].layer);
if(c&0x80) write8(fp,r[j].delay);
if(sf&0x20) {
c=0;
if(r[j].sensor.kind) c|=0x01;
if(r[j].sensor.color) c|=0x02;
if(r[j].sensor.param) c|=0x04;
if(r[j].sensor.stat) c|=0x08;
if(r[j].frame) c|=0x10;
if(r[j].extra) c|=0x20;
write8(fp,c);
if(c&0x01) write8(fp,r[j].sensor.kind);
if(c&0x02) write8(fp,r[j].sensor.color);
if(c&0x04) write8(fp,r[j].sensor.param);
if(c&0x08) write8(fp,r[j].sensor.stat);
if(c&0x10) write16(fp,r[j].frame);
if(c&0x20) write16(fp,r[j].extra);
}
}
}
// Stat grid
if(ef&0x8000) {
pt=b_under;
while(pt<end) {
write8(fp,c=pt++->stat);
if(c) continue;
for(i=0;i<65535 && pt<end && !pt->stat;i++,pt++);
write16(fp,i);
}
}
// Board grid
*guess=1;
for(pt=b_under;pt<end;) pt+=save_board_run(fp,pt,end,0,guess);
memset(guess,0,256);
for(pt=b_under;pt<end;) pt+=save_board_run(fp,pt,end,1,guess);
memset(guess,0,256);
for(pt=b_under;pt<end;) pt+=save_board_run(fp,pt,end,2,guess);
if(ef&0x0400) layer_inversion();
// Zones
if(ef&0x80) save_zones(fp);
// Restore saved stat1
if(savedstat1.text) {
*stats=savedstat1;
if(stats->count) stats->xy->frame=stat1frame;
}
return 0;
}
const char*load_screen(FILE*fp) {
Uint8 vp;
Uint8 x,y,z,c;
Uint32 at=0;
int i,n;
free(cur_screen.varprop.item);
memset(&cur_screen,0,sizeof(Screen));
vp=fgetc(fp);
if(vp&0x7F) return "Unrecognized file format";
cur_screen.flag=fgetc(fp);
cur_screen.border_color=fgetc(fp);
fread(cur_screen.border,1,4,fp);
fread(cur_screen.soft_edge,1,4,fp);
fread(cur_screen.hard_edge,1,4,fp);
cur_screen.view_x=fgetc(fp);
cur_screen.view_y=fgetc(fp);
cur_screen.message_x=fgetc(fp);
cur_screen.message_y=fgetc(fp);
cur_screen.message_l=fgetc(fp);
cur_screen.message_r=fgetc(fp);
// Variable property list
load_varproperties(fp,vp&0x80,&cur_screen.varprop);
// Screen grid
for(at=0;at<80*25;) {
c=fgetc(fp);
if(c<80) {
c++;
if(at+c>80*25) return "Out of bounds access";