/*

  Egg Panic - Burn thy Rubber in Vengance - by Angular
  --Changing the Eggman Bomb by making it more akin to the Thundercloud a la MKWii - this means:
  --Making the Eggbomb last longer ON INITIAL PICKUP.
  --Giving the affected player a temp speed/accel boost in an attempt to catch other players
  --Giving Audio Indicators to nearby drivers in case you can't see CHECK
  --Optional Eggbump that allows the karter to pass the eggbomb to someone.
  
*/

freeslot("sfx_eggpan") --lol at s3ka4 not being loud enough
sfxinfo[sfx_eggpan].flags = $|SF_X8AWAYSOUND --not putting my socs on just for this

-- "optimization"
local TICRATE = TICRATE
local FRACUNIT = FRACUNIT
local FRACBITS = FRACBITS
local k_sneakertimer = k_sneakertimer
local k_spinouttimer = k_spinouttimer
local k_wipeoutslow = k_wipeoutslow
local k_eggmanexplode = k_eggmanexplode
local k_eggmanblame = k_eggmanblame
local k_speedboost = k_speedboost
local k_boostpower = k_boostpower
local k_accelboost = k_accelboost
local k_invincibilitytimer = k_invincibilitytimer
local k_growshrinktimer = k_growshrinktimer

local eggpanic_active = CV_RegisterVar({
	name = "eggpanic",
	defaultvalue = "On",
	flags = CV_NETVAR,
	possiblevalue = CV_OnOff
})

local eggbump_active = CV_RegisterVar({
	name = "eggbump",
	defaultvalue = "On",
	flags = CV_NETVAR,
	possiblevalue = CV_OnOff
})

local eggxtend_active = CV_RegisterVar({ --and everyone in the room groaned as loud as they could
	name = "eggbox_extend",
	defaultvalue = "On",
	flags = CV_NETVAR,
	possiblevalue = CV_OnOff
})

rawset(_G, "eggpanic", --an outside mod could adjust the speeds as they see fit, but now that's out of my hands fam...
{
	active = true,
	speedmod = 3*FRACUNIT/10,
	accelmod = 2*FRACUNIT
}) 

addHook("NetVars", function(n)
	eggpanic = n($) --I actually don't HAVE to do this but the edge case mentioned above warrants this precaution.
end)


--yoink from callmore's, thanks m8
local function shouldHurt(p)
	local pks = p.kartstuff
    return not (p.powers[pw_flashing] > 0 or pks[k_squishedtimer] > 0 or pks[k_spinouttimer] > 0
        or pks[k_invincibilitytimer] > 0 or not(pks[k_growshrinktimer] == 0) or pks[k_hyudorotimer] > 0 --we don't need to be cruel to shrunken squished folks
        or (G_BattleGametype() and ((pks[k_bumper] <= 0 and pks[k_comebacktimer]) or pks[k_comebackmode] == 1)))
end

local function egg_init(p)
	p.eggbuffer = 0
	p.prevegg = 0
	p.prevroulette = 0
end

local function eggxtension(p)
	if not(eggxtend_active.value) then return end
	
	local pks = p.kartstuff
	
	if p.prevegg == 0 and --we weren't egged before.
	pks[k_eggmanexplode] == 4*TICRATE and --we now suddenly have it.
	p.prevroulette == 2 then --and we're SURE it's actually from a roulette
		pks[k_eggmanexplode] = 6*TICRATE
		p.prevroulette = 0 --paranoid flush of the data since we're done.
	end
	
end

local function egg_main(p)

	if not(eggpanic_active.value) --wow this isn't obtuse and confusing at all
		eggpanic.active = false
		return 
	end
	
	eggpanic.active = true
	
	for p in players.iterate
	
		if not p.mo or not p.mo.valid continue end
		
		local pks = p.kartstuff
		
		if p.eggbuffer == nil then
			egg_init(p)
		end
		
		eggxtension(p)
		
		if pks[k_eggmanexplode] == 0 then --KILL ALL SOUNDS DOT EXE
			S_StopSoundByID(p.mo, sfx_eggpan)
		end
		
		if pks[k_eggmanexplode] == 1 then --color correction really fuckin sucks honestly
			p.mo.colorized = false
			p.mo.color = p.skincolor	
		end
		
		if pks[k_eggmanexplode] > 0
		
			if pks[k_eggmanexplode] == 101 -- the sound is just short of 3 seconds and that hurts me in a new way
				S_StartSound(p.mo, sfx_eggpan)
			end
			
			pks[k_speedboost] = max($, eggpanic.speedmod)
			pks[k_accelboost] = max($, eggpanic.accelmod)
			
			if leveltime%5 == 1 or (pks[k_eggmanexplode] < 55 and leveltime%2 == 1) --more intense flashing as you run out of time
				p.mo.colorized = true
				p.mo.color = SKINCOLOR_CRIMSON
			else
				p.mo.colorized = false
				p.mo.color = p.skincolor	
			end
			
		end
		
		p.eggbuffer = max($-1, 0)
		p.prevegg = pks[k_eggmanexplode]
		p.prevroulette = pks[k_roulettetype]
	end
	
end

local pl, pl2
local function egg_pass(mo, mo2)
	if not(eggbump_active.value) then return end 
	if mo and mo2 and mo.valid and mo2.valid
	
		if (mo.z > mo2.z + mo2.height) or (mo.z + mo.height < mo2.z) -- WHY DO I HAVE TO DO HEIGHT CHECKS MYSELF?
			return 
		end
		
		if mo.type == MT_PLAYER and mo2.type == MT_PLAYER

			pl = mo.player
			pl2 = mo2.player
			
			if not(shouldHurt(pl2)) --other kart's invincible, dummy
				return
			end
			
			if pl.eggbuffer or pl2.eggbuffer then --gotta add a cooldown to prevent this.
				return
			end

			--Pass the curse along...
			if pl.kartstuff[k_eggmanexplode]
				--Edge case of if both are eggboxed - mutually assured destruction
				if (pl2.kartstuff[k_eggmanexplode])
					pl.kartstuff[k_eggmanexplode], pl2.kartstuff[k_eggmanexplode] = 1, 1
					pl.eggbuffer, pl2.eggbuffer = 4*TICRATE, 4*TICRATE
					return
				end
				K_DropItems(pl2) --Knock the item outta em.
				pl2.kartstuff[k_eggmanexplode] = 4*TICRATE --Curse their existence...
				pl2.kartstuff[k_eggmanblame] = pl.kartstuff[k_eggmanblame] --A transgression has been sent
				--print("get busted")
				pl.kartstuff[k_eggmanexplode] = 0  --...and absolve yourself of sin.
				pl.kartstuff[k_eggmanblame] = -1
				S_StopSoundByID(mo, sfx_eggpan)
				S_StartSound(mo, sfx_s3k8b)
				S_StartSound(nil, sfx_itrole, pl2)
				mo.colorized = false
				mo.color = pl.skincolor
				pl.eggbuffer, pl2.eggbuffer = 2*TICRATE, TICRATE/2 --Adding a buffer so you can't just trade it. However, should the next victim be cunning enough...
			end
			
		end
		
	end
	
end

addHook("ThinkFrame", egg_main)
--I'm not particularly thrilled these both exist for redunadncy's sake.
addHook("MobjCollide", egg_pass, MT_PLAYER)
addHook("MobjMoveCollide", egg_pass, MT_PLAYER)

--The eggman timer is HARDCODED TO 3. pain.
local flashItem, forcecmap, itemX, itemY, ssflags, ssindex, eggcached, kp_eggnum
local function drawEggxtended(v, p, c)
	if not(eggxtend_active.value) then return end
	if (p.spectator) then return end
	
	--Figure out who we're drawing for.
	ssindex = nil
	for i = 0, splitscreen do
		if p == displayplayers[i] then
			ssindex = i 
			break
		end
	end
	
	if p.kartstuff[k_eggmanexplode] > 4*TICRATE-1
		hud.disable("item")
		itemX = 5
		itemY = 5
		
		if splitscreen <= 1
			ssflags = ssindex == 1 and V_SPLITSCREEN or V_SNAPTOTOP
			v.draw(itemX, itemY, v.cachePatch("K_ITBG"), ssflags|V_SNAPTOLEFT|V_HUDTRANS)
			if leveltime%2 == 1 
				v.draw(itemX, itemY, v.cachePatch("K_ITEGGM"), ssflags|V_SNAPTOLEFT|V_HUDTRANS) 
			end
			v.draw(itemX+17, itemY+13, v.cachePatch("K_EGGN" .. G_TicsToSeconds(p.kartstuff[k_eggmanexplode])), ssflags|V_SNAPTOLEFT|V_HUDTRANS) 
		else
		
			itemX = -9
			itemY = -8
			
			if ssindex == 0 --1p
				ssflags = V_SNAPTOTOP|V_SNAPTOLEFT
			elseif ssindex == 1 --2p
				ssflags = V_SNAPTOTOP|V_SNAPTORIGHT
				itemX = 280
			elseif ssindex == 2 --3p
				ssflags = V_SPLITSCREEN|V_SNAPTOLEFT
			elseif ssindex == 3 --4p
				ssflags = V_SPLITSCREEN|V_SNAPTORIGHT
				itemX = 280
			end
			
			v.draw(itemX, itemY, v.cachePatch("K_ISBG"), ssflags)
			
			if leveltime%2 == 1  
				v.draw(itemX, itemY, v.cachePatch("K_ISEGGM"), ssflags|V_HUDTRANS) 
			end
			v.draw(itemX+17, itemY+13, v.cachePatch("K_EGGN" .. G_TicsToSeconds(p.kartstuff[k_eggmanexplode])), ssflags|V_HUDTRANS) 
		end		
	
	else
		hud.enable("item")
	end
end
hud.add(drawEggxtended, "game")

local function egg_debug(v, p, c)
	if p.exiting or not p.mo or not p.mo.valid then return end 

	v.drawString(12,65, "BUFFER: ".. p.eggbuffer,V_SNAPTOLEFT,"left")
	v.drawString(12,75+(#p*10), "P" .. #p+1 .. " EGGEXPLODE: " .. p.kartstuff[k_eggmanexplode], V_SNAPTOLEFT, "left")
	v.drawString(12,85, "EGGTIMER: ".. p.prevegg,V_SNAPTOLEFT,"left")
	v.drawString(12,95, "PREVROULETTE: ".. p.prevroulette,V_SNAPTOLEFT,"left")
	
end
--hud.add(egg_debug, "game")

--HOSTMOD Neoscoreboard yaddayadda
local egg_hmload = false
addHook("ThinkFrame", function()
	if egg_hmload then return end
	if server and (CV_FindVar("hm_scoreboard")) and leveltime > TICRATE 
		HM_Scoreboard_AddMod({disp = "Egg Panic", var = "eggpanic"})
		HM_Scoreboard_AddTip({msg = "If you got eggboxed, try and pass it off to someone! Bump into them to pass the timer around. Just don't get bumped back afterwards..."})
		egg_hmload = true
	end
end)

