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

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

private alias object.value0 : object.unused
private alias object.value1 : object.activeOrbs
private alias object.value2 : object.startPos.x
private alias object.value3 : object.fireOrbs
private alias object.value4 : object.targetPlayer
private alias object.value5 : object.backupTargetPlayer // if we cant target any players, target the last player detected (or P1 if none)
private alias object.value6 : object.targetPlayerDistance

private alias 0 : ORBINAUT_NOTARGET
private alias 1 : ORBINAUT_FIRINGORBS
private alias 2 : ORBINAUT_NOORBS
private alias 3 : ORBINAUT_ORB

// Player Aliases
private alias object.xpos : player.xpos
private alias object.ypos : player.ypos

private alias object.value40 : player.hitboxLeft
private alias object.value38 : player.hitboxTop
private alias object.value41 : player.hitboxRight
private alias object.value39 : player.hitboxBottom

private alias object.value17 : debugMode.currentSelection


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

reserve function Orbinaut_DebugDraw
reserve function Orbinaut_DebugSpawn
reserve function Orbinaut_HandleCollisions
reserve function Orbinaut_HandleCollisions_Orb
reserve function Orbinaut_CheckOffScreen

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

private value Orbinaut_startDebugID = 0


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

private function Orbinaut_DebugDraw
	temp0 = debugMode.currentSelection
	temp0 -= Orbinaut_startDebugID
	temp0--
	temp0 ^= 1
	temp0 <<= 1
	DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
end function


private function Orbinaut_DebugSpawn
	temp0 = debugMode.currentSelection
	temp0 -= Orbinaut_startDebugID
	CreateTempObject(TypeName[Orbinaut], temp0, object.xpos, object.ypos)
	object[tempObjectPos].startPos.x = object[tempObjectPos].xpos
	object[tempObjectPos].activeOrbs = 15

	if object[tempObjectPos].propertyValue > 1
		object[tempObjectPos].fireOrbs = false
		object[tempObjectPos].propertyValue &= 1
		GetBit(temp0, object.direction, 0)
		if temp0 == FLIP_NONE
			object[tempObjectPos].direction = FLIP_NONE
			object[tempObjectPos].xvel = -0x4000
		else
			object[tempObjectPos].direction = FLIP_X
			object[tempObjectPos].xvel = 0x4000
		end if
	else
		object[tempObjectPos].fireOrbs = true
		object[tempObjectPos].xvel = 0
		GetBit(temp0, object.direction, 0)
		if temp0 == FLIP_NONE
			object[tempObjectPos].direction = FLIP_NONE
		else
			object[tempObjectPos].direction = FLIP_X
		end if
	end if
end function


private function Orbinaut_HandleCollisions
	temp0 = 0
	temp7 = object.angle
	temp1 = object.xpos
	temp2 = object.ypos
	while temp0 < 4
		GetBit(temp6, object.activeOrbs, temp0)
		if temp6 == true
			Cos256(object.xpos, temp7)
			object.xpos <<= 12
			object.xpos += temp1

			Sin256(object.ypos, temp7)
			object.ypos <<= 12
			object.ypos += temp2

			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				BoxCollisionTest(C_TOUCH, object.entityPos, -4, -4, 4, 4, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
				if checkResult == true
					CallFunction(Player_Hit)
				end if
			next
		end if
		temp7 += 0x40
		temp7 &= 0xFF
		temp0++
	loop

	object.xpos = temp1
	object.ypos = temp2
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -8, -8, 8, 8, currentPlayer, player[currentPlayer].hitboxLeft, player[currentPlayer].hitboxTop, player[currentPlayer].hitboxRight, player[currentPlayer].hitboxBottom)
		if checkResult == true
			CallFunction(Player_BadnikBreak)
		end if
	next
end function


private function Orbinaut_HandleCollisions_Orb
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -4, -4, 4, 4, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		if checkResult == true
			CallFunction(Player_Hit)
		end if
	next
end function


private function Orbinaut_CheckOffScreen
	if object.outOfBounds == true
		temp0 = object.xpos
		object.xpos = object.startPos.x
		if object.outOfBounds == true
			object.xpos = object.startPos.x
			object.activeOrbs = 15
			if object.fireOrbs == false
				if object.propertyValue == 0
					object.direction = FLIP_NONE
					object.xvel = -0x4000
				else
					object.direction = FLIP_X
					object.xvel = 0x4000
				end if
			else
				if object.propertyValue == 0
					object.direction = FLIP_NONE
				else
					object.direction = FLIP_X
				end if
				object.xvel = 0
			end if

			object.unused = 0
			object.animationTimer = 0
			object.priority = PRIORITY_BOUNDS
			object.state = ORBINAUT_NOTARGET
			object.angle = 0
		else
			object.xpos = temp0
		end if
	end if
end function


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

event ObjectUpdate
	switch object.state
	case ORBINAUT_NOTARGET
		object.priority = PRIORITY_ACTIVE
		object.xpos += object.xvel
		if object.direction == FLIP_NONE
			object.angle++
		else
			object.angle--
		end if
		object.angle &= 255
		CallFunction(Orbinaut_HandleCollisions)

		if object.fireOrbs == true
			object.targetPlayerDistance = 0x7FFFFFFF
			object.targetPlayer = 0xFFFF
			object.backupTargetPlayer = 0
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				temp0 = player[currentPlayer].ypos
				temp0 -= object.ypos
				Abs(temp0)
				if temp0 < object.targetPlayerDistance
					object.targetPlayerDistance = temp0
					object.backupTargetPlayer = currentPlayer
				end if

				if temp0 < 0x400000
					if object.targetPlayer != 0xFFFF
						arrayPos0 = object.targetPlayer
						temp0 = player[currentPlayer].xpos
						temp0 -= object.xpos
						Abs(temp0)
						temp1 = object[arrayPos0].xpos
						temp1 -= object.xpos
						Abs(temp1)
						if temp0 < temp1
							object.targetPlayer = currentPlayer
						end if
					else
						object.targetPlayer = currentPlayer
					end if
				end if
			next

			if object.targetPlayer != 0xFFFF
				arrayPos0 = object.targetPlayer
				temp0 = object.xpos
				temp0 -= object[arrayPos0].xpos
				Abs(temp0)
				if temp0 <= 0x800000
					object.state = ORBINAUT_FIRINGORBS
					object.frame = 1
				end if
			else
				arrayPos0 = object.backupTargetPlayer
			end if

			if object[arrayPos0].xpos < object.xpos
				object.direction = FLIP_NONE
			else
				object.direction = FLIP_X
			end if
		end if
		CallFunction(Orbinaut_CheckOffScreen)
		break

	case ORBINAUT_FIRINGORBS
		if object.direction == FLIP_NONE
			object.angle++
		else
			object.angle--
		end if
		object.angle &= 0xFF

		temp0 = 0
		temp7 = object.angle
		while temp0 < 4
			if temp7 == 64
				GetBit(temp6, object.activeOrbs, temp0)
				if temp6 == true
					Cos256(temp1, temp7)
					temp1 <<= 12
					temp1 += object.xpos
					Sin256(temp2, temp7)
					temp2 <<= 12
					temp2 += object.ypos
					SetBit(object.activeOrbs, temp0, false)
					CreateTempObject(TypeName[Orbinaut], 0, temp1, temp2)
					object[tempObjectPos].state = ORBINAUT_ORB
					if object.direction == FLIP_NONE
						object[tempObjectPos].xvel = -0x20000
					else
						object[tempObjectPos].xvel = 0x20000
					end if
				end if
			end if
			temp7 += 0x40
			temp7 &= 0xFF
			temp0++
		loop

		CallFunction(Orbinaut_HandleCollisions)

		if object.activeOrbs == 0
			object.state = ORBINAUT_NOORBS
			if object.direction == FLIP_NONE
				object.xvel = -0x4000
			else
				object.xvel = 0x4000
			end if
		end if

		if object.animationTimer < 16
			object.animationTimer++
		else
			object.frame = 2
		end if

		CallFunction(Orbinaut_CheckOffScreen)
		break

	case ORBINAUT_NOORBS
		object.xpos += object.xvel
		CallFunction(Orbinaut_HandleCollisions)
		CallFunction(Orbinaut_CheckOffScreen)
		break

	case ORBINAUT_ORB
		object.xpos += object.xvel
		CallFunction(Orbinaut_HandleCollisions_Orb)
		if object.outOfBounds == true
			object.type = TypeName[Blank Object]
		end if
		break
		
	end switch
end event


event ObjectDraw
	switch object.state
	case ORBINAUT_NOTARGET
	case ORBINAUT_FIRINGORBS
		temp0 = 0
		temp7 = object.angle
		while temp0 < 4
			GetBit(temp6, object.activeOrbs, temp0)
			if temp6 == true
				Cos256(temp1, temp7)
				temp1 <<= 12
				temp1 += object.xpos
				Sin256(temp2, temp7)
				temp2 <<= 12
				temp2 += object.ypos
				DrawSpriteXY(3, temp1, temp2)
			end if
			temp7 += 64
			temp7 &= 255
			temp0++
		loop
		DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
		break

	case ORBINAUT_NOORBS
		DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
		break

	case ORBINAUT_ORB
		DrawSprite(3)
		break
		
	end switch
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone04")
	if checkResult == true
		LoadSpriteSheet("LZ/Objects.gif")
		SpriteFrame(-10, -10, 20, 20, 50, 105)
		SpriteFrame(-10, -10, 20, 20, 71, 105)
		SpriteFrame(-10, -10, 20, 20, 50, 154)
		SpriteFrame(-8, -8, 16, 16, 107, 1)
	end if

	CheckCurrentStageFolder("Zone05")
	if checkResult == true
		LoadSpriteSheet("SLZ/Objects.gif")
		SpriteFrame(-10, -10, 20, 20, 51, 1)
		SpriteFrame(-10, -10, 20, 20, 72, 1)
		SpriteFrame(-10, -10, 20, 20, 93, 1)
		SpriteFrame(-8, -8, 16, 16, 114, 1)
	end if

	CheckCurrentStageFolder("Zone06")
	if checkResult == true
		LoadSpriteSheet("SBZ/Objects.gif")
		SpriteFrame(-10, -10, 20, 20, 1, 138)
		SpriteFrame(-10, -10, 20, 20, 22, 138)
		SpriteFrame(-10, -10, 20, 20, 43, 138)
		SpriteFrame(-8, -8, 16, 16, 64, 142)
	end if

	CheckCurrentStageFolder("Zone07")
	if checkResult == true
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-10, -10, 20, 20, 119, 114)
		SpriteFrame(-10, -10, 20, 20, 140, 114)
		SpriteFrame(-10, -10, 20, 20, 119, 135)
		SpriteFrame(-8, -8, 16, 16, 140, 135)
	end if

	foreach (TypeName[Orbinaut], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].startPos.x = object[arrayPos0].xpos
		object[arrayPos0].activeOrbs = 15
		if object[arrayPos0].propertyValue > 1
			object[arrayPos0].fireOrbs = false
			object[arrayPos0].propertyValue &= 1
			if object[arrayPos0].propertyValue == 0
				object[arrayPos0].direction = FLIP_NONE
				object[arrayPos0].xvel = -0x4000
			else
				object[arrayPos0].direction = FLIP_X
				object[arrayPos0].xvel = 0x4000
			end if
		else
			object[arrayPos0].fireOrbs = true
			object[arrayPos0].xvel = 0
			if object[arrayPos0].propertyValue == 0
				object[arrayPos0].direction = FLIP_NONE
			else
				object[arrayPos0].direction = FLIP_X
			end if
		end if
	next

	temp0 = 0
	Orbinaut_startDebugID = DebugMode_ObjCount
	Orbinaut_startDebugID--
	while temp0 < 2
		SetTableValue(TypeName[Orbinaut], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(Orbinaut_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(Orbinaut_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop
end event


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

event RSDKEdit
	if editor.returnVariable == true
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			checkResult = object.propertyValue
			break
			
		case 0 // direction
			GetBit(checkResult, object.propertyValue, 0)
			break
			
		case 1 // fireOrbs
			GetBit(checkResult, object.propertyValue, 1)
			checkResult ^= true
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // direction
			CheckNotEqual(editor.variableValue, false)
			SetBit(object.propertyValue, 0, checkResult)
			break
			
		case 1 // fireOrbs
			CheckNotEqual(editor.variableValue, false)
			checkResult ^= true
			SetBit(object.propertyValue, 1, checkResult)
			break
			
		end switch
	end if
end event


event RSDKDraw
	GetBit(object.direction, object.propertyValue, 0)
	
	// Draw Orbs
	temp0 = 0
	temp7 = object.angle
	while temp0 < 4
		Cos256(temp1, temp7)
		temp1 <<= 12
		temp1 += object.xpos

		Sin256(temp2, temp7)
		temp2 <<= 12
		temp2 += object.ypos
		
		DrawSpriteXY(3, temp1, temp2)

		temp7 += 64
		temp7 &= 0xFF
		temp0++
	loop
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
end event


event RSDKLoad
	// Make sure we have a fall-back, in case the current folder isn't actually a valid one
	temp0 = false
	
	CheckCurrentStageFolder("Zone05")
	temp0 |= checkResult
	if checkResult == true
		LoadSpriteSheet("SLZ/Objects.gif")
		SpriteFrame(-10, -10, 20, 20, 51, 1)
		SpriteFrame(-10, -10, 20, 20, 72, 1)
		SpriteFrame(-10, -10, 20, 20, 93, 1)
		SpriteFrame(-8, -8, 16, 16, 114, 1)
	end if

	CheckCurrentStageFolder("Zone06")
	temp0 |= checkResult
	if checkResult == true
		LoadSpriteSheet("SBZ/Objects.gif")
		SpriteFrame(-10, -10, 20, 20, 1, 138)
		SpriteFrame(-10, -10, 20, 20, 22, 138)
		SpriteFrame(-10, -10, 20, 20, 43, 138)
		SpriteFrame(-8, -8, 16, 16, 64, 142)
	end if

	CheckCurrentStageFolder("Zone07")
	temp0 |= checkResult
	if checkResult == true
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-10, -10, 20, 20, 119, 114)
		SpriteFrame(-10, -10, 20, 20, 140, 114)
		SpriteFrame(-10, -10, 20, 20, 119, 135)
		SpriteFrame(-8, -8, 16, 16, 140, 135)
	end if
	
	if temp0 == false
		// We use LZ's sprites as the fallback, "Zone04" (LZ's folder) will come here too
		LoadSpriteSheet("LZ/Objects.gif")
		SpriteFrame(-10, -10, 20, 20, 50, 105)
		SpriteFrame(-10, -10, 20, 20, 71, 105)
		SpriteFrame(-10, -10, 20, 20, 50, 154)
		SpriteFrame(-8, -8, 16, 16, 107, 1)
	end if
	
	AddEditorVariable("direction")
	SetActiveVariable("direction")
	AddEnumVariable("Left", 0)
	AddEnumVariable("Right", 1)

	// the actual value is "cantFireOrbs" but fireOrbs is cleaner
	AddEditorVariable("fireOrbs")
	SetActiveVariable("fireOrbs")
	AddEnumVariable("false", false)
	AddEnumVariable("true", true)
end event
