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

//-------Aliases-------//
private alias 2 : TYPE_SPECIALSETUP

	private alias object.value32 : pause.sel
	private alias object.value33 : pause.state
	private alias object.value34 : pause.xoffset
	private alias object.value35 : pause.yoffset
	private alias object.value36 : pause.fade
	private alias object.value28 : ring.tick

// Function declarations
reserve function SpecialSetup_Function7
reserve function SpecialSetup_Function8
reserve function SpecialSetup_Function9
reserve function SpecialSetup_Function10

// Static Values
public value SpecialSetup_value10 = 0;
public value SpecialSetup_value11 = 0;
public value SpecialSetup_value12 = 0;

public alias saveRAM[8004] : saved.tailsclear
public alias saveRAM[8014] : saved.knuxclear
public alias saveRAM[8015] : saved.amyclear
public alias saveRAM[8016] : saved.rayclear
public alias saveRAM[8017] : saved.mightyclear

// Tables
public table SpecialSetup_table0
	0xF00000, 0x340000, 0, 68, 48, 1, 32, 2, 36, 25, 4, 12, 0, 8, 8, 28, 0, 53, 48, 1, 32, 30, 40, 8, 8, 2, 0, 3, 4, 35, 0, 85, 4, 21, 0, 66, 4, 19, 0, 68, 56, 1, 40, 24, 8, 20, 56, 1, 40, 12, 8, 124, 0, 73, 8, 12, 56, 1, 40, 7, 8, 6, 0, 7, 4, 31, 0, 31, 48, 1, 32, 5, 0, 30, 8, 24, 0, 21, 56, 1, 40, 8, 8, 6, 0, 17, 8, 43, 56, 1, 40, 12, 8, 27, 
end table

function SpecialSetup_Function7
	temp0 = 0
	while temp0 < 16
		Copy16x16Tile(temp1, temp2)
		temp1++
		temp2++
		temp0++
	loop
end function


function SpecialSetup_Function8
	temp0 = oscillation
	temp0 <<= 1
	Sin(hParallax[8].scrollPos, temp0)
	hParallax[8].scrollPos <<= 10
	temp0 = oscillation
	temp0 <<= 2
	Sin(hParallax[9].scrollPos, temp0)
	hParallax[9].scrollPos <<= 9
	temp0 = oscillation
	temp0 <<= 1
	temp0 += 256
	Sin(hParallax[10].scrollPos, temp0)
	hParallax[10].scrollPos <<= 11
	temp0 = oscillation
	temp0 <<= 2
	temp0 += 128
	Sin(hParallax[11].scrollPos, temp0)
	hParallax[11].scrollPos <<= 9
end function


function SpecialSetup_Function9
	temp2 = object.xpos
	temp2 -= object[0].xpos
	temp2 >>= 8
	temp3 = object.ypos
	temp3 -= object[0].ypos
	temp3 >>= 8
	Sin(temp4, PlayerObject_value1)
	temp4 *= temp3
	Cos(temp5, PlayerObject_value1)
	temp5 *= temp2
	temp0 = temp4
	temp0 += temp5
	temp0 >>= 1
	temp0 += object[0].xpos
	Cos(temp4, PlayerObject_value1)
	temp4 *= temp3
	Sin(temp5, PlayerObject_value1)
	temp5 *= temp2
	temp1 = temp4
	temp1 -= temp5
	temp1 >>= 1
	temp1 += object[0].ypos
	object.rotation = 512
	object.rotation -= PlayerObject_value1
end function


function SpecialSetup_Function10
	BoxCollisionTest(C_BOX2, object.entityPos, -12, -12, 12, 12, 0, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
	if checkResult != 0
		SetBit(object[0].value11, checkResult, 1)
	else
		BoxCollisionTest(C_TOUCH, object.entityPos, -10, -10, 10, 10, 0, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
		if checkResult == 1
			temp0 = object[0].xvel
			temp1 = object[0].yvel
			Abs(temp0)
			Abs(temp1)
			if temp0 > temp1
				if object[0].xvel > 0
					SetBit(object[0].value11, 2, 1)
					object[0].xpos = object[0].collisionLeft
					object[0].xpos -= 12
					object[0].xpos <<= 16
				else
					SetBit(object[0].value11, 3, 1)
					object[0].xpos = object[0].collisionRight
					object[0].xpos += 12
					object[0].xpos <<= 16
				end if
				object[0].xpos += object.xpos
			else
				if object[0].yvel > 0
					SetBit(object[0].value11, 1, 1)
					object[0].ypos = object[0].collisionTop
					object[0].ypos -= 12
					object[0].ypos <<= 16
				else
					SetBit(object[0].value11, 4, 1)
					object[0].ypos = object[0].collisionBottom
					object[0].ypos += 12
					object[0].ypos <<= 16
				end if
				object[0].ypos += object.ypos
			end if
		end if
	end if
end function


event ObjectMain
	if stage.state == STAGE_RUNNING
		
		//Stage Rotate
		if PlayerObject_value3 == 0
			switch PlayerObject_value2
			case 0
				temp0 = oscillation
				temp0 &= 1
				specialStageAngle -= temp0
				if specialStageAngle < 0
					specialStageAngle += 512
				end if
				break
			case 1
				specialStageAngle--
				if specialStageAngle < 0
					specialStageAngle += 512
				end if
				break
			end switch
		else
			switch PlayerObject_value2
			case 0
				temp0 = oscillation
				temp0 &= 1
				specialStageAngle += temp0
				specialStageAngle &= 511
				break
			case 1
				specialStageAngle++
				specialStageAngle &= 511
				break
			end switch
		end if
		PlayerObject_value1 = specialStageAngle
		
		//Restore original spin
		if saved.specialSpin != 1
			PlayerObject_value1 &= 504
		end if
		
		oscillation++
		oscillation &= 511
		SpecialSetup_value10++
		SpecialSetup_value10 %= 20
		SpecialSetup_value12++
		SpecialSetup_value12 &= 63
		switch object.state
		case 0
			object.value0++
			if object.value0 == 8
				object.value0 = 0
				object.value1 = 0
				object.value2 = 160
				object.value3 = 160
				object.value4 = 255
				temp1 = 184
				temp2 = 264
				CallFunction(SpecialSetup_Function7)
				object.state++
			end if
			break
		case 1
			object.value0++
			if object.value0 == 4
				object.value0 = 0
				object.value1 = 0
				object.value2 = 96
				object.value3 = 128
				object.value4 = 255
				object.state++
			end if
			break
		case 2
			object.value0++
			if object.value0 == 4
				object.value0 = 0
				object.value1 = 0
				object.value2 = 64
				object.value3 = 96
				object.value4 = 255
				object.state++
			end if
			break
		case 3
			object.value0++
			if object.value0 == 4
				object.value0 = 0
				object.value1 = 0
				object.value2 = 0
				object.value3 = 64
				object.value4 = 255
				object.state++
			end if
			break
		case 4
			object.value0++
			if object.value0 == 4
				object.value0 = 0
				stage[0].activeLayer = 1
				object.value1 = 0
				object.value2 = 0
				object.value3 = 64
				object.value4 = 224
				temp1 = 104
				temp2 = 136
				CallFunction(SpecialSetup_Function7)
				object.state++
			end if
			break
		case 5
			if object.value4 > 0
				object.value4 -= 16
			end if
			object.value0++
			if object.value0 == 8
				object.value0 = 0
				temp1 = 104
				temp2 = 152
				CallFunction(SpecialSetup_Function7)
				object.state++
			end if
			break
		case 6
			if object.value4 > 0
				object.value4 -= 16
			end if
			object.value0++
			if object.value0 == 8
				object.value0 = 0
				stage[1].activeLayer = 4
				hParallax[0].scrollSpeed = -0x10000
				object.state++
			end if
			break
		case 7
			object.value0++
			if object.value0 == 1020
				object.value0 = 0
				stage[1].activeLayer = 3
				hParallax[0].scrollSpeed = 0
				object.state++
			end if
			break
		case 8
			if object.value4 < 0
				object.value4 += 16
			end if
			object.value0++
			if object.value0 == 8
				object.value0 = 0
				temp1 = 104
				temp2 = 136
				CallFunction(SpecialSetup_Function7)
				object.state++
			end if
			break
		case 9
			if object.value4 < 0
				object.value4 += 16
			end if
			object.value0++
			if object.value0 == 8
				object.value0 = 0
				object.value1 = 0
				object.value2 = 0
				object.value3 = 64
				object.value4 = 255
				temp1 = 104
				temp2 = 120
				CallFunction(SpecialSetup_Function7)
				object.state++
			end if
			break
		case 10
			object.value0++
			if object.value0 == 4
				object.value0 = 0
				object.value1 = 0
				object.value2 = 64
				object.value3 = 96
				object.value4 = 255
				object.state++
			end if
			break
		case 11
			object.value0++
			if object.value0 == 4
				object.value0 = 0
				object.value1 = 0
				object.value2 = 96
				object.value3 = 128
				object.value4 = 255
				object.state++
			end if
			break
		case 12
			object.value0++
			if object.value0 == 4
				object.value0 = 0
				object.value1 = 0
				object.value2 = 160
				object.value3 = 160
				object.value4 = 255
				object.state++
			end if
			break
		case 13
			object.value0++
			if object.value0 == 4
				object.value0 = 0
				object.value4 = 0
				temp1 = 184
				temp2 = 200
				CallFunction(SpecialSetup_Function7)
				object.state++
			end if
			break
		case 14
			object.value0++
			if object.value0 == 8
				object.value0 = 0
				object.value1 = 96
				object.value2 = 192
				object.value3 = 64
				object.value4 = 255
				temp1 = 104
				temp2 = 264
				CallFunction(SpecialSetup_Function7)
				object.state++
			end if
			break
		case 15
			object.value0++
			if object.value0 == 4
				object.value0 = 0
				object.value1 = 64
				object.value2 = 128
				object.value3 = 64
				object.value4 = 255
				object.state++
			end if
			break
		case 16
			object.value0++
			if object.value0 == 4
				object.value0 = 0
				object.value1 = 32
				object.value2 = 64
				object.value3 = 64
				object.value4 = 255
				object.state++
			end if
			break
		case 17
			object.value0++
			if object.value0 == 4
				object.value0 = 0
				object.value1 = 0
				object.value2 = 0
				object.value3 = 64
				object.value4 = 255
				object.state++
			end if
			break
		case 18
			object.value0++
			if object.value0 == 4
				object.value0 = 0
				stage[0].activeLayer = 2
				object.value1 = 0
				object.value2 = 0
				object.value3 = 64
				object.value4 = 224
				temp1 = 184
				temp2 = 216
				CallFunction(SpecialSetup_Function7)
				object.state++
			end if
			break
		case 19
			CallFunction(SpecialSetup_Function8)
			if object.value4 > 0
				object.value4 -= 16
			end if
			object.value0++
			if object.value0 == 8
				object.value0 = 0
				temp1 = 184
				temp2 = 232
				CallFunction(SpecialSetup_Function7)
				object.state++
			end if
			break
		case 20
			CallFunction(SpecialSetup_Function8)
			if object.value4 > 0
				object.value4 -= 16
			end if
			object.value0++
			if object.value0 == 8
				object.value0 = 0
				stage[1].activeLayer = 5
				hParallax[0].scrollSpeed = 0x10000
				object.state++
			end if
			break
		case 21
			CallFunction(SpecialSetup_Function8)
			object.value0++
			if object.value0 == 1020
				object.value0 = 0
				stage[1].activeLayer = 3
				hParallax[0].scrollSpeed = 0
				object.state++
			end if
			break
		case 22
			CallFunction(SpecialSetup_Function8)
			if object.value4 < 0
				object.value4 += 16
			end if
			object.value0++
			if object.value0 == 8
				object.value0 = 0
				temp1 = 184
				temp2 = 216
				CallFunction(SpecialSetup_Function7)
				object.state++
			end if
			break
		case 23
			CallFunction(SpecialSetup_Function8)
			if object.value4 < 0
				object.value4 += 16
			end if
			object.value0++
			if object.value0 == 8
				object.value0 = 0
				object.value1 = 0
				object.value2 = 0
				object.value3 = 64
				object.value4 = 255
				temp1 = 184
				temp2 = 200
				CallFunction(SpecialSetup_Function7)
				object.state++
			end if
			break
		case 24
			object.value0++
			if object.value0 == 4
				object.value0 = 0
				object.value1 = 32
				object.value2 = 64
				object.value3 = 64
				object.value4 = 255
				object.state++
			end if
			break
		case 25
			object.value0++
			if object.value0 == 4
				object.value0 = 0
				object.value1 = 64
				object.value2 = 128
				object.value3 = 64
				object.value4 = 255
				object.state++
			end if
			break
		case 26
			object.value0++
			if object.value0 == 4
				object.value0 = 0
				object.value1 = 96
				object.value2 = 192
				object.value3 = 64
				object.value4 = 255
				object.state++
			end if
			break
		case 27
			object.value0++
			if object.value0 == 4
				object.value0 = 0
				object.value4 = 0
				temp1 = 104
				temp2 = 120
				CallFunction(SpecialSetup_Function7)
				object.state = 0
			end if
			break
		end switch
		ringTimer++
		if ringTimer == 4
			ringTimer = 0
			ringFrame++
			ringFrame &= 7
		end if
	end if
end event


event ObjectDraw
	if stage.state == STAGE_RUNNING
		screen.xoffset = object[0].xpos
		screen.xoffset >>= 16
		screen.xoffset -= screen.xcenter
		screen.yoffset = object[0].ypos
		screen.yoffset >>= 16
		screen.yoffset -= 104
		DrawRect(0, 0, screen.xsize, screen.ysize, object.value1, object.value2, object.value3, object.value4)
	end if
end event


event ObjectStartup
	SetMusicTrack("Specialstage.ogg", 0, 1)
	switch stage.playerlistpos
	case 0
		SetMusicTrack("ActComplete.ogg", 1, 0)
		object.victorylength = 299
		break
	case 1
		if saved.tailsclear == 1
			SetMusicTrack("ActClearTails.ogg", 1, 0)
			object.victorylength = 169
		else
			SetMusicTrack("ActComplete.ogg", 1, 0)
			object.victorylength = 299
		end if
		break
	case 2
		switch saved.knuxclear 
		case 1
			SetMusicTrack("ActClearKnux.ogg", 1, 0)
			object.victorylength = 259
			break
		case 0	
			SetMusicTrack("ActComplete.ogg", 1, 0)
			object.victorylength = 299
			break
		case 2
			SetMusicTrack("ActClearMighty.ogg", 1, 0)
			object.victorylength = 219
			break			
		end switch	
		break
	case 5
		if saved.amyclear == 1
			SetMusicTrack("ActClearAmy.ogg", 1, 0)
			object.victorylength = 249
		else
			SetMusicTrack("ActComplete.ogg", 1, 0)
			object.victorylength = 299
		end if
		break
	case 7
		switch saved.mightyclear 
		case 2
			SetMusicTrack("ActClearRay.ogg", 1, 0)
			object.victorylength = 169
			break		
		case 1
			SetMusicTrack("ActClearMighty.ogg", 1, 0)
			object.victorylength = 219
			break
		case 0
			SetMusicTrack("ActComplete.ogg", 1, 0)
			object.victorylength = 299
			break
		end switch		
		break
	case 8
		if saved.rayclear == 1
			SetMusicTrack("ActClearRay.ogg", 1, 0)
			object.victorylength = 169
		else
			SetMusicTrack("ActComplete.ogg", 1, 0)
			object.victorylength = 299
		end if
		break			
	end switch
	LoadPalette("SpecialStage.act",0,0,0,256)
	//if options.gameMode == 2
		object[9].type = TypeName[HUD]
		object[9].priority = PRIORITY_ACTIVE
		object[9].drawOrder = 6
		object[9].propertyValue = object[arrayPos0].propertyValue
	//end if
	object[19].type = TypeName[Special Setup]
	object[19].priority = PRIORITY_ACTIVE_PAUSED
	object[19].drawOrder = 1
	object[20].type = TypeName[Fade In]
	object[20].priority = PRIORITY_ACTIVE
	stage[0].activeLayer = 9
	stage[1].activeLayer = 3
	stage[2].activeLayer = 9
	stage[3].activeLayer = 9
	if options.attractMode == 0
		stage.pauseEnabled = 1
	else
		stage.pauseEnabled = 0
	end if
	ringExtraLife = 100
	SpecialSetup_value10 = 0
	SpecialSetup_value11 = 0
	SpecialSetup_value12 = 0
	PlayerObject_value2 = 0
	PlayerObject_value3 = 0
	if options.attractMode == 1
		PlayerObject_ReplayData = SpecialSetup_table0
		PlayerObject_ReplaySize = 94
		PlayerObject_ReplayLength = 0x4B0
		CallFunction(PlayerObject_InitReplay)
	end if
	if stage.actNum == 8
		ReadSaveRAM()
		arrayPos0 = 32
		arrayPos1 = 0x800
		while arrayPos0 < 0x420
			temp0 = saveRAM[arrayPos1]
			temp0 &= 255
			temp1 = saveRAM[arrayPos1]
			temp1 >>= 8
			arrayPos1++
			temp2 = saveRAM[arrayPos1]
			temp2 &= 0x7FF
			temp2 *= 0x180000
			temp2 += 0xC0000
			temp3 = saveRAM[arrayPos1]
			temp3 >>= 11
			temp3 *= 0x180000
			temp3 += 0xC0000
			ResetObjectEntity(arrayPos0, temp0, temp1, temp2, temp3)
			arrayPos1++
			arrayPos0++
		loop
	end if
	
	if stage.actNum == 8
		object[0].type = TypeName[Debug Mode]
		if stage.playerListPos == 1
			object[1].type = TypeName[Blank Object]
		end if
		object[0].value2 = 0
		object[0].rotation = 0
		object[0].frame = 0
		PlayerObject_value1 = 0
		PlayerObject_value2 = 2
		object[0].frame = object.value10
		specialStageAngle = 0
		object[0].interaction = 0	
	end if
end event
	
	


event RSDKDraw
	DrawSprite(0)
end event

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