forked from snesrev/zelda3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathancilla.cpp
7424 lines (6742 loc) · 233 KB
/
ancilla.cpp
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
#include "ancilla.h"
#include "variables.h"
#include "variables_weathervane.h"
#include "variables_happiness_pond.h"
#include "variables_blastwall.h"
#include "variables_skullwoodsfire.h"
#include "variables_breaktowerseal.h"
#include "sprite.h"
#include "hud.h"
#include "load_gfx.h"
#include "tagalong.h"
#include "overworld.h"
#include "tile_detect.h"
#include "player.h"
#include "misc.h"
#include "dungeon.h"
#include "tables/generated_ancilla.h"
static const uint8 kAncilla_Pflags[68] = {
0, 8, 0xc, 0x10, 0x10, 4, 0x10, 0x18, 8, 8, 8, 0, 0x14, 0, 0x10, 0x28,
0x18, 0x10, 0x10, 0x10, 0x10, 0xc, 8, 8, 0x50, 0, 0x10, 8, 0x40, 0, 0xc, 0x24,
0x10, 0xc, 8, 0x10, 0x10, 4, 0xc, 0x1c, 0, 0x10, 0x14, 0x14, 0x10, 8, 0x20, 0x10,
0x10, 0x10, 4, 0, 0x80, 0x10, 4, 0x30, 0x14, 0x10, 0, 0x10, 0, 0, 8, 0,
0x10, 8, 0x78, 0x80,
};
static const int8 kFireRod_Xvel2[12] = {0, 0, -40, 40, 0, 0, -48, 48, 0, 0, -64, 64};
static const int8 kFireRod_Yvel2[12] = {-40, 40, 0, 0, -48, 48, 0, 0, -64, 64, 0, 0};
static const uint8 kTagalongLayerBits[4] = {0x20, 0x10, 0x30, 0x20};
static const uint8 kBombos_Sfx[8] = {0x80, 0x80, 0x80, 0, 0, 0x40, 0x40, 0x40};
const uint8 kBomb_Tab0[11] = {0xA0, 6, 4, 4, 4, 4, 4, 6, 6, 6, 6};
OamEnt *Ancilla_ReceiveItem_Draw(int k, int x, int y);
struct AncillaOamInfo {
uint8 x;
uint8 y;
uint8 flags;
};
#define swordbeam_temp_x (*(uint16*)(g_ram+0x1580E))
#define swordbeam_temp_y (*(uint16*)(g_ram+0x15810))
#define swordbeam_arr ((uint8*)(g_ram+0x15800))
#define swordbeam_var1 (*(uint8*)(g_ram+0x15804))
#define swordbeam_var2 (*(uint8*)(g_ram+0x15808))
void Ancilla_MoveX(int k) {
uint32 t = ancilla_x_subpixel[k] + (ancilla_x_lo[k] << 8) + (ancilla_x_hi[k] << 16) + ((int8)ancilla_x_vel[k] << 4);
ancilla_x_subpixel[k] = t, ancilla_x_lo[k] = t >> 8, ancilla_x_hi[k] = t >> 16;
}
void Ancilla_MoveY(int k) {
uint32 t = ancilla_y_subpixel[k] + (ancilla_y_lo[k] << 8) + (ancilla_y_hi[k] << 16) + ((int8)ancilla_y_vel[k] << 4);
ancilla_y_subpixel[k] = t, ancilla_y_lo[k] = t >> 8, ancilla_y_hi[k] = t >> 16;
}
void Ancilla_MoveZ(int k) {
uint32 t = ancilla_z_subpixel[k] + (ancilla_z[k] << 8) + ((int8)ancilla_z_vel[k] << 4);
ancilla_z_subpixel[k] = t, ancilla_z[k] = t >> 8;
}
uint16 Ancilla_GetX(int k) {
return ancilla_x_lo[k] | ancilla_x_hi[k] << 8;
}
uint16 Ancilla_GetY(int k) {
return ancilla_y_lo[k] | ancilla_y_hi[k] << 8;
}
void Ancilla_SetX(int k, uint16 x) {
ancilla_x_lo[k] = x;
ancilla_x_hi[k] = x >> 8;
}
void Ancilla_SetY(int k, uint16 y) {
ancilla_y_lo[k] = y;
ancilla_y_hi[k] = y >> 8;
}
void Ancilla_SetXY(int k, uint16 x, uint16 y) {
Ancilla_SetX(k, x);
Ancilla_SetY(k, y);
}
void Ancilla_SetOam_XY(OamEnt *oam, uint16 x, uint16 y) {
uint8 yval = 0xf0;
if (x < 256 && y < 256) {
oam->x = x;
if (y < 0xf0)
yval = y;
}
oam->y = yval;
}
uint8 Ancilla_SetSafeOam_XY(OamEnt *oam, uint16 x, uint16 y) {
uint8 rv = 0;
oam->x = x;
if ((uint16)(x + 0x80) < 0x180) {
rv = (x >> 8) & 1;
oam->y = y;
if ((uint16)(y + 0x10) < 0x100)
return rv;
}
oam->y = 0xf0;
return rv;
}
bool Ancilla_HasAncillaOfType(uint8 a) {
for (int k = 5; k >= 0; k--) {
if (ancilla_type[k] == a)
return true;
}
return false;
}
int Ancilla_AllocHigh() {
for (int k = 9; k >= 0; k--) {
if (ancilla_type[k] == 0)
return k;
}
return -1;
}
PairU8 Ancilla_IsBelowPlayer(int k) {
int y = link_y_coord - Ancilla_GetY(k);
PairU8 rv = { (uint8)(sign16(y) ? 1 : 0), (uint8)y };
return rv;
}
PairU8 Ancilla_IsToRightOfPlayer(int k) {
uint16 x = link_x_coord - Ancilla_GetX(k);
PairU8 rv = { (uint8)(sign16(x) ? 1 : 0), (uint8)x };
return rv;
}
ProjectSpeedRet Ancilla_ProjectSpeedTowardsPlayer(int k, uint8 vel) {
if (vel == 0) {
ProjectSpeedRet rv = { 0, 0, 0, 0 };
return rv;
}
PairU8 below = Ancilla_IsBelowPlayer(k);
uint8 r12 = sign8(below.b) ? -below.b : below.b;
PairU8 right = Ancilla_IsToRightOfPlayer(k);
uint8 r13 = sign8(right.b) ? -right.b : right.b;
uint8 t;
bool swapped = false;
if (r13 < r12) {
swapped = true;
t = r12, r12 = r13, r13 = t;
}
uint8 xvel = vel, yvel = 0;
t = 0;
do {
t += r12;
if (t >= r13)
t -= r13, yvel++;
} while (--vel);
if (swapped)
t = xvel, xvel = yvel, yvel = t;
ProjectSpeedRet rv = {
(uint8)(right.a ? -xvel : xvel),
(uint8)(below.a ? -yvel : yvel),
right.b,
below.b
};
return rv;
}
int Ancilla_AllocateOam(int k, uint8 size) {
if (sort_sprites_setting) {
if (ancilla_floor[k])
return OAM_AllocateFromRegionF(size);
else
return OAM_AllocateFromRegionD(size);
} else {
return OAM_AllocateFromRegionA(size);
}
}
OamEnt *Ancilla_CustomAllocateOam(OamEnt *oam) {
int a = (uint8 *)oam - g_ram;
if (sort_sprites_setting) {
if (a < 0x900) {
if (a < 0x8e0)
return oam;
a = 0x820;
} else {
if (a < 0x9d0)
return oam;
a = 0x940;
}
} else {
if (a < 0x990)
return oam;
a = 0x820;
}
oam_cur_ptr = a;
oam_ext_cur_ptr = ((a - 0x800) >> 2) + 0xa20;
return GetOamCurPtr();
}
void Ancilla_PrepOamCoord(int k, Point16U *info) {
oam_priority_value = kTagalongLayerBits[ancilla_floor[k]] << 8;
info->x = Ancilla_GetX(k) - BG2HOFS_copy2;
info->y = Ancilla_GetY(k) - BG2VOFS_copy2;
}
void Ancilla_PrepAdjustedOamCoord(int k, Point16U *info) {
oam_priority_value = kTagalongLayerBits[ancilla_floor[k]] << 8;
info->x = Ancilla_GetX(k) - BG2HOFS_copy;
info->y = Ancilla_GetY(k) - BG2VOFS_copy;
}
void Ancilla_SetupHitBox(int k, SpriteHitBox *hb) {
static const int8 kAncilla_HitBox_X[12] = {4, 4, 4, 4, 3, 3, 2, 11, -16, -16, -1, -8};
static const int8 kAncilla_HitBox_Y[12] = {4, 4, 4, 4, 2, 11, 3, 3, -1, -8, -16, -16};
static const uint8 kAncilla_HitBox_W[12] = {8, 8, 8, 8, 1, 1, 1, 1, 32, 32, 8, 8};
static const uint8 kAncilla_HitBox_H[12] = {8, 8, 8, 8, 1, 1, 1, 1, 8, 8, 32, 32};
int j = ancilla_dir[k];
if (ancilla_type[k] == 0xc)
j |= 8;
int x = Ancilla_GetX(k) + kAncilla_HitBox_X[j];
hb->r0_xlo = x;
hb->r8_xhi = x >> 8;
int y = Ancilla_GetY(k) + kAncilla_HitBox_Y[j];
hb->r1_ylo = y;
hb->r9_yhi = y >> 8;
hb->r2 = kAncilla_HitBox_W[j];
hb->r3 = kAncilla_HitBox_H[j];
}
void Ancilla_SetupBasicHitBox(int k, SpriteHitBox *hb) {
int x = Ancilla_GetX(k) - 8;
hb->r0_xlo = x;
hb->r8_xhi = x >> 8;
int y = Ancilla_GetY(k) - 8 - ancilla_z[k];
hb->r1_ylo = y;
hb->r9_yhi = y >> 8;
hb->r2 = 15;
hb->r3 = 15;
}
bool Ancilla_ReturnIfOutsideBounds(int k, AncillaOamInfo *info) {
static const uint8 kAncilla_FloorFlags[2] = {0x20, 0x10};
info->flags = kAncilla_FloorFlags[ancilla_floor[k]];
if ((info->x = ancilla_x_lo[k] - BG2HOFS_copy2) >= 0xf4 ||
(info->y = ancilla_y_lo[k] - BG2VOFS_copy2) >= 0xf0) {
ancilla_type[k] = 0;
return true;
}
return false;
}
bool Ancilla_CheckPlayerCollision(int k, int j, CheckPlayerCollOut *out) {
static const int16 kAncilla_Coll_Yoffs[5] = {0, 8, 8, 8, 0};
static const int16 kAncilla_Coll_Xoffs[5] = {0, 8, 8, 8, 0};
static const int16 kAncilla_Coll_H[5] = {20, 20, 8, 28, 14};
static const int16 kAncilla_Coll_W[5] = {20, 3, 8, 24, 14};
static const int16 kAncilla_Coll_LinkYoffs[5] = {12, 12, 12, 12, 12};
static const int16 kAncilla_Coll_LinkXoffs[5] = {8, 8, 8, 12, 8};
uint16 x = Ancilla_GetX(k), y = Ancilla_GetY(k);
y += kAncilla_Coll_Yoffs[j] + (int8)ancilla_z[k];
x += kAncilla_Coll_Xoffs[j];
out->r4 = link_y_coord + kAncilla_Coll_LinkYoffs[j] - y;
out->r8 = abs16(out->r4);
out->r6 = link_x_coord + kAncilla_Coll_LinkXoffs[j] - x;
out->r10 = abs16(out->r6);
return out->r8 < kAncilla_Coll_H[j] && out->r10 < kAncilla_Coll_W[j];
}
static const int8 kAncilla_TileColl_Attrs[256] = {
0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0,
1, 1, 1, 1, 0, 0, 0, 0, 2, 2, 2, 2, 0, 3, 3, 3,
0, 0, 0, 0, 0, 0, 1, 1, 4, 4, 4, 4, 4, 4, 4, 4,
1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 3, 3, 3,
0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 4, 4,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
};
static const uint8 kAncilla_TileColl0_Attrs[256] = {
0, 1, 0, 3, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0,
1, 1, 1, 1, 0, 0, 0, 0, 2, 2, 2, 2, 0, 3, 3, 3,
0, 0, 0, 0, 0, 0, 1, 1, 4, 4, 4, 4, 4, 4, 4, 4,
1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 3, 3, 3,
0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 4, 4,
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0,
0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
};
bool Ancilla_CheckTileCollision_Class2_Inner(int k) {
static const int8 kAncilla_CheckTileColl_Y[4] = {-8, 8, 0, 0};
static const int8 kAncilla_CheckTileColl_X[4] = {0, 0, -8, 8};
uint16 x = Ancilla_GetX(k) + kAncilla_CheckTileColl_X[ancilla_dir[k]];
uint16 y = Ancilla_GetY(k) + kAncilla_CheckTileColl_Y[ancilla_dir[k]];
if ((uint16)(y - BG2VOFS_copy2) >= 224 || (uint16)(x - BG2HOFS_copy2) >= 256)
return false;
uint8 tile_attr;
if (!player_is_indoors) {
x >>= 3;
tile_attr = Overworld_GetTileAttrAtLocation(x, y);
} else {
tile_attr = Entity_GetTileAttr(ancilla_floor[k], &x, y);
}
ancilla_tile_attr[k] = tile_attr;
if (tile_attr == 3 && ancilla_floor2[k])
return false;
uint8 t = kAncilla_TileColl_Attrs[tile_attr];
if (t == 0)
return false;
if (t == 2)
return Entity_CheckSlopedTileCollision(x, y);
if (t == 4) {
if (ancilla_floor2[k])
return true;
ancilla_objprio[k] = 1;
return false;
}
if (t == 3)
return ancilla_floor2[k] != 0;
return true;
}
bool Ancilla_CheckTileCollision_Class2(int k) {
if (!dung_hdr_collision)
return Ancilla_CheckTileCollision_Class2_Inner(k);
uint16 x = 0, y = 0;
if (dung_hdr_collision < 3) {
x = BG1HOFS_copy2 - BG2HOFS_copy2;
y = BG1VOFS_copy2 - BG2VOFS_copy2;
}
uint16 oldx = Ancilla_GetX(k), oldy = Ancilla_GetY(k);
Ancilla_SetX(k, oldx + x);
Ancilla_SetY(k, oldy + y);
ancilla_floor[k] = 1;
bool b = Ancilla_CheckTileCollision_Class2_Inner(k);
ancilla_floor[k] = 0;
Ancilla_SetX(k, oldx);
Ancilla_SetY(k, oldy);
return (b | Ancilla_CheckTileCollision_Class2_Inner(k)) != 0;
}
bool Ancilla_CheckInitialTileCollision_Class2(int k) {
static const int16 kAncilla_InitialTileColl_Y[9] = {15, 16, 28, 24, 12, 12, 12, 12, 8};
static const int16 kAncilla_InitialTileColl_X[9] = {8, 8, 8, 8, -1, 0, 17, 16, 0x4b8b}; // wtf
int j = ancilla_dir[k] * 2;
for (int n = 2; n >= 0; n--, j++) {
Ancilla_SetXY(k, link_x_coord + kAncilla_InitialTileColl_X[j],
link_y_coord + kAncilla_InitialTileColl_Y[j]);
if (Ancilla_CheckTileCollision_Class2(k))
return true;
}
return false;
}
bool Ancilla_CheckTileCollisionOneFloorEx(int k, uint16 x, uint16 y) {
if ((uint16)(y - BG2VOFS_copy2) >= 224 || (uint16)(x - BG2HOFS_copy2) >= 256)
return 0;
uint8 tile_attr;
if (!player_is_indoors) {
x >>= 3;
tile_attr = Overworld_GetTileAttrAtLocation(x, y);
} else {
tile_attr = Entity_GetTileAttr(ancilla_floor[k], &x, y);
}
ancilla_tile_attr[k] = tile_attr;
if (tile_attr == 3 && ancilla_floor2[k])
return 0;
uint8 t = kAncilla_TileColl0_Attrs[tile_attr];
if (ancilla_type[k] == 2 && (tile_attr & 0xf0) == 0xc0)
t = 0;
if (!ancilla_objprio[k]) {
if (t == 0)
return false;
if (t == 1)
goto return_true_set_alert;
if (t == 2)
return Entity_CheckSlopedTileCollision(x, y);
if (t == 3) {
if (ancilla_floor2[k])
goto return_true_set_alert;
return 0;
}
}
if (sign8(--ancilla_U[k])) {
ancilla_U[k] = 0;
if (t == 4) {
ancilla_U[k] = 6;
ancilla_objprio[k] ^= 1;
}
}
return 0;
return_true_set_alert:
sprite_alert_flag = 3;
return 1;
}
bool Ancilla_CheckTileCollisionOneFloor(int k) {
static const int8 kAncilla_CheckTileColl0_X[20] = {
8, 8, 0, 16, 4, 4, 0, 16, 4, 4, 4, 12, 12, 12, 4, 12, 0, 0, 0, 0,
};
static const int8 kAncilla_CheckTileColl0_Y[20] = {
0, 16, 5, 5, 0, 16, 4, 4, 4, 12, 5, 5, 4, 12, 12, 12, 0, 0, 0, 0,
};
uint16 x = Ancilla_GetX(k) + kAncilla_CheckTileColl0_X[ancilla_dir[k]];
uint16 y = Ancilla_GetY(k) + kAncilla_CheckTileColl0_Y[ancilla_dir[k]];
return Ancilla_CheckTileCollisionOneFloorEx(k, x, y);
}
uint8 Ancilla_CheckTileCollision(int k) {
if (!player_is_indoors && ancilla_objprio[k]) {
ancilla_tile_attr[k] = 0;
return 0;
}
if (!dung_hdr_collision)
return Ancilla_CheckTileCollisionOneFloor(k);
uint16 x = 0, y = 0;
if (dung_hdr_collision < 3) {
x = BG1HOFS_copy2 - BG2HOFS_copy2;
y = BG1VOFS_copy2 - BG2VOFS_copy2;
}
uint16 oldx = Ancilla_GetX(k), oldy = Ancilla_GetY(k);
Ancilla_SetX(k, oldx + x);
Ancilla_SetY(k, oldy + y);
ancilla_floor[k] = 1;
uint8 b = Ancilla_CheckTileCollisionOneFloor(k);
ancilla_floor[k] = 0;
Ancilla_SetX(k, oldx);
Ancilla_SetY(k, oldy);
return (b << 1) | (uint8)Ancilla_CheckTileCollisionOneFloor(k);
}
uint8 Ancilla_CheckTileCollisionStaggered(int k) {
if ((frame_counter ^ k) & 1)
return Ancilla_CheckTileCollision(k);
return 0;
}
void Sprite_CheckAncillaDamage2(int k, uint8 type) {
static const uint8 kAncilla_Damage[57] = {
6, 1, 11, 0, 0, 0, 0, 8, 0, 6, 0, 12, 1, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 0, 14, 13, 0, 0, 15, 0, 0, 7,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 11,
0, 1, 1, 1, 1, 1, 1, 1, 1,
};
uint8 dmg = kAncilla_Damage[type];
if (dmg == 6 && link_item_bow >= 3) {
if (sprite_type[k] == 0xd7)
sprite_delay_aux4[k] = 32;
dmg = 9;
}
Sprite_Func11(k, dmg);
}
void Sprite_CheckAncillaDamage(int k, uint8 type) {
if (!sign8(sprite_hit_timer[k]))
Sprite_CheckAncillaDamage2(k, type);
}
bool Ancilla_CheckSingleBasicSpriteCollision(int k, int j) {
SpriteHitBox hb;
Ancilla_SetupBasicHitBox(k, &hb);
Sprite_SetupHitBox(j, &hb);
if (!Utility_CheckIfHitBoxesOverlap(&hb))
return false;
if (sprite_type[j] == 0x92 && sprite_C[j] < 3)
return true;
if (sprite_type[j] == 0x80 && sprite_delay_aux4[j] == 0) {
sprite_delay_aux4[j] = 24;
sprite_D[j] ^= 1;
}
if (sprite_ignore_projectile[j])
return false;
int x = Ancilla_GetX(k) - 8, y = Ancilla_GetY(k) - 8 - ancilla_z[k];
ProjectSpeedRet pt = Sprite_ProjectSpeedTowardsEntity(j, x, y, 80);
sprite_y_recoil[j] = ~pt.y;
sprite_x_recoil[j] = ~pt.x;
Sprite_CheckAncillaDamage(j, ancilla_type[k]);
return true;
}
int Ancilla_CheckBasicSpriteCollision(int k) {
for (int j = 15; j >= 0; j--) {
if (((j ^ frame_counter) & 3 | sprite_pause[j] | sprite_hit_timer[j]) != 0)
continue;
if (sprite_state[j] < 9 || !(sprite_defl_bits[j] & 2) && ancilla_objprio[k])
continue;
if (ancilla_floor[k] != sprite_floor[j])
continue;
if (ancilla_type[k] == 0x2c && (sprite_type[j] == 0x1e || sprite_type[j] == 0x90))
continue;
if (Ancilla_CheckSingleBasicSpriteCollision(k, j))
return j;
}
return -1;
}
void Ancilla_CreateDeflectedArrow(int k) {
ancilla_type[k] = 0;
SpriteSpawnInfo info;
int j = Sprite_SpawnDynamically(k, 0x1b, &info);
if (j >= 0) {
sprite_x_lo[j] = ancilla_x_lo[k];
sprite_x_hi[j] = ancilla_x_hi[k];
sprite_y_lo[j] = ancilla_y_lo[k];
sprite_y_hi[j] = ancilla_y_hi[k];
sprite_state[j] = 6;
sprite_delay_main[j] = 31;
sprite_x_vel[j] = ancilla_x_vel[k];
sprite_y_vel[j] = ancilla_y_vel[k];
sprite_floor[j] = link_is_on_lower_level;
Sprite_PlaceRupulseSpark(j);
}
}
bool Ancilla_CheckOneSpriteCollision(int k, int j) {
int i;
SpriteHitBox hb;
Ancilla_SetupHitBox(k, &hb);
Sprite_SetupHitBox(j, &hb);
if (!Utility_CheckIfHitBoxesOverlap(&hb))
return false;
bool return_value = true;
if (sprite_flags[j] & 8 && ancilla_type[k] == 9) {
if (sprite_type[j] != 0x1b) {
Ancilla_CreateDeflectedArrow(k);
return false;
}
if (link_item_bow < 3) {
Ancilla_CreateDeflectedArrow(k);
} else {
return_value = false;
}
}
if (sprite_defl_bits[j] & 0x10) {
static const uint8 kAncilla_CheckSpriteColl_Dir[4] = {2, 3, 0, 1};
ancilla_dir[k] &= 3;
if (ancilla_dir[k] == kAncilla_CheckSpriteColl_Dir[ancilla_dir[k]])
goto return_true_set_alert;
}
if (ancilla_type[k] == 5 || ancilla_type[k] == 0x1f) {
if (ancilla_type[k] == 0x1f && sprite_type[j] == 0x8d)
goto skip;
if (sprite_hit_timer[j])
goto return_true_set_alert;
if (sprite_defl_bits[j] & 2) {
skip:
sprite_B[j] = k + 1;
sprite_unk2[j] = ancilla_type[k];
goto return_true_set_alert;
}
}
if (!sprite_ignore_projectile[j]) {
static const int8 kAncilla_CheckSpriteColl_RecoilX[4] = {0, 0, -64, 64};
static const int8 kAncilla_CheckSpriteColl_RecoilY[4] = {-64, 64, 0, 0};
if (sprite_type[j] == 0x92 && sprite_C[j] < 3)
goto return_true_set_alert;
i = ancilla_dir[k] & 3;
sprite_x_recoil[j] = kAncilla_CheckSpriteColl_RecoilX[i];
sprite_y_recoil[j] = kAncilla_CheckSpriteColl_RecoilY[i];
byte_7E0FB6 = k;
Sprite_CheckAncillaDamage(j, ancilla_type[k]);
return_true_set_alert:
sprite_unk2[j] = ancilla_type[k];
sprite_alert_flag = 3;
return return_value;
}
return false;
}
int Ancilla_CheckSpriteCollision(int k) {
for (int j = 15; j >= 0; j--) {
if (ancilla_type[k] == 9 || ancilla_type[k] == 0x1f || ((j ^ frame_counter) & 3 | sprite_pause[j]) == 0) {
if ((sprite_state[j] >= 9 && (sprite_defl_bits[j] & 2 || !ancilla_objprio[k])) && ancilla_floor[k] == sprite_floor[j]) {
if (Ancilla_CheckOneSpriteCollision(k, j))
return j;
}
}
}
return -1;
}
uint8 Ancilla_GetPanFlag(int k) {
return Sound_GetPanForX(Ancilla_GetX(k));
}
void Ancilla_DoSfx1(int k, uint8 v) {
byte_7E0CF8 = v;
sound_effect_ambient = v | Ancilla_GetPanFlag(k);
}
void Ancilla_DoSfx2(int k, uint8 v) {
byte_7E0CF8 = v;
sound_effect_1 = v | Ancilla_GetPanFlag(k);
}
void Ancilla_DoSfx3(int k, uint8 v) {
byte_7E0CF8 = v;
sound_effect_2 = v | Ancilla_GetPanFlag(k);
}
int Ancilla_AllocInit(uint8 type, uint8 y) {
int n = 0;
for (int k = 0; k < 5; k++) {
if (ancilla_type[k] == type)
n++;
}
if (y + 1 == n)
return -1;
int k = (type == 7 || type == 8) ? 1 : 4;
for (; k >= 0; k--) {
if (ancilla_type[k] == 0)
return k;
}
do {
if (sign8(--ancilla_alloc_rotate))
ancilla_alloc_rotate = y;
uint8 type = ancilla_type[ancilla_alloc_rotate];
if (type == 0x3c || type == 0x13 || type == 0xa)
return ancilla_alloc_rotate;
} while (ancilla_alloc_rotate != 0);
return -1;
}
int Ancilla_Func1(uint8 type, uint8 y) {
int k = Ancilla_AllocInit(type, y);
if (k >= 0) {
ancilla_type[k] = type;
ancilla_numspr[k] = kAncilla_Pflags[type];
ancilla_floor[k] = link_is_on_lower_level;
ancilla_floor2[k] = link_is_on_lower_level_mirror;
ancilla_y_vel[k] = 0;
ancilla_x_vel[k] = 0;
ancilla_objprio[k] = 0;
ancilla_U[k] = 0;
}
return k;
}
void AddSomarianBlockDivide(int k) {
ancilla_type[k] = 0x2e;
ancilla_numspr[k] = kAncilla_Pflags[0x2e];
ancilla_aux_timer[k] = 3;
ancilla_step[k] = 0;
ancilla_item_to_link[k] = 0;
ancilla_arr3[k] = 0;
ancilla_arr1[k] = 0;
ancilla_R[k] = 0;
ancilla_objprio[k] = 0;
dung_flag_somaria_block_switch = 0;
sound_effect_2 = Ancilla_GetPanFlag(k) | 1;
}
void Ancilla_RepulseSpark() {
if (!repulsespark_timer)
return;
sprite_alert_flag = 2;
if (sign8(--repulsespark_anim_delay)) {
repulsespark_timer--;
repulsespark_anim_delay = 1;
}
if (sort_sprites_setting) {
if (repulsespark_floor_status)
OAM_AllocateFromRegionF(0x10);
else
OAM_AllocateFromRegionD(0x10);
} else {
OAM_AllocateFromRegionA(0x10);
}
uint8 x = repulsespark_x_lo - BG2HOFS_copy2;
uint8 y = repulsespark_y_lo - BG2VOFS_copy2;
if (x >= 0xf8 || y >= 0xf0) {
repulsespark_timer = 0;
return;
}
OamEnt *oam = GetOamCurPtr();
static const uint8 kRepulseSpark_Flags[4] = {0x22, 0x12, 0x22, 0x22};
uint8 flags = kRepulseSpark_Flags[repulsespark_floor_status];
if (repulsespark_timer >= 3) {
oam->x = x;
oam->y = y;
oam->charnum = (repulsespark_timer < 9) ? 0x92 : 0x80;
oam->flags = flags;
bytewise_extended_oam[oam - oam_buf] = 0;
return;
}
oam[0].x = x - 4;
oam[2].x = x - 4;
oam[1].x = x + 4;
oam[3].x = x + 4;
oam[0].y = y - 4;
oam[1].y = y - 4;
oam[2].y = y + 4;
oam[3].y = y + 4;
oam[0].flags = flags;
oam[1].flags = flags | 0x40;
oam[2].flags = flags | 0x80;
oam[3].flags = flags | 0xc0;
static const uint8 kRepulseSpark_Char[3] = {0x93, 0x82, 0x81};
uint8 c = kRepulseSpark_Char[repulsespark_timer];
oam[0].charnum = c;
oam[1].charnum = c;
oam[2].charnum = c;
oam[3].charnum = c;
uint8 *ext = &bytewise_extended_oam[oam - oam_buf];
ext[0] = ext[1] = ext[2] = ext[3] = 0;
}
void SomarianBlast_Draw(int k) {
static const uint8 kSomarianBlast_Flags[2] = {2, 6};
AncillaOamInfo info;
if (Ancilla_ReturnIfOutsideBounds(k, &info))
return;
info.flags |= kSomarianBlast_Flags[ancilla_item_to_link[k]];
if (ancilla_objprio[k])
info.flags |= 0x30;
static const int8 kSomarianBlast_Draw_X0[24] = {
0, 0, 0, 0, 4, 4, 0, 0, 0, 0, 4, 4, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
};
static const int8 kSomarianBlast_Draw_X1[24] = {
8, 8, 8, 8, 4, 4, 8, 8, 8, 8, 4, 4, 0, 0, 0, 0,
8, 8, 0, 0, 0, 0, 8, 8,
};
static const uint8 kSomarianBlast_Draw_Y0[24] = {
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4, 4, 0, 0, 0, 0, 4, 4,
};
static const uint8 kSomarianBlast_Draw_Y1[24] = {
0, 0, 0, 0, 8, 8, 0x80, 0, 0, 0, 8, 8, 0x80, 8, 8, 8,
4, 4, 0x80, 8, 8, 8, 4, 4,
};
static const uint8 kSomarianBlast_Draw_Flags0[24] = {
0xc0, 0xc0, 0xc0, 0xc0, 0x80, 0xc0, 0x40, 0x40, 0x40, 0x40, 0, 0x40, 0x40, 0x40, 0x40, 0x40,
0x40, 0xc0, 0, 0, 0, 0, 0, 0x80,
};
static const uint8 kSomarianBlast_Draw_Flags1[24] = {
0x80, 0x80, 0x80, 0x80, 0x80, 0xc0, 0, 0, 0, 0, 0, 0x40, 0xc0, 0xc0, 0xc0, 0xc0,
0x40, 0xc0, 0x80, 0x80, 0x80, 0x80, 0, 0x80,
};
static const uint8 kSomarianBlast_Draw_Char0[24] = {
0x50, 0x50, 0x44, 0x44, 0x52, 0x52, 0x50, 0x50, 0x44, 0x44, 0x51, 0x51, 0x43, 0x43, 0x42, 0x42,
0x41, 0x41, 0x43, 0x43, 0x42, 0x42, 0x40, 0x40,
};
static const uint8 kSomarianBlast_Draw_Char1[24] = {
0x50, 0x50, 0x44, 0x44, 0x51, 0x51, 0x50, 0x50, 0x44, 0x44, 0x52, 0x52, 0x43, 0x43, 0x42, 0x42,
0x40, 0x40, 0x43, 0x43, 0x42, 0x42, 0x41, 0x41,
};
OamEnt *oam = GetOamCurPtr();
int j = ancilla_dir[k] * 6 + ancilla_step[k];
oam[0].x = info.x + kSomarianBlast_Draw_X0[j];
oam[1].x = info.x + kSomarianBlast_Draw_X1[j];
if (!sign8(kSomarianBlast_Draw_Y0[j]))
oam[0].y = info.y + kSomarianBlast_Draw_Y0[j];
if (!sign8(kSomarianBlast_Draw_Y1[j]))
oam[1].y = info.y + kSomarianBlast_Draw_Y1[j];
oam[0].charnum = 0x82 + kSomarianBlast_Draw_Char0[j];
oam[1].charnum = 0x82 + kSomarianBlast_Draw_Char1[j];
oam[0].flags = info.flags | kSomarianBlast_Draw_Flags0[j];
oam[1].flags = info.flags | kSomarianBlast_Draw_Flags1[j];
bytewise_extended_oam[oam - oam_buf] = 0;
bytewise_extended_oam[oam - oam_buf + 1] = 0;
}
void Ancilla_SomarianBlast(int k) {
static const uint8 kSomarianBlast_Mask[6] = {7, 3, 1, 0, 0, 0};
if (!submodule_index) {
if (!(frame_counter & kSomarianBlast_Mask[ancilla_step[k]])) {
Ancilla_MoveX(k);
Ancilla_MoveY(k);
}
if (ancilla_timer[k] == 0) {
ancilla_timer[k] = 3;
uint8 a = ancilla_step[k] + 1;
if (a >= 6)
a = 4;
ancilla_step[k] = a;
}
if (Ancilla_CheckSpriteCollision(k) >= 0 || Ancilla_CheckTileCollisionStaggered(k)) {
ancilla_type[k] = 4;
ancilla_timer[k] = 7;
ancilla_numspr[k] = 16;
}
}
SomarianBlast_Draw(k);
}
void FireShot_Draw(int k) {
static const uint8 kFireShot_Draw_X2[16] = {7, 0, 8, 0, 8, 4, 0, 0, 2, 8, 0, 0, 1, 4, 9, 0};
static const uint8 kFireShot_Draw_Y2[16] = {1, 4, 9, 0, 7, 0, 8, 0, 8, 4, 0, 0, 2, 8, 0, 0};
static const uint8 kFireShot_Draw_Char2[3] = {0x8d, 0x9d, 0x9c};
AncillaOamInfo info;
if (Ancilla_ReturnIfOutsideBounds(k, &info))
return;
if (ancilla_objprio[k])
info.flags |= 0x30;
OamEnt *oam = GetOamCurPtr();
int j = ancilla_item_to_link[k] & 0xc;
for (int i = 2; i >= 0; i--) {
oam->x = info.x + kFireShot_Draw_X2[j + i];
oam->y = info.y + kFireShot_Draw_Y2[j + i];
oam->charnum = kFireShot_Draw_Char2[i];
oam->flags = info.flags | 2;
bytewise_extended_oam[oam - oam_buf] = 0;
oam++;
}
}
void ConsumingFire_TransmuteToSkullWoodsFire(int k) {
if (player_is_indoors || !(BYTE(overworld_screen_index) & 0x40))
return;
ancilla_type[0] = 0x34;
ancilla_type[1] = 0;
ancilla_type[2] = 0;
ancilla_type[3] = 0;
ancilla_type[4] = 0;
ancilla_type[5] = 0;
flag_for_boomerang_in_place = 0;
ancilla_numspr[0] = kAncilla_Pflags[0x34];
skullwoodsfire_var0[0] = 253;
skullwoodsfire_var0[1] = 254;
skullwoodsfire_var0[2] = 255;
skullwoodsfire_var0[3] = 0;
skullwoodsfire_var4 = 0;
skullwoodsfire_var5[0] = 5;
skullwoodsfire_var5[1] = 5;
skullwoodsfire_var5[2] = 5;
skullwoodsfire_var5[3] = 5;
ancilla_aux_timer[0] = 5;
skullwoodsfire_var9 = 0x100;
skullwoodsfire_var10 = 0x100;
skullwoodsfire_var11 = 0x98;
skullwoodsfire_var12 = 0x98;
trigger_special_entrance = 2;
subsubmodule_index = 0;
BYTE(R16) = 0;
ancilla_floor[0] = link_is_on_lower_level;
ancilla_floor2[0] = link_is_on_lower_level_mirror;
ancilla_item_to_link[0] = 0;
ancilla_step[0] = 0;
}
void Ancilla_FireShot(int k) {
if (ancilla_step[k] == 0) {
if (!submodule_index) {
ancilla_L[k] = 0;
Ancilla_MoveX(k);
Ancilla_MoveY(k);
uint8 coll = Ancilla_CheckSpriteCollision(k) >= 0;
if (!coll) {
ancilla_dir[k] |= 8;
coll = Ancilla_CheckTileCollision(k);
ancilla_L[k] = ancilla_tile_attr[k];
if (!coll) {
ancilla_dir[k] |= 12;
uint8 bak = ancilla_U[k];
coll = Ancilla_CheckTileCollision(k);
ancilla_U[k] = bak;
}
}
if (coll) {
ancilla_step[k]++;
ancilla_timer[k] = 31;
ancilla_numspr[k] = 8;
Ancilla_DoSfx2(k, 0x2a);
}
ancilla_item_to_link[k]++;
ancilla_dir[k] &= ~0xC;
if (((byte_7E0333 = ancilla_L[k]) & 0xf0) == 0xc0 || ((byte_7E0333 = ancilla_tile_attr[k]) & 0xf0) == 0xc0)
Dungeon_LightTorch();
}
FireShot_Draw(k);
} else {
AncillaOamInfo info;
Ancilla_CheckBasicSpriteCollision(k);
if (Ancilla_ReturnIfOutsideBounds(k, &info))
return;
OamEnt *oam = GetOamCurPtr();
if (!ancilla_timer[k]) {
uint8 old_type = ancilla_type[k];
ancilla_type[k] = 0;
if (old_type != 0x2f && BYTE(overworld_screen_index) == 64 && ancilla_tile_attr[k] == 0x43)
ConsumingFire_TransmuteToSkullWoodsFire(k);
return;
}
int j = ancilla_timer[k] >> 3;
if (j != 0) {
static const uint8 kFireShot_Draw_Char[3] = {0xa2, 0xa0, 0x8e};
oam->x = info.x;
oam->y = info.y;
oam->charnum = kFireShot_Draw_Char[j - 1];
oam->flags = info.flags | 2;
bytewise_extended_oam[oam - oam_buf] = 2;
} else {
bytewise_extended_oam[oam - oam_buf] = 0;
bytewise_extended_oam[oam - oam_buf + 1] = 0;
oam[0].x = info.x;
oam[1].x = info.x + 8;
oam[0].y = info.y - 3;
oam[1].y = info.y - 3;
oam[0].charnum = 0xa4;
oam[1].charnum = 0xa5;
oam[0].flags = info.flags | 2;
oam[1].flags = info.flags | 2;
}
}
}
void Ancilla_Empty(int k) {
}
void Ancilla_BeamHit(int k) {
static const int8 kBeamHit_X[16] = {-12, 20, -12, 20, -8, 16, -8, 16, -4, 12, -4, 12, 0, 8, 0, 8};
static const int8 kBeamHit_Y[16] = {-12, -12, 20, 20, -8, -8, 16, 16, -4, -4, 12, 12, 0, 0, 8, 8};
static const uint8 kBeamHit_Char[16] = {0x53, 0x53, 0x53, 0x53, 0x53, 0x53, 0x53, 0x53, 0x53, 0x53, 0x53, 0x53, 0x54, 0x54, 0x54, 0x54};
static const uint8 kBeamHit_Flags[16] = {0x40, 0, 0xc0, 0x80, 0x40, 0, 0xc0, 0x80, 0x40, 0, 0xc0, 0x80, 0, 0x40, 0x80, 0xc0};
AncillaOamInfo info;
if (Ancilla_ReturnIfOutsideBounds(k, &info))
return;
if (!ancilla_timer[k]) {
ancilla_type[k] = 0;
return;
}
OamEnt *oam = GetOamCurPtr();
int j = ancilla_timer[k] >> 1;
uint16 ancilla_x = Ancilla_GetX(k);
uint16 ancilla_y = Ancilla_GetY(k);
uint8 r7 = ancilla_x - BG2HOFS_copy2;
uint8 r6 = ancilla_y - BG2VOFS_copy2;
for (int i = 3; i >= 0; i--, oam++) {
int m = j * 4 + i;
oam->x = info.x + kBeamHit_X[m];
oam->y = info.y + kBeamHit_Y[m];
oam->charnum = kBeamHit_Char[m] + 0x82;
oam->flags = kBeamHit_Flags[m] | 2 | info.flags;
uint16 x_adj = (uint16)(ancilla_x + (int8)(oam->x - r7) - BG2HOFS_copy2);
bytewise_extended_oam[oam - oam_buf] = (x_adj >= 0x100) ? 1 : 0;
uint16 y_adj = (uint16)(ancilla_y + (int8)(oam->y - r6) - BG2VOFS_copy2 + 0x10);
if (y_adj >= 0x100)
oam->y = 0xf0;
}
}