// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Transport Tube Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// ========================
// Aliases
// ========================

private alias object.value0  : object.targetPlayer
private alias object.value1  : object.newPos.x
private alias object.value2  : object.newPos.y
private alias object.value3  : object.timer
private alias object.value7  : object.hitboxRight
private alias object.value8  : object.tubeType
private alias object.value9  : object.transportPath
private alias object.value10 : object.transportTable
private alias object.value11 : object.tablePos
private alias object.value12 : object.transportDelay

private alias 0 : TRANSPORTTUBE_AWAITPLAYER
private alias 1 : TRANSPORTTUBE_BLANK
private alias 2 : TRANSPORTTUBE_HASPLAYER
private alias 3 : TRANSPORTTUBE_TRANSPORTING
private alias 4 : TRANSPORTTUBE_EXITNODE

// Player Aliases
private alias object.type : player.type
private alias object.state : player.state
private alias object.xpos : player.xpos
private alias object.ypos : player.ypos
private alias object.xvel : player.xvel
private alias object.yvel : player.yvel
private alias object.speed : player.speed
private alias object.gravity : player.gravity
private alias object.animationSpeed : player.animationSpeed
private alias object.animation : player.animation
private alias object.interaction : player.interaction
private alias object.scrollTracking : player.scrollTracking
private alias object.tileCollisions : player.tileCollisions
private alias object.value1  : player.timer
private alias object.value5  : player.rollAnimationSpeed
private alias object.value18 : player.sortedDrawOrder

// ========================
// Function Declarations
// ========================

reserve function TransportTube_HandleVelChange

// ========================
// Tables
// ========================

private table TransportTube_sizeTable
	160, 256, 288
end table

private table TransportTube_pathsTable
	2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 0, 1, 2, 1
end table

private table TransportTube_transportDelays1
	29, 23, 29, 23, 28, 23, 28, 23, 29, 23, 29, 23
end table

private table TransportTube_transportDelays2
	5, 5, 10, 5, 5, 5, 4, 6, 6, 4, 4, 6, 5, 3, 7
end table

private table TransportTube_nodeFlagsTable
	2, 1, 0, 0, -1, 3, 0, 0, 4, -2, 0, 0, -3, -4, 0, 0
	-5, -5, 0, 0, 7, 6, 0, 0, -7, -6, 0, 0, 8, 9, 0, 0
	-8, -9, 0, 0, 11, 10, 0, 0, 12, 0, 0, 0, -11, -10, 0, 0
	-12, 0, 0, 0, 0, 13, 0, 0, -13, 14, 0, 0, 0, -14, 0, 0
end table

private table TransportTube_transportTables1[12]

private table TransportTube_transportTables2[15]

// Lots of large tables here...
// They store all

private table TransportTube_transportPath1_0
	0x900000, 0x100000
	0x900000, 0x700000
	0x400000, 0x700000
	0x350000, 0x6F0000
	0x280000, 0x6A0000
	0x1E0000, 0x620000
	0x150000, 0x580000
	0x110000, 0x4A0000
	0x100000, 0x400000
	0x110000, 0x350000
	0x150000, 0x270000
	0x1E0000, 0x1E0000
	0x280000, 0x150000
	0x350000, 0x110000
	0x400000, 0x100000
	0x500000, 0x100000
	0x5E0000, 0x120000
	0x680000, 0x180000
	0x6D0000, 0x240000
	0x700000, 0x300000
	0x6D0000, 0x3D0000
	0x680000, 0x480000
	0x5E0000, 0x4E0000
	0x500000, 0x500000
	0x300000, 0x500000
	0x220000, 0x520000
	0x170000, 0x5A0000
	0x110000, 0x630000
	0x100000, 0x700000
end table

private table TransportTube_transportPath1_1
	0x900000, 0x100000
	0x900000, 0x700000
	0x400000, 0x700000
	0x2E0000, 0x6E0000
	0x1D0000, 0x620000
	0x130000, 0x530000
	0x100000, 0x400000
	0x130000, 0x2D0000
	0x1D0000, 0x1E0000
	0x2E0000, 0x130000
	0x400000, 0x100000
	0x580000, 0x100000
	0x640000, 0x140000
	0x6C0000, 0x1A0000
	0x700000, 0x280000
	0x6C0000, 0x360000
	0x640000, 0x3C0000
	0x580000, 0x400000
	0x4B0000, 0x3D0000
	0x400000, 0x380000
	0x360000, 0x320000
	0x280000, 0x300000
	0x100000, 0x300000
end table

private table TransportTube_transportPath1_2
	0x100000, 0x700000
	0x110000, 0x630000
	0x170000, 0x5A0000
	0x220000, 0x520000
	0x300000, 0x500000
	0x500000, 0x500000
	0x5E0000, 0x4E0000
	0x680000, 0x480000
	0x6D0000, 0x3D0000
	0x700000, 0x300000
	0x6D0000, 0x240000
	0x680000, 0x180000
	0x5E0000, 0x120000
	0x500000, 0x100000
	0x400000, 0x100000
	0x350000, 0x110000
	0x280000, 0x150000
	0x1E0000, 0x1E0000
	0x150000, 0x270000
	0x110000, 0x350000
	0x100000, 0x400000
	0x110000, 0x4A0000
	0x150000, 0x580000
	0x1E0000, 0x620000
	0x280000, 0x6A0000
	0x350000, 0x6F0000
	0x400000, 0x700000
	0x900000, 0x700000
	0x900000, 0x100000
end table

private table TransportTube_transportPath1_3
	0x100000, 0x300000
	0x280000, 0x300000
	0x360000, 0x320000
	0x400000, 0x380000
	0x4B0000, 0x3D0000
	0x580000, 0x400000
	0x640000, 0x3C0000
	0x6C0000, 0x360000
	0x700000, 0x280000
	0x6C0000, 0x1A0000
	0x640000, 0x140000
	0x580000, 0x100000
	0x400000, 0x100000
	0x2E0000, 0x130000
	0x1D0000, 0x1E0000
	0x130000, 0x2D0000
	0x100000, 0x400000
	0x130000, 0x530000
	0x1D0000, 0x620000
	0x2E0000, 0x6E0000
	0x400000, 0x700000
	0x900000, 0x700000
	0x900000, 0x100000
end table

private table TransportTube_transportPath1_4
	0x100000, 0x100000
	0x100000, 0x700000
	0xC00000, 0x700000
	0xCA0000, 0x6F0000
	0xD40000, 0x6C0000
	0xDB0000, 0x680000
	0xE30000, 0x620000
	0xE80000, 0x5A0000
	0xED0000, 0x520000
	0xEF0000, 0x480000
	0xF00000, 0x400000
	0xEF0000, 0x360000
	0xED0000, 0x2E0000
	0xE80000, 0x260000
	0xE30000, 0x1E0000
	0xDB0000, 0x170000
	0xD40000, 0x140000
	0xCA0000, 0x120000
	0xC00000, 0x100000
	0xB70000, 0x110000
	0xAF0000, 0x120000
	0xA60000, 0x170000
	0x9E0000, 0x1E0000
	0x970000, 0x260000
	0x930000, 0x2E0000
	0x910000, 0x360000
	0x900000, 0x400000
	0x900000, 0x700000
end table

private table TransportTube_transportPath1_5
	0x100000, 0x100000
	0x100000, 0x700000
	0xC00000, 0x700000
	0xD20000, 0x6E0000
	0xE30000, 0x620000
	0xED0000, 0x530000
	0xF00000, 0x400000
	0xED0000, 0x2D0000
	0xE30000, 0x1E0000
	0xD20000, 0x130000
	0xC00000, 0x100000
	0xA80000, 0x100000
	0x9C0000, 0x140000
	0x940000, 0x1A0000
	0x900000, 0x280000
	0x940000, 0x360000
	0x9C0000, 0x3C0000
	0xA80000, 0x400000
	0xB50000, 0x3D0000
	0xC00000, 0x380000
	0xCA0000, 0x320000
	0xD80000, 0x300000
	0xF00000, 0x300000
end table

private table TransportTube_transportPath1_6
	0x900000, 0x700000
	0x900000, 0x400000
	0x910000, 0x360000
	0x930000, 0x2E0000
	0x970000, 0x260000
	0x9E0000, 0x1E0000
	0xA60000, 0x170000
	0xAF0000, 0x120000
	0xB70000, 0x110000
	0xC00000, 0x100000
	0xCA0000, 0x120000
	0xD40000, 0x140000
	0xDB0000, 0x170000
	0xE30000, 0x1E0000
	0xE80000, 0x260000
	0xED0000, 0x2E0000
	0xEF0000, 0x360000
	0xF00000, 0x400000
	0xEF0000, 0x480000
	0xED0000, 0x520000
	0xE80000, 0x5A0000
	0xE30000, 0x620000
	0xDB0000, 0x680000
	0xD40000, 0x6C0000
	0xCA0000, 0x6F0000
	0xC00000, 0x700000
	0x100000, 0x700000
	0x100000, 0x100000
end table

private table TransportTube_transportPath1_7
	0xF00000, 0x300000
	0xD80000, 0x300000
	0xCA0000, 0x320000
	0xC00000, 0x380000
	0xB50000, 0x3D0000
	0xA80000, 0x400000
	0x9C0000, 0x3C0000
	0x940000, 0x360000
	0x900000, 0x280000
	0x940000, 0x1A0000
	0x9C0000, 0x140000
	0xA80000, 0x100000
	0xC00000, 0x100000
	0xD20000, 0x130000
	0xE30000, 0x1E0000
	0xED0000, 0x2D0000
	0xF00000, 0x400000
	0xED0000, 0x530000
	0xE30000, 0x620000
	0xD20000, 0x6E0000
	0xC00000, 0x700000
	0x100000, 0x700000
	0x100000, 0x100000
end table

private table TransportTube_transportPath1_8
	0x1100000, 0x100000
	0x1100000, 0x700000
	0x400000, 0x700000
	0x350000, 0x6F0000
	0x280000, 0x6A0000
	0x1E0000, 0x620000
	0x150000, 0x580000
	0x110000, 0x4A0000
	0x100000, 0x400000
	0x110000, 0x350000
	0x150000, 0x270000
	0x1E0000, 0x1E0000
	0x280000, 0x150000
	0x350000, 0x110000
	0x400000, 0x100000
	0x500000, 0x100000
	0x5E0000, 0x120000
	0x680000, 0x180000
	0x6D0000, 0x240000
	0x700000, 0x300000
	0x6D0000, 0x3D0000
	0x680000, 0x480000
	0x5E0000, 0x4E0000
	0x500000, 0x500000
	0x300000, 0x500000
	0x220000, 0x520000
	0x170000, 0x5A0000
	0x110000, 0x630000
	0x100000, 0x700000
end table

private table TransportTube_transportPath1_9
	0x1100000, 0x100000
	0x1100000, 0x700000
	0x400000, 0x700000
	0x2E0000, 0x6E0000
	0x1D0000, 0x620000
	0x130000, 0x530000
	0x100000, 0x400000
	0x130000, 0x2D0000
	0x1D0000, 0x1E0000
	0x2E0000, 0x130000
	0x400000, 0x100000
	0x580000, 0x100000
	0x640000, 0x140000
	0x6C0000, 0x1A0000
	0x700000, 0x280000
	0x6C0000, 0x360000
	0x640000, 0x3C0000
	0x580000, 0x400000
	0x4B0000, 0x3D0000
	0x400000, 0x380000
	0x360000, 0x320000
	0x280000, 0x300000
	0x100000, 0x300000
end table

private table TransportTube_transportPath1_10
	0x100000, 0x700000
	0x110000, 0x630000
	0x170000, 0x5A0000
	0x220000, 0x520000
	0x300000, 0x500000
	0x500000, 0x500000
	0x5E0000, 0x4E0000
	0x680000, 0x480000
	0x6D0000, 0x3D0000
	0x700000, 0x300000
	0x6D0000, 0x240000
	0x680000, 0x180000
	0x5E0000, 0x120000
	0x500000, 0x100000
	0x400000, 0x100000
	0x350000, 0x110000
	0x280000, 0x150000
	0x1E0000, 0x1E0000
	0x150000, 0x270000
	0x110000, 0x350000
	0x100000, 0x400000
	0x110000, 0x4A0000
	0x150000, 0x580000
	0x1E0000, 0x620000
	0x280000, 0x6A0000
	0x350000, 0x6F0000
	0x400000, 0x700000
	0x1100000, 0x700000
	0x1100000, 0x100000
end table

private table TransportTube_transportPath1_11
	0x100000, 0x300000
	0x280000, 0x300000
	0x360000, 0x320000
	0x400000, 0x380000
	0x4B0000, 0x3D0000
	0x580000, 0x400000
	0x640000, 0x3C0000
	0x6C0000, 0x360000
	0x700000, 0x280000
	0x6C0000, 0x1A0000
	0x640000, 0x140000
	0x580000, 0x100000
	0x400000, 0x100000
	0x2E0000, 0x130000
	0x1D0000, 0x1E0000
	0x130000, 0x2D0000
	0x100000, 0x400000
	0x130000, 0x530000
	0x1D0000, 0x620000
	0x2E0000, 0x6E0000
	0x400000, 0x700000
	0x1100000, 0x700000
	0x1100000, 0x100000
end table

private table TransportTube_transportPath2_0
	0x7900000, 0x3B00000
	0x7100000, 0x3B00000
	0x7100000, 0x6B00000
	0xA900000, 0x6B00000
	0xA900000, 0x6700000
end table

private table TransportTube_transportPath2_1
	0x7900000, 0x3F00000
	0x7900000, 0x4B00000
	0xA000000, 0x4B00000
	0xC100000, 0x4B00000
	0xC100000, 0x3300000
	0xD900000, 0x3300000
	0xD900000, 0x1B00000
	0xF100000, 0x1B00000
	0xF100000, 0x2B00000
	0xF900000, 0x2B00000
end table

private table TransportTube_transportPath2_2
	0xAF00000, 0x6300000
	0xE900000, 0x6300000
	0xE900000, 0x6B00000
	0xF900000, 0x6B00000
	0xF900000, 0x6700000
end table

private table TransportTube_transportPath2_3
	0xF900000, 0x2F00000
	0xF900000, 0x4B00000
	0xF100000, 0x4B00000
	0xF100000, 0x6300000
	0xF900000, 0x6300000
end table

private table TransportTube_transportPath2_4
	0x14100000, 0x5300000
	0x11900000, 0x5300000
	0x11900000, 0x6B00000
	0x14100000, 0x6B00000
	0x14100000, 0x5700000
end table

private table TransportTube_transportPath2_5
	0x1AF00000, 0x5300000
	0x1B900000, 0x5300000
	0x1B900000, 0x3300000
	0x1E100000, 0x3300000
end table

private table TransportTube_transportPath2_6
	0x1A900000, 0x5700000
	0x1A900000, 0x5B00000
	0x1C100000, 0x5B00000
	0x1C100000, 0x4300000
	0x1E100000, 0x4300000
	0x1E100000, 0x3700000
end table

private table TransportTube_transportPath2_7
	0x24900000, 0x3700000
	0x24900000, 0x3D00000
	0x23900000, 0x3D00000
	0x23900000, 0x5D00000
	0x25100000, 0x5D00000
	0x25100000, 0x5700000
end table

private table TransportTube_transportPath2_8
	0x24F00000, 0x3300000
	0x25900000, 0x3300000
	0x25900000, 0x5300000
	0x25700000, 0x5300000
end table

private table TransportTube_transportPath2_9
	0x3100000, 0x3300000
	0x2900000, 0x3300000
	0x2900000, 0x2300000
	0x4900000, 0x2300000
end table

private table TransportTube_transportPath2_10
	0x3100000, 0x3700000
	0x3100000, 0x3B00000
	0x4100000, 0x3B00000
	0x4100000, 0x2B00000
	0x4900000, 0x2B00000
	0x4900000, 0x2700000
end table

private table TransportTube_transportPath2_11
	0x4900000, 0x6F00000
	0x4900000, 0x7300000
	0x6900000, 0x7300000
	0x8900000, 0x7300000
	0x8900000, 0x6F00000
end table

private table TransportTube_transportPath2_12
	0xBF00000, 0x3300000
	0xD900000, 0x3300000
	0xD900000, 0x2F00000
end table

private table TransportTube_transportPath2_13
	0xD900000, 0x2B00000
	0xC900000, 0x2B00000
	0xC900000, 0xB00000
	0xE800000, 0xB00000
	0x11100000, 0xB00000
	0x11100000, 0x2300000
	0x10F00000, 0x2300000
end table


// ========================
// Function Definitions
// ========================

private function TransportTube_HandleVelChange
	temp0 = object.newPos.x
	temp2 = 0x800
	temp0 -= player[currentPlayer].xpos
	if temp0 < 0
		FlipSign(temp0)
		FlipSign(temp2)
	end if
	temp0 >>= 16

	temp1 = object.newPos.y
	temp3 = 0x800
	temp1 -= player[currentPlayer].ypos
	if temp1 < 0
		FlipSign(temp1)
		FlipSign(temp3)
	end if
	temp1 >>= 16
	
	if temp1 >= temp0
		temp1 = object.newPos.y
		temp1 -= player[currentPlayer].ypos
		temp1 &= 0xFFFF0000
		temp1 /= temp3
		temp0 = object.newPos.x
		temp0 -= player[currentPlayer].xpos
		if temp0 != 0
			temp0 &= 0xFFFF0000
			temp0 /= temp1
		end if
		temp0 <<= 8
		temp3 <<= 8
		temp1 >>= 8
		player[currentPlayer].xvel = temp0
		player[currentPlayer].yvel = temp3
		Abs(temp1)
		object.timer = temp1
	else
		temp0 = object.newPos.x
		temp0 -= player[currentPlayer].xpos
		temp0 &= 0xFFFF0000
		temp0 /= temp2
		temp1 = object.newPos.y
		temp1 -= player[currentPlayer].ypos
		if temp1 != 0
			temp1 &= 0xFFFF0000
			temp1 /= temp0
		end if
		temp1 <<= 8
		temp2 <<= 8
		temp0 >>= 8
		player[currentPlayer].xvel = temp2
		player[currentPlayer].yvel = temp1
		Abs(temp0)
		object.timer = temp0
	end if
end function


// ========================
// Events
// ========================

event ObjectUpdate
	switch object.state
	case TRANSPORTTUBE_AWAITPLAYER
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			arrayPos0 = object.entityPos
			arrayPos0 += currentPlayer
			arrayPos0++
			if player[currentPlayer].state != Player_State_Static
				if player[currentPlayer].yvel > -0x40000
					temp0 = object.hitboxright
					BoxCollisionTest(C_TOUCH, object.entityPos, 0, 0, object.hitboxRight, 128, currentPlayer, 0, 0, 0, 0)
					if checkResult == true
						if object[arrayPos0].state == Player_State_Static
							temp0 = player[currentPlayer].xpos
							temp0 -= object.xpos
							if object.hitboxRight == 256
								FlipSign(temp0)
								temp0 += 0x1000000
							end if

							if temp0 >= 0x800000
								if object.transportPath != 2
									temp3 = object.transportPath
								else
									temp3 = stage.seconds
									temp3 &= 1
								end if
							else
								temp1 = player[currentPlayer].ypos
								temp1 -= object.ypos
								if temp1 >= 0x400000
									temp3 = 2
								else
									temp3 = 3
								end if
							end if
							object[arrayPos0].transportPath = temp3

							temp3 += object.tubetype
							GetTableValue(object[arrayPos0].transportTable, temp3, TransportTube_transportTables1)
							GetTableValue(object[arrayPos0].transportDelay, temp3, TransportTube_transportDelays1)
							object[arrayPos0].transportDelay--

							object[arrayPos0].tablePos = 0
							GetTableValue(player[currentPlayer].xpos, object[arrayPos0].tablePos, object[arrayPos0].transportTable)
							player[currentPlayer].xpos += object.xpos

							object[arrayPos0].tablePos++
							GetTableValue(player[currentPlayer].ypos, object[arrayPos0].tablePos, object[arrayPos0].transportTable)
							player[currentPlayer].ypos += object.ypos

							object[arrayPos0].tablePos++
							temp0 = object[arrayPos0].tablePos
							GetTableValue(object.newPos.x, temp0, object[arrayPos0].transportTable)
							object.newPos.x += object.xpos
							temp0++

							GetTableValue(object.newPos.y, temp0, object[arrayPos0].transportTable)
							object.newPos.y += object.ypos

							CallFunction(TransportTube_HandleVelChange)
							object[arrayPos0].timer = object.timer
							object[arrayPos0].state = TRANSPORTTUBE_HASPLAYER
							object[arrayPos0].propertyValue = object.propertyValue
							object[arrayPos0].priority = PRIORITY_ACTIVE
							object[arrayPos0].hitboxRight = object.hitboxRight
							object[arrayPos0].targetPlayer = currentPlayer

							player[currentPlayer].speed = 0
							player[currentPlayer].timer = 0
							player[currentPlayer].state = Player_State_Static
							player[currentPlayer].tileCollisions = false
							player[currentPlayer].interaction = false
							player[currentPlayer].animation = ANI_JUMPING
							player[currentPlayer].rollAnimationSpeed = 0
							player[currentPlayer].animationSpeed = 0
							player[currentPlayer].gravity = GRAVITY_AIR
							player[currentPlayer].scrollTracking = true
							player[currentPlayer].sortedDrawOrder = 4
							PlaySfx(SfxName[Rolling], false)
						end if
					end if
				end if
			end if
		next
		break

	case TRANSPORTTUBE_BLANK
		break

	case TRANSPORTTUBE_HASPLAYER
		currentPlayer = object.targetPlayer
		object.timer--
		if object.timer >= 0
			// waiting... (I think)
		else
			GetTableValue(player[currentPlayer].xpos, object.tablePos, object.transportTable)
			player[currentPlayer].xpos += object.xpos
			object.tablePos++
			GetTableValue(player[currentPlayer].ypos, object.tablePos, object.transportTable)
			player[currentPlayer].ypos += object.ypos
			object.tablePos++
			if object.transportPath < 0
				object.tablePos -= 4
			end if

			object.transportDelay--
			if object.transportDelay == 0
				if object.transportPath >= 4
					object.state = TRANSPORTTUBE_EXITNODE
					player[currentPlayer].ypos &= 0x7FF0000
					player[currentPlayer].state = Player_State_Air
					player[currentPlayer].tileCollisions = true
					player[currentPlayer].interaction = true
					player[currentPlayer].xvel = 0
					player[currentPlayer].yvel = -0x80000

					PlaySfx(SfxName[Release], false)
				else
					temp0 = object.propertyValue
					temp0 &= 0xFC
					temp0 += object.transportPath
					object.transportPath = 4

					GetTableValue(temp1, temp0, TransportTube_nodeFlagsTable)
					if temp1 == 0
						object.state = TRANSPORTTUBE_EXITNODE
						player[currentPlayer].ypos &= 0x7FF0000
						player[currentPlayer].state = Player_State_Air
						player[currentPlayer].tileCollisions = true
						player[currentPlayer].interaction = true
						player[currentPlayer].xvel = 0
						player[currentPlayer].yvel = -0x80000

						PlaySfx(SfxName[Release], false)
					else
						if temp1 < 0
							FlipSign(temp1)
							object.transportPath = -4
							GetTableValue(object.transportTable, temp1, TransportTube_transportTables2)
							GetTableValue(object.transportDelay, temp1, TransportTube_transportDelays2)
							object.transportDelay--
							object.tablePos = object.transportDelay
							object.tablePos <<= 1
						else
							GetTableValue(object.transportTable, temp1, TransportTube_transportTables2)
							GetTableValue(object.transportDelay, temp1, TransportTube_transportDelays2)
							object.transportDelay--
							object.tablePos = 0
						end if

						GetTableValue(player[currentPlayer].xpos, object.tablePos, object.transportTable)
						object.tablePos++

						GetTableValue(player[currentPlayer].ypos, object.tablePos, object.transportTable)
						object.tablePos++

						if object.transportPath < 0
							object.tablePos -= 4
						end if

						temp0 = object.tablePos
						GetTableValue(object.newPos.x, temp0, object.transportTable)
						temp0++
						GetTableValue(object.newPos.y, temp0, object.transportTable)
						CallFunction(TransportTube_HandleVelChange)
						PlaySfx(SfxName[Rolling], false)
						object.state = TRANSPORTTUBE_TRANSPORTING
					end if
				end if
			else
				temp0 = object.tablePos
				GetTableValue(object.newPos.x, temp0, object.transportTable)
				object.newPos.x += object.xpos
				temp0++

				GetTableValue(object.newPos.y, temp0, object.transportTable)
				object.newPos.y += object.ypos
				CallFunction(TransportTube_HandleVelChange)
			end if
		end if

		if player[currentPlayer].state != Player_State_Static
			object.state = TRANSPORTTUBE_BLANK
			object.priority = PRIORITY_BOUNDS
		end if

		if player[currentPlayer].type == TypeName[Debug Mode]
			object.state = TRANSPORTTUBE_BLANK
			object.priority = PRIORITY_BOUNDS
		end if
		break

	case TRANSPORTTUBE_TRANSPORTING
		currentPlayer = object.targetPlayer

		object.timer--
		if object.timer >= 0
			// waiting... (I think)
		else
			GetTableValue(player[currentPlayer].xpos, object.tablePos, object.transportTable)
			object.tablePos++

			GetTableValue(player[currentPlayer].ypos, object.tablePos, object.transportTable)
			object.tablePos++
			if object.transportPath < 0
				object.tablePos -= 4
			end if

			object.transportDelay--
			if object.transportDelay == 0
				object.state = TRANSPORTTUBE_EXITNODE
				player[currentPlayer].ypos &= 0x7FF0000
				player[currentPlayer].state = Player_State_Air
				player[currentPlayer].tileCollisions = true
				player[currentPlayer].interaction = true
				player[currentPlayer].xvel = 0
				player[currentPlayer].yvel = -0x80000

				PlaySfx(SfxName[Release], false)
			else
				temp0 = object.tablePos
				GetTableValue(object.newPos.x, temp0, object.transportTable)
				temp0++

				GetTableValue(object.newPos.y, temp0, object.transportTable)
				CallFunction(TransportTube_HandleVelChange)
			end if
		end if

		if player[currentPlayer].state != TRANSPORTTUBE_BLANK
			object.state = TRANSPORTTUBE_BLANK
			object.priority = PRIORITY_BOUNDS
		end if

		if player[currentPlayer].type == TypeName[Debug Mode]
			object.state = TRANSPORTTUBE_BLANK
			object.priority = PRIORITY_BOUNDS
		end if
		break

	case TRANSPORTTUBE_EXITNODE
		BoxCollisionTest(C_TOUCH, object.entityPos, 0, 0, object.hitboxRight, 128, currentPlayer, 0, 0, 0, 0)
		if checkResult == false
			object.state = TRANSPORTTUBE_BLANK
			object.priority = PRIORITY_BOUNDS
		end if
		break
		
	end switch
end event


event ObjectStartup
	// That's neat, you don't see many in-game objects that use the script box
	// Well I suppose "use" would be a strech since the object doesn't ever actually draw anything, but
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)

	SetTableValue(TransportTube_transportPath1_0,  0, TransportTube_transportTables1)
	SetTableValue(TransportTube_transportPath1_1,  1, TransportTube_transportTables1)
	SetTableValue(TransportTube_transportPath1_2,  2, TransportTube_transportTables1)
	SetTableValue(TransportTube_transportPath1_3,  3, TransportTube_transportTables1)
	SetTableValue(TransportTube_transportPath1_4,  4, TransportTube_transportTables1)
	SetTableValue(TransportTube_transportPath1_5,  5, TransportTube_transportTables1)
	SetTableValue(TransportTube_transportPath1_6,  6, TransportTube_transportTables1)
	SetTableValue(TransportTube_transportPath1_7,  7, TransportTube_transportTables1)
	SetTableValue(TransportTube_transportPath1_8,  8, TransportTube_transportTables1)
	SetTableValue(TransportTube_transportPath1_9,  9, TransportTube_transportTables1)
	SetTableValue(TransportTube_transportPath1_10, 10, TransportTube_transportTables1)
	SetTableValue(TransportTube_transportPath1_11, 11, TransportTube_transportTables1)

	SetTableValue(TransportTube_transportPath2_0,  0, TransportTube_transportTables2)
	SetTableValue(TransportTube_transportPath2_0,  1, TransportTube_transportTables2)
	SetTableValue(TransportTube_transportPath2_1,  2, TransportTube_transportTables2)
	SetTableValue(TransportTube_transportPath2_2,  3, TransportTube_transportTables2)
	SetTableValue(TransportTube_transportPath2_3,  4, TransportTube_transportTables2)
	SetTableValue(TransportTube_transportPath2_4,  5, TransportTube_transportTables2)
	SetTableValue(TransportTube_transportPath2_5,  6, TransportTube_transportTables2)
	SetTableValue(TransportTube_transportPath2_6,  7, TransportTube_transportTables2)
	SetTableValue(TransportTube_transportPath2_7,  8, TransportTube_transportTables2)
	SetTableValue(TransportTube_transportPath2_8,  9, TransportTube_transportTables2)
	SetTableValue(TransportTube_transportPath2_9,  10, TransportTube_transportTables2)
	SetTableValue(TransportTube_transportPath2_10, 11, TransportTube_transportTables2)
	SetTableValue(TransportTube_transportPath2_11, 12, TransportTube_transportTables2)
	SetTableValue(TransportTube_transportPath2_12, 13, TransportTube_transportTables2)
	SetTableValue(TransportTube_transportPath2_13, 14, TransportTube_transportTables2)

	foreach (TypeName[Transport Tube], arrayPos0, ALL_ENTITIES)
		if object[arrayPos0].propertyValue == 0xFF
			object[arrayPos0].state = TRANSPORTTUBE_BLANK
			object[arrayPos0].propertyValue = 0
		else
			object[arrayPos0].tubetype = object[arrayPos0].propertyValue
			object[arrayPos0].tubetype &= 3
			GetTableValue(object[arrayPos0].hitboxRight, object[arrayPos0].tubetype, TransportTube_sizeTable)
			object[arrayPos0].tubetype <<= 2

			temp0 = object[arrayPos0].propertyValue
			temp0 >>= 2
			temp0 &= 15
			GetTableValue(object[arrayPos0].transportPath, temp0, TransportTube_pathsTable)
		end if
	next
end event


// ========================
// Editor Events
// ========================

event RSDKEdit
	if editor.returnVariable == true
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			checkResult = object.propertyValue
			break
			
		case 0 // type
			checkResult = object.propertyValue
			if object.propertyValue != 0xFF
				checkResult &= 3
			end if
			break
			
		case 1 // path
			checkResult = 0
			if object.propertyValue != 0xFF
				checkResult = object.propertyValue
				checkResult >>= 2
				checkResult &= 0x0F
			end if
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // type
			if editor.variableValue == 0xFF
				object.propertyValue = editor.variableValue
			else
				if object.propertyValue != 0xFF
					// Not Transporter, keep our path value
					temp0 = 3
					Not(temp0)
					object.propertyValue &= temp0
				else
					// was Transporter, reset property value
					object.propertyValue = 0
				end if

				editor.variableValue &= 3
				object.propertyValue |= editor.variableValue
			end if
			break
			
		case 1 // path
			if object.propertyValue != 0xFF
				temp0 = 0x0F
				temp0 <<= 2
				Not(temp0)
				object.propertyValue &= temp0

				editor.variableValue &= 0x0F
				editor.variableValue <<= 2
				object.propertyValue |= editor.variableValue
			end if
			break
			
		end switch
	end if
end event


event RSDKDraw
	DrawSprite(0)
end event


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

	AddEditorVariable("type")
	SetActiveVariable("type")
	AddEnumVariable("Entry", 0)
	AddEnumVariable("Entry1", 1)
	AddEnumVariable("Entry2", 2)
	AddEnumVariable("Entry3", 3)
	AddEnumVariable("Transporter", 0xFF)

	AddEditorVariable("path")
	SetActiveVariable("path")
end event
