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

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

private alias object.value1  : object.localPos.x
private alias object.value2  : object.localPos.y
private alias object.value3  : object.localPos.z
private alias object.value4  : object.worldPos.x
private alias object.value5  : object.worldPos.y
private alias object.value6  : object.worldPos.z

private alias object.value7  : object.shadowsEnabled
private alias object.value8  : object.localShadowPos.x
private alias object.value9  : object.localShadowPos.y
private alias object.value11 : object.worldShadowPos.x
private alias object.value12 : object.worldShadowPos.y
private alias object.value13 : object.worldShadowPos.z
private alias object.value14 : object.worldShadowPos.u

// Halfpipe Aliases
private alias object.value13 : halfpipe.playerPos.z

// Player Aliases
private alias object.value0  : player.rings
private alias object.value14 : player.localPos.z
private alias object.value15 : player.localPos.x
private alias object.value16 : player.localPos.y

// Face Buffer Flag Aliases
private alias 5 : FACE_TEXTURED_C
private alias 6 : FACE_TEXTURED_C_BLEND


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

private table Halfpipe_vertexUTable
	50, 83, 116, 149, 182, 215, 17, 50
end table

private table Halfpipe_vertexVTable
	17, 17, 17, 17, 17, 17, 50, 50
end table


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

event ObjectUpdate
	arrayPos0 = scene3D.vertexCount
	arrayPos1 = scene3D.faceCount

	if object.shadowsEnabled == true
		faceBuffer[arrayPos1].flag = FACE_TEXTURED_C_BLEND
		faceBuffer[arrayPos1].a = arrayPos0
		vertexBuffer[arrayPos0].x = object.worldShadowPos.x
		vertexBuffer[arrayPos0].y = object.worldShadowPos.y
		vertexBuffer[arrayPos0].z = object.worldShadowPos.z
		vertexBuffer[arrayPos0].u = object.worldShadowPos.u
		vertexBuffer[arrayPos0].v = 50
		arrayPos0++

		faceBuffer[arrayPos1].b = arrayPos0
		vertexBuffer[arrayPos0].x = object.worldShadowPos.x
		vertexBuffer[arrayPos0].y = object.worldShadowPos.y
		vertexBuffer[arrayPos0].z = object.worldShadowPos.z
		temp0 = object[0].ypos
		temp0 -= 0xE00000
		object.scale = object.ypos
		object.scale -= temp0
		if object.scale > 0
			object.scale >>= 14
			object.scale += 0x400
		else
			object.scale = 0x400
		end if
		vertexBuffer[arrayPos0].u = object.scale
		vertexBuffer[arrayPos0].v = object.scale
		arrayPos0++

		faceBuffer[arrayPos1].c = arrayPos0
		vertexBuffer[arrayPos0].x = object.worldShadowPos.x
		vertexBuffer[arrayPos0].y = object.worldShadowPos.y
		vertexBuffer[arrayPos0].z = object.worldShadowPos.z
		vertexBuffer[arrayPos0].u = 17
		vertexBuffer[arrayPos0].v = 16
		arrayPos0++

		faceBuffer[arrayPos1].d = arrayPos0
		vertexBuffer[arrayPos0].x = object.worldShadowPos.x
		vertexBuffer[arrayPos0].y = object.worldShadowPos.y
		vertexBuffer[arrayPos0].z = object.worldShadowPos.z
		arrayPos0++

		arrayPos1++
		scene3D.vertexCount += 4
		scene3D.faceCount++
	end if

	faceBuffer[arrayPos1].flag = FACE_TEXTURED_C
	faceBuffer[arrayPos1].a = arrayPos0
	vertexBuffer[arrayPos0].x = object.worldPos.x
	vertexBuffer[arrayPos0].y = object.worldPos.y
	vertexBuffer[arrayPos0].z = object.worldPos.z
	GetTableValue(vertexBuffer[arrayPos0].u, ringFrame, Halfpipe_vertexUTable)
	GetTableValue(vertexBuffer[arrayPos0].v, ringFrame, Halfpipe_vertexVTable)
	arrayPos0++

	faceBuffer[arrayPos1].b = arrayPos0
	vertexBuffer[arrayPos0].x = object.worldPos.x
	vertexBuffer[arrayPos0].y = object.worldPos.y
	vertexBuffer[arrayPos0].z = object.worldPos.z
	
	temp0 = object[0].ypos
	temp0 -= 0xE00000
	object.scale = object.ypos
	object.scale -= temp0
	if object.scale > 0
		object.scale >>= 14
		object.scale += 0x400
	else
		object.scale = 0x400
	end if
	vertexBuffer[arrayPos0].u = object.scale
	vertexBuffer[arrayPos0].v = object.scale
	arrayPos0++

	faceBuffer[arrayPos1].c = arrayPos0
	vertexBuffer[arrayPos0].x = object.worldPos.x
	vertexBuffer[arrayPos0].y = object.worldPos.y
	vertexBuffer[arrayPos0].z = object.worldPos.z
	vertexBuffer[arrayPos0].u = 16
	vertexBuffer[arrayPos0].v = 16
	arrayPos0++

	faceBuffer[arrayPos1].d = arrayPos0
	vertexBuffer[arrayPos0].x = object.worldPos.x
	vertexBuffer[arrayPos0].y = object.worldPos.y
	vertexBuffer[arrayPos0].z = object.worldPos.z
	scene3D.vertexCount += 4
	scene3D.faceCount++

	if object.localPos.z < halfpipe[0].playerPos.z
		object.type = TypeName[Blank Object]
	else
		foreach (TypeName[Player Object], currentPlayer, ACTIVE_ENTITIES)
			temp1 = object.localPos.z
			temp1 -= 0x20000
			temp2 = object.localPos.z
			temp2 += 0x20000

			if player[currentPlayer].localPos.z > temp1
				if player[currentPlayer].localPos.z < temp2
					temp0 = player[currentPlayer].localPos.x
					temp0 -= object.localPos.x
					temp1 = temp0
					temp1 *= temp0
					temp0 = player[currentPlayer].localPos.y
					temp0 -= object.localPos.y
					temp2 = temp0
					temp2 *= temp0
					temp1 += temp2

					if temp1 < 0xC40000
						if options.vsMode == false
							player[currentPlayer].rings++
						else
							if ReceiveValue > -1
								if currentPlayer == 2
									player[currentPlayer].rings++
								end if
							else
								player[currentPlayer].rings++
							end if
						end if

						object.type = TypeName[Ring Sparkle]
						object.animationTimer = 0
						object.frame = 0
						if ringPan == 0
							PlaySfx(SfxName[Ring L], false)
							SetSfxAttributes(SfxName[Ring L], -1, -100)
							ringPan = 1
						else
							PlaySfx(SfxName[Ring R], false)
							SetSfxAttributes(SfxName[Ring R], -1, 100)
							ringPan = 0
						end if
					end if
				end if
			end if
		next
	end if
end event


event ObjectStartup
	LoadSpriteSheet("Special/Objects.gif")

	foreach (TypeName[Ring], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].angle = object[arrayPos0].ixpos
		object[arrayPos0].angle -= 448
		object[arrayPos0].angle &= 0x1FF

		Sin(object[arrayPos0].localPos.x, object[arrayPos0].angle)
		object[arrayPos0].localPos.x *= -48
		object[arrayPos0].localPos.x >>= 1

		Cos(object[arrayPos0].localPos.y, object[arrayPos0].angle)
		object[arrayPos0].localPos.y *= -48
		object[arrayPos0].localPos.y >>= 1

		if object[arrayPos0].localPos.y < -0x400
			object[arrayPos0].shadowsEnabled = true

			Sin(object[arrayPos0].localShadowPos.x, object[arrayPos0].angle)
			object[arrayPos0].localShadowPos.x *= -60
			object[arrayPos0].localShadowPos.x >>= 1

			Cos(object[arrayPos0].localShadowPos.y, object[arrayPos0].angle)
			object[arrayPos0].localShadowPos.y *= -60
			object[arrayPos0].localShadowPos.y >>= 1

			object[arrayPos0].worldShadowPos.u = 116
			if object[arrayPos0].angle > 32
				if object[arrayPos0].angle < 480
					if object[arrayPos0].angle < 256
						object[arrayPos0].worldShadowPos.u = 149
					else
						object[arrayPos0].worldShadowPos.u = 182
					end if
				end if
			end if
		end if
	next
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("Special/Objects.gif")
	SpriteFrame(-8, -8, 16, 16, 233, 0)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
