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

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

private alias object.value0 : object.startPos.x
private alias object.value1 : object.startPos.y
private alias object.value2 : object.backupPlayer
private alias object.value3 : object.targetPlayer
private alias object.value4 : object.targetDistance
private alias object.value5 : object.dropPos.y

// States
private alias 0 : BATBRAIN_AWAITPLAYER
private alias 1 : BATBRAIN_DROPPING
private alias 2 : BATBRAIN_FLYING
private alias 3 : BATBRAIN_RETURNTOROOF

// Animations
private alias 0 : BATBRAIN_ANI_IDLE
private alias 1 : BATBRAIN_ANI_DROPPING
private alias 2 : BATBRAIN_ANI_FLYING

// Player aliases
private alias object.xpos : player.xpos
private alias object.ypos : player.ypos
private alias object.value38 : player.hitboxTop
private alias object.value39 : player.hitboxBottom
private alias object.value40 : player.hitboxLeft
private alias object.value41 : player.hitboxRight

// Path ID Aliases
private alias 0 : PATH_A


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

reserve function Batbrain_DebugDraw
reserve function Batbrain_DebugSpawn
reserve function Batbrain_CheckOutOfRange


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

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


private function Batbrain_DebugSpawn
	CreateTempObject(TypeName[Batbrain], 0, object.xpos, object.ypos)
	object[tempObjectPos].startPos.x = object.xpos
	object[tempObjectPos].startPos.y = object.ypos
end function


private function Batbrain_CheckOutOfRange
	temp1 = object.ixpos
	temp1 -= screen.xoffset
	CheckLower(temp1, 0)
	temp0 = checkResult
	
	CheckGreater(temp1, screen.xsize)
	temp0 |= checkResult
	
	temp1 = object.iypos
	temp1 -= screen.yoffset
	CheckLower(temp1, 0)
	temp0 |= checkResult
	
	CheckGreater(temp1, screen.ysize)
	checkResult |= temp0
end function


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


event ObjectUpdate
	switch object.state
	case BATBRAIN_AWAITPLAYER
		object.priority = PRIORITY_ACTIVE
		object.targetDistance = 0x7FFFFFFF
		object.backupPlayer = 0
		object.targetPlayer = 0xFFFF
		
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			temp0 = player[currentPlayer].xpos
			temp0 -= object.xpos
			Abs(temp0)
			if temp0 < object.targetDistance
				object.targetDistance = temp0
				object.backupPlayer = currentPlayer
			end if
			
			if temp0 < 0x800000
				if player[currentPlayer].ypos >= object.ypos
					if object.targetPlayer != 0xFFFF
						arrayPos0 = object.targetPlayer
						if player[currentPlayer].ypos < object[arrayPos0].ypos
							object.targetPlayer = currentPlayer
						end if
					else
						object.targetPlayer = currentPlayer
					end if
				end if
			end if
		next
		
		if object.targetPlayer != 0xFFFF
			arrayPos0 = object.targetPlayer
			temp0 = object[arrayPos0].ypos
			temp0 -= object.ypos
			if temp0 >= 0
				if temp0 <= 0x800000
					Rand(temp0, 8)
					if temp0 == 0
						object.state = BATBRAIN_DROPPING
						object.dropPos.y = object[arrayPos0].ypos
						object.animation = BATBRAIN_ANI_DROPPING
					end if
				end if
			end if
		else
			arrayPos0 = object.backupPlayer
		end if
		
		if object[arrayPos0].xpos < object.xpos
			object.direction = FLIP_NONE
		else
			object.direction = FLIP_X
		end if
		break
		
	case BATBRAIN_DROPPING
		object.ypos += object.yvel
		object.yvel += 0x1800
		arrayPos0 = object.targetPlayer
		if object[arrayPos0].xpos < object.xpos
			object.direction = FLIP_NONE
		else
			object.direction = FLIP_X
		end if
		temp0 = object.dropPos.y
		temp0 -= object.ypos
		if temp0 < 0x100000
			if object.direction == FLIP_NONE
				object.xvel = -0x10000
			else
				object.xvel = 0x10000
			end if
			object.yvel = 0
			object.animation = BATBRAIN_ANI_FLYING
			object.state = BATBRAIN_FLYING
		end if
		break
		
	case BATBRAIN_FLYING
		object.xpos += object.xvel
		arrayPos0 = object.targetPlayer
		temp0 = object[arrayPos0].xpos
		temp0 -= object.xpos
		Abs(temp0)
		if temp0 >= 0x800000
			Rand(temp0, 8)
			if temp0 == 0
				object.state = BATBRAIN_RETURNTOROOF
			end if
		end if
		break
		
	case BATBRAIN_RETURNTOROOF
		object.xpos += object.xvel
		object.ypos += object.yvel
		object.yvel -= 0x1800
		
		ObjectTileCollision(CSIDE_ROOF, 0, -12, PATH_A)
		if checkResult == true
			object.xvel = 0
			object.yvel = 0
			object.state = BATBRAIN_AWAITPLAYER
			object.animation = BATBRAIN_ANI_IDLE
		end if
		break
		
	end switch
	
	if object.outOfBounds == true
		temp0 = object.xpos
		temp1 = object.ypos
		object.xpos = object.startPos.x
		object.ypos = object.startPos.y
		if object.outOfBounds == true
			object.xvel = 0
			object.yvel = 0
			object.direction = FLIP_NONE
			object.animation = BATBRAIN_ANI_IDLE
			object.frame = 0
			object.priority = PRIORITY_BOUNDS
			object.state = BATBRAIN_AWAITPLAYER
		else
			object.xpos = temp0
			object.ypos = temp1
		end if
	end if
	
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -12, -18, 12, 18, currentPlayer, player[currentPlayer].hitboxLeft, player[currentPlayer].hitboxTop, player[currentPlayer].hitboxRight, player[currentPlayer].hitboxBottom)
		
		if checkResult == true
			CallFunction(Player_BadnikBreak)
		end if
	next
	
	switch object.animation
	case BATBRAIN_ANI_IDLE
		object.frame = 0
		break
		
	case BATBRAIN_ANI_DROPPING
		object.frame = 1
		break
		
	case BATBRAIN_ANI_FLYING
		if object.animationTimer == 0
			CallFunction(Batbrain_CheckOutOfRange)
			if checkResult == 0
				PlaySfx(SfxName[Batbrain Fly], false)
			end if
		end if
		object.frame = object.animationTimer
		object.frame >>= 2
		object.frame++
		object.animationTimer++
		object.animationTimer &= 15
		break
		
	end switch
end event


event ObjectDraw
	DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone02")
	temp0 = checkResult
	if game.playMode == BOOT_PLAYMODE_MISSION
		CheckCurrentStageFolder("Zone01")
		temp0 |= checkResult
		CheckCurrentStageFolder("Zone03")
		temp0 |= checkResult
	end if
	if temp0 == true
		LoadSpriteSheet("MZ/Objects.gif")
		SpriteFrame(-7, -12, 14, 24, 37, 98)
		SpriteFrame(-12, -14, 32, 30, 52, 81)
		SpriteFrame(-15, -6, 35, 20, 1, 106)
		SpriteFrame(-12, -8, 29, 28, 85, 81)
		SpriteFrame(-15, -6, 35, 20, 1, 106)
	end if
	
	CheckCurrentStageFolder("Zone07")
	if checkResult == true
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-7, -12, 14, 24, 52, 68)
		SpriteFrame(-12, -14, 32, 30, 1, 93)
		SpriteFrame(-15, -6, 35, 20, 64, 99)
		SpriteFrame(-12, -8, 29, 28, 34, 93)
		SpriteFrame(-15, -6, 35, 20, 64, 99)
	end if
	
	foreach (TypeName[Batbrain], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].startPos.x = object[arrayPos0].xpos
		object[arrayPos0].startPos.y = object[arrayPos0].ypos
		object[arrayPos0].drawOrder = 5
	next
	
	SetTableValue(TypeName[Batbrain], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(Batbrain_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(Batbrain_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone01")
	temp0 = checkResult
	CheckCurrentStageFolder("Zone02")
	temp0 |= checkResult
	CheckCurrentStageFolder("Zone03")
	temp0 |= checkResult
	if temp0 == true
		// This following sheet *should* be only in Zone02, but Sonic Team kinda just didn't wanna...
		LoadSpriteSheet("MZ/Objects.gif")
		SpriteFrame(-7, -12, 14, 24, 37, 98)
		SpriteFrame(-12, -14, 32, 30, 52, 81)
		SpriteFrame(-15, -6, 35, 20, 1, 106)
		SpriteFrame(-12, -8, 29, 28, 85, 81)
		SpriteFrame(-15, -6, 35, 20, 1, 106)
	end if
	
	CheckCurrentStageFolder("Zone07")
	if checkResult == true
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-7, -12, 14, 24, 52, 68)
		SpriteFrame(-12, -14, 32, 30, 1, 93)
		SpriteFrame(-15, -6, 35, 20, 64, 99)
		SpriteFrame(-12, -8, 29, 28, 34, 93)
		SpriteFrame(-15, -6, 35, 20, 64, 99)
	end if
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
