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

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

private alias object.value0 : object.paletteTimer
private alias object.value1 : object.paletteIndex
private alias object.value2 : object.scrollPos

// Tracks
private alias 0 : TRACK_STAGE
private alias 2 : TRACK_INVINCIBLE
private alias 2 : 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 84364 : MUSIC_LOOP_SLZ
private alias 67640 : MUSIC_LOOP_SLZ_F

private alias 39528 : MUSIC_LOOP_INV
private alias 30436 : MUSIC_LOOP_INV_F


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

reserve function SLZSetup_SpeedUpMusic
reserve function SLZSetup_SlowDownMusic


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

public value SLZSetup_rotateTimer = 0


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

private table SLZSetup_palTable1
	0x00E0E0, 0x00A0A0, 0x006060, 0x002020, 0x006060, 0x00A0A0
end table


private table SLZSetup_palTable2
	0x600000, 0xA00000, 0xE00000, 0xA00000, 0x600000, 0x200000
end table


private table SLZSetup_palTable3
	0x606000, 0x202000, 0x606000, 0xA0A000, 0xE0E000, 0xA0A000
end table


private table SLZSetup_replay_credits_STK
	0xBB00000, 0x4C0000
	0x00, 37
	0x08, 36
	0x00, 6
	0x08, 204
	0x02, 39
	0x00, 159
	0x04, 37
	0x34, 1
	0x24, 7
	0x04, 47
	0x34, 1
	0x24, 33
end table


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

private function SLZSetup_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 == 0
		switch music.currentTrack
		case TRACK_STAGE
			SetMusicTrack("Invincibility_F.ogg", TRACK_INVINCIBLE, MUSIC_LOOP_INV_F)
			SwapMusicTrack("Starlight_F.ogg", TRACK_STAGE, MUSIC_LOOP_SLZ_F, 8000)
			break

		case TRACK_INVINCIBLE
			SetMusicTrack("Starlight_F.ogg", TRACK_STAGE, MUSIC_LOOP_SLZ_F)
			SwapMusicTrack("Invincibility_F.ogg", TRACK_INVINCIBLE, MUSIC_LOOP_INV_F, 8000)
			break

		case TRACK_BOSS
			SetMusicTrack("Starlight_F.ogg", TRACK_STAGE, MUSIC_LOOP_SLZ_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 SLZSetup_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("Starlight.ogg", TRACK_STAGE, MUSIC_LOOP_SLZ, 12500)
			break

		case TRACK_INVINCIBLE
			SetMusicTrack("Starlight.ogg", TRACK_STAGE, MUSIC_LOOP_SLZ)
			SwapMusicTrack("Invincibility.ogg", TRACK_INVINCIBLE, MUSIC_LOOP_INV, 12500)
			break

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


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

event ObjectUpdate
	GetTableValue(temp0, 21, StageSetup_oscillationTable)
	if temp0 == 0
		SLZSetup_rotateTimer++
		SLZSetup_rotateTimer &= 3
	end if

	object.paletteTimer++
	if object.paletteTimer == 8
		object.paletteTimer = 0
		object.paletteIndex++
		object.paletteIndex %= 6

		GetTableValue(temp0, object.paletteIndex, SLZSetup_palTable1)
		SetPaletteEntry(0, 171, temp0)
		GetTableValue(temp0, object.paletteIndex, SLZSetup_palTable2)
		SetPaletteEntry(0, 173, temp0)
		GetTableValue(temp0, object.paletteIndex, SLZSetup_palTable3)
		SetPaletteEntry(0, 174, temp0)
	end if
end event


event ObjectDraw
	// Draw FG Construction Sprites
	temp0 = screen.xoffset
	temp0 <<= 1
	temp0 &= 511
	FlipSign(temp0)

	temp0 += object.scrollPos
	temp1 = screen.yoffset
	temp1 <<= 1
	temp1 &= 255
	FlipSign(temp1)
	DrawSpriteScreenXY(0, temp0, temp1)
	DrawSpriteScreenXY(1, temp0, temp1)
end event


event ObjectStartup
	LoadSpriteSheet("SLZ/Objects.gif")
	
	SetMusicTrack("Starlight.ogg", TRACK_STAGE, MUSIC_LOOP_SLZ)
	SpeedUpMusic = SLZSetup_SpeedUpMusic
	SlowDownMusic = SLZSetup_SlowDownMusic
	
	// Sprite Frames
	// (Foreground pole)
	SpriteFrame(-16, 0, 32, 256, 224, 0)
	SpriteFrame(-16, 256, 32, 256, 224, 0)

	// Flicky and Picky are the ones living in these stars
	animalType1 = TypeName[Flicky]
	animalType2 = TypeName[Picky]

	object[SLOT_ZONESETUP].type = TypeName[SLZ Setup]
	object[SLOT_ZONESETUP].priority = PRIORITY_ACTIVE
	object[SLOT_ZONESETUP].drawOrder = 5
	object[SLOT_ZONESETUP].scrollPos = 496
	
	temp0 = screen.xcenter
	temp0 >>= 2
	object[SLOT_ZONESETUP].scrollPos -= temp0
	SLZSetup_rotateTimer = 0

	if options.attractMode == true
		Player_attractTable = SLZSetup_replay_credits_STK
		Player_attractTableSize = 26
		Player_attractDuration = 540
		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
