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

//-------Aliases-------//
private alias 67 : TYPE_BELTPLATFORM

// Function declarations
reserve function BeltPlatform_Function148

// Static Values

// Tables
public table BeltPlatform_table48
	5, 9, 3, 2, 6, 0, 4, 8, 7, 1
end table

public table BeltPlatform_table47
	18, 18, 18
end table

public table BeltPlatform_table46
	0, 0, 0
end table

private table BeltPlatform_table0
	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_table1
	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_table2
	0, 0, -22, 10, -32, 32, -32, 480, -22, 502, 0, 512, 22, 502, 32, 480, 32, 32, 22, 10
end table

function BeltPlatform_Function148
	temp0 = object[arrayPos0].value7
	GetTableValue(object[arrayPos0].value2, temp0, object[arrayPos0].value6)
	object[arrayPos0].value2 += object[arrayPos0].value0
	temp0++
	GetTableValue(object[arrayPos0].value3, temp0, object[arrayPos0].value6)
	object[arrayPos0].value3 += object[arrayPos0].value1
	temp0 = object[arrayPos0].xpos
	temp0 >>= 16
	temp2 = temp0
	temp0 -= object[arrayPos0].value2
	Abs(temp0)
	temp1 = object[arrayPos0].ypos
	temp1 -= object.value31
	temp1 >>= 16
	temp1 &= 0x7FF
	temp3 = temp1
	temp1 -= object[arrayPos0].value3
	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].value2 < temp2
		FlipSign(object[arrayPos0].xvel)
	end if
	if object[arrayPos0].value3 < temp3
		FlipSign(object[arrayPos0].yvel)
	end if
end function


event ObjectMain
	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.value2
				if temp1 <= object.value3
					temp2 = 1
				end if
			end if
		else
			if temp0 <= object.value2
				if temp1 >= object.value3
					temp2 = 1
				end if
			end if
		end if
	else
		if object.yvel < 0
			if temp0 >= object.value2
				if temp1 <= object.value3
					temp2 = 1
				end if
			end if
		else
			if temp0 >= object.value2
				if temp1 >= object.value3
					temp2 = 1
				end if
			end if
		end if
	end if
	if temp2 == 1
		if object.direction == FLIP_NONE
			object.value7 += 2
			if object.value7 > object.value8
				object.value7 = 0
			end if
		else
			object.value7 -= 2
			if object.value7 < 0
				object.value7 = object.value8
			end if
		end if
		arrayPos0 = object.entityPos
		CallFunction(BeltPlatform_Function148)
	end if
	temp0 = object.xpos
	temp0 &= -0x10000
	temp1 = object.ypos
	temp1 &= -0x10000
	temp2 = object.xpos
	temp2 += object.xvel
	temp3 = object.ypos
	temp3 += object.yvel
	object.value4 = temp2
	object.value4 &= -0x10000
	object.value4 -= temp0
	object.value5 = temp3
	object.value5 &= -0x10000
	object.value5 -= temp1
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_PLATFORM, object.entityPos, -16, -8, 16, 8, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
		if checkResult == 1
			object[currentPlayer].xpos += object.value4
			object[currentPlayer].ypos += object.value5
		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_table0, 0, BeltPlatform_table46)
	SetTableValue(BeltPlatform_table1, 1, BeltPlatform_table46)
	SetTableValue(BeltPlatform_table2, 2, BeltPlatform_table46)
	foreach (TypeName[Belt Platform], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].priority = PRIORITY_ACTIVE
		temp0 = object[arrayPos0].propertyValue
		temp0 >>= 4
		GetTableValue(object[arrayPos0].value6, temp0, BeltPlatform_table46)
		GetTableValue(object[arrayPos0].value8, temp0, BeltPlatform_table47)
		object[arrayPos0].value7 = object[arrayPos0].propertyValue
		object[arrayPos0].value7 &= 15
		if object[arrayPos0].direction == FLIP_X
			GetTableValue(temp0, object[arrayPos0].value7, BeltPlatform_table48)
			object[arrayPos0].value7 = temp0
		end if
		object[arrayPos0].value7 <<= 1
		CallFunction(BeltPlatform_Function148)
	next
end event

event RSDKDraw
	DrawSprite(0)
end event

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