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

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

private alias object.value1 : object.paletteTimer
private alias object.value2 : object.paletteFrame

// Tracks
private alias 0 : TRACK_STAGE
private alias 1 : TRACK_ACTFINISH
private alias 2 : TRACK_INVINCIBLE
private alias 3 : TRACK_CONTINUE
private alias 4 : TRACK_BOSS
private alias 5 : TRACK_GAMEOVER
private alias 6 : TRACK_DROWNING
private alias 7 : TRACK_SUPER

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

// 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 99356 : MUSIC_LOOP_MCZ_1P
private alias 79574 : MUSIC_LOOP_MCZ_1P_F

private alias 18672 : MUSIC_LOOP_MCZ_2P // This is 18671 in the Sound Test and HPZ's initial music setting...
private alias 15272 : MUSIC_LOOP_MCZ_2P_F

private alias 38679 : MUSIC_LOOP_INV
private alias 30897 : MUSIC_LOOP_INV_F


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

reserve function MCZSetup_SpeedUpMusic1P
reserve function MCZSetup_SlowDownMusic1P
reserve function MCZSetup_SpeedUpMusic2P
reserve function MCZSetup_SlowDownMusic2P

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

public table MCZSetup_paletteCycle
	0xC00000, 0xE06000, 0xE0C000, 0xE0E080
end table


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

private function MCZSetup_SpeedUpMusic1P
	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("MysticCave_F.ogg", TRACK_STAGE, MUSIC_LOOP_MCZ_1P_F, 8000)
			break

		case TRACK_INVINCIBLE
			SetMusicTrack("MysticCave_F.ogg", TRACK_STAGE, MUSIC_LOOP_MCZ_1P_F)
			SwapMusicTrack("Invincibility_F.ogg", TRACK_INVINCIBLE, MUSIC_LOOP_INV_F, 8000)
			break

		case TRACK_BOSS
		case TRACK_DROWNING
		case TRACK_SUPER
			SetMusicTrack("MysticCave_F.ogg", TRACK_STAGE, MUSIC_LOOP_MCZ_1P_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 MCZSetup_SlowDownMusic1P
	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("MysticCave.ogg", TRACK_STAGE, MUSIC_LOOP_MCZ_1P, 12500)
			break

		case TRACK_INVINCIBLE
			SetMusicTrack("MysticCave.ogg", TRACK_STAGE, MUSIC_LOOP_MCZ_1P)
			SwapMusicTrack("Invincibility.ogg", TRACK_INVINCIBLE, MUSIC_LOOP_INV, 12500)
			break

		case TRACK_BOSS
		case TRACK_DROWNING
		case TRACK_SUPER
			SetMusicTrack("MysticCave.ogg", TRACK_STAGE, MUSIC_LOOP_MCZ_1P)
			SetMusicTrack("Invincibility.ogg", TRACK_INVINCIBLE, MUSIC_LOOP_INV)
			break
			
		end switch
	else
		stage.musicFlag = MUSICEVENT_FLAG_SLOWDOWN
	end if
end function


private function MCZSetup_SpeedUpMusic2P
	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("MysticCave2_F.ogg", TRACK_STAGE, MUSIC_LOOP_MCZ_2P_F, 8000)
			break

		case TRACK_INVINCIBLE
			SetMusicTrack("MysticCave2_F.ogg", TRACK_STAGE, MUSIC_LOOP_MCZ_2P_F)
			SwapMusicTrack("Invincibility_F.ogg", TRACK_INVINCIBLE, MUSIC_LOOP_INV_F, 8000)
			break

		case TRACK_BOSS
			SetMusicTrack("MysticCave2_F.ogg", TRACK_STAGE, MUSIC_LOOP_MCZ_2P_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 MCZSetup_SlowDownMusic2P
	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("MysticCave2.ogg", TRACK_STAGE, MUSIC_LOOP_MCZ_2P, 12500)
			break

		case TRACK_INVINCIBLE
			SetMusicTrack("MysticCave2.ogg", TRACK_STAGE, MUSIC_LOOP_MCZ_2P)
			SwapMusicTrack("Invincibility.ogg", TRACK_INVINCIBLE, MUSIC_LOOP_INV, 12500)
			break

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


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

event ObjectUpdate
	// Update the flashing lantern palette cycle
	object.paletteTimer++
	if object.paletteTimer == 2
		object.paletteTimer = 0
		object.paletteFrame++
		object.paletteFrame &= 3

		GetTableValue(temp0, object.paletteFrame, MCZSetup_paletteCycle)
		SetPaletteEntry(0, 155, temp0)
	end if
end event


event ObjectStartup
	if options.vsMode == false
		SetMusicTrack("MysticCave.ogg", TRACK_STAGE, MUSIC_LOOP_MCZ_1P)
		SpeedUpMusic = MCZSetup_SpeedUpMusic1P
		SlowDownMusic = MCZSetup_SlowDownMusic1P
	else
		SetMusicTrack("MysticCave2.ogg", TRACK_STAGE, MUSIC_LOOP_MCZ_2P)
		SpeedUpMusic = MCZSetup_SpeedUpMusic2P
		SlowDownMusic = MCZSetup_SlowDownMusic2P
	end if

	// Reset the colour used in the boss flash
	SetPaletteEntry(0, 192, 0x000000)

	// Cucky and Mocky live in this endless mine
	animalType1 = TypeName[Cucky]
	animalType2 = TypeName[Mocky]

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

	// The different acts get different parallax values
	// Do note that the boss act (act "B") doesn't have anything here and instead uses the default values already in MCZ's Backgrounds.bin
	switch stage.actNum
	case 1
		stage.curYBoundary1 		= 960
		tileLayer[1].scrollPos 		= -0x1400000
		tileLayer[1].parallaxFactor = 85
		break

	case 2
		stage.curYBoundary1 		= 96
		tileLayer[1].scrollPos 		= -0x100000
		tileLayer[1].parallaxFactor = 42
		// Some small bounds adjustments are needed for the HPZ entrance to trigger
		stage.curYBoundary2 = 2048
		stage.deathBoundary = stage.curYBoundary2
		stage.deathBoundary <<= 16
		break
		
	end switch
end event


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
