summaryrefslogtreecommitdiff
path: root/src/endingMgr.cpp
blob: 09dfc3c1afb2e1603889ba6b0849213b4e34d214 (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
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
#include <game.h>
#include <playerAnim.h>
#include <sfx.h>
#include <stage.h>
extern void *SoundRelatedClass;

class dEndingMgr_c : public daBossDemo_c {
	int onCreate();
	int onDelete();

	void init();
	Vec2 _vf70();

	USING_STATES(dEndingMgr_c);
	DECLARE_STATE_VIRTUAL(DemoSt);
	DECLARE_STATE(GoRight);
	DECLARE_STATE(LookUp);
	DECLARE_STATE(JumpOntoSwitch);
	DECLARE_STATE(ThanksPeach);

	dAcPy_c *players[4];

	int timer;

	static dEndingMgr_c *build();
};

dEndingMgr_c *dEndingMgr_c::build() {
	void *buf = AllocFromGameHeap1(sizeof(dEndingMgr_c));
	return new(buf) dEndingMgr_c;
}


int dEndingMgr_c::onCreate() {
	if (StageE4::instance->currentBossDemo) {
		fBase_c::Delete();
		return false;
	}
	StageE4::instance->currentBossDemo = this;

	init();

	acState.setState(&StateID_DemoSt);
	acState.executeNextStateThisTick();

	return true;
}

int dEndingMgr_c::onDelete() {
	nw4r::snd::SoundHandle *fanfare = (nw4r::snd::SoundHandle*)
		(((u32)SoundRelatedClass) + 0x900);
	if (fanfare->Exists())
		fanfare->Stop(60);

	daBossDemo_c::onDelete();
	WLClass::instance->disableDemoControl(true);
	return true;
}


void dEndingMgr_c::init() {
	dStageActorMgr_c::instance->_BCA = true;
	WLClass::instance->demoControlAllPlayers();
	BalloonRelatedClass::instance->_20 = 1;
}


CREATE_STATE(dEndingMgr_c, DemoSt);

void dEndingMgr_c::beginState_DemoSt() {
	_360 = 1;
}
void dEndingMgr_c::endState_DemoSt() {
}
void dEndingMgr_c::executeState_DemoSt() {
	for (int i = 0; i < 4; i++) {
		dAcPy_c *player;
		if (player = dAcPy_c::findByID(i)) {
			if (!player->isReadyForDemoControlAction())
				return;
		}
	}
	acState.setState(&StateID_GoRight);
}

static float manipFourPlayerPos(int id, float pos) {
	int fromRight = 3 - id;
	return pos - (fromRight * 20.0f);
}

CREATE_STATE(dEndingMgr_c, GoRight);
void dEndingMgr_c::beginState_GoRight() {
	timer = 0;

	// Sort the players into a list
	// We shall first find the rightmost player, then the second
	// rightmost, then ...
	float maxBound = 50000.0f;
	for (int targetOffs = 3; targetOffs >= 0; targetOffs--) {
		float maxX = 0.0f;
		dAcPy_c *maxPlayer = 0;

		for (int check = 0; check < 4; check++) {
			if (dAcPy_c *player = dAcPy_c::findByID(check)) {
				if (player->pos.x >= maxBound)
					continue;

				if (player->pos.x > maxX) {
					maxX = player->pos.x;
					maxPlayer = player;
				}
			}
		}

		maxBound = maxX;
		players[targetOffs] = maxPlayer;
	}

	// And now move them
	for (int i = 0; i < 4; i++) {
		if (dAcPy_c *player = players[i]) {
			float target = manipFourPlayerPos(i, 1060.0f);
			float speed = 2.0f;
			player->moveInDirection(&target, &speed);
		}
	}
}

void dEndingMgr_c::endState_GoRight() { }

void dEndingMgr_c::executeState_GoRight() {
	for (int i = 0; i < 4; i++) {
		if (dAcPy_c *player = players[i]) {
			if (player->pos.x > manipFourPlayerPos(i, 920.0f)) {
				player->demoMoveSpeed *= 0.994f;
			}
		}
	}

	// Can we leave this state yet?
	for (int i = 0; i < 4; i++) {
		if (dAcPy_c *player = players[i]) {
			if (!player->isReadyForDemoControlAction())
				return;
		}
	}

	// WE CAN LEAVE YAY
	timer++;
	if (timer >= 20)
		acState.setState(&StateID_LookUp);
}

CREATE_STATE(dEndingMgr_c, LookUp);
void dEndingMgr_c::beginState_LookUp() {
	_120 |= 8;
	lookAtMode = 2; // Higher maximum distance

	timer = 0;
}

void dEndingMgr_c::endState_LookUp() {
}

void dEndingMgr_c::executeState_LookUp() {
	timer++;

	if (timer >= 60)
		_120 &= ~8;

	if (timer >= 90)
		acState.setState(&StateID_JumpOntoSwitch);
}


CREATE_STATE(dEndingMgr_c, JumpOntoSwitch);
void dEndingMgr_c::beginState_JumpOntoSwitch() {
	for (int i = 0; i < 4; i++) {
		if (dAcPy_c *player = players[i]) {
			float target = manipFourPlayerPos(i, 1144.0f);
			float speed = 2.0f;
			player->moveInDirection(&target, &speed);
		}
	}

	timer = 0;
}
void dEndingMgr_c::endState_JumpOntoSwitch() {
}
void dEndingMgr_c::executeState_JumpOntoSwitch() {
	dAcPy_c *player = players[3];

	if (timer > 60) {
		acState.setState(&StateID_ThanksPeach);
	} else if (player->isReadyForDemoControlAction()) {
		player->input.setTransientForcedButtons(WPAD_DOWN);
	} else if (timer > 30) {
		player->input.unsetPermanentForcedButtons(WPAD_TWO);
	} else if (timer > 15) {
		player->input.setPermanentForcedButtons(WPAD_TWO);
	}

	timer++;
}

static daEnBossKoopaDemoPeach_c *getPeach() {
	return (daEnBossKoopaDemoPeach_c*)dEn_c::search(EN_BOSS_KOOPA_DEMO_PEACH);
}

CREATE_STATE(dEndingMgr_c, ThanksPeach);
void dEndingMgr_c::beginState_ThanksPeach() {
	timer = -1;
	getPeach()->doStateChange(&daEnBossKoopaDemoPeach_c::StateID_Turn);
}
void dEndingMgr_c::endState_ThanksPeach() {
}
void dEndingMgr_c::executeState_ThanksPeach() {
	daEnBossKoopaDemoPeach_c *peach = getPeach();
	dStateBase_c *peachSt = peach->acState.getCurrentState();

	if (peach->stage == 1 && peachSt == &peach->StateID_Turn) {
		timer++;
		if (timer == 1) {
			// This plays the Peach harp fanfare
			sub_8019C390(_8042A788, 6);
		}
		if (timer > 20) {
			peach->doStateChange(&peach->StateID_Open);
			peach->_120 |= 8;
			timer = -1;
		}
	}

	if (peach->stage == 1 && peachSt == &peach->StateID_Open) {
		peach->doStateChange(&peach->StateID_Rescue);
	}

	if (peach->stage == 3 && peachSt == &peach->StateID_Rescue) {
		peach->doStateChange(&peach->StateID_Thank);
	}

	if (peachSt == &peach->StateID_Thank) {
		// 1. Freeze Peach by changing to Stage 8 as soon as she's almost done
		// 2. Do our thing
		// 3. Go back!
		if (peach->stage == 0 && peach->counter == 33 && timer == -1) {
			peach->stage = 8;
			timer = 0;
		} else if (peach->stage == 8) {
			timer++;
			if (timer == 8) {
				float target = peach->pos.x - 20.0f;
				float speed = 1.5f;
				players[3]->moveInDirection(&target, &speed);
			} else if (timer == 90) {
				peach->stage = 0;
				players[3]->setAnimePlayWithAnimID(dm_escort);
			}
		} else if (peach->stage == 7) {
			// All the players are glad
			timer = 0;
			peach->stage = 9;
			for (int i = 0; i < 4; i++) {
				if (dAcPy_c *player = players[i]) {
					OSReport("Going to dmglad\n");
					OSReport("Player %d states: %s and %s\n", i, player->states2.getCurrentState()->getName(), player->demoStates.getCurrentState()->getName());
					player->setAnimePlayWithAnimID(dm_glad);

					static const int vocs[4] = {
						SE_VOC_MA_SAVE_PRINCESS,
						SE_VOC_LU_SAVE_PRINCESS,
						SE_VOC_KO_SAVE_PRINCESS,
						SE_VOC_KO2_SAVE_PRINCESS
					};
					dPlayerModelHandler_c *pmh = (dPlayerModelHandler_c*)(((u32)player) + 0x2A60);
					int voc = vocs[pmh->mdlClass->player_id_1];
					nw4r::snd::SoundHandle handle;
					PlaySoundWithFunctionB4(SoundRelatedClass, &handle, voc, 1);

					int powerup = *((u32*)( 0x1090 + ((u8*)player) ));
					handle.SetPitch(powerup == 3 ? 1.5f : 1.0f);
				}
			}
		} else if (peach->stage == 9) {
			timer++;
			if (timer == 90) {
				RESTART_CRSIN_LevelStartStruct.purpose = 0;
				RESTART_CRSIN_LevelStartStruct.world1 = 6;
				RESTART_CRSIN_LevelStartStruct.world2 = 6;
				RESTART_CRSIN_LevelStartStruct.level1 = 40;
				RESTART_CRSIN_LevelStartStruct.level2 = 40;
				RESTART_CRSIN_LevelStartStruct.areaMaybe = 0;
				RESTART_CRSIN_LevelStartStruct.entrance = 0xFF;
				RESTART_CRSIN_LevelStartStruct.unk4 = 0; // load replay
				DontShowPreGame = true;
				ExitStage(RESTART_CRSIN, 0, BEAT_LEVEL, MARIO_WIPE);
			}
		}
	}
}




Vec2 dEndingMgr_c::_vf70() {
	// HACK OF THE MILLENIUM
	// DON'T TRY THIS AT HOME.
	Vec2 *v = (Vec2*)this;
	v->x = 1195.0f;
	v->y = -394.0f;
	return (const Vec2){12345.0f, 67890.f};
}