// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: SCZ Setup 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.palRotateTimer
private alias object.value2 : object.palRotateIndex

private alias -1 : SCZSETUP_STATIC
private alias  0 : SCZSETUP_FORWARD
private alias  1 : SCZSETUP_WINGFORTRESS
private alias  2 : SCZSETUP_GIVECHASE
private alias  3 : SCZSETUP_TOWINGFORTRESS
private alias  4 : SCZSETUP_FADEOUT

// Player Aliases
private alias object.xpos : player.xpos
private alias object.xvel : player.xvel
private alias object.yvel : player.yvel
private alias object.speed : player.speed
private alias object.gravity : player.gravity
private alias object.controlMode : player.controlMode
private alias object.tileCollisions : player.tileCollisions
private alias object.collisionRight : player.collisionRight
private alias object.jumpPress : player.jumpPress
private alias object.jumpHold : player.jumpHold
private alias object.up	: player.up
private alias object.down : player.down
private alias object.left : player.left
private alias object.right : player.right

private alias object.value0 : player.rings

// Game Modes
private alias 0 : MODE_NOSAVE
private alias 1 : MODE_SAVEGAME
private alias 2 : MODE_TIMEATTACK

// Tracks
private alias 0 : TRACK_STAGE

// Reserved object slots
private alias 10 : SLOT_ZONESETUP

// Music Loops
private alias 198253 : MUSIC_LOOP_SCZ


// ========================
// Static Values
// ========================

public value SCZSetup_scrollPos.x 		= 0
public value SCZSetup_scrollPos.y 		= 0
public value SCZSetup_scrollVelocity.x 	= 0
public value SCZSetup_scrollVelocity.y 	= 0
public value SCZSetup_hPropellerFrame 	= 0
public value SCZSetup_hPropellerTimer 	= 0
public value SCZSetup_vPropellerFrame 	= 0
public value SCZSetup_vPropellerTimer 	= 0


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

public table SCZSetup_palRotationTable
	0xE0E040, 0xE0A020, 0xE06000, 0xC02000
	0xE0E0E0, 0xE0E040, 0xE0A020, 0xE06000
	0xE0E040, 0xE0A020, 0xE06000, 0xC02000
	0xE0A020, 0xE06000, 0xC02000, 0x802000
end table


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

event ObjectUpdate
	object.palRotateTimer++
	if object.palRotateTimer == 2
		object.palRotateTimer = 0

		GetTableValue(temp0, object.palRotateIndex, SCZSetup_palRotationTable)
		SetPaletteEntry(0, 167, temp0)

		object.palRotateIndex++
		object.palRotateIndex &= 15
		GetTableValue(temp0, object.palRotateIndex, SCZSetup_palRotationTable)
		SetPaletteEntry(0, 168, temp0)

		object.palRotateIndex++
		object.palRotateIndex &= 15
		GetTableValue(temp0, object.palRotateIndex, SCZSetup_palRotationTable)
		SetPaletteEntry(0, 169, temp0)

		object.palRotateIndex++
		object.palRotateIndex &= 15
		GetTableValue(temp0, object.palRotateIndex, SCZSetup_palRotationTable)
		SetPaletteEntry(0, 170, temp0)

		object.palRotateIndex++
		object.palRotateIndex &= 15
	end if

	// Always keep the camera moving
	camera[0].style = CAMERASTYLE_FOLLOW

	player[0].gravity = GRAVITY_AIR
	player[0].tileCollisions = false

	switch object.state
	case SCZSETUP_FORWARD // initial flight
		SCZSetup_scrollPos.x += SCZSetup_scrollVelocity.x

		temp0 = SCZSetup_scrollPos.x
		temp0 >>= 16
		stage.curXBoundary1 = temp0

		temp0 += screen.xsize
		stage.curXBoundary2 = temp0

		if stage.curXBoundary1 >= 4480
			object.state++
		end if
		break

	case SCZSETUP_WINGFORTRESS // see wing fortress "fly past" (we actually just move down+left LOL)
		SCZSetup_scrollVelocity.x = -0x10000
		SCZSetup_scrollVelocity.y = 0x10000
		SCZSetup_scrollPos.x += SCZSetup_scrollVelocity.x
		SCZSetup_scrollPos.y += SCZSetup_scrollVelocity.y

		temp0 = SCZSetup_scrollPos.x
		temp0 >>= 16
		stage.curXBoundary1 = temp0

		temp0 += screen.xsize
		stage.curXBoundary2 = temp0
		temp0 = SCZSetup_scrollPos.y

		temp0 >>= 16
		stage.curYBoundary1 = temp0

		temp0 += screen.ysize
		stage.curYBoundary2 = temp0

		temp0 <<= 16
		stage.deathBoundary = temp0

		if stage.curYBoundary1 >= 1280
			object.state++
		end if
		break

	case SCZSETUP_GIVECHASE // we saw wing fortress, continue to chase that egghead down!
		SCZSetup_scrollVelocity.x = 0x10000
		SCZSetup_scrollVelocity.y = 0
		SCZSetup_scrollPos.x += SCZSetup_scrollVelocity.x
		temp0 = SCZSetup_scrollPos.x
		temp0 >>= 16
		stage.curXBoundary1 = temp0
		temp0 += screen.xsize
		stage.curXBoundary2 = temp0
		if stage.curXBoundary1 >= 5256
			player[0].controlMode = CONTROLMODE_NONE
			player[0].jumpPress = false
			player[0].jumpHold 	= false
			player[0].left 		= false
			player[0].right 	= true
			player[0].up 		= false
			player[0].down 		= false
			hParallax[0].scrollSpeed = 0
			object.state++
		end if
		break

	case SCZSETUP_TOWINGFORTRESS // player holds right and goes off screen
		SCZSetup_scrollVelocity.x = 0
		temp0 = stage.curXBoundary2
		temp0 -= 32
		temp0 <<= 16
		if player[0].xpos > temp0
			object.state++

#platform: USE_ORIGINS
			stage.timeEnabled = false
			CallNativeFunction2(NotifyCallback, NOTIFY_ACT_FINISH, 0)
			if game.oneStageFlag != false
				game.callbackResult = -1
				CallNativeFunction4(NotifyCallback, NOTIFY_STAGE_RETRY, 1, stage.listPos, 0)
				object.state = SCZSETUP_STATIC
			end if
#endplatform
		end if
		break

	case SCZSETUP_FADEOUT // fade to WFZ
		player[0].yvel = 0
		object.timer += 4
		music.volume -= 2
		SetScreenFade(0, 0, 0, object.timer)
		if object.timer == 320
			fadeColor = 0x000000
			object.timer = 0
			starPostID = 0
			
			// This object's direction is never used, is it?
			// I don't think this does anything...
			object.direction = FACING_RIGHT
			
			stage.listPos++

			if options.gameMode == MODE_SAVEGAME
				arrayPos1 = options.saveSlot
				arrayPos1 <<= 3
				if stage.player2Enabled == true
					saveRAM[arrayPos1] = 3
				else
					saveRAM[arrayPos1] = stage.playerListPos
				end if
				arrayPos1++
				saveRAM[arrayPos1] = player.lives
				arrayPos1++
				saveRAM[arrayPos1] = player.score
				arrayPos1++
				saveRAM[arrayPos1] = player.scoreBonus
				arrayPos1++
				
				// Bug Details:
				// This is missing the check for if your save file is already past the next act
				// This means the save file's act will be overwritten to Wing Fortress Zone, even if you're on a Complete save file...
				saveRAM[arrayPos1] = stage.listPos
				saveRAM[arrayPos1]++
				
				arrayPos1++
				saveRAM[arrayPos1] = specialStage.emeralds
				arrayPos1++
				saveRAM[arrayPos1] = specialStage.listPos
				WriteSaveRAM()
			end if

			// ???
			// you cannot speedrun sky chase.
			if options.gameMode == MODE_TIMEATTACK
				timeAttack.result = stage.seconds
				timeAttack.result *= 100
				temp0 = stage.minutes
				temp0 *= 6000
				timeAttack.result += temp0
				timeAttack.result += stage.milliSeconds
				stage.listPos--
				CallNativeFunction2(SetLeaderboard, stage.listPos, timeAttack.result)
				engine.state = 8
			else
				temp0 = engine.trialMode
				if stage.listPos >= stage.listSize
					temp0 = true
				end if

				if temp0 == false
#platform: USE_ORIGINS
					CallNativeFunction4(NotifyCallback, NOTIFY_STATS_ENEMY, StageStatsUsabilityParam1, StageStatsUsabilityParam2, StageStatsUsabilityParam3)
					CallNativeFunction2(NotifyCallback, NOTIFY_STATS_RING, player[0].rings)
					temp0 = StageStatsUsabilityParam5
					temp0 &= 1
					temp1 = StageStatsUsabilityParam5
					temp1 &= 2
					temp2 = StageStatsUsabilityParam5
					temp2 &= 4
					CallNativeFunction4(NotifyCallback, NOTIFY_STATS_CHARA_ACTION, temp0, temp1, temp2)
					CallNativeFunction2(NotifyCallback, NOTIFY_ACT_FINISH, 0)
					StageStatsUsabilityParam1 = 0
					StageStatsUsabilityParam2 = 0
					StageStatsUsabilityParam3 = 0
					StageStatsUsabilityParam4 = 0
					StageStatsUsabilityParam5 = 0
					StatsUsabilityParam1 = 0
					StatsUsabilityParam2 = 0
					StatsUsabilityParam3 = 0
					StatsUsabilityParam4 = 0
					StatsUsabilityParam5 = 0
#endplatform

					LoadStage()
				else
					stage.activeList = PRESENTATION_STAGE
					stage.listPos = 0
					LoadStage()
				end if
			end if
		end if
		break

	end switch

	// R Boundary
	if object.state < SCZSETUP_TOWINGFORTRESS
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			temp0 = player[currentPlayer].collisionRight
			temp0 <<= 16
			temp0 += player[currentPlayer].xpos
			temp1 = stage.curXBoundary2
			temp1 <<= 16
			if temp0 > temp1
				player[currentPlayer].xvel = 0
				player[currentPlayer].speed = 0
				player[currentPlayer].xpos = temp1
				temp0 = player[currentPlayer].collisionRight
				temp0 <<= 16
				player[currentPlayer].xpos -= temp0
			end if
		next
	end if

	SCZSetup_hPropellerFrame = SCZSetup_hPropellerTimer
	SCZSetup_hPropellerFrame >>= 1
	SCZSetup_hPropellerTimer++
	SCZSetup_hPropellerTimer %= 12

	SCZSetup_vPropellerFrame = SCZSetup_vPropellerTimer
	SCZSetup_vPropellerFrame >>= 1
	SCZSetup_vPropellerTimer++
	SCZSetup_vPropellerTimer %= 6
end event


event ObjectStartup
	SetMusicTrack("SkyChase.ogg", TRACK_STAGE, MUSIC_LOOP_SCZ)

	// Set the "inhabitants" of this zone
	// I sure hope they can fly...
	animalType1 = TypeName[Cucky]
	animalType2 = TypeName[Tocky]

	object[SLOT_ZONESETUP].type = TypeName[SCZ Setup]
	object[SLOT_ZONESETUP].priority = PRIORITY_ACTIVE

	SCZSetup_scrollPos.x = screen.xcenter
	SCZSetup_scrollPos.x *= -0x10000
	SCZSetup_scrollPos.x += player[0].xpos

	SCZSetup_scrollPos.y = 0

	SCZSetup_scrollVelocity.x = 0x10000
	SCZSetup_scrollVelocity.y = 0
	
	SCZSetup_hPropellerFrame = 0
	SCZSetup_hPropellerTimer = 0
	SCZSetup_vPropellerFrame = 0
	SCZSetup_vPropellerTimer = 0

	stage.curYBoundary2 = 240
	stage.deathBoundary = 0xF00000
	hParallax[0].scrollSpeed = 0x8000
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
