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

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

private alias object.value0  : object.aniTileTimer
private alias object.value14 : object.palTimer
private alias object.value15 : object.bgTowerLightPalIndex
private alias object.value16 : object.rainbowLightPalIndex
private alias object.value17 : object.chemLiquidPalIndex1
private alias object.value18 : object.chemLiquidPalIndex2
private alias object.value19 : object.chemLiquidPalIndex3

// Player Aliases
private alias object.type : player.type

// Reserved object slots
private alias 10 : SLOT_ZONESETUP

// Tracks
private alias 0 : TRACK_STAGE

// Music Loops
private alias 1 : MUSIC_LOOP_DEZ


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

private table DEZSetup_bgTowerLightPalTable
	0xE00000, 0xC00000, 0xA00000, 0x800000, 0x600000, 0x400000, 0x200000, 0x400000
	0x600000, 0x800000, 0xA00000, 0xC00000, 0xE00000, 0xE02000, 0xE04000, 0xE02000
end table

private table DEZSetup_rainbowLightPalTable
	0x00E000, 0x20C000, 0x40A000, 0x608000, 0x806000, 0xA04000, 0xC02000
	0xE00000, 0xC00020, 0xA00040, 0x800060, 0x600080, 0x4000A0, 0x2000C0
	0x0000E0, 0x0020C0, 0x0040A0, 0x006080, 0x008060, 0x00A040, 0x00C020
end table

private table DEZSetup_chemLiquidPalTable
	0x0040E0, 0x0000C0, 0x0000C0
	0x0060E0, 0x0020C0, 0x0000C0
	0x0040E0, 0x0040E0, 0x0000C0
	0x0020C0, 0x0060E0, 0x0020C0
	0x0000C0, 0x0040E0, 0x0040C0
	0x0000C0, 0x0020C0, 0x0040E0
	0x0000C0, 0x0000C0, 0x0060E0
	0x0020C0, 0x0000C0, 0x0040E0
	0x0020E0, 0x0000C0, 0x0020C0
end table

// Unused palette table, also found in CPZSetup and EndSetup and even Mania's CPZ!
// These are actually colours for one of WFZ's palette cycles
// However, you won't find this in WFZSetup because in this 2013 remake, WFZ only uses 1 of the 3 palette cycles it had in the original game,
// this one being among the 2 that were cut
// Why they were cut, I have no idea, but at the very least, it's certainly a mystery how one of them ended up here of all places...
private table DEZSetup_unusedPalette
	0x00E000, 0x00A000, 0x006000, 0x002000, 0x000000, 0x000020, 0x000060, 0x0020A0
	0x0060E0, 0x0020A0, 0x000060, 0x000020, 0x000000, 0x200000, 0x600000, 0xA00000
	0xE00000, 0xE04000, 0xE00000, 0xA00000, 0x600000, 0x200000, 0x000000, 0x202000
	0x606000, 0xA0A000, 0xE0E000, 0xA0A000, 0x606000, 0x202000, 0x000000, 0x002000
	0x006000, 0x00A000
end table


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

event ObjectUpdate
	// Animate the converyor belts in the background
	object.aniTileTimer++
	if object.aniTileTimer == 5
		object.aniTileTimer = 0
		object.frame++
		object.frame &= 7
		
		temp0 = object.frame
		temp0 += 768
		Copy16x16Tile(767, temp0)
	end if

	// Manage the various cycling palettes throughout the stage
	object.palTimer++
	if object.palTimer == 8
		object.palTimer = 0

		object.bgTowerLightPalIndex++
		object.bgTowerLightPalIndex &= 15
		GetTableValue(temp0, object.bgTowerLightPalIndex, DEZSetup_bgTowerLightPalTable)
		SetPaletteEntry(0, 175, temp0)

		object.rainbowLightPalIndex++
		object.rainbowLightPalIndex %= 21
		GetTableValue(temp0, object.rainbowLightPalIndex, DEZSetup_rainbowLightPalTable)
		SetPaletteEntry(0, 191, temp0)

		object.chemLiquidPalIndex1 += 3
		object.chemLiquidPalIndex1 %= 27
		GetTableValue(temp0, object.chemLiquidPalIndex1, DEZSetup_chemLiquidPalTable)
		SetPaletteEntry(0, 188, temp0)

		object.chemLiquidPalIndex2 += 3
		object.chemLiquidPalIndex2 %= 27
		GetTableValue(temp0, object.chemLiquidPalIndex2, DEZSetup_chemLiquidPalTable)
		SetPaletteEntry(0, 189, temp0)

		object.chemLiquidPalIndex3 += 3
		object.chemLiquidPalIndex3 %= 27
		GetTableValue(temp0, object.chemLiquidPalIndex3, DEZSetup_chemLiquidPalTable)
		SetPaletteEntry(0, 190, temp0)
	end if
end event


event ObjectStartup
	// Set the stage music
	SetMusicTrack("DeathEgg.ogg", TRACK_STAGE, MUSIC_LOOP_DEZ)

	// No speed up/slow down functions need to be set since Speed Shoes shouldn't exist in DEZ at all
	
	// Reset the boss flash colour to be black
	SetPaletteEntry(0, 192, 0x000000)

	// Start the Conveyor Belt tiles, set them to frame 1
	Copy16x16Tile(767, 768)

	// There shouldn't be any animals in DEZ, reset them
	// (though, since there aren't any they could just go left unchanged...)
	animalType1 = TypeName[Blank Object]
	animalType2 = TypeName[Blank Object]

	// Remove player 2, only P1 should be active here
	player[1].type = TypeName[Blank Object]
	playerCount = 1

	// Place a DEZ object into the scene
	object[SLOT_ZONESETUP].type 		= TypeName[DEZ Setup]
	object[SLOT_ZONESETUP].priority 	= PRIORITY_ACTIVE
	object[SLOT_ZONESETUP].drawOrder 	= 0 // This is just a leftover from this script being copied from CPZSetup, it doesn't actually do anything since this object doesn't have an ObjectDraw event
	object[SLOT_ZONESETUP].chemLiquidPalIndex2 	= 1
	object[SLOT_ZONESETUP].chemLiquidPalIndex3 	= 2

	// Set stage bounds
	temp0 = stage.curYBoundary2
	temp0 -= screen.ysize
	stage.newYBoundary1 = temp0
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
