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

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

private alias object.value0 : object.unused // Set to 0, but never used again...
private alias object.value1 : object.activeFireballs // Bitfield, bits 1-4 are either true or false for the state of each of the Sol's 4 fireballs
private alias object.value2 : object.startPos.x
private alias object.value3 : object.fireOrbs // true if the Sol should attack the player (always true)
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 object.value7 : object.fireballFrame
private alias object.value8 : object.fireballFrameTimer

private alias 0 : SOL_AWAITPLAYER
private alias 1 : SOL_FIREFIREBALLS
private alias 2 : SOL_NOFIREBALLS
private alias 3 : SOL_FIREBALL

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


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

reserve function Sol_DebugDraw
reserve function Sol_DebugSpawn
reserve function Sol_CheckColFull
reserve function Sol_CheckColFireball
reserve function Sol_CheckOffScreen


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

private function Sol_DebugDraw
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
end function


private function Sol_DebugSpawn
	CreateTempObject(TypeName[Sol], 0, object.xpos, object.ypos)
	object[tempObjectPos].startPos.x = object[tempObjectPos].xpos
	object[tempObjectPos].activeFireballs = 15
	object[tempObjectPos].fireOrbs = true
	if object[tempObjectPos].propertyValue == 0
		object[tempObjectPos].direction = FLIP_NONE
		object[tempObjectPos].xvel = -0x4000
	else
		object[tempObjectPos].direction = FLIP_X
		object[tempObjectPos].xvel = 0x4000
	end if
end function


private function Sol_CheckColFull
	// Called to check for collision of full entity (namely main Sol along with its fireballs)
	
	// First check collision with the fireballs
	temp0 = 0
	temp7 = object.angle
	temp1 = object.xpos
	temp2 = object.ypos
	while temp0 < 4
		GetBit(temp6, object.activeFireballs, 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_FireHit)
				end if
			next
		end if
		temp7 += 0x40
		temp7 &= 0xFF
		temp0++
	loop

	// Then check collision with the Sol itself
	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 Sol_CheckColFireball
	// Called to check collision ONLY with the fireball, this is called from the child fireball object, separate from the actual parent object Sol
	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_FireHit)
		end if
	next
end function


private function Sol_CheckOffScreen
	if object.outOfBounds == true
		temp0 = object.xpos
		object.xpos = object.startPos.x
		if object.outOfBounds == true
			// Reset the Sol
			
			object.xpos 			= object.startPos.x
			object.activeFireballs 	= 15
			if object.propertyValue == 0
				object.direction 	= FLIP_NONE
				object.xvel 		= -0x4000
			else
				object.direction 	= FLIP_X
				object.xvel 		= 0x4000
			end if
			object.unused 			= 0
			object.animationTimer 	= 0
			object.priority 		= PRIORITY_BOUNDS
			object.state 			= SOL_AWAITPLAYER
			object.angle 			= 0
		else
			object.xpos = temp0
		end if
	end if
end function


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

event ObjectUpdate
	switch object.state
	case SOL_AWAITPLAYER
		object.priority = PRIORITY_ACTIVE
		object.xpos += object.xvel

		if object.direction == FLIP_NONE
			object.angle++
		else
			object.angle--
		end if
		object.angle &= 0xFF
		CallFunction(Sol_CheckColFull)

		if object.fireOrbs == true
			// (Attempt to) find a target player in order to throw fireballs at them
			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 = SOL_FIREFIREBALLS
					
					// Frame 1 is just a dupe of frame 0, this doesn't matter much
					object.frame = 1
				end if
			else
				arrayPos0 = object.backupTargetPlayer
			end if

			if object[arrayPos0].xpos < object.xpos
				object.direction = FLIP_NONE
				object.xvel = -0x4000
			else
				object.direction = FLIP_X
				object.xvel = 0x4000
			end if
		end if
		CallFunction(Sol_CheckOffScreen)
		break

	case SOL_FIREFIREBALLS
		object.xpos += object.xvel
		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.activeFireballs, temp0)
				if temp6 == true
					Cos256(temp1, temp7)
					temp1 <<= 12
					temp1 += object.xpos
					Sin256(temp2, temp7)
					temp2 <<= 12
					temp2 += object.ypos
					SetBit(object.activeFireballs, temp0, false)
					CreateTempObject(TypeName[Sol], 0, temp1, temp2)
					object[tempObjectPos].state = SOL_FIREBALL
					object[tempObjectPos].fireballFrameTimer = object.fireballFrameTimer
					object[tempObjectPos].fireballFrame 	 = object.fireballFrame
					if object.direction == FLIP_NONE
						object[tempObjectPos].xvel = -0x20000
					else
						object[tempObjectPos].xvel = 0x20000
					end if
				end if
			end if
			temp7 += 64
			temp7 &= 255
			temp0++
		loop

		CallFunction(Sol_CheckColFull)
		if object.activeFireballs == false
			object.state = SOL_NOFIREBALLS
			if object.direction == FLIP_NONE
				object.xvel = -0x4000
			else
				object.xvel = 0x4000
			end if
		end if

		if object.animationTimer < 16
			object.animationTimer++
		else
			// Frame 2 is just a dupe of frame 0 (just like frame 1) so this doesn't matter much
			object.frame = 2
		end if
		CallFunction(Sol_CheckOffScreen)
		break

	case SOL_NOFIREBALLS
		object.xpos += object.xvel
		CallFunction(Sol_CheckColFull)
		CallFunction(Sol_CheckOffScreen)
		break

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

	object.fireballFrameTimer++
	if object.fireballFrameTimer >= 12
		object.fireballFrameTimer = 0
	end if

	object.fireballFrame = object.fireballFrameTimer
	object.fireballFrame /= 6
	object.fireballFrame += 3
end event


event ObjectDraw
	switch object.state
	case SOL_AWAITPLAYER
	case SOL_FIREFIREBALLS
		temp0 = 0
		temp7 = object.angle
		while temp0 < 4
			GetBit(temp6, object.activeFireballs, temp0)
			if temp6 == true
				Cos256(temp1, temp7)
				temp1 <<= 12
				temp1 += object.xpos
				Sin256(temp2, temp7)
				temp2 <<= 12
				temp2 += object.ypos
				DrawSpriteXY(object.fireballFrame, temp1, temp2)
			end if
			temp7 += 0x40
			temp7 &= 0xFF
			temp0++
		loop

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

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

	case SOL_FIREBALL
		DrawSprite(object.fireballFrame)
		break
		
	end switch
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone05")
	if checkResult == true
		LoadSpriteSheet("HTZ/Objects.gif")
		
		// Normal Frame
		SpriteFrame(-8, -8, 16, 16, 91, 222)
		
		// Getting Angry Frames (Same as above)
		SpriteFrame(-8, -8, 16, 16, 91, 222)
		SpriteFrame(-8, -8, 16, 16, 91, 222)
		
		// Fireball Frames
		SpriteFrame(-8, -8, 16, 16, 1, 33)
		SpriteFrame(-8, -8, 16, 16, 18, 33)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		
		// This SpriteFrame data is just duplicated from HTZ's sheet, so it appears broken when loaded
		
		// Normal Frame
		SpriteFrame(-8, -8, 16, 16, 91, 222)
		
		// Getting Angry Frames (Same as above)
		SpriteFrame(-8, -8, 16, 16, 91, 222)
		SpriteFrame(-8, -8, 16, 16, 91, 222)
		
		// Fireball Frames
		SpriteFrame(-8, -8, 16, 16, 1, 33)
		SpriteFrame(-8, -8, 16, 16, 18, 33)
	end if

	foreach (TypeName[Sol], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].startPos.x 		= object[arrayPos0].xpos
		object[arrayPos0].activeFireballs 	= 15
		object[arrayPos0].fireOrbs 			= true
		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
	next

	SetTableValue(TypeName[Sol], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(Sol_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(Sol_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
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
			checkResult = object.propertyValue
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // direction
			object.propertyValue = editor.variableValue
			break
			
		end switch
	end if
end event


event RSDKDraw
	GetBit(object.direction, object.propertyValue, 0)
	
	temp0 = 0
	temp3 = 0
	while temp0 < 4
		Cos256(temp1, temp3)
		temp1 <<= 12
		temp1 += object.xpos

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

		DrawSpriteXY(3, temp1, temp2)
		temp3 += 0x40
		temp3 &= 0xFF
		temp0++
	loop
	
	DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone05")
	if checkResult == true
		LoadSpriteSheet("HTZ/Objects.gif")
		SpriteFrame(-8, -8, 16, 16, 91, 222)
		SpriteFrame(-8, -8, 16, 16, 91, 222)
		SpriteFrame(-8, -8, 16, 16, 91, 222)
		SpriteFrame(-8, -8, 16, 16, 1, 33)
		SpriteFrame(-8, -8, 16, 16, 18, 33)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		
		// Still broken stuff here :(
		SpriteFrame(-8, -8, 16, 16, 91, 222)
		SpriteFrame(-8, -8, 16, 16, 91, 222)
		SpriteFrame(-8, -8, 16, 16, 91, 222)
		SpriteFrame(-8, -8, 16, 16, 1, 33)
		SpriteFrame(-8, -8, 16, 16, 18, 33)
	end if

	AddEditorVariable("direction")
	SetActiveVariable("direction")
	AddEnumVariable("Left", 0)
	AddEnumVariable("Right", 1)
end event
