/*
	This script file is part of the Sonic 3 A.I.R. script software distribution.
	Copyright (C) 2017-2024 by Eukaryot

	Published under the GNU GPLv3 open source software license, see license.txt
	or https://www.gnu.org/licenses/gpl-3.0.en.html
*/



//# address-hook(0x0624de) end(0x0624ec)
function void fn0624de()
{
#if STANDALONE
	// Skip Knuckles in Time Attack
	if (Game.isTimeAttack())
	{
		UnloadObject()
		return
	}
#endif

	if (isMainCharacter(CHARACTER_KNUCKLES))
	{
		UnloadObject()
		return
	}

	A1 = 0x062520
	if (InitBoss(0x0624f2))
		return

	fn0624f2()
}


//# address-hook(0x0624f2) end(0x06250a)
function void fn0624f2()
{
	// Targets:
	//  - 0x062528	-> objA0.base_state = 0x00	-> Initialization
	//  - 0x06254e	-> objA0.base_state = 0x02	-> Waiting for player to reach the right place
	//  - 0x06256c	-> objA0.base_state = 0x04	-> Move the player forward a bit
	//  - 0x062014	-> objA0.base_state = 0x06	-> Waiting time before Knuckles starts
	//  - 0x0620aa	-> objA0.base_state = 0x08	-> Knuckles jumps onto the button and back, triggers transporter in between
	//  - 0x0625be	-> objA0.base_state = 0x0a	-> Commence chuckling
	//  - 0x0625e2	-> objA0.base_state = 0x0c	-> Walking away
	//  - 0x06261a	-> objA0.base_state = 0x0e	-> Sonic being taken by the transporter
	call tableLookupAddress(0x062510, objA0.base_state)

	Object.loadSpritePatternsForVDP(0x06666a)
	fn085208()
}


//# address-hook(0x062528) end(0x062548)
function void fn062528()
{
	if (SetupObjectAttributesSlotted(0x0663ba))
		return

	control.player1 = 0
	player1.control_override = 0xff
	u8[0xffffb000 + 0x2e] = control_flag.DISABLE_INTERACTION
	fn065dd6()

#if STANDALONE
	Game.startSkippableCutscene()
#else
	// Load Knuckles' palette -- not needed in standalone
	CutsceneKnuckles.loadPalette()
#endif
}


//# address-hook(0x06254e) end(0x06256a)
function void fn06254e()
{
	A1 = 0xffffb000
	if (objA1.position.x.u16 >= 0x4728 && u8[A1 + 0x2a] & char.flag.IN_AIR)
	{
		objA0.base_state = 0x04
	}
}


//# address-hook(0x06256c) end(0x0625a4)
function void fn06256c()
{
	control.player1 = 0x0808		// CONTROL_RIGHT, CONTROL_RIGHT

	if (u16[0xffffb000 + 0x10] < 0x4760)
		return

	objA0.base_state = 0x06
	control.player1 = 0

	A1 = 0xffffb000
	objA1.velocity.x = 0
	objA1.velocity.y = 0
	objA1.groundspeed = 0

	objA0.countdown_value = 31
	objA0.countdown_callback = 0x0625a6
}


//# address-hook(0x0625a6) end(0x0625ba)
function void fn0625a6()
{
	// Start Knuckles jumping back and forth on button
	objA0.base_state = 0x08
	objA0.countdown_value = 127
	objA0.countdown_callback = 0x0625ca
	fn062080()
}


//# address-hook(0x0625be) end(0x0625c4)
function void fn0625be()
{
	Object.AnimationProgressByLocalA1()
	Object.CountdownAndTrigger()
}


//# address-hook(0x0625ca) end(0x0625e0)
function void fn0625ca()
{
	// Knuckles runs away
	objA0.base_state = 0x0c
	objA0.animation.frame = 0
	objA0.animation.timer = 0
	u32[A0 + 0x30] = 0x06669f		// Cutscene Knuckles walking animation data
}


//# address-hook(0x0625e2) end(0x0625f4)
function void fn0625e2()
{
	objA0.position.x.u16 += 4
	Object.AnimationProgressByLocalA1()

	if ((objA0.render_flags & render_flag.VISIBLE) == 0)
	{
		objA0.base_state = 0x0e
		fn0625fa()
	}
}


//# address-hook(0x0625fa) end(0x062618)
function void fn0625fa()
{
	u8[0xffffb000 + 0x2e] = 0

	// Reset palette after Knuckles is off screen
	loadPaletteLine1(0x0a8fbc)

	if (allocDynamicObjectStd())
	{
		// Create the object that fades out the Knuckles theme and starts the level music afterwards
		objA1.update_address = 0x085b0a
	}
}


//# address-hook(0x06261a) end(0x06263a)
function void fn06261a()
{
	// Sonic walks left into the transporter
	control.player1 = 0x0404		// CONTROL_LEFT, CONTROL_LEFT

	D0.u16 = camera.position.y.u16 + 0x0160
	if (D0.u16 >= objA0.position.y.u16)
		return

#if STANDALONE
	Game.endSkippableCutscene()
#endif

	player1.control_override = 0
	fn0851e4()
	Object.TriggerUnloading()
}
