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

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

private alias object.value0  : object.timer
private alias object.value1  : object.drawPos.x
private alias object.value2  : object.solidBoxL
private alias object.value3  : object.solidBoxR
private alias object.value4  : object.pushBoxL
private alias object.value5  : object.pushBoxR
private alias object.value6  : object.beingPushed
private alias object.value7  : object.startPos.x
private alias object.value8  : object.startPos.y
private alias object.value9  : object.pushBoundary
private alias object.value10 : object.activePlayers
private alias object.value11 : object.activePriority
private alias object.value12 : object.storedPriority

private alias 0 : PUSHBLOCK_PUSHABLE
private alias 1 : PUSHBLOCK_MOVING
private alias 2 : PUSHBLOCK_FALLING
private alias 3 : PUSHBLOCK_INLAVA
private alias 4 : PUSHBLOCK_SINKING

// Player Aliases
private alias object.xpos : player.xpos
private alias object.ypos : player.ypos
private alias object.ixpos : player.ixpos
private alias object.xvel : player.xvel
private alias object.speed : player.speed
private alias object.gravity : player.gravity
private alias object.pushing : player.pushing
private alias object.left : player.left
private alias object.right : player.right
private alias object.collisionRight : player.collisionRight
private alias object.collisionLeft : player.collisionLeft

// Debug Mode Aliases
private alias object.value17 : debugMode.currentSelection

// Chained Crusher Aliases
private alias object.value1 : chainedCrusher.crusherType
private alias object.value7 : chainedCrusher.dontPlaySfx

private alias 0 : CHAINCRUSHER_TYPE_LARGE
private alias 1 : CHAINCRUSHER_TYPE_MED
private alias 2 : CHAINCRUSHER_TYPE_SMALL
private alias 3 : CHAINCRUSHER_TYPE_SMALL_ACTIVE

// Tile Flag Aliases
private alias 1 : TILE_LAVA

// Tile Info ID Aliases
private alias 8 : TILEINFO_ANGLEB

// Path ID Aliases
private alias 0 : PATH_A


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

reserve function PushBlock_DebugDraw
reserve function PushBlock_DebugSpawn
reserve function PushBlock_HandlePushBounds
reserve function PushBlock_HandleObjectCollisions
reserve function PushBlock_HandleTileCollisions

// ========================
// Static Values
// ========================

private value PushBlock_startDebugID = 0


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

private function PushBlock_DebugDraw
	temp0 = debugMode[0].currentSelection
	temp0 -= PushBlock_startDebugID
	DrawSprite(temp0)
end function


private function PushBlock_DebugSpawn
	CreateTempObject(TypeName[Push Block], 0, object.xpos, object.ypos)
	object[tempObjectPos].beingPushed = false
	object[tempObjectPos].frame = debugMode[0].currentSelection
	object[tempObjectPos].frame -= PushBlock_startDebugID
	if object[tempObjectPos].frame == 0
		object[tempObjectPos].solidBoxL = -16
		object[tempObjectPos].solidBoxR = 16
		object[tempObjectPos].pushBoxL = -17
		object[tempObjectPos].pushBoxR = 17
	else
		object[tempObjectPos].solidBoxL = -64
		object[tempObjectPos].solidBoxR = 64
		object[tempObjectPos].pushBoxL = -65
		object[tempObjectPos].pushBoxR = 65
		object[tempObjectPos].drawOrder = 5
	end if
end function


private function PushBlock_HandlePushBounds
	if object.floorSensorC == false
		if object.floorSensorL == false
			object.xvel = -0x40000
			object.pushBoundary = object.xpos
			object.pushBoundary &= 0xFFFF00000
			object.state++
		else
			if object.floorSensorR == false
				object.xvel = 0x40000
				object.pushBoundary = object.xpos
				object.pushBoundary &= 0xFFFF00000
				object.pushBoundary += 0x100000
				object.state++
			end if
		end if
	end if
end function


private function PushBlock_HandleObjectCollisions
	object.activePlayers = 0
	temp2 = 0
	object.xvel = 0

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		CheckEqual(options.attractMode, true)
		temp1 = checkResult
		CheckEqual(currentPlayer, 1)
		temp1 &= checkResult

		if player[currentPlayer].gravity == GRAVITY_GROUND
			if player[currentPlayer].xvel < 0
				BoxCollisionTest(C_TOUCH, object.entityPos, 0, -12, object.pushBoxR, 12, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
				if checkResult == true
					if player[currentPlayer].left == true
						if temp1 == false
							SetBit(object.activePlayers, temp2, true)
							object.xvel -= 0x6000
							object.beingPushed = true
						end if
					end if

					player[currentPlayer].xpos = player[currentPlayer].collisionRight
					player[currentPlayer].xpos += object.solidBoxR
					player[currentPlayer].xpos <<= 16
					player[currentPlayer].xpos += object.drawPos.x
					player[currentPlayer].xvel = 0
					player[currentPlayer].speed = 0
					player[currentPlayer].pushing = 2
				end if
			else
				if player[currentPlayer].xvel > 0
					BoxCollisionTest(C_TOUCH, object.entityPos, object.pushBoxL, -12, 0, 12, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
					if checkResult == true
						if player[currentPlayer].right == true
							if temp1 == false
								SetBit(object.activePlayers, temp2, true)
								object.xvel += 0x6000
								object.beingPushed = true
							end if
						end if

						player[currentPlayer].xpos = player[currentPlayer].collisionLeft
						player[currentPlayer].xpos += object.solidBoxL
						player[currentPlayer].xpos <<= 16
						player[currentPlayer].xpos += object.drawPos.x
						player[currentPlayer].xvel = 0
						player[currentPlayer].speed = 0
						player[currentPlayer].pushing = 2
					end if
				else
					BoxCollisionTest(C_SOLID, object.entityPos, object.solidBoxL, -16, object.solidBoxR, 16, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
				end if
			end if
		else
			BoxCollisionTest(C_SOLID, object.entityPos, object.solidBoxL, -16, object.solidBoxR, 16, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		end if

		temp2++
	next

	if object.xvel != 0
		object.xpos += object.xvel
		if object.xvel < 0
			ObjectTileCollision(CSIDE_RWALL, object.solidBoxL, 0, PATH_A)
		else
			ObjectTileCollision(CSIDE_LWALL, object.solidBoxR, 0, PATH_A)
		end if
		CallFunction(PushBlock_HandlePushBounds)

		temp0 = oscillation
		temp0 &= 31
		if temp0 < 3
			PlaySfx(SfxName[Push Block], false)
		end if

		temp0 = object.xpos
		temp0 &= 0xFFFF0000
		if object.drawPos.x != temp0
			// No code?
			// This check statement exists in the scripts... but there's nothing that actually happens
		end if
	end if

	object.drawPos.x = object.xpos
	object.drawPos.x &= 0xFFFF0000
	temp2 = 0
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		GetBit(temp0, object.activePlayers, temp2)
		if temp0 == true
			if player[currentPlayer].xpos < object.xpos
				player[currentPlayer].ixpos = player[currentPlayer].collisionRight
				temp1 = object.solidBoxL
			else
				player[currentPlayer].ixpos = player[currentPlayer].collisionLeft
				temp1 = object.solidBoxR
			end if
			
			FlipSign(player[currentPlayer].xpos)
			temp1 <<= 16
			player[currentPlayer].xpos += temp1
			player[currentPlayer].xpos += object.xpos
		else
			BoxCollisionTest(C_SOLID, object.entityPos, object.solidBoxL, -16, object.solidBoxR, 16, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		end if

		temp2++
	next
end function


private function PushBlock_HandleTileCollisions
	if object.beingPushed == true
		if object.gravity == GRAVITY_AIR
			object.yvel += 0x1800
			object.ypos += object.yvel
			temp0 = object.solidBoxL
			temp0 += 2
			ObjectTileCollision(CSIDE_FLOOR, temp0, 16, PATH_A)
			object.floorSensorL = checkResult
			object.gravity = checkResult

			ObjectTileCollision(CSIDE_FLOOR, 0, 16, PATH_A)
			object.floorSensorC = checkResult
			object.gravity |= checkResult

			temp0 = object.solidBoxR
			temp0 -= 2
			ObjectTileCollision(CSIDE_FLOOR, temp0, 16, PATH_A)
			object.floorSensorR = checkResult
			object.gravity |= checkResult
		else
			object.yvel = 0
			temp0 = object.solidBoxL
			temp0 += 2
			ObjectTileGrip(CSIDE_FLOOR, temp0, 16, PATH_A)

			object.floorSensorL = checkResult
			object.gravity = checkResult
			ObjectTileGrip(CSIDE_FLOOR, 0, 16, PATH_A)

			object.floorSensorC = checkResult
			object.gravity |= checkResult

			temp0 = object.solidBoxR
			temp0 -= 2
			ObjectTileGrip(CSIDE_FLOOR, temp0, 16, PATH_A)
			object.floorSensorR = checkResult
			object.gravity |= checkResult
		end if
		object.gravity ^= GRAVITY_AIR
	else
		object.floorSensorL = true
		object.floorSensorC = true
		object.floorSensorR = true
	end if

	foreach (TypeName[Chained Crusher], arrayPos0, ACTIVE_ENTITIES)
		switch chainedCrusher[arrayPos0].crusherType
		case CHAINCRUSHER_TYPE_LARGE
			BoxCollisionTest(C_PLATFORM, arrayPos0, -56, -12, 56, 12, object.entityPos, object.solidBoxL, -16, object.solidBoxR, 16)
			object.beingPushed |= checkResult
			ObjectTileCollision(CSIDE_ROOF, 0, -17, PATH_A)
			if checkResult == true
				object.ypos -= 0x10000
				object[arrayPos0].ypos = object.ypos
				object[arrayPos0].ypos += 0x1D0000
				chainedCrusher[arrayPos0].dontPlaySfx = true
			end if
			break

		case CHAINCRUSHER_TYPE_MED
			BoxCollisionTest(C_PLATFORM, arrayPos0, -48, -12, 48, 12, object.entityPos, object.solidBoxL, -16, object.solidBoxR, 16)
			object.beingPushed |= checkResult
			ObjectTileCollision(CSIDE_ROOF, 0, -17, PATH_A)
			if checkResult == true
				object.ypos -= 0x10000
				object[arrayPos0].ypos = object.ypos
				object[arrayPos0].ypos += 0x1D0000
				chainedCrusher[arrayPos0].dontPlaySfx = true
			end if
			break

		case CHAINCRUSHER_TYPE_SMALL
			BoxCollisionTest(C_PLATFORM, arrayPos0, -16, -12, 16, 12, object.entityPos, object.solidBoxL, -16, object.solidBoxR, 16)
			object.beingPushed |= checkResult
			ObjectTileCollision(CSIDE_ROOF, 0, -17, PATH_A)
			if checkResult == true
				object.ypos -= 0x10000
				object[arrayPos0].ypos = object.ypos
				object[arrayPos0].ypos += 0x1D0000
				chainedCrusher[arrayPos0].dontPlaySfx = true
			end if
			break
			
		end switch
	next
end function


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

event ObjectUpdate
	temp6 = object.xpos
	temp7 = object.ypos
	switch object.state
	case PUSHBLOCK_PUSHABLE
		object.priority = object.activePriority
		CallFunction(PushBlock_HandleObjectCollisions)
		CallFunction(PushBlock_HandleTileCollisions)
		break

	case PUSHBLOCK_MOVING
		object.xpos += object.xvel
		if object.xvel > 0
			if object.xpos >= object.pushBoundary
				object.xpos = object.pushBoundary
				object.gravity = GRAVITY_AIR
				object.state = PUSHBLOCK_FALLING
			end if
		else
			if object.xpos <= object.pushBoundary
				object.xpos = object.pushBoundary
				object.gravity = GRAVITY_AIR
				object.state = PUSHBLOCK_FALLING
			end if
		end if
		break

	case PUSHBLOCK_FALLING
		CallFunction(PushBlock_HandleTileCollisions)
		if object.gravity == GRAVITY_GROUND
			temp1 = object.iypos
			temp1 += 12
			Get16x16TileInfo(temp0, object.ixpos, temp1, TILEINFO_ANGLEB)
			if temp0 == TILE_LAVA
				object.state = PUSHBLOCK_INLAVA
				if object.xvel > 0
					object.xvel = 0x8000
				else
					object.xvel = -0x8000
				end if
			else
				object.state = PUSHBLOCK_PUSHABLE
			end if
		end if
		break

	case PUSHBLOCK_INLAVA
		temp0 = object.xpos
		temp0 -= object.drawPos.x
		temp0 += object.xvel
		temp0 &= 0xFFFF0000
		if object.gravity == GRAVITY_AIR
			temp1 = object.ypos
			temp1 &= 0xFFFF0000
			object.yvel += 0x1800
			object.ypos += object.yvel
			ObjectTileCollision(0, 0, 16, PATH_A)
			if checkResult == true
				object.yvel = 0
				object.gravity = GRAVITY_GROUND
			end if
			temp2 = object.ypos
			temp2 &= 0xFFFF0000
			temp2 -= temp1
			temp3 = object.ypos
			object.ypos = temp1
		else
			temp2 = 0
		end if

		object.xpos += object.xvel
		if temp2 != 0
			object.ypos = temp3
		end if

		if object.xvel > 0
			ObjectTileCollision(1, 16, 12, PATH_A)
			if checkResult == true
				object.xvel = 0
				object.timer = 0
				object.state = PUSHBLOCK_SINKING
			end if
		else
			ObjectTileCollision(2, -16, 12, PATH_A)
			if checkResult == true
				object.xvel = 0
				object.timer = 0
				object.state = PUSHBLOCK_SINKING
			end if
		end if
		break

	case PUSHBLOCK_SINKING
		if object.timer < 320
			object.timer++
			temp0 = object.timer
			temp0 &= 7
			if temp0 == 0
				object.ypos += 0x10000
			end if
		end if
		break
		
	end switch

	temp0 = object.xpos
	temp1 = object.ypos
	temp4 = object.xpos
	temp4 &= 0xFFFF0000
	temp5 = object.ypos
	temp5 &= 0xFFFF0000
	object.xpos = temp6
	object.ypos = temp7
	temp6 &= 0xFFFF0000
	temp7 &= 0xFFFF0000
	temp4 -= temp6
	temp5 -= temp7

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		CheckNotEqual(player[currentPlayer].pushing, false)
		temp2 = checkResult
		CheckEqual(object.state, PUSHBLOCK_PUSHABLE)
		temp2 &= checkResult
		if temp2 == false
			BoxCollisionTest(C_SOLID, object.entityPos, object.solidBoxL, -16, object.solidBoxR, 16, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == COL_TOP
				player[currentPlayer].xpos += temp4
				player[currentPlayer].ypos += temp5
			end if
		end if
	next

	object.xpos = temp0
	object.ypos = temp1
	object.drawPos.x = object.xpos
	object.drawPos.x &= 0xFFFF0000

	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.yvel = 0
			object.beingPushed = false
			object.priority = object.storedPriority
			object.state = PUSHBLOCK_PUSHABLE
		else
			object.xpos = temp0
			object.ypos = temp1
		end if
	end if
end event


event ObjectDraw
	DrawSpriteXY(object.frame, object.drawPos.x, object.ypos)
end event


event ObjectStartup
	LoadSpriteSheet("MZ/Objects.gif")
	SpriteFrame(-16, -16, 32, 32, 159, 322)
	SpriteFrame(-64, -16, 128, 32, 159, 322)

	foreach (TypeName[Push Block], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].frame = object[arrayPos0].propertyValue
		object[arrayPos0].frame &= 1
		if object[arrayPos0].frame == 0
			object[arrayPos0].activepriority = PRIORITY_ACTIVE_SMALL
			object[arrayPos0].storedpriority = PRIORITY_BOUNDS_SMALL
			object[arrayPos0].priority = PRIORITY_BOUNDS_SMALL
		else
			object[arrayPos0].activepriority = PRIORITY_ACTIVE
			object[arrayPos0].storedpriority = PRIORITY_BOUNDS
			object[arrayPos0].priority = PRIORITY_BOUNDS
		end if

		object[arrayPos0].startPos.x = object[arrayPos0].xpos
		object[arrayPos0].startPos.y = object[arrayPos0].ypos
		object[arrayPos0].beingPushed = false

		if object[arrayPos0].frame == 0
			object[arrayPos0].solidBoxL = -16
			object[arrayPos0].solidBoxR = 16
			object[arrayPos0].pushBoxL = -17
			object[arrayPos0].pushBoxR = 17
		else
			object[arrayPos0].solidBoxL = -64
			object[arrayPos0].solidBoxR = 64
			object[arrayPos0].pushBoxL = -65
			object[arrayPos0].pushBoxR = 65
			object[arrayPos0].drawOrder = 5
		end if
	next

	temp0 = 0
	PushBlock_startDebugID = DebugMode_ObjCount
	while temp0 < 2
		SetTableValue(TypeName[Push Block], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(PushBlock_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(PushBlock_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop
end event


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

event RSDKEdit
	if editor.returnVariable == true
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			checkResult = object.propertyValue
			checkResult &= 1
			break
			
		case 0 // frameID
			checkResult = object.propertyValue
			checkResult &= 1
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			object.propertyValue &= 1
			break
			
		case 0 // frameID
			object.propertyValue = editor.variableValue
			object.propertyValue &= 1
			break
			
		end switch
	end if
end event


event RSDKDraw
	GetBit(object.frame, object.propertyValue, 0)
	DrawSprite(object.frame)
end event


event RSDKLoad
	LoadSpriteSheet("MZ/Objects.gif")
	SpriteFrame(-16, -16, 32, 32, 159, 322)
	SpriteFrame(-64, -16, 128, 32, 159, 322)
	
	AddEditorVariable("frameID")
	SetActiveVariable("frameID")
	AddEnumVariable("Small", 0)
	AddEnumVariable("Long", 1)
end event
