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
|
/**
* @file button.c
* @ingroup Objects
*
* @brief Button object
*/
#include "functions.h"
#include "tiles.h"
#include "object.h"
typedef struct {
/*0x00*/ Entity base;
/*0x68*/ u8 unused1[8];
/*0x70*/ u16 unk_70;
/*0x72*/ u16 unk_72;
/*0x74*/ u16 tilePos;
/*0x76*/ u8 unused2[14];
/*0x84*/ u16 unk_84;
/*0x86*/ u16 flag;
} ButtonEntity;
void Button_Init(ButtonEntity* this);
void Button_Action1(ButtonEntity* this);
void Button_Action2(ButtonEntity* this);
void Button_Action3(ButtonEntity* this);
void Button_Action4(ButtonEntity* this);
void Button_Action5(ButtonEntity* this);
void Button(ButtonEntity* this) {
static void (*const Button_Actions[])(ButtonEntity*) = {
Button_Init, Button_Action1, Button_Action2, Button_Action3, Button_Action4, Button_Action5,
};
Button_Actions[super->action](this);
}
extern u32 sub_08081E3C(ButtonEntity*);
void Button_Init(ButtonEntity* this) {
COLLISION_OFF(super);
super->updatePriority = PRIO_NO_BLOCK;
super->y.HALF.HI++;
if (this->unk_84 != 0) {
super->collisionLayer = this->unk_84;
}
this->tilePos = (((super->x.HALF.HI - gRoomControls.origin_x) >> 4) & 0x3F) |
((((super->y.HALF.HI - gRoomControls.origin_y) >> 4) & 0x3F) << 6);
this->unk_72 = GetTileTypeAtTilePos(this->tilePos, super->collisionLayer);
if (super->type == 0 && CheckFlags(this->flag)) {
super->action = 5;
SetTileType(TILE_TYPE_122, this->tilePos, super->collisionLayer);
} else {
if (sub_08081E3C(this)) {
super->action = 2;
} else {
super->action = 1;
}
}
}
void Button_Action1(ButtonEntity* this) {
if (sub_08081E3C(this)) {
super->action = 2;
this->unk_72 = GetTileTypeAtTilePos(this->tilePos, super->collisionLayer);
}
}
u32 sub_08081CB0(ButtonEntity*);
void sub_08081FF8(Entity*);
void Button_Action2(ButtonEntity* this) {
if (sub_08081CB0(this)) {
super->subAction = 0;
super->timer = 10;
RequestPriorityDuration(super, 10);
sub_08081FF8(super);
if (super->type == 1) {
super->action = 3;
} else {
super->action = 5;
}
}
}
u32 sub_08081F7C(ButtonEntity*, u32 tileType);
u32 sub_08081D28(ButtonEntity*);
void sub_08081E6C(ButtonEntity*);
void Button_Action3(ButtonEntity* this) {
if (!sub_08081F7C(this, TILE_TYPE_120))
return;
if (!sub_08081D28(this)) {
super->action = 4;
super->subtimer = 1;
if ((gPlayerState.heldObject == 2) || (!(gPlayerState.field_0x35 & 0x80))) {
super->timer = 24;
} else {
super->timer = 8;
}
} else {
sub_08081E6C(this);
}
}
void Button_Action4(ButtonEntity* this) {
if (super->timer != 0) {
super->timer--;
if (super->subtimer != 0) {
super->subtimer = 0;
SetTile(SPECIAL_TILE_53, this->tilePos, super->collisionLayer);
}
if (sub_08081CB0(this)) {
super->action = 3;
super->timer = 0;
}
} else {
super->action = 2;
ClearFlag(this->flag);
SetTileType(TILE_TYPE_119, this->tilePos, super->collisionLayer);
SoundReq(SFX_BUTTON_PRESS);
}
}
void Button_Action5(ButtonEntity* this) {
if (sub_08081F7C(this, TILE_TYPE_122)) {
sub_08081E6C(this);
}
}
Entity* sub_08081D74(ButtonEntity*);
bool32 sub_08081CB0(ButtonEntity* this) {
u16 tileType;
if (sub_08081D74(this)) {
this->unk_70 = -1;
if (GetTileTypeAtTilePos(this->tilePos, super->collisionLayer) == SPECIAL_TILE_53) {
sub_0807B7D8(0x78, this->tilePos, super->collisionLayer);
}
return TRUE;
} else {
tileType = GetTileTypeAtTilePos(this->tilePos, super->collisionLayer);
if (tileType != 0x77 && tileType != 0x79 && tileType != SPECIAL_TILE_53) {
this->unk_70 = GetTileIndex(this->tilePos, super->collisionLayer);
return TRUE;
}
}
return FALSE;
}
bool32 sub_08081D28(ButtonEntity* this) {
if (sub_08081D74(this)) {
this->unk_70 = 0xFFFF;
return TRUE;
} else {
if (this->unk_70 == 0xFFFF) {
return FALSE;
}
if (GetTileIndex(this->tilePos, super->collisionLayer) != this->unk_70) {
return FALSE;
}
}
return TRUE;
}
extern Entity* gPlayerClones[3];
u32 sub_08081E0C(Entity*);
Entity* sub_08081D74(ButtonEntity* this) {
Entity* ent;
if (GetCollisionDataAtTilePos(this->tilePos, super->collisionLayer) == COLLISION_DATA_15) {
return NULL;
}
ent = 0;
if (sub_08081E0C(super)) {
if ((gPlayerState.flags & PL_CAPTURED) == 0 && (gPlayerState.flags & PL_MINISH) == 0) {
ent = &gPlayerEntity.base;
}
} else {
if (gPlayerState.flags & PL_CLONING) {
if (EntityInRectRadius(super, gPlayerClones[0], 5, 6)) {
ent = gPlayerClones[0];
} else if (EntityInRectRadius(super, gPlayerClones[1], 5, 6)) {
ent = gPlayerClones[1];
} else if (EntityInRectRadius(super, gPlayerClones[2], 5, 6)) {
ent = gPlayerClones[2];
}
}
}
super->child = ent;
return ent;
}
u32 sub_08081E0C(Entity* this) {
Entity* tmp = &gPlayerEntity.base;
if (tmp->z.HALF.HI != 0 || !PlayerCanBeMoved()) {
return 0;
} else {
return EntityInRectRadius(this, tmp, 5, 6);
}
}
u32 sub_08081E3C(ButtonEntity* this) {
static const u16 gUnk_0811EE50[] = {
0x77, 0x78, 0x79, 0x7a, 0, 0,
};
const u16* tmp1;
s32 tmp2;
tmp2 = GetTileTypeAtTilePos(this->tilePos, super->collisionLayer);
tmp1 = gUnk_0811EE50;
do {
if (*tmp1 == tmp2)
return 1;
} while (*++tmp1);
return 0;
}
u32 sub_08081F00(u32*, u32*);
extern u16 gMapDataTopSpecial[0x2000];
extern u16 gMapDataBottomSpecial[];
void sub_08081E6C(ButtonEntity* this) {
u32 tileType;
MapLayer* mapLayer;
u16* tmp2;
u16* subTiles;
u16* tmp3;
u32 tilePos = this->tilePos;
u32 layer = super->collisionLayer;
u32 specialTile = GetTileTypeAtTilePos(tilePos, layer);
if (specialTile < 0x4000)
return;
mapLayer = GetLayerByIndex(layer);
tileType = (super->type == 0 ? TILE_TYPE_122 : TILE_TYPE_120);
subTiles = mapLayer->subTiles;
subTiles = subTiles + (mapLayer->tileIndices[tileType] << 2);
tmp2 = (layer == 2 ? gMapDataTopSpecial : gMapDataBottomSpecial);
tmp2 += (((0x3f & tilePos) << 1) + ((0xfc0 & tilePos) << 2));
if (sub_08081F00((u32*)tmp2, (u32*)subTiles))
return;
SetTileType(tileType, tilePos, layer);
SetTile(specialTile, tilePos, layer);
}
// Are the two tiles already set to the correct one
bool32 sub_08081F00(u32* screenblock, u32* tileList) {
if (screenblock[0] != tileList[0])
return FALSE;
if (screenblock[0x40] != tileList[1])
return FALSE;
return TRUE;
}
void sub_08081F24(Entity* this) {
Entity* fx = CreateFx(this, FX_DASH, 0x40);
if (fx) {
fx->updatePriority = PRIO_NO_BLOCK;
fx->x.HALF.HI += 7;
fx->y.HALF.HI += 5;
}
fx = CreateFx(this, FX_DASH, 0x40);
if (fx) {
fx->updatePriority = PRIO_NO_BLOCK;
fx->x.HALF.HI -= 7;
fx->y.HALF.HI += 5;
}
}
bool32 sub_08081F7C(ButtonEntity* this, u32 tileType) {
u16 tmp;
if (super->timer == 0)
return TRUE;
if (--super->timer > 6) {
if (super->child != NULL)
super->child->spriteOffsetY = -4;
} else {
if (super->timer == 6) {
SetFlag(this->flag);
SetTileType(tileType, this->tilePos, super->collisionLayer);
sub_08081F24(super);
SoundReq(SFX_BUTTON_PRESS);
if (this->unk_70 != 0xFFFF)
SetTile(this->unk_70, this->tilePos, super->collisionLayer);
return FALSE;
}
}
return TRUE;
}
void sub_08081FF8(Entity* this) {
u32 direction;
u32 i;
if (this->child != &gPlayerEntity.base)
return;
direction = GetFacingDirection(this->child, this);
sub_080044AE(this->child, 0x200, direction);
for (i = 0; i < 3; i++) {
if (gPlayerClones[i]) {
sub_080044AE(gPlayerClones[i], 0x200, direction);
}
}
}
|