// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: 2P VS Menu 3 Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// Slot 0 = 2PVSSetup
// Slot 1 = 2PVSMenu (should be this entity)
// Slot 2 = other 2PVSMenu (the other player's 2PVSMenu3 that we got from ReceiveEntity)

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

private alias object.value0 : object.timer
private alias object.value1 : object.iconSheet
private alias object.value2 : object.textSheet
private alias object.value3 : object.selection
private alias object.value4 : object.selectionTimer
private alias object.value5 : object.selectedCharacter
private alias object.value6 : object.vsSendReceieveValue

// States
private alias 0 : 2PVSMENU3_SETUP
private alias 1 : 2PVSMENU3_SETUPROOM
private alias 2 : 2PVSMENU3_FINISHROOMSETUP
private alias 3 : 2PVSMENU3_SELECTING
private alias 4 : 2PVSMENU3_STAGESELECTED
private alias 5 : 2PVSMENU3_SHOWSELSTAGE
private alias 6 : 2PVSMENU3_TOSELECTEDSTAGE
private alias 7 : 2PVSMENU3_TOMAINMENU
private alias 8 : 2PVSMENU3_NONE

// Selections
private alias 0 : 2PVSMENU3_SEL_NONE
private alias 1 : 2PVSMENU3_SEL_EHZ
private alias 2 : 2PVSMENU3_SEL_CPZ
private alias 3 : 2PVSMENU3_SEL_ARZ
private alias 4 : 2PVSMENU3_SEL_CNZ
private alias 5 : 2PVSMENU3_SEL_HTZ
private alias 6 : 2PVSMENU3_SEL_MCZ
private alias 7 : 2PVSMENU3_SEL_OOZ
private alias 8 : 2PVSMENU3_SEL_SS

// ZoneButton Aliases
private alias object.value1 : zoneButton.zoneDetailsWidth
private alias object.value2 : zoneButton.disabled

// 2PVSSetup States
private alias 3 : 2PVSSETUP_TOREGULARSTAGE
private alias 4 : 2PVSSETUP_TOSPECIALSTAGE


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

reserve function 2PVSMenu3_SetupIcons
reserve function 2PVSMenu3_HandleIconMovement


// ========================
// Function Definitions
// ========================

private function 2PVSMenu3_SetupIcons
	object[10].alpha = 256
	object[11].alpha = 256
	object[12].alpha = 256
	object[13].alpha = 256
	object[14].alpha = 256
	object[15].alpha = 256
	object[16].alpha = 256
	object[17].alpha = 256

	object[+1].selectedCharacter = vs.player2Type
	object[+1].selectedCharacter += 2
	object.state = 2PVSMENU3_SELECTING
	if options.physicalControls == false
		object.selection = 2PVSMENU3_SEL_NONE
	else
		object.selection = 2PVSMENU3_SEL_EHZ
	end if
end function


private function 2PVSMenu3_HandleIconMovement
	object[10].xpos = -108
	object[10].xpos *= object.scale
	object[10].xpos >>= 8
	object[14].xpos = object[10].xpos

	object[11].xpos = -36
	object[11].xpos *= object.scale
	object[11].xpos >>= 8
	object[15].xpos = object[11].xpos

	object[12].xpos = 36
	object[12].xpos *= object.scale
	object[12].xpos >>= 8
	object[16].xpos = object[12].xpos

	object[13].xpos = 108
	object[13].xpos *= object.scale
	object[13].xpos >>= 8
	object[17].xpos = object[13].xpos

	object[10].ypos = -28
	object[10].ypos *= object.scale
	object[10].ypos >>= 8
	object[11].ypos = object[10].ypos
	object[12].ypos = object[10].ypos
	object[13].ypos = object[10].ypos

	object[14].ypos = 28
	object[14].ypos *= object.scale
	object[14].ypos >>= 8
	object[15].ypos = object[14].ypos
	object[16].ypos = object[14].ypos
	object[17].ypos = object[14].ypos
end function


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

event ObjectUpdate
	switch object.state
	case 2PVSMENU3_SETUP
		LoadSpriteSheet("LevelSelect/Icons.gif")
		object.iconSheet = object.spriteSheet

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

		if engine.deviceType == STANDARD
			options.physicalControls = true
		end if

		menu1.selection = -1
		object.scale = 256
		object.selectedCharacter = stage.playerListPos
		object.selectedCharacter += 2
		SetupMenu(MENU_1, 0, 3, 2)
		AddMenuEntry(MENU_1, "MATCH:", true)
		AddMenuEntry(MENU_1, "@", true)
		AddMenuEntry(MENU_1, "OF", true)

		arrayPos0 = 10
		
		// EHZ
		ResetObjectEntity(arrayPos0, TypeName[Zone Button], 1, -108, -28)
		object[arrayPos0].priority = PRIORITY_ACTIVE
		arrayPos0++

		// CPZ
		ResetObjectEntity(arrayPos0, TypeName[Zone Button], 2, -36, -28)
		object[arrayPos0].priority = PRIORITY_ACTIVE
		arrayPos0++

		// ARZ
		ResetObjectEntity(arrayPos0, TypeName[Zone Button], 3, 36, -28)
		object[arrayPos0].priority = PRIORITY_ACTIVE
		arrayPos0++

		// CNZ
		ResetObjectEntity(arrayPos0, TypeName[Zone Button], 4, 108, -28)
		object[arrayPos0].priority = PRIORITY_ACTIVE
		arrayPos0++

		// HTZ
		ResetObjectEntity(arrayPos0, TypeName[Zone Button], 5, -108, 28)
		object[arrayPos0].priority = PRIORITY_ACTIVE
		arrayPos0++

		// MCZ
		ResetObjectEntity(arrayPos0, TypeName[Zone Button], 6, -36, 28)
		object[arrayPos0].priority = PRIORITY_ACTIVE
		arrayPos0++

		// OOZ
		ResetObjectEntity(arrayPos0, TypeName[Zone Button], 7, 36, 28)
		object[arrayPos0].priority = PRIORITY_ACTIVE
		arrayPos0++

		// Special Stage
		ResetObjectEntity(arrayPos0, TypeName[Zone Button], 8, 108, 28)
		object[arrayPos0].priority = PRIORITY_ACTIVE

		object[2].state = 2PVSMENU3_SELECTING

#platform: USE_STANDALONE
		if options.vsMode == true // we prolly got here from results screen
			CallFunction(2PVSMenu3_SetupIcons)
		else
			object.state  = 2PVSMENU3_SETUPROOM
			object.vsSendReceieveValue = false
			object.timer  = 0
			vs.randomValue = 313
		end if
#endplatform

#platform: USE_ORIGINS
		options.vsMode = true
		vs.playerID = 0
		CallFunction(2PVSMenu3_SetupIcons)
		CallNativeFunction2(NotifyCallback, NOTIFY_1P_VS_SELECT, 1)
#endplatform
		break

	case 2PVSMENU3_SETUPROOM
		// if ReceiveValue isn't set do some dummy stuff
		if ReceiveValue == -1
			options.vsMode = true
			object.state = 2PVSMENU3_FINISHROOMSETUP
			if vs.gameLength == 0
				vs.gameLength = 4
			end if

			if vs.itemMode == 0
				vs.itemMode = 1
			end if
		end if

		CallNativeFunction2(ReceiveValue, object.vsSendReceieveValue, true)
		if object.vsSendReceieveValue > 1
			vs.gameLength = object.vsSendReceieveValue
			vs.gameLength >>= 4
			vs.gameLength &= 15

			vs.itemMode = object.vsSendReceieveValue
			vs.itemMode >>= 8
			vs.itemMode &= 15

			vs.playerID = object.vsSendReceieveValue
			vs.playerID >>= 12
			vs.playerID &= 15

			options.vsMode = true
			object.state = 2PVSMENU3_FINISHROOMSETUP
		end if

		if object.vsSendReceieveValue == true
			object.state = 2PVSMENU3_TOMAINMENU
		end if
		break

	case 2PVSMENU3_FINISHROOMSETUP
		object.animationTimer++
		object.animationTimer &= 15
		if object.timer < 120
			object.timer++
			if object.timer == 60
				CallNativeFunction2(TransmitGlobal, "vs.player2Type", stage.playerListPos)
			end if
		else
			// if ReceiveValue isn't set do some dummy stuff
			if ReceiveValue == -1
				if keyPress[0].buttonC == true
					Rand(vs.player2Type, 3)
				end if
			end if

			if vs.player2Type > -1
				PlaySfx(SfxName[Star Post], false)
				object.timer = 0
				CallFunction(2PVSMenu3_SetupIcons)
			end if
		end if
		break

	case 2PVSMENU3_SELECTING
		CallNativeFunction2(ReceiveEntity, 2, true)
		if object[2].type != TypeName[Blank Object]
			if object[2].type != TypeName[2P VS Menu 3]
				ResetObjectEntity(2, TypeName[Blank Object], 0, 0, 0)
			end if
		end if

		object[2].type = TypeName[Blank Object]
		if object[2].state == 2PVSMENU3_SELECTING
			if options.physicalControls == false
				CheckTouchRect(0, 0, screen.xsize, screen.ysize)
				if checkResult == -1
					if object.selection > 2PVSMENU3_SEL_NONE
						arrayPos0 = object.selection
						arrayPos0 += 9
						if zoneButton[arrayPos0].disabled == false
							object.state = 2PVSMENU3_STAGESELECTED
							PlaySfx(SfxName[Menu Select], false)
							CallNativeFunction2(SendEntity, 1, true)
						else
							PlaySfx(SfxName[Fail], false)
						end if
					end if
				end if

				if object.state != 2PVSMENU3_STAGESELECTED
					object.selection = 2PVSMENU3_SEL_NONE
					if keyPress[0].left == true
						options.physicalControls = true
						object.selection = 2PVSMENU3_SEL_SS
					end if

					if keyPress[0].right == true
						options.physicalControls = true
						object.selection = 2PVSMENU3_SEL_EHZ
					end if

					if keyPress[0].up == 1
						options.physicalControls = true
						object.selection = 2PVSMENU3_SEL_HTZ
					end if

					if keyPress[0].down == true
						options.physicalControls = true
						object.selection = 2PVSMENU3_SEL_EHZ
					end if
				end if
			else
				if keyPress[0].left == true
					object.selection--
					if object.selection < 2PVSMENU3_SEL_EHZ
						object.selection = 2PVSMENU3_SEL_SS
					end if
					PlaySfx(SfxName[Menu Move], false)
				end if

				if keyPress[0].right == true
					object.selection++
					if object.selection > 2PVSMENU3_SEL_SS
						object.selection = 2PVSMENU3_SEL_EHZ
					end if
					PlaySfx(SfxName[Menu Move], false)
				end if

				if keyPress[0].up == true
					object.selection -= 4
					if object.selection < 2PVSMENU3_SEL_EHZ
						object.selection += 2PVSMENU3_SEL_SS
					end if
					PlaySfx(SfxName[Menu Move], false)
				end if

				if keyPress[0].down == true
					object.selection += 4
					if object.selection > 2PVSMENU3_SEL_SS
						object.selection -= 2PVSMENU3_SEL_SS
					end if
					PlaySfx(SfxName[Menu Move], false)
				end if

				if keyPress[0].start == true
					arrayPos0 = object.selection
					arrayPos0 += 9
					if zoneButton[arrayPos0].disabled == false
						object.state = 2PVSMENU3_STAGESELECTED
						PlaySfx(SfxName[Menu Select], false)
						CallNativeFunction2(SendEntity, 1, true)
					else
						PlaySfx(SfxName[Fail], false)
					end if
				end if

				if keyPress[0].buttonA == true
					arrayPos0 = object.selection
					arrayPos0 += 9
					if zoneButton[arrayPos0].disabled == false
						object.state = 2PVSMENU3_STAGESELECTED
						PlaySfx(SfxName[Menu Select], false)
						CallNativeFunction2(SendEntity, 1, true)
					else
						PlaySfx(SfxName[Fail], false)
					end if
				end if

				if object.state != 2PVSMENU3_STAGESELECTED
					CheckTouchRect(0, 0, screen.xsize, screen.ysize)
					if checkResult > -1
						options.physicalControls = false
						object.selection = 2PVSMENU3_SEL_MCZ
					end if
				end if
			end if
		else
			object.state = 2PVSMENU3_STAGESELECTED
			object.selection = object[2].selection
			PlaySfx(SfxName[Menu Select], false)
		end if
		break

	case 2PVSMENU3_STAGESELECTED
		if object.scale > 0
			object.scale -= 8
		end if
		object[10].alpha = object.scale
		object[11].alpha = object.scale
		object[12].alpha = object.scale
		object[13].alpha = object.scale
		object[14].alpha = object.scale
		object[15].alpha = object.scale
		object[16].alpha = object.scale
		object[17].alpha = object.scale

		object[10].drawOrder = 3
		object[11].drawOrder = 3
		object[12].drawOrder = 3
		object[13].drawOrder = 3
		object[14].drawOrder = 3
		object[15].drawOrder = 3
		object[16].drawOrder = 3
		object[17].drawOrder = 3

		arrayPos0 = object.selection
		arrayPos0 += 9
		object[arrayPos0].alpha = 0x100
		object[arrayPos0].drawOrder = 4

		arrayPos0 = object.selection
		arrayPos0 += 9
		CallFunction(2PVSMenu3_HandleIconMovement)
		if object.selectionTimer < 32
			object.selectionTimer++
		else
			object.selectionTimer = 0
			object.state = 2PVSMENU3_SHOWSELSTAGE
		end if
		break

	case 2PVSMENU3_SHOWSELSTAGE
		arrayPos0 = object.selection
		arrayPos0 += 9
		if zoneButton[arrayPos0].zoneDetailsWidth < 224
			zoneButton[arrayPos0].zoneDetailsWidth += 8
		else
			if object.selection < 2PVSMENU3_SEL_SS
				temp0 = object.selection
				temp0--
				temp0 <<= 1
				vs.listPos = temp0
				vs.activeList = REGULAR_STAGE
			else
				vs.listPos = 4
				vs.activeList = SPECIAL_STAGE
			end if


			// We selected this stage, so lets remember that for next time we come back here
			SetBit(vs.stageMask, object[arrayPos0].propertyValue, true)

			object.state = 2PVSMENU3_TOSELECTEDSTAGE
			if vs.playerID == 0
				object.vsSendReceieveValue = vs.listPos
				object.vsSendReceieveValue <<= 4
				object.vsSendReceieveValue += vs.activeList
				CallNativeFunction2(SendValue, object.vsSendReceieveValue, true)
			else
				object.vsSendReceieveValue = 0
			end if
		end if
		break

	case 2PVSMENU3_TOSELECTEDSTAGE
		object.timer++
		if object.timer >= 60
			if vs.playerID == 0
				stage.activeList = vs.activeList
				stage.listPos = vs.listPos
				if stage.activeList == REGULAR_STAGE
					object[0].state = 2PVSSETUP_TOREGULARSTAGE
				else
					PlaySfx(SfxName[Warp], false)
					object[0].state = 2PVSSETUP_TOSPECIALSTAGE
				end if
				object.state = 2PVSMENU3_NONE
			else
				CallNativeFunction2(ReceiveValue, object.vsSendReceieveValue, true)
				if object.vsSendReceieveValue > 0
					vs.activeList = object.vsSendReceieveValue
					vs.activeList &= 0x0F

					vs.listPos = object.vsSendReceieveValue
					vs.listPos >>= 4

					stage.activeList = vs.activeList
					stage.listPos = vs.listPos
					if stage.activeList == REGULAR_STAGE
						object[0].state = 2PVSSETUP_TOREGULARSTAGE
					else
						PlaySfx(SfxName[Warp], false)
						object[0].state = 2PVSSETUP_TOSPECIALSTAGE
					end if
					object.state = 2PVSMENU3_NONE
				end if
			end if
		end if
		break
		
	case 2PVSMENU3_TOMAINMENU
		StopMusic()
		options.vsMode = false
#platform: USE_STANDALONE
		engine.state = 8
#endplatform
#platform: USE_ORIGINS
		stage.activeList = PRESENTATION_STAGE
		stage.listPos = 0
		LoadStage()
#endplatform
		SetScreenFade(0x00, 0x00, 0x00, 0xFF)
		break
		
	end switch
end event


event ObjectDraw
	// STAGE SELECT
	object.spriteSheet = object.iconSheet
	DrawSpriteScreenXY(0, screen.xcenter, 17)
	DrawSpriteScreenXY(1, screen.xcenter, 17)

	temp0 = screen.xcenter
	temp0 -= 64
	if vs.playerID == 0
		DrawSpriteScreenXY(object.selectedCharacter, temp0, 56)
		DrawSpriteScreenXY(7, temp0, 56)
	else
		if object.state > 2PVSMENU3_FINISHROOMSETUP
			// Player 1 Character
			DrawSpriteScreenXY(object[+1].selectedCharacter, temp0, 56)
		else
			// Waiting For...
			if object.animationTimer < 8
				DrawSpriteScreenXY(19, temp0, 56)
			end if
		end if
		// 1P
		DrawSpriteScreenXY(5, temp0, 56)
	end if

	temp0 = screen.xcenter
	temp0 += 64
	if vs.playerID == 1
		DrawSpriteScreenXY(object.selectedCharacter, temp0, 56)
		DrawSpriteScreenXY(8, temp0, 56)
	else
		if object.state > 2PVSMENU3_FINISHROOMSETUP
			// Player 2 Character
			DrawSpriteScreenXY(object[+1].selectedCharacter, temp0, 56)
		else
			// Waiting For...
			if object.animationTimer < 8
				DrawSpriteScreenXY(19, temp0, 56)
			end if
		end if
		// 2P
		DrawSpriteScreenXY(6, temp0, 56)
	end if

	// MATCH x of y
	temp0 = screen.xcenter
	temp0 -= 24
	temp2 = vs.match
	temp2++
	DrawNumbers(9, temp0, 69, temp2, 6, 8, 0)

	temp0 += 40
	DrawNumbers(9, temp0, 69, vs.gameLength, 6, 8, 0)

	object.spriteSheet = object.textSheet
	DrawMenu(MENU_1, screen.xcenter, 52)
end event


event ObjectStartup
	LoadSpriteSheet("LevelSelect/Icons.gif")
	LoadSpriteSheet("LevelSelect/Text.gif")
	SpriteFrame(-96, 0, 80, 14, 83, 335)	// STAGE text 		- #0
	SpriteFrame(0, 0, 96, 14, 1, 264)		// SELECT text 		- #1
	SpriteFrame(-12, -20, 24, 40, 1, 279) 	// Sonic 			- #2
	SpriteFrame(-16, -20, 32, 40, 54, 279)	// Tails 			- #3
	SpriteFrame(-14, -20, 27, 40, 26, 279)	// Knuckles 		- #4
	SpriteFrame(-8, 24, 16, 7, 218, 124)	// 1P 				- #5
	SpriteFrame(-8, 24, 16, 7, 218, 132)	// 2P 				- #6
	SpriteFrame(-8, 24, 16, 7, 98, 136)		// 1P (Selected) 	- #7
	SpriteFrame(-8, 24, 16, 7, 115, 136)	// 2P (Selected) 	- #8
	SpriteFrame(0, 0, 8, 7, 175, 140)		// 0 				- #9
	SpriteFrame(0, 0, 8, 7, 183, 140)		// 1 				- #10
	SpriteFrame(0, 0, 8, 7, 191, 140)		// 2 				- #11
	SpriteFrame(0, 0, 8, 7, 199, 140)		// 3 				- #12
	SpriteFrame(0, 0, 8, 7, 207, 140)		// 4 				- #13
	SpriteFrame(0, 0, 8, 7, 215, 140)		// 5 				- #14
	SpriteFrame(0, 0, 8, 7, 223, 140)		// 6 				- #15
	SpriteFrame(0, 0, 8, 7, 231, 140)		// 7 				- #16
	SpriteFrame(0, 0, 8, 7, 239, 140)		// 8 				- #17
	SpriteFrame(0, 0, 8, 7, 247, 140)		// 9 				- #18
	SpriteFrame(-28, -3, 56, 15, 44, 362)	// WAITING FOR text - #19

	foreach (TypeName[2P VS Menu 3], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].priority = PRIORITY_ACTIVE
	next
end event


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

event RSDKDraw
	DrawSprite(0)
	DrawSprite(1)
end event


event RSDKLoad
	LoadSpriteSheet("LevelSelect/Icons.gif")
	SpriteFrame(-96, 0, 80, 14, 83, 335)	// STAGE text 		- #0
	SpriteFrame(0, 0, 96, 14, 1, 264)		// SELECT text 		- #1
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
