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

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

private alias object.value1 : object.prizePivot.y

// Frame IDs
private alias 0 : SLOTPRIZE_SONIC
private alias 1 : SLOTPRIZE_TAILS
private alias 2 : SLOTPRIZE_RING
private alias 3 : SLOTPRIZE_BAR
private alias 4 : SLOTPRIZE_JACKPOT
private alias 5 : SLOTPRIZE_EGGMAN
private alias 6 : SLOTPRIZE_KNUX  // Only Sonic OR Knux can appear at any one time
private alias 7 : SLOTPRIZE_SUPER // (Unused)


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

// Bool, the starting value is `true`
// (Aliases can't be used in static declarations)
private value SlotDisplay_needsSetup = 1


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

public table SlotDisplay_sheetY_L
	3 // SLOTPRIZE_BAR
	5 // SLOTPRIZE_EGGMAN
	2 // SLOTPRIZE_RING
	1 // SLOTPRIZE_TAILS
	0 // SLOTPRIZE_SONIC
	4 // SLOTPRIZE_JACKPOT
	1 // SLOTPRIZE_TAILS
	2 // SLOTPRIZE_RING
end table

public table SlotDisplay_sheetY_M
	3 // SLOTPRIZE_BAR
	5 // SLOTPRIZE_EGGMAN
	2 // SLOTPRIZE_RING
	1 // SLOTPRIZE_TAILS
	0 // SLOTPRIZE_SONIC
	4 // SLOTPRIZE_JACKPOT
	5 // SLOTPRIZE_EGGMAN
	0 // SLOTPRIZE_SONIC
end table

public table SlotDisplay_sheetY_R
	3 // SLOTPRIZE_BAR
	5 // SLOTPRIZE_EGGMAN
	2 // SLOTPRIZE_RING
	1 // SLOTPRIZE_TAILS
	0 // SLOTPRIZE_SONIC
	4 // SLOTPRIZE_JACKPOT
	1 // SLOTPRIZE_TAILS
	2 // SLOTPRIZE_RING
end table


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

event ObjectDraw
	// Slot L - Upper
	temp0 = SlotMachine_slotPrizeL
	temp0 >>= 5
	GetTableValue(temp1, temp0, SlotDisplay_sheetY_L)
	temp0 = SlotMachine_slotPrizeL
	temp0 &= 31
	temp2 = 32
	temp2 -= temp0
	object.prizePivot.y = temp0
	object.prizePivot.y -= 16
	EditFrame(0, -48, object.prizePivot.y, 32, temp2, 1, temp1)

	// Slot L - Lower
	temp0 = SlotMachine_slotPrizeL
	temp0 >>= 5
	temp0++
	temp0 &= 7
	GetTableValue(temp1, temp0, SlotDisplay_sheetY_L)
	temp0 = SlotMachine_slotPrizeL
	temp0 &= 31
	temp2 = 32
	temp2 -= temp0
	temp1 += temp2
	EditFrame(1, -48, -16, 32, temp0, 1, temp1)

	// Slot M - Upper
	temp0 = SlotMachine_slotPrizeM
	temp0 >>= 5
	GetTableValue(temp1, temp0, SlotDisplay_sheetY_M)
	temp0 = SlotMachine_slotPrizeM
	temp0 &= 31
	temp2 = 32
	temp2 -= temp0
	object.prizePivot.y = temp0
	object.prizePivot.y -= 16
	EditFrame(2, -16, object.prizePivot.y, 32, temp2, 1, temp1)

	// Slot M - Lower
	temp0 = SlotMachine_slotPrizeM
	temp0 >>= 5
	temp0++
	temp0 &= 7
	GetTableValue(temp1, temp0, SlotDisplay_sheetY_M)
	temp0 = SlotMachine_slotPrizeM
	temp0 &= 31
	temp2 = 32
	temp2 -= temp0
	temp1 += temp2
	EditFrame(3, -16, -16, 32, temp0, 1, temp1)

	// Slot R - Upper
	temp0 = SlotMachine_slotPrizeR
	temp0 >>= 5
	GetTableValue(temp1, temp0, SlotDisplay_sheetY_R)
	temp0 = SlotMachine_slotPrizeR
	temp0 &= 31
	temp2 = 32
	temp2 -= temp0
	object.prizePivot.y = temp0
	object.prizePivot.y -= 16
	EditFrame(4, 16, object.prizePivot.y, 32, temp2, 1, temp1)

	// Slot R - Lower
	temp0 = SlotMachine_slotPrizeR
	temp0 >>= 5
	temp0++
	temp0 &= 7
	GetTableValue(temp1, temp0, SlotDisplay_sheetY_R)
	temp0 = SlotMachine_slotPrizeR
	temp0 &= 31
	temp2 = 32
	temp2 -= temp0
	temp1 += temp2
	EditFrame(5, 16, -16, 32, temp0, 1, temp1)

	DrawSprite(0)
	DrawSprite(1)
	DrawSprite(2)
	DrawSprite(3)
	DrawSprite(4)
	DrawSprite(5)
end event


event ObjectStartup
	LoadSpriteSheet("CNZ/Objects.gif")
	SpriteFrame(-48, -16, 32, 32, 1, 256) // Slot L (Upper) - #0
	SpriteFrame(-16, -16, 32, 32, 1, 256) // Slot L (Lower) - #1
	SpriteFrame( 16, -16, 32, 32, 1, 256) // Slot M (Upper) - #2
	SpriteFrame(-48, -16, 32, 32, 1, 256) // Slot M (Lower) - #3
	SpriteFrame(-16, -16, 32, 32, 1, 256) // Slot R (Upper) - #4
	SpriteFrame( 16, -16, 32, 32, 1, 256) // Slot R (Lower) - #5

	if SlotDisplay_needsSetup == true
		SlotDisplay_needsSetup = false

		temp0 = 0
		while temp0 < 8
			GetTableValue(temp1, temp0, SlotDisplay_sheetY_L)
			GetTableValue(temp2, temp0, SlotDisplay_sheetY_M)
			GetTableValue(temp3, temp0, SlotDisplay_sheetY_R)
			if stage.playerListPos == PLAYER_KNUX_ALONE
				// Replace all Sonic's with Knuckles instead
				
				if temp1 == SLOTPRIZE_SONIC
					temp1 = SLOTPRIZE_KNUX
				end if

				if temp2 == SLOTPRIZE_SONIC
					temp2 = SLOTPRIZE_KNUX
				end if

				if temp3 == SLOTPRIZE_SONIC
					temp3 = SLOTPRIZE_KNUX
				end if
			end if

			temp1 <<= 5
			temp1 += 0x100

			temp2 <<= 5
			temp2 += 0x100

			temp3 <<= 5
			temp3 += 0x100

			SetTableValue(temp1, temp0, SlotDisplay_sheetY_L)
			SetTableValue(temp2, temp0, SlotDisplay_sheetY_M)
			SetTableValue(temp3, temp0, SlotDisplay_sheetY_R)

			temp0++
		loop
	end if
end event


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

event RSDKDraw
	DrawSprite(0)
	DrawSprite(1)
	DrawSprite(2)
end event


event RSDKLoad
	LoadSpriteSheet("CNZ/Objects.gif")
	SpriteFrame(-48, -16, 32, 32, 1, 256) // Slot L - Sonic
	SpriteFrame(-16, -16, 32, 32, 1, 416) // Slot M - Eggman
	SpriteFrame( 16, -16, 32, 32, 1, 384) // Slot R - Jackpot!
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
