// ----------------------------------
// RSDK Project: Sonic 1
// Script Description: Belt Platform Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

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

private alias object.value0 : object.movePos.x
private alias object.value1 : object.movePos.y
private alias object.value2 : object.collisionOffset.x
private alias object.value3 : object.collisionOffset.y
private alias object.value4 : object.tablePtr
private alias object.value5 : object.tablePos
private alias object.value6 : object.tableSize

// Player Aliases
private alias object.xpos : player.xpos
private alias object.ypos : player.ypos


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

reserve function BeltPlatform_UpdateMovement

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

private table BeltPlatform_frameTable
	0, 1, 2, 3, 4, 3, 2, 1
end table

private table BeltPlatform_directionTable
	0, 0, 0, 0
	0, 2, 2, 2
	2, 3, 3, 3
	3, 1, 1, 1
end table

private table BeltPlatform_tableSizes
	6, 6, 6, 6, 6, 6
end table

private table BeltPlatform_tablePtrs
	0, 0, 0, 0, 0, 0
end table

private table BeltPlatform_movementTable0
	0x0E14, 0x0370, 0x0EF0, 0x0302, 0x0EF0, 0x0340, 0x0E14, 0x03AE
end table

private table BeltPlatform_movementTable1
	0x0F14, 0x02E0, 0x0FF0, 0x0272, 0x0FF0, 0x02B0, 0x0F14, 0x031E
end table

private table BeltPlatform_movementTable2
	0x1014, 0x0270, 0x10F0, 0x0202, 0x10F0, 0x0240, 0x1014, 0x02AE
end table

private table BeltPlatform_movementTable3
	0x0F14, 0x0570, 0x0FF0, 0x0502, 0x0FF0, 0x0540, 0x0F14, 0x05AE
end table

private table BeltPlatform_movementTable4
	0x1B14, 0x0670, 0x1BF0, 0x0602, 0x1BF0, 0x0640, 0x1B14, 0x06AE
end table

private table BeltPlatform_movementTable5
	0x1C14, 0x05E0, 0x1CF0, 0x0572, 0x1CF0, 0x05B0, 0x1C14, 0x061E
end table

private function BeltPlatform_UpdateMovement
	temp0 = object[arrayPos0].tablePos
	GetTableValue(object[arrayPos0].movePos.x, temp0, object[arrayPos0].tablePtr)
	temp0++
	GetTableValue(object[arrayPos0].movePos.y, temp0, object[arrayPos0].tablePtr)

	temp0 = object[arrayPos0].ixpos
	temp0 -= object[arrayPos0].movePos.x
	Abs(temp0)
	temp1 = object[arrayPos0].iypos
	temp1 -= object[arrayPos0].movePos.y
	Abs(temp1)

	if temp1 < temp0
		object[arrayPos0].xvel = 0x10000
		temp1 <<= 16
		object[arrayPos0].yvel = temp1
		object[arrayPos0].yvel /= temp0
	else
		if temp1 != 0
			temp0 <<= 16
			object[arrayPos0].xvel = temp0
			object[arrayPos0].xvel /= temp1
		else
			object[arrayPos0].xvel = 0
		end if
		
		object[arrayPos0].yvel = 0x10000
	end if

	if object[arrayPos0].movePos.x < object[arrayPos0].ixpos
		FlipSign(object[arrayPos0].xvel)
	end if

	if object[arrayPos0].movePos.y < object[arrayPos0].iypos
		FlipSign(object[arrayPos0].yvel)
	end if
end function


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

event ObjectUpdate
	temp0 = object.xpos
	temp1 = object.ypos
	temp0 >>= 16
	temp1 >>= 16

	if temp0 == object.movePos.x
		if temp1 == object.movePos.y
			object.tablePos += 2
			if object.tablePos > object.tableSize
				object.tablePos = 0
			end if
			arrayPos0 = object.entityPos
			CallFunction(BeltPlatform_UpdateMovement)
		end if
	end if

	if object.rotation != 0
		object.rotation++
		object.rotation &= 15
	else
		if object.tablePos > 2
			object.rotation++
		end if
	end if

	temp0 = object.rotation
	GetTableValue(object.direction, temp0, BeltPlatform_directionTable)
	temp0 &= 7
	GetTableValue(object.frame, temp0, BeltPlatform_frameTable)
	temp0 = object.xpos
	temp0 &= 0xFFFF0000
	temp1 = object.ypos
	temp1 &= 0xFFFF0000
	temp2 = object.xpos
	temp2 += object.xvel
	temp3 = object.ypos
	temp3 += object.yvel

	object.collisionOffset.x = temp2
	object.collisionOffset.x &= 0xFFFF0000
	object.collisionOffset.x -= temp0
	object.collisionOffset.y = temp3
	object.collisionOffset.y &= 0xFFFF0000
	object.collisionOffset.y -= temp1

	if object.rotation == 0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_SOLID, object.entityPos, -16, -7, 16, 7, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == COL_TOP
				player[currentPlayer].xpos += object.collisionOffset.x
				player[currentPlayer].ypos += object.collisionOffset.y
			end if
		next
	end if

	object.xpos = temp2
	object.ypos = temp3
end event


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


event ObjectStartup
	LoadSpriteSheet("SBZ/Objects.gif")
	SpriteFrame(-16, -7, 32, 14, 413, 166)
	SpriteFrame(-16, -12, 32, 25, 446, 169)
	SpriteFrame(-15, -15, 30, 30, 480, 166)
	SpriteFrame(-13, -16, 25, 32, 449, 198)
	SpriteFrame(-7, -16, 14, 32, 480, 198)

	SetTableValue(BeltPlatform_movementTable0, 0, BeltPlatform_tablePtrs)
	SetTableValue(BeltPlatform_movementTable1, 1, BeltPlatform_tablePtrs)
	SetTableValue(BeltPlatform_movementTable2, 2, BeltPlatform_tablePtrs)
	SetTableValue(BeltPlatform_movementTable3, 3, BeltPlatform_tablePtrs)
	SetTableValue(BeltPlatform_movementTable4, 4, BeltPlatform_tablePtrs)
	SetTableValue(BeltPlatform_movementTable5, 5, BeltPlatform_tablePtrs)

	foreach (TypeName[Belt Platform], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].priority = PRIORITY_INACTIVE

		temp0 = object[arrayPos0].propertyValue
		temp0 >>= 4
		GetTableValue(object[arrayPos0].tablePtr, temp0, BeltPlatform_tablePtrs)
		GetTableValue(object[arrayPos0].tableSize, temp0, BeltPlatform_tableSizes)

		object[arrayPos0].tablePos = object[arrayPos0].propertyValue
		object[arrayPos0].tablePos &= 15
		object[arrayPos0].tablePos <<= 1
		CallFunction(BeltPlatform_UpdateMovement)
	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
			checkResult >>= 4
			checkResult %= 6
			break
			
		case 1 // startOffset
			checkResult = object.propertyValue
			checkResult &= 0x0F
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // type
			editor.variableValue %= 6
			editor.variableValue <<= 4
			
			object.propertyValue &= 0x0F
			object.propertyValue |= editor.variableValue
			break
			
		case 1 // startOffset
			editor.variableValue &= 0x0F

			object.propertyValue &= 0xF0
			object.propertyValue |= editor.variableValue
			break
			
		end switch
	end if
end event


event RSDKDraw
	DrawSprite(0)

	if editor.showGizmos == true
		editor.drawingOverlay = true

		temp0 = object.propertyValue
		temp0 >>= 4
		GetTableValue(object.tablePtr, temp0, BeltPlatform_tablePtrs)
		GetTableValue(object.tableSize, temp0, BeltPlatform_tableSizes)

		object.tablePos = object.propertyValue
		object.tablePos &= 15
		object.tablePos <<= 1
		arrayPos0 = object.entityPos
		CallFunction(BeltPlatform_UpdateMovement)

		temp0 = object.xvel
		temp0 <<= 5
		temp0 += object.xpos

		temp1 = object.yvel
		temp1 <<= 5
		temp1 += object.ypos

		DrawArrow(object.xpos, object.ypos, temp0, temp1, 0x00, 0xFF, 0x00)

		editor.drawingOverlay = false
	end if
end event


event RSDKLoad
	LoadSpriteSheet("SBZ/Objects.gif")
	SpriteFrame(-16, -7, 32, 14, 413, 166)

	SetTableValue(BeltPlatform_movementTable0, 0, BeltPlatform_tablePtrs)
	SetTableValue(BeltPlatform_movementTable1, 1, BeltPlatform_tablePtrs)
	SetTableValue(BeltPlatform_movementTable2, 2, BeltPlatform_tablePtrs)
	SetTableValue(BeltPlatform_movementTable3, 3, BeltPlatform_tablePtrs)
	SetTableValue(BeltPlatform_movementTable4, 4, BeltPlatform_tablePtrs)
	SetTableValue(BeltPlatform_movementTable5, 5, BeltPlatform_tablePtrs)

	AddEditorVariable("type")
	SetActiveVariable("type")

	AddEditorVariable("startOffset")
	SetActiveVariable("startOffset")
end event
