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



//-------------------------------------------------------//
// Rotating Pillar                                       //
//-------------------------------------------------------//

//# address-hook(0x032656) end(0x0326b0)
function void fn032656()
{
	objA0.mapping_offset = 0x231aee
	objA0.sprite_attributes = (sprite_attribute.PALETTE.LINE2 | 0x0040)
	objA0.render_flags = render_flag.WORLD
	objA0.sprite_priority = 0x0280
	objA0.box_size.x = 0x10
	objA0.box_size.y = 0x20
	objA0.countdown_value = objA0.position.x.u16
	u16[A0 + 0x30] = objA0.position.y.u16
	u16[A0 + 0x3a] = (objA0.subtype2c * 2) & 0x06
	D1.u16 = objA0.subtype2c & 0xf0
	u16[A0 + 0x40] = D1.u16
	objA0.value42 = (D1.u16 == 0xe0) ? -1 : 1

	objA0.update_address = 0x0326b6
	fn0326b6()
}


//# address-hook(0x0326b6) end(0x03270c)
function void fn0326b6()
{
	fn032712()

	A2 = A0 + 0x32
	A1 = 0xffffb000
	D6 = 3		// object.flag.P1_ON_OBJECT
	D5.u16 = control.player1
	fn032784()

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

	D1.u16 = 0x1b
	D2.u16 = 0x20
	D3.u16 = 0x21
	D4.u16 = objA0.position.x.u16
	fn01dc56()

	--objA0.animation.timer
	if (s8(objA0.animation.timer) < 0)
	{
		objA0.animation.timer = 7
		--objA0.animation.sprite
		if (objA0.animation.sprite == 0xff)
			objA0.animation.sprite = 2
	}

	D0.u16 = objA0.countdown_value
	DrawOrUnloadObjectByD0()
}


//# address-hook(0x032712) end(0x03271a)
function void fn032712()
{
	// Targets:
	//  - 0x032724	-> u16[A0 + 0x3a] = 0x00
	//  - 0x032726	-> u16[A0 + 0x3a] = 0x02
	//  - 0x032766	-> u16[A0 + 0x3a] = 0x04
	call tableLookupAddress(0x03271e, u16[A0 + 0x3a])
}


//# address-hook(0x032726) end(0x032764)
function void fn032726()
{
	D1.u16 = objA0.value42
	D0.u16 = u16[A0 + 0x40] + D1.u16
	if (D1.s16 >= 0)
	{
		if (D0.u16 == 0xe0)
		{
			s16[A0 + 0x42] = -s16[A0 + 0x42]
		}
	}
	else
	{
		if (D0.u16 == 0)
		{
			s16[A0 + 0x42] = -s16[A0 + 0x42]
		}
	}

	u16[A0 + 0x40] = D0.u16
	D0.u16 -= 0x70
	if (objA0.flags2a & 0x01)
		D0.s16 = -D0.s16
	D0.u16 += objA0.countdown_value
	objA0.position.x.u16 = D0.u16
}


//# address-hook(0x032766) end(0x032782)
function void fn032766()
{
	D0 = u8[0xfffffe8c]
	if (objA0.flags2a & 0x01)
	{
		D0.s16 = 0x80 - D0.s16
	}
	D0.u16 += u16[A0 + 0x30]
	objA0.position.y.u16 = D0.u16
}


//# address-hook(0x032784) end(0x0328c2)
function void fn032784()
{
#if STANDALONE
	if (debug_mode.state)
		return
#endif

	u8 bit = (1 << D6.u8)
	D0.u8 = u8[A2]
	if (D0.u8 == 0)
	{
		if (objA0.flags2a & bit)
		{
			u8[A2 + 0x01] = 0
			D0.u16 = objA1.position.x.u16 - objA0.position.x.u16
			if (D0.s16 < 0)
			{
				D0.s16 = -D0.s16
				u8[A2 + 0x01] = 0x80
			}
			u8[A2 + 0x02] = D0.u8

			objA1.velocity.x = 0
			objA1.velocity.y = 0
			objA1.groundspeed = 0
			u8[A1 + 0x1e] = u8[A1 + 0x44]
			u8[A1 + 0x1f] = u8[A1 + 0x45]
			u8[A1 + 0x20] = 0
			objA1.flags2a &= ~(char.flag.IN_AIR | char.flag.ROLLING | char.flag.CONTROL_LOCK | char.flag.PUSHING)
			u8[A1 + 0x2e] = (control_flag.DISABLE_ANIMATION | control_flag.DISABLE_UPDATE)
			u8[A1 + 0x40] = 0

			u8[A2] = 1
			fn03260a()
		}
		return
	}

	if ((objA1.render_flags & render_flag.VISIBLE) && u8[A1 + 0x05] < 0x04)
	{
		if ((objA0.flags2a & bit) == 0)
		{
			u8[A2] = 0
			return
		}

		if (u8[A2 + 0x02] != 0)
		{
			--u8[A2 + 0x02]
		}

		D0 = u8[A2 + 0x01]
		LookupSinCos()
		D0.u16 = (D0.s16 + 0x0100) >> 2
		u8[A2 + 0x03] = D0.u8

		D2 = u16[A2 + 0x02]
		D1.s32 = s32(D1.s16) * D2.s16
	#if STANDALONE
		bool negative = D1.s32 < 0 				// Prevents the game from shifting the characters over a pixel
		D1.s32 = (negative) ? -D1.s32 : D1.s32	// everytime the game mirrors the rotation animation; #contributed by iCloudius
		D1 >>= 16
		D1.u16 = (negative) ? -D1.u16 : D1.u16
	#else
		D1 = (D1 << 16) + (D1 >> 16)
	#endif
		D1.u16 += objA0.position.x.u16
		objA1.position.x.u16 = D1.u16
		u8[A2 + 0x01] += 2
		objA1.groundspeed = 0
		D0.u16 = objA0.velocity.y
		if (D0.s16 < 0)
			D0.s16 = -D0.s16

		if ((u8[A1 + 0x2a] & char.flag.IN_AIR) == 0)
		{
			if (D0.u16 >= 0x0480)
			{
				objA1.groundspeed = 0x800
			}
		}

		// Check if jump button pressed
		D5.u8 &= CONTROL_ABC
		if (D5.u8 == 0)
		{
			fn03260a()
			return
		}

		u8[A1 + 0x40] = 0x01
		objA1.hitbox_extends.y = char.hitbox.y.ROLLING
		objA1.hitbox_extends.x = char.hitbox.x.ROLLING
		objA1.state = char.state.ROLLING
		u8[A1 + 0x2a] |= char.flag.ROLLING
		objA1.velocity.y = objA0.velocity.y - 0x680
		objA1.velocity.x = 0
		objA1.groundspeed = 0
	}

	u8[A1 + 0x2a] |= char.flag.IN_AIR
	objA1.sprite_priority = 0x0100
	u8[A1 + 0x2e] = 0
	u8[A2] = 0
}
