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

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

private alias object.value1 : object.lavaTileFrame
private alias object.value2 : object.lavaAniTileID
private alias object.value3 : object.lavaAnimTimer
private alias object.value4 : object.candleAnimTimer

// Player Aliases
private alias object.xpos : player.xpos
private alias object.ypos : player.ypos
private alias object.value37 : player.shield

// Tile Info ID Aliases
private alias 8 : TILEINFO_ANGLEB

// Tile Flag Aliases
private alias 1 : TILE_LAVA

// Shields
private alias 3 : SHIELD_FIRE

// 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

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 99380 : MUSIC_LOOP_MZ
private alias 79537 : MUSIC_LOOP_MZ_F

private alias 39528 : MUSIC_LOOP_INV
private alias 30436 : MUSIC_LOOP_INV_F


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

reserve function MZSetup_SpeedUpMusic
reserve function MZSetup_SlowDownMusic


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

private table MZSetup_replay_attract_S
	0x0300000, 0x2640000
	0x00, 8
	0x08, 137
	0x38, 1
	0x28, 4
	0x08, 14
	0x00, 18
	0x08, 18
	0x38, 1
	0x28, 7
	0x08, 2
	0x00, 32
	0x02, 13
	0x00, 3
	0x30, 1
	0x20, 8
	0x00, 3
	0x08, 49
	0x38, 1
	0x28, 7
	0x08, 37
	0x38, 1
	0x28, 8
	0x08, 2
	0x00, 18
	0x04, 16
	0x00, 5
	0x30, 1
	0x20, 11
	0x08, 11
	0x00, 7
	0x04, 16
	0x00, 4
	0x08, 6
	0x00, 22
	0x08, 5
	0x38, 1
	0x28, 12
	0x08, 16
	0x00, 17
	0x08, 31
	0x00, 13
	0x38, 1
	0x28, 9
	0x00, 36
	0x08, 2
	0x38, 1
	0x28, 9
	0x08, 36
	0x00, 66
	0x30, 1
	0x20, 8
	0x04, 37
	0x00, 6
	0x04, 17
	0x34, 1
	0x24, 6
	0x04, 68
	0x00, 28
	0x02, 12
	0x32, 1
	0x22, 8
	0x02, 50
	0x00, 32
	0x08, 83
	0x38, 1
	0x28, 8
	0x08, 9
	0x00, 21
	0x38, 1
	0x28, 7
	0x08, 5
	0x00, 98
	0x08, 69
	0x00, 25
	0x08, 21
	0x38, 1
	0x28, 6
	0x08, 4
	0x00, 15
	0x04, 16
	0x00, 4
	0x08, 18
	0x38, 1
	0x28, 7
	0x08, 5
	0x00, 27
	0x08, 23
	0x00, 9
	0x30, 1
	0x20, 6
	0x00, 19
	0x08, 19
	0x00, 20
	0x38, 4
	0x28, 9
	0x08, 4
end table

private table MZSetup_replay_attract_T
	0x0300000, 0x2680000
	0x00, 8
	0x08, 41
	0x38, 1
	0x28, 3
	0x08, 80
	0x38, 1
	0x28, 6
	0x08, 7
	0x00, 23
	0x08, 21
	0x38, 1
	0x28, 5
	0x08, 6
	0x00, 15
	0x08, 8
	0x00, 17
	0x38, 1
	0x28, 5
	0x08, 46
	0x38, 1
	0x28, 12
	0x08, 30
	0x00, 9
	0x30, 1
	0x20, 3
	0x28, 3
	0x00, 25
	0x04, 4
	0x00, 7
	0x08, 3
	0x38, 1
	0x28, 7
	0x00, 14
	0x04, 18
	0x00, 5
	0x08, 5
	0x38, 1
	0x28, 9
	0x08, 7
	0x00, 24
	0x08, 17
	0x00, 12
	0x08, 19
	0x00, 3
	0x30, 1
	0x20, 7
	0x28, 4
	0x08, 12
	0x00, 20
	0x08, 17
	0x38, 1
	0x28, 10
	0x08, 98
	0x00, 7
	0x04, 80
	0x34, 1
	0x24, 20
	0x04, 2
	0x00, 14
	0x08, 21
	0x38, 1
	0x28, 10
	0x08, 15
	0x38, 1
	0x28, 7
	0x08, 59
	0x00, 23
	0x08, 6
	0x38, 1
	0x28, 6
	0x08, 56
	0x00, 27
	0x08, 24
	0x00, 17
	0x08, 73
	0x38, 1
	0x28, 8
	0x08, 23
	0x00, 5
	0x04, 25
	0x00, 24
	0x04, 36
	0x00, 77
	0x04, 35
	0x34, 1
	0x24, 7
	0x04, 7
	0x00, 13
	0x08, 19
	0x00, 53
	0x04, 10
	0x34, 1
	0x24, 4
	0x04, 16
	0x00, 14
	0x04, 8
	0x00, 101
	0x04, 4
	0x34, 1
	0x24, 2
	0x04, 32
	0x00, 14
end table

private table MZSetup_replay_attract_K
	0x0300000, 0x2640000
	0x00, 22
	0x08, 137
	0x38, 1
	0x28, 7
	0x08, 6
	0x00, 40
	0x08, 9
	0x38, 1
	0x28, 6
	0x08, 18
	0x00, 3
	0x04, 16
	0x08, 5
	0x38, 1
	0x28, 8
	0x08, 22
	0x38, 1
	0x28, 27
	0x08, 32
	0x38, 1
	0x28, 15
	0x08, 9
	0x00, 27
	0x30, 1
	0x20, 10
	0x28, 2
	0x08, 18
	0x00, 21
	0x08, 2
	0x38, 1
	0x28, 14
	0x20, 3
	0x00, 8
	0x04, 16
	0x00, 3
	0x08, 40
	0x38, 1
	0x28, 24
	0x08, 4
	0x00, 18
	0x08, 11
	0x38, 1
	0x28, 13
	0x08, 86
	0x38, 1
	0x28, 5
	0x08, 6
	0x38, 1
	0x28, 201
	0x08, 31
	0x38, 1
	0x28, 6
	0x08, 11
	0x00, 17
	0x08, 29
	0x00, 47
	0x30, 1
	0x20, 14
	0x00, 24
	0x04, 15
	0x00, 16
	0x08, 9
	0x00, 47
	0x30, 1
	0x20, 13
	0x28, 2
	0x08, 8
	0x38, 1
	0x28, 20
	0x09, 3
	0x01, 83
	0x09, 2
	0x08, 91
	0x00, 29
	0x30, 1
	0x20, 13
	0x00, 7
	0x30, 1
	0x20, 15
	0x21, 1
end table

private table MZSetup_replay_credits_ST
	0x0EA00000, 0x46C0000
	0x00, 23
	0x04, 40
	0x00, 5
	0x08, 31
	0x38, 1
	0x28, 30
	0x08, 24
	0x38, 1
	0x28, 3
	0x2C, 2
	0x24, 8
	0x04, 13
	0x00, 15
	0x04, 16
	0x00, 5
	0x04, 22
	0x34, 1
	0x24, 3
	0x04, 4
	0x00, 16
	0x04, 121
	0x00, 42
	0x08, 8
	0x00, 19
	0x00, 8
	0x04, 1
	0x00, 64
end table

private table MZSetup_replay_credits_K
	0x0EA00000, 0x46C0000
	0x00, 25
	0x04, 29
	0x34, 1
	0x24, 15
	0x04, 5
	0x00, 18
	0x08, 17
	0x38, 1
	0x28, 10
	0x08, 4
	0x38, 1
	0x28, 43
	0x08, 4
	0x00, 17
	0x04, 4
	0x34, 1
	0x24, 5
	0x04, 3
	0x00, 33
	0x04, 109
	0x00, 34
	0x08, 9
	0x00, 15
	0x04, 5
	0x00, 255
	0x00, 40
	0x30, 1
	0x20, 8
	0x00, 7
	0x04, 12
end table


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

private function MZSetup_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("Marble_F.ogg", TRACK_STAGE, MUSIC_LOOP_MZ_F, 8000)
			break

		case TRACK_INVINCIBLE
			SetMusicTrack("Marble_F.ogg", TRACK_STAGE, MUSIC_LOOP_MZ_F)
			SwapMusicTrack("Invincibility_F.ogg", TRACK_INVINCIBLE, MUSIC_LOOP_INV_F, 8000)
			break

		case TRACK_BOSS
			SetMusicTrack("Marble_F.ogg", TRACK_STAGE, MUSIC_LOOP_MZ_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 MZSetup_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("Marble.ogg", TRACK_STAGE, MUSIC_LOOP_MZ, 12500)
			break
			
		case TRACK_INVINCIBLE
			SetMusicTrack("Marble.ogg", TRACK_STAGE, MUSIC_LOOP_MZ)
			SwapMusicTrack("Invincibility.ogg", TRACK_INVINCIBLE, MUSIC_LOOP_INV, 12500)
			break

		case TRACK_BOSS
			SetMusicTrack("Marble.ogg", TRACK_STAGE, MUSIC_LOOP_MZ)
			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.frame = object.animationTimer
	object.frame /= 20
	object.animationTimer++
	object.animationTimer %= 60

	object.lavaAniTileID = object.frame
	object.lavaAniTileID <<= 1
	object.lavaAniTileID += 440
	Copy16x16Tile(366, object.lavaAniTileID)

	object.lavaAniTileID++
	Copy16x16Tile(367, object.lavaAniTileID)

	switch object.frame
	case 0
		object.lavaAniTileID = 460
		break
		
	case 1
		object.lavaAniTileID = 528
		break
		
	case 2
		object.lavaAniTileID = 596
		break
		
	end switch

	object.lavaAnimTimer++
	object.lavaAnimTimer &= 0x1FF
	Sin(object.lavaTileFrame, object.lavaAnimTimer)
	object.lavaTileFrame += 0x200
	object.lavaTileFrame >>= 2
	object.lavaTileFrame &= 0x1F
	if object.lavaTileFrame > 0xF
		object.lavaTileFrame &= 15
		object.lavaTileFrame <<= 2

		object.lavaAniTileID += object.lavaTileFrame
		Copy16x16Tile(363, object.lavaAniTileID)

		object.lavaAniTileID++
		Copy16x16Tile(362, object.lavaAniTileID)

		object.lavaAniTileID++
		Copy16x16Tile(365, object.lavaAniTileID)

		object.lavaAniTileID++
		Copy16x16Tile(364, object.lavaAniTileID)
	else
		object.lavaTileFrame <<= 2

		object.lavaAniTileID += object.lavaTileFrame
		Copy16x16Tile(362, object.lavaAniTileID)

		object.lavaAniTileID++
		Copy16x16Tile(363, object.lavaAniTileID)

		object.lavaAniTileID++
		Copy16x16Tile(364, object.lavaAniTileID)

		object.lavaAniTileID++
		Copy16x16Tile(365, object.lavaAniTileID)
	end if

	// BG Candles
	temp0 = object.candleAnimTimer
	temp0 >>= 4
	temp0 <<= 1
	temp0 += 446
	Copy16x16Tile(360, temp0)
	temp0++
	Copy16x16Tile(361, temp0)
	temp0++
	object.candleAnimTimer++
	object.candleAnimTimer &= 0x3F

	// Check interaction with lava tiles
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		temp1 = player[currentPlayer].xpos
		temp1 >>= 16
		
		temp2 = player[currentPlayer].ypos
		temp2 >>= 16
		temp2 += 4
		
		Get16x16TileInfo(temp0, temp1, temp2, TILEINFO_ANGLEB)
		if temp0 == TILE_LAVA
			// Instead of calling Player_FireHit, this script checks against the Player's shield and hurts them via the normal function if the player isn't protected
			// Don't know why, but my best guess is that this script predated Player_FireHit's creation or something
			if player[currentPlayer].shield != SHIELD_FIRE
				CallFunction(Player_Hit)
			end if
		end if
	next
end event


event ObjectDraw
	// Special handling for keeping the interior BG aligned
	if screen.yoffset > 458
		// Camera is underground, inside the interior
		temp0 = screen.yoffset
		temp0 -= 458
		temp0 *= 0xC000
		tileLayer[1].scrollPos = temp0
	else
		// Camera is above ground
		tileLayer[1].scrollPos = 0
	end if
end event


event ObjectStartup
	// Set the music track for this Stage
	SetMusicTrack("Marble.ogg", TRACK_STAGE, MUSIC_LOOP_MZ)
	SpeedUpMusic = MZSetup_SpeedUpMusic
	SlowDownMusic = MZSetup_SlowDownMusic

	// Ricky and Rocky are the inhabitants along this flamed quest
	animalType1 = TypeName[Ricky]
	animalType2 = TypeName[Rocky]

	// Set object 10 to an MZ Setup object
	object[SLOT_ZONESETUP].type = TypeName[MZ Setup]
	object[SLOT_ZONESETUP].priority = PRIORITY_ACTIVE
	object[SLOT_ZONESETUP].drawOrder = 0

	// Different parallax between the different acts
	// (Act 1 parallax info is built into the normal MZ BG file)
	switch stage.actNum
	case 2
		hParallax[0].scrollPos = 0x1000000
		hParallax[1].scrollPos = 0x2000000
		hParallax[2].scrollPos = 0x2000000
		hParallax[3].scrollPos = 0x2000000
		hParallax[4].scrollPos = 0x2000000
		hParallax[5].scrollPos = 0x2000000
		hParallax[6].scrollPos = 0x2000000
		break
	
	case 3
		hParallax[0].scrollPos = 0x4000000
		hParallax[1].scrollPos = 0x1000000
		hParallax[2].scrollPos = 0x1000000
		hParallax[3].scrollPos = 0x1000000
		hParallax[4].scrollPos = 0x1000000
		hParallax[5].scrollPos = 0x1000000
		hParallax[6].scrollPos = 0x1000000
		break
		
	end switch

	if options.attractMode == true
		switch stage.playerListPos
		case PLAYER_SONIC_ALONE
			if credits.screen == 0
				Player_attractTable = MZSetup_replay_attract_S
				Player_attractTableSize = 194
				Player_attractDuration = 1660
			else
				Player_attractTable = MZSetup_replay_credits_ST
				Player_attractTableSize = 56
				Player_attractDuration = 540
			end if
			break
			
		case PLAYER_TAILS_ALONE
			if credits.screen == 0
				Player_attractTable = MZSetup_replay_attract_T
				Player_attractTableSize = 206
				Player_attractDuration = 1800
			else
				Player_attractTable = MZSetup_replay_credits_ST
				Player_attractTableSize = 56
				Player_attractDuration = 540
			end if
			break
			
		case PLAYER_KNUX_ALONE
			if credits.screen == 0
				Player_attractTable = MZSetup_replay_attract_K
				Player_attractTableSize = 162
				Player_attractDuration = 1800
			else
				Player_attractTable = MZSetup_replay_credits_K
				Player_attractTableSize = 62
				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
