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

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

private alias object.value0  : object.bodyPosition1.x
private alias object.value1  : object.bodyPosition1.y
private alias object.value2  : object.bodyDirection1
private alias object.value3  : object.bodyFrame1

private alias object.value4  : object.bodyPosition2.x
private alias object.value5  : object.bodyPosition2.y
private alias object.value6  : object.bodyDirection2
private alias object.value7  : object.bodyFrame2

private alias object.value8  : object.bodyPosition3.x
private alias object.value9  : object.bodyPosition3.y
private alias object.value10 : object.bodyDirection3
private alias object.value11 : object.bodyFrame3

private alias object.value12 : object.startPos.x
private alias object.value13 : object.startPos.y

private alias object.value14 : object.timer

private alias object.value15 : object.xBoundary1
private alias object.value16 : object.xBoundary2

private alias object.value17 : object.bodyPositionRevive.x
private alias object.value18 : object.bodyPositionRevive.y

private alias 0 : CATERKILLER_SETUP
private alias 1 : CATERKILLER_CONTRACT
private alias 2 : CATERKILLER_LIFTHEAD
private alias 3 : CATERKILLER_UNCONTRACT
private alias 4 : CATERKILLER_LOWERHEAD
private alias 5 : CATERKILLER_SPLITBODYPART
private alias 6 : CATERKILLER_REVIVE

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

// Revive Enemy Aliases
private alias object.value47 : object.backupType
private alias object.value46 : object.backupState
private alias object.value44 : object.backupvalueA
private alias object.value43 : object.backupvalueB
private alias object.value0  : object.target

// Mission Number Aliases
private alias 8 : MISSIONNO_MERCY

// Tile Info ID Aliases
private alias 1 : TILEINFO_DIRECTION
private alias 6 : TILEINFO_ANGLEA

// Path ID Aliases
private alias 0 : PATH_A


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

reserve function Caterkiller_SetupBody
reserve function Caterkiller_DebugDraw
reserve function Caterkiller_DebugSpawn
reserve function Caterkiller_CheckPlayerCollisions
reserve function Caterkiller_CheckTileAngle
reserve function Caterkiller_CheckTileCollisions


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

private function Caterkiller_SetupBody
	object[arrayPos0].bodyPositionRevive.x = object[arrayPos0].bodyPosition1.x
	object[arrayPos0].bodyPositionRevive.y = object[arrayPos0].bodyPosition1.y
	
	object[arrayPos0].startPos.x = object[arrayPos0].xpos
	object[arrayPos0].startPos.y = object[arrayPos0].ypos

	temp0 = object[arrayPos0].xpos
	temp1 = 0xC0000
	if temp7 == FLIP_X
		FlipSign(temp1)
	end if
	object[arrayPos0].direction = temp7
	object[arrayPos0].frame = 0
	temp0 += temp1

	object[arrayPos0].bodyPosition1.x = temp0
	object[arrayPos0].bodyPosition1.y = object[arrayPos0].ypos
	object[arrayPos0].bodyDirection1 = object[arrayPos0].direction
	object[arrayPos0].bodyFrame1 = 0
	temp0 += temp1

	object[arrayPos0].bodyPosition2.x = temp0
	object[arrayPos0].bodyPosition2.y = object[arrayPos0].ypos
	object[arrayPos0].bodyDirection2 = object[arrayPos0].direction
	object[arrayPos0].bodyFrame2 = 0
	temp0 += temp1

	object[arrayPos0].bodyPosition3.x = temp0
	object[arrayPos0].bodyPosition3.y = object[arrayPos0].ypos
	object[arrayPos0].bodyDirection3 = object[arrayPos0].direction
	object[arrayPos0].bodyFrame3 = 0
	
	object[arrayPos0].state = CATERKILLER_SETUP
	object[arrayPos0].timer = 0
	
	object[arrayPos0].xBoundary1 = object[arrayPos0].xpos
	object[arrayPos0].xBoundary1 &= -0x200000
	object[arrayPos0].xBoundary2 = object[arrayPos0].xBoundary1
	if object[arrayPos0].bodyPositionRevive.y > 2
		temp0 = object[arrayPos0].bodyPositionRevive.y
		temp0 *= 0x200000
		object[arrayPos0].xBoundary2 += temp0
	else
		object[arrayPos0].xBoundary2 += 0x400000
	end if
	temp0 = object[arrayPos0].bodyPositionRevive.x
	temp0 *= 0x200000
	object[arrayPos0].xBoundary1 -= temp0
end function


private function Caterkiller_DebugDraw
	temp0 = object.xpos
	GetBit(temp1, object.direction, 0)
	if temp1 == FLIP_NONE
		temp0 += 0x240000
		temp1 = -0xC0000
	else
		temp0 -= 0x240000
		temp1 = 0xC0000
	end if

	temp2 = 0
	while temp2 < 3
		DrawSpriteFX(16, FX_FLIP, temp0, object.ypos)
		temp0 += temp1
		temp2++
	loop

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


private function Caterkiller_DebugSpawn
	temp0 = object.ixpos
	temp0 <<= 16
	temp1 = object.iypos
	temp1 <<= 16

	CreateTempObject(TypeName[Caterkiller], 0, temp0, temp1)
	arrayPos0 = object[tempObjectPos].entityPos
	temp7 = object.direction

	CallFunction(Caterkiller_SetupBody)
end function


private function Caterkiller_CheckPlayerCollisions
	temp7 = false
	temp5 = object.xpos
	temp6 = object.ypos
	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
			CreateTempObject(TypeName[Revive Enemy], object.propertyValue, object.startPos.x, object.startPos.y)
			object[tempObjectPos].priority = PRIORITY_ACTIVE
			object[tempObjectPos].target = object.entityPos
			object[tempObjectPos].backupType = object.type
			object[tempObjectPos].backupState = CATERKILLER_REVIVE
			object[tempObjectPos].backupvalueA = object.bodyPositionRevive.x
			object[tempObjectPos].backupvalueB = object.bodyPositionRevive.y
			CallFunction(Player_BadnikBreak)
		else
			object.xpos = object.bodyPosition1.x
			object.ypos = object.bodyPosition1.y
			BoxCollisionTest(C_TOUCH, object.entityPos, -8, -8, 8, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == true
				CallFunction(Player_Hit)
				temp7 = true
			else
				object.xpos = object.bodyPosition2.x
				object.ypos = object.bodyPosition2.y
				BoxCollisionTest(C_TOUCH, object.entityPos, -8, -8, 8, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
				if checkResult == true
					CallFunction(Player_Hit)
					temp7 = true
				else
					object.xpos = object.bodyPosition3.x
					object.ypos = object.bodyPosition3.y
					BoxCollisionTest(C_TOUCH, object.entityPos, -8, -8, 8, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
					if checkResult == true
						CallFunction(Player_Hit)
						temp7 = true
					end if
				end if
			end if
		end if

		object.xpos = temp5
		object.ypos = temp6
	next

	// Uh oh! we hit the body! time to explode!
	if temp7 == true
		temp0 = object.frame
		temp0 <<= 15
		object.ypos -= temp0

		CreateTempObject(TypeName[Caterkiller], 0, object.xpos, object.ypos)
		if object.direction == FLIP_NONE
			object[tempObjectPos].xvel = -0x20000
		else
			object[tempObjectPos].xvel = 0x20000
		end if
		object[tempObjectPos].yvel = -0x40000
		object[tempObjectPos].direction = object.direction
		object[tempObjectPos].frame = object.frame
		object[tempObjectPos].frame &= 8
		object[tempObjectPos].state = CATERKILLER_SPLITBODYPART
		object[tempObjectPos].priority = PRIORITY_ACTIVE_SMALL

		CreateTempObject(TypeName[Caterkiller], 0, object.bodyPosition1.x, object.bodyPosition1.y)
		if object.bodyDirection1 == FLIP_NONE
			object[tempObjectPos].xvel = -0x18000
		else
			object[tempObjectPos].xvel = 0x18000
		end if
		object[tempObjectPos].yvel 		= -0x40000
		object[tempObjectPos].direction = object.bodyDirection1
		object[tempObjectPos].frame 	= 16
		object[tempObjectPos].state 	= CATERKILLER_SPLITBODYPART
		object[tempObjectPos].priority 	= PRIORITY_ACTIVE_SMALL
		temp0 = object.bodyFrame2
		temp0 <<= 15
		object.bodyPosition2.y -= temp0
		object.frame -= temp0

		CreateTempObject(TypeName[Caterkiller], 0, object.bodyPosition2.x, object.bodyPosition2.y)
		if object.bodyDirection2 == FLIP_NONE
			object[tempObjectPos].xvel = 0x18000
		else
			object[tempObjectPos].xvel = -0x18000
		end if
		object[tempObjectPos].yvel 		= -0x40000
		object[tempObjectPos].direction = object.bodyDirection2
		object[tempObjectPos].frame 	= 16
		object[tempObjectPos].state 	= CATERKILLER_SPLITBODYPART
		object[tempObjectPos].priority 	= PRIORITY_ACTIVE_SMALL

		CreateTempObject(TypeName[Caterkiller], 0, object.bodyPosition3.x, object.bodyPosition3.y)
		if object.bodyDirection3 == FLIP_NONE
			object[tempObjectPos].xvel = 0x20000
		else
			object[tempObjectPos].xvel = -0x20000
		end if
		object[tempObjectPos].yvel 		= -0x40000
		object[tempObjectPos].direction = object.bodyDirection3
		object[tempObjectPos].frame 	= 16
		object[tempObjectPos].state 	= CATERKILLER_SPLITBODYPART
		object[tempObjectPos].priority 	= PRIORITY_ACTIVE_SMALL
		
		CreateTempObject(TypeName[Revive Enemy], object.propertyValue, object.startPos.x, object.startPos.y)
		object[tempObjectPos].priority = PRIORITY_ACTIVE
		object[tempObjectPos].target = object.entityPos
		object[tempObjectPos].backupType = object.type
		object[tempObjectPos].backupState = CATERKILLER_REVIVE
		object[tempObjectPos].backupvalueA = object.bodyPositionRevive.x
		object[tempObjectPos].backupvalueB = object.bodyPositionRevive.y

		ResetObjectEntity(object.entityPos, TypeName[Blank Object], 0, object.xpos, object.ypos)

		if game.playMode == BOOT_PLAYMODE_MISSION
			CallNativeFunction2(NotifyCallback, NOTIFY_KILL_ENEMY, temp0)
		end if
	end if
end function


private function Caterkiller_CheckTileAngle
	temp2 >>= 16
	temp3 >>= 16
	temp3 += 8
	Get16x16TileInfo(temp5, temp2, temp3, TILEINFO_ANGLEA)
	Get16x16TileInfo(temp6, temp2, temp3, TILEINFO_DIRECTION)
	switch temp6
	case FLIP_NONE
		temp5 &= 255
		break

	case FLIP_X
		temp5 &= 255
		temp6 = temp5
		temp5 = 256
		temp5 -= temp6
		temp5 &= 255
		break

	case FLIP_Y
		temp5 >>= 24
		temp5 += 128
		temp6 = temp5
		temp5 = 256
		temp5 -= temp6
		temp5 &= 255
		break

	case FLIP_XY
		temp5 >>= 24
		temp5 += 128
		temp5 &= 255
		break
		
	end switch

	checkResult = false
	if temp4 == FLIP_NONE
		if temp5 < 128
			if temp5 > 24
				checkResult = true
			end if
		end if
	else
		if temp5 > 128
			if temp5 < 232
				checkResult = true
			end if
		end if
	end if
end function


private function Caterkiller_CheckTileCollisions
	if object.state != CATERKILLER_LIFTHEAD
		CheckLower(object.xpos, object.xBoundary1)
		temp2 = checkResult
		CheckGreater(object.xpos, object.xBoundary2)
		checkResult |= temp2
		if checkResult == true
			object.direction ^= 1
		end if
	end if
	
	CheckLower(object.bodyPosition1.x, object.xBoundary1)
	temp2 = checkResult
	CheckGreater(object.bodyPosition1.x, object.xBoundary2)
	checkResult |= temp2
	if checkResult == true
		object.bodyDirection1 = object.direction
	end if

	CheckLower(object.bodyPosition2.x, object.xBoundary1)
	temp2 = checkResult
	CheckGreater(object.bodyPosition2.x, object.xBoundary2)
	checkResult |= temp2
	if checkResult == true
		object.bodyDirection2 = object.direction
	end if

	if object.state != CATERKILLER_LOWERHEAD
		CheckLower(object.bodyPosition3.x, object.xBoundary1)
		temp2 = checkResult
		CheckGreater(object.bodyPosition3.x, object.xBoundary2)
		checkResult |= temp2
		if checkResult == true
			object.bodyDirection3 = object.direction
		end if
	end if
end function


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

event ObjectUpdate
	switch object.state
	case CATERKILLER_SETUP
		object.priority = PRIORITY_ACTIVE
		// [Fallthrough]
		
	case CATERKILLER_CONTRACT
		if object.timer == 0
			object.timer = 15
			object.state++
			object.frame = -1
			object.bodyFrame2 = -1
			// fallthrough....right.?
		else
			object.timer--
			CallFunction(Caterkiller_CheckPlayerCollisions)
			break //whats this doin here
		end if
		// [Fallthrough, I think]
	case CATERKILLER_LIFTHEAD
		if object.timer == 0
			object.timer = 7
			object.state++
		else
			object.timer--

			if object.bodyDirection1 == FLIP_NONE
				object.bodyPosition1.x -= 0x4000
			else
				object.bodyPosition1.x += 0x4000
			end if

			if object.bodyDirection2 == FLIP_NONE
				object.bodyPosition2.x -= 0x8000
			else
				object.bodyPosition2.x += 0x8000
			end if

			if object.bodyDirection3 == FLIP_NONE
				object.bodyPosition3.x -= 0xC000
			else
				object.bodyPosition3.x += 0xC000
			end if

			object.frame++
			object.bodyFrame2++
			CallFunction(Caterkiller_CheckTileCollisions)
			CallFunction(Caterkiller_CheckPlayerCollisions)
		end if
		break

	case CATERKILLER_UNCONTRACT
		if object.timer == 0
			object.timer = 15
			object.state++
			object.frame = 16
			object.bodyFrame2 = 16
		else
			object.timer--
			CallFunction(Caterkiller_CheckPlayerCollisions)
			break
		end if
		// [Fallthrough]
	case CATERKILLER_LOWERHEAD
		if object.timer == 0
			object.timer = 7
			object.state = CATERKILLER_CONTRACT
		else
			object.timer--
			if object.direction == FLIP_NONE
				object.xpos -= 0xC000
			else
				object.xpos += 0xC000
			end if
			
			if object.bodyDirection1 == FLIP_NONE
				object.bodyPosition1.x -= 0x8000
			else
				object.bodyPosition1.x += 0x8000
			end if
			
			if object.bodyDirection2 == FLIP_NONE
				object.bodyPosition2.x -= 0x4000
			else
				object.bodyPosition2.x += 0x4000
			end if
			
			object.frame--
			object.bodyFrame2--
			CallFunction(Caterkiller_CheckTileCollisions)
			CallFunction(Caterkiller_CheckPlayerCollisions)
		end if
		break

	case CATERKILLER_SPLITBODYPART
		object.xpos += object.xvel
		object.ypos += object.yvel
		object.yvel += 0x3800

		if object.yvel > 0
			ObjectTileCollision(CSIDE_FLOOR, 0, 8, PATH_A)
			if checkResult == true
				object.yvel = -0x40000
			end if
		end if

		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
				if object.frame < 16
					CallFunction(Player_BadnikBreak)
				else
					CallFunction(Player_Hit)
				end if
			end if
		next

		if object.outOfBounds == true
			object.type = TypeName[Blank Object]
		end if
		break
		
	case CATERKILLER_REVIVE
		object.startPos.x = object.xpos
		object.startPos.y = object.ypos
		object.bodyPosition1.x = object.backupvalueA
		object.bodyPosition1.y = object.backupvalueB
		arrayPos0 = object.entityPos
		temp7 = object.propertyValue
		CallFunction(Caterkiller_SetupBody)
		object.state = CATERKILLER_SETUP
		break
		
	end switch

	if object.state != CATERKILLER_SPLITBODYPART
		if object.outOfBounds == true
			temp0 = object.xpos
			temp1 = object.ypos
			object.xpos = object.startPos.x
			object.ypos = object.startPos.y
			if object.outOfBounds == true
				arrayPos0 = object.entityPos
				temp7 = object[arrayPos0].propertyValue
				object.bodyPosition1.x = object.bodyPositionRevive.x
				object.bodyPosition1.y = object.bodyPositionRevive.y
				CallFunction(Caterkiller_SetupBody)
			else
				object.xpos = temp0
				object.ypos = temp1
			end if
		end if
	end if
end event


event ObjectDraw
	if object.state < CATERKILLER_SPLITBODYPART
		temp0 = object.direction
		object.direction = object.bodyDirection3
		temp1 = object.bodyFrame3
		temp1 >>= 1
		temp1 += 16
		DrawSpriteFX(temp1, FX_FLIP, object.bodyPosition3.x, object.bodyPosition3.y)

		object.direction = object.bodyDirection2
		temp1 = object.bodyFrame2
		temp1 >>= 1
		temp1 += 16
		DrawSpriteFX(temp1, FX_FLIP, object.bodyPosition2.x, object.bodyPosition2.y)

		object.direction = object.bodyDirection1
		temp1 = object.bodyFrame1
		temp1 >>= 1
		temp1 += 16
		DrawSpriteFX(temp1, FX_FLIP, object.bodyPosition1.x, object.bodyPosition1.y)

		object.direction = temp0
		temp0 = object.frame
		temp0 >>= 1
		CheckEqual(object.state, CATERKILLER_LIFTHEAD)
		temp1 = checkResult
		CheckEqual(object.state, CATERKILLER_UNCONTRACT)
		temp1 |= checkResult
		if temp1 == true
			temp0 += 8
		end if
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
	else
		DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
	end if
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone02")
	if checkResult == true
		LoadSpriteSheet("MZ/Objects.gif")
		SpriteFrame(-8, -14, 16, 22, 18, 81)
		SpriteFrame(-8, -15, 16, 22, 18, 81)
		SpriteFrame(-8, -16, 16, 22, 18, 81)
		SpriteFrame(-8, -17, 16, 22, 18, 81)
		SpriteFrame(-8, -18, 16, 22, 18, 81)
		SpriteFrame(-8, -19, 16, 22, 18, 81)
		SpriteFrame(-8, -20, 16, 22, 18, 81)
		SpriteFrame(-8, -21, 16, 22, 18, 81)
		SpriteFrame(-8, -14, 16, 24, 1, 81)
		SpriteFrame(-8, -15, 16, 24, 1, 81)
		SpriteFrame(-8, -16, 16, 24, 1, 81)
		SpriteFrame(-8, -17, 16, 24, 1, 81)
		SpriteFrame(-8, -18, 16, 24, 1, 81)
		SpriteFrame(-8, -19, 16, 24, 1, 81)
		SpriteFrame(-8, -20, 16, 24, 1, 81)
		SpriteFrame(-8, -21, 16, 24, 1, 81)
		SpriteFrame(-8, -8, 16, 16, 35, 81)
		SpriteFrame(-8, -9, 16, 16, 35, 81)
		SpriteFrame(-8, -10, 16, 16, 35, 81)
		SpriteFrame(-8, -11, 16, 16, 35, 81)
		SpriteFrame(-8, -12, 16, 16, 35, 81)
		SpriteFrame(-8, -13, 16, 16, 35, 81)
		SpriteFrame(-8, -14, 16, 16, 35, 81)
		SpriteFrame(-8, -15, 16, 16, 35, 81)
	end if

	CheckCurrentStageFolder("Zone03")
	if checkResult == true
		LoadSpriteSheet("SYZ/Objects.gif")
		SpriteFrame(-8, -14, 16, 22, 98, 98)
		SpriteFrame(-8, -15, 16, 22, 98, 98)
		SpriteFrame(-8, -16, 16, 22, 98, 98)
		SpriteFrame(-8, -17, 16, 22, 98, 98)
		SpriteFrame(-8, -18, 16, 22, 98, 98)
		SpriteFrame(-8, -19, 16, 22, 98, 98)
		SpriteFrame(-8, -20, 16, 22, 98, 98)
		SpriteFrame(-8, -21, 16, 22, 98, 98)
		SpriteFrame(-8, -14, 16, 24, 81, 98)
		SpriteFrame(-8, -15, 16, 24, 81, 98)
		SpriteFrame(-8, -16, 16, 24, 81, 98)
		SpriteFrame(-8, -17, 16, 24, 81, 98)
		SpriteFrame(-8, -18, 16, 24, 81, 98)
		SpriteFrame(-8, -19, 16, 24, 81, 98)
		SpriteFrame(-8, -20, 16, 24, 81, 98)
		SpriteFrame(-8, -21, 16, 24, 81, 98)
		SpriteFrame(-8, -8, 16, 16, 98, 121)
		SpriteFrame(-8, -9, 16, 16, 98, 121)
		SpriteFrame(-8, -10, 16, 16, 98, 121)
		SpriteFrame(-8, -11, 16, 16, 98, 121)
		SpriteFrame(-8, -12, 16, 16, 98, 121)
		SpriteFrame(-8, -13, 16, 16, 98, 121)
		SpriteFrame(-8, -14, 16, 16, 98, 121)
		SpriteFrame(-8, -15, 16, 16, 98, 121)
	end if

	CheckCurrentStageFolder("Zone06")
	if checkResult == true
		LoadSpriteSheet("SBZ/Objects.gif")
		SpriteFrame(-8, -14, 16, 22, 75, 26)
		SpriteFrame(-8, -15, 16, 22, 75, 26)
		SpriteFrame(-8, -16, 16, 22, 75, 26)
		SpriteFrame(-8, -17, 16, 22, 75, 26)
		SpriteFrame(-8, -18, 16, 22, 75, 26)
		SpriteFrame(-8, -19, 16, 22, 75, 26)
		SpriteFrame(-8, -20, 16, 22, 75, 26)
		SpriteFrame(-8, -21, 16, 22, 75, 26)
		SpriteFrame(-8, -14, 16, 24, 75, 1)
		SpriteFrame(-8, -15, 16, 24, 75, 1)
		SpriteFrame(-8, -16, 16, 24, 75, 1)
		SpriteFrame(-8, -17, 16, 24, 75, 1)
		SpriteFrame(-8, -18, 16, 24, 75, 1)
		SpriteFrame(-8, -19, 16, 24, 75, 1)
		SpriteFrame(-8, -20, 16, 24, 75, 1)
		SpriteFrame(-8, -21, 16, 24, 75, 1)
		SpriteFrame(-8, -8, 16, 16, 75, 49)
		SpriteFrame(-8, -9, 16, 16, 75, 49)
		SpriteFrame(-8, -10, 16, 16, 75, 49)
		SpriteFrame(-8, -11, 16, 16, 75, 49)
		SpriteFrame(-8, -12, 16, 16, 75, 49)
		SpriteFrame(-8, -13, 16, 16, 75, 49)
		SpriteFrame(-8, -14, 16, 16, 75, 49)
		SpriteFrame(-8, -15, 16, 16, 75, 49)
	end if

	CheckCurrentStageFolder("Zone07")
	if checkResult == true
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-8, -14, 16, 22, 1, 68)
		SpriteFrame(-8, -15, 16, 22, 1, 68)
		SpriteFrame(-8, -16, 16, 22, 1, 68)
		SpriteFrame(-8, -17, 16, 22, 1, 68)
		SpriteFrame(-8, -18, 16, 22, 1, 68)
		SpriteFrame(-8, -19, 16, 22, 1, 68)
		SpriteFrame(-8, -20, 16, 22, 1, 68)
		SpriteFrame(-8, -21, 16, 22, 1, 68)
		SpriteFrame(-8, -14, 16, 24, 18, 68)
		SpriteFrame(-8, -15, 16, 24, 18, 68)
		SpriteFrame(-8, -16, 16, 24, 18, 68)
		SpriteFrame(-8, -17, 16, 24, 18, 68)
		SpriteFrame(-8, -18, 16, 24, 18, 68)
		SpriteFrame(-8, -19, 16, 24, 18, 68)
		SpriteFrame(-8, -20, 16, 24, 18, 68)
		SpriteFrame(-8, -21, 16, 24, 18, 68)
		SpriteFrame(-8, -8, 16, 16, 35, 68)
		SpriteFrame(-8, -9, 16, 16, 35, 68)
		SpriteFrame(-8, -10, 16, 16, 35, 68)
		SpriteFrame(-8, -11, 16, 16, 35, 68)
		SpriteFrame(-8, -12, 16, 16, 35, 68)
		SpriteFrame(-8, -13, 16, 16, 35, 68)
		SpriteFrame(-8, -14, 16, 16, 35, 68)
		SpriteFrame(-8, -15, 16, 16, 35, 68)
	end if

	foreach (TypeName[Caterkiller], arrayPos0, ALL_ENTITIES)
		temp7 = object[arrayPos0].propertyValue
		CallFunction(Caterkiller_SetupBody)
	next

	SetTableValue(TypeName[Caterkiller], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(Caterkiller_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(Caterkiller_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
	arrayPos0 = object.entityPos
	temp7 = object.propertyValue
	CallFunction(Caterkiller_SetupBody)

	temp0 = object.direction
	object.direction = object.bodyDirection3
	temp1 = object.bodyFrame3
	temp1 >>= 1
	temp1 += 16
	DrawSpriteFX(temp1, FX_FLIP, object.bodyPosition3.x, object.bodyPosition3.y)

	object.direction = object.bodyDirection2
	temp1 = object.bodyFrame2
	temp1 >>= 1
	temp1 += 16
	DrawSpriteFX(temp1, FX_FLIP, object.bodyPosition2.x, object.bodyPosition2.y)

	object.direction = object.bodyDirection1
	temp1 = object.bodyFrame1
	temp1 >>= 1
	temp1 += 16
	DrawSpriteFX(temp1, FX_FLIP, object.bodyPosition1.x, object.bodyPosition1.y)

	object.direction = temp0
	temp0 = object.frame
	temp0 >>= 1
	DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone02")
	if checkResult == true
		LoadSpriteSheet("MZ/Objects.gif")
		SpriteFrame(-8, -14, 16, 22, 18, 81)
		SpriteFrame(-8, -15, 16, 22, 18, 81)
		SpriteFrame(-8, -16, 16, 22, 18, 81)
		SpriteFrame(-8, -17, 16, 22, 18, 81)
		SpriteFrame(-8, -18, 16, 22, 18, 81)
		SpriteFrame(-8, -19, 16, 22, 18, 81)
		SpriteFrame(-8, -20, 16, 22, 18, 81)
		SpriteFrame(-8, -21, 16, 22, 18, 81)
		SpriteFrame(-8, -14, 16, 24, 1, 81)
		SpriteFrame(-8, -15, 16, 24, 1, 81)
		SpriteFrame(-8, -16, 16, 24, 1, 81)
		SpriteFrame(-8, -17, 16, 24, 1, 81)
		SpriteFrame(-8, -18, 16, 24, 1, 81)
		SpriteFrame(-8, -19, 16, 24, 1, 81)
		SpriteFrame(-8, -20, 16, 24, 1, 81)
		SpriteFrame(-8, -21, 16, 24, 1, 81)
		SpriteFrame(-8, -8, 16, 16, 35, 81)
		SpriteFrame(-8, -9, 16, 16, 35, 81)
		SpriteFrame(-8, -10, 16, 16, 35, 81)
		SpriteFrame(-8, -11, 16, 16, 35, 81)
		SpriteFrame(-8, -12, 16, 16, 35, 81)
		SpriteFrame(-8, -13, 16, 16, 35, 81)
		SpriteFrame(-8, -14, 16, 16, 35, 81)
		SpriteFrame(-8, -15, 16, 16, 35, 81)
	end if
	CheckCurrentStageFolder("Zone03")
	if checkResult == true
		LoadSpriteSheet("SYZ/Objects.gif")
		SpriteFrame(-8, -14, 16, 22, 98, 98)
		SpriteFrame(-8, -15, 16, 22, 98, 98)
		SpriteFrame(-8, -16, 16, 22, 98, 98)
		SpriteFrame(-8, -17, 16, 22, 98, 98)
		SpriteFrame(-8, -18, 16, 22, 98, 98)
		SpriteFrame(-8, -19, 16, 22, 98, 98)
		SpriteFrame(-8, -20, 16, 22, 98, 98)
		SpriteFrame(-8, -21, 16, 22, 98, 98)
		SpriteFrame(-8, -14, 16, 24, 81, 98)
		SpriteFrame(-8, -15, 16, 24, 81, 98)
		SpriteFrame(-8, -16, 16, 24, 81, 98)
		SpriteFrame(-8, -17, 16, 24, 81, 98)
		SpriteFrame(-8, -18, 16, 24, 81, 98)
		SpriteFrame(-8, -19, 16, 24, 81, 98)
		SpriteFrame(-8, -20, 16, 24, 81, 98)
		SpriteFrame(-8, -21, 16, 24, 81, 98)
		SpriteFrame(-8, -8, 16, 16, 98, 121)
		SpriteFrame(-8, -9, 16, 16, 98, 121)
		SpriteFrame(-8, -10, 16, 16, 98, 121)
		SpriteFrame(-8, -11, 16, 16, 98, 121)
		SpriteFrame(-8, -12, 16, 16, 98, 121)
		SpriteFrame(-8, -13, 16, 16, 98, 121)
		SpriteFrame(-8, -14, 16, 16, 98, 121)
		SpriteFrame(-8, -15, 16, 16, 98, 121)
	end if
	CheckCurrentStageFolder("Zone06")
	if checkResult == true
		LoadSpriteSheet("SBZ/Objects.gif")
		SpriteFrame(-8, -14, 16, 22, 75, 26)
		SpriteFrame(-8, -15, 16, 22, 75, 26)
		SpriteFrame(-8, -16, 16, 22, 75, 26)
		SpriteFrame(-8, -17, 16, 22, 75, 26)
		SpriteFrame(-8, -18, 16, 22, 75, 26)
		SpriteFrame(-8, -19, 16, 22, 75, 26)
		SpriteFrame(-8, -20, 16, 22, 75, 26)
		SpriteFrame(-8, -21, 16, 22, 75, 26)
		SpriteFrame(-8, -14, 16, 24, 75, 1)
		SpriteFrame(-8, -15, 16, 24, 75, 1)
		SpriteFrame(-8, -16, 16, 24, 75, 1)
		SpriteFrame(-8, -17, 16, 24, 75, 1)
		SpriteFrame(-8, -18, 16, 24, 75, 1)
		SpriteFrame(-8, -19, 16, 24, 75, 1)
		SpriteFrame(-8, -20, 16, 24, 75, 1)
		SpriteFrame(-8, -21, 16, 24, 75, 1)
		SpriteFrame(-8, -8, 16, 16, 75, 49)
		SpriteFrame(-8, -9, 16, 16, 75, 49)
		SpriteFrame(-8, -10, 16, 16, 75, 49)
		SpriteFrame(-8, -11, 16, 16, 75, 49)
		SpriteFrame(-8, -12, 16, 16, 75, 49)
		SpriteFrame(-8, -13, 16, 16, 75, 49)
		SpriteFrame(-8, -14, 16, 16, 75, 49)
		SpriteFrame(-8, -15, 16, 16, 75, 49)
	end if
	CheckCurrentStageFolder("Zone07")
	if checkResult == true
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-8, -14, 16, 22, 1, 68)
		SpriteFrame(-8, -15, 16, 22, 1, 68)
		SpriteFrame(-8, -16, 16, 22, 1, 68)
		SpriteFrame(-8, -17, 16, 22, 1, 68)
		SpriteFrame(-8, -18, 16, 22, 1, 68)
		SpriteFrame(-8, -19, 16, 22, 1, 68)
		SpriteFrame(-8, -20, 16, 22, 1, 68)
		SpriteFrame(-8, -21, 16, 22, 1, 68)
		SpriteFrame(-8, -14, 16, 24, 18, 68)
		SpriteFrame(-8, -15, 16, 24, 18, 68)
		SpriteFrame(-8, -16, 16, 24, 18, 68)
		SpriteFrame(-8, -17, 16, 24, 18, 68)
		SpriteFrame(-8, -18, 16, 24, 18, 68)
		SpriteFrame(-8, -19, 16, 24, 18, 68)
		SpriteFrame(-8, -20, 16, 24, 18, 68)
		SpriteFrame(-8, -21, 16, 24, 18, 68)
		SpriteFrame(-8, -8, 16, 16, 35, 68)
		SpriteFrame(-8, -9, 16, 16, 35, 68)
		SpriteFrame(-8, -10, 16, 16, 35, 68)
		SpriteFrame(-8, -11, 16, 16, 35, 68)
		SpriteFrame(-8, -12, 16, 16, 35, 68)
		SpriteFrame(-8, -13, 16, 16, 35, 68)
		SpriteFrame(-8, -14, 16, 16, 35, 68)
		SpriteFrame(-8, -15, 16, 16, 35, 68)
	end if
	
	AddEditorVariable("direction")
	SetActiveVariable("direction")
	AddEnumVariable("Left", 0)
	AddEnumVariable("Right", 1)
	
	// TODO: val1 is set from editor?? bodyPosition1.y??? huh? (M102)
end event
