// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Game 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.round1Results
private alias object.value4  : object.round2Results
private alias object.value5  : object.round3Results
private alias object.value6  : object.wins1P
private alias object.value7  : object.wins2P
private alias object.value8  : object.overallWinner
private alias object.value9  : object.numberPos1P.x
private alias object.value10 : object.numberPos2P.x
private alias object.value11 : object.drawPos.x
private alias object.value12 : object.drawPos.y
private alias object.value13 : object.menuID

// States
private alias 0 : GAMERESULTS_SETUP
private alias 1 : GAMERESULTS_WAITING
private alias 2 : GAMERESULTS_SHOWRESULTS
private alias 3 : GAMERESULTS_FADEOUT

// Achievement Aliases
private alias 8 : ACHIEVEMENT_HEAD2HEAD


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

reserve function GameResults_DrawResults


// ========================
// Function Definition
// ========================

private function GameResults_DrawResults
	object.round1Results = saveRAM[arrayPos0]
	arrayPos0++
	object.round2Results = saveRAM[arrayPos0]
	arrayPos0++
	object.round3Results = saveRAM[arrayPos0]
	arrayPos0 += 2

	// Wins 1P
	temp2 = object.round1Results
	temp2 &= 15
	// Wins 2P
	temp3 = object.round1Results
	temp3 >>= 4
	DrawNumbers(4, object.numberPos1P.x, object.drawPos.y, temp2, 1, 8, 0)
	DrawNumbers(4, object.numberPos2P.x, object.drawPos.y, temp3, 1, 8, 0)

	if temp2 == temp3
		DrawSpriteScreenXY(3, object.drawPos.x, object.drawPos.y)
	else
		if temp2 > temp3
			DrawSpriteScreenXY(1, object.drawPos.x, object.drawPos.y)
		else
			DrawSpriteScreenXY(2, object.drawPos.x, object.drawPos.y)
		end if
	end if
	object.drawPos.y += 8

	// Wins 1P
	temp2 = object.round2Results
	temp2 &= 15
	// Wins 2P
	temp3 = object.round2Results
	temp3 >>= 4
	DrawNumbers(4, object.numberPos1P.x, object.drawPos.y, temp2, 1, 8, 0)
	DrawNumbers(4, object.numberPos2P.x, object.drawPos.y, temp3, 1, 8, 0)

	if temp2 == temp3
		DrawSpriteScreenXY(3, object.drawPos.x, object.drawPos.y)
	else
		if temp2 > temp3
			DrawSpriteScreenXY(1, object.drawPos.x, object.drawPos.y)
		else
			DrawSpriteScreenXY(2, object.drawPos.x, object.drawPos.y)
		end if
	end if

	object.drawPos.y += 8
	if object.round3Results > 0
		// Wins 1P
		temp2 = object.round3Results
		temp2 &= 15
		// Wins 2P
		temp3 = object.round3Results
		temp3 >>= 4
		DrawNumbers(4, object.numberPos1P.x, object.drawPos.y, temp2, 1, 8, 0)
		DrawNumbers(4, object.numberPos2P.x, object.drawPos.y, temp3, 1, 8, 0)

		if temp2 == temp3
			DrawSpriteScreenXY(3, object.drawPos.x, object.drawPos.y)
		else
			if temp2 > temp3
				DrawSpriteScreenXY(1, object.drawPos.x, object.drawPos.y)
			else
				DrawSpriteScreenXY(2, object.drawPos.x, object.drawPos.y)
			end if
		end if
	end if
	object.drawPos.y += 16
end function


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

event ObjectUpdate
	switch object.state
	case GAMERESULTS_SETUP
		SetupMenu(MENU_1, 0, 3, 0)
		SetupMenu(MENU_2, 0, 3, 0)
		options.vsMode = false

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

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

		menu1.selection = -1

		temp0 = 0
		temp1 = vs.gameLength
		if temp1 > 4
			arrayPos0 = 0x400
			AddMenuEntry(MENU_1, "@@@@@@@@@@@@@WINS@@@@@@@@@@@@@@WINS", true)
			AddMenuEntry(MENU_1, "@", true)
			object.menuID = MENU_1

			while temp0 < vs.gameLength
				object.round1Results = saveRAM[arrayPos0]
				arrayPos0++
				object.round2Results = saveRAM[arrayPos0]
				arrayPos0++
				object.round3Results = saveRAM[arrayPos0]
				arrayPos0++
				temp2 = saveRAM[arrayPos0]
				arrayPos0++

				temp3 = object.round1Results
				temp3 &= 15
				temp4 = object.round1Results
				temp4 >>= 4
				if temp3 != temp4
					if temp3 > temp4
						object.wins1P++
					else
						object.wins2P++
					end if
				end if

				temp3 = object.round2Results
				temp3 &= 15
				temp4 = object.round2Results
				temp4 >>= 4
				if temp3 != temp4
					if temp3 > temp4
						object.wins1P++
					else
						object.wins2P++
					end if
				end if

				temp3 = object.round3Results
				temp3 &= 15
				temp4 = object.round3Results
				temp4 >>= 4
				if temp3 != temp4
					if temp3 > temp4
						object.wins1P++
					else
						object.wins2P++
					end if
				end if

				switch temp2
				case 0
					AddMenuEntry(object.menuID, "@EHZ 1*@@:", true)
					AddMenuEntry(object.menuID, "@@@@ 2*@@:", true)
					if object.round3Results > 0
						AddMenuEntry(object.menuID, "@@@@ SS@@:", true)
					else
						AddMenuEntry(object.menuID, "@@@@ SS@NO GAME", true)
					end if
					break

				case 1
					AddMenuEntry(object.menuID, "@CPZ 1*@@:", true)
					AddMenuEntry(object.menuID, "@@@@ 2*@@:", true)
					if object.round3Results > 0
						AddMenuEntry(object.menuID, "@@@@ SS@@:", true)
					else
						AddMenuEntry(object.menuID, "@@@@ SS@NO GAME", true)
					end if
					break

				case 2
					AddMenuEntry(object.menuID, "@ARZ 1*@@:", true)
					AddMenuEntry(object.menuID, "@@@@ 2*@@:", true)
					if object.round3Results > 0
						AddMenuEntry(object.menuID, "@@@@ SS@@:", true)
					else
						AddMenuEntry(object.menuID, "@@@@ SS@NO GAME", true)
					end if
					break

				case 3
					AddMenuEntry(object.menuID, "@CNZ 1*@@:", true)
					AddMenuEntry(object.menuID, "@@@@ 2*@@:", true)
					if object.round3Results > 0
						AddMenuEntry(object.menuID, "@@@@ SS@@:", true)
					else
						AddMenuEntry(object.menuID, "@@@@ SS@NO GAME", true)
					end if
					break

				case 4
					AddMenuEntry(object.menuID, "@HTZ 1*@@:", true)
					AddMenuEntry(object.menuID, "@@@@ 2*@@:", true)
					if object.round3Results > 0
						AddMenuEntry(object.menuID, "@@@@ SS@@:", true)
					else
						AddMenuEntry(object.menuID, "@@@@ SS@NO GAME", true)
					end if
					break

				case 5
					AddMenuEntry(object.menuID, "@MCZ 1*@@:", true)
					AddMenuEntry(object.menuID, "@@@@ 2*@@:", true)
					if object.round3Results > 0
						AddMenuEntry(object.menuID, "@@@@ SS@@:", true)
					else
						AddMenuEntry(object.menuID, "@@@@ SS@NO GAME", true)
					end if
					break

				case 6
					AddMenuEntry(object.menuID, "@OOZ 1*@@:", true)
					AddMenuEntry(object.menuID, "@@@@ 2*@@:", true)
					if object.round3Results > 0
						AddMenuEntry(object.menuID, "@@@@ SS@@:", true)
					else
						AddMenuEntry(object.menuID, "@@@@ SS@NO GAME", true)
					end if
					break

				case 7
					AddMenuEntry(object.menuID, "@S.S 1*@@:", true)
					AddMenuEntry(object.menuID, "@@@@ 2*@@:", true)
					if object.round3Results > 0
						AddMenuEntry(object.menuID, "@@@@ 3*@@:", true)
					else
						AddMenuEntry(object.menuID, "@@@@ 3*@NO GAME", true)
					end if
					break
				end switch

				AddMenuEntry(object.menuID, "@", true)
				temp0++
				if temp0 == 4
					object.menuID = MENU_2
				end if
			loop

			while temp0 < 4
				AddMenuEntry(MENU_1, "", true)
				AddMenuEntry(MENU_1, "", true)
				AddMenuEntry(MENU_1, "", true)
				AddMenuEntry(MENU_1, "", true)
				temp0++
			loop
		else
			arrayPos0 = 0x400
			AddMenuEntry(MENU_1, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@WINS", true)
			AddMenuEntry(MENU_1, "@", true)

			while temp0 < temp1
				object.round1Results = saveRAM[arrayPos0]
				arrayPos0++
				object.round2Results = saveRAM[arrayPos0]
				arrayPos0++
				object.round3Results = saveRAM[arrayPos0]
				arrayPos0++
				temp2 = saveRAM[arrayPos0]
				arrayPos0++

				temp3 = object.round1Results
				temp3 &= 15
				temp4 = object.round1Results
				temp4 >>= 4
				if temp3 != temp4
					if temp3 > temp4
						object.wins1P++
					else
						object.wins2P++
					end if
				end if

				temp3 = object.round2Results
				temp3 &= 15
				temp4 = object.round2Results
				temp4 >>= 4
				if temp3 != temp4
					if temp3 > temp4
						object.wins1P++
					else
						object.wins2P++
					end if
				end if

				temp3 = object.round3Results
				temp3 &= 15
				temp4 = object.round3Results
				temp4 >>= 4
				if temp3 != temp4
					if temp3 > temp4
						object.wins1P++
					else
						object.wins2P++
					end if
				end if

				switch temp2
				case 0
					AddMenuEntry(MENU_1, "@@EMERALD HILL *1*@@@@:", true)
					AddMenuEntry(MENU_1, "@@@@@@@@@@@@@@ *2*@@@@:", true)
					if object.round3Results > 0
						AddMenuEntry(MENU_1, "@@@@@@@@@@@@@@ S.S@@@@:", true)
					else
						AddMenuEntry(MENU_1, "@@@@@@@@@@@@@@ S.S@@NO GAME", true)
					end if
					break

				case 1
					AddMenuEntry(MENU_1, "CHEMICAL PLANT *1*@@@@:", true)
					AddMenuEntry(MENU_1, "@@@@@@@@@@@@@@ *2*@@@@:", true)
					if object.round3Results > 0
						AddMenuEntry(MENU_1, "@@@@@@@@@@@@@@ S.S@@@@:", true)
					else
						AddMenuEntry(MENU_1, "@@@@@@@@@@@@@@ S.S@@NO GAME", true)
					end if
					break

				case 2
					AddMenuEntry(MENU_1, "@@AQUATIC RUIN *1*@@@@:", true)
					AddMenuEntry(MENU_1, "@@@@@@@@@@@@@@ *2*@@@@:", true)
					if object.round3Results > 0
						AddMenuEntry(MENU_1, "@@@@@@@@@@@@@@ S.S@@@@:", true)
					else
						AddMenuEntry(MENU_1, "@@@@@@@@@@@@@@ S.S@@NO GAME", true)
					end if
					break

				case 3
					AddMenuEntry(MENU_1, "@@CASINO NIGHT *1*@@@@:", true)
					AddMenuEntry(MENU_1, "@@@@@@@@@@@@@@ *2*@@@@:", true)
					if object.round3Results > 0
						AddMenuEntry(MENU_1, "@@@@@@@@@@@@@@ S.S@@@@:", true)
					else
						AddMenuEntry(MENU_1, "@@@@@@@@@@@@@@ S.S@@NO GAME", true)
					end if
					break

				case 4
					AddMenuEntry(MENU_1, "@@@@@@HILL TOP *1*@@@@:", true)
					AddMenuEntry(MENU_1, "@@@@@@@@@@@@@@ *2*@@@@:", true)
					if object.round3Results > 0
						AddMenuEntry(MENU_1, "@@@@@@@@@@@@@@ S.S@@@@:", true)
					else
						AddMenuEntry(MENU_1, "@@@@@@@@@@@@@@ S.S@@NO GAME", true)
					end if
					break

				case 5
					AddMenuEntry(MENU_1, "@@@MYSTIC CAVE *1*@@@@:", true)
					AddMenuEntry(MENU_1, "@@@@@@@@@@@@@@ *2*@@@@:", true)
					if object.round3Results > 0
						AddMenuEntry(MENU_1, "@@@@@@@@@@@@@@ S.S@@@@:", true)
					else
						AddMenuEntry(MENU_1, "@@@@@@@@@@@@@@ S.S@@NO GAME", true)
					end if
					break

				case 6
					AddMenuEntry(MENU_1, "@@@@@OIL OCEAN *1*@@@@:", true)
					AddMenuEntry(MENU_1, "@@@@@@@@@@@@@@ *2*@@@@:", true)
					if object.round3Results > 0
						AddMenuEntry(MENU_1, "@@@@@@@@@@@@@@ S.S@@@@:", true)
					else
						AddMenuEntry(MENU_1, "@@@@@@@@@@@@@@ S.S@@NO GAME", true)
					end if
					break

				case 7
					AddMenuEntry(MENU_1, "@SPECIAL STAGE *1*@@@@:", true)
					AddMenuEntry(MENU_1, "@@@@@@@@@@@@@@ *2*@@@@:", true)
					if object.round3Results > 0
						AddMenuEntry(MENU_1, "@@@@@@@@@@@@@@ *3*@@@@:", true)
					else
						AddMenuEntry(MENU_1, "@@@@@@@@@@@@@@ *3*@@NO GAME", true)
					end if
					break
				end switch

				AddMenuEntry(MENU_1, "@", true)
				temp0++
			loop

			while temp0 < 4
				AddMenuEntry(MENU_1, "@", true)
				AddMenuEntry(MENU_1, "@", true)
				AddMenuEntry(MENU_1, "@", true)
				AddMenuEntry(MENU_1, "@", true)
				temp0++
			loop
		end if

		AddMenuEntry(MENU_1, "@@@@@@@* TOTAL *@@@@@@:", true)
		AddMenuEntry(MENU_1, "@", true)
		AddMenuEntry(MENU_1, "@", true)
		AddMenuEntry(MENU_1, "@", true)
		AddMenuEntry(MENU_1, "@@@@@@@@@@@@@@THE GAME", true)

		if object.wins1P == object.wins2P
			object.overallWinner = 18
		else
			// Here, we grant the "Head 2 Head" Achievement if we won
			// (The Achievement's description may say you need to beat a friend, but thankfully it isn't really that way)
			
			if object.wins1P > object.wins2P
				object.overallWinner = 14
				if vs.playerID == 0
					CallNativeFunction2(SetAchievement, ACHIEVEMENT_HEAD2HEAD, 100)
				end if
			else
				object.overallWinner = 16
				if vs.playerID == 1
					CallNativeFunction2(SetAchievement, ACHIEVEMENT_HEAD2HEAD, 100)
				end if
			end if
		end if
		object.state++
		break

	case GAMERESULTS_WAITING
		object.state++
		break
		
	case GAMERESULTS_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
				object.state = GAMERESULTS_FADEOUT
			end if
		end if
		break

	case GAMERESULTS_FADEOUT
		if object.timer < 256
			music.volume -= 10
			object.timer += 16
			SetScreenFade(0x00, 0x00, 0x00, object.timer)
		else
			StopMusic()
			CallNativeFunction(Disconnect2PVS)
			engine.state = 8
			SetScreenFade(0x00, 0x00, 0x00, 0xFF)
		end if
		break
		
	end switch

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


event ObjectDraw
	object.spriteSheet = object.iconSheet
	DrawSpriteScreenXY(0, screen.xcenter, 17)

	arrayPos0 = 0x400
	if vs.gameLength <= 4
		object.numberPos1P.x = screen.xcenter
		object.numberPos1P.x += 16
		object.numberPos2P.x = object.numberPos1P.x
		object.numberPos2P.x += 32
		object.drawPos.x = screen.xcenter
		object.drawPos.x += 120
		object.drawPos.y = 57

		temp0 = 0
		while temp0 < vs.gameLength
			CallFunction(GameResults_DrawResults)
			temp0++
		loop
	else
		object.numberPos1P.x = screen.xcenter
		object.numberPos1P.x -= 80
		object.numberPos2P.x = object.numberPos1P.x
		object.numberPos2P.x += 16
		object.drawPos.x = screen.xcenter
		object.drawPos.x -= 24
		object.drawPos.y = 57

		temp0 = 0
		while temp0 < 4
			CallFunction(GameResults_DrawResults)
			temp0++
		loop

		object.numberPos1P.x = screen.xcenter
		object.numberPos1P.x += 64
		object.numberPos2P.x = object.numberPos1P.x
		object.numberPos2P.x += 16
		object.drawPos.x = screen.xcenter
		object.drawPos.x += 120
		object.drawPos.y = 57
		while temp0 < vs.gameLength
			CallFunction(GameResults_DrawResults)
			temp0++
		loop
	end if

	object.numberPos1P.x = screen.xcenter
	object.numberPos1P.x += 24
	object.numberPos2P.x = object.numberPos1P.x
	object.numberPos2P.x += 24
	DrawNumbers(4, object.numberPos1P.x, 185, object.wins1P, 2, 8, 1)
	DrawNumbers(4, object.numberPos2P.x, 185, object.wins2P, 2, 8, 1)

	temp0 = object.animationTimer
	temp0 >>= 3
	temp0 += object.overallWinner
	DrawSpriteScreenXY(temp0, screen.xcenter, 198)

	object.spriteSheet = object.textSheet
	temp0 = screen.xcenter
	temp0 -= 144
	DrawMenu(MENU_1, temp0, 40)

	if vs.gameLength > 4
		DrawMenu(MENU_2, screen.xcenter, 56)
	end if
end event


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

	SpriteFrame(-48, 0, 96, 14, 1, 136)		// RESULT				- #0
	SpriteFrame(-8, 0, 16, 7, 98, 136)		// 1P					- #1
	SpriteFrame(-8, 0, 16, 7, 115, 136)		// 2P					- #2
	SpriteFrame(-16, 0, 32, 7, 132, 136)	// TIED					- #3
	SpriteFrame(0, 0, 8, 7, 175, 140)		// 0					- #4
	SpriteFrame(0, 0, 8, 7, 183, 140)		// 1					- #5
	SpriteFrame(0, 0, 8, 7, 191, 140)		// 2					- #6
	SpriteFrame(0, 0, 8, 7, 199, 140)		// 3					- #7
	SpriteFrame(0, 0, 8, 7, 207, 140)		// 4					- #8
	SpriteFrame(0, 0, 8, 7, 215, 140)		// 5					- #9
	SpriteFrame(0, 0, 8, 7, 223, 140)		// 6					- #10
	SpriteFrame(0, 0, 8, 7, 231, 140)		// 7					- #11
	SpriteFrame(0, 0, 8, 7, 239, 140)		// 8					- #12
	SpriteFrame(0, 0, 8, 7, 247, 140)		// 9					- #13
	SpriteFrame(-47, 0, 91, 12, 1, 110)		// 1P WINS				- #14
	SpriteFrame(-47, 0, 91, 12, 1, 123)		// 1P WINS (flashing)	- #15
	SpriteFrame(-47, 0, 91, 12, 93, 110)	// 2P WINS				- #16
	SpriteFrame(-47, 0, 91, 12, 93, 123)	// 2P WINS (flashing)	- #17
	SpriteFrame(-22, 0, 44, 12, 1, 385)		// TIED					- #18
	SpriteFrame(-22, 0, 44, 12, 1, 398)		// TIED (flashing)		- #19
	
	// Game Results objects aren't even placed in the scene normally, but if one is then make it active
	foreach (TypeName[Game Results], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].priority = PRIORITY_ACTIVE
		
#platform: USE_DECOMP
		// Hacky fix for this loading in place of the complete save select on an Origins RSDK
		// -> This happens because, when adding the 2P VS Menu 4 object, the Origins folks added it into the 
		//    middle of the object list, therefore offsetting the IDs all objects after it.
		//    That included the Stage Menu object (formerly ID 12), where it got bumped up and now its scene
		//    is comprised of a Game Results object (current ID 12) instead.
		if stage.activeList == REGULAR_STAGE
			// Just turn the object into a Stage Menu object, that way it'll be caught by the Stage Menu object's Startup
			// (Do note, ID-wise the Stage Menu comes after this object and their Startups are run in that order too so we don't gotta do any more than this here)
			object[arrayPos0].type = TypeName[Stage Menu]
		end if
#endplatform
		
	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				- #0
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
