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

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

private alias object.value0 : object.palRotateTimer
private alias object.value1 : object.palRotateIndex
private alias object.value2 : object.rLightTimer

// Tracks
private alias 0 : TRACK_STAGE
private alias 2 : TRACK_INVINCIBLE
private alias 3 : 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 101364 : MUSIC_LOOP_SYZ // This is 100712 on the level select...
private alias 81128  : MUSIC_LOOP_SYZ_F

private alias 39528  : MUSIC_LOOP_INV
private alias 30436  : MUSIC_LOOP_INV_F


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

reserve function SYZSetup_SpeedUpMusic
reserve function SYZSetup_SlowDownMusic


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

public value SYZSetup_oscillation = 0
public value SYZSetup_RLightFrame = 0


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

private table SYZSetup_replay_attract_S
	0x300000, 0x3BD0000
	0x00, 100
	0x08, 33
	0x38, 1
	0x28, 19
	0x00, 22
	0x04, 18
	0x30, 1
	0x20, 2
	0x28, 39
	0x00, 10
	0x08, 7
	0x00, 13
	0x08, 11
	0x00, 17
	0x08, 14
	0x00, 20
	0x04, 15
	0x00, 30
	0x38, 1
	0x28, 11
	0x20, 29
	0x28, 8
	0x20, 39
	0x00, 7
	0x04, 27
	0x06, 2
	0x02, 32
	0x00, 14
	0x04, 45
	0x00, 37
	0x08, 8
	0x00, 20
	0x30, 1
	0x24, 7
	0x04, 49
	0x34, 1
	0x24, 18
	0x04, 28
	0x00, 20
	0x08, 9
	0x00, 19
	0x02, 9
	0x32, 1
	0x22, 5
	0x02, 7
	0x32, 1
	0x22, 4
	0x02, 5
	0x32, 1
	0x22, 4
	0x02, 33
	0x00, 73
	0x08, 69
	0x00, 16
	0x04, 45
	0x00, 62
	0x08, 19
	0x00, 4
	0x04, 39
	0x00, 36
	0x04, 9
	0x00, 16
	0x08, 7
	0x00, 7
	0x02, 17
	0x00, 23
	0x08, 18
	0x38, 1
	0x28, 41
	0x08, 16
	0x00, 32
	0x02, 14
	0x00, 2
	0x08, 25
	0x00, 11
	0x04, 9
	0x00, 44
	0x08, 5
	0x00, 14
	0x08, 45
	0x00, 52
	0x04, 38
	0x34, 1
	0x24, 5
	0x00, 13
	0x08, 7
	0x00, 21
	0x08, 8
end table

private table SYZSetup_replay_attract_T
	0x300000, 0x3C10000
	0x00, 76
	0x08, 27
	0x38, 1
	0x28, 24
	0x08, 4
	0x00, 15
	0x04, 16
	0x34, 1
	0x24, 4
	0x20, 5
	0x28, 30
	0x08, 8
	0x00, 9
	0x08, 13
	0x00, 12
	0x08, 14
	0x00, 15
	0x08, 13
	0x00, 15
	0x04, 16
	0x00, 13
	0x08, 11
	0x38, 1
	0x28, 21
	0x20, 13
	0x24, 21
	0x04, 38
	0x02, 18
	0x00, 31
	0x04, 51
	0x00, 9
	0x08, 23
	0x00, 8
	0x34, 1
	0x24, 30
	0x04, 29
	0x34, 1
	0x24, 26
	0x04, 29
	0x00, 9
	0x08, 13
	0x00, 14
	0x02, 13
	0x32, 1
	0x22, 7
	0x02, 9
	0x00, 69
	0x08, 39
	0x38, 1
	0x28, 9
	0x08, 28
	0x00, 15
	0x04, 54
	0x00, 72
	0x04, 7
	0x30, 1
	0x20, 13
	0x00, 6
	0x30, 1
	0x20, 7
	0x00, 4
	0x30, 1
	0x20, 5
	0x00, 6
	0x38, 1
	0x28, 3
	0x08, 9
	0x38, 1
	0x28, 5
	0x08, 4
	0x00, 2
	0x30, 1
	0x20, 5
	0x00, 5
	0x34, 1
	0x24, 6
	0x20, 2
	0x00, 5
	0x30, 1
	0x20, 6
	0x00, 6
	0x30, 1
	0x20, 3
	0x24, 3
	0x04, 5
	0x30, 1
	0x20, 7
	0x00, 3
	0x04, 5
	0x30, 1
	0x20, 7
	0x00, 6
	0x30, 1
	0x20, 5
	0x00, 4
	0x08, 2
	0x38, 1
	0x28, 3
	0x20, 2
	0x00, 6
	0x30, 1
	0x20, 5
	0x00, 7
	0x30, 1
	0x20, 5
	0x00, 2
	0x08, 8
	0x38, 1
	0x28, 5
	0x08, 5
	0x00, 7
	0x04, 39
	0x34, 1
	0x24, 5
	0x28, 3
	0x08, 3
	0x38, 1
	0x28, 8
	0x08, 26
	0x30, 1
	0x20, 4
	0x24, 4
	0x04, 3
	0x00, 12
	0x08, 56
	0x00, 14
	0x04, 37
	0x00, 3
	0x34, 1
	0x24, 12
	0x04, 14
	0x00, 11
	0x08, 15
	0x38, 1
	0x28, 15
	0x08, 6
	0x00, 21
	0x04, 54
	0x00, 21
	0x08, 63
	0x38, 1
	0x28, 2
	0x20, 17
	0x00, 39
end table

private table SYZSetup_replay_attract_K
	0x300000, 0x3BD0000
	0x00, 25
	0x08, 47
	0x38, 1
	0x28, 7
	0x00, 27
	0x04, 9
	0x00, 2
	0x04, 5
	0x34, 1
	0x24, 5
	0x28, 32
	0x08, 6
	0x00, 30
	0x04, 14
	0x00, 4
	0x08, 7
	0x00, 18
	0x02, 12
	0x32, 1
	0x22, 4
	0x00, 21
	0x08, 10
	0x38, 1
	0x28, 23
	0x08, 5
	0x38, 1
	0x28, 28
	0x08, 1
	0x00, 16
	0x04, 89
	0x00, 8
	0x08, 154
	0x38, 1
	0x28, 6
	0x08, 55
	0x00, 11
	0x04, 18
	0x00, 122
	0x08, 41
	0x00, 9
	0x08, 13
	0x00, 11
	0x04, 6
	0x00, 10
	0x04, 11
	0x00, 3
	0x02, 46
	0x00, 14
	0x08, 27
	0x00, 20
	0x04, 44
	0x34, 1
	0x24, 26
	0x04, 46
	0x34, 1
	0x24, 6
	0x00, 5
	0x08, 17
	0x00, 25
	0x04, 47
	0x00, 11
	0x02, 16
	0x00, 19
	0x04, 27
	0x00, 30
	0x02, 57
	0x00, 58
	0x02, 11
	0x00, 27
	0x08, 14
	0x00, 35
	0x30, 1
	0x20, 17
	0x00, 8
	0x08, 5
	0x38, 1
	0x28, 40
	0x08, 6
	0x00, 5
	0x30, 1
	0x20, 6
	0x00, 12
	0x08, 4
	0x00, 42
	0x08, 11
	0x38, 1
	0x28, 9
	0x08, 40
	0x38, 1
	0x28, 4
end table

private table SYZSetup_replay_credits_STK
	0x17500000, 0xBD0000
	0x00, 31
	0x08, 36
	0x00, 12
	0x08, 38
	0x00, 4
	0x02, 221
	0x00, 68
	0x02, 91
	0x00, 40
	0x30, 1
	0x20, 96
	0x00, 3
	0x02, 13
end table


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

private function SYZSetup_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("SpringYard_F.ogg", TRACK_STAGE, MUSIC_LOOP_SYZ_F, 8000)
			break

		case TRACK_INVINCIBLE
			SetMusicTrack("SpringYard_F.ogg", TRACK_STAGE, MUSIC_LOOP_SYZ_F)
			SwapMusicTrack("Invincibility_F.ogg", TRACK_INVINCIBLE, MUSIC_LOOP_INV_F, 8000)
			break

		case TRACK_BOSS
			SetMusicTrack("SpringYard_F.ogg", TRACK_STAGE, MUSIC_LOOP_SYZ_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 SYZSetup_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("SpringYard.ogg", TRACK_STAGE, MUSIC_LOOP_SYZ, 12500)
			break

		case TRACK_INVINCIBLE
			SetMusicTrack("SpringYard.ogg", TRACK_STAGE, MUSIC_LOOP_SYZ)
			SwapMusicTrack("Invincibility.ogg", TRACK_INVINCIBLE, MUSIC_LOOP_INV, 12500)
			break

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


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

event ObjectUpdate
	object.palRotateTimer++
	if object.palRotateTimer == 6
		object.palRotateTimer = 0
		object.palRotateIndex++
		object.palRotateIndex &= 3
		temp0 = object.palRotateIndex
		temp0++
		CopyPalette(temp0, 160, 0, 160, 32)
	end if

	object.rLightTimer++
	object.rLightTimer %= 48
	SYZSetup_RLightFrame = object.rLightTimer
	SYZSetup_RLightFrame >>= 3

	SYZSetup_oscillation++
	SYZSetup_oscillation %= 356
end event


event ObjectStartup
	// Set the music track
	SetMusicTrack("SpringYard.ogg", TRACK_STAGE, MUSIC_LOOP_SYZ)

	// Also set the speed up/slow down functions
	SpeedUpMusic = SYZSetup_SpeedUpMusic
	SlowDownMusic = SYZSetup_SlowDownMusic

	// Load colors used for the palette cycle
	// While the majority of this game uses tables for colour data, SYZ instead goes back to the v3 approach of loading colours from an external file into alternate palette banks
	temp0 = 1  // Palette bank
	temp1 = 0  // Start Index
	temp2 = 32 // End Index
	while temp0 < 5
		LoadPalette("SYZ_PalCycle.act", temp0, 160, temp1, temp2)
		temp0++
		temp1 += 32
		temp2 += 32
	loop

	// Set the animal types
	animalType1 = TypeName[Cucky]
	animalType2 = TypeName[Picky]

	// Place an SYZ Setup Object into the scene
	object[SLOT_ZONESETUP].type = TypeName[SYZ Setup]
	object[SLOT_ZONESETUP].priority = PRIORITY_ACTIVE

	SYZSetup_oscillation = 0
	SYZSetup_RLightFrame = 0

	if options.attractMode == true
		switch stage.playerListPos
		case PLAYER_SONIC_ALONE
			if credits.screen == 0
				Player_attractTable = SYZSetup_replay_attract_S
				Player_attractTableSize = 178
				Player_attractDuration = 0x708
			else
				Player_attractTable = SYZSetup_replay_credits_STK
				Player_attractTableSize = 28
				Player_attractDuration = 540
			end if
			break
			
		case PLAYER_TAILS_ALONE
			if credits.screen == 0
				Player_attractTable = SYZSetup_replay_attract_T
				Player_attractTableSize = 290
				Player_attractDuration = 1800
			else
				Player_attractTable = SYZSetup_replay_credits_STK
				Player_attractTableSize = 28
				Player_attractDuration = 540
			end if
			break
			
		case PLAYER_KNUX_ALONE
			if credits.screen == 0
				Player_attractTable = SYZSetup_replay_attract_K
				Player_attractTableSize = 182
				Player_attractDuration = 1800
			else
				Player_attractTable = SYZSetup_replay_credits_STK
				Player_attractTableSize = 28
				Player_attractDuration = 540
			end if
			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
