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

local rbc_enabled = CV_RegisterVar({
	 name = "rbc_enabled",
	 defaultvalue = "On",
	 flags = "0",
	 PossibleValue = {Off = 0, On = 1}
	})

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

addHook("ThinkFrame", function()

	-- Main loop
	for player in players.iterate do
		if player.mo and player.mo.valid then
			local mo = player.mo
			
			if not S_IdPlaying(sfx_gbcjac) then mo.gbc_jackpotplayed = nil end
			
			-- Anything that's not a jackpot sound plays "aw dangit"
			if S_IdPlaying(sfx_slot00) -- Bar
			or S_IdPlaying(sfx_slot01) -- Two Bar
			or S_IdPlaying(sfx_slot02) -- Three Bar
			or S_IdPlaying(sfx_slot03)  -- Ring
			or S_IdPlaying(sfx_slot04) then -- Seven
				if mo.gbc_dangitplayed == nil and rbc_enabled.value then
					-- print("DEBUG: Not a jackpot, play aw dangit")
					S_StartSound(nil, sfx_dangit, player)
					mo.gbc_dangitplayed = 1
					mo.gbc_gogamblingplayed = nil
				end
			else
				mo.gbc_dangitplayed = nil
			end
			
			-- Good ending jackpot
			if S_IdPlaying(sfx_slot05) then
				if mo.gbc_jackpotplayed == nil and rbc_enabled.value then
					-- print("DEBUG: Jackpot, play good ending")
					S_StartSound(nil, sfx_gbcjac, player)
					mo.gbc_jackpotplayed = 1
					mo.gbc_gogamblingplayed = nil
				end
			end
		end
	end
end)



addHook("TouchSpecial", function(spec, touch)
	if spec and spec.valid and spec.health
	and touch and touch.valid then
		--if S_IdPlaying(sfx_kc2e) and not (S_IdPlaying(sfx_lggamb)) then touch.player.mo.gbc_gogamblingplayed = nil end
		
		-- We only go gambling once
		local gamblingplayed = 0
		
		if spec.state >= S_RINGBOX1 and spec.state <= S_RINGBOX12 then
			if touch.gbc_gogamblingplayed == nil and rbc_enabled.value then
				if gamblingplayed == 0 then
					-- print("DEBUG: Ring box hit, let's go gambling")
					S_StartSound(nil, sfx_lggamb, touch.player)
					touch.gbc_gogamblingplayed = 1
					gamblingplayed = 1
				else return false end
			end
		end
	end
end, MT_RANDOMITEM)
