-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.c
4229 lines (4084 loc) · 168 KB
/
exec.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 ${CFLAGS:--s -O2} -c -fwrapv exec.c `sdl-config --cflags`
exit
#endif
#include "SDL.h"
#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sqlite3.h"
#include "smallxrm.h"
#include "quarks.h"
#include "heromesh.h"
#include "instruc.h"
#include "names.h"
#ifndef VSTACKSIZE
#define VSTACKSIZE 0x400
#endif
Uint32 max_objects;
Uint32 generation_number;
Object**objects;
Uint32 nobjects;
Value globals[0x800];
Uint32 firstobj=VOIDLINK;
Uint32 lastobj=VOIDLINK;
Uint32 playfield[64*64]; // bottom-most object per cell
Uint32 bizplayfield[64*64]; // bizarro world
Uint8 pfwidth,pfheight;
Sint8 gameover,key_ignored;
Uint8 generation_number_inc;
Uint32 move_number;
unsigned char*quiz_text;
Inventory*inventory;
Uint32 ninventory;
unsigned char**levelstrings;
Uint16 nlevelstrings;
Value*array_data;
Uint16 ndeadanim;
DeadAnimation*deadanim;
Uint8 no_dead_anim;
Uint32 max_trigger;
Uint8 conn_option;
Sint32 gameover_score;
Uint32 trigmode;
typedef struct {
Uint16 msg;
Uint32 from;
Value arg1,arg2,arg3;
} MessageVars;
typedef struct {
Uint32 obj;
Uint16 id;
Uint8 visual;
} Connection;
static jmp_buf my_env;
static const char*my_error;
static MessageVars msgvars;
static Uint8 lastimage_processing,changed,all_flushed;
static Value vstack[VSTACKSIZE];
static int vstackptr;
static const char*traceprefix;
static Uint8 current_key;
static Value quiz_obj;
static Value traced_obj;
static Uint32 control_obj=VOIDLINK;
static Connection conn[VSTACKSIZE];
static int nconn,pconn;
static Uint8 conn_dir;
static Uint16 subst_class;
static Uint32 subst_obj;
#define Throw(x) (my_error=(x),longjmp(my_env,1))
#define StackReq(x,y) do{ if(vstackptr<(x)) Throw("Stack underflow"); if(vstackptr-(x)+(y)>=VSTACKSIZE) Throw("Stack overflow"); }while(0)
#define Push(x) (vstack[vstackptr++]=(x))
#define Pop() (vstack[--vstackptr])
// For arrival/departure masks
#define Xbit(a) ((a)%5-2)
#define Ybit(a) (2-(a)/5)
static void execute_program(Uint16*code,int ptr,Uint32 obj);
static Value send_message(Uint32 from,Uint32 to,Uint16 msg,Value arg1,Value arg2,Value arg3);
static Uint32 broadcast(Uint32 from,int c,Uint16 msg,Value arg1,Value arg2,Value arg3,Uint8 s);
static Value destroy(Uint32 from,Uint32 to,Uint32 why);
static const Sint8 x_delta[8]={1,1,0,-1,-1,-1,0,1};
static const Sint8 y_delta[8]={0,-1,-1,-1,0,1,1,1};
const unsigned char*value_string_ptr(Value v) {
switch(v.t) {
case TY_STRING: return stringpool[v.u];
case TY_LEVELSTRING: return v.u<nlevelstrings?levelstrings[v.u]:(unsigned char*)"<Invalid level string>";
default: fatal("Internal confusion: Trying to get string pointer for a non-string\n");
}
}
void pfunlink(Uint32 n) {
Object*o=objects[n];
Uint32*pf=(o->oflags&OF_BIZARRO?bizplayfield:playfield);
if(o->down==VOIDLINK) pf[o->x+o->y*64-65]=o->up;
else objects[o->down]->up=o->up;
if(o->up!=VOIDLINK) objects[o->up]->down=o->down;
o->down=o->up=VOIDLINK;
}
void pflink(Uint32 n) {
Object*o=objects[n];
Uint32*pf=(o->oflags&OF_BIZARRO?bizplayfield:playfield);
int p=o->x+o->y*64-65;
if(p<0) return;
if(pf[p]==VOIDLINK) {
pf[p]=n;
} else {
Sint32 d=o->density;
Uint32 m=pf[p];
Uint32 t=VOIDLINK;
while(m!=VOIDLINK && objects[m]->density>=d) t=m,m=objects[m]->up;
o->down=t;
o->up=m;
if(t!=VOIDLINK) objects[t]->up=n; else pf[p]=n;
if(m!=VOIDLINK) objects[m]->down=n;
}
}
#define OBJECT_ARRAY_BLOCK 256
Uint32 objalloc(Uint16 c) {
// Allocates a new object of the given class; links into the event list but not into the playfield.
// All fields are initialized by the class or to zero.
// Does not send any messages or otherwise notify anyone that it has been created.
// Returns VOIDLINK if object cannot be created.
Uint32 n;
Class*cl=classes[c];
Object*o=0;
if(!c || !cl || cl->cflags&(CF_GROUP|CF_NOCLASS2)) goto bad;
o=calloc(1,sizeof(Object)+cl->uservars*sizeof(Value));
if(!o) fatal("Allocation failed\n");
o->class=c;
if(generation_number_inc) {
generation_number_inc=0;
++generation_number;
if(generation_number<=TY_MAXTYPE) goto bad;
}
o->generation=generation_number;
#define C(x) o->x=cl->x;
C(height) C(weight) C(climb) C(density) C(volume) C(strength) C(arrivals) C(departures) C(temperature)
C(shape) C(shovable) C(oflags)
C(sharp[0]) C(sharp[1]) C(sharp[2]) C(sharp[3])
C(hard[0]) C(hard[1]) C(hard[2]) C(hard[3])
#undef C
o->misc4=NVALUE(cl->misc4);
o->misc5=NVALUE(cl->misc5);
o->misc6=NVALUE(cl->misc6);
o->misc7=NVALUE(cl->misc7);
if(nobjects) for(n=nobjects-1;;n--) {
if(!objects[n]) goto found;
if(!n) break;
}
if(nobjects>=max_objects) goto bad;
objects=realloc(objects,(nobjects+OBJECT_ARRAY_BLOCK)*sizeof(Object*));
if(!objects) fatal("Allocation failed\n");
for(n=nobjects;n<nobjects+OBJECT_ARRAY_BLOCK;n++) objects[n]=0;
n=nobjects;
nobjects+=OBJECT_ARRAY_BLOCK;
found:
o->up=o->down=o->prev=o->next=VOIDLINK;
if(cl->nmsg || classes[0]->nmsg) {
o->prev=lastobj;
if(lastobj!=VOIDLINK) objects[lastobj]->next=n;
lastobj=n;
if(firstobj==VOIDLINK) firstobj=n;
}
objects[n]=o;
return n;
bad:
free(o);
return VOIDLINK;
}
#define animfree free
static void set_dead_animation(const Object*o) {
DeadAnimation*d;
if(no_dead_anim) return;
deadanim=realloc(deadanim,(ndeadanim+1)*sizeof(DeadAnimation));
if(!deadanim) fatal("Allocation failed\n");
d=deadanim+ndeadanim++;
d->class=o->class;
d->x=o->x;
d->y=o->y;
d->s=o->anim->step[o->anim->vstep];
d->vtime=o->anim->vtime;
d->vimage=o->anim->vimage;
d->delay=0;
}
static void v_animate_dead(Value x,Value y,Value c,Value s,Value e,Value z) {
DeadAnimation*d;
if(no_dead_anim || ndeadanim>=0x1000) return;
if(x.t || y.t || s.t || e.t || z.t || c.t!=TY_CLASS) return;
if(!z.u || (z.u&~255) || x.u<1 || x.u>pfwidth || y.u<1 || y.u>pfheight) return;
if(!classes[c.u] || !classes[c.u]->nimages || (classes[c.u]->cflags&(CF_GROUP|CF_NOCLASS2))) return;
deadanim=realloc(deadanim,(ndeadanim+1)*sizeof(DeadAnimation));
if(!deadanim) fatal("Allocation failed\n");
d=deadanim+ndeadanim++;
d->class=c.u;
d->x=x.u;
d->y=y.u;
d->s.flag=ANI_ONCE;
d->s.start=s.u;
d->s.end=e.u;
d->s.speed=z.u;
d->vtime=0;
d->vimage=s.u;
d->delay=0;
}
void objtrash(Uint32 n) {
Object*o=objects[n];
if(!o) return;
if(!main_options['e']) {
if(n==traced_obj.u && o->generation==traced_obj.t && main_options['t']) {
printf("# Object traced at (%d,%d)\n",o->x,o->y);
Throw("Object traced");
} else if(ndeadanim<0x1000 && o->anim && o->anim->status==ANISTAT_VISUAL && !(o->oflags&OF_INVISIBLE)) {
if(o->up==VOIDLINK || (objects[o->up]->oflags&OF_DESTROYED)) set_dead_animation(o);
}
}
animfree(o->anim);
if(o->down==VOIDLINK) (o->oflags&OF_BIZARRO?bizplayfield:playfield)[o->x+o->y*64-65]=o->up;
else objects[o->down]->up=o->up;
if(o->up!=VOIDLINK) objects[o->up]->down=o->down;
if(!(o->oflags&OF_DESTROYED)) {
if(firstobj==n) firstobj=o->next;
if(lastobj==n) lastobj=o->prev;
if(o->prev!=VOIDLINK) objects[o->prev]->next=o->next;
if(o->next!=VOIDLINK) objects[o->next]->prev=o->prev;
}
free(o);
objects[n]=0;
generation_number_inc=1;
}
static inline void clear_inventory(void) {
free(inventory);
inventory=0;
ninventory=0;
}
static inline Uint8 resolve_dir(Uint32 n,Uint16 d) {
return d<8?d:(objects[n]->dir+d)&7;
}
/*
Working of animation: There are two separate animations, being logical
and visual animations. New animations replace the current logical
animation; if there isn't one, but there is a visual animation, then a
new logical animation is added at the end. The logical animation is
always at the same step or ahead of the visual animation.
- lstep - Step number of the logical animation; this cannot equal or
exceed max_animation. Only a single logical step is present at once.
- vstep - Step number of the visual animation; this cannot equal or
exceed max_animation. The tail of the visual animation is the same as
the head of the logical animation, and the buffer is circular.
- status - Contains bit flags. If the ANISTAT_LOGICAL flag is set, then
it means a logical animation is active. If the ANISTAT_VISUAL flag is
set, then it means a visual animation is active. ANISTAT_SYNCHRONIZED
is set only for synchronized animations, which are visual only.
- ltime - Amount of logical time passed
- vtime - Number of centiseconds elapsed.
- vimage - If the status field has the ANISTAT_VISUAL bit set, then this
will decide the picture to display rather than using the object's image.
- count - Number of logical animation steps so far this turn. If it is
equal to max_animation then no more animations can be added this turn.
Synchronized animations only use vstep, status, and vimage.
*/
static Animation*animalloc(void) {
Animation*a=malloc(sizeof(Animation)+max_animation*sizeof(AnimationStep));
if(!a) fatal("Allocation failed\n");
a->lstep=a->vstep=a->ltime=a->vtime=a->status=a->count=0;
return a;
}
static void animate(Uint32 n,Uint32 f,Uint32 a0,Uint32 a1,Uint32 t) {
Animation*an=objects[n]->anim;
objects[n]->image=a0;
f&=0x0B;
if(!an) an=objects[n]->anim=animalloc();
if(an->status&ANISTAT_SYNCHRONIZED) an->status=0;
if(an->count==max_animation) f=ANI_STOP;
if(!an->count && (an->status&ANISTAT_VISUAL)) an->status=0;
if(f&(ANI_ONCE|ANI_LOOP)) {
switch(an->status) {
case 0:
an->vtime=an->lstep=an->vstep=0;
an->vimage=a0;
break;
case ANISTAT_LOGICAL:
an->vstep=an->lstep;
an->vtime=0;
an->vimage=a0;
break;
case ANISTAT_VISUAL:
an->lstep++;
if(an->lstep>=max_animation) an->lstep=0;
if(an->lstep==an->vstep && max_animation>1) {
an->vstep++;
if(an->vstep==max_animation) an->vstep=0;
an->vimage=an->step[an->vstep].start;
an->vtime=0;
}
break;
case ANISTAT_VISUAL|ANISTAT_LOGICAL:
if(an->lstep==an->vstep) {
an->vimage=a0;
an->vtime=0;
}
break;
}
an->step[an->lstep].flag=f;
an->step[an->lstep].start=a0;
an->step[an->lstep].end=a1;
an->step[an->lstep].speed=t;
an->ltime=0;
an->status=ANISTAT_VISUAL|ANISTAT_LOGICAL;
an->count++;
} else if(an->lstep==an->vstep) {
if(an->status&ANISTAT_LOGICAL) an->status=0;
} else if(an->status&ANISTAT_LOGICAL) {
an->lstep=(an->lstep?:max_animation)-1;
an->status&=~ANISTAT_LOGICAL;
}
}
static void animate_ext(Uint32 n,Uint32 f,Uint32 a0,Uint32 a1,Uint32 t) {
Animation*an=objects[n]->anim;
if(!an) an=objects[n]->anim=animalloc();
an->lstep=an->vstep=an->count=an->ltime=an->vtime=an->status=0;
an->step->start=a0;
an->step->end=a1;
an->step->speed=t;
an->vimage=(f&0x08?objects[n]->image:a0);
if(f&0x10) an->ltime=an->vtime=t/2;
switch(f&0x07) {
case 0: an->status=0; an->step->flag=0; break;
case 1: an->status=ANISTAT_LOGICAL; an->step->flag=ANI_ONCE; break;
case 2: an->status=ANISTAT_VISUAL|ANISTAT_LOGICAL; an->step->flag=ANI_ONCE; break;
case 3: an->status=ANISTAT_VISUAL; an->step->flag=ANI_ONCE; break;
case 4: an->status=ANISTAT_VISUAL|ANISTAT_LOGICAL; an->step->flag=ANI_LOOP; break;
case 5: an->status=ANISTAT_VISUAL|ANISTAT_LOGICAL; an->step->flag=ANI_LOOP|ANI_OSC; break;
case 6: an->status=ANISTAT_VISUAL|ANISTAT_SYNCHRONIZED; an->step->flag=ANI_LOOP|ANI_SYNC; an->step->slot=t&7; break;
case 7: an->status=ANISTAT_LOGICAL; an->step->flag=ANI_ONCE; objects[n]->image=a0; break;
}
}
static void animate_sync(Uint32 n,Uint32 sl,Uint32 a0) {
Animation*an=objects[n]->anim;
objects[n]->image=a0;
sl&=7;
if(!an) an=objects[n]->anim=animalloc();
an->status=ANISTAT_VISUAL|ANISTAT_SYNCHRONIZED;
an->vimage=a0+anim_slot[sl].frame;
an->lstep=an->vstep=0;
an->step->flag=ANI_LOOP|ANI_SYNC;
an->step->start=a0;
an->step->slot=sl;
}
static Sint32 height_at(Uint32 x,Uint32 y) {
Sint32 r=0;
Object*o;
Uint32 i;
if(x<1 || x>pfwidth || y<1 || y>pfheight) return 0;
i=playfield[x+y*64-65];
while(i!=VOIDLINK) {
o=objects[i];
if(r<o->height && !(o->oflags&(OF_DESTROYED|OF_VISUALONLY))) r=o->height;
i=o->up;
}
return r;
}
static Sint32 volume_at(Uint32 x,Uint32 y) {
Sint32 r=0;
Object*o;
Uint32 i;
if(x<1 || x>pfwidth || y<1 || y>pfheight) return 0;
i=playfield[x+y*64-65];
while(i!=VOIDLINK) {
o=objects[i];
if(r<o->volume && !(o->oflags&(OF_DESTROYED|OF_VISUALONLY))) r=o->volume;
i=o->up;
}
return r;
}
static Uint32 obj_seek(Uint32 aa,Uint32 bb) {
Object*a;
Object*b;
if(aa==VOIDLINK || bb==VOIDLINK) Throw("Cannot seek to zero object");
a=objects[aa]; b=objects[bb];
if(a->x==b->x) {
if(a->y==b->y) return 8;
return a->y<b->y?6:2;
} else if(a->x<b->x) {
if(a->y==b->y) return 0;
return a->y<b->y?7:1;
} else {
if(a->y==b->y) return 4;
return a->y<b->y?5:3;
}
}
static Uint32 obj_above(Uint32 i) {
Object*o;
if(i==VOIDLINK) return VOIDLINK;
o=objects[i];
i=o->up;
while(i!=VOIDLINK) {
o=objects[i];
if(!(o->oflags&(OF_DESTROYED|OF_VISUALONLY))) return i;
i=o->up;
}
return VOIDLINK;
}
static Uint32 obj_below(Uint32 i) {
Object*o;
if(i==VOIDLINK) return VOIDLINK;
o=objects[i];
i=o->down;
while(i!=VOIDLINK) {
o=objects[i];
if(!(o->oflags&(OF_DESTROYED|OF_VISUALONLY))) return i;
i=o->down;
}
return VOIDLINK;
}
static Uint32 obj_bottom_at(Uint32 x,Uint32 y) {
Uint32 i;
if(x<1 || x>pfwidth || y<1 || y>pfheight) return VOIDLINK;
i=playfield[x+y*64-65];
while(i!=VOIDLINK) {
if(!(objects[i]->oflags&(OF_DESTROYED|OF_VISUALONLY))) return i;
i=objects[i]->up;
}
return VOIDLINK;
}
static Uint32 obj_class_at(Uint32 c,Uint16 x,Uint16 y) {
Uint32 i;
if(x<1 || x>pfwidth || y<1 || y>pfheight) return VOIDLINK;
i=playfield[x+y*64-65];
if(c && (classes[c]->cflags&CF_GROUP)) {
Uint16 k;
while(i!=VOIDLINK) {
if(!(objects[i]->oflags&OF_DESTROYED)) {
k=objects[i]->class;
while(k!=c && classes[k]->codes && classes[k]->codes[0]==OP_SUPER) k=classes[k]->codes[1];
}
i=objects[i]->up;
}
return VOIDLINK;
}
while(i!=VOIDLINK) {
if(objects[i]->class==c && !(objects[i]->oflags&OF_DESTROYED)) return i;
i=objects[i]->up;
}
return VOIDLINK;
}
static Uint32 obj_layer_at(Uint8 b,Uint32 x,Uint32 y) {
Uint32 i;
if(x<1 || x>pfwidth || y<1 || y>pfheight) return VOIDLINK;
i=playfield[x+y*64-65];
while(i!=VOIDLINK) {
if(!(objects[i]->oflags&OF_DESTROYED) && (classes[objects[i]->class]->collisionLayers&b)) return i;
i=objects[i]->up;
}
return VOIDLINK;
}
static int connection_collision(Uint8 b,Uint32 x,Uint32 y) {
Uint32 i;
if(x<1 || x>pfwidth || y<1 || y>pfheight) return 1;
i=playfield[x+y*64-65];
while(i!=VOIDLINK) {
if(!(objects[i]->oflags&OF_DESTROYED) && (classes[objects[i]->class]->collisionLayers&b)) return !!((OF_CONNECTION|OF_MOVING)&~objects[i]->oflags);
i=objects[i]->up;
}
return 0;
}
static Uint32 obj_top_at(Uint32 x,Uint32 y) {
Uint32 i,r;
if(x<1 || x>pfwidth || y<1 || y>pfheight) return VOIDLINK;
i=playfield[x+y*64-65];
r=VOIDLINK;
while(i!=VOIDLINK) {
if(!(objects[i]->oflags&(OF_DESTROYED|OF_VISUALONLY))) r=i;
i=objects[i]->up;
}
return r;
}
static Uint32 obj_dir(Uint32 n,Uint32 d) {
if(n==VOIDLINK) return VOIDLINK;
d=resolve_dir(n,d);
return obj_top_at(objects[n]->x+x_delta[d],objects[n]->y+y_delta[d]);
}
static Sint16 new_x(Sint16 n,Uint8 d) {
n+=x_delta[d&7];
return n<0?0:n>pfwidth?pfwidth+1:n;
}
static Sint16 new_y(Sint16 n,Uint8 d) {
n+=y_delta[d&7];
return n<0?0:n>pfheight?pfheight+1:n;
}
static void change_shape(Uint32 n,int d,int v) {
v&=3;
v<<=d+d;
v|=objects[n]->shape&~(3<<(d+d));
objects[n]->shape=v;
}
static void sink(Uint32 x,Uint32 y) {
// Before, x(o) was above y(p); now make y(p) above x(o)
Object*o=objects[x];
Object*p=objects[y];
int i=o->x+o->y*64-65;
Value v;
p->up=o->up;
o->down=p->down;
o->up=y;
p->down=x;
if(o->down==VOIDLINK) playfield[i]=x; else objects[o->down]->up=x;
if(p->up!=VOIDLINK) objects[p->up]->down=y;
v=send_message(x,y,MSG_FLOATED,NVALUE(0),NVALUE(0),NVALUE(0));
if(!((o->oflags|p->oflags)&(OF_VISUALONLY|OF_DESTROYED))) send_message(y,x,MSG_SUNK,NVALUE(0),NVALUE(0),v);
}
static void change_density(Uint32 n,Sint32 v) {
Object*o=objects[n];
Uint32 i;
if(o->oflags&OF_BIZARRO) return;
if(n==control_obj) {
o->density=v;
return;
}
if(v<o->density) {
o->density=v;
for(;;) {
i=o->up;
if(i==VOIDLINK || objects[i]->density<=v) return;
sink(i,n);
}
} else if(v>o->density) {
o->density=v;
for(;;) {
i=o->down;
if(i==VOIDLINK || objects[i]->density>=v) return;
sink(n,i);
}
}
}
static inline int v_bool(Value v) {
switch(v.t) {
case TY_NUMBER: return v.u!=0;
case TY_SOUND: case TY_USOUND: Throw("Cannot convert sound to boolean");
default: return 1;
}
}
static inline int v_equal(Value x,Value y) {
if(x.t==TY_SOUND || y.t==TY_SOUND || x.t==TY_USOUND || y.t==TY_USOUND) Throw("Cannot compare sounds");
if(x.t==y.t && x.u==y.u) return 1;
if((x.t==TY_STRING || x.t==TY_LEVELSTRING) && (y.t==TY_STRING || y.t==TY_LEVELSTRING)) {
if(!strcmp(value_string_ptr(x),value_string_ptr(y))) return 1;
}
return 0;
}
static Uint32 v_object(Value v) {
if(v.t==TY_NUMBER) {
if(v.u) Throw("Cannot convert non-zero number to object");
return VOIDLINK;
} else if(v.t>TY_MAXTYPE) {
if(v.u>=nobjects || !objects[v.u] || objects[v.u]->generation!=v.t) {
if(main_options['t']) printf("[Object %lu in generation %lu does not exist]\n",(long)v.u,(long)v.t);
Throw("Attempt to use a nonexistent object");
}
return v.u;
} else {
Throw("Cannot convert non-object to object");
}
}
static inline int v_sh_dir(Value x) {
if(x.t!=TY_NUMBER) Throw("Type mismatch");
if(x.u&~6) Throw("Direction must be E, N, W, or S");
return x.u>>1;
}
static inline int v_signed_greater(Value x,Value y) {
if(x.t!=TY_NUMBER || y.t!=TY_NUMBER) Throw("Type mismatch");
return x.s>y.s;
}
static inline int v_unsigned_greater(Value x,Value y) {
if(x.t!=TY_NUMBER || y.t!=TY_NUMBER) Throw("Type mismatch");
return x.u>y.u;
}
static Uint32 v_is(Value x,Value y) {
Uint32 n;
if(x.t==TY_NUMBER && !x.u) {
return 0;
} else if(x.t>TY_MAXTYPE && y.t==TY_CLASS) {
n=v_object(x);
if(classes[y.u]->cflags&CF_GROUP) {
n=objects[n]->class;
while(n!=y.u && classes[n]->codes && classes[n]->codes[0]==OP_SUPER) n=classes[n]->codes[1];
return (n==y.u)?1:0;
}
return (objects[n]->class==y.u)?1:0;
} else if((x.t>TY_MAXTYPE || x.t==TY_CLASS) && y.t==TY_NUMBER && !y.u) {
return 1;
} else if(x.t==TY_CLASS && y.t==TY_CLASS) {
if(classes[y.u]->cflags&CF_GROUP) {
n=x.u;
while(n!=y.u && classes[n]->codes && classes[n]->codes[0]==OP_SUPER) n=classes[n]->codes[1];
return (n==y.u)?1:0;
}
return (x.u==y.u)?1:0;
//TODO: CF_GROUP
} else {
Throw("Type mismatch");
}
}
static Uint32 v_destroyed(Value v) {
if(v.t==TY_NUMBER) {
if(v.u) Throw("Cannot convert non-zero number to object");
return 0;
} else if(v.t>TY_MAXTYPE) {
if(v.u>=nobjects || !objects[v.u] || objects[v.u]->generation!=v.t) return 1;
return objects[v.u]->oflags&OF_DESTROYED?1:0;
} else {
Throw("Cannot convert non-object to object");
}
}
static Uint8 collisions_at(Uint32 x,Uint32 y) {
Uint8 c=0;
Uint32 n;
if(x<1 || y<1 || x>pfwidth || y>pfheight) return 0;
n=playfield[x+y*64-65];
while(n!=VOIDLINK) {
if(!(objects[n]->oflags&OF_DESTROYED)) c|=classes[objects[n]->class]->collisionLayers;
n=objects[n]->up;
}
return c;
}
static Uint16 collide_with(Uint8 b,Uint32 n,Uint8 x,Uint8 y,Uint16 c) {
int i,j;
Uint8 r=0;
Uint32 e[8]={VOIDLINK,VOIDLINK,VOIDLINK,VOIDLINK,VOIDLINK,VOIDLINK,VOIDLINK,VOIDLINK};
Uint8 re[8]={0,0,0,0,0,0,0,0};
Value v;
if(StackProtection()) Throw("Call stack overflow");
for(i=0;i<8;i++) if(b&(1<<i)) e[i]=obj_layer_at(1<<i,x,y);
for(i=0;i<7;i++) for(j=i+1;j<8;j++) if(e[i]==e[j]) e[j]=VOIDLINK;
if(n!=VOIDLINK) {
v=send_message(VOIDLINK,n,MSG_COLLIDE,NVALUE(x),NVALUE(y),NVALUE(b));
if(v.t) Throw("Type mismatch in COLLIDE");
r=v.u;
}
for(i=0;i<8;i++) if(e[i]!=VOIDLINK && !(r&0x02)) {
v=send_message(n,e[i],MSG_COLLIDEBY,NVALUE(n==VOIDLINK?0:objects[n]->x),NVALUE(n==VOIDLINK?0:objects[n]->y),CVALUE(c));
if(v.t==TY_CLASS) {
subst_class=v.u;
subst_obj=e[i];
return 0x8001;
} else if(v.t>TY_MAXTYPE) {
subst_class=0;
subst_obj=v_object(v);
return 0x8001;
}
if(v.t) Throw("Type mismatch in COLLIDEBY");
r|=re[i]=v.u;
}
if(!(r&0x01)) {
// See if we can destroy some objects to make room
b=classes[c]->collisionLayers;
if(obj_layer_at(b,x,y)==VOIDLINK) return r;
n=playfield[x+y*64-65];
while(n!=VOIDLINK) {
if(!(objects[n]->oflags&OF_DESTROYED)) {
if(i=classes[objects[n]->class]->collisionLayers) {
i=__builtin_ctz(i);
if(e[i]!=n || !(re[i]&0x10)) return r|0x01;
}
}
n=objects[n]->up;
}
for(i=0;i<8;i++) {
if(e[i]!=VOIDLINK && (re[i]&0x10) && objects[e[i]]->x==x && objects[e[i]]->y==y && !(objects[e[i]]->oflags&OF_DESTROYED)) {
destroy(n,e[i],3);
}
}
if(obj_layer_at(b,x,y)!=VOIDLINK) r|=0x01;
}
return r;
}
static void set_order(Uint32 obj) {
// To avoid confusing order of execution at the wrong time,
// calling this function is limited to only certain times.
Object*o=objects[obj];
Uint8 ord=classes[o->class]->order;
Uint8 u;
Sint32 v0,v1;
Uint16 p;
Uint32 n=firstobj;
Value v;
for(;;) {
if(n==obj || n==VOIDLINK) goto notfound;
u=classes[objects[n]->class]->order;
if(u<ord || !(objects[n]->oflags&OF_ORDERED)) goto found;
if(u==ord) {
p=orders[ord]+1;
criteria: switch(orders[p]) {
case OP_RET: goto found;
case OP_DENSITY: v0=o->density; v1=objects[n]->density; goto compare;
case OP_DENSITY_C: v1=o->density; v0=objects[n]->density; goto compare;
case OP_IMAGE: v0=o->image; v1=objects[n]->image; goto compare;
case OP_IMAGE_C: v1=o->image; v0=objects[n]->image; goto compare;
case OP_MISC1:
if(o->misc1.t || objects[n]->misc1.t) Throw("Type mismatch in order criteria");
v0=o->misc1.s; v1=objects[n]->misc1.s; goto compare;
case OP_MISC1_C:
if(o->misc1.t || objects[n]->misc1.t) Throw("Type mismatch in order criteria");
v1=o->misc1.s; v0=objects[n]->misc1.s; goto compare;
case OP_MISC2:
if(o->misc2.t || objects[n]->misc2.t) Throw("Type mismatch in order criteria");
v0=o->misc2.s; v1=objects[n]->misc2.s; goto compare;
case OP_MISC2_C:
if(o->misc2.t || objects[n]->misc2.t) Throw("Type mismatch in order criteria");
v1=o->misc2.s; v0=objects[n]->misc2.s; goto compare;
case OP_MISC3:
if(o->misc3.t || objects[n]->misc3.t) Throw("Type mismatch in order criteria");
v0=o->misc3.s; v1=objects[n]->misc3.s; goto compare;
case OP_MISC3_C:
if(o->misc3.t || objects[n]->misc3.t) Throw("Type mismatch in order criteria");
v1=o->misc3.s; v0=objects[n]->misc3.s; goto compare;
case OP_MISC4:
if(o->misc4.t || objects[n]->misc4.t) Throw("Type mismatch in order criteria");
v0=o->misc4.s; v1=objects[n]->misc4.s; goto compare;
case OP_MISC4_C:
if(o->misc4.t || objects[n]->misc4.t) Throw("Type mismatch in order criteria");
v1=o->misc4.s; v0=objects[n]->misc4.s; goto compare;
case OP_MISC5:
if(o->misc5.t || objects[n]->misc5.t) Throw("Type mismatch in order criteria");
v0=o->misc5.s; v1=objects[n]->misc5.s; goto compare;
case OP_MISC5_C:
if(o->misc5.t || objects[n]->misc5.t) Throw("Type mismatch in order criteria");
v1=o->misc5.s; v0=objects[n]->misc5.s; goto compare;
case OP_MISC6:
if(o->misc6.t || objects[n]->misc6.t) Throw("Type mismatch in order criteria");
v0=o->misc6.s; v1=objects[n]->misc6.s; goto compare;
case OP_MISC6_C:
if(o->misc6.t || objects[n]->misc6.t) Throw("Type mismatch in order criteria");
v1=o->misc6.s; v0=objects[n]->misc6.s; goto compare;
case OP_MISC7:
if(o->misc7.t || objects[n]->misc7.t) Throw("Type mismatch in order criteria");
v0=o->misc7.s; v1=objects[n]->misc7.s; goto compare;
case OP_MISC7_C:
if(o->misc7.t || objects[n]->misc7.t) Throw("Type mismatch in order criteria");
v1=o->misc7.s; v0=objects[n]->misc7.s; goto compare;
case OP_TEMPERATURE: v0=o->temperature; v1=objects[n]->temperature; goto compare;
case OP_TEMPERATURE_C: v1=o->temperature; v0=objects[n]->temperature; goto compare;
case OP_XLOC: v0=o->x; v1=objects[n]->x; goto compare;
case OP_XLOC_C: v1=o->x; v0=objects[n]->x; goto compare;
case OP_YLOC: v0=o->y; v1=objects[n]->y; goto compare;
case OP_YLOC_C: v1=o->y; v0=objects[n]->y; goto compare;
case 0xC000 ... 0xFFFF: // message
changed=0;
v=send_message(obj,n,(orders[p]&0x3FFF)+256,NVALUE(o->x),NVALUE(o->y),NVALUE(0));
if(changed) Throw("State-changing is not allowed during ordering");
changed=1;
if((o->oflags|objects[n]->oflags)&OF_DESTROYED) Throw("Destruction during ordering");
if(v.t) Throw("Type mismatch in order criteria");
v0=0; v1=v.s; goto compare;
compare:
if(v0==v1) {
p++;
goto criteria;
}
if(v0>v1) goto found;
break;
default: fatal("Internal confusion: Invalid order criteria (%d)\n",orders[p]);
}
}
n=objects[n]->next;
}
found:
// Now it has been found; insert this object previous to the found object, removing from its existing slot.
// (Objects are executed in reverse order, so previous in the linked list means executed next)
if(o->prev==VOIDLINK) firstobj=o->next; else objects[o->prev]->next=o->next;
if(o->next==VOIDLINK) lastobj=o->prev; else objects[o->next]->prev=o->prev;
o->next=n;
o->prev=objects[n]->prev;
objects[n]->prev=obj;
if(o->prev==VOIDLINK) firstobj=obj; else objects[o->prev]->next=obj;
notfound:
objects[obj]->oflags|=OF_ORDERED;
}
static Uint32 x_create(Uint32 from,Uint16 c,Uint32 x,Uint32 y,Uint32 d,Value arg1,Value arg2,Value arg3) {
// This function is only called if x and y and d are already verified to be valid, so need not be checked again.
Uint32 m,n;
int i,xx,yy;
Object*o;
Object*p;
Value v;
if(c==control_class || !c) return VOIDLINK;
// Although this function can be called recursively, StackProtection() is unnecessary,
// because in the case that it is called recursively, it is guaranteed to call either
// collide_with or execute_program (indirectly), which then calls StackProtection().
if((i=classes[c]->collisionLayers) && (xx=collisions_at(x,y)&i)) {
if((i=collide_with(xx,VOIDLINK,x,y,c))&0x01) {
if(i&0x8000) {
if(subst_class) {
m=subst_obj;
v=destroy(VOIDLINK,m,5);
if(v.t) return VOIDLINK;
n=x_create(m,subst_class,x,y,d,arg1,arg2,arg3);
if(n!=VOIDLINK) objects[from]->replacement=objects[m]->replacement=OVALUE(n);
return n;
} else {
return subst_obj;
}
}
return VOIDLINK;
}
}
n=objalloc(c);
if(n==VOIDLINK) Throw("Error creating object");
o=objects[n];
o->x=x;
o->y=y;
o->image=0;
o->dir=d;
o->oflags&=~OF_BIZARRO;
objects[from]->replacement=OVALUE(n);
pflink(n);
v=send_message(from,n,MSG_XCREATE,arg1,arg2,arg3);
if(v.t>TY_MAXTYPE) {
if(classes[objects[n]->class]->order && !(o->oflags&OF_DESTROYED)) set_order(n);
return v_object(v);
} else if(v.t) {
Throw("Type mismatch");
}
if(o->oflags&OF_DESTROYED) return VOIDLINK;
if(v.u) goto skip;
v=send_message(VOIDLINK,n,MSG_CREATE,NVALUE(0),NVALUE(0),NVALUE(0));
if(o->oflags&OF_DESTROYED) return VOIDLINK;
if(o->oflags&OF_BIZARRO) {
skip:
if(classes[objects[n]->class]->order) set_order(n);
return n;
}
for(y=0;y<5;y++) for(x=0;x<5;x++) {
xx=o->x+x-2; yy=o->y+y-2;
if(xx<1 || xx>pfwidth || yy<1 || yy>pfheight) continue;
i=x+5*(4-y);
m=playfield[xx+yy*64-65];
while(m!=VOIDLINK) {
p=objects[m];
if(p->arrivals&(1<<i)) if(m!=n) send_message(n,m,MSG_CREATED,NVALUE(o->x),NVALUE(o->y),v);
m=p->up;
}
}
if(o->oflags&OF_DESTROYED) return VOIDLINK;
if(classes[objects[n]->class]->order) set_order(n);
m=objects[n]->up;
if(m!=VOIDLINK) {
v=send_message(VOIDLINK,n,MSG_SUNK,NVALUE(0),NVALUE(0),v);
while(m!=VOIDLINK) {
send_message(n,m,MSG_FLOATED,NVALUE(0),NVALUE(0),v);
m=objects[m]->up;
}
}
if(o->oflags&OF_DESTROYED) return VOIDLINK;
return n;
}
static Uint32 create(Uint32 from,Uint16 c,Uint32 x,Uint32 y,Uint32 im,Uint32 d) {
Uint32 m,n;
int i,xx,yy;
Object*o;
Object*p;
Value v;
if(d>7) d=0;
if(x<1 || y<1 || x>pfwidth || y>pfheight || c==control_class) return VOIDLINK;
if(!(classes[c]->oflags&OF_BIZARRO) && (i=classes[c]->collisionLayers) && (xx=collisions_at(x,y)&i)) {
if((i=collide_with(xx,VOIDLINK,x,y,c))&0x01) {
if(i&0x8000) {
if(subst_class) {
m=subst_obj;
v=destroy(VOIDLINK,m,5);
if(v.t) return VOIDLINK;
n=x_create(m,subst_class,x,y,d,CVALUE(c),NVALUE(im),NVALUE(0));
if(n!=VOIDLINK) objects[m]->replacement=OVALUE(n);
return n;
} else {
return subst_obj;
}
}
return VOIDLINK;
}
}
n=objalloc(c);
if(n==VOIDLINK) Throw("Error creating object");
o=objects[n];
o->x=x;
o->y=y;
o->image=im;
o->dir=d;
pflink(n);
v=send_message(from,n,MSG_CREATE,NVALUE(0),NVALUE(0),NVALUE(0));
if(o->oflags&OF_DESTROYED) return VOIDLINK;
if(o->oflags&OF_BIZARRO) {
if(classes[objects[n]->class]->order) set_order(n);
return n;
}
for(y=0;y<5;y++) for(x=0;x<5;x++) {
xx=o->x+x-2; yy=o->y+y-2;
if(xx<1 || xx>pfwidth || yy<1 || yy>pfheight) continue;
i=x+5*(4-y);
m=playfield[xx+yy*64-65];
while(m!=VOIDLINK) {
p=objects[m];
if(p->arrivals&(1<<i)) if(m!=n) send_message(n,m,MSG_CREATED,NVALUE(o->x),NVALUE(o->y),v);
m=p->up;
}
}
if(o->oflags&OF_DESTROYED) return VOIDLINK;
if(classes[objects[n]->class]->order) set_order(n);
m=objects[n]->up;
if(m!=VOIDLINK) {
v=send_message(VOIDLINK,n,MSG_SUNK,NVALUE(0),NVALUE(0),v);
while(m!=VOIDLINK) {
send_message(n,m,MSG_FLOATED,NVALUE(0),NVALUE(0),v);
m=objects[m]->up;
}
}
if(o->oflags&OF_DESTROYED) return VOIDLINK;
return n;
}
static Value destroy(Uint32 from,Uint32 to,Uint32 why) {
Object*o;
Value v;
int i,x,y,xx,yy;
Uint32 n;
if(to==VOIDLINK || to==control_obj) return NVALUE(0);
o=objects[to];
// EKS Hero Mesh doesn't check if it already destroyed.
v=why==8?NVALUE(0):send_message(from,to,MSG_DESTROY,NVALUE(0),NVALUE(0),NVALUE(why));
if(!v_bool(v)) {
if(!(o->oflags&OF_DESTROYED)) {
if(firstobj==to) firstobj=o->next;
if(lastobj==to) lastobj=o->prev;
if(o->prev!=VOIDLINK) objects[o->prev]->next=o->next;
if(o->next!=VOIDLINK) objects[o->next]->prev=o->prev;
// This object is not itself removed from the linked list, since it may be destroyed while enumerating all objects
}
o->oflags|=OF_DESTROYED;
if(why!=8 && !(o->oflags&(OF_VISUALONLY|OF_BIZARRO))) {