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

//-------Aliases-------//
private alias 39 : TYPE_SBZSETUP

// Function declarations
reserve function SBZSetup_Function93
reserve function SBZSetup_Function94
reserve function SBZSetup_Function95
reserve function SBZSetup_Function96

// Static Values
public value SBZSetup_value31 = 0;
public value SBZSetup_value30 = 0;
public value SBZSetup_value29 = 0;

// Tables
public table SBZSetup_table33
	0x404000, 0xA0E0, 0x404000, 0xA0E0, 0x80E000, 0x80E000, 0x404000, 0xE040E0
end table

public table SBZSetup_table35
	0xE0E000, 0xE0E000, 0xE08000, 0xE00000, 0x800000, 0x800000, 0xE00000, 0xE08000
end table

public table SBZSetup_table26
	0xA00000, 0xA00000, 0x800000, 0x600000, 0x400000, 0x400000, 0x600000, 0x800000
end table

public table SBZSetup_table27
	0xE00000, 0xE00000, 0xA00000, 0x800000, 0x600000, 0x600000, 0x800000, 0xA00000
end table

public table SBZSetup_table34
	0x60E0C0, 0x60E0C0, 0x40C0A0, 0x20A080, 0x8060, 0x8060, 0x20A080, 0x40C0A0
end table

public table SBZSetup_table28
	0xA0E0, 0xA0E0, 0x6080E0, 0xA060E0, 0xE040E0, 0xE08080, 0xE0A040, 0xE0C020, 0xE0E000, 0xE0E000, 0xE0C020, 0xE0A040, 0xE08080, 0xE040E0, 0xA060E0, 0x6080E0
end table

public table SBZSetup_table29
	0xA0E0, 0xA0E0, 0x4060A0, 0xA04060, 0xE00000, 0xE00000, 0xA04060, 0x4060A0
end table

//0xCC48
public table SBZSetup_table25
	20, 3, 1, 2, 3, 3, 20, 3, 1, 2, 3, 3, 20, 3, 1, 2, 3, 3, 20, 3, 1, 2, 3, 3
end table

//0xCC61
public table SBZSetup_table30
	0xE0A0E0, 0xE040E0, 0x800080
end table

//0xCC65
public table SBZSetup_table31
	0xE040E0, 0x800080, 0xE0A0E0
end table

//0xCC69
public table SBZSetup_table32
	0x800080, 0xE0A0E0, 0xE040E0
end table

public table SBZSetup_table3
	0x15700000, 0x16C0000, 0, 6, 4, 194, 0, 187, 4, 35, 0, 14, 4, 10, 0, 24, 4, 29, 0, 11, 8, 198
end table

public table SBZSetup_table4
	0x1B00000, 0x72C0000, 0, 63, 8, 18, 0, 113, 8, 46, 56, 1, 40, 7, 8, 96, 2, 47, 0, 43, 8, 34, 56, 1, 40, 16, 32, 8, 40, 55
end table

function SBZSetup_Function93
	if object.value2 < 65
		temp0 = object.value2
		temp0 &= 7
		if temp0 == 0
			temp0 = object.value2
			temp0 &= 63
			temp0 >>= 3
			temp0 <<= 2
			temp0 += 604
			Copy16x16Tile(592, temp0)
			temp0++
			Copy16x16Tile(593, temp0)
			temp0++
			Copy16x16Tile(594, temp0)
		end if
	end if
	object.value2++
	object.value2 %= 192
	if object.value3 < 65
		temp0 = object.value3
		temp0 &= 7
		if temp0 == 0
			temp0 = object.value3
			temp0 &= 63
			temp0 >>= 3
			temp0 <<= 2
			temp0 += 604
			Copy16x16Tile(596, temp0)
			temp0++
			Copy16x16Tile(597, temp0)
			temp0++
			Copy16x16Tile(598, temp0)
		end if
	end if
	object.value3++
	object.value3 %= 144
	object.value4++
	if object.value4 == 2
		object.value4 = 0
		RotatePalette(0, 172, 174, 1)
	end if
	object.value8++
	if object.value8 == 15
		object.value8 = 0
		object.value16++
		object.value16 &= 7
		GetTableValue(temp0, object.value16, SBZSetup_table26)
		SetPaletteEntry(0, 183, temp0)
	end if
	object.value9++
	if object.value9 == 12
		object.value9 = 0
		object.value17++
		object.value17 &= 7
		GetTableValue(temp0, object.value17, SBZSetup_table27)
		SetPaletteEntry(0, 184, temp0)
	end if
	object.value11++
	if object.value11 == 29
		object.value11 = 0
		object.value19++
		object.value19 &= 15
		GetTableValue(temp0, object.value19, SBZSetup_table28)
		SetPaletteEntry(0, 191, temp0)
	end if
end function


function SBZSetup_Function94
	RotatePalette(0, 172, 174, 1)
	object.value12++
	if object.value12 == 10
		object.value12 = 0
		object.value20++
		object.value20 &= 7
		GetTableValue(temp0, object.value20, SBZSetup_table29)
		SetPaletteEntry(0, 184, temp0)
	end if
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		if object[currentPlayer].gravity == GRAVITY_GROUND
			temp2 = object[currentPlayer].ypos
			temp2 >>= 16
			temp2 += object[currentPlayer].collisionBottom
			temp2 += 2
			temp1 = object[currentPlayer].xpos
			temp1 >>= 16
			temp1 += object[currentPlayer].collisionLeft
			temp3 = 0
			Get16x16TileInfo(temp0, temp1, temp2, TILEINFO_ANGLEB)
			if temp0 == 1
				Get16x16TileInfo(temp0, temp1, temp2, TILEINFO_DIRECTION)
				if temp0 == 0
					temp3 += 0x20000
				else
					temp3 -= 0x20000
				end if
			end if
			temp1 = object[currentPlayer].xpos
			temp1 >>= 16
			temp1 += object[currentPlayer].collisionRight
			Get16x16TileInfo(temp0, temp1, temp2, TILEINFO_ANGLEB)
			if temp0 == 1
				Get16x16TileInfo(temp0, temp1, temp2, TILEINFO_DIRECTION)
				if temp0 == 0
					temp3 += 0x20000
					if temp3 > 0x20000
						temp3 = 0x20000
					end if
				else
					temp3 -= 0x20000
					if temp3 < -0x20000
						temp3 = -0x20000
					end if
				end if
			end if
			object[currentPlayer].xpos += temp3
		end if
	next
end function


function SBZSetup_SBZ2SpeedUp
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility_F.ogg", 2, 0x76E4)
			SwapMusicTrack("ScrapBrain2_F.ogg", 0, 1, 0x1EF0)
			break
		case 2
			SetMusicTrack("ScrapBrain2_F.ogg", 0, 1)
			SwapMusicTrack("Invincibility_F.ogg", 2, 0x76E4, 0x1F40)
			break
		default
			SetMusicTrack("ScrapBrain2_F.ogg", 0, 1)
			SetMusicTrack("Invincibility_F.ogg", 2, 0x76E4)
			break
		end switch
	else
		stage.musicFlag = 1
	end if
end function


function SBZSetup_SBZ2SlowDown
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility.ogg", 2, 0x9A68)
			SwapMusicTrack("ScrapBrain2.ogg", 0, 1, 0x30D4)
			break
		case 2
			SetMusicTrack("ScrapBrain2.ogg", 0, 1)
			SwapMusicTrack("Invincibility.ogg", 2, 0x9A68, 0x30D4)
			break
		default
			SetMusicTrack("ScrapBrain2.ogg", 0, 1)
			SetMusicTrack("Invincibility.ogg", 2, 0x9A68)
			break			
		end switch
	else
		stage.musicFlag = 2
	end if
end function


function SBZSetup_Function95
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility_F.ogg", 2, 0x76E4)
			SwapMusicTrack("ScrapBrain_F.ogg", 0, 1, 0x1F40)
			break
		case 2
			SetMusicTrack("ScrapBrain_F.ogg", 0, 1)
			SwapMusicTrack("Invincibility_F.ogg", 2, 0x76E4, 0x1F40)
			break
		default
			SetMusicTrack("ScrapBrain_F.ogg", 0, 1)
			SetMusicTrack("Invincibility_F.ogg", 2, 0x76E4)
			break
		end switch
	else
		stage.musicFlag = 1
	end if
end function


function SBZSetup_Function96
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility.ogg", 2, 0x9A68)
			SwapMusicTrack("ScrapBrain.ogg", 0, 1, 0x30D4)
			break
		case 2
			SetMusicTrack("ScrapBrain.ogg", 0, 1)
			SwapMusicTrack("Invincibility.ogg", 2, 0x9A68, 0x30D4)
			break
		default
			SetMusicTrack("ScrapBrain.ogg", 0, 1)
			SetMusicTrack("Invincibility.ogg", 2, 0x9A68)
			break			
		end switch
	else
		stage.musicFlag = 2
	end if
end function


event ObjectMain
	CallFunction(object.value1)
	SBZSetup_value31++
	if SBZSetup_value30 > 1
		SBZSetup_value30--
	else
		SBZSetup_value29++
		SBZSetup_value29 %= 24
		GetTableValue(SBZSetup_value30, SBZSetup_value29, SBZSetup_table25)
	end if
	object.value5++
	if object.value5 == 4
		object.value5 = 0
		object.value13++
		object.value13 %= 3
		GetTableValue(temp0, object.value13, SBZSetup_table30)
		SetPaletteEntry(0, 188, temp0)
		GetTableValue(temp0, object.value13, SBZSetup_table31)
		SetPaletteEntry(0, 189, temp0)
		GetTableValue(temp0, object.value13, SBZSetup_table32)
		SetPaletteEntry(0, 190, temp0)
	end if
	object.value6++
	if object.value6 == 8
		object.value6 = 0
		object.value14++
		object.value14 &= 7
		GetTableValue(temp0, object.value14, SBZSetup_table33)
		SetPaletteEntry(0, 168, temp0)
		GetTableValue(temp0, object.value14, SBZSetup_table34)
		SetPaletteEntry(0, 185, temp0)
	end if
	object.value7++
	if object.value7 == 14
		object.value7 = 0
		object.value15++
		object.value15 &= 7
		GetTableValue(temp0, object.value15, SBZSetup_table35)
		SetPaletteEntry(0, 169, temp0)
	end if
	object.value35++
	if object.value35 == 6
		object.value35 = 0
		RotatePalette(0, 210, 211, 1)
	end if
end event


event ObjectStartup
	if stage.actNum < 5
		SetMusicTrack("ScrapBrain.ogg", 0, 1)
		SpeedUpMusic = SBZSetup_Function95
		SlowDownMusic = SBZSetup_Function96
	else
		SetMusicTrack("Final.ogg", 0, 0x14AC8)
	end if
	if saved.actTheme == 1
		switch stage.actNum
		default
			SetMusicTrack("ScrapBrain.ogg", 0, 1)
			SpeedUpMusic = SBZSetup_Function95
			SlowDownMusic = SBZSetup_Function96
			break
		case 2
			SetMusicTrack("ScrapBrain2.ogg", 0, 1)
			SpeedUpMusic = SBZSetup_SBZ2SpeedUp
			SlowDownMusic = SBZSetup_SBZ2SlowDown
			break
		case 5
			SetMusicTrack("Final.ogg", 0, 0x14AC8)
			break
		end switch
	else
		if stage.actNum < 5
			SetMusicTrack("ScrapBrain.ogg", 0, 1)
			SpeedUpMusic = SBZSetup_Function95
			SlowDownMusic = SBZSetup_Function96
		else
			SetMusicTrack("Final.ogg", 0, 0x14AC8)
		end if
	end if	
	animalType1 = TypeName[Cucky]
	animalType2 = TypeName[Pocky]
	object[10].type = TypeName[SBZ Setup]
	object[10].priority = PRIORITY_ACTIVE
	object[10].drawOrder = 0
	Copy16x16Tile(592, 604)
	Copy16x16Tile(593, 605)
	Copy16x16Tile(594, 606)
	Copy16x16Tile(596, 604)
	Copy16x16Tile(597, 605)
	Copy16x16Tile(598, 606)
	switch stage.actNum
	case 1
		object[10].value1 = SBZSetup_Function93
		break
	case 2
		temp1 = 5639
		temp1 <<= 16
		temp2 = -128
		temp2 <<= 16
		CreateTempObject(TypeName[Invisible Block], 31, temp1, temp2)
		object[tempObjectPos].drawOrder = 5
		object[tempObjectPos].value0 = 16
		object[tempObjectPos].value1 = 128
		
		temp1 = 5503
		temp1 <<= 16
		temp2 = -128
		temp2 <<= 16
		CreateTempObject(TypeName[Invisible Block], 31, temp1, temp2)
		object[tempObjectPos].drawOrder = 5
		object[tempObjectPos].value0 = 16
		object[tempObjectPos].value1 = 128
	case 5
		SetPaletteEntry(0, 172, 0xC0E0E0)
		SetPaletteEntry(0, 173, 0x60A0A0)
		SetPaletteEntry(0, 174, 0x6060)
		object[10].value1 = SBZSetup_Function94
		break
	end switch
	SBZSetup_value29 = 0
	GetTableValue(SBZSetup_value30, SBZSetup_value29, SBZSetup_table25)
	if options.attractMode == 1
		if credits.screen == 6
			PlayerObject_ReplayTable = SBZSetup_table3
			PlayerObject_ReplaySize = 22
		else
			PlayerObject_ReplayTable = SBZSetup_table4
			PlayerObject_ReplaySize = 30
		end if
		PlayerObject_ReplayLength = 540
		CallFunction(PlayerObject_InitReplay)
	end if
end event

event RSDKDraw
	DrawSprite(0)
end event

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