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

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

private alias object.value0  : object.timer
private alias object.value1  : object.iconSheet
private alias object.value2  : object.textSheet
private alias object.value3  : object.round1Winner
private alias object.value4  : object.round2Winner
private alias object.value5  : object.round3Winner
private alias object.value6  : object.round1Results
private alias object.value7  : object.round2Results
private alias object.value8  : object.round3Results
private alias object.value9  : object.wins1P
private alias object.value10 : object.wins2P
private alias object.value11 : object.overallWinner
private alias object.value12 : object.zoneID
private alias object.value13 : object.vsReady
private alias object.value14 : object.hasTieBreaker

// States
private alias 0 : ZONERESULTS_SETUP
private alias 1 : ZONERESULTS_WAITING
private alias 2 : ZONERESULTS_SHOWRESULTS
private alias 3 : ZONERESULTS_FADEOUT
private alias 4 : ZONERESULTS_RETURNTOZONESEL

// 2PVSSetup Aliases
private alias object.value0 : 2PVSSetup.timer

// 2PVSSetup States
private alias 1 : 2PVSSETUP_FADEIN


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

event ObjectUpdate
	switch object.state
	case ZONERESULTS_SETUP
		SetupMenu(MENU_1, 0, 3, 0)

		LoadSpriteSheet("LevelSelect/Icons.gif")
		object.iconSheet = object.spriteSheet

		LoadSpriteSheet("LevelSelect/Text.gif")
		object.textSheet = object.spriteSheet

		menu1.selection = -1
		
		arrayPos0 = vs.match
		arrayPos0 <<= 2
		arrayPos0 += 0x400
		object.round1Results = saveRAM[arrayPos0]

		arrayPos0++
		object.round2Results = saveRAM[arrayPos0]

		arrayPos0++
		object.round3Results = saveRAM[arrayPos0]

		object.zoneID = vs.listPos
		object.zoneID >>= 1
		object.wins1P = 0
		object.wins2P = 0

		// 1P Wins
		temp0 = object.round1Results
		temp0 &= 15
		// 2P Wins
		temp1 = object.round1Results
		temp1 >>= 4
		if temp0 == temp1
			object.round1Winner = 11
		else
			if temp0 > temp1
				object.round1Winner = 9
				object.wins1P++
			else
				object.round1Winner = 10
				object.wins2P++
			end if
		end if

		// 1P Wins
		temp0 = object.round2Results
		temp0 &= 15
		// 2P Wins
		temp1 = object.round2Results
		temp1 >>= 4
		if temp0 == temp1
			object.round2Winner = 11
		else
			if temp0 > temp1
				object.round2Winner = 9
				object.wins1P++
			else
				object.round2Winner = 10
				object.wins2P++
			end if
		end if
		object.hasTieBreaker = vs.tieBreaker
		vs.tieBreaker = false

		// 1P Wins
		temp0 = object.round3Results
		temp0 &= 15
		// 2P Wins
		temp1 = object.round3Results
		temp1 >>= 4
		if temp0 == temp1
			object.round3Winner = 11
		else
			if temp0 > temp1
				object.round3Winner = 9
				object.wins1P++
			else
				object.round3Winner = 10
				object.wins2P++
			end if
		end if

		if object.wins1P == object.wins2P
			object.overallWinner = 26
		else
			if object.wins1P > object.wins2P
				object.overallWinner = 22
			else
				object.overallWinner = 24
			end if
		end if

		AddMenuEntry(MENU_1, "@@@@@@@@@@@@@@@@@@@@@@@@WINS", true)
		AddMenuEntry(MENU_1, "@", true)
		AddMenuEntry(MENU_1, "@@@@@@@@* 1 *@@@@@:", true)
		AddMenuEntry(MENU_1, "@", true)
		AddMenuEntry(MENU_1, "@", true)
		AddMenuEntry(MENU_1, "@@@@@@@@* 2 *@@@@@:", true)
		AddMenuEntry(MENU_1, "@", true)
		AddMenuEntry(MENU_1, "@", true)
		AddMenuEntry(MENU_1, "SPECIAL STAGE", true)
		AddMenuEntry(MENU_1, "@", true)
		if object.hasTieBreaker == false
			AddMenuEntry(MENU_1, "@@@@@@@@@@@@@@NO GAME", true)
		else
			AddMenuEntry(MENU_1, "@@@@@@@@@@@@@@@@@@:", true)
		end if
		AddMenuEntry(MENU_1, "@", true)
		AddMenuEntry(MENU_1, "@", true)
		AddMenuEntry(MENU_1, "@", true)
		AddMenuEntry(MENU_1, "@", true)
		AddMenuEntry(MENU_1, "@@@@@@@@@@THE ZONE", true)
		object.state++
		break

	case ZONERESULTS_WAITING
		CallNativeFunction2(ReceiveEntity, 32, true)
		ResetObjectEntity(32, TypeName[Blank Object], 0, 0, 0)
		object.state++
		break

	case ZONERESULTS_SHOWRESULTS
		if object.timer < 60
			object.timer++
		else
			CheckTouchRect(0, 0, screen.xsize, screen.ysize)
			if keyPress[0].start == true
				checkResult = 0
			end if

			if keyPress[0].buttonA == true
				checkResult = 0
			end if

			if keyPress[0].buttonB == true
				checkResult = 0
			end if

			if keyPress[0].buttonC == true
				checkResult = 0
			end if

			if checkResult > -1
				object.timer = 0
				vs.match++
				object.state = ZONERESULTS_FADEOUT
				CallNativeFunction2(SendValue, 1, true)
				object.vsReady = false
			end if
		end if
		break

	case ZONERESULTS_FADEOUT
		if object.timer < 384
			object.timer += 16
			if vs.match < vs.gameLength
				music.volume -= 10
			end if
			SetScreenFade(0x00, 0x00, 0x00, object.timer)
		else
			if vs.match >= vs.gameLength
				ResetObjectEntity(1, TypeName[Game Results], 0, 0, 0)
				object[1].priority = PRIORITY_ACTIVE
				object[0].state = 2PVSSETUP_FADEIN
				2PVSSetup[0].timer = 384
			else
				object.state = ZONERESULTS_RETURNTOZONESEL
			end if
			SetScreenFade(0x00, 0x00, 0x00, 0xFF)
		end if
		break

	case ZONERESULTS_RETURNTOZONESEL
		if ReceiveValue == -1
			// If the ReceiveValue function doesn't even exist, then force the object to be ready
			object.vsReady = true
		else
			CallNativeFunction2(ReceiveValue, object.vsReady, true)
		end if

		if object.vsReady == true
			SetMusicTrack("Options.ogg", 0, true)
			PlayMusic(0)

			ResetObjectEntity(1, TypeName[2P VS Menu 3], 0, 0, 0)
			object[1].priority = PRIORITY_ACTIVE
			object[0].state = 2PVSSETUP_FADEIN
			2PVSSetup[0].timer = 384
			SetScreenFade(0x00, 0x00, 0x00, 0xFF)
		end if
		break
		
	end switch

	object.animationTimer++
	object.animationTimer &= 15
end event


event ObjectDraw
	object.spriteSheet = object.iconSheet
	if object.state < ZONERESULTS_RETURNTOZONESEL
		// ZONE RESULTS
		DrawSpriteScreenXY(8, screen.xcenter, 17)
		DrawSpriteScreenXY(object.zoneID, screen.xcenter, 65)

		temp0 = screen.xcenter
		temp0 += 16
		temp1 = temp0
		temp1 += 32

		// Round 1 (Act 1)
		temp2 = object.round1Results
		temp2 &= 15
		temp3 = object.round1Results
		temp3 >>= 4
		DrawNumbers(12, temp0, 97, temp2, 6, 8, 0)
		DrawNumbers(12, temp1, 97, temp3, 6, 8, 0)
		
		// Round 2 (Act 2)
		temp2 = object.round2Results
		temp2 &= 15
		temp3 = object.round2Results
		temp3 >>= 4
		DrawNumbers(12, temp0, 121, temp2, 6, 8, 0)
		DrawNumbers(12, temp1, 121, temp3, 6, 8, 0)
		
		// Tie Breaker
		if object.hasTieBreaker == true
			temp2 = object.round3Results
			temp2 &= 15
			temp3 = object.round3Results
			temp3 >>= 4
			DrawNumbers(12, temp0, 161, temp2, 6, 8, 0)
			DrawNumbers(12, temp1, 161, temp3, 6, 8, 0)
		end if

		// Round Winners
		temp0 = screen.xcenter
		temp0 += 96
		DrawSpriteScreenXY(object.round1Winner, temp0, 97)
		DrawSpriteScreenXY(object.round2Winner, temp0, 121)
		if object.round3Results > 0
			DrawSpriteScreenXY(object.round3Winner, temp0, 161)
		end if
		temp0 = object.animationTimer
		temp0 >>= 3
		temp0 += object.overallWinner
		DrawSpriteScreenXY(temp0, screen.xcenter, 178)

		// Menu
		object.spriteSheet = object.textSheet
		temp0 = screen.xcenter
		temp0 -= 112
		DrawMenu(MENU_1, temp0, 80)
	else
		// Waiting...
		DrawRect(0, 0, screen.xsize, screen.ysize, 0x00, 0x00, 0x00, 0xFF)
		DrawSpriteScreenXY(28, screen.xsize, screen.ysize)
	end if
end event


event ObjectStartup
	LoadSpriteSheet("LevelSelect/Icons.gif")
	LoadSpriteSheet("LevelSelect/Text.gif")

	SpriteFrame(-88, 0, 136, 7, 103, 350)	// EMERALD HILL ZONE 	- #0
	SpriteFrame(-104, 0, 152, 7, 103, 358)	// CHEMICAL PLANT ZONE 	- #1
	SpriteFrame(-88, 0, 136, 7, 103, 366)	// AQUATIC RUIN ZONE 	- #2
	SpriteFrame(-88, 0, 136, 7, 103, 374)	// CASINO NIGHT ZONE 	- #3
	SpriteFrame(-56, 0, 104, 7, 103, 382)	// HILL TOP ZONE 		- #4
	SpriteFrame(-80, 0, 128, 7, 103, 390)	// MYSTIC CAVE ZONE 	- #5
	SpriteFrame(-64, 0, 112, 7, 103, 398)	// OIL OCEAN ZONE 		- #6
	SpriteFrame(-56, 0, 104, 7, 103, 406)	// SPECIAL STAGE 		- #7
	SpriteFrame(-48, 0, 96, 14, 1, 136)		// RESULT				- #8
	SpriteFrame(-8, 0, 16, 7, 98, 136)		// 1P text 				- #9
	SpriteFrame(-8, 0, 16, 7, 115, 136)		// 2P text 				- #10
	SpriteFrame(-16, 0, 32, 7, 132, 136) 	// TIED text 			- #11
	SpriteFrame(0, 0, 8, 7, 175, 140)		// 0 					- #12
	SpriteFrame(0, 0, 8, 7, 183, 140)		// 1 					- #13
	SpriteFrame(0, 0, 8, 7, 191, 140)		// 2 					- #14
	SpriteFrame(0, 0, 8, 7, 199, 140)		// 3 					- #15
	SpriteFrame(0, 0, 8, 7, 207, 140)		// 4 					- #16
	SpriteFrame(0, 0, 8, 7, 215, 140)		// 5 					- #17
	SpriteFrame(0, 0, 8, 7, 223, 140)		// 6 					- #18
	SpriteFrame(0, 0, 8, 7, 231, 140)		// 7 					- #19
	SpriteFrame(0, 0, 8, 7, 239, 140)		// 8 					- #20
	SpriteFrame(0, 0, 8, 7, 247, 140)		// 9 					- #21
	SpriteFrame(-47, 0, 91, 12, 1, 110)		// 1P WINS 				- #22
	SpriteFrame(-47, 0, 91, 12, 1, 123)		// 1P WINS (Flashing) 	- #23
	SpriteFrame(-47, 0, 91, 12, 93, 110)	// 2P WINS 				- #24
	SpriteFrame(-47, 0, 91, 12, 93, 123)	// 2P WINS (Flashing) 	- #25
	SpriteFrame(-22, 0, 44, 12, 1, 385)		// TIED 				- #26
	SpriteFrame(-22, 0, 44, 12, 1, 398)		// TIED (Flashing) 		- #27
	if vs.playerID == 0
		SpriteFrame(-160, -16, 136, 7, 103, 422)	// WAITING FOR 2P... 	- #28
	else
		SpriteFrame(-160, -16, 136, 7, 103, 414)	// WAITING FOR 1P... 	- #28
	end if

	foreach (TypeName[Zone Results], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].priority = PRIORITY_ACTIVE
	next
end event


// ========================
// Editor Events
// ========================

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("LevelSelect/Icons.gif")
	SpriteFrame(-48, 0, 96, 14, 1, 136)		// RESULT
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
