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

//-------Aliases-------//
private alias 1 : TYPE_SPECIALSETUP
public alias 256 : GROUP_PLAYERS
public alias 65536 : HITBOX_AUTO
public alias arrayPos6 : currentPlayer
public alias arrayPos7 : playerCount

public alias saveRAM[257] : saved.spindash


public alias saveRAM[260] : saved.spikeBehavior
public alias saveRAM[261] : saved.shieldType
public alias saveRAM[262] : saved.Title
public alias saveRAM[263] : saved.Jumplock
public alias saveRAM[264] : saved.Supermusic
public alias saveRAM[265] : saved.Dropdash
public alias saveRAM[266] : saved.Instashield
public alias saveRAM[267] : saved.Peelout
public alias saveRAM[268] : saved.FlightCancel
public alias saveRAM[269] : saved.PeeloutAnim
public alias saveRAM[270] : saved.Speen
public alias saveRAM[271] : saved.Supercancel
public alias saveRAM[272] : saved.Drowning
public alias saveRAM[273] : saved.Timer
public alias saveRAM[274] : saved.region
public alias saveRAM[275] : saved.elemental
public alias saveRAM[276] : saved.rotate
public alias saveRAM[277] : saved.invincibilityType
public alias saveRAM[278] : saved.speedCap
public alias saveRAM[279] : saved.airSpeedCap
public alias saveRAM[280] : saved.GoGoPlus
public alias saveRAM[281] : saved.lives
public alias saveRAM[282] : saved.bubble

// Static Values
public value SpecialSetup_value0 = 0;
public value SpecialSetup_value1 = 0;
public value SpecialSetup_value2 = 0;
public value SpecialSetup_value3 = 0;
public value SpecialSetup_value4 = 0;

// Tables
public table SpecialSetup_table0
	0, 0, 0
end table

public table SpecialSetup_table1
	0, 0, 0
end table

public table SpecialSetup_table2
	0, 0, 0
end table

public table SpecialSetup_table3
	0xE0E0E0, 0xD0D0D0, 0xC0C0C0, 0xB0B0B0, 0xA0A0A0, 0x909090, 0x808080, 0x707070, 0x606060, 0x707070, 0x808080, 0x909090, 0xA0A0A0, 0xB0B0B0, 0xC0C0C0, 0xD0D0D0
end table


event ObjectMain
	ringTimer++
	if ringTimer == 4
		ringTimer = 0
		ringFrame++
		ringFrame &= 7
	end if
	object.value1++
	if object.value1 == 4
		object.value1 = 0
		object.value2++
		object.value2 &= 15
		GetTableValue(temp0, object.value2, SpecialSetup_table3)
		SetPaletteEntry(0, 142, temp0)
		object.value3++
		object.value3 &= 15
		GetTableValue(temp0, object.value3, SpecialSetup_table3)
		SetPaletteEntry(0, 143, temp0)
	end if
end event


event ObjectStartup
	SetMusicTrack("SpecialStage.ogg", 0, 0x79404)
	SetMusicTrack("ActComplete.ogg", 1, 0)
	SpecialSetup_value0 = 0
	object[9].type = TypeName[Special Setup]
	object[9].priority = PRIORITY_ACTIVE
	object[9].drawOrder = 1
	object[9].value3 = 8
	object[10].type = TypeName[HUD]
	object[10].priority = PRIORITY_ACTIVE
	object[10].drawOrder = 6
	if stage.playerListPos > 2
		stage.playerListPos = 0
		stage.player2Enabled = 1
	end if
	if options.vsMode == 0
		temp1 = stage.actNum
	else
		if vs.activeList == 3
			temp1 = stage.actNum
			temp1 += 3
		else
			vs.randomValue *= 0x41C64E6D
			vs.randomValue += 0x3039
			vs.randomValue &= 0x7FFFFFFF
			temp1 = vs.randomValue
			temp1 >>= 16
			temp1 %= 12
			temp1++
		end if
	end if
	temp1 <<= 3
	temp0 = temp1
	temp0 -= 8
	LoadPalette("SpecialStage.act", 0, 192, temp0, temp1)
	GetPaletteEntry(0, 197, SpecialSetup_value1)
	SpecialSetup_value1 |= -0x1000000
	GetPaletteEntry(0, 192, SpecialSetup_value2)
	SpecialSetup_value2 |= -0x1000000
	if options.attractMode == 0
	end if
	temp0 = 1
	foreach (TypeName[Start Message], arrayPos0, ALL_ENTITIES)
		object[11].type = TypeName[Start Message]
		//object[11].priority = PRIORITY_ACTIVE_PAUSED
		object[11].drawOrder = 5
		SetTableValue(object[arrayPos0].value0, 0, SpecialSetup_table0)
		SetTableValue(object[arrayPos0].value1, 0, SpecialSetup_table1)
		SetTableValue(object[arrayPos0].value2, 0, SpecialSetup_table2)
		ResetObjectEntity(arrayPos0, TypeName[Blank Object], 0, 0, 0)
	next
	temp0 = 1
	foreach (TypeName[Checkpoint], arrayPos0, ALL_ENTITIES)
		if temp0 < 3
			SetTableValue(object[arrayPos0].value0, temp0, SpecialSetup_table0)
			SetTableValue(object[arrayPos0].value1, temp0, SpecialSetup_table1)
			SetTableValue(object[arrayPos0].value2, temp0, SpecialSetup_table2)
			temp0++
		end if
		object[arrayPos0].value0 = 0
		object[arrayPos0].value1 = 0
		object[arrayPos0].value2 = 0
	next
	SpecialSetup_value3 = 0
	if stage.player2Enabled == 1
		SpecialSetup_value4 = 5
	else
		if stage.playerListPos == 2
			SpecialSetup_value4 = 13
		else
			SpecialSetup_value4 = 9
		end if
	end if
	arrayPos0 = 32
	while arrayPos0 < 0x420
		if object[arrayPos0].type == TypeName[Blank Object]
			object[arrayPos0].ypos = 0x7FFF0000
		else
			object[arrayPos0].priority = PRIORITY_INACTIVE
		end if
		arrayPos0++
	loop
	arrayPos0 = 32
	while arrayPos0 < 768
		arrayPos1 = 767
		arrayPos2 = 766
		while arrayPos1 > arrayPos0
			if object[arrayPos1].ypos < object[arrayPos2].ypos
				temp0 = object[arrayPos1].type
				temp1 = object[arrayPos1].propertyValue
				temp2 = object[arrayPos1].xpos
				temp3 = object[arrayPos1].ypos
				object[arrayPos1].type = object[arrayPos2].type
				object[arrayPos1].propertyValue = object[arrayPos2].propertyValue
				object[arrayPos1].xpos = object[arrayPos2].xpos
				object[arrayPos1].ypos = object[arrayPos2].ypos
				object[arrayPos2].type = temp0
				object[arrayPos2].propertyValue = temp1
				object[arrayPos2].xpos = temp2
				object[arrayPos2].ypos = temp3
			end if
			arrayPos1--
			arrayPos2--
		loop
		arrayPos0++
	loop
end event

event RSDKDraw
	DrawSprite(0)
end event

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