-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.c
More file actions
6854 lines (6736 loc) · 256 KB
/
game.c
File metadata and controls
6854 lines (6736 loc) · 256 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 -std=gnu99 -Wno-unused-result -fwrapv game.c `sdl-config --cflags`
exit
#endif
#include "common.h"
#include "opcodes.h"
#include <math.h>
#include <time.h>
ElementDef elem_def[256];
Uint8 appearance_mapping[128];
Animation animation[4];
Uint16 cur_board_id;
BoardInfo board_info;
Tile*b_under;
Tile*b_main;
Tile*b_over;
Stat*stats;
Uint8 maxstat;
NumericFormat num_format[16];
Screen cur_screen;
Uint16 cur_screen_id;
Sint32 scroll_x,scroll_y; // relative to screen(0,0)
Uint16 memory[0x10000];
Sint32 regs[8];
Uint8 condflag;
Uint8**gtext;
Uint8*vgtext;
Uint16 ngtext;
Sint32 status_vars[16];
Uint8**boardnames;
Uint16 maxboard;
Uint8 textbuf[81];
Uint8 ntextbuf;
Uint8 vtextbuf[81];
Uint8 nvtextbuf;
Uint16 vtexttime;
NamedFlag namedflag[16];
Uint8*global_text;
Uint16 global_length;
Uint16 global_frameoffset;
Uint16 global_frameptr;
DynaString*dynastr;
Uint8 ndynastr;
Uint16 start_mode=1;
VarProperty pvarproperty;
Uint8*itemnames;
ItemDef*itemdefs;
Uint16 nitemdefs;
Inventory inventory[8];
UnordZone*uzone[16];
OrdZone*ozone[16];
ASN1_Value asn1reg[4];
static uint64_t rseed;
static char soundon;
static Uint8 autofire=0;
static Sint8 autofire_dir=-1;
#define PLAYSTATE_NORMAL 16
#define PLAYSTATE_FAST 175
#define PLAYSTATE_PAUSED 186
static Uint8 playstate=PLAYSTATE_NORMAL;
static Uint8 saved_playstate=PLAYSTATE_NORMAL;
typedef struct {
Uint8 text[81];
} MessageScrollback;
static MessageScrollback*scrback;
static Uint16 nscrback;
#define TEXTREC 81
static FILE*textfile;
static char*textfile_text;
static size_t textfile_size;
// Text window
static Uint16 tnlines,tcursor,tscroll;
static const char*end_of_label;
static Sint32 run_program(Uint16 pc,Sint32 w,Sint32 x,Sint32 y,Sint32 z);
static Sint32 give_item(Uint32 item,Uint32 qty,Uint16 how);
static Sint32 take_item(Uint32 item,Uint32 qty,Uint16 how);
static Sint32 move_item(Uint32 item,Uint32 qty,Uint16 how);
static Sint32 count_items(Sint32 t,Uint32 m);
static Uint32 do_joystick(Uint8 mode) {
Uint8 b=event.jbutton.button;
Uint16 v;
if((joystat->user_shift&0x200) && (v=(joystat->user_shift>>5)&7)) v+=16; else v=mode;
v=joystat->map[b].a[v];
if(v==0x300) {
if(joystat->world_shift&0x200) v=(joystat->world_shift>>5)&15; else v=(memory[MEM_JOY_LEVEL]>>(4*(mode&3)))&15;
v=joystat->map[b].a[v];
}
if(event.type==SDL_JOYBUTTONDOWN) {
switch(v) {
case 0x000 ... 0x07F: return v;
case 0x080 ... 0x0FF: autofire=v&=0x7F; autofire_dir=-1; return v;
case 0x100 ... 0x17F: return v;
case 0x200 ... 0x20F: joystat->world_shift=(joystat->world_shift<<10)+0x200+((v&0x0F)<<5)+b; v_status[79]='w'; return 0;
case 0x210 ... 0x217: joystat->user_shift=(joystat->user_shift<<10)+0x200+((v&0x0F)<<5)+b; v_status[79]='u'; return 0;
default: return 0;
}
} else {
if(joystat->state) {
while((joystat->user_shift&0x200) && !((1ULL<<(joystat->user_shift&0x1F))&joystat->state)) joystat->user_shift>>=10;
while((joystat->world_shift&0x200) && !((1ULL<<(joystat->world_shift&0x1F))&joystat->state)) joystat->world_shift>>=10;
} else {
joystat->world_shift=joystat->user_shift=autofire=v_status[79]=0;
}
switch(v) {
case 0x080 ... 0x0FF: if(autofire==(v&0x7F)) autofire=0; return 0;
case 0x100 ... 0x17F: return v+0x80;
default: return 0;
}
}
}
static void debug_log(Uint8 fo,Sint32 so,Sint32 w,Sint32 x,Sint32 y,Sint32 z,Uint16 pc) {
FILE*f=stdout; // should it be configurable?
int i;
fprintf(f,"[$%04X] %ld ($%lX) : [%c]",pc,(long)so,(unsigned long)so,fo+'A');
switch(fo) {
case 0: // All registers
for(i=0;i<8;i++) fprintf(f," %c=%ld(%lX)",i+'A',(long)regs[i],(unsigned long)regs[i]);
fprintf(f," W=%ld(%lX) X=%ld(%lX) Y=%ld(%lX) Z=%ld(%lX)",(long)w,(unsigned long)w,(long)x,(unsigned long)x,(long)y,(unsigned long)y,(long)z,(unsigned long)z);
fprintf(f," ?=%d",condflag);
break;
case 1: // Board/screen info
fprintf(f," B#%d %dx%d S#%d %+ld%+ld",cur_board_id,board_info.width,board_info.height,cur_screen_id,(long)scroll_x,(long)scroll_y);
break;
case 2: // Text buffer
fprintf(f," %d {",ntextbuf);
for(i=0;i<ntextbuf;i++) if(textbuf[i]>=0x20 && textbuf[i]<0x7F) fputc(textbuf[i],f); else fprintf(f,"<%02X>",textbuf[i]);
fputc('}',f);
break;
case 3: // Status variables
for(i=0;i<16;i++) fprintf(f," %c=%ld",i+(i&8?'S'-8:'A'),(long)status_vars[i]);
break;
case 4: // Memory management
fputs("(Not implemented)",f);
break;
case 5: // Stats
for(i=0;i<maxstat;i++) fprintf(f,"\n %d: count=%d xy=%p length=%d text=%p speed=%d",i+1,stats[i].count,stats[i].xy,stats[i].length,stats[i].text,stats[i].speed);
break;
case 6: // Inventory
for(i=0;i<inventory[so&7].count;i++)
fprintf(f,"\n q=%lu ext=%lu,%u,%u i=%u f=(%lX)",(long)inventory[so&7].item[i].quantity,(long)inventory[so&7].item[i].ext0,inventory[so&7].item[i].ext1,inventory[so&7].item[i].ext2,
inventory[so&7].item[i].item,(long)inventory[so&7].item[i].flag);
break;
}
fputc('\n',f);
}
static int allow_saving(void) {
int i;
if(!(memory[MEM_CONTROL]&CONTROL_DISABLE_SAVING)) return 1;
if(!(board_info.flag&0x0300) || !maxstat || !stats->count) return 0;
for(i=0;i<stats->count;i++) {
if((board_info.flag&BF_SAVE_ON_SENSOR) && stats->xy[i].sensor.kind) return 1;
if(stats->xy[i].x<board_info.width && stats->xy[i].y<board_info.height) {
if(((elem_def[stats->xy[i].sensor.kind].attrib|elem_def[b_under[stats->xy[i].y*board_info.width+stats->xy[i].x].kind].attrib|elem_def[b_main[stats->xy[i].y*board_info.width+stats->xy[i].x].kind].attrib)
&A_SENSOR?BF_SAVE_ON_SENSOR:BF_SAVE_NOT_SENSOR)&board_info.flag) return 1;
}
}
return 0;
}
#define CBRANDOM_KEY 6738671342737314685ULL
static inline Uint32 cbrandom(Uint64 c) {
// Square RNG counter-based random numbers.
Uint64 x=CBRANDOM_KEY*c;
Uint64 y=x;
Uint64 z=CBRANDOM_KEY+y;
x=x*x+y; x=(x>>32)|(x<<32);
x=x*x+z; x=(x>>32)|(x<<32);
x=x*x+y; x=(x>>32)|(x<<32);
return (x*x+z)>>32;
}
Uint32 dice(Uint32 n) {
Uint32 o;
Uint32 m=n-1;
m|=m>>1; m|=m>>2; m|=m>>4; m|=m>>8; m|=m>>16;
do {
rseed^=rseed>>12; rseed^=rseed<<25; rseed^=rseed>>27;
o=((rseed*0x2545F4914F6CDD1DULL)>>32)&m;
} while(o>=n);
return o;
}
Uint32 reseed(uint64_t n) {
rseed=n?:((uint64_t)time(0))?:(uint64_t)1;
rseed^=rseed>>12; rseed^=rseed<<25; rseed^=rseed>>27;
rseed^=rseed>>12; rseed^=rseed<<25; rseed^=rseed>>27;
rseed++;
if(!rseed) rseed=1;
rseed^=rseed>>12; rseed^=rseed<<25; rseed^=rseed>>27;
rseed^=rseed>>12; rseed^=rseed<<25; rseed^=rseed>>27;
}
const char*select_board(Uint16 b) {
FILE*fp=0;
const char*e;
if(maxstat && stats->text==global_text) stats->text=0;
if((memory[MEM_CONTROL]&CONTROL_RESTORE_BOARD) && (fp=open_lump("PREVIOUS.BRD","r")) && !lump_size) fclose(fp),fp=0;
if(!fp) fp=open_lump_by_number(b,"BRD","r");
if(fp) {
e=load_board(fp);
fclose(fp);
if(!e && (memory[MEM_CONTROL]&CONTROL_RESTORE_BOARD)) revert_lump("PREVIOUS.BRD");
return e;
} else {
return "Cannot open lump";
}
}
static void load_script_library(Stat*s,const Uint8*name) {
FILE*fp;
char buf[16];
int i;
for(i=0;i<8;i++) {
if(name[i]=='.' || name[i]==';' || name[i]<39) break;
buf[i]=name[i];
}
buf[i]='.'; buf[i+1]='L'; buf[i+2]='I'; buf[i+3]='B'; buf[i+4]=0;
fp=open_lump(buf,"r");
if(!fp) errx(1,"Cannot open %s",buf);
if(lump_size>65530) errx(1,"Script library is too big");
if(s->text!=global_text) free(s->text);
s->text=malloc(lump_size+1);
if(!s->text) err(1,"Allocation failed");
fread(s->text,1,s->length=lump_size,fp);
s->text[s->length]=0;
fclose(fp);
}
static Uint16 do_asn1_operator(Uint8 fo,Uint16 op,Uint16 pc) {
FILE*fp;
ASN1_Encoder*enc;
ASN1_Value*r1=asn1reg+(op&3);
ASN1_Value*r2=asn1reg+((op>>2)&3);
ASN1_Value u,v;
size_t s;
Uint16 pc1=pc;
Uint32 n;
int q;
switch(op>>8) {
case 0x00: if(r1==r2) break; asn1_free(r1); if(asn1_copy(r2,r1)) err(1,"Allocation failed"); break;
case 0x01: if(r1==r2) break; asn1_free(r1); *r1=*r2; *r2=(ASN1_Value){.type=ASN1_NULL}; break;
case 0x02:
fp=open_lump_by_number(regs[fo]&0xFFFF,"USD","w");
if(!fp) errx(1,"Cannot open .USD lump");
asn1_write_type(r1->constructed,r1->class,r1->type,fp);
asn1_write_length(r1->length,fp);
fwrite(r1->data,1,r1->length,fp);
fclose(fp);
break;
case 0x03:
asn1_free(r1);
fp=open_lump_by_number(regs[fo]&0xFFFF,"USD","r");
if(!fp) errx(1,"Cannot open .USD lump");
if(asn1_read_item(fp,r1,0)) errx(1,"Error reading from .USD lump");
fclose(fp);
break;
case 0x04: memory[MEM_ARG_J]=r1->class; memory[MEM_ARG_K]=r1->type; regs[fo]=r1->length; condflag=r1->constructed; break;
case 0x05: r1->class=memory[MEM_ARG_J]; r1->type=memory[MEM_ARG_K]; r1->constructed=condflag; break;
case 0x06: case 0x07:
asn1_free(r1);
if(!(enc=asn1_start_encoding_value(r1))) err(1,"Allocation failed");
if(op&0x100?asn1_encode_uint32(enc,regs[fo]):asn1_encode_int32(enc,regs[fo])) errx(1,"Error encoding ASN.1 value");
if(asn1_finish_encoder(enc)) err(1,"Allocation failed");
break;
case 0x08: condflag=!asn1_decode_int32(r1,ASN1_AUTO,(void*)(regs+fo)); break;
case 0x09: condflag=!asn1_decode_uint32(r1,ASN1_AUTO,(void*)(regs+fo)); break;
#ifndef CONFIG_DISABLE_FRONT
case 0x0A: asn1_encode(extern_out,r1); asn1_flush(extern_out); break;
#endif
case 0x0B:
condflag=1;
for(s=0;condflag && s<r1->length;s++) {
if(ntextbuf>=80 || !r1->data[s]) condflag=0; else textbuf[ntextbuf++]=r1->data[s];
}
textbuf[ntextbuf]=0;
regs[fo]=ntextbuf;
break;
case 0x0C: revert_lump_by_number(regs[fo]&0xFFFF,"USD"); break;
case 0x0D: case 0x0E: case 0x2D: case 0x2E:
q=0x0100&~op;
v=(ASN1_Value){};
#ifndef CONFIG_DISABLE_FRONT
if(op&0x2000) {
enc=extern_out;
if(!q) asn1_construct(enc,memory[MEM_ARG_J],memory[MEM_ARG_K],fo==2?ASN1_KVSORT:fo==1?ASN1_SORT:0);
} else
#endif
enc=q?asn1_start_encoding_value(&v):asn1_start_encoding_constructed_value(&v,memory[MEM_ARG_J],memory[MEM_ARG_K],fo==2?ASN1_KVSORT:fo==1?ASN1_SORT:0);
if(!enc) errx(1,"Internal error");
for(n=1;n;) {
op=run_program(pc,0,0,0,0);
pc=(op&0x20?pc1:memory[MEM_RETURNED_PC]);
switch(op>>8) {
case 0x00: case 0x01: case 0x0A: case 0x0F: pc=do_asn1_operator(0,op,pc); break;
case 0x10: n++; asn1_construct(enc,ASN1_UNIVERSAL,ASN1_SEQUENCE,0); break;
case 0x11: n++; asn1_construct(enc,ASN1_UNIVERSAL,ASN1_SET,ASN1_SORT); break;
case 0x12: n++; asn1_construct(enc,ASN1_UNIVERSAL,ASN1_KEY_VALUE_LIST,ASN1_KVSORT); break;
case 0x13: asn1_implicit(enc,memory[MEM_ARG_J],memory[MEM_ARG_K]); break;
case 0x14: asn1_encode(enc,asn1reg+(op&3)); goto once;
case 0x15: asn1_encode_boolean(enc,condflag); goto once;
case 0x16: asn1_primitive(enc,ASN1_UNIVERSAL,ASN1_NULL,"",0); goto once;
case 0x17: asn1_primitive(enc,ASN1_UNIVERSAL,ASN1_OCTET_STRING,textbuf,ntextbuf); goto once;
case 0x18: asn1_encode_integer(enc,status_vars[op&15]); goto once;
case 0x19: if(n>1 || !q) asn1_encode(enc,asn1reg+(op&3)); asn1_encode(enc,asn1reg+((op>>4)&3)); goto once;
default: errx(1,"Unimplemented ASN.1 operator (or incorrect context): $%04X",op); break;
once: if(q && n==1) goto fin; break;
}
if(op&0x10) if(--n) {
asn1_end(enc);
if(q && n<=1) goto fin;
}
}
#ifndef CONFIG_DISABLE_FRONT
fin: if(enc==extern_out?(q?0:asn1_end(enc))||asn1_flush(enc):asn1_finish_encoder(enc)) err(1,"Allocation failed");
#else
fin: if(asn1_finish_encoder(enc)) err(1,"Allocation failed");
#endif
asn1_free(r1);
*r1=v;
break;
case 0x0F: v=*r1; *r1=*r2; *r2=v; break;
case 0x28: if(regs[fo]>=0 && regs[fo]<r1->length && !r1->constructed) memory[MEM_ARG_J]=r1->data[regs[fo]],condflag=1; else condflag=0; break;
case 0x29:
q=regs[fo]&0xFF;
if(q<1 || q>maxstat || stats[q-1].text || r1->constructed || r1->length>0xFFF8 || r1->class!=ASN1_UNIVERSAL || r1->length<0
|| (r1->type!=ASN1_IA5_STRING && r1->type!=ASN1_PC_STRING && r1->type!=ASN1_OCTET_STRING) || (r1->length && memchr(r1->data,0,r1->length))) errx(1,"Improper use of ASN1 $%04X",op);
if(!r1->length) break;
if(!(stats[q-1].text=malloc(r1->length+1))) err(1,"Allocation failed");
memcpy(stats[q-1].text,r1->data,r1->length);
stats[q-1].text[r1->length]=0;
stats[q-1].length=r1->length;
stats[q-1].frame=0;
break;
case 0x30:
u=*r1; r1->own=0; asn1_free(r2); *r2=(ASN1_Value){.type=ASN1_NULL};
condflag=0;
if(!asn1_first_of(&v,&u)) for(condflag=1;;) {
asn1_free(r2); *r2=v;
op=run_program(pc,v.class,v.type,v.length,v.constructed);
pc=(op&0x20?pc1:memory[MEM_RETURNED_PC]);
switch(op>>8) {
case 0x00: case 0x01: case 0x0A: case 0x0F: pc=do_asn1_operator(0,op,pc); break;
case 0x31: condflag=0; if(asn1_next_of(&v,&u)) goto finr; condflag=1; break;
case 0x32: condflag=1; asn1_first_of(&v,&u); break;
default: errx(1,"Unimplemented ASN.1 operator (or incorrect context): $%04X",op); break;
}
if(op&0x10) break;
if((op&0x40) && condflag) pc=pc1;
}
finr:
asn1_free(r1); *r1=u;
asn1_free(r2); *r2=(ASN1_Value){.type=ASN1_NULL};
break;
case 0x38: case 0x39:
v=(ASN1_Value){};
#ifndef CONFIG_DISABLE_FRONT
if(op&0x100) enc=extern_out; else
#endif
enc=asn1_start_encoding_value(&v);
if(!enc) errx(1,"Internal error");
fp=asn1_primitive_stream(enc,memory[MEM_ARG_J],memory[MEM_ARG_K]);
if(!fp) errx(1,"Internal error");
for(;;) {
op=run_program(pc,v.class,v.type,v.length,v.constructed);
pc=(op&0x20?pc1:memory[MEM_RETURNED_PC]);
switch(op>>8) {
case 0x3A: fputc(memory[MEM_ARG_J],fp); break;
case 0x3B: fputc(regs[op&7],fp); break;
case 0x3C: fwrite(textbuf,1,ntextbuf,fp); break;
case 0x3D: fwrite(asn1reg[op&3].data,1,asn1reg[op&3].length,fp); break;
case 0x3E:
if(regs[0]<0) regs[1]+=regs[0],regs[0]=0;
if(regs[0]>asn1reg[op&3].length) regs[0]=asn1reg[op&3].length;
if(regs[1]<0) regs[1]=0;
if(regs[1]>asn1reg[op&3].length-regs[0]) regs[1]=asn1reg[op&3].length-regs[0];
if(regs[1]>0 && regs[0]>=0 && regs[0]<asn1reg[op&3].length) fwrite(asn1reg[op&3].data+regs[0],1,regs[1],fp);
break;
default: errx(1,"Unimplemented ASN.1 operator (or incorrect context): $%04X",op); break;
}
if(op&0x10) break;
}
if(asn1_end(enc)) errx(1,"Internal error");
#ifndef CONFIG_DISABLE_FRONT
if(enc==extern_out?asn1_flush(enc):asn1_finish_encoder(enc)) err(1,"Allocation failed");
if(enc!=extern_out)
#else
if(asn1_finish_encoder(enc)) err(1,"Allocation failed");
#endif
{ asn1_free(r1); *r1=v; }
break;
default: errx(1,"Unimplemented ASN.1 operator (or incorrect context): $%04X",op);
}
if(op&0x80) regs[fo]=memory[MEM_ARG_J];
return pc;
}
static void init_new_board(void) {
Uint16 m=memory[MEM_CREATE_BOARD];
Uint16 n=memory[m]&0xFF;
Uint32 a,i;
if((m+n)&~0xFFFF) errx(1,"Create board memory has too many fields");
if(maxstat && stats->text==global_text) stats->text=0,stats->length=0;
board_info.width=(n<1?0:memory[m+1])?:board_info.width;
board_info.height=(n<2?0:memory[m+2])?:board_info.height;
if(!board_info.width || !board_info.height) errx(1,"Trying to create a board with improper dimensions");
board_info.screen=(n<4?0:memory[m+4]);
board_info.flag=(n<5?0:memory[m+5]);
for(i=0;i<4;i++) board_info.exits[i]=0;
if(!(memory[m]&0x2000)) board_info.userdata=0;
for(i=0;i<16;i++) {
free(ozone[i]); ozone[i]=0;
free(uzone[i]); uzone[i]=0;
}
free(b_under);
b_under=calloc(3*sizeof(Tile),board_info.width*(uint32_t)board_info.height);
if(!b_under) err(1,"Allocation failed");
b_main=b_under+board_info.height*board_info.width;
b_over=b_main+board_info.height*board_info.width;
if(n>=6 && (i=memory[m+6])) {
for(a=0;a<board_info.width*board_info.height;a++) b_over[a]=(Tile){OVER_VISIBLE,i>>8,i,0};
}
if(n>=7 && (i=memory[m+7])) {
for(a=0;a<board_info.width*board_info.height;a++) b_main[a]=(Tile){i,i>>8,0,0};
}
a=(n<3?0:memory[m+3]);
if(a>maxstat) a=maxstat;
for(i=a;i<maxstat;i++) {
free(stats[i].text);
free(stats[i].xy);
}
maxstat=a;
for(i=0;i<a;i++) if(!((stats[i].mode&STAT_INDEPENDENT) && (memory[m]&0x1000))){
free(stats[i].xy);
stats[i].xy=0;
stats[i].count=0;
}
if(!(memory[m]&0x4000)) {
free(board_info.varprop.item);
board_info.varprop.item=0;
board_info.varprop.count=0;
}
}
Uint8 in_zone(Uint32 x,Uint32 y,Uint8 z) {
OrdZone*o;
UnordZone*u;
Uint8 r=z>>7;
Uint32 at=y*board_info.width+x;
if(x>=board_info.width || y>=board_info.height) return 0;
switch(z&=0x7F) {
case 0x00 ... 0x0F: if((u=uzone[z&15]) && y<=u->maxy && ((1<<(at&7))&u->data[at/8])) r^=1; break;
case 0x10 ... 0x1F: if(o=ozone[z&15]) for(at=0;at<o->ncells;at++) if(o->xy[at].x==x && o->xy[at].y==y) r^=1; break;
case 0x20 ... 0x2F: if((elem_def[b_main[at].kind].attrib&15)==(z&15) || (elem_def[b_under[at].kind].attrib&15)==(z&15)) r^=1; break;
case 0x30 ... 0x37: if(b_over[at].kind&(1<<(z&7))) r^=1; break;
case 0x38 ... 0x3F: if((elem_def[b_main[at].kind].attrib|elem_def[b_under[at].kind].attrib)&(A_MISC_A<<(z&7))) r^=1; break;
case 0x40 ... 0x4F: if((elem_def[b_main[at].kind].attrib&15)==(z&15) || (elem_def[b_under[at].kind].attrib&15)==(z&15)) r^=1; break;
case 0x50 ... 0x5F: if((elem_def[(elem_def[b_main[at].kind].attrib&A_FLOOR?b_main:b_under)[at].kind].attrib&15)==(z&15)) r^=1; break;
}
return r;
}
void zone_add(Uint32 x,Uint32 y,Uint8 z,Uint8 w) {
OrdZone*o;
UnordZone*u;
Uint32 at=y*board_info.width+x;
if(z&0x80) zone_remove(x,y,z&0x7F);
if(x>=board_info.width || y>=board_info.height) return;
switch(z) {
case 0x00 ... 0x0F:
if(!(u=uzone[z&15])) u=uzone[z&15]=calloc(1,board_info.width/8+1+sizeof(UnordZone));
if(!u) err(1,"Allocation failed");
if(y>u->maxy) {
u=uzone[z&15]=realloc(u,((y+1)*(Uint32)board_info.width)/8+1+sizeof(UnordZone));
if(!u) err(1,"Allocation failed");
memset(u->data+((u->maxy+1)*(Uint32)board_info.width)/8+1,0,((y+1)*(Uint32)board_info.width)/8-((u->maxy+1)*(Uint32)board_info.width)/8);
u->maxy=y;
}
u->data[at/8]|=1<<(at&7);
break;
case 0x10 ... 0x1F:
if(!(o=ozone[z&15])) o=ozone[z&15]=calloc(1,sizeof(OrdZone)+sizeof(OrdZoneXY));
if(!o) err(1,"Allocation failed");
if(o->ncells==0xFFFF) errx(1,"Too many cells in ordered zone");
if(o->flag&ZF_REVERSE) w^=1;
o=ozone[z&15]=realloc(o,sizeof(OrdZone)+(o->ncells+1)*sizeof(OrdZoneXY));
if(!o) err(1,"Allocation failed");
if(w) {
o->xy[o->ncells].x=x; o->xy[o->ncells].y=y;
} else {
memmove(o->xy+1,o->xy,o->ncells*sizeof(OrdZoneXY));
o->xy->x=x; o->xy->y=y;
}
o->ncells++;
break;
case 0x30 ... 0x37: b_over[at].kind|=1<<(z&7);
case 0x80 ... 0xFF: zone_remove(x,y,z&0x7F); break;
}
}
void zone_remove(Uint32 x,Uint32 y,Uint8 z) {
OrdZone*o;
UnordZone*u;
Uint32 at=y*board_info.width+x;
if(x>=board_info.width || y>=board_info.height) return;
switch(z) {
case 0x00 ... 0x0F: if((u=uzone[z&15]) && y<=u->maxy) u->data[at/8]&=~(1<<(at&7)); break;
case 0x10 ... 0x1F:
if(o=ozone[z&15]) for(at=0;at<o->ncells;at++) if(o->xy[at].x==x && o->xy[at].y==y) {
if(at!=o->ncells-1) memmove(o->xy+at+1,o->xy+at,(o->ncells-at)*sizeof(OrdZoneXY));
o->ncells--;
break;
}
break;
case 0x30 ... 0x37: b_over[at].kind&=~(1<<(z&7));
case 0x80 ... 0xFF: zone_add(x,y,z&0x7F,1); break;
}
}
static Uint32 zone_info(Uint8 s,Uint32 v) {
UnordZone*u=uzone[s&15];
OrdZone*o=ozone[s&15];
Uint32 a=(s>>4)&15;
Uint32 b;
if(a==6 || a==10) {
if(!u) u=uzone[s&15]=calloc(1,board_info.width/8+1+sizeof(UnordZone));
if(!u) errx(1,"Allocation failed");
} else if(a==7 || a==11) {
if(!o) o=ozone[s&15]=calloc(1,sizeof(OrdZone)+sizeof(OrdZoneXY));
if(!o) errx(1,"Allocation failed");
}
switch((s>>4)&15) {
case 0: // unordered read count
v=0;
if(u) {
b=((u->maxy+1)*board_info.width)>>3;
for(a=0;a<b;a++) v+=__builtin_popcount(u->data[a]);
if((b=(u->maxy+1)*board_info.width)&7) v+=__builtin_popcount(u->data[b>>3]&~(0xFF<<(b&7)));
}
return v;
case 1: // ordered read count
return o?o->ncells:0;
case 2: // unordered write count
if(u) {
u->maxy=0;
memset(u->data,0,board_info.width/8+1);
}
return v;
case 3: // ordered write count
if(o) o->ncells=0;
return v;
case 4: // unordered read flag
return u?u->flag:0;
case 5: // ordered read flag
return o?o->flag:0;
case 6: // unordered write flag
return u->flag=v;
case 7: // ordered write flag
return o->flag=v;
case 8: // unordered read extra
return u?u->extra:0;
case 9: // ordered read extra
return o?o->extra:0;
case 10: // unordered write extra
return u->extra=v;
case 11: // ordered write extra
return o->extra=v;
default: errx(1,"Improper use of ZINF");
}
}
static Sint32 zone_enum(Uint8 r,Uint16 z) {
OrdZone*o=ozone[z&15];
Sint32 w=regs[r];
if(!o) {
if(!w) {
append:
zone_add(0,0,(z&15)+16,w?1:0);
condflag=1;
}
return w;
}
if(o->ncells==0xFFFF && (z&0x10)) return w;
if(w<0) return w;
if(w>=o->ncells) {
if(w==o->ncells && (z&0x10)) goto append;
return w;
}
if(z&0x10) {
if(o->ncells==0xFFFF) return w;
o=ozone[z&15]=realloc(o,sizeof(OrdZone)+(o->ncells+1)*sizeof(OrdZoneXY));
if(!o) err(1,"Allocation failed");
memmove(o->xy+w+1,o->xy+w,(o->ncells-w)*sizeof(OrdZoneXY));
o->ncells++;
}
condflag=1;
return w;
}
static void warp_to_board(Uint16 b,char m) {
FILE*fp;
const char*e;
Sint32 x,y;
if(global_text && maxstat && stats->text==global_text) {
global_frameoffset=stats->frame;
if(stats->count) {
memory[MEM_GLOBAL_INSTPTR]=stats->xy->instptr;
x=memory[MEM_GLOBAL_DELAY];
if(x&0x0100) x=(x&0xFF00)|(stats->xy->delay&0xFF);
if(x&0x0200) x=(x&0xA3FF)|((stats->xy->layer&0x5C)<<8);
if(x&0x2000) x=(x&0x7FFF)|((stats->xy->layer&0x80)<<8);
memory[MEM_GLOBAL_DELAY]=x;
global_frameptr=stats->xy->frame;
}
}
if((memory[MEM_CONTROL]&CONTROL_SAVE_BOARD) && !m) {
fp=open_lump("PREVIOUS.BRD","w");
if(!fp) err(1,"Cannot open PREVIOUS.BRD");
if(e=save_board(fp,1)) errx(1,"Error saving board #%d as PREVIOUS.BRD: %s",cur_board_id,e);
fclose(fp);
memory[MEM_CONTROL]&=~CONTROL_SAVE_BOARD;
} else if((board_info.flag&BF_PERSIST) && !m) {
for(x=0;x<maxstat;x++) {
if(stats[x].xy && !(stats[x].mode&STAT_INDEPENDENT)) for(y=0;y<stats[x].count;y++) {
if(!(stats[x].xy[y].layer&3) || stats[x].xy[y].x>=board_info.width && stats[x].xy[y].y>=board_info.height) {
memmove(stats[x].xy+y,stats[x].xy+y+1,(--stats[x].count-y)*sizeof(StatXY));
--y;
}
}
}
fp=open_lump_by_number(cur_board_id,"BRD","w");
if(!fp) err(1,"Cannot open %04X.BRD",cur_board_id);
if(e=save_board(fp,0)) errx(1,"Error saving board #%d: %s",cur_board_id,e);
fclose(fp);
}
if(cur_board_id!=b || !board_info.width || (!m && !(board_info.flag&BF_PERSIST)) || (memory[MEM_CONTROL]&CONTROL_RESTORE_BOARD)) {
x=cur_board_id;
if(e=select_board(cur_board_id=b)) {
if(memory[MEM_CREATE_BOARD]) {
init_new_board();
run_program(memory[MEM_CREATE_BOARD]+(memory[memory[MEM_CREATE_BOARD]]&0xFF),x,0,0,0);
if(memory[memory[MEM_CREATE_BOARD]]&0x8000) {
fp=open_lump_by_number(cur_board_id,"BRD","w");
if(!fp) err(1,"Cannot open %04X.BRD",cur_board_id);
if(e=save_board(fp,0)) errx(1,"Error saving board #%d: %s",cur_board_id,e);
fclose(fp);
}
goto created;
}
errx(1,"Error loading board #%d: %s",b,e);
}
}
for(x=0;x<maxstat;x++) if(stats[x].text && stats[x].text[0]=='@' && stats[x].text[1]=='!' && stats[x].text!=global_text) load_script_library(stats+x,stats[x].text+2);
created:
if(global_text && maxstat && !stats->text && !(board_info.flag&BF_NO_GLOBAL)) {
stats->text=global_text;
stats->length=global_length;
stats->frame=global_frameoffset;
if(stats->count) {
stats->xy->instptr=memory[MEM_GLOBAL_INSTPTR];
stats->xy->frame=global_frameptr;
x=memory[MEM_GLOBAL_DELAY];
if(x&0x0100) stats->xy->delay=x&0xFF;
if(x&0x0200) stats->xy->layer=(stats->xy->layer&0xA3)|((x>>8)&0x5C);
if(x&0x2000) stats->xy->layer=(stats->xy->layer&0x7F)|((x>>8)&0x80);
}
}
if(m || cur_screen_id!=board_info.screen) {
fp=open_lump_by_number(cur_screen_id=board_info.screen,"SCR","r");
if(!fp) err(1,"Cannot open %04X.SCR",cur_screen_id);
if(e=load_screen(fp)) errx(1,"Error loading screen #%d: %s",cur_screen_id,e);
fclose(fp);
work_varproperties(&cur_screen.varprop);
}
// Initial scrolling
if((cur_screen.flag&SF_NO_SCROLL) || !maxstat || !stats->count) {
scroll_x=-cur_screen.hard_edge[DIR_W];
scroll_y=-cur_screen.hard_edge[DIR_N];
} else {
x=stats->xy->x; y=stats->xy->y;
scroll_x=x-cur_screen.view_x;
scroll_y=y-cur_screen.view_y;
if(scroll_x<-(Sint32)cur_screen.hard_edge[DIR_W]) scroll_x=cur_screen.hard_edge[DIR_W];
else if(scroll_x>=board_info.width-(Sint32)cur_screen.hard_edge[DIR_E]) scroll_x=board_info.width-cur_screen.hard_edge[DIR_E]-1;
if(scroll_y<-(Sint32)cur_screen.hard_edge[DIR_N]) scroll_y=cur_screen.hard_edge[DIR_N];
else if(scroll_y>=board_info.height-(Sint32)cur_screen.hard_edge[DIR_S]) scroll_y=board_info.height-cur_screen.hard_edge[DIR_S]-1;
}
work_varproperties(&board_info.varprop);
}
static void display_message_text(void) {
Uint32 y=cur_screen.message_y;
Sint32 x,z;
Uint8 m=(cur_screen.flag&SF_FLASHY_MESSAGE?0x80:0xFF);
if(y>25) return;
if(cur_screen.flag&SF_LEFT_ALIGN_MESSAGE) {
x=cur_screen.message_l;
} else {
x=cur_screen.message_x-nvtextbuf/2;
if(x<cur_screen.message_l) x=cur_screen.message_l;
}
z=draw_text(x,y,vtextbuf,(cur_screen.flag&SF_FLASHY_MESSAGE?memory[MEM_FRAME_COUNTER]%7+9:0),nvtextbuf);
if(cur_screen.flag&SF_MESSAGE_EDGE) {
if(x>cur_screen.message_l) v_char[--x +y*80]=0,v_color[x+y*80]=0;
if(z<cur_screen.message_r) v_color[z+y*80]=0,v_char[z++ +y*80]=0;
}
x+=y*80;
z+=y*80;
while(x<z) {
v_color[x]|=m&cur_screen.color[x];
x++;
}
}
static Uint8 digit_of(Uint32 n,Uint8 f) {
static const Uint8 roman1[10]={0,1,2,3,2,1,2,3,4,1};
static const Uint8 roman2[40]={
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,1,0,0,
1,0,0,0,
1,0,0,0,
1,0,0,0,
1,0,0,0,
0,2,0,0,
};
static const Uint8 roman3[7]={'I'-'A','V'-'A','X'-'A','L'-'A','C'-'A','D'-'A','M'-'A'};
NumericFormat*nf=num_format+(f>>4);
Uint8 d=nf->div;
const Uint8*b;
int i;
Uint32 q;
f&=15;
switch(nf->code) {
case NF_DECIMAL: decimal:
n/=d;
for(i=0;i<f;i++) n/=10;
return n?(n%10+'0'):nf->lead;
case NF_HEX_UPPER:
n/=d;
n>>=4*f;
return n?((n&15)+((n&15)>9?'A'-10:'0')):nf->lead;
case NF_HEX_LOWER:
n/=d;
n>>=4*f;
return n?((n&15)+((n&15)>9?'a'-10:'0')):nf->lead;
case NF_OCTAL:
n/=d;
n>>=3*f;
return n?(n&7)+'0':nf->lead;
case NF_COMMA:
n/=d;
for(i=0;i<f;i++) n/=10;
return n?nf->mark:nf->lead;
case NF_ROMAN:
n+=n; n/=d;
if(n>=2000) {
q=n/2000;
n%=2000;
if(f<q) return 'M'-'A'+nf->mark;
f-=q;
}
if(n>=200) {
q=n/200;
n%=200;
if(f<roman1[q]) return nf->mark+roman3[roman2[4*q+f]+4];
f-=roman1[q];
}
if(n>=20) {
q=n/20;
n%=20;
if(f<roman1[q]) return nf->mark+roman3[roman2[4*q+f]+2];
f-=roman1[q];
}
if(n>=2) {
q=n/2;
n%=2;
if(f<roman1[q]) return nf->mark+roman3[roman2[4*q+f]+0];
f-=roman1[q];
}
if(n && !f) return nf->mark+'S'-'A';
return nf->lead;
case NF_LSD_MONEY:
if(f>4) {
n/=240;
goto decimal;
}
if(!f) {
if(d==2) return n&1?nf->mark:nf->lead;
if(d==4) return n&3?(n&3)+'0':nf->lead;
return nf->lead;
}
n/=d;
if(f<3) n%=12; else n=(n/12)%20;
if(!(f&1)) n/=10;
return n?(n%10+'0'):nf->lead;
case NF_METER:
return (n+d-1)/d>f?nf->mark:nf->lead;
case NF_METER_HALF:
n+=d-1; n/=d;
return n>=f+f?219:n==f+f-1?nf->mark:nf->lead;
case NF_METER_EXT:
return (n+d-1)/d>f+16?nf->mark:nf->lead;
case NF_METER_HALF_EXT:
n/=d;
return n>=f+f+32?219:n==f+f+31?nf->mark:nf->lead;
case NF_BINARY:
return ((n/d)>>f)&1?nf->mark:nf->lead;
case NF_BINARY_EXT:
return ((n/d)>>(f+16))&1?nf->mark:nf->lead;
case NF_CHARACTER:
return (n>>(8*f))?:nf->lead;
case NF_NONZERO:
return n>=d?nf->mark:nf->lead;
case NF_BOARD_NAME:
if(n>maxboard || !(b=boardnames[n])) return nf->lead;
for(i=0;i<f && b[i];i++);
return b[i]?:nf->lead;
case NF_BOARD_NAME_EXT:
if(n>maxboard || !(b=boardnames[n])) return nf->lead;
for(i=0;i<f+16 && b[i];i++);
return b[i]?:nf->lead;
}
return '?';
}
static inline Uint8 line_class_of(Sint32 bx,Sint32 by,Uint32 xy) {
if(bx<0 || bx>=board_info.width || by<0 || by>=board_info.height) return 0x80;
return (1<<(elem_def[b_main[xy].kind].app[0]>>6))|(1<<(elem_def[b_under[xy].kind].app[0]>>6));
}
static Uint8 draw_tile(Sint32 bx,Sint32 by,Uint16 at,Uint8 h) {
Uint32 xy=by*board_info.width+bx;
Sint32 q;
Uint16 f;
Uint8 o=0;
Uint8 z,m,d;
ElementDef*e;
Tile*t;
if(bx>=0 && bx<board_info.width && by>=0 && by<board_info.height) {
tile:
t=b_main+xy;
e=elem_def+t->kind;
if(!(h&4) && !(e->attrib&A_LIGHT) && (b_over[xy].kind&OVER_VISIBLE) && (board_info.flag&BF_OVERLAY)) {
if(memory[MEM_LIGHT]<65486 && maxstat && stats->count) {
q=by-stats->xy->y-scroll_y;
if(q>-25 && q<25) {
f=memory[memory[MEM_LIGHT]+q+24];
q=128+bx-stats->xy->x-scroll_x;
if(q>=(f>>8) && q<=(f&0xFF)) goto light;
}
}
o=b_over[xy].kind;
}
light:
v_font[at]=board_info.flag&((o&(OVER_VISIBLE|OVER_BG_THRU))==OVER_VISIBLE?BF_OVER_ALT_MODE:BF_MAIN_ALT_MODE)?VF_ALTERNATE:0;
if(o&OVER_VISIBLE) {
v_char[at]=b_over[xy].param;
v_color[at]=b_over[xy].color;
if(o&OVER_BG_THRU) v_color[at]|=t->color&0xF0;
} else {
if((e->app[0]&0x3F)==AP_UNDER) {
t=b_under+xy;
e=elem_def+t->kind;
}
switch(e->app[0]&0x3F) {
case AP_FIXED: v_char[at]=e->app[1]; break;
case AP_PARAM: v_char[at]=e->app[1]+t->param; break;
case AP_OVER: v_char[at]=b_over[xy].param; break;
case AP_UNDER: v_char[at]=e->app[1]; break;
case AP_SCREEN: v_char[at]=cur_screen.parameter[at]; break;
case AP_MISC1: v_char[at]=(t->stat && t->stat<=maxstat)?stats[t->stat-1].misc1:e->app[1]; break;
case AP_MISC2: v_char[at]=(t->stat && t->stat<=maxstat)?stats[t->stat-1].misc2:e->app[1]; break;
case AP_MISC3: v_char[at]=(t->stat && t->stat<=maxstat)?stats[t->stat-1].misc3:e->app[1]; break;
case AP_LINES:
m=e->app[1];
if(h&0x80) {
z=(m&0x80?15:0);
} else {
z=0;
if(m&line_class_of(bx+1,by,xy+1)) z|=1;
if(m&line_class_of(bx,by-1,xy-board_info.width)) z|=2;
if(m&line_class_of(bx-1,by,xy-1)) z|=4;
if(m&line_class_of(bx,by+1,xy+board_info.width)) z|=8;
}
v_char[at]=appearance_mapping[(m&0x70)+z];
break;
case AP_ANIMATE:
m=animation[e->app[1]&3].mode;
z=bx*(m&3)+by*((m>>2)&3);
if(e->app[1]&0x80) z+=memory[MEM_FRAME_COUNTER]>>(m&AM_SLOW?1:0);
d=animation[e->app[1]&3].step[z&3];
v_char[at]=appearance_mapping[((e->app[1]&0x7C)+d)&0x7F];
break;
case AP_CBRANDOM:
f=cbrandom((cur_board_id*123456789ULL)^(bx*54321ULL)^(by*42ULL)^(e->app[1]&0x83));
if((e->app[1]&0x80) && ((bx^by)&1)) f&=f>>8;
z=
"0000111122223333"
"0000001111122233"
"0000000111222333"
"0000000000111223"
[(f&15)+((e->app[1]&3)<<4)]&3;
v_char[at]=appearance_mapping[(e->app[1]&0x7C)+z];
break;
default:
d=(t->param>>(e->app[0]&7))&~(0xFF<<(((e->app[0]>>3)&3)+1));
if(e->app[1]&0x80) {
m=animation[e->app[1]&1].mode;
z=memory[MEM_FRAME_COUNTER];
if(m&AM_SLOW) z>>=1;
z+=bx*(m&3)+by*((m>>2)&3);
d+=animation[e->app[1]&1].step[z&3];
}
v_char[at]=appearance_mapping[((e->app[1]&0x7E)+d)&0x7F];
break;
}
switch(e->attrib&(A_OVER_COLOR|A_UNDER_COLOR)) {
case 0: v_color[at]=b_main[xy].color; break;
case A_OVER_COLOR: v_color[at]=b_over[xy].color; break;
case A_UNDER_COLOR: v_color[at]=b_under[xy].color; break;
case A_OVER_COLOR|A_UNDER_COLOR: v_color[at]=cur_screen.color[at]; break;
}
}
if(v_color[at]<16 && (e->attrib&A_UNDER_BGCOLOR)) v_color[at]|=b_under[xy].color&0xF0;
} else if(h&2) {
if(bx==-1 && cur_screen.border[DIR_W] && by>=0 && by<board_info.height) {
v_char[at]=cur_screen.border[DIR_W];
v_color[at]=cur_screen.border_color;
} else if(bx==board_info.width && cur_screen.border[DIR_E] && by>=0 && by<board_info.height) {
v_char[at]=cur_screen.border[DIR_E];
v_color[at]=cur_screen.border_color;
} else if(by==board_info.height && cur_screen.border[DIR_S] && bx>=0 && bx<board_info.width) {
v_char[at]=cur_screen.border[DIR_S];
v_color[at]=cur_screen.border_color;
} else if(by==-1 && cur_screen.border[DIR_N] && bx>=0 && bx<board_info.width) {
v_char[at]=cur_screen.border[DIR_N];
v_color[at]=cur_screen.border_color;
} else {
goto noborder;
}
} else {
noborder:
if(h&1) {
v_char[at]=cur_screen.parameter[at];
v_color[at]=cur_screen.color[at];
} else {
h|=0x80;
xy=(by<0?0:by>=board_info.height?board_info.height-1:by)*board_info.width+(bx<0?0:bx>=board_info.width?board_info.width-1:bx);
goto tile;
}
}
}
static void display_item_element_cell(Uint16 i,Uint8 col,Uint8 inv,Uint16 sl) {
ItemSlot s;
ItemDef d;
if(inventory[inv].count<=sl) s=(ItemSlot){}; else s=inventory[inv].item[sl];
if(s.item && s.item<=nitemdefs) d=itemdefs[s.item-1]; else d=(ItemDef){.element=244,.color=col};
switch(elem_def[d.element].app[0]&0x3F) {
case AP_FIXED: v_char[i]=elem_def[d.element].app[1]; break;
case AP_PARAM: v_char[i]=d.parameter; break;
case AP_UNDER: v_char[i]=elem_def[d.element].app[1]; break;
case AP_MISC1: v_char[i]=s.ext1?:elem_def[d.element].app[1]; break;
case AP_MISC2: v_char[i]=s.ext2?:elem_def[d.element].app[1]; break;
case AP_MISC3: v_char[i]=d.ext3?:elem_def[d.element].app[1]; break;
case 0x20 ... 0x3F: v_char[i]=appearance_mapping[((elem_def[d.element].app[1]&0x7E)+((d.parameter>>(elem_def[d.element].app[0]&7))&((2<<((elem_def[d.element].app[0]>>3)&3))-1)))&0x7F]; break;
}
v_color[i]=elem_def[d.element].attrib&(A_OVER_COLOR|A_UNDER_COLOR)?col:d.color;
if(elem_def[d.element].attrib&A_UNDER_BGCOLOR) v_color[i]=(v_color[i]&0x0F)|(col&0xF0);
}
void update_screen(void) {
int i;
Uint32 v,x,y;
Uint8 cmd,col,chr;
memset(v_font,cur_screen.flag&SF_ALT_MODE?VF_ALTERNATE:0,80*25);
for(i=0;i<80*25;i++) {
cmd=cur_screen.command[i];
col=cur_screen.color[i];
chr=cur_screen.parameter[i];
switch(cmd&0xF0) {
case SC_BACKGROUND:
if(cmd&1) v_char[i]=chr;
if(cmd&2) v_color[i]=col;
break;
case SC_BOARD:
draw_tile((i%80)+scroll_x,(i/80)+scroll_y,i,cmd&0x0F);