// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Transporter Object
// Script Author: Christan Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 49 : TYPE_TRANSPORTER

// Function declarations
reserve function Transporter_Function116

// Static Values

// Tables
public table Transporter_table43
	6, 2, 6, 2, 2, 6, 6, 6, 6, 6, 6, 6, 6
end table

public table Transporter_table44
	1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0
end table

public table Transporter_table42
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
end table

private table Transporter_table0
	0x7A80000, 0x2700000, 0x7500000, 0x2700000, 0x7400000, 0x2800000, 0x7400000, 0x3E00000, 0x7500000, 0x3F00000, 0x7A80000, 0x3F00000
end table

private table Transporter_table1
	0xC580000, 0x5F00000, 0xE280000, 0x5F00000
end table

private table Transporter_table2
	0x18280000, 0x6B00000, 0x17D00000, 0x6B00000, 0x17C00000, 0x6C00000, 0x17C00000, 0x7E00000, 0x17B00000, 0x7F00000, 0x17580000, 0x7F00000
end table

private table Transporter_table3
	0x5D80000, 0x3700000, 0x7800000, 0x3700000
end table

private table Transporter_table4
	0x5D80000, 0x5F00000, 0x7000000, 0x5F00000
end table

private table Transporter_table5
	0xBD80000, 0x1F00000, 0xC300000, 0x1F00000, 0xC400000, 0x1E00000, 0xC400000, 0xC00000, 0xC500000, 0xB00000, 0xCA80000, 0xB00000
end table

private table Transporter_table6
	0x17280000, 0x3300000, 0x15D00000, 0x3300000, 0x15C00000, 0x3200000, 0x15C00000, 0x2400000, 0x15D00000, 0x2300000, 0x16280000, 0x2300000
end table

private table Transporter_table7
	0x6D80000, 0x1F00000, 0x7300000, 0x1F00000, 0x7400000, 0x1E00000, 0x7400000, 0x1000000, 0x7500000, 0xF00000, 0x7A80000, 0xF00000
end table

private table Transporter_table8
	0x7D80000, 0x3300000, 0x8280000, 0x3300000, 0x8400000, 0x3400000, 0x8400000, 0x4580000, 0x8280000, 0x4700000, 0x7D80000, 0x4700000
end table

private table Transporter_table9
	0xFD80000, 0x3B00000, 0x10280000, 0x3B00000, 0x10400000, 0x3980000, 0x10400000, 0x2C40000, 0x10580000, 0x2B00000, 0x10A80000, 0x2B00000
end table

private table Transporter_table10
	0xFD80000, 0x4B00000, 0x10280000, 0x4B00000, 0x10400000, 0x4C00000, 0x10400000, 0x5D80000, 0x10580000, 0x5F00000, 0x10A80000, 0x5F00000
end table

private table Transporter_table11
	0x20580000, 0x4300000, 0x20A80000, 0x4300000, 0x20C00000, 0x4180000, 0x20C00000, 0x2C00000, 0x20D00000, 0x2B00000, 0x21280000, 0x2B00000
end table

private table Transporter_table12
	0x23280000, 0x5B00000, 0x22D00000, 0x5B00000, 0x22C00000, 0x5A00000, 0x22C00000, 0x4C00000, 0x22D00000, 0x4B00000, 0x23280000, 0x4B00000
end table

function Transporter_Function116
	temp0 = object.value1
	temp2 = 0x1000
	temp0 -= object[currentPlayer].xpos
	if temp0 < 0
		FlipSign(temp0)
		FlipSign(temp2)
	end if
	temp0 >>= 16
	temp1 = object.value2
	temp3 = 0x1000
	temp1 -= object[currentPlayer].ypos
	if temp1 < 0
		FlipSign(temp1)
		FlipSign(temp3)
	end if
	temp1 >>= 16
	if temp1 >= temp0
		temp1 = object.value2
		temp1 -= object[currentPlayer].ypos
		temp1 &= -0x10000
		temp1 /= temp3
		temp0 = object.value1
		temp0 -= object[currentPlayer].xpos
		if temp0 != 0
			temp0 &= -0x10000
			temp0 /= temp1
		end if
		temp0 <<= 8
		temp3 <<= 8
		temp1 >>= 8
		object[currentPlayer].xvel = temp0
		object[currentPlayer].yvel = temp3
		Abs(temp1)
		object.value3 = temp1
	else
		temp0 = object.value1
		temp0 -= object[currentPlayer].xpos
		temp0 &= -0x10000
		temp0 /= temp2
		temp1 = object.value2
		temp1 -= object[currentPlayer].ypos
		if temp1 != 0
			temp1 &= -0x10000
			temp1 /= temp0
		end if
		temp1 <<= 8
		temp2 <<= 8
		temp0 >>= 8
		object[currentPlayer].xvel = temp2
		object[currentPlayer].yvel = temp1
		Abs(temp0)
		object.value3 = temp0
	end if
end function


event ObjectMain
	switch object.state
	case 0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object[currentPlayer].state != PlayerObject_Blank
				temp0 = object[currentPlayer].xpos
				temp0 -= object.xpos
				temp0 += 0x30000
				if object.direction == FLIP_X
					temp0 += 0xA0000
				end if
				if temp0 >= 0
					if temp0 < 0x100000
						temp1 = object[currentPlayer].ypos
						temp1 -= object.ypos
						temp1 += 0x200000
						if temp1 >= 0
							if temp1 < 0x400000
								temp0 = object.propertyValue
								temp0 &= 15
								CreateTempObject(TypeName[Transporter], object.propertyValue, object.xpos, object.ypos)
								GetTableValue(object[tempObjectPos].value4, temp0, Transporter_table42)
								GetTableValue(object[tempObjectPos].value5, temp0, Transporter_table43)
								GetTableValue(temp1, 0, object[tempObjectPos].value4)
								GetTableValue(temp2, 2, object[tempObjectPos].value4)
								if temp2 > temp1
									object[tempObjectPos].direction = FLIP_X
								end if
								object[tempObjectPos].state = 1
								object[tempObjectPos].value0 = currentPlayer
								object[currentPlayer].state = PlayerObject_Blank
								object[currentPlayer].interaction = 0
								object[currentPlayer].tileCollisions = 0
								object[currentPlayer].animation = ANI_JUMPING
								object[currentPlayer].value5 = 0
								object[currentPlayer].animationSpeed = 0
								object[currentPlayer].xvel = 0
								object[currentPlayer].yvel = 0
								object[currentPlayer].gravity = GRAVITY_AIR
								object[currentPlayer].scrollTracking = 1
								object[currentPlayer].xpos = object.xpos
								object[currentPlayer].ypos = object.ypos
								PlaySfx(SfxName[Rolling], 0)
							end if
						end if
					end if
				end if
			end if
		next
		break
	case 1
		currentPlayer = object.value0
		Sin256(object[currentPlayer].ypos, object.angle)
		object.angle += 2
		object[currentPlayer].ypos <<= 11
		FlipSign(object[currentPlayer].ypos)
		object[currentPlayer].ypos += object.ypos
		if object[currentPlayer].value7 < 2
			object[currentPlayer].value7 = 2
		end if
		if object.angle == 128
			temp0 = object.value6
			temp0 <<= 1
			GetTableValue(object.value1, temp0, object.value4)
			temp0++
			GetTableValue(object.value2, temp0, object.value4)
			CallFunction(Transporter_Function116)
			object.state++
			object.frame = 0
			PlaySfx(SfxName[Release], 0)
		end if
		break
	case 2
		currentPlayer = object.value0
		object.value3--
		if object.value3 < 0
			object[currentPlayer].xpos = object.value1
			object[currentPlayer].ypos = object.value2
			object.value6++
			if object.value6 >= object.value5
				object.type = TypeName[Blank Object]
				object[currentPlayer].ypos &= 0x7FF0000
				object[currentPlayer].interaction = 1
				object[currentPlayer].tileCollisions = 1
				GetBit(temp1, object.propertyValue, 4)
				if temp1 == 0
					object[currentPlayer].xvel = 0
					object[currentPlayer].yvel = 0x20000
					object[currentPlayer].speed = 0
					object[currentPlayer].animation = ANI_WALKING
					object[currentPlayer].state = PlayerObject_HandleAir
				else
					object[currentPlayer].xvel = 0x100000
					object[currentPlayer].speed = 0x100000
					object[currentPlayer].state = PlayerObject_HandleGround
				end if
			else
				temp0 = object.value6
				temp0 <<= 1
				GetTableValue(object.value1, temp0, object.value4)
				temp0++
				GetTableValue(object.value2, temp0, object.value4)
				CallFunction(Transporter_Function116)
			end if
		end if
		break
	end switch
	if object.state >= 1
		temp0 = object.animationTimer
		temp0 >>= 1
		GetTableValue(object.frame, temp0, Transporter_table44)
		object.animationTimer++
		if object.animationTimer == 30
			object.animationTimer = 26
		end if
	end if
end event


event ObjectDraw
	DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
end event


event ObjectStartup
	LoadSpriteSheet("MPZ/Objects.gif")
	SpriteFrame(0, 0, 1, 1, 1, 1)
	SpriteFrame(-8, -24, 28, 40, 0, 29)
	SetTableValue(Transporter_table0, 0, Transporter_table42)
	SetTableValue(Transporter_table1, 1, Transporter_table42)
	SetTableValue(Transporter_table2, 2, Transporter_table42)
	SetTableValue(Transporter_table3, 3, Transporter_table42)
	SetTableValue(Transporter_table4, 4, Transporter_table42)
	SetTableValue(Transporter_table5, 5, Transporter_table42)
	SetTableValue(Transporter_table6, 6, Transporter_table42)
	SetTableValue(Transporter_table7, 7, Transporter_table42)
	SetTableValue(Transporter_table8, 8, Transporter_table42)
	SetTableValue(Transporter_table9, 9, Transporter_table42)
	SetTableValue(Transporter_table10, 10, Transporter_table42)
	SetTableValue(Transporter_table11, 11, Transporter_table42)
	SetTableValue(Transporter_table12, 12, Transporter_table42)
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
