/*
	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(0x034800) end(0x0348fa)
function void fn034800()
{
	objA0.mapping_offset = 0x2340c0
	objA0.sprite_attributes = (sprite_attribute.PALETTE.LINE1 | 0x035f)
	objA0.render_flags |= render_flag.WORLD
	objA0.box_size.x = 0x20
	objA0.box_size.y = 0x20
	objA0.sprite_priority = 0x0280
	objA0.render_flags |= render_flag.COMPOUND
	objA0.compound.count = 1

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

	D0 = objA0.subtype2c * 8
	u16[A0 + 0x40] = D0.u16
	u16[A0 + 0x3c] = D0.u16

	if (allocDynamicObjectAfterA0())
	{
		objA1.update_address = 0x034bee
		objA1.mapping_offset = objA0.mapping_offset
		objA1.sprite_attributes = objA0.sprite_attributes
		objA1.render_flags = objA0.render_flags
		objA1.box_size.x = 0x60
		objA1.box_size.y = 0xc0
		objA1.sprite_priority = 0x0300
		objA1.position.x.u16 = objA0.position.x.u16 + ((objA0.flags2a & 0x01) ? 0x78 : -0x78)
		u16[A1 + 0x12] = objA0.position.x.u16
		objA1.position.y.u16 = objA0.position.y.u16 + 0xd0
		objA1.render_flags |= render_flag.COMPOUND
		objA1.compound.count = 0x08
		A2 = A1 + 0x18

		D2.u16 = objA0.position.x.u16 - 0x24
		D3.u16 = objA0.position.y.u16 + 0x28
		for (u8 i = 0; i < 8; ++i)
		{
			u16[A2]   = D2.u16
			u16[A2+2] = D3.u16
			u16[A2+4] = 5
			D2.u16 -= 0x18
			D3.u16 += 0x30
			A2 += 6
		}
		u16[A2-2] = 6
		objA0.value3e = A1.u16
	}

	objA0.update_address = 0x034900
	fn034900()
}


//# address-hook(0x034900) end(0x03499c)
function void fn034900()
{
	bool goto034966 = false

	D4 = 0
	if (objA0.flags38 == 0)
	{
		D0.u16 = u16[A0 + 0x3c]
		if (D0.u16 == u16[A0 + 0x40])
		{
			objA0.animation.sprite = 0
		}
		else
		{
			if (D0.u16 < u16[A0 + 0x40])
			{
				u16[A0 + 0x3c] += 2
				D4.u8 = -1
			}
			else
			{
				u16[A0 + 0x3c] -= 2
				D4.u8 = 1
			}

			goto034966 = true
		}
	}
	else if (u16[A0 + 0x34] == 0)
	{
		if (u16[A0 + 0x3c] == 0)
		{
			objA0.animation.sprite = 0
		}
		else
		{
			u16[A0 + 0x3c] -= 4
			D4.u8 = 1

			if (s16[A0 + 0x3c] < 0)
				u16[A0 + 0x3c] = 0

			goto034966 = true
		}
	}
	else
	{
		--u16[A0 + 0x34]
		if (u16[A0 + 0x34] >= 0x0c)
		{
			D0.u16 = u16[A0 + 0x40] + 8
			if (D0.u16 != u16[A0 + 0x3c])
			{
				u16[A0 + 0x3c] += 2
				D4.u8 = -1

				goto034966 = true
			}
		}
	}

	if (goto034966)
	{
		--objA0.animation.timer
		if (s8(objA0.animation.timer) < 0)
		{
			objA0.animation.timer = 1
			objA0.compound.sprite1.animation.sprite = (objA0.compound.sprite1.animation.sprite + D4.u8) & 0x03
		}

		if (objA0.animation.sprite == 0)
		{
			if (D4.u16 != 0)
			{
				objA0.animation.sprite = 4
			}
		}
		else
		{
			objA0.animation.sprite = 0
		}
	}
	else
	{
		objA0.animation.sprite = 0
	}

	fn034b22()
	fn0349a2()
	DrawOrUnloadObject()
}


//# address-hook(0x0349a2) end(0x034b20)
function void fn0349a2()
{
	A2 = A0 + 0x38
	A1 = 0xffffb000
	D0.u16 = control.pad1
	fn0349ba()

	++A2
	A1 = 0xffffb04a
	D0.u16 = control.pad2
	fn0349ba()
}


function void fn0349ba()
{
	if (u8[A2] != 0)
	{
		if ((objA1.render_flags & render_flag.VISIBLE) && u8[A1 + 0x05] < 0x04)
		{
			// Check if jump button pressed
			if ((D0.u8 & CONTROL_ABC) == 0)
			{
				D2.u16 = objA0.position.x.u16 - 0x26
				D3.u16 = objA0.position.y.u16 + 0x2e
				D0.u16 = u16[A0 + 0x3c]
				D3.u16 += D0.u16
				D0.u16 >>= 1
				if (objA0.flags2a & 0x01)
				{
					D2.u16 += 0x4c
					D0.s16 = -D0.s16
				}
				D2.u16 -= D0.u16
				objA1.position.x.u16 = D2.u16
				objA1.position.y.u16 = D3.u16
				return
			}

			// Jump off
			objA1.velocity.x = (objA0.flags2a & 0x01) ? 0x400 : -0x400
			objA1.velocity.y = -0x600
			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.IN_AIR | char.flag.ROLLING)

			#if STANDALONE
				// Set jump flag to allow for immediate mid-jump action
				u8[A1 + 0x40] = 1
			#endif
		}

		u8[A1 + 0x2e] = 0
		u8[A2] = 0
		u8[A2 + 0x02] = 0x3c
	}
	else
	{
		if (u8[A2 + 0x02] != 0)
		{
			--u8[A2 + 0x02]
			if (u8[A2 + 0x02] != 0)
			{
				return
			}
		}

		D2.u16 = objA0.position.x.u16 - 0x26
		D3.u16 = objA0.position.y.u16 + 0x2e
		D0.u16 = u16[A0 + 0x3c]
		D3.u16 += D0.u16
		D0.u16 >>= 1
		if (objA0.flags2a & 0x01)
		{
			D2.u16 += 0x4c
			D0.s16 = -D0.s16
		}
		D2.u16 -= D0.u16

		D0.u16 = objA1.position.x.u16 - D2.u16 + 0x0c
		D1.u16 = objA1.position.y.u16 - D3.u16 + 0x0c
		if (D0.u16 < 0x18 && D1.u16 < 0x18)
		{
			if (u8[A1 + 0x2e] == 0 && u8[A1 + 0x05] < 0x04 && debug_mode.state == 0)
			{
				D0.u16 = objA1.velocity.x
				if (objA0.flags2a & 0x01)
					D0.s16 = -D0.s16

				if (D0.s16 < 0)
				{
					objA1.velocity.x = 0
					objA1.velocity.y = 0
					objA1.groundspeed = 0
					objA1.position.x.u16 = D2.u16
					objA1.position.y.u16 = D3.u16
					objA1.state = char.state.PULLEY
					u8[A1 + 0x2e] = control_flag.DISABLE_UPDATE
					if (objA0.flags2a & 0x01)
						u8[A1 + 0x2a] |= 0x01
					else
						u8[A1 + 0x2a] &= ~0x01
					u16[A0 + 0x34] = 0x10
					u8[A2] = 1

					playSound(0x64)
				}
			}
		}
	}
}


//# address-hook(0x034b22) end(0x034bec)
function void fn034b22()
{
	s8 sign = (objA0.flags2a & 0x01) ? 1 : -1

	A1 = 0xffff0000 + objA0.value3e
	D0 = u16[A0 + 0x3c] + 0x18
	u16 numSections = D0 / 0x30
	u16 remainder = D0 % 0x30

	objA1.compound.count = numSections
	D1.u16 = numSections

	A2 = A1 + 0x18
	u16 px = objA0.position.x.u16 + 0x0c * sign
	u16 py = objA0.position.y.u16 - 0x08
	if (D1.u16 == 8)
	{
		remainder = 0x30
		D1 = 7
	}
	else
	{
		++objA1.compound.count
	}

	py += remainder
	px += (remainder >> 1) * sign
#if STANDALONE
	// Add the compound object's sub-sprites in reverse order, which works better for frame interpolation
	A2 += D1.u8 * 6
	for (u8 i = 0; i <= D1.u8; ++i)
	{
		u16[A2]   = px
		u16[A2+2] = py
		u16[A2+4] = 5
		px += 0x18 * sign
		py += 0x30
		A2 -= 6
	}
	A2 = A1 + 0x18 + 6
#else
	for (u8 i = 0; i <= D1.u8; ++i)
	{
		u16[A2]   = px
		u16[A2+2] = py
		u16[A2+4] = 5
		px += 0x18 * sign
		py += 0x30
		A2 += 6
	}
#endif
	u16[A2-2] = 6
}


//# address-hook(0x034bee) end(0x034bf2)
function void fn034bee()
{
	D0.u16 = u16[A0 + 0x12]
	DrawOrUnloadObjectByD0()
}
