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

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

private alias object.value0  : object.basePos.x
private alias object.value1  : object.basePos.y
private alias object.value2  : object.movePos.x
private alias object.value3  : object.movePos.y
private alias object.value4  : object.collisionOffset.x
private alias object.value5  : object.collisionOffset.y
private alias object.value6  : object.tablePtr
private alias object.value7  : object.tablePos
private alias object.value8  : object.tableSize
private alias object.value31 : object.wrapOffset.y

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


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

reserve function BeltPlatform_UpdateMovement


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

private table BeltPlatform_flippedTablePos
	5, 9, 3, 2, 6, 0, 4, 8, 7, 1
end table

private table BeltPlatform_tableSizes
	18, 18, 18
end table

private table BeltPlatform_tablePtrs[3]

private table BeltPlatform_movementTable0
	0, 0, -22, 10, -32, 32, -32, 224, -22, 246, 0, 256, 22, 246, 32, 224, 32, 32, 22, 10
end table

private table BeltPlatform_movementTable1
	0, 0, -22, 10, -32, 32, -32, 352, -22, 374, 0, 384, 22, 374, 32, 352, 32, 32, 22, 10
end table

private table BeltPlatform_movementTable2
	0, 0, -22, 10, -32, 32, -32, 480, -22, 502, 0, 512, 22, 502, 32, 480, 32, 32, 22, 10
end table


// ========================
// Funcion Definitions
// ========================

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

	temp0++
	GetTableValue(object[arrayPos0].movePos.y, temp0, object[arrayPos0].tablePtr)
	object[arrayPos0].movePos.y += object[arrayPos0].basePos.y

	temp0 = object[arrayPos0].xpos
	temp0 >>= 16
	temp2 = temp0
	temp0 -= object[arrayPos0].movePos.x
	Abs(temp0)

	temp1 = object[arrayPos0].ypos
	temp1 -= object.wrapOffset.y
	temp1 >>= 16
	temp1 &= 0x7FF
	temp3 = temp1
	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 < temp2
		FlipSign(object[arrayPos0].xvel)
	end if

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


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

event ObjectUpdate
	temp2 = 0
	temp0 = object.xpos
	temp1 = object.ypos
	temp0 >>= 16
	temp1 >>= 16
	temp1 &= 0x7FF

	if object.xvel < 0
		if object.yvel < 0
			if temp0 <= object.movePos.x
				if temp1 <= object.movePos.y
					temp2 = true
				end if
			end if
		else
			if temp0 <= object.movePos.x
				if temp1 >= object.movePos.y
					temp2 = true
				end if
			end if
		end if
	else
		if object.yvel < 0
			if temp0 >= object.movePos.x
				if temp1 <= object.movePos.y
					temp2 = true
				end if
			end if
		else
			if temp0 >= object.movePos.x
				if temp1 >= object.movePos.y
					temp2 = true
				end if
			end if
		end if
	end if

	if temp2 == true
		if object.direction == FLIP_NONE
			object.tablePos += 2
			if object.tablePos > object.tableSize
				object.tablePos = 0
			end if
		else
			object.tablePos -= 2
			if object.tablePos < 0
				object.tablePos = object.tableSize
			end if
		end if

		arrayPos0 = object.entityPos
		CallFunction(BeltPlatform_UpdateMovement)
	end if

	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

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_PLATFORM, object.entityPos, -16, -8, 16, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		if checkResult == true
			player[currentPlayer].xpos += object.collisionOffset.x
			player[currentPlayer].ypos += object.collisionOffset.y
		end if
	next

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


event ObjectDraw
	DrawSprite(0)
end event


event ObjectStartup
	LoadSpriteSheet("MPZ/Objects.gif")
	SpriteFrame(-16, -8, 32, 16, 34, 67)

	SetTableValue(BeltPlatform_movementTable0, 0, BeltPlatform_tablePtrs)
	SetTableValue(BeltPlatform_movementTable1, 1, BeltPlatform_tablePtrs)
	SetTableValue(BeltPlatform_movementTable2, 2, 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 &= 0xF
		if object[arrayPos0].direction == FLIP_X
			GetTableValue(temp0, object[arrayPos0].tablePos, BeltPlatform_flippedTablePos)
			object[arrayPos0].tablePos = temp0
		end if
		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
			
		case 2 // direction
			checkResult = object.direction
			checkResult &= 1
			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
			
		case 2 // direction
			editor.variableValue &= 1
			object.direction = editor.variableValue
			break
			
		end switch
	end if
end event


event RSDKDraw
	DrawSprite(0)

	if editor.showGizmos == 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
		if object.direction == FLIP_X
			GetTableValue(temp0, object.tablePos, BeltPlatform_flippedTablePos)
			object.tablePos = temp0
		end if
		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)
	end if
end event


event RSDKLoad
	LoadSpriteSheet("MPZ/Objects.gif")
	SpriteFrame(-16, -8, 32, 16, 34, 67)

	SetTableValue(BeltPlatform_movementTable0, 0, BeltPlatform_tablePtrs)
	SetTableValue(BeltPlatform_movementTable1, 1, BeltPlatform_tablePtrs)
	SetTableValue(BeltPlatform_movementTable2, 2, BeltPlatform_tablePtrs)

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

	AddEditorVariable("startOffset")
	SetActiveVariable("startOffset")
	
	AddEditorVariable("direction")
	SetActiveVariable("direction")
	AddEnumVariable("Clockwise", 0)
	AddEnumVariable("Counter-clockwise", 1)
	
	// represent positions in the scene
	// should be aligned with top most platform in the chain
	SetVariableAlias(ALIAS_VAR_VAL0, "basePos.x")
	SetVariableAlias(ALIAS_VAR_VAL1, "basePos.y")
end event
