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

// This script is mostly just a copy of the standard [SCZ Setup] object, just with a few changes for Mission Mode
// Changes are marked throughout the script

// Everything here is still labelled under `SCZSetup` since only the script path is different - the object name is still the same (beyond removing the space for some reason)

// ========================
// 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
private alias  5 : SCZSETUP_RESULTS

// 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

// 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
// ========================

private 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
			// The first of the SCZMSetup changes, additions were made here to lock player control
			// Normally, it's just `object.state++` inside here, but we skip WFZ in Missions
			
			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
			
			// Let's skip the Wing Fortress passby states, just end the level here
			object.state = SCZSETUP_TOWINGFORTRESS
		end if
		break

	case SCZSETUP_WINGFORTRESS
		// This whole state is unused in this Mission script, it's skipped by the above
		
		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
		// This whole state is unused here as well
		
		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
		
		// Another SCZMSetup change, the following two lines are just here to make the player go off screen a bit faster
		player[0].right = true
		player[0].xpos += player[0].xvel
		
		SCZSetup_scrollVelocity.x = 0
		temp0 = stage.curXBoundary2
		temp0 -= 32
		temp0 <<= 16
		if player[0].xpos > temp0
			object.state++
		end if
		break

	case SCZSETUP_FADEOUT // fade to WFZ
		
		// One more SCZMSetup change, they added a line here to reset player xvel
		// yvel reset is already part of the normal script, it's just this xvel part that's new
		player[0].xvel = 0
		
		player[0].yvel = 0
		object.timer += 4
		music.volume -= 2
		
		// (Right here there would be a SetScreenFade to fade the screen to black, but SCZMSetup removed it)
		
		if object.timer == 320
			fadeColor = 0x000000
			object.timer = 0
			starPostID = 0
			object.direction = FACING_RIGHT // this object's direction doesn't even matter, i think?
			
			// Almost at the end of the SCZMSetup changes, the whole standard level end routine here was replaced
			//  with the following three lines, making the Origins mission complete HUD pop up instead
			CallNativeFunction2(NotifyCallback, NOTIFY_TOUCH_SIGNPOST, 0)
			object.state++
			PlayMusic(1)
		end if
		break

	case SCZSETUP_RESULTS
		// The final SCZMSetup change, this state is new in this script
		// There's not much to it, just making sure the player doesn't go flying off or anything
		player[0].gravity = GRAVITY_GROUND
		player[0].xpos = 0
		player[0].xvel = 0
		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

	// The Missions never fly by WFZ, but this animation code is still here anyways cuz why not
	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[SCZSetup]
	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
	
	// These values are essentially unused, the WFZ flyby never happens in Missions
	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
