summaryrefslogtreecommitdiff
path: root/src/enemy/torchTrap.c
blob: f2e4dd2d72999ecca188cc1528078fc0bc7addfd (plain)
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
/**
 * @file torchTrap.c
 * @ingroup Enemies
 *
 * @brief Torch Trap enemy
 */
#include "enemy.h"
#include "entity.h"
#include "physics.h"
#include "player.h"
#include "room.h"
#include "tiles.h"
#include "asm.h"

typedef struct {
    Entity base;
    u8 filler[0xc];
    u16 tilePos;
    u16 filler2;
    u16 unk_78;
    u16 projectileTimer;
    u16 flag1;
    u8 filler3[0x2];
    u16 flag2;
    u16 unk_82;
    u16 unk_84;
} TorchTrapEntity;

void (*const gTorchTrapActions[])(TorchTrapEntity*);
const u16 gTorchTrapTimerLengths[];
const u16 gTorchTrapProjectileSpeeds[];

extern Entity* gUnk_020000B0;

bool32 sub_0803CFF0(TorchTrapEntity*);
bool32 sub_0803CFD8(TorchTrapEntity*);
void sub_0803D0B0(TorchTrapEntity*);
void TorchTrap_Reset(TorchTrapEntity*);
void TorchTrap_CreateProjectile(TorchTrapEntity*);

void TorchTrap(Entity* this) {
    gTorchTrapActions[this->action]((TorchTrapEntity*)this);
}

void TorchTrap_Init(TorchTrapEntity* this) {
    super->speed = (this->unk_84 & 0xf000) >> 5;
    this->unk_84 &= 0xfff;
    sub_0804A720(super);
    super->action = 1;
    this->tilePos = this->unk_82 & 0xfff;
    super->x.HALF.HI = ((this->unk_82 & 0x3f) << 4) + 8 + gRoomControls.origin_x;
    super->y.HALF.HI = ((this->unk_82 & 0xfc0) >> 2) + (gRoomControls.origin_y + 8);
    super->direction = ((s16)this->unk_82 & 0xf000) >> 10;
    this->unk_78 = super->type << 2;
    if (sub_0803CFF0(this)) {
        sub_0803D0B0(this);
    }
}

void sub_0803CF24(TorchTrapEntity* this) {
    if (sub_0803CFD8(this)) {
        TorchTrap_Reset(this);
    }
}

void sub_0803CF38(TorchTrapEntity* this) {
    if (sub_0803CFF0(this)) {
        sub_0803D0B0(this);
    } else if (sub_08049FDC(super, 0)) {
        if (EntityWithinDistance(super, gUnk_020000B0->x.HALF.HI, gUnk_020000B0->y.HALF.HI, 0x20) == 0) {
            if (--this->projectileTimer == 0) {
                TorchTrap_CreateProjectile(this);
                TorchTrap_Reset(this);
            }
        }
    }
}

void sub_0803CF94(TorchTrapEntity* this) {
    if (GetTileTypeAtTilePos(this->tilePos, super->collisionLayer) == TILE_TYPE_118) {
        this->flag2 = 0;
        TorchTrap_Reset(this);
    } else if (this->flag1 && sub_0803CFD8(this)) {
        TorchTrap_Reset(this);
    }
}

bool32 sub_0803CFD8(TorchTrapEntity* this) {
    u32 result;
    if (this->flag1 == 0) {
        result = TRUE;
    } else {
        result = CheckFlags(this->flag1);
    }
    return result;
}

bool32 sub_0803CFF0(TorchTrapEntity* this) {
    u32 result;
    if (this->flag2 != 0) {
        if (this->flag2 == this->flag1) {
            u32 val = CheckFlags(this->flag2);
            result = FALSE;
            if (val == 0) {
                result = TRUE;
            }
            return result;
        } else {
            return CheckFlags(this->flag2);
        }
    }

    return FALSE;
}

void TorchTrap_Reset(TorchTrapEntity* this) {
    super->action = 2;
    if (this->unk_78) {
        this->projectileTimer = this->unk_78;
    } else {
        this->projectileTimer = gTorchTrapTimerLengths[Random() & 7];
    }
}

void TorchTrap_CreateProjectile(TorchTrapEntity* this) {
    Entity* proj = EnemyCreateProjectile(super, TORCH_TRAP_PROJECTILE, 0);
    if (proj) {
        u16 speed = super->speed;
        if (super->speed) {
            proj->speed = speed;
        } else {
            proj->speed = gTorchTrapProjectileSpeeds[Random() & 3];
        }

        if (super->direction & 0x20) {
            proj->direction = GetFacingDirection(super, gUnk_020000B0);
        } else {
            proj->direction = super->direction;
        }
    }
}

void sub_0803D0B0(TorchTrapEntity* this) {
    super->action = 3;
    sub_0807B7D8(TILE_TYPE_117, this->tilePos, super->collisionLayer);
}

void (*const gTorchTrapActions[])(TorchTrapEntity*) = {
    TorchTrap_Init,
    sub_0803CF24,
    sub_0803CF38,
    sub_0803CF94,
};

const u16 gTorchTrapTimerLengths[] = {
    60, 60, 90, 90, 90, 120, 120, 150,
};

const u16 gTorchTrapProjectileSpeeds[] = {
    128,
    256,
    384,
    512,
};