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

//-------Aliases-------//
private alias 79 : TYPE_EGGMAN

// Function declarations
reserve function MPZEggman_Function171
reserve function MPZEggman_Function172

// Static Values
public value MPZEggman_value42 = 0;

// Tables
public table MPZEggman_table53
	8, 9, 9, 8, 8, 17
end table

public table MPZEggman_table52
	0, 2, 4, 6, 1, 3, 5
end table


function MPZEggman_Function171
	object.ypos = object.value1
	Sin256(temp0, object.value2)
	temp0 <<= 9
	object.ypos += temp0
	object.value2 += 4
	object.value2 &= 255
end function


function MPZEggman_Function172
	object.value22--
	if object.value22 == 0
		if object.value21 > 0
			PlaySfx(SfxName[Small Laser], 0)
			object.value22 = 30
			object.value4 = 16
			arrayPos0 = object.entityPos
			arrayPos0 += 8
			arrayPos0 += object.value21
			ResetObjectEntity(arrayPos0, MPZEggman_value42, 0, object.xpos, object.ypos)
			object[arrayPos0].direction = object.direction
			object[arrayPos0].xvel = object.value11
			object[arrayPos0].xvel <<= 2
			if object.direction == FLIP_NONE
				FlipSign(object[arrayPos0].xvel)
				object[arrayPos0].xpos += 0x40000
			else
				object[arrayPos0].xpos -= 0x40000
			end if
			object[arrayPos0].value0 = object[arrayPos0].xpos
			object[arrayPos0].value0 &= -0x10000
			object[arrayPos0].ypos += 0x70000
			object.value21--
		end if
	end if
end function


event ObjectMain
	switch object.state
	case 0
		temp0 = object.ixpos
		temp0 -= screen.xcenter
		stage.newXBoundary1 = temp0
		temp0 = object.ixpos
		temp0 += screen.xcenter
		stage.newXBoundary2 = temp0
		temp0 = object.iypos
		temp0 += 72
		stage.newYBoundary2 = temp0
		if object[0].xpos >= object.xpos
			object.animation = 1
			ResetObjectEntity(26, TypeName[Music Event], 0, 0, 0)
			object[26].priority = PRIORITY_ACTIVE
			object.value11 = 0x10000
			object.value11 *= screen.xcenter
			object.value11 /= 160
			object.value23 = 0x200000
			object.value23 *= screen.xcenter
			object.value23 /= 160
			object.value2 = 128
			object.value15 = 1
			object.value5 = stage.newXBoundary1
			object.value5 += 32
			object.value5 <<= 16
			object.value6 = stage.newXBoundary2
			object.value6 -= 32
			object.value6 <<= 16
			arrayPos0 = object.entityPos
			arrayPos1 = arrayPos0
			arrayPos1 += 7
			temp0 = 36
			while arrayPos0 < arrayPos1
				arrayPos0++
				object[arrayPos0].type = TypeName[Eggman Balloon]
				object[arrayPos0].value0 = object.entityPos
				object[arrayPos0].angle = temp0
				if temp0 >= 128
					object[arrayPos0].drawOrder = 2
				end if
				object[arrayPos0].priority = PRIORITY_ACTIVE
				temp0 += 36
			loop
			object.value17 = 39
			object.value18 = 39
			object.value7 = object.xpos
			object.value8 = object.ypos
			object.value8 -= 0x300000
			object.value9 = object.ypos
			object.value9 -= 0x800000
			object.value10 = object.ypos
			object.ypos -= 0x1200000
			object.value1 = object.ypos
			object.value4 = 0
			object.value12 = 8
			object.priority = PRIORITY_ACTIVE
			object.state = 1
		end if
		break
	case 1
		object.value1 += 0x10000
		if object.value1 >= object.value10
			object.xvel = object.value11
			if object[0].xpos < object.xpos
				object.direction = FLIP_NONE
				FlipSign(object.xvel)
			else
				object.direction = FLIP_X
			end if
			object.value4 = 2
			object.state = 2
		end if
		CallFunction(MPZEggman_Function171)
		break
	case 2
		object.xpos += object.xvel
		temp0 = 0
		if object.direction == FLIP_NONE
			if object.xpos <= object.value5
				temp0 = 1
			end if
		else
			if object.xpos >= object.value6
				temp0 = 1
			end if
		end if
		if temp0 == 1
			FlipSign(object.xvel)
			object.direction ^= FLIP_X
			object.value4--
			if object.value4 == 0
				object.xvel = object.value11
				if object.direction == FLIP_NONE
					FlipSign(object.xvel)
				end if
				object.yvel = -0x10000
				object.state = 3
			end if
		end if
		CallFunction(MPZEggman_Function171)
		break
	case 3
		object.xpos += object.xvel
		object.value1 += object.yvel
		if object.value1 <= object.value8
			object.yvel = 0
		end if
		if object.direction == FLIP_NONE
			if object.xpos <= object.value7
				temp0 = 1
			end if
		else
			if object.xpos >= object.value7
				temp0 = 1
			end if
		end if
		if temp0 == 1
			object.xvel = 0
			object.xpos = object.value7
			object.state = 4
		end if
		CallFunction(MPZEggman_Function171)
		break
	case 4
		object.value17++
		object.value18++
		if object.value17 >= 104
			object.state = 5
		end if
		CallFunction(MPZEggman_Function171)
		break
	case 5
		object.value18--
		if object.value18 <= 0
			object.state = 6
		end if
		CallFunction(MPZEggman_Function171)
		break
	case 6
		object.value17--
		if object.value17 <= 39
			object.state = 7
		end if
		CallFunction(MPZEggman_Function171)
		break
	case 7
		object.value18++
		if object.value18 >= 39
			object.state = 1
		end if
		CallFunction(MPZEggman_Function171)
		break
	case 8
		object.value1 += object.yvel
		if object.value1 <= object.value9
			object.yvel = 0
		end if
		if object.value17 > 39
			object.value17--
		end if
		if object.value18 > 0
			object.value18--
		else
			if object.value19 < 0x100000
				object.value19 += 0x8000
			end if
		end if
		if object.value20 == 0
			if object.value12 > 1
				object.state = 9
			else
				object.yvel = -0x18000
				object.xvel = object.value11
				if object.direction == FLIP_NONE
					FlipSign(object.xvel)
				end if
				object.state = 10
			end if
		end if
		CallFunction(MPZEggman_Function171)
		break
	case 9
		object.value19 -= 0x8000
		if object.value19 <= 0
			object.value19 = 0
			object.state = 7
		end if
		CallFunction(MPZEggman_Function171)
		break
	case 10
		if object.value4 == 0
			object.xpos += object.xvel
			object.value1 += object.yvel
			if object.value1 <= object.value9
				object.yvel = 0
			end if
			if object.direction == FLIP_NONE
				temp0 = object.value5
				temp0 += object.value23
				if object.xpos <= temp0
					object.yvel = 0x18000
					object.value21 = 3
					object.value22 = 30
					object.direction = FLIP_X
					object.state = 11
				end if
			else
				temp0 = object.value6
				temp0 -= object.value23
				if object.xpos >= temp0
					object.yvel = 0x18000
					object.value21 = 3
					object.value22 = 30
					object.direction = FLIP_NONE
					object.state = 11
				end if
			end if
			CallFunction(MPZEggman_Function171)
			CallFunction(MPZEggman_Function172)
		else
			object.value4--
		end if
		break
	case 11
		if object.value4 == 0
			object.xpos += object.xvel
			object.value1 += object.yvel
			if object.value1 >= object.value10
				object.yvel = -0x18000
				object.state = 12
			end if
			if object.direction == FLIP_NONE
				if object.xpos >= object.value6
					object.xvel = 0
				end if
			else
				if object.xpos <= object.value5
					object.xvel = 0
				end if
			end if
			CallFunction(MPZEggman_Function171)
			CallFunction(MPZEggman_Function172)
		else
			object.value4--
		end if
		break
	case 12
		if object.value4 == 0
			object.xpos += object.xvel
			object.value1 += object.yvel
			temp0 = object.value10
			temp0 -= 0x300000
			if object.value1 <= temp0
				object.xvel = object.value11
				if object.direction == FLIP_NONE
					FlipSign(object.xvel)
				end if
			end if
			if object.value1 <= object.value9
				object.yvel = 0
				object.state = 10
			end if
			CallFunction(MPZEggman_Function171)
			CallFunction(MPZEggman_Function172)
		else
			object.value4--
		end if
		break
	case 13
		object.value4++
		if object.value4 == 180
			object.value4 = 0
			object.animation = 5
			object.value14 = 0
			object.state++
			ResetObjectEntity(26, TypeName[Music Event], 1, 0, 0)
			object[26].priority = PRIORITY_ACTIVE
			temp0 = tileLayer[0].xsize
			temp0 <<= 7
			stage.newXBoundary2 = temp0
		end if
		break
	case 14
		object.ypos += object.yvel
		object.yvel += 0x1800
		object.value4++
		if object.value4 == 15
			object.yvel = 0
			object.value4 = 0
			object.animationTimer = 0
			object.state++
		end if
		break
	case 15
		if object.value4 < 48
			object.ypos += object.yvel
			object.yvel -= 0x800
			object.value4++
		else
			object.value4 = 0
			object.yvel = 0
			object.value1 = object.ypos
			object.state++
		end if
		break
	case 16
		object.ypos = object.value1
		CallFunction(MPZEggman_Function171)
		if object.value4 < 8
			object.value4++
		else
			object.value4 = 0
			object.animation = 6
			object.animationTimer = 0
			object.value15 = 2
			object.value16 = 0
			object.direction = FLIP_X
			object.priority = PRIORITY_ACTIVE
			temp0 = tileLayer[0].xsize
			temp0 <<= 7
			//stage.newXBoundary2 = temp0
			object.state++
		end if
		break
	case 17
		object.xpos += 0x40000
		object.value1 -= 0x4000
		CallFunction(MPZEggman_Function171)
		if object.outOfBounds == 1
			object.type = TypeName[Blank Object]
		end if
		break
	end switch
	if object.value12 != 0
		if object.value13 > 0
			object.value13--
			GetBit(temp0, object.value13, 0)
			if temp0 == 1
				SetPaletteEntry(0, 192, 0xE0E0E0)
			else
				SetPaletteEntry(0, 192, 0)
			end if
		end if
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object.value13 == 0
				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.animation != 2
						object.animation = 2
						object.animationTimer = 0
					end if
				end if
				BoxCollisionTest(C_TOUCH, object.entityPos, -24, -24, 24, 22, 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.value12--
					if object.value12 == 0
						player.score += 1000
						object.value15 = 0
						object.value16 = 0
						object.animation = 4
						object.animationTimer = 0
						object.value14 = 1
						object.value1 = object.ypos
						object.state = 13
						object.value4 = 0
					else
						temp0 = 7
						temp0 -= object.value12
						GetTableValue(arrayPos0, temp0, MPZEggman_table52)
						arrayPos0 += object.entityPos
						arrayPos0++
						object[arrayPos0].state++
						if object[0].xpos < object[arrayPos0].xpos
							object[arrayPos0].xvel = -0x8000
							object[arrayPos0].direction = FLIP_NONE
						else
							object[arrayPos0].xvel = 0x8000
							object[arrayPos0].direction = FLIP_X
						end if
						object[arrayPos0].yvel = -0x40000
						object[arrayPos0].priority = PRIORITY_ACTIVE
						object.value20++
						object.xvel = 0
						object.yvel = -0x18000
						object.state = 8
						object.animation = 3
						object.animationTimer = 0
						object.value13 = 64
						PlaySfx(SfxName[Boss Hit], 0)
					end if
				end if
			end if
		next
	end if
	if object.state != 17
		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.value14 != 0
		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
	switch object.animation
	case 1
		temp0 = object.animationTimer
		temp0 >>= 3
		object.animationTimer++
		object.animationTimer &= 15
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 2
		temp0 = object.animationTimer
		temp0 &= 15
		temp0 >>= 3
		temp0 += 2
		object.animationTimer++
		if object.animationTimer == 50
			object.animationTimer = 0
			if object[0].animation != ANI_DYING
				object.animation = 1
			end if
		end if
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 3
		temp0 = object.animationTimer
		temp0 &= 1
		temp0 += 2
		object.animationTimer++
		if object.animationTimer == 50
			object.animationTimer = 0
			object.animation = 1
		end if
		DrawSpriteFX(4, FX_FLIP, object.xpos, object.ypos)
		break
	case 4
		temp0 = object.animationTimer
		temp0 >>= 5
		temp0 += 4
		temp0 %= 5
		object.animationTimer++
		object.animationTimer &= 63
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 5
		DrawSpriteFX(5, FX_FLIP, object.xpos, object.ypos)
		break
	case 6
		temp0 = object.animationTimer
		temp0 >>= 3
		object.animationTimer++
		object.animationTimer &= 15
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		temp0 = object.animationTimer
		temp0 >>= 2
		temp0 &= 1
		if temp0 == 1
			DrawSpriteFX(16, FX_FLIP, object.xpos, object.ypos)
		end if
	end switch
	if object.animation > 0
		DrawSpriteFX(6, FX_FLIP, object.xpos, object.ypos)
		DrawSpriteFX(7, FX_FLIP, object.xpos, object.ypos)
	end if
	switch object.value15
	case 1
		GetTableValue(temp0, object.value16, MPZEggman_table53)
		object.value16++
		if object.value16 == 6
			object.value16 = 0
		end if
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 2
		temp0 = object.value16
		temp0 /= 3
		temp0 += 10
		object.value16++
		if object.value16 >= 18
			object.value16 = 0
			temp0 = 8
			object.value15 = 1
		end if
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	end switch
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone09")
	if checkResult == 1
		if stage.playerListPos == 2
			if saveRAM[284] == 1
				LoadSpriteSheet("MPZ/ObjectsRobo.gif")
			else
				LoadSpriteSheet("MPZ/Objects.gif")
			end if
		else
			LoadSpriteSheet("MPZ/Objects.gif")
		end if
		SpriteFrame(-28, -24, 44, 16, 195, 151)
		SpriteFrame(-28, -24, 44, 16, 195, 168)
		SpriteFrame(-28, -24, 44, 16, 240, 155)
		SpriteFrame(-28, -24, 44, 16, 240, 172)
		SpriteFrame(-28, -24, 44, 16, 285, 157)
		SpriteFrame(-28, -24, 44, 16, 285, 174)
		SpriteFrame(-24, -28, 56, 49, 220, 206)
		SpriteFrame(-32, -8, 8, 23, 190, 69)
		SpriteFrame(31, 4, 12, 8, 289, 125)
		SpriteFrame(30, 2, 14, 12, 302, 125)
		SpriteFrame(31, -2, 22, 14, 285, 192)
		SpriteFrame(31, -9, 32, 26, 67, 75)
		SpriteFrame(31, -2, 22, 14, 285, 192)
		SpriteFrame(31, -9, 32, 26, 67, 75)
		SpriteFrame(31, 4, 12, 8, 289, 125)
		SpriteFrame(30, 2, 14, 12, 302, 125)
		SpriteFrame(-3, -28, 11, 8, 332, 144)
		SpriteFrame(0, 0, 1, 1, 332, 144)
		MPZEggman_value42 = TypeName[Eggman Laser]
		CheckEqual(options.vsMode, 1)
		temp0 = checkResult
		CheckEqual(options.gameMode, 2)
		temp0 |= checkResult
		if temp0 == 1
			foreach (TypeName[Eggman], arrayPos0, ALL_ENTITIES)			
				temp0 = 11097
				temp0 <<= 16
				temp1 = 1190
				temp1 <<= 16
				ResetObjectEntity(arrayPos0, TypeName[Sign Post], 0, temp0, temp1)
				object[arrayPos0].frame = 1
				object[arrayPos0].value1 = 16
				object[arrayPos0].priority = PRIORITY_XBOUNDS
				if options.vsMode == 1
					object[arrayPos0].state = 3
				end if
			next
		end if
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-28, -28, 60, 20, 1, 1)
		SpriteFrame(-28, -28, 60, 20, 62, 1)
		SpriteFrame(-28, -28, 60, 20, 1, 22)
		SpriteFrame(-28, -28, 60, 20, 62, 22)
		SpriteFrame(-28, -28, 60, 20, 1, 43)
		SpriteFrame(-28, -28, 60, 20, 62, 43)
		SpriteFrame(-24, -28, 56, 49, 255, 206)
		SpriteFrame(-32, -8, 8, 23, 316, 232)
		SpriteFrame(31, 4, 12, 8, 57, 90)
		SpriteFrame(30, 2, 14, 12, 57, 99)
		SpriteFrame(31, -2, 22, 14, 34, 97)
		SpriteFrame(31, -9, 32, 26, 72, 86)
		SpriteFrame(31, -2, 22, 14, 34, 97)
		SpriteFrame(31, -9, 32, 26, 72, 86)
		SpriteFrame(31, 4, 12, 8, 57, 90)
		SpriteFrame(30, 2, 14, 12, 57, 99)
		SpriteFrame(-3, -28, 11, 8, 45, 88)
		SpriteFrame(0, 0, 0, 0, 7, 2)
		MPZEggman_value42 = TypeName[Eggman Laser 2]
	end if
end event

event RSDKDraw
	DrawSprite(0)
end event

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