-- LET'S GO GAMBLING --
-- AW DANGIT --

freeslot(
"sfx_lggamb",
"sfx_dangit",
"sfx_gbcjac"
)

-- Thanks haya for these upcoming bits of code:

-- ringbox states to check for
local gamblestates = {
	[S_RINGBOX1] = true,
	[S_RINGBOX2] = true,
	[S_RINGBOX3] = true,
	[S_RINGBOX4] = true,
	[S_RINGBOX5] = true,
	[S_RINGBOX6] = true,
	[S_RINGBOX7] = true,
	[S_RINGBOX8] = true,
	[S_RINGBOX9] = true,
	[S_RINGBOX10] = true,
	[S_RINGBOX11] = true,
	[S_RINGBOX12] = true,
}

-- if we should be disappointed
local gamblecond = {
	[0] = sfx_dangit,
	[1] = sfx_dangit,
	[2] = sfx_dangit,
	[3] = sfx_dangit,
	[4] = sfx_dangit,
	[5] = sfx_gbcjac
}

-- gamble sounds to note
local gamblesounds = {
	sfx_slot00, sfx_slot01, sfx_slot02, sfx_slot03, sfx_slot04
}

-- S_StartSound players are shit
local function P_IsDisplayPlayer(p)
	if not (p and p.valid) then return false end
	
	-- There are only 4 display players at a time... right?
	for i = 0, 3 do
		if not (displayplayers[i] and displayplayers[i].valid) then continue end
		if p == displayplayers[i] then return true end
	end
	
	return false
end

-- Partial Lua implementation of P_IsPickupCheesy
local function isPickupCheesy(player, pickuptype)
	if player.lastpickupdistance and player.lastpickuptype == pickuptype then
		local distancedelta = abs(min(player.distancetofinish - player.lastpickupdistance, player.lastpickupdistance - player.distancetofinish))
		if distancedelta < 2500 then return true end
	end  
	return false
end

-- And now back to my code

local rbc_enabled = CV_RegisterVar({
	name = "rbc_enabled",
	description = "Enables Ringboxcore sounds.",
	defaultvalue = "Yes",
	PossibleValue = CV_YesNo	
})

addHook("PlayerThink", function(player)

	-- Is the command off?
	if rbc_enabled.value == 0 then return end
	
	-- Are we spectating?
	if player.spectator then return end
	
	-- Are we not a valid player mobj?
	if not (player.mo and player.mo.valid) then return end
	
	-- Are we not a display player (i.e., local client players up to 4)?
	if not P_IsDisplayPlayer(player) then return end
	
	local matchdelay = TICRATE
	if player.ringboxdelay ~= matchdelay then return end
	
	-- If all the checks pass then we can play our resulting sound
	S_StartSound(nil, gamblecond[player.ringboxaward])
end)


-- Handling first half of the sequence (thanks haya for this fix)
addHook("TouchSpecial", function(spec, touch)
	
	if rbc_enabled.value == 0 then return end
	
	-- Disregard any states that are not the ringbox states
	if not gamblestates[spec.state] then return end
	
	-- If our player isn't a display player (i.e., local client players up to 4) then disregard
	local player = touch.player
	if not (player and player.valid) then return end
	if not P_IsDisplayPlayer(player) then return end
	
	-- Disregard if we can't pick up ringboxes
	if not P_CanPickupItem(touch.player, 1) then return end
	
	-- Disregard if the anti-farm measure is active
	local cheesetype = (spec.flags2 & MF2_BOSSDEAD) and 2 or 1 -- Permanent ring boxes are also factored in
	if isPickupCheesy(player, cheesetype) then return end
	
	-- If ALL of those checks pass, we can finally go gambling
	S_StartSound(nil, sfx_lggamb)
end, MT_RANDOMITEM)
