brawllib_rs 0.24.1

Brawl character file parser, based on brawlbox/brawllib
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
use crate::fighter::WiiRDFrameSpeedModifier;
use crate::high_level_fighter;
use crate::high_level_fighter::{CollisionBoxValues, SectionScriptAst};
use crate::sakurai::fighter_data::ArcFighterData;
use crate::script::{Requirement, VariableDataType};
use crate::script_ast::variable_ast::{
    InternalConstantInt, LongtermAccessBool, LongtermAccessFloat, LongtermAccessInt,
    RandomAccessBool, RandomAccessFloat, RandomAccessInt, VariableAst,
};
use crate::script_ast::{
    ArmorType, BinaryExpression, Block, ComparisonOperator, DisableMovement, EdgeSlide, EventAst,
    Expression, FloatValue, GrabBoxArguments, HitBoxArguments, HurtBoxState, Interrupt, Iterations,
    LedgeGrabEnable, ScriptAst, SpecialHitBoxArguments, SpecifyThrow, ThrowUse,
};

use std::collections::HashMap;

pub struct ScriptRunner<'a> {
    pub subaction_name: String,
    pub wiird_frame_speed_modifiers: &'a [WiiRDFrameSpeedModifier],
    pub call_stacks: Vec<CallStack<'a>>,
    pub fighter_scripts: &'a [&'a ScriptAst],
    pub common_scripts: &'a [&'a ScriptAst],
    pub section_scripts: &'a [SectionScriptAst],
    pub call_every_frame: HashMap<i32, CallEveryFrame<'a>>,
    pub visited_gotos: Vec<i32>,
    pub subaction_index: usize,
    pub frame_index: f32,     // affected by frame speed modifiers
    pub animation_index: f32, // affected by frame speed modifiers, usually in sync with frame_index but not always because some commands affect only animation_index
    pub frame_count: usize, // goes up by exactly 1 every frame, only used for external statistics like iasa
    pub interruptible: bool,
    pub hitboxes: [Option<ScriptCollisionBox>; 7],
    pub hurtbox_state_all: HurtBoxState,
    pub hurtbox_states: HashMap<i32, HurtBoxState>,
    pub ledge_grab_enable: LedgeGrabEnable,
    pub frame_speed_modifier: f32,
    pub tag_display: bool,
    /// State is maintained across frames
    pub x: f32,
    /// State is maintained across frames
    pub y: f32,
    /// State is maintained across frames
    pub x_vel: f32,
    /// State is maintained across frames
    pub y_vel: f32,
    /// Reset to None before processing each frame
    pub x_vel_modify: VelModify,
    /// Reset to None before processing each frame
    pub y_vel_modify: VelModify,
    pub disable_movement: DisableMovement,
    pub armor_type: ArmorType,
    pub armor_tolerance: f32,
    pub damage: f32,
    pub airbourne: bool,
    pub edge_slide: EdgeSlide, // TODO: This value seems inaccurate as its rarely set, is ledge cancel normally just hardcoded for say movement vs attack
    pub reverse_direction: bool,
    pub change_subaction: ChangeSubaction,
    /// Children of these bones are also visible
    pub invisible_bones: Vec<i32>,
    /// Each Interrupt is rechecked every frame after being created
    pub interrupts: Vec<Interrupt>,
    /// As a hack to get some subactions working we store "bad interrupts" here instead.
    pub bad_interrupts: Vec<Interrupt>,
    pub hitbox_sets_rehit: [bool; 10],
    pub slope_contour_stand: Option<i32>,
    pub slope_contour_full: Option<(i32, i32)>,
    pub rumble: Option<(i32, i32)>,
    pub rumble_loop: Option<(i32, i32)>,
    pub grab_interrupt_damage: Option<i32>,
    pub throw: Option<SpecifyThrow>,
    /// Reset to false before processing each frame.
    pub throw_activate: bool,

    // LongtermAccessInt
    pub jumps_used: i32,
    pub wall_jump_count: i32,
    pub wall_jump_interval: i32,
    pub footstool_count: i32,
    pub fall_time: i32,
    pub swim_time: i32,
    pub lip_stick_refresh: i32,
    pub curry_remaining_time: i32,
    pub curry_angle2: i32,
    pub star_remaining_time: i32,
    pub mushroom_remaining_time: i32,
    pub lightning_remaining_time: i32,
    pub size_flag: i32,
    pub metal_block_remaining_time: i32,
    pub combo_count: i32,
    pub bubble_time: i32,
    pub attacks_performed: i32,
    pub costume_id: i32,
    pub hitstun_frames_remaining: i32,
    pub meteor_cancel_window: i32,
    pub missed_techs: i32,
    pub tether_count: i32,
    pub temp1: i32,
    pub temp2: i32,
    pub longterm_access_int: Vec<i32>,

    // LongtermAccessFloat
    pub special_landing_lag: f32,
    pub special_fall_mobility_multiplier: f32,
    pub shield_charge: f32,
    pub curry_angle1: f32,
    pub curry_randomness: f32,
    pub longterm_access_float: Vec<f32>,

    // LongtermAccessBool
    pub is_dead: bool,
    pub cannot_die: bool,
    pub automatic_footstool: bool,
    pub has_final: bool,
    pub has_final_aura: bool,
    pub has_curry: bool,
    pub has_hammer: bool,
    pub hit_by_paralyze: bool,
    pub has_screw_attack: bool,
    pub stamina_dead: bool,
    pub has_tag: bool,
    pub can_not_ledge_grab: bool,
    pub can_not_teeter: bool,
    pub velocity_ignore_hitstun: bool,
    pub deflection: bool,
    pub longterm_access_bool: Vec<bool>,

    // RandomAccessInt
    pub throw_data_param1: i32,
    pub throw_data_param2: i32,
    pub throw_data_param3: i32,
    pub random_access_int: Vec<i32>,

    // RandomAccessFloat
    pub enable_turn_when_below_zero: f32,
    pub random_access_float: Vec<f32>,

    // RandomAccessBool
    pub character_float: bool,
    pub enable_fast_fall: bool,
    pub shorthop: bool,
    pub enable_action_transition: bool,
    pub specials_movement: bool,
    pub enable_glide: bool,
    pub enable_jab_loop: bool,
    pub enable_auto_jab: bool,
    pub enable_jab_end: bool,
    pub landing_lag: bool,
    pub random_access_bool: Vec<bool>,
}

pub struct CallStack<'a> {
    pub calls: Vec<Call<'a>>,
    pub wait_until: f32,
}

pub struct Call<'a> {
    pub block: &'a Block,
    pub else_branch: Option<&'a Block>,
    pub index: usize,
    pub subroutine: bool,
    pub external: bool,
    /// I have tested the `And` and `Or` events to have no effect outside of any if statement
    pub if_statement: bool,
    pub execute: bool,
}

pub enum ChangeSubaction {
    Continue,
    InfiniteLoop,
    ChangeSubaction(i32),
    ChangeSubactionRestartFrame(i32),
    // TODO: Because we currently only operate at the subaction level, this is the best we can do.
    Interrupt(i32),
}

#[derive(Clone, Debug)]
pub struct ScriptCollisionBox {
    pub bone_index: i16,
    pub hitbox_id: u8,
    pub x_offset: f32,
    pub y_offset: f32,
    pub z_offset: f32,
    pub size: f32,
    pub values: CollisionBoxValues,
    pub interpolate: bool,
}

impl ScriptCollisionBox {
    fn from_hitbox(args: &HitBoxArguments, interpolate: bool, damage: f32) -> ScriptCollisionBox {
        ScriptCollisionBox {
            bone_index: args.bone_index,
            hitbox_id: args.hitbox_id,
            x_offset: args.x_offset,
            y_offset: args.y_offset,
            z_offset: args.z_offset,
            size: args.size,
            values: CollisionBoxValues::from_hitbox(args, damage),
            interpolate,
        }
    }

    fn from_special_hitbox(
        args: &SpecialHitBoxArguments,
        interpolate: bool,
        damage: f32,
    ) -> ScriptCollisionBox {
        ScriptCollisionBox {
            bone_index: args.hitbox_args.bone_index,
            hitbox_id: args.hitbox_args.hitbox_id,
            x_offset: args.hitbox_args.x_offset,
            y_offset: args.hitbox_args.y_offset,
            z_offset: args.hitbox_args.z_offset,
            size: args.hitbox_args.size,
            values: CollisionBoxValues::from_special_hitbox(args, damage),
            interpolate,
        }
    }

    fn from_grabbox(args: &GrabBoxArguments, interpolate: bool) -> ScriptCollisionBox {
        ScriptCollisionBox {
            bone_index: args.bone_index as i16,
            hitbox_id: args.hitbox_id as u8,
            x_offset: args.x_offset,
            y_offset: args.y_offset,
            z_offset: args.z_offset,
            size: args.size,
            values: CollisionBoxValues::from_grabbox(args),
            interpolate,
        }
    }

    fn is_hit(&self) -> bool {
        match self.values {
            CollisionBoxValues::Hit(_) => true,
            CollisionBoxValues::Grab(_) => false,
        }
    }

    fn is_grab(&self) -> bool {
        match self.values {
            CollisionBoxValues::Hit(_) => false,
            CollisionBoxValues::Grab(_) => true,
        }
    }
}

impl<'a> ScriptRunner<'a> {
    /// Runs the action main, gfx, sfx and other scripts in subaction_scripts.
    /// all_scripts contains any functions that the action scripts need to call into.
    /// The returned runner has completed the first frame.
    /// Calling `runner.step` will advance to frame 2 and then frame 3 and so on.
    pub fn new(
        subaction_index: usize,
        wiird_frame_speed_modifiers: &'a [WiiRDFrameSpeedModifier],
        subaction_scripts: &[&'a ScriptAst],
        fighter_scripts: &'a [&'a ScriptAst],
        common_scripts: &'a [&'a ScriptAst],
        section_scripts: &'a [SectionScriptAst],
        init_hack_script: &Block,
        fighter_data: &ArcFighterData,
        subaction_name: String,
    ) -> ScriptRunner<'a> {
        let mut call_stacks = vec![];
        for script in subaction_scripts {
            let calls = vec![Call {
                block: &script.block,
                else_branch: None,
                index: 0,
                subroutine: false,
                external: false,
                if_statement: false,
                execute: true,
            }];
            call_stacks.push(CallStack {
                calls,
                wait_until: -1.0,
            });
        }

        let ledge_grab_enable = match subaction_name.as_ref() {
            "JumpF" | "JumpB" | "JumpAerialF" | "JumpAerialB" | "FallF" | "FallB" | "Fall"
            | "FallAerialF" | "FallAerialB" | "FallAerial" | "FallSpecialF" | "FallSpecialB"
            | "FallSpecial" => LedgeGrabEnable::EnableInFront,
            _ => LedgeGrabEnable::Disable,
        };

        // TODO: Need to understand what ModelVisibility.cs:269 is doing (ResetVisibility)
        //
        // Actually:
        // I'm pretty sure it doesnt affect children bones, so everything makes sense now.
        // Actually x2:
        // Why is bone 0 used then, its got nothing to render :O
        // ABORT ABORT
        //
        // I think I want a tree.
        // *   It lets me handle the initial values from the flags
        // *   It lets me easily set all children
        // *   It lets me easily set and reset values

        let mut invisible_bones = vec![];
        // TODO: populate with bone flags
        // This isnt actually part of the visibility reset that occurs at the start of a subaction
        // and should be only called during initialization if such a refactor occurs.

        for reference in &fighter_data.model_visibility.references {
            for default in &fighter_data.model_visibility.defaults {
                if let Some(bone_switch) =
                    reference.bone_switches.get(default.switch_index as usize)
                {
                    if let Some(group) = bone_switch.groups.get(default.group_index as usize) {
                        for bone in &group.bones {
                            invisible_bones.push(*bone);
                        }
                    }
                }
            }
        }
        ////First, disable bones
        //foreach (ModelVisBoneSwitch Switch in entry)
        //{
        //    int i = 0;
        //    foreach (ModelVisGroup Group in Switch)
        //    {
        //        if (i != Switch._defaultGroup)
        //            foreach (BoneIndexValue b in Group._bones)
        //                if (b.BoneNode != null)
        //                    foreach (DrawCall p in b.BoneNode._visDrawCalls)
        //                        p._render = false;
        //        i++;
        //    }
        //}

        // TODO: enable bones.
        // This doesnt actually affect anything at the moment.
        // The two cases where this could affect things in the future are:
        // *   ScriptRunner is extended to run at the action level, in which case new subactions would cause the bones to reset after being potentially modified
        // *   invisible_bones is populated with the visible bone flags from the MDL0 bone data.
        for reference in &fighter_data.model_visibility.references {
            for default in &fighter_data.model_visibility.defaults {
                if let Some(bone_switch) =
                    reference.bone_switches.get(default.switch_index as usize)
                {
                    if let Some(group) = bone_switch.groups.get(default.group_index as usize) {
                        for bone in &group.bones {
                            invisible_bones.retain(|x| x != bone);
                        }
                    }
                }
            }
        }

        ////Now, enable bones
        //foreach (ModelVisBoneSwitch Switch in entry)
        //    if (Switch._defaultGroup >= 0 && Switch._defaultGroup < Switch.Count)
        //    {
        //        ModelVisGroup Group = Switch[Switch._defaultGroup];
        //        foreach (BoneIndexValue b in Group._bones)
        //            if (b.BoneNode != null)
        //                foreach (DrawCall p in b.BoneNode._visDrawCalls)
        //                    p._render = true;
        //    }
        //
        //
        //
        //    FOR THE EVENT:
        //public void ApplyVisibility(int refId, int switchID, int groupID)
        //{
        //    if (refId < 0 || refId >= _references.Count)
        //        return;

        //    //Get the target reference
        //    ModelVisReference refEntry = _references[refId];

        //    //Check if the reference and switch id is usable
        //    if (switchID >= refEntry.Count || switchID < 0)
        //        return;

        //    //Turn off objects
        //    ModelVisBoneSwitch switchEntry = refEntry[switchID];
        //    foreach (ModelVisGroup grp in switchEntry)
        //        foreach (BoneIndexValue b in grp._bones)
        //            if (b.BoneNode != null)
        //                foreach (DrawCall obj in b.BoneNode._visDrawCalls)
        //                    obj._render = false;

        //    //Check if the group id is usable
        //    if (groupID >= switchEntry.Count || groupID < 0)
        //        return;

        //    //Turn on objects
        //    ModelVisGroup group = switchEntry[groupID];
        //    if (group != null)
        //        foreach (BoneIndexValue b in group._bones)
        //            if (b.BoneNode != null)
        //                foreach (DrawCall obj in b.BoneNode._visDrawCalls)
        //                    obj._render = true;
        //}

        let mut runner = ScriptRunner {
            subaction_name,
            wiird_frame_speed_modifiers,
            call_stacks,
            fighter_scripts,
            common_scripts,
            section_scripts,
            subaction_index,
            ledge_grab_enable,
            call_every_frame: HashMap::new(),
            visited_gotos: vec![],
            frame_index: 0.0,
            animation_index: 0.0,
            frame_count: 0,
            interruptible: false,
            hitboxes: [None, None, None, None, None, None, None],
            hurtbox_state_all: HurtBoxState::Normal,
            hurtbox_states: HashMap::new(),
            frame_speed_modifier: 1.0,
            tag_display: true,
            x: 0.0,
            y: 0.0,
            x_vel: 0.0,
            y_vel: 0.0,
            x_vel_modify: VelModify::None,
            y_vel_modify: VelModify::None,
            disable_movement: DisableMovement::Enable,
            armor_type: ArmorType::None,
            armor_tolerance: 0.0,
            damage: 0.0,
            airbourne: false,
            edge_slide: EdgeSlide::SlideOff,
            reverse_direction: false,
            change_subaction: ChangeSubaction::Continue,
            interrupts: vec![],
            bad_interrupts: vec![],
            hitbox_sets_rehit: [false; 10],
            slope_contour_stand: None,
            slope_contour_full: None,
            rumble: None,
            rumble_loop: None,
            grab_interrupt_damage: None,
            throw: None,
            throw_activate: false,
            invisible_bones,

            // LongtermAccessInt
            jumps_used: 0,
            wall_jump_count: 0,
            wall_jump_interval: 0,
            footstool_count: 0,
            fall_time: 0,
            swim_time: 0,
            lip_stick_refresh: 0,
            curry_remaining_time: 0,
            curry_angle2: 0,
            star_remaining_time: 0,
            mushroom_remaining_time: 0,
            lightning_remaining_time: 0,
            size_flag: 0,
            metal_block_remaining_time: 0,
            combo_count: 0,
            bubble_time: 0,
            attacks_performed: 0,
            costume_id: 0,
            hitstun_frames_remaining: 0,
            meteor_cancel_window: 0,
            missed_techs: 0,
            tether_count: 0,
            temp1: 0,
            temp2: 0,
            longterm_access_int: vec![0; 0x500], // TODO: How big should this be?

            // LongtermAccessFloat
            special_landing_lag: 0.0,
            special_fall_mobility_multiplier: 0.0,
            shield_charge: 0.0,
            curry_angle1: 0.0,
            curry_randomness: 0.0,
            longterm_access_float: vec![0.0; 0x500], // TODO: How big should this be?

            // LongtermAccessBool
            is_dead: false,
            cannot_die: false,
            automatic_footstool: false,
            has_final: false,
            has_final_aura: false,
            has_curry: false,
            has_hammer: false,
            hit_by_paralyze: false,
            has_screw_attack: false,
            stamina_dead: false,
            has_tag: false,
            can_not_ledge_grab: false,
            can_not_teeter: false,
            velocity_ignore_hitstun: false,
            deflection: false,
            longterm_access_bool: vec![false; 0x500], // TODO: How big should this be?

            // RandomAccessInt
            throw_data_param1: 0,
            throw_data_param2: 0,
            throw_data_param3: 0,
            random_access_int: vec![0; 0x100], // Pika has 72 bytes allocated, so this should be plenty.

            // RandomAccessFloat
            enable_turn_when_below_zero: 0.0,
            random_access_float: vec![0.0; 0x100], // Pika has 56 bytes allocated, so this should be plenty.

            // RandomAccessBool
            character_float: false,
            enable_fast_fall: false,
            shorthop: false,
            enable_action_transition: false,
            specials_movement: false,
            enable_glide: false,
            enable_jab_loop: false,
            enable_auto_jab: false,
            enable_jab_end: false,
            landing_lag: false,
            random_access_bool: vec![false; 0x100], // normally has 8-16 bytes allocated
        };

        for event in &init_hack_script.events {
            runner.step_event(event, false, &[], &[], &[]);
        }

        // Need to run the script until the first wait, so that the script is in the valid state
        // for the first frame.
        runner.step_script();

        runner
    }

    /// Steps the main, gfx, sfx and other scripts by 1 game frame.
    pub fn step(&mut self) {
        let mut fsms = vec![];
        for fsm in self.wiird_frame_speed_modifiers {
            // TODO: Because we currently only operate at the subaction level, this is the best we can do.
            if !fsm.action
                && fsm.action_subaction_id as usize == self.subaction_index
                && (self.frame_index as u16) >= fsm.frame as u16
            {
                fsms.push(fsm);
            }
        }

        fsms.sort_by_key(|x| x.frame);
        if let Some(fsm) = fsms.last() {
            self.frame_speed_modifier = fsm.frame_speed;
        }
        self.frame_index += self.frame_speed_modifier;
        self.animation_index += self.frame_speed_modifier;
        self.frame_count += 1;
        self.step_script();
    }

    fn step_script(&mut self) {
        for rehit in self.hitbox_sets_rehit.iter_mut() {
            *rehit = false;
        }
        self.throw_activate = false;
        self.rumble = None; // TODO: I guess rumble_loop shouldnt be reset?
        self.visited_gotos.clear();
        self.x_vel_modify = VelModify::None;
        self.y_vel_modify = VelModify::None;
        self.reverse_direction = false;

        // The hitbox existed last frame so should be interpolated.
        // (Unless it gets overwritten, but that will be handled when that happens)
        for hitbox in &mut self.hitboxes.iter_mut().flatten() {
            hitbox.interpolate = true;
        }

        for interrupt in self.interrupts.clone() {
            if self.evaluate_expression(&interrupt.test).unwrap_bool() {
                // TODO: Because we currently only operate at the subaction level, this is the best we can do.
                self.change_subaction = ChangeSubaction::Interrupt(interrupt.action);
            }
        }

        // create a callstack for CallEveryFrame block
        for call_every_frame in self.call_every_frame.values() {
            let calls = vec![Call {
                block: call_every_frame.block,
                else_branch: None,
                index: 0,
                subroutine: false,
                external: call_every_frame.external,
                if_statement: false,
                execute: true,
            }];
            self.call_stacks.push(CallStack {
                calls,
                wait_until: -1.0,
            });
        }

        // run the main, gfx, sfx and other scripts
        for i in 0..self.call_stacks.len() {
            while !self.call_stacks[i].calls.is_empty() {
                // reached the end of the script
                // Handle wait events
                if self.frame_index < self.call_stacks[i].wait_until {
                    break;
                }

                // Process the next event in the call_stack
                let call = self.call_stacks[i].calls.last().unwrap();
                if let Some(event) = call.block.events.get(call.index) {
                    self.call_stacks[i].calls.last_mut().unwrap().index += 1;
                    let external = self.call_stacks[i].calls.last().unwrap().external;

                    if self.call_stacks[i].calls.last().unwrap().execute {
                        match self.step_event(
                            event,
                            external,
                            self.fighter_scripts,
                            self.common_scripts,
                            self.section_scripts,
                        ) {
                            StepEventResult::WaitUntil(value) => {
                                self.call_stacks[i].wait_until = value;
                            }
                            StepEventResult::NewForLoop { block, iterations } => {
                                for _ in 0..iterations {
                                    self.call_stacks[i].calls.push(Call {
                                        block,
                                        else_branch: None,
                                        index: 0,
                                        subroutine: false,
                                        if_statement: false,
                                        execute: true,
                                        external,
                                    });
                                }
                            }
                            StepEventResult::NewCall { block } => {
                                self.call_stacks[i].calls.push(Call {
                                    block,
                                    else_branch: None,
                                    index: 0,
                                    subroutine: false,
                                    if_statement: false,
                                    execute: true,
                                    external,
                                });
                            }
                            StepEventResult::NewIfStatement {
                                then_branch,
                                else_branch,
                                execute,
                            } => {
                                self.call_stacks[i].calls.push(Call {
                                    block: then_branch,
                                    else_branch,
                                    index: 0,
                                    subroutine: false,
                                    if_statement: true,
                                    execute,
                                    external,
                                });
                            }
                            StepEventResult::IfStatementDisableExecution => {
                                if self.call_stacks[i].calls.last().unwrap().if_statement {
                                    self.call_stacks[i].calls.last_mut().unwrap().execute = false;
                                }
                            }
                            StepEventResult::Subroutine { block, external } => {
                                self.call_stacks[i].calls.push(Call {
                                    block,
                                    else_branch: None,
                                    index: 0,
                                    subroutine: true,
                                    if_statement: false,
                                    execute: true,
                                    external,
                                });
                            }
                            StepEventResult::CallEveryFrame {
                                block,
                                thread_id,
                                external,
                            } => {
                                self.call_every_frame
                                    .insert(thread_id, CallEveryFrame { block, external });
                            }
                            StepEventResult::Return => {
                                let mut run = false;
                                while run {
                                    run = self.call_stacks[i]
                                        .calls
                                        .pop()
                                        .map(|x| !x.subroutine)
                                        .unwrap_or(false);
                                }
                            }
                            StepEventResult::Goto { block, external } => {
                                self.call_stacks[i].calls.pop();
                                self.call_stacks[i].calls.push(Call {
                                    block,
                                    else_branch: None,
                                    index: 0,
                                    subroutine: false,
                                    if_statement: false,
                                    execute: true,
                                    external,
                                });
                            }
                            StepEventResult::None => {}
                        }
                    } else {
                        // when execution is disabled we run events that may resume execution, otherwise we do nothing
                        let new_execution = match event {
                            EventAst::IfStatementOr(test) => {
                                self.evaluate_expression(test).unwrap_bool()
                                    && self.call_stacks[i].calls.last().unwrap().if_statement
                            }
                            _ => false,
                        };
                        self.call_stacks[i].calls.last_mut().unwrap().execute = new_execution;
                    }
                } else {
                    // If there is an else branch, begin processing that as the main block, otherwise pop the call
                    let call = self.call_stacks[i].calls.last_mut().unwrap();
                    if let Some(else_branch) = call.else_branch {
                        call.block = else_branch;
                        call.index = 0;
                        call.else_branch = None;
                        call.execute = !call.execute;
                    } else {
                        self.call_stacks[i].calls.pop();
                    }
                }
            }
        }

        // CallEveryFrame call stacks only get one frame to complete so remove them now.
        self.call_stacks.truncate(4); // keep main, gfx, sfx and other call stacks

        if self.frame_speed_modifier == 0.0 {
            self.change_subaction = ChangeSubaction::InfiniteLoop
        }

        match self.disable_movement {
            DisableMovement::Enable => {
                self.x += self.x_vel;
                self.y += self.y_vel;
            }
            DisableMovement::DisableVertical => {
                self.x += self.x_vel;
            }
            DisableMovement::DisableHorizontal => {
                self.y += self.y_vel;
            }
            _ => error!("Unknown DisableMovement value"),
        }
    }

    /// Returns the wait_until value
    fn step_event<'b>(
        &mut self,
        event: &'b EventAst,
        external: bool,
        fighter_scripts: &[&'b ScriptAst],
        common_scripts: &[&'b ScriptAst],
        section_scripts: &'b [SectionScriptAst],
    ) -> StepEventResult<'b> {
        match event {
            EventAst::SyncWait(value) => {
                return StepEventResult::WaitUntil(self.frame_index + *value);
            }
            EventAst::AsyncWait(value) => {
                return StepEventResult::WaitUntil(*value);
            }
            EventAst::ForLoop(for_loop) => {
                match for_loop.iterations {
                    Iterations::Finite(iterations) => {
                        return StepEventResult::NewForLoop {
                            block: &for_loop.block,
                            iterations,
                        };
                    }
                    Iterations::Infinite => {
                        // obviously an infinite loop should not be attempted :P
                        return StepEventResult::NewCall {
                            block: &for_loop.block,
                        };
                    }
                }
            }
            EventAst::Subroutine(offset) => {
                if !external {
                    if let Some(script) = section_scripts
                        .iter()
                        .find(|x| x.callers.contains(&offset.origin))
                    {
                        return StepEventResult::Subroutine {
                            block: &script.script.block,
                            external: true,
                        };
                    }
                }

                let all_scripts = if external {
                    common_scripts
                } else {
                    fighter_scripts
                };
                // TODO: Maybe I should implement a protection similar to visited_gotos for subroutines.
                // If that turns out to be a bad idea document why.
                for script in all_scripts.iter() {
                    if script.offset == offset.offset {
                        if !script.block.events.is_empty()
                            && std::ptr::eq(&script.block.events[0], event)
                        {
                            error!("Avoided hard Subroutine infinite loop (attempted to jump to the same location)");
                        } else {
                            return StepEventResult::Subroutine {
                                block: &script.block,
                                external,
                            };
                        }
                    }
                }
                error!("Couldnt find Subroutine offset");
            }
            EventAst::Return => {
                return StepEventResult::Return;
            }
            EventAst::Goto(offset) => {
                if !external {
                    if let Some(script) = section_scripts
                        .iter()
                        .find(|x| x.callers.contains(&offset.origin))
                    {
                        return StepEventResult::Goto {
                            block: &script.script.block,
                            external: true,
                        };
                    }
                }

                let all_scripts = if external {
                    common_scripts
                } else {
                    fighter_scripts
                };
                if !self.visited_gotos.iter().any(|x| *x == offset.offset) {
                    self.visited_gotos.push(offset.offset);
                    for script in all_scripts.iter() {
                        if script.offset == offset.offset {
                            return StepEventResult::Goto {
                                block: &script.block,
                                external,
                            };
                        }
                    }
                    error!("Couldnt find Goto offset");
                }
                error!("Avoided Goto infinite loop");
            }
            EventAst::IfStatement(if_statement) => {
                let then_branch = &if_statement.then_branch;
                let else_branch = if_statement.else_branch.as_deref();
                let execute = self.evaluate_expression(&if_statement.test).unwrap_bool();
                return StepEventResult::NewIfStatement {
                    then_branch,
                    else_branch,
                    execute,
                };
            }
            EventAst::IfStatementAnd(test) => {
                if !self.evaluate_expression(test).unwrap_bool() {
                    return StepEventResult::IfStatementDisableExecution;
                }
            }
            EventAst::IfStatementOr(_) => {} // This is handled in the !execution branch
            EventAst::Switch(_, _) => {}     // TODO
            EventAst::EndSwitch => {}
            EventAst::Case(_) => {}
            EventAst::DefaultCase => {}
            EventAst::LoopRest => {
                error!("LoopRest: This means the code is expected to infinite loop")
            } // TODO: Handle infinite loops better
            EventAst::CallEveryFrame { thread_id, offset } => {
                if !external {
                    if let Some(script) = section_scripts
                        .iter()
                        .find(|x| x.callers.contains(&offset.origin))
                    {
                        return StepEventResult::CallEveryFrame {
                            thread_id: *thread_id,
                            block: &script.script.block,
                            external: true,
                        };
                    }
                }

                let all_scripts = if external {
                    common_scripts
                } else {
                    fighter_scripts
                };
                for script in all_scripts.iter() {
                    if script.offset == offset.offset {
                        return StepEventResult::CallEveryFrame {
                            thread_id: *thread_id,
                            block: &script.block,
                            external,
                        };
                    }
                }
            }
            EventAst::RemoveCallEveryFrame { thread_id } => {
                self.call_every_frame.remove(thread_id);
            }
            EventAst::IndependentSubroutine { .. } => {} // TODO
            EventAst::RemoveIndependentSubroutine { .. } => {} // TODO
            EventAst::SetIndependentSubroutineThreadType { .. } => {} // TODO
            EventAst::DisableInterrupt(_) => {}          // TODO
            EventAst::EnableInterrupt(_) => {}           // TODO
            EventAst::ToggleInterrupt { .. } => {}       // TODO
            EventAst::EnableInterruptGroup(_) => {}      // TODO
            EventAst::DisableInterruptGroup(_) => {}     // TODO
            EventAst::ClearInterruptGroup(_) => {}       // TODO
            EventAst::CreateInterrupt(interrupt) => {
                // If the interrupt would succeed on the first frame then ignore it.
                // This is a super hacky hack to get moves like DK's dash attack working.
                if !(self.frame_index == 0.0
                    && self.evaluate_expression(&interrupt.test).unwrap_bool())
                {
                    self.interrupts.push(interrupt.clone());
                } else {
                    self.bad_interrupts.push(interrupt.clone());
                }
            }
            EventAst::PreviousInterruptAddRequirement { test } => {
                if let Some(interrupt) = self.interrupts.last_mut() {
                    let left = Box::new(interrupt.test.clone());
                    let right = Box::new(test.clone());
                    let operator = ComparisonOperator::And;
                    interrupt.test = Expression::Binary(BinaryExpression {
                        left,
                        operator,
                        right,
                    });
                }
            }
            EventAst::InterruptAddRequirement { .. } => {} // TODO
            EventAst::AllowInterrupts => {
                self.interruptible = true;
            }
            EventAst::DisallowInterrupts => {
                self.interruptible = false;
            }
            EventAst::ChangeSubaction(v0) => {
                self.change_subaction = ChangeSubaction::ChangeSubaction(*v0);
            }
            EventAst::ChangeSubactionRestartFrame(v0) => {
                self.change_subaction = ChangeSubaction::ChangeSubactionRestartFrame(*v0);
            }

            // timing
            EventAst::SetAnimationFrame(v0) => {
                self.animation_index = *v0;
            }
            EventAst::SetAnimationAndTimerFrame(v0) => {
                self.animation_index = *v0;
                self.frame_index = *v0;
            }
            EventAst::FrameSpeedModifier { multiplier, .. } => {
                self.frame_speed_modifier = *multiplier;
            }
            EventAst::TimeManipulation(_, _) => {}

            // misc state
            EventAst::SetAirGround(v0) => {
                self.airbourne = *v0 == 0; // TODO: Seems like brawlbox is incomplete here e.g 36
            }
            EventAst::SetEdgeSlide(v0) => {
                self.edge_slide = v0.clone();
            }
            EventAst::ReverseDirection => {
                self.reverse_direction = !self.reverse_direction;
            }

            // hitboxes
            EventAst::CreateHitBox(args) => {
                if args.hitbox_id < self.hitboxes.len() as u8 {
                    // Need to check this here, as hitboxes can be deleted in the current frame but still exist in the previous frame.
                    // In this case interpolation should not occur.
                    let interpolate =
                        if let Some(prev_hitbox) = &self.hitboxes[args.hitbox_id as usize] {
                            match &prev_hitbox.values {
                                CollisionBoxValues::Hit(prev_hitbox) => {
                                    args.set_id == prev_hitbox.set_id
                                }
                                _ => false,
                            }
                        } else {
                            false
                        };

                    // Force rehit if no existing hitboxes.
                    // Both DeleteAllHitboxes and DeleteHitbox on the last hitbox will trigger rehit.
                    // http://localhost:8000/PM3.6/Squirtle/subactions/SpecialHi.html?frame=11
                    let mut empty_set = true;
                    for hitbox in self.hitboxes.iter().filter_map(|x| x.clone()) {
                        if let CollisionBoxValues::Hit(hitbox) = hitbox.values {
                            if hitbox.set_id == args.set_id {
                                empty_set = false;
                            }
                        }
                    }
                    if empty_set {
                        if let Some(rehit) = self.hitbox_sets_rehit.get_mut(args.set_id as usize) {
                            *rehit = true;
                        }
                    }

                    let damage = self.get_float_value(&args.damage);
                    self.hitboxes[args.hitbox_id as usize] =
                        Some(ScriptCollisionBox::from_hitbox(args, interpolate, damage));
                } else {
                    error!(
                        "invalid hitbox index {} {}",
                        args.hitbox_id, self.subaction_name
                    );
                }
            }
            EventAst::ThrownHitBox(_) => {}
            EventAst::CreateSpecialHitBox(args) => {
                if args.hitbox_args.hitbox_id < self.hitboxes.len() as u8 {
                    // Need to check this here, as hitboxes can be deleted in the current frame but still exist in the previous frame.
                    // In this case interpolation should not occur.
                    let index = args.hitbox_args.hitbox_id as usize;
                    let interpolate = if let Some(prev_hitbox) = &self.hitboxes[index] {
                        match &prev_hitbox.values {
                            CollisionBoxValues::Hit(prev_hitbox) => {
                                args.hitbox_args.set_id == prev_hitbox.set_id
                            }
                            _ => false,
                        }
                    } else {
                        false
                    };

                    // Force rehit if no existing hitboxes.
                    // Both DeleteAllHitboxes and DeleteHitbox on the last hitbox will trigger rehit.
                    // http://localhost:8000/PM3.6/Squirtle/subactions/SpecialHi.html
                    let mut empty_set = true;
                    for hitbox in self.hitboxes.iter().filter_map(|x| x.clone()) {
                        if let CollisionBoxValues::Hit(hitbox) = hitbox.values {
                            if hitbox.set_id == args.hitbox_args.set_id {
                                empty_set = false;
                            }
                        }
                    }
                    if empty_set {
                        if let Some(rehit) = self
                            .hitbox_sets_rehit
                            .get_mut(args.hitbox_args.set_id as usize)
                        {
                            *rehit = true;
                        }
                    }

                    let damage = self.get_float_value(&args.hitbox_args.damage);
                    self.hitboxes[index] = Some(ScriptCollisionBox::from_special_hitbox(
                        args,
                        interpolate,
                        damage,
                    ));
                } else {
                    error!(
                        "invalid hitbox index {} {}",
                        args.hitbox_args.hitbox_id, self.subaction_name
                    );
                }
            }
            EventAst::DeleteAllHitBoxes => {
                for hitbox in self.hitboxes.iter_mut() {
                    if hitbox.as_ref().map(|x| x.is_hit()).unwrap_or(false) {
                        *hitbox = None;
                    }
                }
            }
            EventAst::DefensiveCollision { .. } => {}
            EventAst::MoveHitBox(move_hitbox) => {
                if let Some(hitbox) = &mut self.hitboxes[move_hitbox.hitbox_id as usize] {
                    hitbox.bone_index = move_hitbox.new_bone as i16;
                    hitbox.x_offset = move_hitbox.new_x_offset;
                    hitbox.y_offset = move_hitbox.new_y_offset;
                    hitbox.z_offset = move_hitbox.new_z_offset;
                }
            }
            EventAst::ChangeHitBoxDamage {
                hitbox_id,
                new_damage,
            } => {
                if let Some(hitbox) = &mut self.hitboxes[*hitbox_id as usize] {
                    if let CollisionBoxValues::Hit(hitbox) = &mut hitbox.values {
                        hitbox.damage = *new_damage as f32;
                    }
                }
            }
            EventAst::ChangeHitBoxSize {
                hitbox_id,
                new_size,
            } => {
                if let Some(hitbox) = &mut self.hitboxes[*hitbox_id as usize] {
                    if let CollisionBoxValues::Hit(hitbox) = &mut hitbox.values {
                        hitbox.size = *new_size as f32;
                    }
                }
            }
            EventAst::DeleteHitBox(hitbox_id) => {
                // Shock claims this doesnt work on special hitboxes but it seems to work fine here:
                // http://localhost:8000/PM3.6/Squirtle/subactions/SpecialHi.html
                if self.hitboxes[*hitbox_id as usize]
                    .as_ref()
                    .map(|x| x.is_hit())
                    .unwrap_or(false)
                {
                    self.hitboxes[*hitbox_id as usize] = None;
                }
            }
            EventAst::CreateGrabBox(args) => {
                let mut interpolate = false;
                if let Some(prev_hitbox) = &self.hitboxes[args.hitbox_id as usize] {
                    // TODO: Should a grabbox interpolate from an existing hitbox and vice versa
                    if let CollisionBoxValues::Grab(_) = prev_hitbox.values {
                        interpolate = true;
                    }
                }
                if args.hitbox_id < self.hitboxes.len() as i32 {
                    self.hitboxes[args.hitbox_id as usize] =
                        Some(ScriptCollisionBox::from_grabbox(args, interpolate));
                } else {
                    error!(
                        "invalid hitbox index {} {}",
                        args.hitbox_id, self.subaction_name
                    );
                }
            }
            EventAst::DeleteGrabBox(hitbox_id) => {
                if self.hitboxes[*hitbox_id as usize]
                    .as_ref()
                    .map(|x| x.is_grab())
                    .unwrap_or(false)
                {
                    self.hitboxes[*hitbox_id as usize] = None;
                }
            }
            EventAst::DeleteAllGrabBoxes => {
                for hitbox in self.hitboxes.iter_mut() {
                    if hitbox.as_ref().map(|x| x.is_grab()).unwrap_or(false) {
                        *hitbox = None;
                    }
                }
            }
            EventAst::SpecifyThrow(throw) => {
                match &throw.throw_use {
                    ThrowUse::Throw => {
                        self.throw = Some(throw.clone());
                    }
                    ThrowUse::GrabInterrupt => {
                        // The only known value in the specifier that affects the grab interrupt is damage
                        self.grab_interrupt_damage = Some(throw.damage);
                    }
                    ThrowUse::Unknown(_) => {}
                }
            }
            EventAst::ApplyThrow(_) => {
                self.throw_activate = true;
            }
            EventAst::AddHitBoxDamage {
                hitbox_id,
                add_damage,
            } => {
                let add_damage = self.get_float_value(add_damage);
                if let Some(hitbox) = &mut self.hitboxes[*hitbox_id as usize] {
                    if let CollisionBoxValues::Hit(hitbox) = &mut hitbox.values {
                        hitbox.damage += add_damage;
                    }
                }
            }

            // hurtboxes
            EventAst::ChangeHurtBoxStateAll { state } => {
                self.hurtbox_state_all = state.clone();
            }
            EventAst::ChangeHurtBoxStateSpecific { bone, state } => {
                match state {
                    HurtBoxState::Invincible => {
                        // Setting HurtBoxState::Invincible state with this command is broken.
                        // It either has no effect or sets the state to Normal.
                        // I havent confirmed which, so the current implementation just does nothing.
                    }
                    state => {
                        self.hurtbox_states
                            .insert(high_level_fighter::get_bone_index(*bone), state.clone());
                    }
                }
            }
            EventAst::UnchangeHurtBoxStateSpecific => {
                self.hurtbox_states.clear();
            }

            // controller
            EventAst::ControllerClearBuffer => {}
            EventAst::ControllerUnk01 => {}
            EventAst::ControllerUnk02 => {}
            EventAst::ControllerUnk06(_) => {}
            EventAst::ControllerUnk0C => {}
            EventAst::Rumble { unk1, unk2 } => self.rumble = Some((*unk1, *unk2)),
            EventAst::RumbleLoop { unk1, unk2 } => self.rumble_loop = Some((*unk1, *unk2)),

            // misc
            EventAst::SlopeContourStand { leg_bone_parent } => {
                if *leg_bone_parent == 0 {
                    self.slope_contour_stand = None;
                } else {
                    self.slope_contour_stand = Some(*leg_bone_parent);
                }
            }
            EventAst::SlopeContourFull {
                hip_n_or_top_n,
                trans_bone,
            } => {
                if *hip_n_or_top_n == 0 && *trans_bone == 0 {
                    self.slope_contour_full = None;
                } else {
                    self.slope_contour_full = Some((*hip_n_or_top_n, *trans_bone));
                }
            }
            EventAst::GenerateArticle { .. } => {}
            EventAst::ArticleEvent(_) => {}
            EventAst::ArticleAnimation(_) => {}
            EventAst::ArticleRemove(_) => {}
            EventAst::ArticleVisibility { .. } => {}
            EventAst::FinalSmashEnter => {}
            EventAst::FinalSmashExit => {}
            EventAst::TerminateSelf => {}
            EventAst::Posture(_) => {}
            EventAst::LedgeGrabEnable(enable) => {
                self.ledge_grab_enable = enable.clone();
            }
            EventAst::TagDisplay(display) => {
                self.tag_display = *display;
            }
            EventAst::Armor {
                armor_type,
                tolerance,
            } => {
                self.armor_type = armor_type.clone();
                self.armor_tolerance = *tolerance;
            }
            EventAst::AddDamage(damage) => {
                self.damage += damage;
            }
            EventAst::SetOrAddVelocity(values) => {
                if values.x_set {
                    self.x_vel = values.x_vel;
                    self.x_vel_modify = VelModify::Set(values.x_vel);
                } else {
                    self.x_vel += values.x_vel;
                    self.x_vel_modify = VelModify::Add(self.x_vel_modify.value() + values.x_vel);
                }

                if values.y_set {
                    self.y_vel = values.y_vel;
                    self.y_vel_modify = VelModify::Set(values.y_vel);
                } else {
                    self.y_vel += values.y_vel;
                    self.y_vel_modify = VelModify::Add(self.y_vel_modify.value() + values.y_vel);
                }
            }
            EventAst::SetVelocity { x_vel, y_vel } => {
                self.x_vel = *x_vel;
                self.y_vel = *y_vel;

                self.x_vel_modify = VelModify::Set(*x_vel);
                self.y_vel_modify = VelModify::Set(*y_vel);
            }
            EventAst::AddVelocity { x_vel, y_vel } => {
                let x_vel = self.get_float_value(x_vel);
                let y_vel = self.get_float_value(y_vel);

                self.x_vel += x_vel;
                self.y_vel += y_vel;

                self.x_vel_modify = VelModify::Add(self.x_vel_modify.value() + x_vel);
                self.y_vel_modify = VelModify::Add(self.y_vel_modify.value() + y_vel);
            }
            EventAst::DisableMovement(disable_movement) => {
                self.disable_movement = disable_movement.clone();
            }
            EventAst::DisableMovement2(_) => {} // TODO: What!?!?
            EventAst::ResetVerticalVelocityAndAcceleration(reset) => {
                if *reset {
                    self.y_vel = 0.0;

                    self.y_vel_modify = VelModify::Set(0.0);
                }
            }
            EventAst::NormalizePhysics => {} // TODO

            // sound
            EventAst::SoundEffect1(_) => {}
            EventAst::SoundEffect2(_) => {}
            EventAst::SoundEffectTransient(_) => {}
            EventAst::SoundEffectStop(_) => {}
            EventAst::SoundEffectVictory(_) => {}
            EventAst::SoundEffectUnk(_) => {}
            EventAst::SoundEffectOther1(_) => {}
            EventAst::SoundEffectOther2(_) => {}
            EventAst::SoundVoiceLow => {}
            EventAst::SoundVoiceDamage => {}
            EventAst::SoundVoiceOttotto => {}
            EventAst::SoundVoiceEating => {}

            // variables
            EventAst::IntVariableSet { value, variable } => {
                self.set_variable_int(variable, *value);
            }
            EventAst::IntVariableAdd { value, variable } => {
                let old_value = self.get_variable_int(variable);
                self.set_variable_int(variable, old_value + *value);
            }
            EventAst::IntVariableSubtract { value, variable } => {
                let old_value = self.get_variable_int(variable);
                self.set_variable_int(variable, old_value - value);
            }
            EventAst::IntVariableIncrement { variable } => {
                let old_value = self.get_variable_int(variable);
                self.set_variable_int(variable, old_value + 1);
            }
            EventAst::IntVariableDecrement { variable } => {
                let old_value = self.get_variable_int(variable);
                self.set_variable_int(variable, old_value - 1);
            }
            EventAst::FloatVariableSet { value, variable } => {
                let value = self.get_float_value(value);
                self.set_variable_float(variable, value);
            }
            EventAst::FloatVariableAdd { value, variable } => {
                let value = self.get_float_value(value);
                let old_value = self.get_variable_float(variable);
                self.set_variable_float(variable, old_value + value);
            }
            EventAst::FloatVariableSubtract { value, variable } => {
                let value = self.get_float_value(value);
                let old_value = self.get_variable_float(variable);
                self.set_variable_float(variable, old_value - value);
            }
            EventAst::FloatVariableMultiply { value, variable } => {
                let value = self.get_float_value(value);
                let old_value = self.get_variable_float(variable);
                self.set_variable_float(variable, old_value * value);
            }
            EventAst::FloatVariableDivide { value, variable } => {
                let value = self.get_float_value(value);
                let old_value = self.get_variable_float(variable);
                self.set_variable_float(variable, old_value / value);
            }
            EventAst::BoolVariableSetTrue { variable } => match variable.data_type() {
                VariableDataType::Int => self.set_variable_int_inner(variable, 1),
                VariableDataType::Float => self.set_variable_float_inner(variable, 1.0),
                VariableDataType::Bool => self.set_variable_bool_inner(variable, true),
                VariableDataType::Unknown { .. } => {}
            },
            EventAst::BoolVariableSetFalse { variable } => match variable.data_type() {
                VariableDataType::Int => self.set_variable_int_inner(variable, 0),
                VariableDataType::Float => self.set_variable_float_inner(variable, 0.0),
                VariableDataType::Bool => self.set_variable_bool_inner(variable, false),
                VariableDataType::Unknown { .. } => {}
            },

            // graphics
            EventAst::GraphicEffect(_) => {}
            EventAst::ExternalGraphicEffect(_) => {}
            EventAst::LimitedScreenTint(_) => {}
            EventAst::UnlimitedScreenTint(_) => {}
            EventAst::EndUnlimitedScreenTint { .. } => {}
            EventAst::SwordGlow(_) => {}
            EventAst::DeleteSwordGlow { .. } => {}
            EventAst::AestheticWindEffect(_) => {}
            EventAst::EndAestheticWindEffect { .. } => {}
            EventAst::ScreenShake { .. } => {}
            //EventAst::ModelChanger { reference, switch_index, bone_group_index } => {
            EventAst::ModelChanger { .. } => {
                // TODO: Model visibility change
            }
            EventAst::CameraCloseup(_) => {}
            EventAst::CameraNormal => {}
            EventAst::RemoveFlashEffect => {}
            EventAst::FlashEffectOverlay { .. } => {}
            EventAst::SetColorOfFlashEffectOverlay { .. } => {}
            EventAst::FlashEffectLight { .. } => {}
            EventAst::SetColorOfFlashEffectLight { .. } => {}

            // items
            EventAst::ItemPickup { .. } => {}
            EventAst::ItemThrow { .. } => {}
            EventAst::ItemThrow2 { .. } => {}
            EventAst::ItemDrop => {}
            EventAst::ItemConsume { .. } => {}
            EventAst::ItemSetProperty { .. } => {}
            EventAst::FireWeapon => {}
            EventAst::FireProjectile => {}
            EventAst::Item1F { .. } => {}
            EventAst::ItemCreate { .. } => {}
            EventAst::ItemVisibility(_) => {}
            EventAst::ItemDelete => {}
            EventAst::BeamSwordTrail { .. } => {}

            // do nothing
            EventAst::Unknown(event) => {
                debug!("unknown event: {:#?}", event);
            }
            EventAst::Nop => {}
        }
        StepEventResult::None
    }

    #[rustfmt::skip]
    fn evaluate_expression(&mut self, expression: &Expression) -> ExprResult {
        match expression {
            Expression::Nullary (requirement) => {
                ExprResult::Bool (match requirement {
                    Requirement::CharacterExists => true,
                    Requirement::OnGround => true,
                    Requirement::InAir => false,
                    Requirement::FacingRight => true,
                    Requirement::HasntTethered3Times => true,
                    Requirement::IsNotInDamagingLens => true,
                    _ => false
                })
            }
            Expression::Unary (unary) => {
                ExprResult::Bool (match unary.requirement {
                    Requirement::CharacterExists => true,
                    Requirement::OnGround => true,
                    Requirement::InAir => false,
                    Requirement::FacingRight => true,
                    Requirement::HasntTethered3Times => true,
                    Requirement::IsNotInDamagingLens => true,
                    Requirement::BoolIsTrue => self.evaluate_expression(&unary.value).unwrap_bool(),
                    _ => false
                })
            }
            #[allow(clippy::float_cmp)]
            Expression::Binary (binary) => {
                let result = match &binary.operator {
                    ComparisonOperator::LessThan => {
                        match (self.evaluate_expression(&binary.left), self.evaluate_expression(&binary.right)) {
                            (ExprResult::Float (left), ExprResult::Float (right)) => left          < right,
                            (ExprResult::Int (left),   ExprResult::Float (right)) => (left as f32) < right,
                            (ExprResult::Float (left), ExprResult::Int (right))   => left          < right as f32,
                            (ExprResult::Int (left),   ExprResult::Int (right))   => left          < right,
                            _ => panic!("Cannot evaluate expression: {:?}", binary),
                        }
                    }
                    ComparisonOperator::LessThanOrEqual => {
                        match (self.evaluate_expression(&binary.left), self.evaluate_expression(&binary.right)) {
                            (ExprResult::Float (left), ExprResult::Float (right)) => left          <= right,
                            (ExprResult::Int (left),   ExprResult::Float (right)) => (left as f32) <= right,
                            (ExprResult::Float (left), ExprResult::Int (right))   => left          <= right as f32,
                            (ExprResult::Int (left),   ExprResult::Int (right))   => left          <= right,
                            _ => panic!("Cannot evaluate expression: {:?}", binary),
                        }
                    }
                    ComparisonOperator::Equal => {
                        match (self.evaluate_expression(&binary.left), self.evaluate_expression(&binary.right)) {
                            (ExprResult::Float (left), ExprResult::Float (right)) => left          == right,
                            (ExprResult::Int (left),   ExprResult::Float (right)) => (left as f32) == right,
                            (ExprResult::Float (left), ExprResult::Int (right))   => left          == right as f32,
                            (ExprResult::Int (left),   ExprResult::Int (right))   => left          == right,
                            _ => panic!("Cannot evaluate expression: {:?}", binary),
                        }
                    }
                    ComparisonOperator::NotEqual => {
                        match (self.evaluate_expression(&binary.left), self.evaluate_expression(&binary.right)) {
                            (ExprResult::Float (left), ExprResult::Float (right)) => left          != right,
                            (ExprResult::Int (left),   ExprResult::Float (right)) => (left as f32) != right,
                            (ExprResult::Float (left), ExprResult::Int (right))   => left          != right as f32,
                            (ExprResult::Int (left),   ExprResult::Int (right))   => left          != right,
                            _ => panic!("Cannot evaluate expression: {:?}", binary),
                        }
                    }
                    ComparisonOperator::GreaterThanOrEqual => {
                        match (self.evaluate_expression(&binary.left), self.evaluate_expression(&binary.right)) {
                            (ExprResult::Float (left), ExprResult::Float (right)) => left          >= right,
                            (ExprResult::Int (left),   ExprResult::Float (right)) => (left as f32) >= right,
                            (ExprResult::Float (left), ExprResult::Int (right))   => left          >= right as f32,
                            (ExprResult::Int (left),   ExprResult::Int (right))   => left          >= right,
                            _ => panic!("Cannot evaluate expression: {:?}", binary),
                        }
                    }
                    ComparisonOperator::GreaterThan => {
                        match (self.evaluate_expression(&binary.left), self.evaluate_expression(&binary.right)) {
                            (ExprResult::Float (left), ExprResult::Float (right)) => left          > right,
                            (ExprResult::Int (left),   ExprResult::Float (right)) => (left as f32) > right,
                            (ExprResult::Float (left), ExprResult::Int (right))   => left          > right as f32,
                            (ExprResult::Int (left),   ExprResult::Int (right))   => left          > right,
                            _ => panic!("Cannot evaluate expression: {:?}", binary),
                        }
                    }
                    ComparisonOperator::Or                 => self.evaluate_expression(&*binary.left).unwrap_bool() || self.evaluate_expression(&*binary.right).unwrap_bool(),
                    ComparisonOperator::And                => self.evaluate_expression(&*binary.left).unwrap_bool() && self.evaluate_expression(&*binary.right).unwrap_bool(),
                    ComparisonOperator::UnknownArg (_)     => false,
                };
                ExprResult::Bool (result)
            }
            Expression::Not (expression) => ExprResult::Bool (!self.evaluate_expression(expression).unwrap_bool()),
            Expression::Variable (variable) => {
                match variable.data_type() {
                    VariableDataType::Int            => ExprResult::Int   (self.get_variable_int_inner(variable)),
                    VariableDataType::Float          => ExprResult::Float (self.get_variable_float_inner(variable)),
                    VariableDataType::Bool           => ExprResult::Bool  (self.get_variable_bool_inner(variable)),
                    VariableDataType::Unknown { .. } => ExprResult::Bool (false), // can't be handled properly so just give a dummy value
                }
            }
            Expression::Value (int) => ExprResult::Int (*int),
            Expression::Scalar (float) => ExprResult::Float (*float),
        }
    }

    fn get_variable_int(&self, variable: &VariableAst) -> i32 {
        match variable.data_type() {
            VariableDataType::Int => self.get_variable_int_inner(variable),
            VariableDataType::Float => self.get_variable_float_inner(variable) as i32,
            VariableDataType::Bool => {
                if self.get_variable_bool_inner(variable) {
                    1
                } else {
                    0
                }
            }
            VariableDataType::Unknown { .. } => 0,
        }
    }

    fn get_variable_float(&self, variable: &VariableAst) -> f32 {
        match variable.data_type() {
            VariableDataType::Int => self.get_variable_int_inner(variable) as f32,
            VariableDataType::Float => self.get_variable_float_inner(variable),
            VariableDataType::Bool => {
                if self.get_variable_bool_inner(variable) {
                    1.0
                } else {
                    0.0
                }
            }
            VariableDataType::Unknown { .. } => 0.0,
        }
    }

    fn get_float_value(&self, value: &FloatValue) -> f32 {
        match value {
            FloatValue::Constant(value) => *value,
            FloatValue::Variable(variable) => self.get_variable_float(variable),
        }
    }

    fn set_variable_int(&mut self, variable: &VariableAst, value: i32) {
        match variable.data_type() {
            VariableDataType::Int => self.set_variable_int_inner(variable, value),
            VariableDataType::Float => self.set_variable_float_inner(variable, value as f32),
            VariableDataType::Bool => self.set_variable_bool_inner(variable, value != 0),
            VariableDataType::Unknown { .. } => {}
        }
    }

    fn set_variable_float(&mut self, variable: &VariableAst, value: f32) {
        match variable.data_type() {
            VariableDataType::Int => self.set_variable_int_inner(variable, value as i32),
            VariableDataType::Float => self.set_variable_float_inner(variable, value),
            VariableDataType::Bool => self.set_variable_bool_inner(variable, value != 0.0),
            VariableDataType::Unknown { .. } => {}
        }
    }

    fn get_variable_int_inner(&self, variable: &VariableAst) -> i32 {
        match variable {
            VariableAst::InternalConstantInt (InternalConstantInt::CurrentFrame) => self.frame_index as i32,
            VariableAst::InternalConstantInt (InternalConstantInt::CharacterDirection) => 1,
            VariableAst::InternalConstantInt (InternalConstantInt::CharacterDirectionOpposite) => -1,
            VariableAst::InternalConstantInt (InternalConstantInt::CurrentFrameSpeed) => self.frame_speed_modifier as i32,
            VariableAst::InternalConstantInt (InternalConstantInt::CurrentSubaction) => 0, // TODO: Get this passed as an argument to ScriptRunner::new
            VariableAst::InternalConstantInt (InternalConstantInt::CurrentAction) => 0, // TODO: Get this passed as an argument to ScriptRunner::new
            VariableAst::InternalConstantInt (InternalConstantInt::CrawlControlStickXOffsetMax) => 20, // TODO: probably character dependent?
            VariableAst::InternalConstantInt (InternalConstantInt::CrawlControlStickXOffsetMin) => -20, // TODO: probably character dependent?
            VariableAst::InternalConstantInt (_) => 0, // Best we can do for everything else is 0
            VariableAst::LongtermAccessInt   (LongtermAccessInt::JumpsUsed) => self.jumps_used,
            VariableAst::LongtermAccessInt   (LongtermAccessInt::WallJumpCount) => self.wall_jump_count,
            VariableAst::LongtermAccessInt   (LongtermAccessInt::WallJumpInterval) => self.wall_jump_interval,
            VariableAst::LongtermAccessInt   (LongtermAccessInt::FootstoolCount) => self.footstool_count,
            VariableAst::LongtermAccessInt   (LongtermAccessInt::FallTime) => self.fall_time,
            VariableAst::LongtermAccessInt   (LongtermAccessInt::SwimTime) => self.swim_time,
            VariableAst::LongtermAccessInt   (LongtermAccessInt::LipStickRefresh) => self.lip_stick_refresh,
            VariableAst::LongtermAccessInt   (LongtermAccessInt::CurryRemainingTime) => self.curry_remaining_time,
            VariableAst::LongtermAccessInt   (LongtermAccessInt::CurryAngle2) => self.curry_angle2,
            VariableAst::LongtermAccessInt   (LongtermAccessInt::StarRemainingTime) => self.star_remaining_time,
            VariableAst::LongtermAccessInt   (LongtermAccessInt::MushroomRemainingTime) => self.mushroom_remaining_time,
            VariableAst::LongtermAccessInt   (LongtermAccessInt::LightningRemainingTime) => self.lightning_remaining_time,
            VariableAst::LongtermAccessInt   (LongtermAccessInt::SizeFlag) => self.size_flag,
            VariableAst::LongtermAccessInt   (LongtermAccessInt::MetalBlockRemainingTime) => self.metal_block_remaining_time,
            VariableAst::LongtermAccessInt   (LongtermAccessInt::ComboCount) => self.combo_count,
            VariableAst::LongtermAccessInt   (LongtermAccessInt::BubbleTime) => self.bubble_time,
            VariableAst::LongtermAccessInt   (LongtermAccessInt::AttacksPerformed) => self.attacks_performed,
            VariableAst::LongtermAccessInt   (LongtermAccessInt::CostumeID) => self.costume_id,
            VariableAst::LongtermAccessInt   (LongtermAccessInt::HitstunFramesRemaining) => self.hitstun_frames_remaining,
            VariableAst::LongtermAccessInt   (LongtermAccessInt::MeteorCancelWindow) => self.meteor_cancel_window,
            VariableAst::LongtermAccessInt   (LongtermAccessInt::MissedTechs) => self.missed_techs,
            VariableAst::LongtermAccessInt   (LongtermAccessInt::TetherCount) => self.tether_count,
            VariableAst::LongtermAccessInt   (LongtermAccessInt::Temp1) => self.temp1,
            VariableAst::LongtermAccessInt   (LongtermAccessInt::Temp2) => self.temp2,
            VariableAst::LongtermAccessInt   (LongtermAccessInt::Address (address)) => self.longterm_access_int.get(*address as usize).cloned().unwrap_or(0),
            VariableAst::RandomAccessInt     (RandomAccessInt::ThrowDataParam1) => self.throw_data_param1,
            VariableAst::RandomAccessInt     (RandomAccessInt::ThrowDataParam2) => self.throw_data_param2,
            VariableAst::RandomAccessInt     (RandomAccessInt::ThrowDataParam3) => self.throw_data_param3,
            VariableAst::RandomAccessInt     (RandomAccessInt::Address (address)) => self.random_access_int.get(*address as usize).cloned().unwrap_or(0),
            VariableAst::Unknown             { .. } => 0, // Likely from garbage data

            VariableAst::LongtermAccessFloat (_) | VariableAst::LongtermAccessBool (_) |
            VariableAst::RandomAccessFloat (_) | VariableAst::RandomAccessBool (_)
                => panic!("Called get_variable_int on a variable that is not an int. '{:?}' It is a brawllib_rs logic error if this is reached", variable),
        }
    }

    fn set_variable_int_inner(&mut self, variable: &VariableAst, value: i32) {
        match variable {
            VariableAst::InternalConstantInt (_) => {}, // Cant set a constant
            VariableAst::LongtermAccessInt (LongtermAccessInt::JumpsUsed) => self.jumps_used = value,
            VariableAst::LongtermAccessInt (LongtermAccessInt::WallJumpCount) => self.wall_jump_count = value,
            VariableAst::LongtermAccessInt (LongtermAccessInt::WallJumpInterval) => self.wall_jump_interval = value,
            VariableAst::LongtermAccessInt (LongtermAccessInt::FootstoolCount) => self.footstool_count = value,
            VariableAst::LongtermAccessInt (LongtermAccessInt::FallTime) => self.fall_time = value,
            VariableAst::LongtermAccessInt (LongtermAccessInt::SwimTime) => self.swim_time = value,
            VariableAst::LongtermAccessInt (LongtermAccessInt::LipStickRefresh) => self.lip_stick_refresh = value,
            VariableAst::LongtermAccessInt (LongtermAccessInt::CurryRemainingTime) => self.curry_remaining_time = value,
            VariableAst::LongtermAccessInt (LongtermAccessInt::CurryAngle2) => self.curry_angle2 = value,
            VariableAst::LongtermAccessInt (LongtermAccessInt::StarRemainingTime) => self.star_remaining_time = value,
            VariableAst::LongtermAccessInt (LongtermAccessInt::MushroomRemainingTime) => self.mushroom_remaining_time = value,
            VariableAst::LongtermAccessInt (LongtermAccessInt::LightningRemainingTime) => self.lightning_remaining_time = value,
            VariableAst::LongtermAccessInt (LongtermAccessInt::SizeFlag) => self.size_flag = value,
            VariableAst::LongtermAccessInt (LongtermAccessInt::MetalBlockRemainingTime) => self.metal_block_remaining_time = value,
            VariableAst::LongtermAccessInt (LongtermAccessInt::ComboCount) => self.combo_count = value,
            VariableAst::LongtermAccessInt (LongtermAccessInt::BubbleTime) => self.bubble_time = value,
            VariableAst::LongtermAccessInt (LongtermAccessInt::AttacksPerformed) => self.attacks_performed = value,
            VariableAst::LongtermAccessInt (LongtermAccessInt::CostumeID) => self.costume_id = value,
            VariableAst::LongtermAccessInt (LongtermAccessInt::HitstunFramesRemaining) => self.hitstun_frames_remaining = value,
            VariableAst::LongtermAccessInt (LongtermAccessInt::MeteorCancelWindow) => self.meteor_cancel_window = value,
            VariableAst::LongtermAccessInt (LongtermAccessInt::MissedTechs) => self.missed_techs = value,
            VariableAst::LongtermAccessInt (LongtermAccessInt::TetherCount) => self.tether_count = value,
            VariableAst::LongtermAccessInt (LongtermAccessInt::Temp1) => self.temp1 = value,
            VariableAst::LongtermAccessInt (LongtermAccessInt::Temp2) => self.temp2 = value,
            VariableAst::LongtermAccessInt (LongtermAccessInt::Address (address)) =>
                if (*address as usize) < self.longterm_access_int.len() {
                    self.longterm_access_int[*address as usize] = value;
                }

            VariableAst::RandomAccessInt (RandomAccessInt::ThrowDataParam1) => self.throw_data_param1 = value,
            VariableAst::RandomAccessInt (RandomAccessInt::ThrowDataParam2) => self.throw_data_param2 = value,
            VariableAst::RandomAccessInt (RandomAccessInt::ThrowDataParam3) => self.throw_data_param3 = value,
            VariableAst::RandomAccessInt (RandomAccessInt::Address (address)) =>
                if (*address as usize) < self.random_access_int.len() {
                    self.random_access_int[*address as usize] = value;
                }

            VariableAst::Unknown             { .. } => {}, // Likely from garbage data

            VariableAst::LongtermAccessFloat (_) | VariableAst::LongtermAccessBool (_) |
            VariableAst::RandomAccessFloat (_) | VariableAst::RandomAccessBool (_)
                => panic!("Called set_variable_int_inner on a variable that is not an int. '{:?}' It is a brawllib_rs logic error if this is reached.", variable),
        }
    }

    fn get_variable_float_inner(&self, variable: &VariableAst) -> f32 {
        match variable {
            VariableAst::LongtermAccessFloat (LongtermAccessFloat::SpecialLandingLag) => self.special_landing_lag,
            VariableAst::LongtermAccessFloat (LongtermAccessFloat::SpecialFallMobilityMultiplier) => self.special_fall_mobility_multiplier,
            VariableAst::LongtermAccessFloat (LongtermAccessFloat::ShieldCharge) => self.shield_charge,
            VariableAst::LongtermAccessFloat (LongtermAccessFloat::CurryAngle1) => self.curry_angle1,
            VariableAst::LongtermAccessFloat (LongtermAccessFloat::CurryRandomness) => self.curry_randomness,
            VariableAst::LongtermAccessFloat (LongtermAccessFloat::Address (address)) => self.longterm_access_float.get(*address as usize).cloned().unwrap_or(0.0),
            VariableAst::RandomAccessFloat   (RandomAccessFloat::EnableTurnWhenBelowZero) => self.enable_turn_when_below_zero,
            VariableAst::RandomAccessFloat   (RandomAccessFloat::Address (address)) => self.random_access_float.get(*address as usize).cloned().unwrap_or(0.0),
            VariableAst::Unknown             { .. } => 0.0, // Likely from garbage data

            VariableAst::LongtermAccessInt (_) | VariableAst::LongtermAccessBool (_) |
            VariableAst::RandomAccessInt (_) | VariableAst::RandomAccessBool (_) |
            VariableAst::InternalConstantInt (_) => panic!("Called get_variable_float on a variable that is not a float. '{:?}' It is a brawllib_rs logic error if this is reached.", variable),
        }
    }

    fn set_variable_float_inner(&mut self, variable: &VariableAst, value: f32) {
        match variable {
            VariableAst::LongtermAccessFloat (LongtermAccessFloat::SpecialLandingLag) => self.special_landing_lag = value,
            VariableAst::LongtermAccessFloat (LongtermAccessFloat::SpecialFallMobilityMultiplier) => self.special_fall_mobility_multiplier = value,
            VariableAst::LongtermAccessFloat (LongtermAccessFloat::ShieldCharge) => self.shield_charge = value,
            VariableAst::LongtermAccessFloat (LongtermAccessFloat::CurryAngle1) => self.curry_angle1 = value,
            VariableAst::LongtermAccessFloat (LongtermAccessFloat::CurryRandomness) => self.curry_randomness = value,
            VariableAst::LongtermAccessFloat (LongtermAccessFloat::Address (address)) =>
                if (*address as usize) < self.longterm_access_float.len() {
                    self.longterm_access_float[*address as usize] = value;
                }
            VariableAst::RandomAccessFloat   (RandomAccessFloat::EnableTurnWhenBelowZero) => self.enable_turn_when_below_zero = value,
            VariableAst::RandomAccessFloat   (RandomAccessFloat::Address (address)) =>
                if (*address as usize) < self.random_access_float.len() {
                    self.random_access_float[*address as usize] = value;
                }
            VariableAst::Unknown             { .. } => { }, // Likely from garbage data

            VariableAst::LongtermAccessInt (_) | VariableAst::LongtermAccessBool (_) |
            VariableAst::RandomAccessInt (_) | VariableAst::RandomAccessBool (_) |
            VariableAst::InternalConstantInt (_) => panic!("Called set_variable_float_inner on a variable that is not a float. '{:?}' It is a brawllib_rs logic error if this is reached.", variable),
        }
    }

    fn get_variable_bool_inner(&self, variable: &VariableAst) -> bool {
        match variable {
            VariableAst::LongtermAccessBool (LongtermAccessBool::IsDead) => self.is_dead,
            VariableAst::LongtermAccessBool (LongtermAccessBool::CannotDie) => self.cannot_die,
            VariableAst::LongtermAccessBool (LongtermAccessBool::AutomaticFootstool) => self.automatic_footstool,
            VariableAst::LongtermAccessBool (LongtermAccessBool::HasFinal) => self.has_final,
            VariableAst::LongtermAccessBool (LongtermAccessBool::HasFinalAura) => self.has_final_aura,
            VariableAst::LongtermAccessBool (LongtermAccessBool::HasCurry) => self.has_curry,
            VariableAst::LongtermAccessBool (LongtermAccessBool::HasHammer) => self.has_hammer,
            VariableAst::LongtermAccessBool (LongtermAccessBool::HitByParalyze) => self.hit_by_paralyze,
            VariableAst::LongtermAccessBool (LongtermAccessBool::HasScrewAttack) => self.has_screw_attack,
            VariableAst::LongtermAccessBool (LongtermAccessBool::StaminaDead) => self.stamina_dead,
            VariableAst::LongtermAccessBool (LongtermAccessBool::HasTag) => self.has_tag,
            VariableAst::LongtermAccessBool (LongtermAccessBool::CanNotLedgeGrab) => self.can_not_ledge_grab,
            VariableAst::LongtermAccessBool (LongtermAccessBool::CanNotTeeter) => self.can_not_teeter,
            VariableAst::LongtermAccessBool (LongtermAccessBool::VelocityIgnoreHitstun) => self.velocity_ignore_hitstun,
            VariableAst::LongtermAccessBool (LongtermAccessBool::Deflection) => self.deflection,
            VariableAst::LongtermAccessBool (LongtermAccessBool::Address (address)) => self.longterm_access_bool.get(*address as usize).cloned().unwrap_or(false),

            VariableAst::RandomAccessBool (RandomAccessBool::CharacterFloat) => self.character_float,
            VariableAst::RandomAccessBool (RandomAccessBool::EnableFastFall) => self.enable_fast_fall,
            VariableAst::RandomAccessBool (RandomAccessBool::Shorthop) => self.shorthop,
            VariableAst::RandomAccessBool (RandomAccessBool::EnableActionTransition) => self.enable_action_transition,
            VariableAst::RandomAccessBool (RandomAccessBool::SpecialsMovement) => self.specials_movement,
            VariableAst::RandomAccessBool (RandomAccessBool::EnableGlide) => self.enable_glide,
            VariableAst::RandomAccessBool (RandomAccessBool::EnableJabLoop) => self.enable_jab_loop,
            VariableAst::RandomAccessBool (RandomAccessBool::EnableAutoJab) => self.enable_auto_jab,
            VariableAst::RandomAccessBool (RandomAccessBool::EnableJabEnd) => self.enable_jab_end,
            VariableAst::RandomAccessBool (RandomAccessBool::EnableLandingLag) => self.landing_lag,
            VariableAst::RandomAccessBool (RandomAccessBool::Address (address)) => self.random_access_bool.get(*address as usize).cloned().unwrap_or(false),
            VariableAst::Unknown          { .. } => false, // Likely from garbage data

            VariableAst::LongtermAccessInt (_) | VariableAst::LongtermAccessFloat (_) |
            VariableAst::RandomAccessInt (_) | VariableAst::RandomAccessFloat (_) |
            VariableAst::InternalConstantInt (_) => panic!("Called get_variable_bool on a variable that is not a bool. '{:?}' It is a brawllib_rs logic error if this is reached.", variable),
        }
    }

    fn set_variable_bool_inner(&mut self, variable: &VariableAst, value: bool) {
        match variable {
            VariableAst::LongtermAccessBool (LongtermAccessBool::IsDead) => self.is_dead = value,
            VariableAst::LongtermAccessBool (LongtermAccessBool::CannotDie) => self.cannot_die = value,
            VariableAst::LongtermAccessBool (LongtermAccessBool::AutomaticFootstool) => self.automatic_footstool = value,
            VariableAst::LongtermAccessBool (LongtermAccessBool::HasFinal) => self.has_final = value,
            VariableAst::LongtermAccessBool (LongtermAccessBool::HasFinalAura) => self.has_final_aura = value,
            VariableAst::LongtermAccessBool (LongtermAccessBool::HasCurry) => self.has_curry = value,
            VariableAst::LongtermAccessBool (LongtermAccessBool::HasHammer) => self.has_hammer = value,
            VariableAst::LongtermAccessBool (LongtermAccessBool::HitByParalyze) => self.hit_by_paralyze = value,
            VariableAst::LongtermAccessBool (LongtermAccessBool::HasScrewAttack) => self.has_screw_attack = value,
            VariableAst::LongtermAccessBool (LongtermAccessBool::StaminaDead) => self.stamina_dead = value,
            VariableAst::LongtermAccessBool (LongtermAccessBool::HasTag) => self.has_tag = value,
            VariableAst::LongtermAccessBool (LongtermAccessBool::CanNotLedgeGrab) => self.can_not_ledge_grab = value,
            VariableAst::LongtermAccessBool (LongtermAccessBool::CanNotTeeter) => self.can_not_teeter = value,
            VariableAst::LongtermAccessBool (LongtermAccessBool::VelocityIgnoreHitstun) => self.velocity_ignore_hitstun = value,
            VariableAst::LongtermAccessBool (LongtermAccessBool::Deflection) => self.deflection = value,
            VariableAst::LongtermAccessBool (LongtermAccessBool::Address (address)) =>
                if (*address as usize) < self.longterm_access_bool.len() {
                    self.longterm_access_bool[*address as usize] = value;
                }

            VariableAst::RandomAccessBool (RandomAccessBool::CharacterFloat) => self.character_float = value,
            VariableAst::RandomAccessBool (RandomAccessBool::EnableFastFall) => self.enable_fast_fall = value,
            VariableAst::RandomAccessBool (RandomAccessBool::Shorthop) => self.shorthop = value,
            VariableAst::RandomAccessBool (RandomAccessBool::EnableActionTransition) => self.enable_action_transition = value,
            VariableAst::RandomAccessBool (RandomAccessBool::SpecialsMovement) => self.specials_movement = value,
            VariableAst::RandomAccessBool (RandomAccessBool::EnableGlide) => self.enable_glide = value,
            VariableAst::RandomAccessBool (RandomAccessBool::EnableJabLoop) => self.enable_jab_loop = value,
            VariableAst::RandomAccessBool (RandomAccessBool::EnableAutoJab) => self.enable_auto_jab = value,
            VariableAst::RandomAccessBool (RandomAccessBool::EnableJabEnd) => self.enable_jab_end = value,
            VariableAst::RandomAccessBool (RandomAccessBool::EnableLandingLag) => self.landing_lag = value,
            VariableAst::RandomAccessBool (RandomAccessBool::Address (address)) =>
                if (*address as usize) < self.random_access_bool.len() {
                    self.random_access_bool[*address as usize] = value;
                }
            VariableAst::Unknown          { .. } => {}, // Likely from garbage data

            VariableAst::LongtermAccessInt (_) | VariableAst::LongtermAccessFloat (_) |
            VariableAst::RandomAccessInt (_) | VariableAst::RandomAccessFloat (_) |
            VariableAst::InternalConstantInt (_) => panic!("Called set_variable_bool_inner on a variable that is not a bool. '{:?}' It is a brawllib_rs logic error if this is reached.", variable),
        }
    }
}

enum StepEventResult<'a> {
    WaitUntil(f32),
    NewForLoop {
        block: &'a Block,
        iterations: i32,
    },
    NewCall {
        block: &'a Block,
    },
    NewIfStatement {
        then_branch: &'a Block,
        else_branch: Option<&'a Block>,
        execute: bool,
    },
    IfStatementDisableExecution,
    Goto {
        block: &'a Block,
        external: bool,
    },
    Subroutine {
        block: &'a Block,
        external: bool,
    },
    CallEveryFrame {
        block: &'a Block,
        external: bool,
        thread_id: i32,
    },
    Return,
    None,
}

pub struct CallEveryFrame<'a> {
    pub block: &'a Block,
    pub external: bool,
}

#[derive(Debug)]
enum ExprResult {
    Int(i32),
    Float(f32),
    Bool(bool),
}

impl ExprResult {
    fn unwrap_bool(&self) -> bool {
        match self {
            ExprResult::Bool(result) => *result,
            ExprResult::Int(value) => *value != 0,
            ExprResult::Float(value) => *value != 0.0,
        }
    }
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub enum VelModify {
    Set(f32),
    Add(f32),
    None,
}

impl VelModify {
    pub fn value(&self) -> f32 {
        match self {
            VelModify::Set(a) => *a,
            VelModify::Add(a) => *a,
            VelModify::None => 0.0,
        }
    }
}