/*
	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(0x025d54) end(0x025d96)
function void fn025d54()
{
	objA0.mapping_offset = 0x224b88
	objA0.sprite_attributes = (sprite_attribute.PALETTE.LINE1 | 0x035f)
	objA0.render_flags |= render_flag.WORLD
	objA0.box_size.x = 0x10
	objA0.box_size.y = 0x10
	objA0.sprite_priority = 0x0280
	objA0.render_flags |= render_flag.COMPOUND
	objA0.compound.count = 1
#if STANDALONE
	// This seems to prevent characters balancing on the edges
	//  -> Unfortunately, I did not find any use-case for this, just the code in "checkForBalancing()"
	u8[A0 + 0x2a] |= 0x80
#endif

	A2 = A0 + 0x18
	u16[(A2+=2)-2] = objA0.position.x.u16
	u16[(A2+=2)-2] = objA0.position.y.u16
	u16[(A2+=2)-2] = 0

	objA0.update_address = 0x025d9c
	fn025d9c()
}


//# address-hook(0x025d9c) end(0x025ea0)
function void fn025d9c()
{
	D1.u16 = 0x1b
	D2.u16 = 0x10
	D4.u16 = objA0.position.x.u16
	A2 = 0x025f0e
	fn01dd0e()

	D6 = (D6 << 16) + (D6 >> 16)
	D6.u16 &= 0x33
	if (D6.u16 != 0)
	{
		D0.u8 = D6.u8 & 0x11
		if (D0.u8 != 0)
		{
			A1 = 0xffffb000
		#if STANDALONE
			// Also allow for drop-dashing on the spin wheel, #contributed by iCloudius
			if (objA1.state == char.state.SPINDASH || (u8[A1 + 0x38] == CHARACTER_SONIC && objA1.state.former == char.state.SONIC_DROPDASH))
		#else
			if (objA1.state == char.state.SPINDASH)
		#endif
			{
				u16[A0 + 0x30] = 0x3c
				u8[A0 + 0x32] = 1
				D0.u8 = (u8[A1 + 0x2a] + objA0.flags2a) & 0x01
				if (D0.u8 == 0)
				{
					u8[A0 + 0x32] = 0xff
				}
			#if STANDALONE
				if (objA1.state.former == char.state.SONIC_DROPDASH)
					objA1.state = char.state.SONIC_DROPDASH
			#endif
			}
		}

		D6.u8 &= 0x22
		if (D6.u8 != 0)
		{
			A1 = 0xffffb04a
		#if STANDALONE
			if (objA1.state == char.state.SPINDASH || (u8[A1 + 0x38] == CHARACTER_SONIC && objA1.state.former == char.state.SONIC_DROPDASH))
		#else
			if (objA1.state == char.state.SPINDASH)
		#endif
			{
				u16[A0 + 0x30] = 0x3c
				u8[A0 + 0x32] = 1
				D0.u8 = (u8[A1 + 0x2a] + objA0.flags2a) & 0x01
				if (D0.u8 == 0)
				{
					u8[A0 + 0x32] = 0xff
				}
			#if STANDALONE
				if (objA1.state.former == char.state.SONIC_DROPDASH)
					objA1.state = char.state.SONIC_DROPDASH
			#endif
			}
		}
	}

	if (u16[A0 + 0x30] != 0)
	{
		D0.u16 = objA0.subtype2c & 0x0f
		A3 = 0xfffff7e0 + D0.u16

		--u16[A0 + 0x30]
		if (u16[A0 + 0x30] == 0)
		{
			u8[A3] = 0
			objA0.animation.sprite = 0
		}
		else
		{
			u8[A3] = 1
			if (objA0.flags2a & object.flag.P1_ON_OBJECT)
			{
				A1 = 0xffffb000
				fn025ea6()
			}
			if (objA0.flags2a & object.flag.P2_ON_OBJECT)
			{
				A1 = 0xffffb04a
				fn025ea6()
			}

			--objA0.animation.timer
			if (s8(objA0.animation.timer) < 0)
			{
				objA0.animation.timer = 1
				objA0.compound.sprite1.animation.sprite = (objA0.compound.sprite1.animation.sprite + u8[A0 + 0x32]) & 0x03
			}
			objA0.animation.sprite = (objA0.animation.sprite != 0) ? 0 : 4
		}
	}

	DrawOrUnloadObject()
}


//# address-hook(0x025ea6) end(0x025f0c)
function void fn025ea6()
{
	D1.u16 = objA0.position.x.u16 - 0x10
	if (objA0.flags2a & 0x01)
	{
		D1.u16 += 0x20
	}
	D2.u16 = objA0.position.y.u16 + 0x10
	D1.u16 -= objA1.position.x.u16
	D2.u16 -= objA1.position.y.u16
	LookupAngleByVector()
	LookupSinCos()
	D1.s32 = (s32(D1.s16) * -0x0700) >> 8
	objA1.velocity.x = D1.u16
	D0.s32 = (s32(D0.s16) * -0x0700) >> 8
	objA1.velocity.y = D0.u16
	u8[A1 + 0x2a] |= char.flag.IN_AIR
	objA1.flags2a &= ~(char.flag.CONTROL_LOCK | char.flag.ON_OBJECT)
#if STANDALONE
	if (objA1.state == char.state.SONIC_DROPDASH)
	{
		u8[A1 + 0x40] = 1	// Re-enable double jump state
		u8[A1 + 0x2f] = 1	// Re-enable jump flag
	}
	else
#endif
	{
		u8[A1 + 0x40] = 0
	}
	u8[A1 + 0x3d] = 0
	playSound(0x7b)
}
