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

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

private alias object.frame : object.sunFlowerFrame
private alias object.value0 : object.sunFlowerTimer
private alias object.value1 : object.extendFlowerTimer
private alias object.value2 : object.extendFlowerFrame
private alias object.value3 : object.paletteTimer
private alias object.value4 : object.waterfallLoopTimer
private alias object.value5 : object.playingWaterfallLoop

// Tracks
private alias 0 : TRACK_STAGE
private alias 2 : TRACK_INVINCIBLE
private alias 4 : TRACK_BOSS

// Reserved object slots
private alias 10 : SLOT_ZONESETUP
private alias 25 : SLOT_MUSICEVENT_CHANGE

// Music Events
private alias 0 : MUSICEVENT_FADETOBOSS
private alias 1 : MUSICEVENT_FADETOSTAGE
private alias 2 : MUSICEVENT_TRANSITION

private alias 0 : MUSICEVENT_FLAG_NOCHANGE
private alias 1 : MUSICEVENT_FLAG_SPEEDUP
private alias 2 : MUSICEVENT_FLAG_SLOWDOWN

// Music loops
private alias 635050 : MUSIC_LOOP_GHZ   // Interestingly, this loop point was instead 635970 in older S1 versions...
private alias 502294 : MUSIC_LOOP_GHZ_F // ...but this sped-up version had its loop point stay the same...

private alias 39528  : MUSIC_LOOP_INV
private alias 30436  : MUSIC_LOOP_INV_F


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

reserve function GHZSetup_SpeedUpMusic
reserve function GHZSetup_SlowDownMusic


// ========================
// Static Values
// ========================

public value GHZSetup_spikeLogsCounter 		= 0

// These next two values are used to transmit values from [Waterfall Sound] Objects
public value GHZSetup_playWaterfallSfx 		= 0
public value GHZSetup_waterfallPan 			= 0


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

private table GHZSetup_replay_attract_S
	0x00500000, 0x3B00000
	0x00, 11
	0x08, 91
	0x38, 1
	0x28, 7
	0x08, 87
	0x38, 1
	0x28, 8
	0x08, 47
	0x38, 1
	0x28, 7
	0x08, 204
	0x38, 1
	0x28, 7
	0x08, 32
	0x38, 1
	0x28, 9
	0x08, 43
	0x38, 1
	0x28, 28
	0x08, 61
	0x38, 1
	0x28, 24
	0x08, 30
	0x38, 1
	0x28, 44
	0x08, 22
	0x38, 1
	0x28, 21
	0x08, 42
	0x38, 1
	0x28, 107
	0x08, 255
	0x08, 33
	0x00, 203
	0x04, 44
	0x00, 12
	0x04, 11
	0x00, 23
	0x08, 20
	0x00, 15
	0x04, 4
	0x00, 19
	0x08, 16
	0x38, 1
	0x28, 10
	0x08, 39
	0x38, 1
	0x28, 8
	0x08, 42
	0x38, 1
end table

private table GHZSetup_replay_attract_T
	0x00500000, 0x3B40000
	0x00, 11
	0x08, 91
	0x38, 1
	0x28, 10
	0x08, 75
	0x04, 12
	0x34, 1
	0x24, 24
	0x04, 19
	0x34, 1
	0x24, 7
	0x04, 8
	0x00, 27
	0x08, 55
	0x38, 1
	0x28, 4
	0x20, 5
	0x00, 29
	0x04, 25
	0x00, 11
	0x08, 34
	0x00, 11
	0x08, 30
	0x38, 1
	0x28, 9
	0x08, 202
	0x38, 1
	0x28, 9
	0x08, 35
	0x38, 1
	0x28, 8
	0x08, 45
	0x38, 1
	0x280, 14
	0x08, 17
	0x00, 7
	0x08, 52
	0x38, 1
	0x28, 17
	0x08, 33
	0x38, 1
	0x28, 33
	0x08, 2
	0x00, 29
	0x08, 14
	0x38, 1
	0x28, 6
	0x08, 5
	0x00, 13
	0x04, 9
	0x00, 11
	0x30, 1
	0x20, 6
	0x08, 6
	0x38, 1
	0x28, 6
	0x08, 16
	0x38, 1
	0x28, 5
	0x08, 33
	0x00, 8
	0x04, 11
	0x00, 21
	0x08, 9
	0x00, 31
	0x30, 1
	0x20, 6
	0x00, 2
	0x08, 28
	0x00, 28
	0x04, 5
	0x00, 5
	0x08, 25
	0x38, 1
	0x28, 2
	0x08, 28
	0x00, 6
	0x04, 9
	0x00, 6
	0x08, 31
	0x00, 45
	0x38, 1
	0x28, 5
	0x00, 2
	0x08, 9
	0x00, 7
	0x08, 10
	0x00, 30
	0x08, 12
	0x00, 37
	0x04, 17
	0x00, 22
	0x08, 20
	0x38, 1
	0x28, 11
	0x08, 52
	0x38, 1
	0x28, 12
end table

private table GHZSetup_replay_attract_K
	0x00500000, 0x3B00000
	0x00, 11
	0x08, 92
	0x38, 1
	0x28, 12
	0x08, 82
	0x02, 12
	0x00, 24
	0x30, 1
	0x20, 26
	0x00, 3
	0x08, 2
	0x38, 1
	0x28, 88
	0x08, 2
	0x00, 28
	0x02, 16
	0x32, 1
	0x22, 7
	0x02, 5
	0x32, 1
	0x22, 2
	0x02, 5
	0x32, 1
	0x22, 3
	0x02, 3
	0x32, 1
	0x22, 2
	0x02, 4
	0x08, 105
	0x00, 109
	0x08, 12
	0x38, 1
	0x28, 44
	0x08, 51
	0x38, 1
	0x28, 33
	0x08, 17
	0x38, 1
	0x28, 40
	0x08, 31
	0x38, 1
	0x28, 39
	0x08, 24
	0x38, 1
	0x28, 64
	0x08, 17
	0x38, 1
	0x28, 15
	0x08, 241
	0x00, 29
	0x30, 1
	0x20, 18
	0x00, 4
	0x30, 1
	0x20, 30
	0x21, 3
	0x01, 171
	0x00, 15
	0x02, 12
	0x32, 1
	0x22, 7
	0x02, 4
	0x32, 1
	0x22, 2
	0x02, 6
	0x32, 1
	0x22, 2
	0x02, 4
	0x32, 1
	0x22, 5
	0x02, 22
	0x00, 2
	0x08, 47
	0x00, 6
end table

private table GHZSetup_replay_credits_STK_1
	0x00500000, 0x3B00000
	0x08, 91
	0x38, 1
	0x28, 9
	0x08, 83
	0x38, 1
	0x28, 5
	0x08, 52
	0x38, 1
	0x28, 8
	0x08, 160
	0x00, 8
	0x08, 26
	0x38, 1
	0x28, 3
	0x20, 2
	0x00, 6
	0x04, 5
	0x00, 13
	0x08, 44
	0x38, 1
	0x28, 5
	0x08, 17
	0x00, 16
	0x08, 13
	0x08, 5
	0x08, 42
	0x00, 52
	0x08, 30
end table

private table GHZSetup_replay_credits_STK_2
	0x13E80000, 0x2AC0000
	0x00, 17
	0x08, 255
	0x08, 31
	0x00, 219
end table


// ========================
// Function Definitions
// ========================

private function GHZSetup_SpeedUpMusic
	CheckEqual(object[SLOT_MUSICEVENT_CHANGE].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[SLOT_MUSICEVENT_CHANGE].propertyValue, MUSICEVENT_TRANSITION)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, MUSICEVENT_FLAG_NOCHANGE)
	temp0 &= checkResult
	if temp0 == false
		switch music.currentTrack
		case TRACK_STAGE
			SetMusicTrack("Invincibility_F.ogg", TRACK_INVINCIBLE, MUSIC_LOOP_INV_F)
			SwapMusicTrack("GreenHill_F.ogg", TRACK_STAGE, MUSIC_LOOP_GHZ_F, 7920)
			break

		case TRACK_INVINCIBLE
			SetMusicTrack("GreenHill_F.ogg", TRACK_STAGE, MUSIC_LOOP_GHZ_F)
			SwapMusicTrack("Invincibility_F.ogg", TRACK_INVINCIBLE, MUSIC_LOOP_INV_F, 8000)
			break

		case TRACK_BOSS
			SetMusicTrack("GreenHill_F.ogg", TRACK_STAGE, MUSIC_LOOP_GHZ_F)
			SetMusicTrack("Invincibility_F.ogg", TRACK_INVINCIBLE, MUSIC_LOOP_INV_F)
			break
			
		end switch
	else
		stage.musicFlag = MUSICEVENT_FLAG_SPEEDUP
	end if
end function


private function GHZSetup_SlowDownMusic
	CheckEqual(object[SLOT_MUSICEVENT_CHANGE].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[SLOT_MUSICEVENT_CHANGE].propertyValue, MUSICEVENT_TRANSITION)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, MUSICEVENT_FLAG_NOCHANGE)
	temp0 &= checkResult
	if temp0 == false
		switch music.currentTrack
		case TRACK_STAGE
			SetMusicTrack("Invincibility.ogg", TRACK_INVINCIBLE, MUSIC_LOOP_INV)
			SwapMusicTrack("GreenHill.ogg", TRACK_STAGE, MUSIC_LOOP_GHZ, 12650)
			break

		case TRACK_INVINCIBLE
			SetMusicTrack("GreenHill.ogg", TRACK_STAGE, MUSIC_LOOP_GHZ)
			SwapMusicTrack("Invincibility.ogg", TRACK_INVINCIBLE, MUSIC_LOOP_INV, 12650)
			break

		case TRACK_BOSS
			SetMusicTrack("GreenHill.ogg", TRACK_STAGE, MUSIC_LOOP_GHZ)
			SetMusicTrack("Invincibility.ogg", TRACK_INVINCIBLE, MUSIC_LOOP_INV)
			break
			
		end switch
	else
		stage.musicFlag = MUSICEVENT_FLAG_SLOWDOWN
	end if
end function


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

event ObjectUpdate
	if GHZSetup_playWaterfallSfx != false
		GHZSetup_playWaterfallSfx = false
		object.waterfallLoopTimer--
		if object.waterfallLoopTimer <= 0
			object.waterfallLoopTimer = 31
			if object.playingWaterfallLoop == false
				if credits.screen == 0
					PlaySfx(SfxName[Waterfall], false)
					StopSfx(SfxName[Waterfall Loop])
				end if
				object.playingWaterfallLoop = true
			else
				if credits.screen == 0
					StopSfx(SfxName[Waterfall])
					PlaySfx(SfxName[Waterfall Loop], false)
				end if
			end if
		end if

		SetSfxAttributes(SfxName[Waterfall], -1, GHZSetup_waterfallPan)
		SetSfxAttributes(SfxName[Waterfall Loop], -1, GHZSetup_waterfallPan)
	else
		object.playingWaterfallLoop = false
		object.waterfallLoopTimer   = 0
	end if
	
	// This object's state never changes...
	if object.state < 5
		object.paletteTimer++
		if object.paletteTimer == 6
			object.paletteTimer = 0
			RotatePalette(0, 168, 171, true)
			
			GHZSetup_spikeLogsCounter++
			GHZSetup_spikeLogsCounter &= 15
		end if
	end if

	if object.sunFlowerTimer == 0
		object.sunFlowerTimer = 20
		temp0 = 768
		temp0 += object.sunFlowerFrame
		Copy16x16Tile(427, temp0)
		temp0++
		Copy16x16Tile(428, temp0)
		temp0++
		Copy16x16Tile(429, temp0)
		temp0++
		Copy16x16Tile(430, temp0)
		object.sunFlowerFrame += 4
		object.sunFlowerFrame &= 7
	else
		object.sunFlowerTimer--
	end if

	if object.extendFlowerTimer == 0
		switch object.extendFlowerFrame
		case 0
			object.extendFlowerTimer = 160
			temp0 = 0
			break
			
		case 1
			object.extendFlowerTimer = 10
			temp0 = 6
			break
			
		case 2
			object.extendFlowerTimer = 160
			temp0 = 12
			break
			
		case 3
			object.extendFlowerTimer = 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.extendFlowerFrame++
		object.extendFlowerFrame &= 3
	else
		object.extendFlowerTimer--
	end if
end event


event ObjectStartup
	SetMusicTrack("GreenHill.ogg", TRACK_STAGE, MUSIC_LOOP_GHZ)
	SpeedUpMusic = GHZSetup_SpeedUpMusic
	SlowDownMusic = GHZSetup_SlowDownMusic

	animalType1 = TypeName[Pocky]
	animalType2 = TypeName[Flicky]

	// Scroll values stored in scenes are unsigned and can't be negative, so we have to reverse the value manually ourselves in script instead
	Abs(tileLayer[1].parallaxFactor)
	FlipSign(tileLayer[1].parallaxFactor)
	tileLayer[1].scrollPos = 0x180000

	object[SLOT_ZONESETUP].type = TypeName[GHZ Setup]
	object[SLOT_ZONESETUP].priority = PRIORITY_ACTIVE

	GHZSetup_spikeLogsCounter = 0

	if options.attractMode == true
		switch stage.playerListPos
		case PLAYER_SONIC_ALONE
			switch credits.screen
			case 0
				Player_attractTable = GHZSetup_replay_attract_S
				Player_attractTableSize = 102
				Player_attractDuration = 1800
				break
				
			case 1
				Player_attractTable = GHZSetup_replay_credits_STK_1
				Player_attractTableSize = 58
				Player_attractDuration = 540
				break
				
			case 8
				Player_attractTable = GHZSetup_replay_credits_STK_2
				Player_attractTableSize = 10
				Player_attractDuration = 540
				break
				
			end switch
			break
			
		case PLAYER_TAILS_ALONE
			switch credits.screen
			case 0
				Player_attractTable 	= GHZSetup_replay_attract_T
				Player_attractTableSize 	= 198
				Player_attractDuration 	= 1800
				break
				
			case 1
				Player_attractTable 	= GHZSetup_replay_credits_STK_1
				Player_attractTableSize 	= 58
				Player_attractDuration 	= 540
				break
				
			case 8
				Player_attractTable 	= GHZSetup_replay_credits_STK_2
				Player_attractTableSize 	= 10
				Player_attractDuration 	= 540
				break
				
			end switch
			break
			
		case PLAYER_KNUX_ALONE
			switch credits.screen
			case 0
				Player_attractTable 	= GHZSetup_replay_attract_K
				Player_attractTableSize 	= 150
				Player_attractDuration 	= 1800
				break
				
			case 1
				Player_attractTable 	= GHZSetup_replay_credits_STK_1
				Player_attractTableSize 	= 58
				Player_attractDuration 	= 540
				break
				
			case 8
				Player_attractTable 	= GHZSetup_replay_credits_STK_2
				Player_attractTableSize 	= 10
				Player_attractDuration 	= 540
				break
				
			end switch
			break
			
		end switch
		
		CallFunction(Player_SetupAttractDemo)
	end if
end event


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

event RSDKDraw
	DrawSprite(0)
end event


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