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

// Using HTZEggman as a prefix here because there's like 12 "Eggman" objects in the game so its less confusing this way

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

// value0 is, suprisingly, unused
private alias object.value1  : object.originPos.y
private alias object.value2  : object.oscillationAngle
// value3 is unused too
private alias object.value4  : object.timer
private alias object.value5  : object.flamethrowerFrame
private alias object.value6  : object.flamethrowerTimer

private alias object.value7  : object.health
private alias object.value8  : object.invincibilityTimer
private alias object.value9  : object.exploding

// Position values used for boss behaviours, see the init state
private alias object.value10 : object.pits.halfpoint

private alias object.value11 : object.pitL.x
private alias object.value13 : object.pitL.y

private alias object.value12 : object.pitR.x
private alias object.value14 : object.pitR.y

private alias object.value15 : object.cannonFrame
private alias object.value16 : object.nonStopBopCount

// States
private alias 0 : HTZEGGMAN_AWAITPLAYER
private alias 1 : HTZEGGMAN_RISE
private alias 2 : HTZEGGMAN_HOVER
private alias 3 : HTZEGGMAN_SINK
private alias 4 : HTZEGGMAN_EXPLODING
private alias 5 : HTZEGGMAN_DESTROYED

// Animations
private alias 1 : HTZEGGANI_IDLE
private alias 2 : HTZEGGANI_LAUGH
private alias 3 : HTZEGGANI_HIT
private alias 4 : HTZEGGANI_DEFEATED
private alias 5 : HTZEGGANI_TOASTED

// Music Events
private alias 26 : SLOT_MUSICEVENT_BOSS

private alias  0 : MUSICEVENT_FADETOBOSS
private alias  1 : MUSICEVENT_FADETOSTAGE

// Player Aliases
private alias object.xpos : player.xpos
private alias object.xvel : player.xvel
private alias object.speed : player.speed
private alias object.gravity : player.gravity
private alias object.animation : player.animation
private alias object.collisionRight : player.collisionRight

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

// Achievement Aliases
private alias 5 : ACHIEVEMENT_BOBNONSTOP


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

reserve function HTZEggman_Oscillate


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

private table HTZEggman_FlameThrowerHitboxes
	-32, -40, -48, -64, -80, -96, -112, -120, -119, -120
end table


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

private function HTZEggman_Oscillate
	// Used when hovering in order to make Robotnik bounce a bit
	
	object.ypos = object.originPos.y
	Sin256(temp0, object.oscillationAngle)
	temp0 <<= 9
	object.ypos += temp0
	object.oscillationAngle += 4
	object.oscillationAngle &= 255
end function


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

event ObjectUpdate
	switch object.state
	case HTZEGGMAN_AWAITPLAYER
		temp0 = object.ixpos
		temp0 -= 352
		stage.newXBoundary1 = temp0

		temp0 = object.ixpos
		temp0 += 96
		stage.newXBoundary2 = temp0

		temp0 = object.xpos
		temp0 -= 0xC00000
		if player[0].xpos >= temp0 // Only activate when the player's past the trigger point
			object.animation = HTZEGGANI_IDLE
			ResetObjectEntity(SLOT_MUSICEVENT_BOSS, TypeName[Music Event], MUSICEVENT_FADETOBOSS, 0, 0)
			object[SLOT_MUSICEVENT_BOSS].priority = PRIORITY_ACTIVE
			object.originPos.y = object.ypos

			// Init arena values

			// X Position of right lava pit
			object.pitR.x = object.xpos

			// X Position of left lava pit
			object.pitL.x = object.pitR.x
			object.pitL.x -= 0x1000000

			// Halfpoint between the two
			object.pits.halfpoint = object.pitL.x
			object.pits.halfpoint += 0x800000

			// Y Position to start from when in left lava pit
			object.pitL.y = object.ypos
			object.pitL.y += 0x260000

			// Y Position of where to start from when in right lava pit
			object.pitR.y = object.ypos
			object.pitR.y += 0xF0000

			object.timer = 144
			object.oscillationAngle = 128
			object.cannonFrame = 7
			
#platform: USE_STANDALONE
			object.health = 8
#endplatform
#platform: USE_ORIGINS
			if game.bossOneLife == false
				object.health = 8
			else
				object.health = 1
			end if
#endplatform
			
			object.priority = PRIORITY_ACTIVE
			object.state++
		end if
		break

	case HTZEGGMAN_RISE
		// Rising from a lava pit
		object.originPos.y -= 0xE000
		object.ypos -= 0xE000

		object.timer--
		if object.timer == 0
			object.state = HTZEGGMAN_HOVER
		end if
		break

	case HTZEGGMAN_HOVER
		// Hovering above a lava pit, firing the flamethrower
		CallFunction(HTZEggman_Oscillate)
		if object.oscillationAngle == 64
			if object.timer == 0
				object.timer++
			else
				object.state = HTZEGGMAN_SINK
			end if
		end if

		if object.timer == 1
			object.flamethrowerTimer++
			switch object.flamethrowerTimer
			case 1
				PlaySfx(SfxName[Fire Dash], false)
				object.flamethrowerFrame = 9
				break

			case 3
				object.flamethrowerFrame = 10
				break

			case 6
				object.flamethrowerFrame = 11
				break

			case 9
				object.flamethrowerFrame = 12
				break

			case 13
				object.flamethrowerFrame = 13
				break

			case 17
				object.flamethrowerFrame = 14
				break

			case 22
				object.flamethrowerFrame = 15
				break

			case 25
				CreateTempObject(TypeName[Eggman Fireball1], 0, object.xpos, object.ypos)
				if object.direction == FLIP_NONE
					object[tempObjectPos].xpos -= 0x680000
					object[tempObjectPos].xvel = -0x40000
				else
					object[tempObjectPos].xpos += 0x680000
					object[tempObjectPos].xvel = 0x40000
				end if
				object[tempObjectPos].ypos -= 0x1D0000
				object[tempObjectPos].direction = object.direction
				break

			case 27
				object.flamethrowerFrame = 16
				break

			case 33
				object.flamethrowerFrame = 17
				break

			case 39
				object.flamethrowerFrame = 18
				break
				
			case 46
				object.timer 				= 2
				object.flamethrowerTimer	= 0
				break
			end switch
		end if
		break

	case HTZEGGMAN_SINK
		// Sinking back into the lava, not to be confused with his defeat sink state
		object.originPos.y += 0xE000
		object.ypos += 0xE000
		object.timer++

		if object.xpos < object.pits.halfpoint
			temp0 = 36
			temp1 = -0x54000
		else
			temp0 = 84
			temp1 = -0x64000
		end if

		if object.timer == temp0
			PlaySfx(SfxName[Small Fireball], false)
			CreateTempObject(TypeName[Eggman Fireball2], 0, object.xpos, object.ypos)
			object[tempObjectPos].xvel = -0x1C000
			object[tempObjectPos].yvel = temp1
			CreateTempObject(TypeName[Eggman Fireball2], 0, object.xpos, object.ypos)
			object[tempObjectPos].xvel = 0x1C000
			object[tempObjectPos].yvel = temp1
		end if

		if object.timer == 160
			if player[0].xpos < object.pits.halfpoint
				object.xpos 		= object.pitL.x
				object.ypos 		= object.pitL.y
				object.originPos.y 	= object.pitL.y
			else
				object.xpos 		= object.pitR.x
				object.ypos 		= object.pitR.y
				object.originPos.y 	= object.pitR.y
			end if

			if player[0].xpos < object.xpos
				object.direction = FLIP_NONE
			else
				object.direction = FLIP_X
			end if

			object.oscillationAngle = 128
			object.state = HTZEGGMAN_RISE
		end if
		break

	case HTZEGGMAN_EXPLODING
		object.timer++
		if object.timer == 120
			// Turn the cannon frame into the broken variant
			object.cannonFrame = 8
		end if

		if object.timer == 180
			object.timer = 0
			object.animation = HTZEGGANI_TOASTED
			object.exploding = false
			object.state++
			ResetObjectEntity(SLOT_MUSICEVENT_BOSS, TypeName[Music Event], MUSICEVENT_FADETOSTAGE, 0, 0)
			object[SLOT_MUSICEVENT_BOSS].priority = PRIORITY_ACTIVE
			
			// Restore normal stage XBounds
			temp0 = tileLayer[0].xsize
			temp0 <<= 7
			stage.newXBoundary2 = temp0
		end if
		break

	case HTZEGGMAN_DESTROYED
		// Taking one final dip in the lava before disappearing one final time, object deletion gets done here
		object.originPos.y += 0x10000
		CallFunction(HTZEggman_Oscillate)
		if object.outOfBounds == true
			object.type = TypeName[Blank Object]
			object.priority = PRIORITY_BOUNDS
		end if
		break
		
	end switch

	if player[0].gravity == GRAVITY_GROUND
		// If the player's touched the ground, they are have stopped Non-Stop Bopping
		object.nonStopBopCount = 0
	end if

	if object.health != 0
		if object.invincibilityTimer > 0
			object.invincibilityTimer--
			GetBit(temp0, object.invincibilityTimer, 0)
			if temp0 == true
				SetPaletteEntry(0, 192, 0xE0E0E0)
			else
				SetPaletteEntry(0, 192, 0x000000)
			end if
		end if

		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object.invincibilityTimer == 0
				CheckEqual(player[currentPlayer].animation, ANI_HURT)
				temp0 = checkResult
				CheckEqual(player[currentPlayer].animation, ANI_DYING)
				temp0 |= checkResult
				CheckEqual(player[currentPlayer].animation, ANI_DROWNING)
				temp0 |= checkResult
				if temp0 != false
					if object.animation != HTZEGGANI_LAUGH
						object.animation = HTZEGGANI_LAUGH
						object.animationTimer = 0
					end if
				end if

				BoxCollisionTest(C_TOUCH, object.entityPos, -24, -24, 24, 22, currentPlayer, player[currentPlayer].hitboxLeft, player[currentPlayer].hitboxTop, player[currentPlayer].hitboxRight, player[currentPlayer].hitboxBottom)
				if checkResult == true
					CallFunction(Player_CheckHit)
					if checkResult == true
						object.health--
						if object.health == 0
#platform: USE_ORIGINS
							CallNativeFunction2(NotifyCallback, NOTIFY_KILL_BOSS, 0)
							CallNativeFunction2(NotifyCallback, NOTIFY_BOSS_END, 1)
#endplatform

							player.score += 1000
							object.animation = HTZEGGANI_DEFEATED
							object.animationTimer = 0
							object.exploding = true
							object.originPos.y = object.ypos
							object.state = HTZEGGMAN_EXPLODING
							object.flamethrowerTimer = 0
							object.flamethrowerFrame = 0
							
							if object.nonStopBopCount >= 7
								if stage.debugMode == false
									// Grant the "Bob Non-stop" Achievement
									CallNativeFunction2(SetAchievement, ACHIEVEMENT_BOBNONSTOP, 100)
								end if
							end if
						else
							object.animation 			= HTZEGGANI_HIT
							object.animationTimer 		= 0
							object.invincibilityTimer 	= 32
							PlaySfx(SfxName[Boss Hit], false)

							if currentPlayer == 0
								if player[0].gravity == GRAVITY_AIR
									object.nonStopBopCount++
								end if
							end if
						end if
					end if
				else
					if object.flamethrowerTimer != 0
						temp1 = object.flamethrowerFrame
						temp1 -= 8
						if temp1 <= 7
							GetTableValue(temp0, temp1, HTZEggman_FlameThrowerHitboxes)
							if object.direction == FLIP_NONE
								BoxCollisionTest(C_TOUCH, object.entityPos, temp0, -32, -24, -29, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
							else
								FlipSign(temp0)
								BoxCollisionTest(C_TOUCH, object.entityPos, 24, -32, temp0, -29, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
							end if

							if checkResult == true
								CallFunction(Player_FireHit)
							end if
						end if
					end if
				end if
			end if
		next
	end if

	if object.state != HTZEGGMAN_DESTROYED
		// Enforce the arena right wall on players
		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

	if object.exploding != false
		temp0 = oscillation
		temp0 &= 7
		if temp0 == 0
			Rand(temp0, 48)
			temp0 -= 24
			temp0 <<= 16
			temp0 += object.xpos
			Rand(temp1, 48)
			temp1 -= 24
			temp1 <<= 16
			temp1 += object.ypos
			CreateTempObject(TypeName[Explosion], 0, temp0, temp1)
			object[tempObjectPos].drawOrder = 5
			PlaySfx(SfxName[Explosion], false)
		end if
	end if

	if object.cannonFrame == 8
		// If the cannon's destroyed, then spawn Smoke Puffs too
		temp0 = oscillation
		temp0 &= 31
		if temp0 == 0
			CreateTempObject(TypeName[Eggman SmokePuff], 0, object.xpos, object.ypos)
			object[tempObjectPos].ypos -= 0x280000
			object[tempObjectPos].xvel -= 0x6000
			object[tempObjectPos].yvel -= 0xC000
		end if
	end if
end event


event ObjectDraw
	if object.flamethrowerTimer != 0
		DrawSpriteFX(object.flamethrowerFrame, FX_FLIP, object.xpos, object.ypos)
	end if

	switch object.animation
	case HTZEGGANI_IDLE
		temp0 = object.animationTimer
		temp0 >>= 3
		object.animationTimer++
		object.animationTimer &= 15
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break

	case HTZEGGANI_LAUGH
		temp0 = object.animationTimer
		temp0 &= 15
		temp0 >>= 3
		temp0 += 2
		object.animationTimer++
		if object.animationTimer == 50
			object.animationTimer = 0

			if player[0].animation != ANI_DYING
				object.animation = HTZEGGANI_IDLE
			end if
		end if
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break

	case HTZEGGANI_HIT
		temp0 = object.animationTimer
		temp0 &= 1
		temp0 += 2
		object.animationTimer++
		if object.animationTimer == 50
			object.animationTimer = 0
			object.animation = HTZEGGANI_IDLE
		end if
		DrawSpriteFX(4, FX_FLIP, object.xpos, object.ypos)
		break

	case HTZEGGANI_DEFEATED
		temp0 = object.animationTimer
		temp0 >>= 5
		temp0 += 4
		temp0 %= 5
		object.animationTimer++
		object.animationTimer &= 63
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break

	case HTZEGGANI_TOASTED
		DrawSpriteFX(5, FX_FLIP, object.xpos, object.ypos)
		break
		
	end switch

	DrawSpriteFX(6, FX_FLIP, object.xpos, object.ypos)
	DrawSpriteFX(object.cannonFrame, FX_FLIP, object.xpos, object.ypos)
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone05")

	if checkResult == true
		LoadSpriteSheet("HTZ/Objects.gif")

		SpriteFrame(-15, -10, 17, 7, 66, 108) // #0  - Visor frame 1
		SpriteFrame(-15, -10, 17, 7, 66, 116) // #1  - Visor frame 2 (seems to be a duplicate?)
		SpriteFrame(-15, -10, 17, 7, 66, 124) // #2  - Laugh frame 1
		SpriteFrame(-15, -10, 17, 7, 66, 132) // #3  - Laugh frame 2
		SpriteFrame(-15, -10, 17, 7, 66, 140) // #4  - Hurt frame
		SpriteFrame(-15, -10, 17, 7, 66, 148) // #5  - Burnt frame
		SpriteFrame(-32, -12, 64, 45, 1, 210) // #6  - Eggmobile main frame (cropped from larger sprite)
		SpriteFrame(-24, -36, 40, 24, 9, 186) // #7  - Eggmobile cannon (croped from same larger sprite as above)
		SpriteFrame(-16, -20, 32, 8, 17, 132) // #8  - Destroyed Eggmobile cannon (again cropped frome larger sprite)

		// #9 -> #18 - Flamethrower frames
		SpriteFrame(-32, -32, 8, 6, 1, 124)		
		SpriteFrame(-40, -33, 16, 7, 10, 124)	
		SpriteFrame(-48, -33, 24, 8, 1, 54)		
		SpriteFrame(-64, -33, 40, 8, 1, 63)		
		SpriteFrame(-80, -33, 56, 8, 1, 72)		
		SpriteFrame(-96, -33, 72, 8, 1, 81)		
		SpriteFrame(-112, -33, 88, 8, 1, 90)	
		SpriteFrame(-120, -33, 88, 8, 1, 99)	
		SpriteFrame(-119, -33, 63, 8, 1, 108)	
		SpriteFrame(-120, -32, 31, 6, 1, 117)	

		if options.vsMode == true
			foreach (TypeName[Eggman], arrayPos0, ALL_ENTITIES)
				// No Eggmen to interfere in 2P mode
				object[arrayPos0].type = TypeName[Blank Object]
			next
		end if
	else
		LoadSpriteSheet("MBZ/Objects.gif")

		SpriteFrame(-15, -10, 17, 7, 1007, 34)  // #0  - Visor frame 1
		SpriteFrame(-15, -10, 17, 7, 1007, 42)  // #1  - Visor frame 2 (seems to be a duplicate?)
		SpriteFrame(-15, -10, 17, 7, 1007, 50)  // #2  - Laugh frame 1
		SpriteFrame(-15, -10, 17, 7, 1007, 58)  // #3  - Laugh frame 2
		SpriteFrame(-15, -10, 17, 7, 1007, 66)  // #4  - Hurt frame
		SpriteFrame(-15, -10, 17, 7, 1007, 74)  // #5  - Burnt frame
		
		// While the flame and visor sprites are all the way on the left of the sheet, these Eggmobile ones are over in the middle, separated...
		SpriteFrame(-32, -12, 64, 45, 415, 154) // #6  - Eggmobile main frame (cropped from larger sprite)
		SpriteFrame(-24, -36, 40, 24, 423, 130) // #7  - Eggmobile cannon (croped from same larger sprite as above)
		
		// Broken cannon frame with another fun difference - while the HTZ sheet has a duplicate Eggmobile attached to this sprite, MBZ optimises it and crops the unnecessary sprite off
		SpriteFrame(-16, -20, 32, 8, 480, 170)  // #8  - Destroyed Eggmobile cannon (again cropped frome larger sprite)

		// #9 -> #18 - Flamethrower frames
		SpriteFrame(-32, -32, 8, 6, 877, 116)
		SpriteFrame(-40, -33, 16, 7, 886, 116)
		SpriteFrame(-48, -33, 24, 8, 982, 46)
		SpriteFrame(-64, -33, 40, 8, 966, 55)
		SpriteFrame(-80, -33, 56, 8, 950, 64)
		SpriteFrame(-96, -33, 72, 8, 934, 73)
		SpriteFrame(-112, -33, 88, 8, 919, 82)
		SpriteFrame(-120, -33, 88, 8, 919, 91)
		SpriteFrame(-119, -33, 63, 8, 877, 100)
		SpriteFrame(-120, -32, 31, 6, 877, 109)
	end if
end event


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

event RSDKDraw
	DrawSprite(0)
	DrawSprite(1)
	DrawSprite(2)
	
	if editor.showGizmos == true
		editor.drawingOverlay = true
		
		// We're gonna draw out all the arena details here
		
		// First, draw the overall boss arena bounds
		temp0 = 352; temp1 = 272; temp2 = 96; temp3 = -32
		CallFunction(EditorHelpers_DrawHitbox)
		
		// X Position of right lava pit
		object.pitR.x = object.xpos

		// X Position of left lava pit
		object.pitL.x = object.pitR.x
		object.pitL.x -= 0x1000000

		// Y Position to start from when in left lava pit
		object.pitL.y = object.ypos
		object.pitL.y += 0x260000

		// Y Position of where to start from when in right lava pit
		object.pitR.y = object.ypos
		object.pitR.y += 0xF0000
		
		// Lava->Sky on left lava pit
		temp0 = object.pitL.y
		temp0 -= 0x8C0000
		DrawArrow(object.pitL.x, object.pitL.y, object.pitL.x, temp0, 0, 255, 0)
		
		// Lava->Sky on right lava pit
		temp0 = object.pitR.y
		temp0 -= 0x8C0000
		DrawArrow(object.pitR.x, object.pitR.y, object.pitR.x, temp0, 0, 255, 0)
		
		DrawLine(object.pitL.x, object.pitL.y, object.pitR.x, object.pitR.y, 0, 255, 0)
		
		editor.drawingOverlay = false
	end if
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone05")
	if checkResult == true
		LoadSpriteSheet("HTZ/Objects.gif")
		SpriteFrame(-15, -10, 17, 7, 66, 108) // Eggman peeking through the visor frame
		SpriteFrame(-32, -12, 64, 45, 1, 210) // Eggmobile
		SpriteFrame(-24, -36, 40, 24, 9, 186) // Cannon
		
		// The last two SpriteFrames could be combined here to simply use the entire sprite, but for the sake of clarity (+ MBZ support), they shall be left alone...
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-15, -10, 17, 7, 1007, 34)
		SpriteFrame(-32, -12, 64, 45, 415, 154)
		SpriteFrame(-24, -36, 40, 24, 423, 130)
	end if
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
