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

//-------Aliases-------//
private alias 39 : TYPE_GHZSETUP

// Function declarations
reserve function GHZSetup_Function93
reserve function GHZSetup_Function94

reserve function GHZSetup_GHZ2SpeedUp
reserve function GHZSetup_GHZ2SlowDown

reserve function GHZSetup_GHZ3SpeedUp
reserve function GHZSetup_GHZ3SlowDown

// Static Values
public value GHZSetup_value29 = 0;
public value GHZSetup_value30 = 0;
public value GHZSetup_value31 = 0;

// Tables

public table GHZSetup_table0
	0x500000, 0x3B00000, 0, 11, 8, 91, 56, 1, 40, 7, 8, 87, 56, 1, 40, 8, 8, 47, 56, 1, 40, 7, 8, 204, 56, 1, 40, 7, 8, 32, 56, 1, 40, 9, 8, 43, 56, 1, 40, 28, 8, 61, 56, 1, 40, 24, 8, 30, 56, 1, 40, 44, 8, 22, 56, 1, 40, 21, 8, 42, 56, 1, 40, 107, 8, 255, 8, 33, 0, 203, 4, 44, 0, 12, 4, 11, 0, 23, 8, 20, 0, 15, 4, 4, 0, 19, 8, 16, 56, 1, 40, 10, 8, 39, 56, 1, 40, 8, 8, 42, 56, 1
end table

public table GHZSetup_table1
	0x500000, 0x3B40000, 0, 11, 8, 91, 56, 1, 40, 10, 8, 75, 4, 12, 52, 1, 36, 24, 4, 19, 52, 1, 36, 7, 4, 8, 0, 27, 8, 55, 56, 1, 40, 4, 32, 5, 0, 29, 4, 25, 0, 11, 8, 34, 0, 11, 8, 30, 56, 1, 40, 9, 8, 202, 56, 1, 40, 9, 8, 35, 56, 1, 40, 8, 8, 45, 56, 1, 40, 14, 8, 17, 0, 7, 8, 52, 56, 1, 40, 17, 8, 33, 56, 1, 40, 33, 8, 2, 0, 29, 8, 14, 56, 1, 40, 6, 8, 5, 0, 13, 4, 9, 0, 11, 48, 1, 32, 6, 8, 6, 56, 1, 40, 6, 8, 16, 56, 1, 40, 5, 8, 33, 0, 8, 4, 11, 0, 21, 8, 9, 0, 31, 48, 1, 32, 6, 0, 2, 8, 28, 0, 28, 4, 5, 0, 5, 8, 25, 56, 1, 40, 2, 8, 28, 0, 6, 4, 9, 0, 6, 8, 31, 0, 45, 56, 1, 40, 5, 0, 2, 8, 9, 0, 7, 8, 10, 0, 30, 8, 12, 0, 37, 4, 17, 0, 22, 8, 20, 56, 1, 40, 11, 8, 52, 56, 1, 40, 12
end table

public table GHZSetup_table2
	0x500000, 0x3B00000, 0, 11, 8, 92, 56, 1, 40, 12, 8, 82, 2, 12, 0, 24, 48, 1, 32, 26, 0, 3, 8, 2, 56, 1, 40, 88, 8, 2, 0, 28, 2, 16, 50, 1, 34, 7, 2, 5, 50, 1, 34, 2, 2, 5, 50, 1, 34, 3, 2, 3, 50, 1, 34, 2, 2, 4, 8, 105, 0, 109, 8, 12, 56, 1, 40, 44, 8, 51, 56, 1, 40, 33, 8, 17, 56, 1, 40, 40, 8, 31, 56, 1, 40, 39, 8, 24, 56, 1, 40, 64, 8, 17, 56, 1, 40, 15, 8, 241, 0, 29, 48, 1, 32, 18, 0, 4, 48, 1, 32, 30, 33, 3, 1, 171, 0, 15, 2, 12, 50, 1, 34, 7, 2, 4, 50, 1, 34, 2, 2, 6, 50, 1, 34, 2, 2, 4, 50, 1, 34, 5, 2, 22, 0, 2, 8, 47, 0, 6
end table

public table GHZSetup_table3
	0x500000, 0x3B00000, 8, 91, 56, 1, 40, 9, 8, 83, 56, 1, 40, 5, 8, 52, 56, 1, 40, 8, 8, 160, 0, 8, 8, 26, 56, 1, 40, 3, 32, 2, 0, 6, 4, 5, 0, 13, 8, 44, 56, 1, 40, 5, 8, 17, 0, 16, 8, 13, 8, 5, 8, 42, 0, 52, 8, 30
end table

public table GHZSetup_table4
	0x13E80000, 0x2AC0000, 0, 17, 8, 255, 8, 31, 0, 219
end table


private table GHZwateroriginal
	160, 0, 128, 224
	168, 96, 128, 160
	169, 96, 128, 224
	170, 128, 160, 224
	171, 160, 192, 224
end table

private table GHZwaterforever
	160, 32, 64, 224
	168, 64, 96, 224
	169, 96, 128, 224
	170, 128, 160, 224
	171, 160, 192, 224
end table

private table GHZwatertitle
	160, 0, 0, 128
	168, 32, 64, 192
	169, 96, 128, 224
	170, 160, 192, 224
	171, 192, 224, 224
end table

function GHZSetup_Function93
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility_F.ogg", 2, 0x76E4)
			SwapMusicTrack("GreenHill_F.ogg", 0, 0x7AA16, 0x1EF0)
			break
		case 2
			SetMusicTrack("GreenHill_F.ogg", 0, 0x7AA16)
			SwapMusicTrack("Invincibility_F.ogg", 2, 0x76E4, 0x1F40)
			break
		default
			SetMusicTrack("GreenHill_F.ogg", 0, 0x7AA16)
			SetMusicTrack("Invincibility_F.ogg", 2, 0x76E4)
			break
		end switch
	else
		stage.musicFlag = 1
	end if
end function


function GHZSetup_Function94
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility.ogg", 2, 0x9A68)
			SwapMusicTrack("GreenHill.ogg", 0, 0x9B0AA, 0x316A)
			break
		case 2
			SetMusicTrack("GreenHill.ogg", 0, 0x9B0AA)
			SwapMusicTrack("Invincibility.ogg", 2, 0x9A68, 0x30D4)
			break
		default
			SetMusicTrack("GreenHill.ogg", 0, 0x9B0AA)
			SetMusicTrack("Invincibility.ogg", 2, 0x9A68)
			break
		end switch
	else
		stage.musicFlag = 2
	end if
end function


function GHZSetup_GHZ2SpeedUp
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility_F.ogg", 2, 0x76E4)
			SwapMusicTrack("GreenHill2_F.ogg", 0, 14700, 8000)
			break
		case 2
			SetMusicTrack("GreenHill2_F.ogg", 0, 14700)
			SwapMusicTrack("Invincibility_F.ogg", 2, 0x76E4, 0x1F40)
			break
		default
			SetMusicTrack("GreenHill2_F.ogg", 0, 14700)
			SetMusicTrack("Invincibility_F.ogg", 2, 0x76E4)
			break
		end switch
	else
		stage.musicFlag = 1
	end if
end function


function GHZSetup_GHZ2SlowDown
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility.ogg", 2, 0x9A68)
			SwapMusicTrack("GreenHill2.ogg", 0, 18375, 0x30D4)
			break
		case 2
			SetMusicTrack("GreenHill2.ogg", 0, 18375)
			SwapMusicTrack("Invincibility.ogg", 2, 0x9A68, 0x30D4)
			break
		default
			SetMusicTrack("GreenHill2.ogg", 0, 18375)
			SetMusicTrack("Invincibility.ogg", 2, 0x9A68)
			break
		end switch
	else
		stage.musicFlag = 2
	end if
end function


function GHZSetup_GHZ3SpeedUp
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility_F.ogg", 2, 0x76E4)
			SwapMusicTrack("GreenHill3_F.ogg", 0, 1, 8000)
			break
		case 2
			SetMusicTrack("GreenHill3_F.ogg", 0, 1)
			SwapMusicTrack("Invincibility_F.ogg", 2, 0x76E4, 0x1F40)
			break
		default
			SetMusicTrack("GreenHill3_F.ogg", 0, 1)
			SetMusicTrack("Invincibility_F.ogg", 2, 0x76E4)
			break
		end switch
	else
		stage.musicFlag = 1
	end if
end function


function GHZSetup_GHZ3SlowDown
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility.ogg", 2, 0x9A68)
			SwapMusicTrack("GreenHill3.ogg", 0, 1, 0x30D4)
			break
		case 2
			SetMusicTrack("GreenHill3.ogg", 0, 1)
			SwapMusicTrack("Invincibility.ogg", 2, 0x9A68, 0x30D4)
			break
		default
			SetMusicTrack("GreenHill3.ogg", 0, 1)
			SetMusicTrack("Invincibility.ogg", 2, 0x9A68)
			break
		end switch
	else
		stage.musicFlag = 2
	end if
end function


event ObjectMain
	if GHZSetup_value30 != 0
		GHZSetup_value30 = 0
		object.value4--
		if object.value4 <= 0
			object.value4 = 31
			if object.value5 == 0
				if credits.screen == 0
					PlaySfx(SfxName[Waterfall], 0)
					StopSfx(SfxName[Waterfall Loop])
				end if
				object.value5 = 1
			else
				if credits.screen == 0
					StopSfx(SfxName[Waterfall])
					PlaySfx(SfxName[Waterfall Loop], 0)
				end if
			end if
		end if
		SetSfxAttributes(SfxName[Waterfall], -1, GHZSetup_value31)
		SetSfxAttributes(SfxName[Waterfall Loop], -1, GHZSetup_value31)
	else
		object.value5 = 0
		object.value4 = 0
	end if
	if object.state < 5
		object.value3++
		if object.value3 == 6
			object.value3 = 0
			RotatePalette(0, 168, 171, 1)
			GHZSetup_value29++
			GHZSetup_value29 &= 15
		end if
	end if
	if object.value0 == 0
		object.value0 = 20
		temp0 = 768
		temp0 += object.frame
		Copy16x16Tile(427, temp0)
		temp0++
		Copy16x16Tile(428, temp0)
		temp0++
		Copy16x16Tile(429, temp0)
		temp0++
		Copy16x16Tile(430, temp0)
		object.frame += 4
		object.frame &= 7
	else
		object.value0--
	end if
	if object.value1 == 0
		switch object.value2
		case 0
			object.value1 = 160
			temp0 = 0
			break
		case 1
			object.value1 = 10
			temp0 = 6
			break
		case 2
			object.value1 = 160
			temp0 = 12
			break
		case 3
			object.value1 = 10
			temp0 = 6
			break
		end switch
		temp0 += 776
		Copy16x16Tile(431, temp0)
		temp0++
		Copy16x16Tile(432, temp0)
		temp0++
		Copy16x16Tile(433, temp0)
		temp0++
		Copy16x16Tile(434, temp0)
		temp0++
		Copy16x16Tile(435, temp0)
		temp0++
		Copy16x16Tile(436, temp0)
		object.value2++
		object.value2 &= 3
	else
		object.value1--
	end if
end event


event ObjectStartup
	if saved.actTheme == 1
		switch stage.actNum
		default
			SetMusicTrack("GreenHill.ogg", 0, 0x9B0AA)
			SpeedUpMusic = GHZSetup_Function93
			SlowDownMusic = GHZSetup_Function94
			break
		case 2
			SetMusicTrack("GreenHill2.ogg", 0, 18375)
			SpeedUpMusic = GHZSetup_GHZ2SpeedUp
			SlowDownMusic = GHZSetup_GHZ2SlowDown
			break
		case 3
			SetMusicTrack("GreenHill3.ogg", 0, 1)
			SpeedUpMusic = GHZSetup_GHZ3SpeedUp
			SlowDownMusic = GHZSetup_GHZ3SlowDown
			break			
		end switch
	else
		SetMusicTrack("GreenHill.ogg", 0, 0x9B0AA)
		SpeedUpMusic = GHZSetup_Function93
		SlowDownMusic = GHZSetup_Function94
	end if
	animalType1 = TypeName[Pocky]
	animalType2 = TypeName[Flicky]
	Abs(tileLayer[1].parallaxFactor)
	FlipSign(tileLayer[1].parallaxFactor)
	tileLayer[1].scrollPos = 0x180000
	object[10].type = TypeName[GHZ Setup]
	object[10].priority = PRIORITY_ACTIVE
	GHZSetup_value29 = 0
	if options.attractMode == 1
		switch stage.playerListPos
		case 0
		case 7
		case 8
			switch credits.screen
			case 0
				PlayerObject_ReplayTable = GHZSetup_table0
				PlayerObject_ReplaySize = 102
				PlayerObject_ReplayLength = 0x708
				break
			case 1
				PlayerObject_ReplayTable = GHZSetup_table3
				PlayerObject_ReplaySize = 58
				PlayerObject_ReplayLength = 540
				break
			case 8
				PlayerObject_ReplayTable = GHZSetup_table4
				PlayerObject_ReplaySize = 10
				PlayerObject_ReplayLength = 540
				break
			end switch
			break
		case 1
			switch credits.screen
			case 0
				PlayerObject_ReplayTable = GHZSetup_table1
				PlayerObject_ReplaySize = 198
				PlayerObject_ReplayLength = 0x708
				break
			case 1
				PlayerObject_ReplayTable = GHZSetup_table3
				PlayerObject_ReplaySize = 58
				PlayerObject_ReplayLength = 540
				break
			case 8
				PlayerObject_ReplayTable = GHZSetup_table4
				PlayerObject_ReplaySize = 10
				PlayerObject_ReplayLength = 540
				break
			end switch
			break
		case 2
			switch credits.screen
			case 0
				PlayerObject_ReplayTable = GHZSetup_table2
				PlayerObject_ReplaySize = 150
				PlayerObject_ReplayLength = 0x708
				break
			case 1
				PlayerObject_ReplayTable = GHZSetup_table3
				PlayerObject_ReplaySize = 58
				PlayerObject_ReplayLength = 540
				break
			case 8
				PlayerObject_ReplayTable = GHZSetup_table4
				PlayerObject_ReplaySize = 10
				PlayerObject_ReplayLength = 540
				break
			end switch
			break
		case 5 //sonic copy, may need new one?
			switch credits.screen
			case 0
				PlayerObject_ReplayTable = GHZSetup_table0
				PlayerObject_ReplaySize = 102
				PlayerObject_ReplayLength = 0x708
				break
			case 1
				PlayerObject_ReplayTable = GHZSetup_table3
				PlayerObject_ReplaySize = 58
				PlayerObject_ReplayLength = 540
				break
			case 8
				PlayerObject_ReplayTable = GHZSetup_table4
				PlayerObject_ReplaySize = 10
				PlayerObject_ReplayLength = 540
				break
			end switch
			break
		end switch
		CallFunction(PlayerObject_InitReplay)
	end if
	
	switch saved.GHZwater
		case 1 //Forever
			temp5 = GHZwaterforever
			break
		case 2 //Title Screen
			temp5 = GHZwatertitle
			break
		default
			temp5 = GHZwateroriginal
			break
	end switch
	
	
	temp0 = 0
	while temp0 < 20
		GetTableValue(temp1, temp0, temp5)
		temp0++
		GetTableValue(temp2, temp0, temp5)
		temp0++
		GetTableValue(temp3, temp0, temp5)
		temp0++
		GetTableValue(temp4, temp0, temp5)
		temp0++
		
		temp2 <<= 8
		temp2 += temp3
		temp2 <<= 8
		temp2 += temp4				
		SetPaletteEntry(0, temp1, temp2)
	loop
end event

event RSDKDraw
	DrawSprite(0)
end event

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