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



// Goal Post object

//# address-hook(0x036be0) end(0x036ce4)
function void fn036be0()
{
	objA0.mapping_offset = 0x036ffa
	objA0.sprite_attributes = 0x06bc
	objA0.render_flags |= render_flag.WORLD
	objA0.sprite_priority = 0x0280
	objA0.box_size.x = 0x08
	objA0.box_size.y = 0x20
	u16[A0 + 0x30] = objA0.position.x.u16
	objA0.value32 = objA0.position.y.u16
	u16[A0 + 0x38] = u16(objA0.subtype2c) * 8 - 0x14
	competition_mode.laps = 0x05

	// competition_mode.current_lap.player1 = 0
	// competition_mode.current_lap.player2 = 0
	u16[0xfffffedc] = 0

	u16[0xffffeee6] = 0
	hud.dirty.timer = 0
	timer.alldata = 0
	hud.dirty.timer.player2 = 0
	timer.alldata.player2 = 0
	ring_counter = 0
	ring_counter.player2 = 0

	fn036f5a()

	if (allocDynamicObjectAfterA0())
	{
		objA1.update_address = 0x037ec2		// Spawn Item Display for player 1
		if (allocDynamicObjectAfterA0())
		{
			objA1.update_address = 0x037ec2		// Spawn Item Display for player 2
			u8[A1 + 0x2c] = 0x01
			if (allocDynamicObjectAfterA0())
			{
				objA1.update_address = 0x03703a		// Spawn lap Display for player 1
				if (allocDynamicObjectAfterA0())
				{
					objA1.update_address = 0x03703a		// Spawn lap Display for player 1
					u8[A1 + 0x2c] = 0x01
					if (allocDynamicObjectAfterA0())
					{
						objA1.update_address = 0x03713a		// Spawn "finish" neon text
						u16[A1 + 0x10] = 0x0120 + getScreenExtend()
						u16[A1 + 0x14] = 0xb8
						if (allocDynamicObjectAfterA0())
						{
							objA1.update_address = 0x037c8e		// Spawn time Display for player 1
							if (allocDynamicObjectAfterA0())
							{
								objA1.update_address = 0x037d00		// Spawn time Display for player 2
								if (allocDynamicObjectAfterA0())
								{
									objA1.update_address = 0x03827a
								}
							}
						}
					}
				}
			}
		}
	}

	objA0.update_address = 0x036cea
	fn036cea()
}


//# address-hook(0x036cea) end(0x036d38)
function void fn036cea()
{
	D1.u16 = objA0.position.x.u16
	A2 = A0 + 0x34

	A1 = 0xffffb000
	A3 = 0xfffffedc
	A4 = 0xfffff74a
	A5 = 0xfffffe22
	A6 = 0xffff7828
	fn036dfc()

	A1 = 0xffffb04a
	A3 = 0xfffffedd
	A4 = 0xfffff74b
	A5 = 0xfffffed2
	A6 = 0xffff7840
	fn036dfc()

	Object.animationUpdate(0x036ff2)
	u16[0xffffee5c] = u16[A0 + 0x36]
	DrawObject()
}


//# address-hook(0x036d3e) end(0x036df6)
function void fn036d3e()
{
	// This gets entered on "Fault"

	--u16[A0 + 0x3c]
	if (u16[A0 + 0x3c] == 0)
	{
		A1 = 0xffffb000
		u16[A1 + 0x10] = checkpoint.x
		u16[A1 + 0x14] = checkpoint.y
		objA1.sprite_attributes = checkpoint.sprite_attr
		u16[A1 + 0x46] = checkpoint.layer_flags
		objA1.velocity.x = 0
		objA1.velocity.y = 0
		objA1.groundspeed = 0
		u8[A1 + 0x2a] = 0x02
		u16[A1 + 0x32] = 0

		if (competition_mode.multiplay)
		{
			A1 = 0xffffb04a
			u16[A1 + 0x10] = level.backup.x
			u16[A1 + 0x14] = level.backup.y
			objA1.sprite_attributes = level.backup.sprite_attr
			u16[A1 + 0x46] = level.backup.layer_flags
			objA1.velocity.x = 0
			objA1.velocity.y = 0
			objA1.groundspeed = 0
			u8[A1 + 0x2a] = 0x02
			u16[A1 + 0x32] = 0
		}

		u16[0xfffffedc] = 0
		hud.dirty.timer = 0
		timer.alldata = 0
		hud.dirty.timer.player2 = 0
		timer.alldata.player2 = 0
		player1.control_override = 0
		player2.control_override = 0
		fn036f5a()
		u16[0xffffeee6] = 0
		objA0.update_address = 0x036cea
	}

	Object.animationUpdate(0x036ff2)
	DrawObject()
}


//# address-hook(0x036dfc) end(0x036f58)
function void fn036dfc()
{
	if (u8[A4] != 0)
	{
		u8[A4] = 0
		u8[(A2+=1)-1] = 0
		if (u8[A3] > 0)
			--u8[A3]
	}
	else
	{
		if (u8[(A2+=1)-1] != 0)
		{
			if (D1.u16 <= u16[A1 + 0x10])
				return

			u8[A2 - 0x01] = 0
			D2.u16 = objA0.position.y.u16 - u16[A0 + 0x38]
			D3.u16 = objA0.position.y.u16 + 0x14
			D4.u16 = u16[A1 + 0x14]
			if (D4.s16 < D2.s16 || D4.s16 >= D3.s16)
				return

			D2.u16 = abs(s16(u16[A1 + 0x10] - D1.u16))
			if (D2.u16 >= 0x20 || u8[A1 + 0x46] != 0x0c)
				return

			if (u8[A3] > 0)
				--u8[A3]
		}
		else
		{
			if (D1.u16 > u16[A1 + 0x10])
				return

			u8[A2 - 0x01] = 1
			D2.u16 = objA0.position.y.u16 - u16[A0 + 0x38]
			D3.u16 = objA0.position.y.u16 + 0x14
			D4.u16 = u16[A1 + 0x14]
			if (D4.s16 < D2.s16 || D4.s16 >= D3.s16)
				return

			D2.u16 = abs(s16(u16[A1 + 0x10] - D1.u16))
			if (D2.u16 >= 0x20 || u8[A1 + 0x46] != 0x0c)
				return

			D0.u8 = u8[A3]
			if (D0.u8 == 0)
			{
				if (u8[A2 + 0x01] != 0 || hud.dirty.timer != 0)
				{
					++u8[A3]
					return
				}

				player1.control_override = 0x01
				player2.control_override = 0x01
				control.player1 = 0
				control.tails = 0
				objA0.update_address = 0x036d3e
				u16[A0 + 0x3c] = 0x78
				u8[0xffffeee6] = 0xff

				A6 = A1
				if (allocDynamicObjectAfterA0())
				{
					// "Fault" neon text
					objA1.update_address = 0x0374b8
					objA1.render_flags = render_flag.COMPETITION_P1_SCREEN
					if (A6.u16 != 0xb000)
					{
						objA1.render_flags ^= (render_flag.COMPETITION_P1_SCREEN | render_flag.COMPETITION_P2_SCREEN)
					}
					playSound(0xb2)
				}
			}
			else
			{
				if (D0.u8 > u8[A2 + 0x01])
				{
					u8[A2 + 0x01] = D0.u8
					--D0.u8
					if (D0.s8 >= 0 && D0.u8 < 0x05)
					{
						D5 = u32[A6 + D0.u8 * 4]
						D6 = u32[A6 + 0x14]
						fn036f76()
						u32[A6 + 0x14] = D6
						u32[A5] = 0
					}

					D4.u16 = objA0.position.y.u16 - D4.u16
					if (D4.s16 < 0x14)
					{
						D0.u16 = u16[A0 + 0x3a] & 0x0f
						fn036faa()
						u16[A0 + 0x3a] += 2
					}
				}
				++u8[A3]
			}
		}
	}
}


//# address-hook(0x036f5a) end(0x036f74)
function void fn036f5a()
{
	zeroMemory(0xffff7828, 0x30)
	competition_mode.time_record.player1 = 0
	competition_mode.time_record.player2 = 0
	competition_mode.timeattack.new_record = 0xff
}


//# address-hook(0x036f76) end(0x036fa8)
function void fn036f76()
{
	D6.u8 += D5.u8
	D3 = 0
	if (D6.u8 >= 100)
	{
		D6.u8 -= 100
		D3 = 1
	}
	D5 = (D5 >> 8) + (D5 << 24)
	D6 = (D6 >> 8) + (D6 << 24)
	D6.u8 += D3.u8 + D5.u8
	D3 = 0
	if (D6.u8 >= 60)
	{
		D6.u8 -= 60
		D3 = 1
	}
	D5 = (D5 >> 8) + (D5 << 24)
	D6 = (D6 >> 8) + (D6 << 24)
	D6.u8 += D3.u8 + D5.u8
	D5 = (D5 << 16) + (D5 >> 16)
	D6 = (D6 << 16) + (D6 >> 16)
}


//# address-hook(0x036faa) end(0x036fd8)
function void fn036faa()
{
	if (competition_mode.item_flag != 0)
		return

	A4 = 0x036fda + D0.u16
	A1 = 0xffffb0de
	D0 = 0x59
	while (D0.s16 >= 0)
	{
		A1 += 0x4a
		if (objA1.update_address == 0x0363d0)
		{
			u8[A1 + 0x20] = u8[(A4+=1)-1]
			u8[A1 + 0x28] = (collision.flag.SPECIAL | collision.size.6x6)
		}
		--D0.s16
	}
}
