/*
	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
*/



//-------------------------------------------------------//
// Pit of quicksand                                      //
//-------------------------------------------------------//

//# address-hook(0x0400ca) end(0x0400ea)
function void fn0400ca()
{
	D2.u16 = u16[A0 + 0x30]
	D3.u16 = objA0.value32

	A1 = 0xffffb000
	D6 = 3		// object.flag.P1_ON_OBJECT
	D5.u16 = control.player1
	fn0400f0()

	A1 = 0xffffb04a
	D6 = 4		// object.flag.P2_ON_OBJECT
	D5.u16 = control.tails
	fn0400f0()

	CheckForUnloadObject()
}


//# address-hook(0x0400f0) end(0x040274)
function void fn0400f0()
{
	u16 dx = objA1.position.x.u16 - objA0.position.x.u16 + D2.u16
	u16 dy = objA1.position.y.u16 - objA0.position.y.u16 + 0x33

	u8 bit = (1 << D6.u8)
	if ((objA0.flags2a & bit) == 0)
	{
		if (dx < D3.u16 && dy < 0x40)
		{
			if (u8[A1 + 0x2a] & char.flag.IN_AIR && (u8[A1 + 0x2a] & char.flag.ON_OBJECT) == 0 && u8[A1 + 0x05] < 0x06 && u8[A1 + 0x2e] == 0)
			{
				if (objA1.velocity.y < 0)
				{
					objA1.velocity.y += 0x68
					return
				}

				u8[A1 + 0x2a] |= char.flag.ON_OBJECT
				objA0.flags2a |= bit
				u8[A1 + 0x1e] = u8[A1 + 0x44]
				u8[A1 + 0x1f] = u8[A1 + 0x45]
				objA1.state = char.state.RUNNING
				objA1.flags2a &= ~(char.flag.ROLLING | char.flag.CONTROL_LOCK)
				u8[A1 + 0x2f] = 0
				u8[A1 + 0x40] = 0
				objA1.velocity.x >>= 1
				objA1.groundspeed = objA1.velocity.x
				objA1.velocity.y >>= 1

				if (u8[A1 + 0x05] != 2)
				{
					u8[A1 + 0x05] = 2
					u8[A1 + 0x34] = 0x78
					u8[A1 + 0x3d] = 0
				}

			#if STANDALONE
				// Fix for spindash bug (if e.g. a moving pillar pushes you into the quicksand)
				u8[A1 + 0x3d] = 0

				// Fix for bug when Tails carries other player and gets caught by the quicksand
				if (u8[A1 + 0x38] == CHARACTER_TAILS && tails.carrying)
				{
					u8[0xffffb000 + 0x2a] |= char.flag.IN_AIR
					u8[0xffffb000 + 0x2e] = 0
				}
			#endif
			}
		}
	}
	else
	{
		if (dx < D3.u16 && dy < 0x40)
		{
			if (u8[A1 + 0x2e] == 0 && u8[A1 + 0x2a] & char.flag.IN_AIR)
			{
				// Check if jump button pressed
				D5.u16 &= CONTROL_ABC
				if (D5.u16 != 0)
				{
					u8[A1 + 0x2a] &= ~char.flag.ON_OBJECT
					objA0.flags2a &= ~bit
					objA1.velocity.y = (u8[A1 + 0x38] == CHARACTER_KNUCKLES) ? -0x600 : -0x680
					objA1.hitbox_extends.y = char.hitbox.y.ROLLING
					objA1.hitbox_extends.x = char.hitbox.x.ROLLING
					objA1.state = char.state.ROLLING
					objA1.flags2a |= char.flag.ROLLING
					objA1.flags2a &= ~char.flag.CONTROL_LOCK
					u8[A1 + 0x2f] = 0
					u8[A1 + 0x40] = 0x01

					playSound(SFX_JUMP)
				}
				else
				{
					objA1.velocity.x >>= 1
					objA1.groundspeed = objA1.velocity.x
					if (objA1.velocity.y < 0)
					{
						objA1.velocity.y += 0x68
					}
					else
					{
						objA1.velocity.y = (objA0.flags2a & 0x02) ? 0xfe68 : 0xa8
					}
				}
				return
			}
		}

		u8[A1 + 0x2a] &= ~char.flag.ON_OBJECT
		objA0.flags2a &= ~bit
	}
}
