forked from pret/pokeemerald
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfield_tasks.c
957 lines (857 loc) · 28.7 KB
/
field_tasks.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
#include "global.h"
#include "bike.h"
#include "clock.h"
#include "event_data.h"
#include "field_camera.h"
#include "field_effect_helpers.h"
#include "field_player_avatar.h"
#include "field_special_scene.h"
#include "field_tasks.h"
#include "fieldmap.h"
#include "item.h"
#include "main.h"
#include "metatile_behavior.h"
#include "overworld.h"
#include "script.h"
#include "secret_base.h"
#include "sound.h"
#include "task.h"
#include "constants/field_tasks.h"
#include "constants/items.h"
#include "constants/songs.h"
#include "constants/metatile_labels.h"
/* This file handles some persistent tasks that run in the overworld.
* - Task_RunTimeBasedEvents: Periodically updates local time and RTC events. Also triggers ambient cries.
* - Task_MuddySlope: Handles the metatile animation when the player steps on muddy slopes.
* - Task_RunPerStepCallback: Calls one of the functions in sPerStepCallbacks, listed below...
* . DummyPerStepCallback: Default, does nothing
* . AshGrassPerStepCallback: Removes the ash from ash-covered grass that the player steps on.
* . FortreeBridgePerStepCallback: Depresses Fortree log bridges that the player steps on.
* . PacifidlogBridgePerStepCallback: Submerges Pacifidlog log bridges that the player steps on.
* . SootopolisGymIcePerStepCallback: Cracks/breaks ice in Sootopolis Gym that the player steps on.
* . EndTruckSequence: Sets the moving truck boxes to their final position when the truck sequence ends.
* . SecretBasePerStepCallback: Records the decorations in a friend's secret base that the player steps on.
* . CrackedFloorPerStepCallback: Breaks cracked floors that the player steps on.
*
* NOTE: "PerStep" is perhaps misleading. One function in sPerStepCallbacks is called
* every frame while in the overworld by Task_RunPerStepCallback regardless of
* whether or not steps are being taken. However, nearly all of the functions in
* the table check if the player has moved from their previous position before
* doing anything else.
*/
struct PacifidlogMetatileOffsets
{
s8 x;
s8 y;
u16 metatileId;
};
static void DummyPerStepCallback(u8);
static void AshGrassPerStepCallback(u8);
static void FortreeBridgePerStepCallback(u8);
static void PacifidlogBridgePerStepCallback(u8);
static void SootopolisGymIcePerStepCallback(u8);
static void CrackedFloorPerStepCallback(u8);
static void Task_MuddySlope(u8);
static const TaskFunc sPerStepCallbacks[] =
{
[STEP_CB_DUMMY] = DummyPerStepCallback,
[STEP_CB_ASH] = AshGrassPerStepCallback,
[STEP_CB_FORTREE_BRIDGE] = FortreeBridgePerStepCallback,
[STEP_CB_PACIFIDLOG_BRIDGE] = PacifidlogBridgePerStepCallback,
[STEP_CB_SOOTOPOLIS_ICE] = SootopolisGymIcePerStepCallback,
[STEP_CB_TRUCK] = EndTruckSequence,
[STEP_CB_SECRET_BASE] = SecretBasePerStepCallback,
[STEP_CB_CRACKED_FLOOR] = CrackedFloorPerStepCallback
};
// Each array has 4 pairs of data, each pair representing two metatiles of a log and their relative position.
// The 4 pairs are for:
// 0: If the player is standing on the top of a vertical log
// 1: If the player is standing on the bottom of a vertical log
// 2: If the player is standing on the left of a horizontal log
// 3: If the player is standing on the right of a horizontal log
// i.e. the element with an offset of 0,0 is the one the player is standing on.
static const struct PacifidlogMetatileOffsets sHalfSubmergedBridgeMetatileOffsets[] =
{
{ 0, 0, METATILE_Pacifidlog_HalfSubmergedLogs_VerticalTop}, {0, 1, METATILE_Pacifidlog_HalfSubmergedLogs_VerticalBottom},
{ 0, -1, METATILE_Pacifidlog_HalfSubmergedLogs_VerticalTop}, {0, 0, METATILE_Pacifidlog_HalfSubmergedLogs_VerticalBottom},
{ 0, 0, METATILE_Pacifidlog_HalfSubmergedLogs_HorizontalLeft}, {1, 0, METATILE_Pacifidlog_HalfSubmergedLogs_HorizontalRight},
{-1, 0, METATILE_Pacifidlog_HalfSubmergedLogs_HorizontalLeft}, {0, 0, METATILE_Pacifidlog_HalfSubmergedLogs_HorizontalRight}
};
static const struct PacifidlogMetatileOffsets sFullySubmergedBridgeMetatileOffsets[] =
{
{ 0, 0, METATILE_Pacifidlog_SubmergedLogs_VerticalTop}, {0, 1, METATILE_Pacifidlog_SubmergedLogs_VerticalBottom},
{ 0, -1, METATILE_Pacifidlog_SubmergedLogs_VerticalTop}, {0, 0, METATILE_Pacifidlog_SubmergedLogs_VerticalBottom},
{ 0, 0, METATILE_Pacifidlog_SubmergedLogs_HorizontalLeft}, {1, 0, METATILE_Pacifidlog_SubmergedLogs_HorizontalRight},
{-1, 0, METATILE_Pacifidlog_SubmergedLogs_HorizontalLeft}, {0, 0, METATILE_Pacifidlog_SubmergedLogs_HorizontalRight}
};
static const struct PacifidlogMetatileOffsets sFloatingBridgeMetatileOffsets[] =
{
{ 0, 0, METATILE_Pacifidlog_FloatingLogs_VerticalTop}, {0, 1, METATILE_Pacifidlog_FloatingLogs_VerticalBottom},
{ 0, -1, METATILE_Pacifidlog_FloatingLogs_VerticalTop}, {0, 0, METATILE_Pacifidlog_FloatingLogs_VerticalBottom},
{ 0, 0, METATILE_Pacifidlog_FloatingLogs_HorizontalLeft}, {1, 0, METATILE_Pacifidlog_FloatingLogs_HorizontalRight},
{-1, 0, METATILE_Pacifidlog_FloatingLogs_HorizontalLeft}, {0, 0, METATILE_Pacifidlog_FloatingLogs_HorizontalRight}
};
// Each element corresponds to a y coordinate row in the sootopolis gym 1F map.
// The rows with ice each have a temp var used to track the ice steps. Each bit in the var
// represents whether ice at that x coordinate (starting from the left edge) has been visited.
// This method of tracking steps will break if the ice puzzle is more than 16 map spaces wide.
static const u16 sSootopolisGymIceRowVars[] =
{
0,
0,
0,
0,
0,
0,
VAR_TEMP_1,
VAR_TEMP_2,
VAR_TEMP_3,
VAR_TEMP_4,
0,
0,
VAR_TEMP_5,
VAR_TEMP_6,
VAR_TEMP_7,
0,
0,
VAR_TEMP_8,
VAR_TEMP_9,
VAR_TEMP_A,
0,
0,
0,
0,
0,
0
};
#define tCallbackId data[0]
static void Task_RunPerStepCallback(u8 taskId)
{
int idx = gTasks[taskId].tCallbackId;
sPerStepCallbacks[idx](taskId);
}
#define tState data[0]
#define tAmbientCryState data[1]
#define tAmbientCryDelay data[2]
#define TIME_UPDATE_INTERVAL (1 << 12)
static void RunTimeBasedEvents(s16 *data)
{
switch (tState)
{
case 0:
if (gMain.vblankCounter1 & TIME_UPDATE_INTERVAL)
{
DoTimeBasedEvents();
tState++;
}
break;
case 1:
if (!(gMain.vblankCounter1 & TIME_UPDATE_INTERVAL))
tState--;
break;
}
}
static void Task_RunTimeBasedEvents(u8 taskId)
{
s16 *data = gTasks[taskId].data;
if (!ArePlayerFieldControlsLocked())
{
RunTimeBasedEvents(data);
UpdateAmbientCry(&tAmbientCryState, (u16*) &tAmbientCryDelay);
}
}
#undef tState
void SetUpFieldTasks(void)
{
if (!FuncIsActiveTask(Task_RunPerStepCallback))
{
u8 taskId = CreateTask(Task_RunPerStepCallback, 80);
gTasks[taskId].tCallbackId = STEP_CB_DUMMY;
}
if (!FuncIsActiveTask(Task_MuddySlope))
CreateTask(Task_MuddySlope, 80);
if (!FuncIsActiveTask(Task_RunTimeBasedEvents))
CreateTask(Task_RunTimeBasedEvents, 80);
}
void ActivatePerStepCallback(u8 callbackId)
{
u8 taskId = FindTaskIdByFunc(Task_RunPerStepCallback);
if (taskId != TASK_NONE)
{
s32 i;
s16 *data = gTasks[taskId].data;
for (i = 0; i < NUM_TASK_DATA; i++)
data[i] = 0;
if (callbackId >= ARRAY_COUNT(sPerStepCallbacks))
tCallbackId = STEP_CB_DUMMY;
else
tCallbackId = callbackId;
}
}
void ResetFieldTasksArgs(void)
{
u8 taskId;
s16 *data;
taskId = FindTaskIdByFunc(Task_RunPerStepCallback);
if (taskId != TASK_NONE)
data = gTasks[taskId].data;
taskId = FindTaskIdByFunc(Task_RunTimeBasedEvents);
if (taskId != TASK_NONE)
{
data = gTasks[taskId].data;
tAmbientCryState = 0;
tAmbientCryDelay = 0;
}
}
#undef tAmbientCryState
#undef tAmbientCryDelay
static void DummyPerStepCallback(u8 taskId)
{
}
static const struct PacifidlogMetatileOffsets *GetPacifidlogBridgeMetatileOffsets(const struct PacifidlogMetatileOffsets *offsets, u16 metatileBehavior)
{
if (MetatileBehavior_IsPacifidlogVerticalLogTop(metatileBehavior))
return &offsets[0 * 2];
else if (MetatileBehavior_IsPacifidlogVerticalLogBottom(metatileBehavior))
return &offsets[1 * 2];
else if (MetatileBehavior_IsPacifidlogHorizontalLogLeft(metatileBehavior))
return &offsets[2 * 2];
else if (MetatileBehavior_IsPacifidlogHorizontalLogRight(metatileBehavior))
return &offsets[3 * 2];
else
return NULL;
}
static void TrySetPacifidlogBridgeMetatiles(const struct PacifidlogMetatileOffsets *offsets, s16 x, s16 y, bool32 redrawMap)
{
offsets = GetPacifidlogBridgeMetatileOffsets(offsets, MapGridGetMetatileBehaviorAt(x, y));
// If offsets is NULL, position is not a log (don't set it)
if (offsets)
{
// Set both metatiles of the log
MapGridSetMetatileIdAt(x + offsets[0].x, y + offsets[0].y, offsets[0].metatileId);
if (redrawMap)
CurrentMapDrawMetatileAt(x + offsets[0].x, y + offsets[0].y);
MapGridSetMetatileIdAt(x + offsets[1].x, y + offsets[1].y, offsets[1].metatileId);
if (redrawMap)
CurrentMapDrawMetatileAt(x + offsets[1].x, y + offsets[1].y);
}
}
static void TrySetLogBridgeHalfSubmerged(s16 x, s16 y, bool32 redrawMap)
{
TrySetPacifidlogBridgeMetatiles(sHalfSubmergedBridgeMetatileOffsets, x, y, redrawMap);
}
static void TrySetLogBridgeFullySubmerged(s16 x, s16 y, bool32 redrawMap)
{
TrySetPacifidlogBridgeMetatiles(sFullySubmergedBridgeMetatileOffsets, x, y, redrawMap);
}
static void TrySetLogBridgeFloating(s16 x, s16 y, bool32 redrawMap)
{
TrySetPacifidlogBridgeMetatiles(sFloatingBridgeMetatileOffsets, x, y, redrawMap);
}
// Returns FALSE if player has moved from one end of a log to the other (log should remain submerged).
// Otherwise it returns TRUE.
static bool32 ShouldRaisePacifidlogLogs(s16 newX, s16 newY, s16 oldX, s16 oldY)
{
u16 oldBehavior = MapGridGetMetatileBehaviorAt(oldX, oldY);
if (MetatileBehavior_IsPacifidlogVerticalLogTop(oldBehavior))
{
// Still on same one if moved from top to bottom
if (newY > oldY)
return FALSE;
}
else if (MetatileBehavior_IsPacifidlogVerticalLogBottom(oldBehavior))
{
// Still on same one if moved from bottom to top
if (newY < oldY)
return FALSE;
}
else if (MetatileBehavior_IsPacifidlogHorizontalLogLeft(oldBehavior))
{
// Still on same one if moved from left to right
if (newX > oldX)
return FALSE;
}
else if (MetatileBehavior_IsPacifidlogHorizontalLogRight(oldBehavior))
{
// Still on same one if moved from right to left
if (newX < oldX)
return FALSE;
}
// Player is either on a different log or no log at all
return TRUE;
}
// Returns FALSE if player has moved from one end of a log to the other (log should remain submerged).
// Otherwise it returns TRUE.
// This is the effectively the same as ShouldRaisePacifidlogLogs, as it swaps both the conditions and which position's behavior to check.
// In effect the previous function asks "was the player's previous position not the other end of a log they're standing on?"
// while this function asks "is the player's current position not the other end of a log they were previously standing on?"
// and with the same positions both questions always have the same answer.
static bool32 ShouldSinkPacifidlogLogs(s16 newX, s16 newY, s16 oldX, s16 oldY)
{
u16 newBehavior = MapGridGetMetatileBehaviorAt(newX, newY);
if (MetatileBehavior_IsPacifidlogVerticalLogTop(newBehavior))
{
// Still on same one if moved from bottom to top
if (newY < oldY)
return FALSE;
}
else if (MetatileBehavior_IsPacifidlogVerticalLogBottom(newBehavior))
{
// Still on same one if moved from top to bottom
if (newY > oldY)
return FALSE;
}
else if (MetatileBehavior_IsPacifidlogHorizontalLogLeft(newBehavior))
{
// Still on same one if moved from right to left
if (newX < oldX)
return FALSE;
}
else if (MetatileBehavior_IsPacifidlogHorizontalLogRight(newBehavior))
{
// Still on same one if moved from left to right
if (newX > oldX)
return FALSE;
}
return TRUE;
}
#define tState data[1]
#define tPrevX data[2]
#define tPrevY data[3]
#define tToRaiseX data[4]
#define tToRaiseY data[5]
#define tDelay data[6]
static void PacifidlogBridgePerStepCallback(u8 taskId)
{
s16 *data;
s16 x, y;
data = gTasks[taskId].data;
PlayerGetDestCoords(&x, &y);
switch (tState)
{
case 0:
tPrevX = x;
tPrevY = y;
// If player is already standing on a log when the callback
// is set then immediately set it to submerged
TrySetLogBridgeFullySubmerged(x, y, TRUE);
tState = 1;
break;
case 1:
// Skip if player hasn't moved
if (x == tPrevX && y == tPrevY)
return;
if (ShouldRaisePacifidlogLogs(x, y, tPrevX, tPrevY))
{
// Player's previous position is not the other end of a log
// they're standing on, try and set it half-submerged (rising to surface).
// The floating metatile is queued up by setting it but not drawing it,
// but this is pointless as state 2 will handle it in full anyway.
TrySetLogBridgeHalfSubmerged(tPrevX, tPrevY, TRUE);
TrySetLogBridgeFloating(tPrevX, tPrevY, FALSE);
tToRaiseX = tPrevX;
tToRaiseY = tPrevY;
tState = 2;
tDelay = 8;
}
else
{
// Player has moved but is still on the same log bridge section.
// Keep it submerged.
tToRaiseX = -1;
tToRaiseY = -1;
}
if (ShouldSinkPacifidlogLogs(x, y, tPrevX, tPrevY))
{
// Player's current position is not the other end of a log
// they were previously standing on, try and set it half-submerged (sinking)
TrySetLogBridgeHalfSubmerged(x, y, TRUE);
tState = 2;
tDelay = 8;
}
tPrevX = x;
tPrevY = y;
// If player's new position is a log play the puddle SE
if (MetatileBehavior_IsPacifidlogLog(MapGridGetMetatileBehaviorAt(x, y)))
PlaySE(SE_PUDDLE);
break;
case 2:
if (--tDelay == 0)
{
// If player's current position is a log submerge it fully.
TrySetLogBridgeFullySubmerged(x, y, TRUE);
// Player's previous position is not the other end of a log
// they're standing on, try to raise their previous position.
if (tToRaiseX != -1 && tToRaiseY != -1)
TrySetLogBridgeFloating(tToRaiseX, tToRaiseY, TRUE);
tState = 1;
}
break;
}
}
#undef tState
#undef tPrevX
#undef tPrevY
#undef tToRaiseX
#undef tToRaiseY
#undef tDelay
static void TryLowerFortreeBridge(s16 x, s16 y)
{
u8 elevation = PlayerGetElevation();
if (!(elevation & 1))
{
switch (MapGridGetMetatileIdAt(x, y))
{
case METATILE_Fortree_BridgeOverGrass_Raised:
MapGridSetMetatileIdAt(x, y, METATILE_Fortree_BridgeOverGrass_Lowered);
break;
case METATILE_Fortree_BridgeOverTrees_Raised:
MapGridSetMetatileIdAt(x, y, METATILE_Fortree_BridgeOverTrees_Lowered);
break;
}
}
}
static void TryRaiseFortreeBridge(s16 x, s16 y)
{
u8 elevation = PlayerGetElevation();
if (!(elevation & 1))
{
switch (MapGridGetMetatileIdAt(x, y))
{
case METATILE_Fortree_BridgeOverGrass_Lowered:
MapGridSetMetatileIdAt(x, y, METATILE_Fortree_BridgeOverGrass_Raised);
break;
case METATILE_Fortree_BridgeOverTrees_Lowered:
MapGridSetMetatileIdAt(x, y, METATILE_Fortree_BridgeOverTrees_Raised);
break;
}
}
}
#define tState data[1]
#define tPrevX data[2]
#define tPrevY data[3]
#define tOldBridgeX data[4]
#define tOldBridgeY data[5]
#define tBounceTime data[6]
static void FortreeBridgePerStepCallback(u8 taskId)
{
bool8 isFortreeBridgeCur;
bool8 isFortreeBridgePrev;
u8 elevation, onBridgeElevation;
s16 x, y, prevX, prevY;
s16 *data = gTasks[taskId].data;
PlayerGetDestCoords(&x, &y);
switch (tState)
{
default:
break;
case 0:
tPrevX = x;
tPrevY = y;
// If player is already on bridge when callback is set then lower it immediately.
if (MetatileBehavior_IsFortreeBridge(MapGridGetMetatileBehaviorAt(x, y)))
{
TryLowerFortreeBridge(x, y);
CurrentMapDrawMetatileAt(x, y);
}
tState = 1;
break;
case 1:
prevX = tPrevX;
prevY = tPrevY;
// Skip if player hasn't moved
if (x == prevX && y == prevY)
break;
isFortreeBridgeCur = MetatileBehavior_IsFortreeBridge(MapGridGetMetatileBehaviorAt(x, y));
isFortreeBridgePrev = MetatileBehavior_IsFortreeBridge(MapGridGetMetatileBehaviorAt(prevX, prevY));
// Make sure player isn't below bridge
elevation = PlayerGetElevation();
onBridgeElevation = FALSE;
if ((u8)(elevation & 1) == 0)
onBridgeElevation = TRUE;
if (onBridgeElevation && (isFortreeBridgeCur == TRUE || isFortreeBridgePrev == TRUE))
PlaySE(SE_BRIDGE_WALK);
// Because this doesn't check for isFortreeBridgeCur, bridge sections aren't
// lowered when first stepping onto them from anything other than another bridge.
#ifdef BUGFIX
if (isFortreeBridgePrev || isFortreeBridgeCur)
#else
if (isFortreeBridgePrev)
#endif
{
// Raise old bridge
TryRaiseFortreeBridge(prevX, prevY);
CurrentMapDrawMetatileAt(prevX, prevY);
// Lower new bridge
TryLowerFortreeBridge(x, y);
CurrentMapDrawMetatileAt(x, y);
}
// These should really be set below the !isFortreeBridgePrev conditional,
// but it doesn't matter because it's not read until case 2 anyway.
tOldBridgeX = prevX;
tOldBridgeY = prevY;
tPrevX = x;
tPrevY = y;
if (!isFortreeBridgePrev)
break;
tBounceTime = 16;
tState = 2;
// fallthrough
case 2:
tBounceTime--;
prevX = tOldBridgeX;
prevY = tOldBridgeY;
switch (tBounceTime % 7)
{
case 0:
CurrentMapDrawMetatileAt(prevX, prevY);
case 1:
case 2:
case 3:
break;
case 4:
// Bounce bridge section that player has stepped off of
TryLowerFortreeBridge(prevX, prevY);
CurrentMapDrawMetatileAt(prevX, prevY);
TryRaiseFortreeBridge(prevX, prevY);
case 5:
case 6:
case 7: // Not possible with % 7
break;
}
if (tBounceTime == 0)
tState = 1;
break;
}
}
#undef tState
#undef tPrevX
#undef tPrevY
#undef tOldBridgeX
#undef tOldBridgeY
#undef tBounceTime
// Boundaries of the ice puzzle in Sootopolis Gym
#define ICE_PUZZLE_L 3
#define ICE_PUZZLE_R 13
#define ICE_PUZZLE_T 6
#define ICE_PUZZLE_B 19
#define ICE_PUZZLE_WIDTH (ICE_PUZZLE_R - ICE_PUZZLE_L + 1)
#define ICE_PUZZLE_HEIGHT (ICE_PUZZLE_B - ICE_PUZZLE_T + 1)
static bool32 CoordInIcePuzzleRegion(s16 x, s16 y)
{
if ((u16)(x - ICE_PUZZLE_L) < ICE_PUZZLE_WIDTH
&& (u16)(y - ICE_PUZZLE_T) < ICE_PUZZLE_HEIGHT
&& sSootopolisGymIceRowVars[y])
return TRUE;
else
return FALSE;
}
static void MarkIcePuzzleCoordVisited(s16 x, s16 y)
{
if (CoordInIcePuzzleRegion(x, y))
*GetVarPointer(sSootopolisGymIceRowVars[y]) |= (1 << (x - ICE_PUZZLE_L));
}
static bool32 IsIcePuzzleCoordVisited(s16 x, s16 y)
{
u16 var;
if (!CoordInIcePuzzleRegion(x, y))
return FALSE;
var = VarGet(sSootopolisGymIceRowVars[y]);
if (var &= (1 << (x - ICE_PUZZLE_L)))
return TRUE;
else
return FALSE;
}
void SetSootopolisGymCrackedIceMetatiles(void)
{
s32 x, y;
s32 width = gMapHeader.mapLayout->width;
s32 height = gMapHeader.mapLayout->height;
for (x = 0; x < width; x++)
{
for (y = 0; y < height; y++)
{
if (IsIcePuzzleCoordVisited(x, y) == TRUE)
MapGridSetMetatileIdAt(x + MAP_OFFSET, y + MAP_OFFSET, METATILE_SootopolisGym_Ice_Cracked);
}
}
}
#define tState data[1]
#define tPrevX data[2]
#define tPrevY data[3]
#define tIceX data[4]
#define tIceY data[5]
#define tDelay data[6]
static void SootopolisGymIcePerStepCallback(u8 taskId)
{
s16 x, y;
u16 tileBehavior;
u16 *iceStepCount;
s16 *data = gTasks[taskId].data;
switch (tState)
{
case 0:
PlayerGetDestCoords(&x, &y);
tPrevX = x;
tPrevY = y;
tState = 1;
break;
case 1:
PlayerGetDestCoords(&x, &y);
// End if player hasn't moved
if (x == tPrevX && y == tPrevY)
return;
tPrevX = x;
tPrevY = y;
tileBehavior = MapGridGetMetatileBehaviorAt(x, y);
iceStepCount = GetVarPointer(VAR_ICE_STEP_COUNT);
if (MetatileBehavior_IsThinIce(tileBehavior) == TRUE)
{
// Thin ice, set it to cracked ice
(*iceStepCount)++;
tDelay = 4;
tState = 2;
tIceX = x;
tIceY = y;
}
else if (MetatileBehavior_IsCrackedIce(tileBehavior) == TRUE)
{
// Cracked ice, set it to broken ice
*iceStepCount = 0;
tDelay = 4;
tState = 3;
tIceX = x;
tIceY = y;
}
break;
case 2:
if (tDelay != 0)
{
tDelay--;
}
else
{
// Crack ice
x = tIceX;
y = tIceY;
PlaySE(SE_ICE_CRACK);
MapGridSetMetatileIdAt(x, y, METATILE_SootopolisGym_Ice_Cracked);
CurrentMapDrawMetatileAt(x, y);
MarkIcePuzzleCoordVisited(x - MAP_OFFSET, y - MAP_OFFSET);
tState = 1;
}
break;
case 3:
if (tDelay != 0)
{
tDelay--;
}
else
{
// Break ice
x = tIceX;
y = tIceY;
PlaySE(SE_ICE_BREAK);
MapGridSetMetatileIdAt(x, y, METATILE_SootopolisGym_Ice_Broken);
CurrentMapDrawMetatileAt(x, y);
tState = 1;
}
break;
}
}
#undef tState
#undef tPrevX
#undef tPrevY
#undef tIceX
#undef tIceY
#undef tDelay
#define tPrevX data[1]
#define tPrevY data[2]
static void AshGrassPerStepCallback(u8 taskId)
{
s16 x, y;
u16 *ashGatherCount;
s16 *data = gTasks[taskId].data;
PlayerGetDestCoords(&x, &y);
// End if player hasn't moved
if (x == tPrevX && y == tPrevY)
return;
tPrevX = x;
tPrevY = y;
if (MetatileBehavior_IsAshGrass(MapGridGetMetatileBehaviorAt(x, y)))
{
// Remove ash from grass
if (MapGridGetMetatileIdAt(x, y) == METATILE_Fallarbor_AshGrass)
StartAshFieldEffect(x, y, METATILE_Fallarbor_NormalGrass, 4);
else
StartAshFieldEffect(x, y, METATILE_Lavaridge_NormalGrass, 4);
// Try to gather ash
if (CheckBagHasItem(ITEM_SOOT_SACK, 1))
{
ashGatherCount = GetVarPointer(VAR_ASH_GATHER_COUNT);
if (*ashGatherCount < 9999)
(*ashGatherCount)++;
}
}
}
#undef tPrevX
#undef tPrevY
// This function uses the constants for gTileset_Cave's metatile labels, but other tilesets with
// the CrackedFloorPerStepCallback callback use the same metatile numbers for the cracked floor
// and hole metatiles, such as gTileset_MirageTower.
static void SetCrackedFloorHoleMetatile(s16 x, s16 y)
{
u16 metatileId = MapGridGetMetatileIdAt(x, y) == METATILE_Cave_CrackedFloor ? METATILE_Cave_CrackedFloor_Hole : METATILE_Pacifidlog_SkyPillar_CrackedFloor_Hole;
MapGridSetMetatileIdAt(x, y, metatileId);
CurrentMapDrawMetatileAt(x, y);
}
#define tPrevX data[2]
#define tPrevY data[3]
#define tFloor1Delay data[4]
#define tFloor1X data[5]
#define tFloor1Y data[6]
#define tFloor2Delay data[7]
#define tFloor2X data[8]
#define tFloor2Y data[9]
static void CrackedFloorPerStepCallback(u8 taskId)
{
s16 x, y;
u16 behavior;
s16 *data = gTasks[taskId].data;
PlayerGetDestCoords(&x, &y);
behavior = MapGridGetMetatileBehaviorAt(x, y);
// Update up to 2 previous cracked floor spaces
if (tFloor1Delay != 0 && (--tFloor1Delay) == 0)
SetCrackedFloorHoleMetatile(tFloor1X, tFloor1Y);
if (tFloor2Delay != 0 && (--tFloor2Delay) == 0)
SetCrackedFloorHoleMetatile(tFloor2X, tFloor2Y);
if (MetatileBehavior_IsCrackedFloorHole(behavior))
VarSet(VAR_ICE_STEP_COUNT, 0); // this var does double duty
// End if player hasn't moved
if (x == tPrevX && y == tPrevY)
return;
tPrevX = x;
tPrevY = y;
if (MetatileBehavior_IsCrackedFloor(behavior))
{
if (GetPlayerSpeed() != PLAYER_SPEED_FASTEST)
VarSet(VAR_ICE_STEP_COUNT, 0); // this var does double duty
if (tFloor1Delay == 0)
{
tFloor1Delay = 3;
tFloor1X = x;
tFloor1Y = y;
}
else if (tFloor2Delay == 0)
{
tFloor2Delay = 3;
tFloor2X = x;
tFloor2Y = y;
}
}
}
#undef tPrevX
#undef tPrevY
#undef tFloor1Delay
#undef tFloor1X
#undef tFloor1Y
#undef tFloor2Delay
#undef tFloor2X
#undef tFloor2Y
#define tMapId data[0]
#define tState data[1]
#define tPrevX data[2]
#define tPrevY data[3]
// data[4] - data[15] are data about up to 4 individual animating muddy slope metatiles
// They're divided into groups of 3, i.e data[4]-[6] track one metatile, data[7]-[9] another, and so on.
// For each data triplet, the 1sst is the animation time, and the 2nd/3rd are the x/y coordinates.
#define SLOPE_DATA_START 4
#define SLOPE_DATA_END (3 * SLOPE_DATA_SIZE + SLOPE_DATA_START) // 13, which is the last slope data start point
enum {
SLOPE_TIME,
SLOPE_X,
SLOPE_Y,
SLOPE_DATA_SIZE
};
#define tSlopeAnimTime(i) data[(i) * SLOPE_DATA_SIZE + SLOPE_DATA_START + SLOPE_TIME]
static const u16 sMuddySlopeMetatiles[] = {
METATILE_General_MuddySlope_Frame0,
METATILE_General_MuddySlope_Frame3,
METATILE_General_MuddySlope_Frame2,
METATILE_General_MuddySlope_Frame1
};
#define SLOPE_ANIM_TIME 32
#define SLOPE_ANIM_STEP_TIME (SLOPE_ANIM_TIME / (int)ARRAY_COUNT(sMuddySlopeMetatiles))
static void SetMuddySlopeMetatile(s16 *data, s16 x, s16 y)
{
u16 metatileId;
if ((--data[SLOPE_TIME]) == 0)
metatileId = METATILE_General_MuddySlope_Frame0;
else
metatileId = sMuddySlopeMetatiles[data[SLOPE_TIME] / SLOPE_ANIM_STEP_TIME];
MapGridSetMetatileIdAt(x, y, metatileId);
CurrentMapDrawMetatileAt(x, y);
MapGridSetMetatileIdAt(x, y, METATILE_General_MuddySlope_Frame0);
}
static void Task_MuddySlope(u8 taskId)
{
s16 x, y, cameraOffsetX, cameraOffsetY;
int i;
u16 mapId;
s16 *data = gTasks[taskId].data;
PlayerGetDestCoords(&x, &y);
mapId = (gSaveBlock1Ptr->location.mapGroup << 8) | gSaveBlock1Ptr->location.mapNum;
switch (tState)
{
case 0:
tMapId = mapId;
tPrevX = x;
tPrevY = y;
tState = 1;
tSlopeAnimTime(0) = 0;
tSlopeAnimTime(1) = 0;
tSlopeAnimTime(2) = 0;
tSlopeAnimTime(3) = 0;
break;
case 1:
// Skip if player hasn't moved
if (tPrevX == x && tPrevY == y)
break;
tPrevX = x;
tPrevY = y;
if (MetatileBehavior_IsMuddySlope(MapGridGetMetatileBehaviorAt(x, y)))
{
for (i = SLOPE_DATA_START; i <= SLOPE_DATA_END; i += SLOPE_DATA_SIZE)
{
if (data[i] == 0)
{
data[i + SLOPE_TIME] = SLOPE_ANIM_TIME;
data[i + SLOPE_X] = x;
data[i + SLOPE_Y] = y;
break;
}
}
}
break;
}
if (gCamera.active && mapId != tMapId)
{
tMapId = mapId;
cameraOffsetX = gCamera.x;
cameraOffsetY = gCamera.y;
}
else
{
cameraOffsetX = 0;
cameraOffsetY = 0;
}
for (i = SLOPE_DATA_START; i <= SLOPE_DATA_END; i += SLOPE_DATA_SIZE)
{
if (data[i + SLOPE_TIME])
{
data[i + SLOPE_X] -= cameraOffsetX;
data[i + SLOPE_Y] -= cameraOffsetY;
SetMuddySlopeMetatile(&data[i + SLOPE_TIME], data[i + SLOPE_X], data[i + SLOPE_Y]);
}
}
}