--function for automating the freesloting process
local addbandagesounds = function(array, prefix, number)
	for i=1,number
		array[i-1] = freeslot(prefix..i)
		if prefix != "sfx_bsds"
			sfxinfo[array[i-1]].flags = SF_TOTALLYSINGLE
		end
		sfxinfo[array[i-1]].caption = "/"
	end
end

local bouncepadsounds = {}
addbandagesounds(bouncepadsounds,"sfx_bpad",3)
local ringsounds = {}
addbandagesounds(ringsounds,"sfx_brng",9)
local spindashsounds = {}
addbandagesounds(spindashsounds,"sfx_bsds",7)
local enerbeamsounds = {}
addbandagesounds(enerbeamsounds,"sfx_bene",9)
local hurtsounds = {}
addbandagesounds(hurtsounds,"sfx_bnow",5)
local deathsounds = {}
addbandagesounds(deathsounds,"sfx_bdie",4)
local victorysounds = {}
addbandagesounds(victorysounds,"sfx_bvic",11)
local winsounds = {}
addbandagesounds(winsounds,"sfx_bwin",11)
freeslot("sfx_bramp")
sfxinfo[sfx_bramp].flags = SF_TOTALLYSINGLE
sfxinfo[sfx_bramp].caption = "/"

local bandagesrandomseed = 3135128580
local BandagesRandom = function(a,b)
	bandagesrandomseed = $*(max(gamemap+globallevelskynum+leveltime, 1)%128)
	if consoleplayer and consoleplayer.valid
	and consoleplayer.mo and consoleplayer.mo.valid
		bandagesrandomseed = $-(consoleplayer.mo.x+consoleplayer.mo.y+consoleplayer.mo.z)
	end
	bandagesrandomseed = $/2
	local dumb = ((bandagesrandomseed*36548569) >> 4) & (FRACUNIT-1)
	return ((dumb * (b-a+1)) >> FRACBITS) + a
end

--function for starting sounds with the cooldown
local startboomsound = function(p, sound)
	if p and p.valid and p.mo and p.mo.valid and p.mo.skin == "bandages"
	and p.bandagevoice and not p.bandagevoicecooldown
		p.bandagevoicecooldown = TICRATE*3/2
		S_StartSound(p.mo, sound)
	end
end

--the command that activates this whole thing
COM_AddCommand("bandagevoice", function(p, arg)
	if not (p.mo and p.mo.valid) then CONS_Printf(player, "This can only be used in a level!") return end
	if arg
		if arg == "0" or arg == "false" or arg == "off"
			p.bandagevoice = false
			CONS_Printf(p, "Bandages is no longer a blabbermouth.")
		elseif arg == "1" or arg == "default" or arg == "true" or arg == "on"
			p.bandagevoice = true
			CONS_Printf(p, "Bandages is now a blabbermouth.")
		elseif p.bandagevoice
			CONS_Printf(p, "bandagevoice[on/off] - When on, Bandages will constantly spout words about everything he's doing. Currently on, plug your ears.")
		else
			CONS_Printf(p, "bandagevoice[on/off] - When on, Bandages will constantly spout words about everything he's doing. Currently off, which is the default")
		end
	elseif p.bandagevoice
		CONS_Printf(p, "bandagevoice[on/off] - When on, Bandages will constantly spout words about everything he's doing. Currently on, plug your ears.")
	else
		CONS_Printf(p, "bandagevoice[on/off] - When on, Bandages will constantly spout words about everything he's doing. Currently off, which is the default")
	end
end)

addHook("PlayerThink",  function(p)
	if p.mo and p.mo.valid and p.mo.skin == "bandages" and p.bandagevoice
		--cooldown
		if p.bandagevoicecooldown
			p.bandagevoicecooldown = $-1
			return
		end
		--springs
		if p.mo.eflags & MFE_SPRUNG
			startboomsound(p, bouncepadsounds[min(BandagesRandom(0,3),2)]) --give bpad3 a slight edge
		end
		--spindash
		if p.pflags&PF_STARTDASH and p.dashspeed < p.maxdash
		and p.powers[pw_carry] == CR_NONE
			startboomsound(p, spindashsounds[BandagesRandom(0,6)])
		end
		--enerbeam
		if S_SoundPlaying(p.mo, sfx_cdfm52) and p.enertarget and p.enertarget.valid
			startboomsound(p, enerbeamsounds[BandagesRandom(0,8)])
		end
		--win
		if not (p.pflags & PF_FINISHED or p.exiting)
			p.firsttryidiot = true
		elseif p.firsttryidiot then
			startboomsound(p, winsounds[BandagesRandom(0,10)])
			p.firsttryidiot = false
		end
		--ramp
		if p.powers[pw_justlaunched] == 2 and not BandagesRandom(0,3)
			startboomsound(p, sfx_bramp)
		end
	end
end)

--hurt
addHook("MobjDamage", function(target)
	if target and target.valid and target.player and target.skin == "bandages"
	and target.player.bandagevoice
		target.player.bandagevoicecooldown = 0
		--don't mention rings when you don't have any
		if target.player.powers[pw_shield] or not target.player.rings
		or mariomode or G_IsSpecialStage()
			startboomsound(target.player, hurtsounds[BandagesRandom(0,3)])
		else
			startboomsound(target.player, hurtsounds[BandagesRandom(0,4)])
		end
	end
end, MT_PLAYER)

--death
addHook("MobjDeath", function(target, inflictor, source)
	if target and target.valid and target.player and target.skin == "bandages"
	and target.player.bandagevoice
		target.player.bandagevoicecooldown = 0
		startboomsound(target.player, deathsounds[BandagesRandom(0,3)])
	end
end)

--victory
addHook("MobjDeath", function(target, inflictor, source)
	if target and target.valid and (target.flags&MF_ENEMY or target.flags&MF_BOSS)
	and source and source.valid and source.player and source.skin == "bandages"
	and source.player.bandagevoice
		--don't mention robots or eggman against a boss or player
		if target.flags&MF_BOSS or target.type == MT_PLAYER
			source.player.bandagevoicecooldown = 0
			startboomsound(source.player, victorysounds[BandagesRandom(0,8)])
		elseif not source.player.bandagevoicecooldown
			startboomsound(source.player, victorysounds[BandagesRandom(0,10)])
		end
	end
end)

--rings
local ringvoice = function(target, inflictor, source)
	if not BandagesRandom(0,3) and source and source.valid
	and source.player and source.skin == "bandages"
		startboomsound(source.player, ringsounds[BandagesRandom(0,8)])
	end
end
addHook("MobjDeath", ringvoice, MT_RING)
addHook("MobjDeath", ringvoice, MT_REDTEAMRING)
addHook("MobjDeath", ringvoice, MT_BLUETEAMRING)