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

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

private alias object.value0  : object.timer
private alias object.value1  : object.localPos.x // unused, required for halfpipe stuff though
private alias object.value2  : object.localPos.y // unused, required for halfpipe stuff though
private alias object.value3  : object.localPos.z
private alias object.value4  : object.worldPos.x // unused, required for halfpipe stuff though
private alias object.value5  : object.worldPos.y // unused, required for halfpipe stuff though
private alias object.value6  : object.worldPos.z // unused, required for halfpipe stuff though
private alias object.value7  : object.thumbPos.y
private alias object.value8  : object.failed
private alias object.value9  : object.emblemFrame
private alias object.value10 : object.thumbsFrame

// States
private alias 0 : CHECKPOINT_SETUP
private alias 1 : CHECKPOINT_AWAITPLAYER
private alias 2 : CHECKPOINT_SHOWRESULTS
private alias 3 : CHECKPOINT_EXITMSG
private alias 4 : CHECKPOINT_SHOWGETRINGSMSG

// Text Message Property Values
private alias 0 : TEXTMESSAGE_COOL
private alias 1 : TEXTMESSAGE_NOTENOUGH
private alias 2 : TEXTMESSAGE_GETRINGS

// Text Message Aliases
private alias object.value4 : textMessage.number

// Player Aliases
private alias object.value0  : player.rings
private alias object.value14 : player.localPos.z

// Face Buffer Flag Aliases
private alias 5 : FACE_TEXTURED_C


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

reserve function Checkpoint_ProcessDrawing


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

private table Checkpoint_vertexUTable
	165, 198, 231, 264, 297, 330, 263
end table


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

private function Checkpoint_ProcessDrawing
	arrayPos0 = scene3D.vertexCount
	arrayPos1 = scene3D.faceCount
	temp0 = object.localPos.z
	temp0 >>= 22
	temp0 &= 3
	GetTableValue(arrayPos2, temp0, Halfpipe_checkpointFaceTable)
	
	temp0 = 0
	while temp0 < 7
		faceBuffer[arrayPos1].flag = FACE_TEXTURED_C
		faceBuffer[arrayPos1].a   = arrayPos0
		vertexBuffer[arrayPos0].x = vertexBuffer[arrayPos2].x
		vertexBuffer[arrayPos0].y = vertexBuffer[arrayPos2].y
		vertexBuffer[arrayPos0].z = vertexBuffer[arrayPos2].z
		GetTableValue(vertexBuffer[arrayPos0].u, object.frame, Checkpoint_vertexUTable)
		vertexBuffer[arrayPos0].v = 256
		arrayPos0++

		faceBuffer[arrayPos1].b = arrayPos0
		vertexBuffer[arrayPos0].x = vertexBuffer[arrayPos2].x
		vertexBuffer[arrayPos0].y = vertexBuffer[arrayPos2].y
		vertexBuffer[arrayPos0].z = vertexBuffer[arrayPos2].z
		vertexBuffer[arrayPos0].u = 0x800
		vertexBuffer[arrayPos0].v = 0x800
		arrayPos0++

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

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

		scene3D.vertexCount += 4
		scene3D.faceCount++
		arrayPos1++
		arrayPos2 += 4
		temp0++
	loop
end function


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

event ObjectUpdate
	switch object.state
	case CHECKPOINT_SETUP
		if object.timer < 16
			object.timer++
		else
			object.localPos.z >>= 22
			object.localPos.z <<= 22
			object.localPos.z += 0x300000
			// Destroy any existing text messages
			ResetObjectEntity(11, TypeName[Blank Object], 0, 0, 0)
			object.timer = 0
			object.state++
		end if
		break

	case CHECKPOINT_AWAITPLAYER
		object.animationTimer++
		if object.animationTimer == 4
			object.animationTimer = 0
			object.frame++
			object.frame %= 7
		end if

		CallFunction(Checkpoint_ProcessDrawing)
		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
					object.thumbPos.y = 80
#platform: USE_STANDALONE
					object.drawOrder = 5
#endplatform
#platform: USE_ORIGINS
					object.drawOrder = 6
#endplatform
					if options.vsMode == false
						object.emblemFrame = 4
						object.emblemFrame += stage.playerListPos
					else
						object.emblemFrame = 4
					end if

					object.state++

					if stage.player2Enabled == true
						temp0 = player[2].rings
						temp0 += player[3].rings
					else
						temp0 = player[2].rings
					end if
					GetTableValue(temp1, SpecialSetup_checkpointID, SpecialSetup_ringCountTable)

					if temp0 >= temp1
						object.failed = false
						CreateTempObject(TypeName[Text Message], TEXTMESSAGE_COOL, 0, 0)
#platform: USE_STANDALONE
						object[tempObjectPos].drawOrder = 5
#endplatform
#platform: USE_ORIGINS
						object[tempObjectPos].drawOrder = 6
#endplatform
						PlaySfx(SfxName[Star Post], false)
					else
						object.failed = true
						CreateTempObject(TypeName[Text Message], TEXTMESSAGE_NOTENOUGH, 0, 0)
#platform: USE_STANDALONE
						object[tempObjectPos].drawOrder = 5
#endplatform
#platform: USE_ORIGINS
						object[tempObjectPos].drawOrder = 6
#endplatform
						PlaySfx(SfxName[Fail], false)
					end if

					object.thumbsFrame = object.failed
					if object.emblemFrame == 6
						object.thumbsFrame += 2
					end if
				end if
			end if
		next
		break

	case CHECKPOINT_SHOWRESULTS
		Sin(object.thumbPos.y, object.angle)
		object.thumbPos.y >>= 7
		object.thumbPos.y += 0x50
		object.angle += 24
		object.angle &= 0x1FF

		if object.timer < 120
			if object.timer < 20
				CallFunction(Checkpoint_ProcessDrawing)
			end if
			object.timer++
		else
			object.timer = 0
			object.state++
		end if
		break

	case CHECKPOINT_EXITMSG
		if object.scale > 0
			object.scale -= 16
		else
			if object.failed == false
				object.state++
			else
				ResetObjectEntity(object.entityPos, TypeName[Blank Object], 0, 0, 0)
				ResetObjectEntity(20, TypeName[Special Finish], 0, 0, 0)
				object[20].priority = PRIORITY_ACTIVE
#platform: USE_ORIGINS
				object[20].drawOrder = 6
#endplatform
			end if
		end if
		break

	case CHECKPOINT_SHOWGETRINGSMSG
		if object.timer < 10
			object.timer++
		else
			CreateTempObject(TypeName[Text Message], TEXTMESSAGE_GETRINGS, 0, 0)
#platform: USE_STANDALONE
			object[tempObjectPos].drawOrder = 5
#endplatform
#platform: USE_ORIGINS
			object[tempObjectPos].drawOrder = 6
#endplatform

			SpecialSetup_checkpointID++
			GetTableValue(textMessage[tempObjectPos].number, SpecialSetup_checkpointID, SpecialSetup_ringCountTable)
			ResetObjectEntity(object.entityPos, TypeName[Blank Object], 0, 0, 0)
		end if
		break

	end switch
end event


event ObjectDraw
	switch object.state
	default
	case CHECKPOINT_SETUP
	case CHECKPOINT_AWAITPLAYER
	case CHECKPOINT_SHOWGETRINGSMSG
		break

	case CHECKPOINT_SHOWRESULTS
		DrawSpriteScreenXY(object.emblemFrame, screen.xcenter, 80)
		DrawSpriteScreenXY(object.thumbsFrame, screen.xcenter, object.thumbPos.y)
		break

	case CHECKPOINT_EXITMSG
		DrawSpriteScreenFX(object.emblemFrame, FX_SCALE, screen.xcenter, 80)
		DrawSpriteScreenFX(object.thumbsFrame, FX_SCALE, screen.xcenter, object.thumbPos.y)
		break
		
	end switch
end event


event ObjectStartup
	LoadSpriteSheet("Special/Objects.gif")
	
	SpriteFrame(-24, -24, 48, 48, 463, 42) 	// Thumbs up - #0
	SpriteFrame(-24, -24, 48, 48, 366, 155) // Thumbs down - #1
	SpriteFrame(-24, -24, 48, 48, 463, 328) // Thumbs up (Knux) - #2
	SpriteFrame(-24, -24, 48, 48, 463, 377) // Thumbs down (Knux) - #3
	SpriteFrame(-48, -24, 96, 48, 415, 122) // Emblem (Sonic) - #4
	SpriteFrame(-48, -24, 96, 48, 415, 171) // Emblem (Tails) - #5
	SpriteFrame(-48, -24, 96, 48, 415, 220) // Emblem (Knux) - #6
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("Special/Objects.gif")
	SpriteFrame(-16, -8, 32, 16, 199, 165)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
	SetVariableAlias(ALIAS_VAR_VAL0, "ringCount2P")
	SetVariableAlias(ALIAS_VAR_VAL1, "ringCountSonic")
	SetVariableAlias(ALIAS_VAR_VAL2, "ringCountKnux")
	SetVariableAlias(ALIAS_VAR_VAL3, "unused")
end event
