-
Notifications
You must be signed in to change notification settings - Fork 0
/
mbtofhm.c
1239 lines (1210 loc) · 34.8 KB
/
mbtofhm.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
#if 0
gcc -s -O2 -o ./mbtofhm -Wno-unused-result mbtofhm.c
exit
#endif
// This program is part of Free Hero Mesh and is public domain.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define HEROMESH_MAIN
#define HEROMESH_CONV_MAIN
#include "names.h"
#define fatal(...) do{ fprintf(stderr,__VA_ARGS__); exit(1); }while(0)
// Pictures
static unsigned char*pict;
static unsigned short*picalloc;
static int npic;
static unsigned short sizes[8];
static unsigned char*inpic;
static unsigned char*outpic;
static unsigned char*inpicstr;
static unsigned char*inpicabovestr;
static int outpiclen;
static char picavail[512];
// Classes
typedef struct {
char*name;
char*desc;
unsigned char attr[62];
short npic;
unsigned short pic[128];
short nvars;
unsigned char*vars;
short nsubslbl;
unsigned char*subslbl;
unsigned char*subscode;
short nmsgslbl;
unsigned char*msgslbl;
unsigned short nmsgs;
unsigned char**msgscode;
short defpic;
} ClassInfo;
static int nusermsg;
static char**usermsg;
static ClassInfo*class[512];
static char has_array;
// Levels
static int nlevels;
static unsigned short*levelid;
// Sounds
static int nsounds;
static char**sounds;
static unsigned short*soundid;
// Common
static char*basename;
static char*nam;
static unsigned long haroffs;
static unsigned long puzzlesetnumber;
static void*alloc(unsigned int s) {
void*o=malloc(s);
if(s && !o) fatal("Allocation failed\n");
return o;
}
static void*my_realloc(void*p,unsigned int s) {
void*o=realloc(p,s);
if(s && !o) fatal("Reallocation failed\n");
return o;
}
#define Allocate(x,y) (x=alloc((y)*sizeof(*(x))))
#define Reallocate(x,y) (x=my_realloc((x),(y)*sizeof(*(x))))
static void hamarc_begin(FILE*fp) {
fwrite(nam,1,strlen(nam)+1,fp);
fwrite("\0\0\0",4,1,fp);
haroffs=ftell(fp);
}
static void hamarc_end(FILE*fp) {
unsigned long end=ftell(fp);
fseek(fp,haroffs-4,SEEK_SET);
fputc((end-haroffs)>>16,fp);
fputc((end-haroffs)>>24,fp);
fputc((end-haroffs)>>0,fp);
fputc((end-haroffs)>>8,fp);
fseek(fp,end,SEEK_SET);
}
static inline void read_header(void) {
unsigned char buf[30];
int i,s;
fread(buf,1,30,stdin);
if(memcmp("MESH",buf+2,4)) fatal("Unrecognized file format\n");
puzzlesetnumber=buf[6]|(buf[7]<<8)|(buf[8]<<16)|(buf[9]<<24);
for(i=0;i<buf[10];i++) sizes[i]=buf[i+i+12]|(buf[i+i+13]<<8);
npic=buf[28]|(buf[29]<<8);
Allocate(picalloc,npic);
for(i=0;i<npic;i++) {
fread(buf,1,2,stdin);
picalloc[i]=buf[0]|(buf[1]<<8);
}
for(i=s=0;i<8;i++) s+=sizes[i]*sizes[i];
Allocate(pict,i=s*npic*16);
fread(pict,1,i,stdin);
for(i=s=0;i<8;i++) if(s<sizes[i]) s=sizes[i];
s*=s;
Allocate(inpic,s);
Allocate(outpic,s+256);
Allocate(inpicstr,s+4);
Allocate(inpicabovestr,s+4);
}
static void out_run(int ch,int am,int st,int le) {
int n=am%85?:85;
outpic[outpiclen++]=ch+n-1;
if(le) memcpy(outpic+outpiclen,inpicstr+st,le<n?le:n);
if(le) outpiclen+=le<n?le:n;
if(ch) st+=n,le-=n;
while(am-=n) {
n=am>7225?7225:am;
outpic[outpiclen++]=ch+n/85-1;
if(le>0) memcpy(outpic+outpiclen,inpicstr+st,le<n?le:n);
if(le>0) outpiclen+=le<n?le:n;
if(ch) st+=n,le-=n;
}
}
static int one_picture(int ps,int me) {
int ms=ps*ps;
int i=0;
int w,x,y,z;
int ca,homo,hetero;
for(y=0;y<ps;y++) for(x=0;x<ps;x++) {
if(me&1) x=ps-x-1;
if(me&2) y=ps-y-1;
inpicstr[i++]=inpic[me&4?x*ps+y:y*ps+x];
if(me&1) x=ps-x-1;
if(me&2) y=ps-y-1;
}
for(i=0;i<ms;i++) inpicabovestr[i]=i<ps?0:inpicstr[i-ps];
inpicabovestr[i]=255; inpicstr[i++]=254;
inpicabovestr[i]=253; inpicstr[i++]=252;
outpiclen=i=0;
while(i<ms) {
ca=0;
while(i+ca<ms && inpicstr[i+ca]==inpicabovestr[i+ca]) ca++;
homo=1;
while(i+homo<ms && inpicstr[i+homo]==inpicstr[i]) homo++;
hetero=1;
while(i+hetero<ms) {
if(inpicstr[i+hetero]==inpicabovestr[i+hetero] && inpicstr[i+hetero+1]==inpicabovestr[i+hetero+1]) break;
if(inpicstr[i+hetero]==inpicstr[i+hetero+1] && i+hetero==ms-1) break;
if(inpicstr[i+hetero]==inpicstr[i+hetero+1] && inpicstr[i+hetero]==inpicstr[i+hetero+2]) break;
hetero++;
}
if(ca>=homo && (ca>=hetero || homo>1)) {
// Output a copy-above run
out_run(170,ca,0,0);
i+=ca;
} else if(homo>1) {
// Output a homogeneous run
out_run(0,homo-1,i,1);
i+=homo;
} else {
// Output a heterogeneous run
if(hetero>85) hetero=85;
out_run(85,hetero,i,hetero);
i+=hetero;
}
if(outpiclen>=ms) return ms+1;
}
return outpiclen;
}
static inline void do_pictures(void) {
FILE*fp;
int i,j,k,s,t,x,y,z;
char meth[8];
int siz[8];
sprintf(nam,"%s.xclass",basename);
fp=fopen(nam,"w");
if(!fp) fatal("Cannot open file '%s' for writing\n",nam);
for(i=0;i<npic;i++) {
for(j=0;j<16;j++) {
if(picalloc[i]&(1<<j)) {
fwrite(nam,sprintf(nam,"%d.IMG",i*16+j)+1,1,fp);
for(z=k=t=0;z<8;z++) {
x=sizes[z];
if(!x) break;
for(y=0;y<x;y++) memcpy(inpic+y*x,pict+k+(i*x+y)*x*16+j*x,x);
meth[z]=15;
siz[z]=x*x;
for(y=0;y<8;y++) {
s=one_picture(x,y);
if(s<siz[z]) siz[z]=s,meth[z]=y;
}
t+=siz[z];
k+=x*x*16*npic;
}
if(z!=8) meth[z]=15;
t+=z+1+(z>>1);
fputc(t>>16,fp); fputc(t>>24,fp); fputc(t,fp); fputc(t>>8,fp);
fputc((*meth<<4)|z,fp);
for(z=0;z<8;z++) {
if(!sizes[z]) break;
fputc(sizes[z],fp);
}
for(z=1;z<8;z+=2) {
if(!sizes[z]) break;
fputc(meth[z]|(z==7?0xF0:meth[z+1]<<4),fp);
}
for(z=k=0;z<8;z++) {
x=sizes[z];
if(!x) break;
for(y=0;y<x;y++) memcpy(inpic+y*x,pict+k+(i*x+y)*x*16+j*x,x);
if(meth[z]==15) fwrite(inpic,x,x,fp);
else fwrite(outpic,1,one_picture(x,meth[z]),fp);
k+=x*x*16*npic;
}
}
}
}
fclose(fp);
}
static void unprefix_messages(const char*pre,int n) {
int i,j;
for(i=0;i<nusermsg;i++) {
if(usermsg[i] && !strncmp(pre,usermsg[i],n)) {
for(j=0;j<nusermsg;j++) if(j!=i && usermsg[j] && !strcmp(usermsg[i],usermsg[j])) goto skip;
memmove(usermsg[i],usermsg[i]+n,strlen(usermsg[i])+1-n);
}
skip: ;
}
}
static inline void do_user_messages(void) {
int i,j;
i=fgetc(stdin);
nusermsg=i|(fgetc(stdin)<<8);
Allocate(usermsg,nusermsg);
for(i=0;i<nusermsg;i++) {
j=fgetc(stdin);
if(j) {
Allocate(usermsg[i],j+1);
fread(usermsg[i],1,j,stdin);
usermsg[i][j]=0;
} else {
usermsg[i]=0;
}
}
unprefix_messages("MSG_",4);
unprefix_messages("USR_",4);
unprefix_messages("WTP_",4);
unprefix_messages("MSG_",4);
}
static void read_one_class(void) {
unsigned char buf[4];
int i,j,k,id;
char*name;
i=fgetc(stdin);
Allocate(name,i+1);
fread(name,1,i,stdin);
name[i]=0;
id=fgetc(stdin);
id|=fgetc(stdin)<<8;
if(id>=512) fatal("Class number %d out of range\n",id);
if(class[id]) fatal("Duplicate class number %d\n",id);
Allocate(class[id],1);
class[id]->name=name;
i=fgetc(stdin);
if(i==255) {
i=fgetc(stdin);
i|=fgetc(stdin)<<8;
}
fread(Allocate(class[id]->desc,i+1),1,i,stdin);
class[id]->desc[i]=0;
fread(class[id]->attr,1,62,stdin);
j=fgetc(stdin);
j|=fgetc(stdin)<<8;
class[id]->npic=j;
class[id]->defpic=256;
for(i=0;i<j;i++) {
k=fgetc(stdin);
k|=fgetc(stdin)<<8;
if(i<128) {
class[id]->pic[i]=k;
if(k<512 && class[id]->defpic==256 && picavail[k]) class[id]->defpic=i;
}
}
class[id]->defpic&=255;
if(j>128) class[id]->npic=128;
j=fgetc(stdin);
j|=fgetc(stdin)<<8;
class[id]->nvars=j;
fread(Allocate(class[id]->vars,j<<3),j,8,stdin);
j=fgetc(stdin);
j|=fgetc(stdin)<<8;
class[id]->nsubslbl=j;
fread(Allocate(class[id]->subslbl,10*j),j,10,stdin);
fread(buf,1,2,stdin);
k=buf[0]|(buf[1]<<8);
Allocate(class[id]->subscode,k<<1);
if(k) {
class[id]->subscode[0]=buf[0];
class[id]->subscode[1]=buf[1];
fread(class[id]->subscode+2,2,k-1,stdin);
}
j=fgetc(stdin);
j|=fgetc(stdin)<<8;
class[id]->nmsgslbl=j;
fread(Allocate(class[id]->msgslbl,10*j),j,10,stdin);
class[id]->nmsgs=0;
class[id]->msgscode=0;
for(;;) {
fread(buf,1,4,stdin);
k=buf[2]|(buf[3]<<8);
if(!k) break;
i=class[id]->nmsgs++;
Reallocate(class[id]->msgscode,class[id]->nmsgs);
Allocate(class[id]->msgscode[i],k<<1);
memcpy(class[id]->msgscode[i],buf,4);
fread(class[id]->msgscode[i]+4,k-2,2,stdin);
}
}
static inline void do_classes(void) {
int n;
n=fgetc(stdin);
n|=fgetc(stdin)<<8;
while(n--) read_one_class();
}
static void out_description(FILE*fp,const char*txt) {
// CLASS section
if(!strncmp("[CLASS]\r\n",txt,9)) txt+=9;
while(txt[0]=='\r' && txt[1]=='\n') txt+=2;
if(!strncmp("[LEVEL]\r\n",txt,9)) goto level_area;
if(!strncmp("[GAME]\r\n",txt,8)) goto game_area;
fprintf(fp," ; ");
for(;;) {
if(*txt=='\r') {
if(!strncmp("\r\n[LEVEL]\r\n",txt,11)) {
fputc('\n',fp);
txt+=11;
goto level_area;
} else if(!strncmp("\r\n[GAME]\r\n",txt,10)) {
fputc('\n',fp);
txt+=10;
goto game_area;
} else {
fprintf(fp,"\n ; ");
txt+=2;
}
} else if(!*txt) {
fputc('\n',fp);
return;
} else {
fputc(*txt++,fp);
}
}
// LEVEL section
level_area:
if(!strncmp("[LEVEL]\r\n",txt,9)) txt+=9;
while(txt[0]=='\r' && txt[1]=='\n') txt+=2;
if(!strncmp("[GAME]\r\n",txt,8)) goto game_area;
fprintf(fp," (EditorHelp\n \"");
for(;;) {
if(*txt=='\r') {
if(!strncmp("\r\n[GAME]\r\n",txt,10)) {
fprintf(fp,"\"\n )\n");
txt+=10;
goto game_area;
} else {
fprintf(fp,"\"\n \"");
txt+=2;
}
} else if(!*txt) {
fprintf(fp,"\"\n )\n");
return;
} else {
if(*txt=='"') fputc('\\',fp);
fputc(*txt++,fp);
}
}
// GAME section
game_area:
if(!strncmp("[GAME]\r\n",txt,8)) txt+=8;
while(txt[0]=='\r' && txt[1]=='\n') txt+=2;
fprintf(fp," (Help\n \"");
for(;;) {
if(*txt=='\r') {
if(txt[1]=='\n' && !txt[2]) {
fprintf(fp,"\"\n )\n");
return;
} else {
fprintf(fp,"\"\n \"");
txt+=2;
}
} else if(!*txt) {
fprintf(fp,"\"\n )\n");
return;
} else {
if(*txt=='"') fputc('\\',fp);
fputc(*txt++,fp);
}
}
}
#define SubOpcode(...) do{ static const char*const ss[]={__VA_ARGS__}; if(*op>=sizeof(ss)/sizeof(*ss)) goto unknown; fprintf(fp," %s",ss[*op]); }while(0)
#define PushFlowControl() do{ flowblock[flowptr]=ofs+2+(op[1]<<1)+(op[2]<<9); if(++flowptr==64) fatal("Too many nested flow controls\n"); len+=2; ind+=2; }while(0)
static void class_codes(FILE*fp,const unsigned char*op,int ofs,const unsigned char*lbl,int nlbl,const unsigned char*subs,int nsubs,unsigned char*vars,const char*cname) {
static const char*const stdvars[256]={
[0]="Class","Temperature","Shape",
[4]="Xloc","Yloc","Dir","Image","Inertia","Misc1","Misc2","Misc3","Misc4","Misc5","Misc6","Misc7",
[16]="Arrived","Departed","Arrivals","Departures",
[32]="Busy","Invisible","UserSignal","UserState","KeyCleared","Player","Destroyed","Stealthy","VisualOnly",
[48]="Msg","From","Arg1","Arg2",
[64]="Density","Volume","Strength","Weight","Distance","Height","Climb",
[72]="Hard","Hard","Hard","Hard","Sharp","Sharp","Sharp","Sharp","ShapeDir","ShapeDir","ShapeDir","ShapeDir",
[84]="Shovable",
};
static const char*const direction[16]={"E","NE","N","NW","W","SW","S","SE","F","LF","L","LB","B","RB","R","RF"};
int flowblock[64];
int flowptr=0;
int ind=3;
int st=0;
int lix=0;
int remlbl=nlbl;
int x,y,z,len;
while(remlbl && (lbl[lix+8]|(lbl[lix+9]<<8))<ofs>>1) {
remlbl--;
lix+=10;
}
for(;;) {
while(flowptr && flowblock[flowptr-1]==ofs) {
--flowptr;
fprintf(fp,"\n%*s then",ind-=2,"");
}
if(*op==102 && op[1]!=1) return;
if(!st) st=fprintf(fp,"\n%*s",ind,"");
while(remlbl && (lbl[lix+8]|(lbl[lix+9]<<8))==ofs>>1) {
fprintf(fp," :%s\n%*s",lbl+lix,ind,"");
remlbl--;
lix+=10;
}
len=1;
switch(*op++) {
case 1 ... 4:
if(!stdvars[*op]) goto unknown;
if(*op>=72 && *op<=83) fprintf(fp," %c","ENWSENWSENWS"[*op&3],stdvars[*op]);
fputc(' ',fp);
if(op[-1]>2) fputc('=',fp);
if(!(op[-1]&1)) fputc(',',fp);
fprintf(fp,"%s",stdvars[*op]);
if(op[-1]>2) st=0;
break;
case 5:
fprintf(fp," %%%s",vars+*op*8);
break;
case 6:
fprintf(fp," =%%%s",vars+*op*8);
st=0; break;
case 7:
fprintf(fp," %d",*op);
break;
case 8:
fprintf(fp," %u",op[1]|(op[2]<<8)|(op[3]<<16)|(op[4]<<24));
len+=4; break;
case 9:
if(!class[*op]) goto unknown;
fprintf(fp," $%s",class[*op]->name);
break;
case 11:
if(*op<20) {
fprintf(fp," %s",standard_message_names[*op]);
} else {
if(*op-20>=nusermsg || !usermsg[*op-20]) goto unknown;
fprintf(fp," #%s",usermsg[*op-20]);
}
break;
case 13:
fprintf(fp," \"");
len+=x=op[1]|(op[2]<<8);
for(y=0;y<x;y++) switch(z=op[y+3]) {
case 0: break; // do nothing
case 1 ... 8: fprintf(fp,"\\%c",z+'0'-1); break;
case 10: fprintf(fp,"\\n"); break;
case 11: fprintf(fp,"\\l"); break;
case 12: fprintf(fp,"\\c"); break;
case 14: fprintf(fp,"\\i%s:%d\\",class[op[y+4]|(op[y+5]<<8)]->name,op[y+6]|(op[y+7]<<8)); y+=4; break;
case 15: fprintf(fp,"\\b"); break;
case 16: fprintf(fp,"\\q"); break;
case 9: case 13: case 17 ... 31: case 127 ... 255: fprintf(fp,"\\x%02X",z); break;
case '\\': fputc(z,fp); //fallthrough
default: fputc(z,fp);
}
fputc('"',fp);
len+=2; break;
case 14:
if(*op<16) fprintf(fp," %s",direction[*op]);
else fprintf(fp," %d",*op);
break;
case 15:
for(x=0;x<nsounds;x++) {
if(soundid[x]==*op) {
fprintf(fp," !%s",sounds[x]);
break;
}
}
if(x==nsounds) goto unknown;
break;
case 16:
SubOpcode("neg","bnot","lnot");
break;
case 17:
SubOpcode("*","/","mod");
break;
case 18:
SubOpcode("+","-","band","bor","bxor");
break;
case 19:
SubOpcode("lsh","rsh");
break;
case 20:
SubOpcode("eq","ne","lt","gt","le","ge");
break;
case 21:
SubOpcode("land","lor","lxor");
break;
case 32:
if(*op==255) fprintf(fp," ObjDir");
else fprintf(fp," %s ObjDir",direction[*op]);
break;
case 33:
if(*op==255) fprintf(fp," ,ObjDir");
else fprintf(fp," %s ,ObjDir",direction[*op]);
break;
case 34:
fprintf(fp," ObjAbove");
break;
case 35:
fprintf(fp," ,ObjAbove");
break;
case 36:
fprintf(fp," ObjBelow");
break;
case 37:
fprintf(fp," ,ObjBelow");
break;
case 38:
SubOpcode("ObjTopAt","ObjBottomAt","VolumeAt","HeightAt","Delta");
break;
case 39:
fprintf(fp," Self");
break;
case 40:
fprintf(fp," ObjClassAt");
break;
case 47:
z=-1;
for(x=0;x<nsubs;x++) {
if(op[1]==subs[x*10+8] && op[2]==subs[x*10+9]) {
z=x; break;
}
}
if(z==-1) fprintf(fp," @???"); else fprintf(fp," @%s.%s",cname,subs+z*10);
len+=2; break;
case 48:
fprintf(fp," Key");
break;
case 49:
SubOpcode("STOP","ONCE","LOOP","3","4","5","6","7","OSC","9","OSCLOOP");
break;
case 50:
if(heromesh_key_names[*op]) fprintf(fp," '%s",heromesh_key_names[*op]);
else fprintf(fp," %d",*op);
break;
case 51:
fprintf(fp," 0x%02X",*op);
break;
case 52:
fprintf(fp," 0x%X",op[1]|(op[2]<<8)|(op[3]<<16)|(op[4]<<24));
len+=4; break;
case 53:
SubOpcode("Level","-1","0","1","0","{PuzzleSetNumber}","MoveNumber");
break;
case 54:
if(*op==255) fprintf(fp," XDir");
else fprintf(fp," %s XDir",direction[*op]);
break;
case 55:
if(*op==255) fprintf(fp," ,XDir");
else fprintf(fp," %s ,XDir",direction[*op]);
break;
case 56:
if(*op==255) fprintf(fp," YDir");
else fprintf(fp," %s YDir",direction[*op]);
break;
case 57:
if(*op==255) fprintf(fp," ,YDir");
else fprintf(fp," %s ,YDir",direction[*op]);
break;
case 58:
fprintf(fp," New%c",*op+'X');
break;
case 59:
fprintf(fp," bit%d",*op);
break;
case 60:
if(*op<50) fprintf(fp," %s",standard_sound_names[*op]);
else goto unknown;
break;
case 61:
if(*op!=1) goto unknown;
fprintf(fp," GetArray");
break;
case 64:
fprintf(fp," Send");
break;
case 65:
fprintf(fp," ,Send");
break;
case 66:
fprintf(fp," (Broadcast $%s)",class[*op]->name);
break;
case 67:
fprintf(fp," Broadcast");
break;
case 68:
if(*op==255) fprintf(fp," Move");
else fprintf(fp," %s Move+",direction[*op]);
break;
case 69:
if(*op==255) fprintf(fp," ,Move");
else fprintf(fp," %s ,Move",direction[*op]);
break;
case 70:
fprintf(fp," JumpTo");
break;
case 71:
fprintf(fp," ,JumpTo");
break;
case 72:
fprintf(fp," Create");
break;
case 74:
fprintf(fp," Destroy");
break;
case 75:
fprintf(fp," ,Destroy");
break;
case 80:
fprintf(fp," Send .");
st=0; break;
case 81:
fprintf(fp," ,Send .");
st=0; break;
case 82:
fprintf(fp," (Broadcast $%s) .",class[*op]->name);
st=0; break;
case 83:
fprintf(fp," Broadcast .");
st=0; break;
case 84:
if(*op==255) fprintf(fp," Move .");
else fprintf(fp," %s Move+ .",direction[*op]);
st=0; break;
case 85:
if(*op==255) fprintf(fp," ,Move .");
else fprintf(fp," %s ,Move .",direction[*op]);
st=0; break;
case 88:
fprintf(fp," Create .");
st=0; break;
case 96:
fprintf(fp," ;");
len+=x=op[1]|(op[2]<<8);
for(y=0;y<x;y++) switch(z=op[y+3]) {
case 0: case 13: break; // do nothing
case 10: fprintf(fp,"\n%*s ;",ind,""); break;
default: fputc(z,fp);
}
len+=2; st=0; break;
case 98:
fprintf(fp," Destroy .");
st=0; break;
case 99:
fprintf(fp," ,Destroy .");
st=0; break;
case 100:
z=-1;
for(x=0;x<nsubs;x++) {
if(op[1]==subs[x*10+8] && op[2]==subs[x*10+9]) {
z=x; break;
}
}
if(z==-1) fprintf(fp," ,???"); else fprintf(fp," ,:%s",subs+z*10);
len+=2; st=0; break;
case 101:
z=-1;
for(x=0;x<nlbl;x++) {
if(op[1]==lbl[x*10+8] && op[2]==lbl[x*10+9]) {
z=x; break;
}
}
if(z==-1) fprintf(fp," =???"); else fprintf(fp," =:%s",lbl+z*10);
len+=2; st=0; break;
case 102: case 104:
fprintf(fp," ret");
st=0; break;
case 103:
fprintf(fp," %d ret",*op);
st=0; break;
case 105:
fprintf(fp," if");
PushFlowControl();
st=0; break;
case 106:
fseek(fp,-1,SEEK_CUR);
fprintf(fp,"else");
if(flowptr) flowptr--,ind-=2;
PushFlowControl();
st=0; break;
case 109:
if(*op) fprintf(fp," (PopUp %d)",*op); else fprintf(fp," PopUp");
st=0; break;
case 110:
fprintf(fp," JumpTo .");
st=0; break;
case 111:
fprintf(fp," ,JumpTo .");
st=0; break;
case 112: case 113:
fprintf(fp," Sound");
st=0; break;
case 114:
if(*op) {
SubOpcode(";","SetArray","InitArray");
} else {
x=0;
y=nlbl;
z=-1;
while(y) {
if((lbl[x+8]|(lbl[x+9]<<8))==(ofs>>1)) {
z=x;
break;
}
y--;
x+=10;
}
x=op[1]|(op[2]<<8);
y=op[3]|(op[4]<<8);
if(z!=-1) fprintf(fp," {Array @%s.%s %d %d}",cname,lbl+z,x,y);
len+=4*(x*y+1);
has_array=1;
}
st=0; break;
case 126:
fprintf(fp," Animate");
st=0; break;
case 127: case 128:
fprintf(fp," LoseLevel");
st=0; break;
case 129:
SubOpcode("WinLevel","LocateMe","IgnoreKey","Misc1 Misc2 Misc3 (PopUp 2)",";UpdateScreen");
st=0; break;
case 130:
fprintf(fp," FlushClass");
st=0; break;
case 131:
fprintf(fp," ,FlushObj");
st=0; break;
case 132:
fprintf(fp," SetInventory 5 MaxInventory");
st=0; break;
case 133:
fprintf(fp," DelInventory");
st=0; break;
case 134:
fprintf(fp," for =%%%s",vars+*op*8);
ind+=2; len+=2; st=0; break;
case 135:
fseek(fp,-1,SEEK_CUR);
fprintf(fp,"next");
ind-=2; len+=6; st=0; break;
case 240:
fprintf(fp," Trace");
st=0; break;
default: unknown:
fprintf(fp," (??? %d %d)",op[-1],op[0]);
st=0;
}
ofs+=len+1;
op+=len;
}
}
static inline void out_classes(void) {
FILE*fp;
ClassInfo*c;
int i,j,n,o;
sprintf(nam,"%s.class",basename);
fp=fopen(nam,"w");
if(!fp) fatal("Cannot open file '%s' for writing\n",nam);
fprintf(fp,"; Note: This file was automatically converted.\n; Remove this notice if you have corrected it.\n\n");
fprintf(fp,"{define \"Array\" {append \"array_defs\" (\\1 Array \\2 \\3)}}\n");
fprintf(fp,"{define \"PuzzleSetNumber\" %lu}\n",puzzlesetnumber);
for(n=0;n<512;n++) if(c=class[n]) {
fprintf(fp,"\n($%s Compatible",c->name);
if(c->attr[1]&1) fprintf(fp," Input");
if(c->attr[1]&128) fprintf(fp," Player");
if(!strcmp(c->name,"Quiz")) fprintf(fp," Quiz");
fprintf(fp,"\n (Image");
for(j=i=0;i<c->npic;i++) {
fprintf(fp," \"%d\"",c->pic[i]);
if(picavail[c->pic[i]]) j++;
}
fprintf(fp,")\n");
if(!j) {
fprintf(fp," (DefaultImage ())\n");
} else if(j!=c->npic) {
fprintf(fp," (DefaultImage");
for(i=0;i<c->npic;i++) if(picavail[c->pic[i]]) fprintf(fp," %d",i);
fprintf(fp,")\n");
}
out_description(fp,c->desc);
if(c->attr[2] || c->attr[3]) fprintf(fp," (Misc4 0x%04X)\n",c->attr[2]|(c->attr[3]<<8));
if(c->attr[4] || c->attr[5]) fprintf(fp," (Misc5 0x%04X)\n",c->attr[4]|(c->attr[5]<<8));
if(c->attr[6] || c->attr[7]) fprintf(fp," (Misc6 0x%04X)\n",c->attr[6]|(c->attr[7]<<8));
if(c->attr[8] || c->attr[9]) fprintf(fp," (Misc7 0x%04X)\n",c->attr[8]|(c->attr[9]<<8));
if(c->attr[50] || c->attr[51]) fprintf(fp," (Temperature %d)\n",c->attr[50]|(c->attr[51]<<8));
if(c->attr[10]) {
if(c->attr[10]==0x55) {
fprintf(fp," Shovable\n");
} else {
fprintf(fp," (Shovable");
if(c->attr[10]&0xAA) {
// I don't know why, but sometimes this happens.
fprintf(fp," 0x%02X",c->attr[10]);
} else {
if(c->attr[10]&0x01) fprintf(fp," E");
if(c->attr[10]&0x04) fprintf(fp," N");
if(c->attr[10]&0x10) fprintf(fp," W");
if(c->attr[10]&0x40) fprintf(fp," S");
}
fprintf(fp,")\n");
}
}
if(!c->attr[12] && !c->attr[13] && !c->attr[15] && !c->attr[16] && !(c->attr[14]&~4)) {
if(c->attr[14]) fprintf(fp," (Arrivals InPlace)\n");
} else {
fprintf(fp," (Arrivals\n");
for(i=0;i<5;i++) {
fprintf(fp," ");
for(j=0;j<5;j++) fprintf(fp," %c",(c->attr[i+12]<<j)&16?'1':'0');
fputc('\n',fp);
}
fprintf(fp," )\n");
}
if(!c->attr[17] && !c->attr[18] && !c->attr[20] && !c->attr[21] && !(c->attr[19]&~4)) {
if(c->attr[19]) fprintf(fp," (Departures InPlace)\n");
} else {
fprintf(fp," (Departures\n");
for(i=0;i<5;i++) {
fprintf(fp," ");
for(j=0;j<5;j++) fprintf(fp," %c",(c->attr[i+17]<<j)&16?'1':'0');
fputc('\n',fp);
}
fprintf(fp," )\n");
}
if(c->attr[22] || c->attr[23]) fprintf(fp," (Density %d)\n",c->attr[22]|(c->attr[23]<<8));
if(c->attr[24] || c->attr[25]) fprintf(fp," (Volume %d)\n",c->attr[24]|(c->attr[25]<<8));
if(c->attr[26] || c->attr[27]) fprintf(fp," (Strength %d)\n",c->attr[26]|(c->attr[27]<<8));
if(c->attr[28] || c->attr[29]) fprintf(fp," (Weight %d)\n",c->attr[28]|(c->attr[29]<<8));
if(c->attr[46] || c->attr[47]) fprintf(fp," (Height %d)\n",c->attr[46]|(c->attr[47]<<8));
if(c->attr[48] || c->attr[49]) fprintf(fp," (Climb %d)\n",c->attr[48]|(c->attr[49]<<8));
switch(i=c->attr[52]) {
case 0x00:
// Nothing
break;
case 0x55: case 0xAA: case 0xFF:
fprintf(fp," (Shape %d)\n",i&3);
break;
default:
fprintf(fp," (Shape");
if(i&0x03) fprintf(fp," (E %d)",(i>>0)&3);
if(i&0x0C) fprintf(fp," (N %d)",(i>>2)&3);
if(i&0x30) fprintf(fp," (W %d)",(i>>4)&3);
if(i&0xC0) fprintf(fp," (S %d)",(i>>6)&3);
fprintf(fp,")\n");
}
if(memcmp(c->attr+30,"\0\0\0\0\0\0\0\0",8)) {
if(memcmp(c->attr+30,c->attr+32,2) || memcmp(c->attr+30,c->attr+34,4)) {
fprintf(fp," (Hard");
if(c->attr[30] || c->attr[31]) fprintf(fp," (E %d)",c->attr[30]|(c->attr[31]<<8));
if(c->attr[32] || c->attr[33]) fprintf(fp," (N %d)",c->attr[32]|(c->attr[33]<<8));
if(c->attr[34] || c->attr[35]) fprintf(fp," (W %d)",c->attr[34]|(c->attr[35]<<8));
if(c->attr[36] || c->attr[37]) fprintf(fp," (S %d)",c->attr[36]|(c->attr[37]<<8));
fprintf(fp,")\n");
} else {
fprintf(fp," (Hard %d)\n",c->attr[30]|(c->attr[31]<<8));
}
}
if(memcmp(c->attr+38,"\0\0\0\0\0\0\0\0",8)) {
if(memcmp(c->attr+38,c->attr+40,2) || memcmp(c->attr+38,c->attr+42,4)) {
fprintf(fp," (Sharp");
if(c->attr[38] || c->attr[39]) fprintf(fp," (E %d)",c->attr[38]|(c->attr[39]<<8));
if(c->attr[40] || c->attr[41]) fprintf(fp," (N %d)",c->attr[40]|(c->attr[41]<<8));
if(c->attr[42] || c->attr[43]) fprintf(fp," (W %d)",c->attr[42]|(c->attr[43]<<8));
if(c->attr[44] || c->attr[45]) fprintf(fp," (S %d)",c->attr[44]|(c->attr[45]<<8));
fprintf(fp,")\n");
} else {
fprintf(fp," (Sharp %d)\n",c->attr[38]|(c->attr[39]<<8));
}
}
if(c->subscode[1] || c->subscode[0]>2) {
fprintf(fp," (SUBS");
class_codes(fp,c->subscode+2,2,c->subslbl,c->nsubslbl,c->subslbl,c->nsubslbl,c->vars,c->name);
fprintf(fp,"\n )\n");
}
o=4;
for(i=0;i<c->nmsgs;i++) {
fprintf(fp," (");
j=c->msgscode[i][0]|(c->msgscode[i][1]<<8);
if(j<20) fprintf(fp,"%s",standard_message_names[j]);
else if(j-20<nusermsg && usermsg[j-20]) fprintf(fp,"#%s",usermsg[j-20]);
else fprintf(fp,"#???%d",j);
class_codes(fp,c->msgscode[i]+4,o,c->msgslbl,c->nmsgslbl,c->subslbl,c->nsubslbl,c->vars,c->name);
o+=(c->msgscode[i][2]<<1)|(c->msgscode[i][3]<<9);
fprintf(fp,"\n )\n");
}
fprintf(fp,")\n");
}
if(has_array) fprintf(fp,"\n{array_defs}\n");
fclose(fp);
}
static void one_level(FILE*fp,int ord) {
unsigned char buf[16];
unsigned char mru[32];
int i,j,x,y,n,q,r;
i=fgetc(stdin);
i|=fgetc(stdin)<<8;
levelid[ord]=i;
sprintf(nam,"%d.LVL",i);
hamarc_begin(fp);
fputc(0,fp); fputc(0,fp); // Level version
fputc(ord,fp); fputc(ord>>8,fp); // Level code
fputc(28,fp); fputc(20,fp); // One less than width/height
// (Width/height can each be up to 64 (stored as 63). Bit6 and bit7 of
// these numbers are extra header flags, currently unused.)
i=fgetc(stdin);
i|=fgetc(stdin)<<8;
// fputc(17,fp); // Select proportional font
while(i--) {
switch(j=fgetc(stdin)) {
case 14:
i-=4;
j=fgetc(stdin);
j|=fgetc(stdin)<<8;
if(j>=512 || !class[j]) {
fgetc(stdin); fgetc(stdin);
break;
}
fputc(14,fp);
fputs(class[j]->name,fp);
j=fgetc(stdin);
j|=fgetc(stdin)<<8;
fprintf(fp,":%d\\",j);
break;
default: fputc(j,fp);
}
}
fread(mru,1,32,stdin); // Skip border colours
fread(buf,1,2,stdin); // Skip border colours
mru[0x04]=mru[0x14]=255;
n=fgetc(stdin);
n|=fgetc(stdin)<<8;
x=q=r=0;
y=1;
// Hero Mesh level format for objects:
// 0-1 Class
// 2-3 CurImage
// 4 LastDir
// 5 Misc Types
// 6-7 X
// 8-9 Y
// 10-15 Misc1,Misc2,Misc3
// Free Hero Mesh level format for objects:
// * bit flags (or 0xFF for end):