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

//-------Aliases-------//
private alias 43 : TYPE_MECHASONIC

// Function declarations
reserve function MechaSonic_Function99
reserve function MechaSonic_Function100

// Static Values

// Tables
public table MechaSonic_table42
	0, -0x180000, 0, -0x30000, -0x100000, -0x100000, -0x20000, -0x20000, -0x180000, 0, -0x30000, 0, -0x100000, 0x100000, -0x20000, 0x20000
	0, 0x180000, 0, 0x30000, 0x100000, 0x100000, 0x20000, 0x20000, 0x180000, 0, 0x30000, 0, 0x100000, -0x100000, 0x20000, -0x20000
end table

public table MechaSonic_table43
	4, 5, 4, 3, 252
end table

public table MechaSonic_table44
	3, 3, 6, 6, 6, 7, 7, 7, 8, 8, 8, 6, 6, 7, 7, 8, 8, 6, 7, 8
end table

public table MechaSonic_table45
	8, 7, 6, 8, 8, 7, 7, 6, 6, 8, 8, 8, 7, 7, 7, 6, 6, 6, 3, 3
end table

public table MechaSonic_table41
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
end table


function MechaSonic_Function99
	if object[-9].animation > 1
		CheckEqual(object[currentPlayer].animation, ANI_HURT)
		temp0 = checkResult
		CheckEqual(object[currentPlayer].animation, ANI_DYING)
		temp0 |= checkResult
		CheckEqual(object[currentPlayer].animation, ANI_DROWNING)
		temp0 |= checkResult
		if temp0 != 0
			if object[-9].animation != 3
				object[-9].animation = 3
				object[-9].animationTimer = 0
			end if
		end if
	end if
end function


function MechaSonic_Function100
	object.value0 = 100
	PlaySfx(SfxName[Saw], 0)
	object.animation = 1
	object.state = 3
end function


event ObjectMain
	switch object.state
	case 0
		temp0 = object.xpos
		temp0 >>= 16
		temp0 += 30
		temp1 = screen.xcenter
		temp1 -= 160
		temp0 += temp1
		stage.newXBoundary2 = temp0
		temp0 -= 320
		temp0 -= temp1
		stage.newXBoundary1 = temp0
		temp0 = object.xpos
		temp0 -= 0x880000
		if object[0].xpos >= temp0
			if temp1 > 32
				temp1 = 32
			end if
			temp2 = temp1
			temp1 += 160
			object.value8 = 0x80000
			object.value8 *= temp1
			object.value8 /= 160
			temp2 <<= 8
			object.value8 += temp2
			temp2 >>= 2
			object.value8 += temp2
			temp2 >>= 6
			object.value9 = 0x2000
			object.value9 *= temp1
			object.value9 /= 160
			object.value10 = 0x40000
			object.value10 *= temp1
			object.value10 /= 160
			temp2 <<= 8
			object.value10 += temp2
			temp2 >>= 2
			object.value10 += temp2
			temp2 >>= 6
			temp2 <<= 16
			object.xpos += temp2
			object.value5 = 8
			object.ypos -= 0xC40000
			ResetObjectEntity(26, TypeName[Music Event], 0, 0, 0)
			object[26].priority = PRIORITY_ACTIVE
			object.state = 1
		end if
		break
	case 1
		object.value0++
		if object.value0 >= 45
			object.value0 = 0
			PlaySfx(SfxName[Thruster], 0)
			object.state = 2
		end if
		break
	case 2
		object.value0++
		if object.value0 == 60
			PlaySfx(SfxName[Thruster], 0)
		end if
		if object.value0 == 120
			PlaySfx(SfxName[Thruster], 0)
		end if
		object.value11 = oscillation
		object.value11 &= 3
		object.value11 >>= 1
		object.value11 += 11
		object.ypos += 0x10000
		ObjectTileCollision(CSIDE_FLOOR, 0, 28, 0)
		if checkResult == 1
			object[-9].animation = 1
			object.value11 = 15
			CallFunction(MechaSonic_Function100)
		end if
		break
	case 3
		object.value0--
		if object.value0 == 50
			PlaySfx(SfxName[Saw2], 0)
		end if
		if object.value0 == 0
			GetTableValue(object.state, object.value1, MechaSonic_table41)
			object.value1++
			object.value1 &= 15
		end if
		break
	case 4
	case 10
	case 13
		object.animation = 4
		object.animationTimer = 1
		object.value2 = 0
		object.value0 = 148
		object.state++
		break
	case 5
		object.value0--
		if object.value0 < 0
			object.value0 = 64
			object.xvel = object.value8
			if object.value3 == 0
				FlipSign(object.xvel)
			end if
			object.value3 ^= 1
			object.state++
		end if
		break
	case 11
	case 14
		object.value0--
		if object.value0 < 0
			object.value0 = 64
			object.xvel = object.value10
			if object.value3 == 0
				FlipSign(object.xvel)
			end if
			object.value3 ^= 1
			object.state++
		end if
		break
	case 6
		object.xpos += object.xvel
		object.value0--
		if object.value0 < 0
			object.animation = 6
			object.animationTimer = 1
			object.value2 = 8
			object.direction ^= FLIP_X
			object.state = 16
		end if
		if object.value3 == 0
			object.xvel -= object.value9
		else
			object.xvel += object.value9
		end if
		break
	case 16
		break
	case 12
		object.xpos += object.xvel
		if object.yvel != 0
			object.ypos += object.yvel
			object.yvel += 0x3800
			if object.yvel > 0
				ObjectTileCollision(CSIDE_FLOOR, 0, 28, 0)
				if checkResult == 1
					object.yvel = 0
				end if
			end if
		end if
		object.value0--
		if object.value0 == 60
			object.yvel = -0x60000
		end if
		if object.value0 < 0
			object.animation = 6
			object.animationTimer = 1
			object.value2 = 8
			object.direction ^= FLIP_X
			object.state = 16
		end if
		break
	case 15
		object.xpos += object.xvel
		if object.yvel != 0
			object.ypos += object.yvel
			object.yvel += 0x3800
			if object.yvel > 0
				if object.value4 == 0
					temp0 = 0
					temp7 = 0
					arrayPos0 = object[-8].entityPos
					while temp7 < 8
						object[arrayPos0].type = TypeName[MechaSonicSpike]
						object[arrayPos0].xpos = object.xpos
						GetTableValue(temp1, temp0, MechaSonic_table42)
						temp0++
						object[arrayPos0].xpos += temp1
						object[arrayPos0].ypos = object.ypos
						GetTableValue(temp1, temp0, MechaSonic_table42)
						temp0++
						object[arrayPos0].ypos += temp1
						GetTableValue(object[arrayPos0].xvel, temp0, MechaSonic_table42)
						temp0++
						GetTableValue(object[arrayPos0].yvel, temp0, MechaSonic_table42)
						temp0++
						object[arrayPos0].frame = temp7
						arrayPos0++
						temp7++
					loop
					PlaySfx(SfxName[Spikes Move], 0)
					object.value4 = 1
				end if
				ObjectTileCollision(CSIDE_FLOOR, 0, 28, 0)
				if checkResult == 1
					object.yvel = 0
				end if
			end if
		end if
		object.value0--
		if object.value0 == 60
			object.yvel = -0x60000
		end if
		if object.value0 < 0
			object.value4 = 0
			object.animation = 6
			object.animationTimer = 1
			object.value2 = 8
			object.direction ^= FLIP_X
			object.state = 16
		end if
		break
	case 7
		object.animation = 2
		object.value0 = 32
		object.value4 = 2
		object.state = 8
		break
	case 8
		object.value0--
		if object.value0 < 0
			object.value0 = 64
			object.xvel = object.value8
			if object.value3 == 0
				FlipSign(object.xvel)
			end if
			object.value3 ^= 1
			PlaySfx(SfxName[Release], 0)
			object.state++
		end if
		break
	case 9
		object.xpos += object.xvel
		object.value0--
		if object.value0 == 32
			object.animationTimer = 0
			object.value2 = 0
			object.value11 = 15
			object.animation = 3
		end if
		if object.value0 < 0
			object.value4--
			if object.value4 > 0
				object.value0 = 32
				object.animation = 2
				object.state = 8
			else
				CallFunction(MechaSonic_Function100)
			end if
		end if
		if object.value3 == 0
			object.xvel -= object.value9
		else
			object.xvel += object.value9
		end if
		break
	case 17
		object.value0++
		if object.value0 == 240
			object.value0 = 0
			object.value7 = 0
			temp0 = tileLayer[0].xsize
			temp0 <<= 7
			stage.newXBoundary2 = temp0
			object.frame = 15
			object.state = 18
		end if
		break
	case 18
		object.value0++
		if object.value0 == 60
			object.type = TypeName[Blank Object]
		end if
		break
	end switch
	switch object.animation
	case 0
		break
	case 1
		object.animationTimer++
		if object.animationTimer >= 3
			object.animationTimer = 0
			object.frame++
			if object.frame >= 3
				object.frame = 0
			end if
		end if
		break
	case 2
		object.frame = 3
		object.value11 = oscillation
		object.value11 &= 3
		object.value11 >>= 1
		if object.state == 9
			object.value11 += 9
		else
			object.value11 += 13
		end if
		break
	case 3
		object.animationTimer--
		if object.animationTimer <= 0
			object.animationTimer = 4
			GetTableValue(object.frame, object.value2, MechaSonic_table43)
			object.value2++
			if object.frame == 5
				object.direction ^= FLIP_X
			end if
			if object.frame == 3
				object.value2 = 0
				object.animationTimer = 255
			end if
		end if
		break
	case 4
		object.animationTimer--
		if object.animationTimer <= 0
			object.animationTimer = 4
			GetTableValue(object.frame, object.value2, MechaSonic_table44)
			object.value2++
			if object.value2 == 21
				PlaySfx(SfxName[Buzzsaw], 0)
				object.value2 = 0
				object.frame = 6
				object.animationTimer = 0
				object.animation = 5
			end if
		end if
		break
	case 5
		object.animationTimer++
		if object.animationTimer >= 3
			object.animationTimer = 0
			object.frame++
			if object.frame >= 9
				object.frame = 6
			end if
		end if
		break
	case 6
		object.animationTimer--
		if object.animationTimer <= 0
			object.animationTimer = 4
			GetTableValue(object.frame, object.value2, MechaSonic_table45)
			object.value2++
			if object.value2 == 21
				object.value2 = 0
				CallFunction(MechaSonic_Function100)
			end if
		end if
		break
	end switch
	object[+1].state = 0
	if object.state > 2
		if object.value5 != 0
			if object.value6 > 0
				object.value6--
				GetBit(temp0, object.value6, 0)
				if temp0 == 1
					SetPaletteEntry(0, 192, 0xE0E0E0)
				else
					SetPaletteEntry(0, 192, 0)
				end if
			end if
			CheckEqual(object.frame, 6)
			temp0 = checkResult
			CheckEqual(object.frame, 7)
			temp0 |= checkResult
			CheckEqual(object.frame, 8)
			temp0 |= checkResult
			if temp0 != 0
				foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
					CallFunction(MechaSonic_Function99)
					BoxCollisionTest(C_TOUCH, object.entityPos, -12, -12, 12, 12, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
					if checkResult == 1
						CallFunction(PlayerObject_Hit)
					end if
				next
			else
				foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
					CallFunction(MechaSonic_Function99)
					if object.animation == 1
						if object.direction == FLIP_NONE
							if object[currentPlayer].xpos >= object.xpos
								BoxCollisionTest(C_TOUCH, object.entityPos, -12, -12, 12, 12, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
								if checkResult == 1
									CallFunction(PlayerObject_Hit)
								end if
							end if
						else
							if object[currentPlayer].xpos <= object.xpos
								BoxCollisionTest(C_TOUCH, object.entityPos, -12, -12, 12, 12, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
								if checkResult == 1
									CallFunction(PlayerObject_Hit)
								end if
							end if
						end if
					end if
					if object.value6 == 0
						if object[currentPlayer].state != PlayerObject_Hurt
							BoxCollisionTest(C_TOUCH, object.entityPos, -12, -12, 12, 12, currentPlayer, object[currentPlayer].value40, object[currentPlayer].value38, object[currentPlayer].value41, object[currentPlayer].value39)
							if checkResult == 1
								CallFunction(PlayerObject_CheckHit)
							else
								CallFunction(BossTarget)
							end if
							if checkResult == 1
								object.value5--
								if object.value5 == 0
									player.score += 1000
									object[-9].frame = 4
									object[-9].animationTimer = 0
									object[-9].animation = 5
									object.value7 = 1
									object.state = 17
									object.value0 = 0
									StopSfx(SfxName[Saw])
									StopSfx(SfxName[Release])
									StopSfx(SfxName[Buzzsaw])
									object.animation = 0
									object.value11 = 15
								else
									object.value6 = 32
									if object[-9].animation > 1
										object[-9].animationTimer = 0
										object[-9].animation = 4
									end if
									PlaySfx(SfxName[Boss Hit], 0)
								end if
							end if
						end if
					end if
				next
			end if
		end if
	end if
	if object.state != 18
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			temp0 = object[currentPlayer].collisionRight
			temp0 <<= 16
			temp0 += object[currentPlayer].xpos
			temp1 = stage.curXBoundary2
			temp1 <<= 16
			if temp0 > temp1
				object[currentPlayer].xvel = 0
				object[currentPlayer].speed = 0
				object[currentPlayer].xpos = temp1
				temp0 = object[currentPlayer].collisionRight
				temp0 <<= 16
				object[currentPlayer].xpos -= temp0
			end if
		next
	end if
	if object.value7 == 1
		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], 0)
		end if
	end if
end event


event ObjectDraw
	if object.state > 0
		DrawSpriteFX(object.value11, FX_FLIP, object.xpos, object.ypos)
		DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
	end if
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone12")
	if checkResult == 1
		LoadSpriteSheet("DEZ/Objects.gif")
		SpriteFrame(-20, -28, 40, 56, 1, 1)
		SpriteFrame(-20, -28, 40, 56, 42, 1)
		SpriteFrame(-20, -28, 40, 56, 83, 1)
		SpriteFrame(-20, -27, 40, 55, 124, 1)
		SpriteFrame(-22, -27, 42, 55, 165, 1)
		SpriteFrame(-22, -27, 44, 55, 208, 1)
		SpriteFrame(-24, -18, 48, 48, 255, 1)
		SpriteFrame(-24, -18, 48, 48, 304, 1)
		SpriteFrame(-24, -18, 48, 48, 353, 1)
		SpriteFrame(14, 4, 24, 24, 402, 1)
		SpriteFrame(14, 5, 24, 22, 427, 1)
		SpriteFrame(-12, 28, 16, 8, 402, 26)
		SpriteFrame(-12, 28, 16, 22, 419, 26)
		SpriteFrame(19, 20, 19, 8, 452, 1)
		SpriteFrame(20, 20, 18, 7, 452, 10)
		SpriteFrame(0, 0, 1, 1, 1, 1)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-20, -28, 40, 56, 513, 1)
		SpriteFrame(-20, -28, 40, 56, 554, 1)
		SpriteFrame(-20, -28, 40, 56, 595, 1)
		SpriteFrame(-20, -27, 40, 55, 636, 1)
		SpriteFrame(-22, -27, 42, 55, 677, 1)
		SpriteFrame(-22, -27, 44, 55, 720, 1)
		SpriteFrame(-24, -18, 48, 48, 767, 1)
		SpriteFrame(-24, -18, 48, 48, 816, 1)
		SpriteFrame(-24, -18, 48, 48, 865, 1)
		SpriteFrame(14, 4, 24, 24, 914, 1)
		SpriteFrame(14, 5, 24, 22, 939, 1)
		SpriteFrame(-12, 28, 16, 8, 914, 26)
		SpriteFrame(-12, 28, 16, 22, 931, 26)
		SpriteFrame(19, 20, 19, 8, 964, 1)
		SpriteFrame(20, 20, 18, 7, 964, 10)
		SpriteFrame(0, 0, 1, 1, 513, 1)
	end if
	SetTableValue(4, 0, MechaSonic_table41)
	SetTableValue(7, 1, MechaSonic_table41)
	SetTableValue(10, 2, MechaSonic_table41)
	SetTableValue(4, 3, MechaSonic_table41)
	SetTableValue(4, 4, MechaSonic_table41)
	SetTableValue(13, 5, MechaSonic_table41)
	SetTableValue(7, 6, MechaSonic_table41)
	SetTableValue(10, 7, MechaSonic_table41)
	SetTableValue(4, 8, MechaSonic_table41)
	SetTableValue(4, 9, MechaSonic_table41)
	SetTableValue(10, 10, MechaSonic_table41)
	SetTableValue(4, 11, MechaSonic_table41)
	SetTableValue(7, 12, MechaSonic_table41)
	SetTableValue(4, 13, MechaSonic_table41)
	SetTableValue(10, 14, MechaSonic_table41)
	SetTableValue(13, 15, MechaSonic_table41)
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
