//Insurgencydude exists because I love all of insurgency's voice clips and I wanted
//to give a character as much potential to react to situations as possible
//ergo I will JAM this file full of lua designed to detect and react to events with voice lines
//also it will probably have like 8 MB of voice lines

//this script is by freeman2651
//the lovely art is by kel in kelchars pack
//so as long as kel is okay with it, you can repurpose this script for what you would like
//(unless your name is retrostation)
//regardless of what you use the script for, the art/skin is NOT reusable without kel's explicit permission

local id_logging = CV_RegisterVar({
	name = "insurgencydude_logging",
	defaultvalue = "Off",
	flags = CV_NOSHOWHELP,
	possiblevalue = CV_OnOff
})

local id_stressreadout = CV_RegisterVar({
	name = "insurgencydude_stresshud",
	defaultvalue = "Off",
	flags = CV_NOSHOWHELP,
	possiblevalue = CV_OnOff
})

local FRACUNIT = FRACUNIT
local TICRATE = TICRATE

//constants for easy access
local pmo = nil
local STRESSED = TICRATE*15 //level of stress at which we switch to panicked voicelines
local MAXSTRESS = TICRATE*25 //caps stress so it doesn't last forever
local VOICEWAIT = TICRATE*3 //wait this long between playing clips where possible
local VOICEWAITLONG = TICRATE*8 //wait this long for playing low priority voiceclips (overtake, boost)
local MOBJGRACETIME = TICRATE/2 //how long mobjs have to wait after being fired from the player until the player can near-miss their own thrown item
local NEARMISSCOOLDOWN = 10 //how often something can be grazed
local lottery = 0 //placeholder random number generated every frame
local nearmissbox = nil

freeslot("MT_IDCOLLIDER")

mobjinfo[MT_IDCOLLIDER] = {
	doomednum = -1,
	spawnstate = S_NULL,
	radius = FRACUNIT*96,
	height = FRACUNIT*96,
	flags = MF_NOGRAVITY|MF_NOTHINK,
}
//boost
freeslot("sfx_idbst1","sfx_idbst2","sfx_idbst3",
		"sfx_idbsta","sfx_idbstb","sfx_idbstc")
//damaged
freeslot("sfx_iddmg1","sfx_iddmg2","sfx_iddmg3","sfx_iddmg4","sfx_iddmg5",
		"sfx_iddmga","sfx_iddmgb","sfx_iddmgc","sfx_iddmgd","sfx_iddmge")
//damagedself (hit yourself with your own orbi dumbass)
freeslot("sfx_idwtf1","sfx_idwtf2","sfx_idwtf3")
//hitconfirm
freeslot("sfx_idhit1","sfx_idhit2","sfx_idhit3","sfx_idhit4",
		"sfx_idhita","sfx_idhitb","sfx_idhitc","sfx_idhitd","sfx_idhite")
//generic item placement
freeslot("sfx_idplc2","sfx_idplc3",
		"sfx_idplca")
//mine item placement
freeslot("sfx_idplc1",
		"sfx_idplcb")
//generic item thrown
freeslot("sfx_idatk1","sfx_idatk2",
		"sfx_idatka","sfx_idatkb","sfx_idatkc","sfx_idatkd","sfx_idatke")
//mine thrown
freeslot("sfx_idatk3",
		"sfx_idatkf","sfx_idatkg","sfx_idatkh")
//loss (| || || |-)
freeslot("sfx_idlse1","sfx_idlse2","sfx_idlse3","sfx_idlse4","sfx_idlse5")
//nearby same skin was hit
freeslot("sfx_iddwn1","sfx_iddwn2","sfx_iddwn3","sfx_iddwn4")
//nearmiss
freeslot("sfx_idmis1","sfx_idmis2","sfx_idmis3",
		"sfx_idmisa","sfx_idmisb","sfx_idmisc")
//overtake
freeslot("sfx_idovr1","sfx_idovr2",
		"sfx_idovra","sfx_idovrb")
//being seeked by jawz
freeslot("sfx_idjwz1","sfx_idjwz2",
		"sfx_idjwza","sfx_idjwzb")
//being seeked by SPB
freeslot("sfx_idspb1","sfx_idspb2","sfx_idspb3",
		"sfx_idspba")
//victory
freeslot("sfx_idwin1","sfx_idwin2","sfx_idwin3")

local soundbank = {
	IDboost = {sfx_idbst1,sfx_idbst2,sfx_idbst3},
	IDboost_S = {sfx_idbsta,sfx_idbstb,sfx_idbstc},

	IDdamage = {sfx_iddmg1,sfx_iddmg2,sfx_iddmg3,sfx_iddmg4,sfx_iddmg5},
	IDdamage_S = {sfx_iddmga,sfx_iddmgb,sfx_iddmgc,sfx_iddmgd,sfx_iddmge},
	IDdamageself = {sfx_idwtf1,sfx_idwtf2,sfx_idwtf3},

	IDhitconfirm = {sfx_idhit1,sfx_idhit2,sfx_idhit3,sfx_idhit4},
	IDhitconfirm_S = {sfx_idhita,sfx_idhitb,sfx_idhitc,sfx_idhitd,sfx_idhite},

	IDitemplaced = {sfx_idplc2,sfx_idplc3},
	IDitemplaced_S = {sfx_idplca},
	IDmineplaced = {sfx_idplc1},
	IDmineplaced_S = {sfx_idplcb},

	IDitemthrown = {sfx_idatk1,sfx_idatk2},
	IDitemthrown_S = {sfx_idatka,sfx_idatkb,sfx_idatkc,sfx_idatkd,sfx_idatke},
	IDminethrown = {sfx_idatk3},
	IDminethrown_S = {sfx_idatkf,sfx_idatkg,sfx_idatkh},

	IDloss = {sfx_idlse1,sfx_idlse2,sfx_idlse3,sfx_idlse4,sfx_idlse5},
	IDnearbyidhit = {sfx_iddwn1,sfx_iddwn2,sfx_iddwn3,sfx_iddwn4},

	IDnearmiss = {sfx_idmis1,sfx_idmis2,sfx_idmis3},
	IDnearmiss_S = {sfx_idmisa,sfx_idmisb,sfx_idmisc},

	IDovertake = {sfx_idovr1,sfx_idovr2},
	IDovertake_S = {sfx_idovra,sfx_idovrb},

	IDseekerjawz = {sfx_idjwz1,sfx_idjwz2},
	IDseekerjawz_S = {sfx_idjwza,sfx_idjwzb},
	IDseekerspb = {sfx_idspb1,sfx_idspb2,sfx_idspb3,sfx_idspba},

	IDwin = {sfx_idwin1,sfx_idwin2,sfx_idwin3}
	}

//the idea here is to try and make things somewhat more organized and use less upvalues
//whenever we want a sound of a specific group we'll just playrandomsound(soundbank[IDdamage])

//generic logging function, drawn upon by how tyron did it in friendmod, thanks pal
local function IDlog(p, msg)
	if not id_logging.value then return end
	if p and p.valid then
		print("\131"..leveltime.."\128: "..p.name.." - "..msg)
	else
		print("\131"..leveltime.."\128: "..msg)
	end
end
//initialize shit so I don't get nil errors out the ass
local function IDinit(p)
	p.IDstuff = {
		IDstresslevel = 0,
		IDchasedbyspb = false,
		IDchasedbyspbmo = nil,
		IDchasedbyjawz = false,
		IDchasedbyjawzmo = nil,
		IDlastposition = 0,
		IDlastitem = 0, //last item in box
		IDlastitemamount = 0, //last amount of items in box
		IDlastbuttons = 0, //last buttons held
		IDlastvoice = 0, //last leveltime we played a clip (some will bypass this)
		IDlastvoicelong = 0, //time remaining to play a low priority voiceclip like boost or overtake
		IDnearmisscooldown = 0, //how long before another object can near-miss you
		IDjawzchasetime = 0,
		IDspbeventdelay = 0, //delays the SPB chase event so the BEWWW doesn't drown it out
		IDlastsneakertimer = 0,
		IDstartedexiting = false //used to flag when someone wins/loses so the voice only plays once
		}
	p.IDinit = true
	IDlog(p,"ID init fired")
end

//why did I even make this its own function
local function stresscheck(p)
	return p.IDstuff.IDstresslevel >= STRESSED
end

local function randsound(tbl, pl, forced)
	//tbl - the table from soundbank we'll try to use, provided as a string (this will try to evaluate to the proper table)
	//pl - the player to play the sound
	//forced - if this will bypass needing to wait for voice cooldown
	//this will automatically check if pl is past stress threshold and try to use stress voices if available
	if not tbl then print("\133ID error: no table provided to randsound") end
	if not pl then print("\133ID error: no player provided to randsound") end
	if not forced and pl.IDstuff.IDlastvoice>0 then
		IDlog(pl,"aborted voice call on "..tbl.." - waiting for cooldown ("..pl.IDstuff.IDlastvoice.." remains)")
		return
	end
	local testtable = soundbank[tbl]
	local realtable = nil
	if not testtable then print("\133ID error: no table could be found at the entry: "..tbl) end
	if not stresscheck(pl) then
		realtable = testtable
	else
		testtable = tbl.."_S"
		testtable = soundbank[testtable]
		if testtable then
			realtable = testtable
		else
			realtable = soundbank[tbl]
		end
	end
	//print(lottery..": lottery % realtable is: "..lottery%#realtable)
	S_StartSound(pl.mo, realtable[(lottery%#realtable)+1] or realtable[1])
	pl.IDstuff.IDlastvoice = VOICEWAIT
	return
	//return tbl[(lottery%#tbl)+1] or tbl[1]
end

local function addstress(p, amt)
	if not p or not amt then return end
	p.IDstuff.IDstresslevel = $+amt
	return
end


//as ashnal has pointed out this could be made better, more efficient, indexed with tables, ect
//but I cannot give enough of a fuck to standardize all of this and I don't really care I just want this done
//if my guess is correct the lua interpreter is so bad anyways that performance gains would be marginal
local function ID_event(event, p)
	if not p then print("\133ID EVENT WITH NO PLAYER DIPSHIT") end
	if event then IDlog(p,"ID event activated: "..event) else print("\133 ID event error: no event") end
	if event == "damaged" then
		randsound("IDdamage", p, true)
		addstress(p,10*TICRATE)
	elseif event == "damagedself" then
		randsound("IDdamageself", p, true)
		addstress(p,10*TICRATE)
	elseif event == "nearbyidhit" then
		randsound("IDnearbyidhit", p)
		addstress(p,4*TICRATE)
	elseif event == "hitconfirm" then
		randsound("IDhitconfirm", p, true)
	elseif event == "nearmiss" then
		randsound("IDnearmiss", p)
		addstress(p,5*TICRATE)
		p.IDstuff.IDnearmisscooldown = NEARMISSCOOLDOWN
	elseif event == "nearmissnovoice" then //for bananas, they're not that dangerous to dodge
		addstress(p,3*TICRATE)
		p.IDstuff.IDnearmisscooldown = NEARMISSCOOLDOWN
	elseif event == "boost" then
		if p.IDstuff.IDlastvoicelong <= 0 then
			p.IDstuff.IDlastvoicelong = VOICEWAITLONG
			randsound("IDboost", p)
		end
		addstress(p,TICRATE)
	elseif event == "overtake" then
		if p.IDstuff.IDlastvoicelong <= 0 then
			p.IDstuff.IDlastvoicelong = VOICEWAITLONG
			randsound("IDovertake", p)
		end
	elseif event == "bumpplayer" then
		addstress(p,3*TICRATE)
	elseif event == "win" then
		randsound("IDwin", p, true)
	elseif event == "lose" then
		randsound("IDloss", p, true)
	elseif event == "throwgeneric" then
		randsound("IDitemthrown", p)
	elseif event == "placegeneric" then
		randsound("IDitemplaced", p)
	elseif event == "throwmine" then
		randsound("IDminethrown", p)
	elseif event == "placemine" then
		randsound("IDmineplaced", p)
	elseif event == "seekerspb" then
		randsound("IDseekerspb", p)
		addstress(p,MAXSTRESS)
	elseif event == "seekerjawz" then
		randsound("IDseekerjawz", p)
		addstress(p,7*TICRATE)
	end
	IDlog(p,"stress level now at "..p.IDstuff.IDstresslevel.." ["..p.IDstuff.IDstresslevel/TICRATE.."]")
end

addHook("ThinkFrame", do
	lottery = P_RandomByte()
	for p in players.iterate do
		if leveltime == 3 or not p.IDinit then
			IDinit(p)
		end
		if p.mo and p.mo.valid then
			nearmissbox = p.idcollider
			pmo = p.mo
			if pmo.skin ~= "insurgencydlua" then
				if nearmissbox and nearmissbox.valid then
					P_RemoveMobj(nearmissbox)
					IDlog(p,"removed invalid collider")
					return
				end
			else
				if (nearmissbox == nil or (not nearmissbox.valid))
					IDlog(p,"generating new collider")
					p.idcollider = P_SpawnMobj(0,0,0,MT_IDCOLLIDER)
					nearmissbox = p.idcollider
					nearmissbox.lastapproach = 0
				end
				if p.IDstuff.IDstresslevel > 0 and not(p.kartstuff[k_spinouttimer]) then
					if p.IDstuff.IDstresslevel > MAXSTRESS then
						p.IDstuff.IDstresslevel = MAXSTRESS
					else
						p.IDstuff.IDstresslevel = $-1
					end
				end
				P_MoveOrigin(nearmissbox,pmo.x,pmo.y,pmo.z)
				//print("collider pos: "..idcollider.x..","..idcollider.y..","..idcollider.z)
				//print("player   pos: "..pmo.x..","..pmo.y..","..pmo.z)
				nearmissbox.scale = pmo.scale
				nearmissbox.target = pmo
				nearmissbox.flags2 = pmo.flags2
				nearmissbox.eflags = pmo.eflags

				if p.IDstuff.IDchasedbyspb then
					if (not(p.IDstuff.IDchasedbyspbmo and p.IDstuff.IDchasedbyspbmo.valid)) or
					(p.IDstuff.IDchasedbyspbmo.tracer and p.IDstuff.IDchasedbyspbmo.tracer.player and p.IDstuff.IDchasedbyspbmo.tracer.player~=p) or
					p.IDstuff.IDchasedbyspbmo.extravalue1 ~= 1 then
						p.IDstuff.IDchasedbyspb = false
						p.IDstuff.IDchasedbyspbmo = nil
					end
				end

				if p.IDstuff.IDchasedbyjawz then
					if (not(p.IDstuff.IDchasedbyjawzmo and p.IDstuff.IDchasedbyjawzmo.valid)) or
					(p.IDstuff.IDchasedbyjawzmo.tracer and p.IDstuff.IDchasedbyjawzmo.tracer.player and p.IDstuff.IDchasedbyjawzmo.tracer.player~=p) or
					p.IDstuff.IDchasedbyjawzmo.extravalue1 then
						p.IDstuff.IDchasedbyjawz = false
						p.IDstuff.IDchasedbyjawzmo = nil
						p.IDstuff.IDjawzchasetime = 0
					else
						if p.IDstuff.IDchasedbyjawzmo then
							p.IDstuff.IDjawzchasetime = $ + 1
							if p.IDstuff.IDjawzchasetime % 5*TICRATE == 0 then
								//long jawz chases generate continual events
								ID_event("seekerjawz",p)
							end
						end
					end
				end
				if not p.exiting then
					if leveltime > 15*TICRATE and p.kartstuff[k_position] < p.IDstuff.IDlastposition then ID_event("overtake", p) end
					if leveltime > 10*TICRATE and p.kartstuff[k_sneakertimer] > p.IDstuff.IDlastsneakertimer then ID_event("boost", p) end
				end
				//IDlog(p, "last buttons: "..p.IDstuff.IDlastbuttons.." cur buttons: "..p.cmd.buttons)
				//not (p.kartstuff[k_stolentimer] or p.kartstuff[k_spinouttimer])
				if (p.IDstuff.IDlastitem~=p.kartstuff[k_itemtype] or p.IDstuff.IDlastitemamount > p.kartstuff[k_itemamount]) and
				not (p.kartstuff[k_stolentimer] or p.kartstuff[k_spinouttimer]) and p.cmd.buttons & BT_ATTACK then
					//we've used an item holey shite!!!
					IDlog(p, "item use fired")
					if p.IDstuff.IDlastitem == KITEM_ORBINAUT or p.IDstuff.IDlastitem == KITEM_JAWZ or p.IDstuff.IDlastitem == KITEM_SPB or p.IDstuff.IDlastitem == KITEM_BALLHOG or
					(p.IDstuff.IDlastbuttons & BT_FORWARD and (p.IDstuff.IDlastitem == KITEM_BANANA or p.IDstuff.IDlastitem == KITEM_EGGMAN)) then
						ID_event("throwgeneric", p)
					elseif p.IDstuff.IDlastitem == KITEM_BANANA or p.IDstuff.IDlastitem == KITEM_EGGMAN then
						ID_event("placegeneric", p)
					elseif p.IDstuff.IDlastbuttons & BT_BACKWARD and p.IDstuff.IDlastitem == KITEM_MINE then
						ID_event("placemine", p)
					elseif p.IDstuff.IDlastitem == KITEM_MINE then
						ID_event("throwmine", p)
					end
				end
				if p.exiting and not p.IDstuff.IDstartedexiting then
					p.IDstuff.IDstartedexiting = true
					local totalp = 0
					for q in players.iterate
						if q and q.valid and q.mo and q.mo.valid and not q.spectator then
							totalp = $+1
						end
					end
					if p.kartstuff[k_position] <= totalp/2 or p.kartstuff[k_position] == 1 then ID_event("win",p, true) else ID_event("lose",p, true) end
				end
				if p.IDstuff.IDspbeventdelay > 0 then p.IDstuff.IDspbeventdelay = $-1 end
				if p.IDstuff.IDspbeventdelay == 1 and p.IDstuff.IDchasedbyspbmo then
					ID_event("seekerspb", p, true)
				end
				p.IDstuff.IDlastitem = p.kartstuff[k_itemtype]
				p.IDstuff.IDlastitemamount = p.kartstuff[k_itemamount]
				p.IDstuff.IDlastbuttons = p.cmd.buttons
				p.IDstuff.IDlastsneakertimer = p.kartstuff[k_sneakertimer]
				p.IDstuff.IDlastposition = p.kartstuff[k_position]
				if p.IDstuff.IDnearmisscooldown > 0 then p.IDstuff.IDnearmisscooldown = $-1 end
				if p.IDstuff.IDlastvoice > 0 then p.IDstuff.IDlastvoice = $-1 end
				if p.IDstuff.IDlastvoicelong > 0 then p.IDstuff.IDlastvoicelong = $-1 end
			end
		end
	end
end)


//quick function that returns distance between two things
local function quickdist(mo1, mo2)
	if not(mo1 and mo1.valid) then
		print("quickdist: mo1 is not valid")
		return 0
	end
	if not(mo2 and mo2.valid) then
		print("quickdist: mo2 is not valid")
		return 0
	end
	return abs(FixedHypot(FixedHypot(mo1.x-mo2.x,mo1.y-mo2.y),mo1.z-mo2.z))/FRACUNIT
end

local mobjtypes = {MT_ORBINAUT, MT_JAWZ, MT_JAWZ_DUD, MT_BANANA, MT_BALLHOG, MT_SINK, MT_SPB, MT_SSMINE, MT_MINE}
local function nearmisscheck(collider, mobj)
	//this is used to detect near-misses to the player
	//print("IDCollider fired a mobj collide")
	if leveltime > 5 and not collider or not collider.target then
		//print(leveltime..":\133 WARNING: Insurgency dude collider collided into something but has no parent mobj to reference.")
		return
	end
	if not ((collider.z >= mobj.z and collider.z < mobj.z + mobj.height)
        or (mobj.z >= collider.z and mobj.z < collider.z + collider.height)) then
		return //SERIOUSLY
	end
	local p = collider.target.player
	//print(leveltime..": mobj source: "..mobj.target.player.name.."; spawntime: "..mobj.spawnedtime.."; must exceed: "..mobj.spawnedtime+MOBJGRACETIME)
	if p and p.speed and p.speed < 20*FRACUNIT and mobj and mobj.speed and mobj.speed < 10*FRACUNIT then return end //can't nearmiss if its too slow, but this is arbitrary
	if mobj.target and mobj.target.player == p and ((mobj.spawnedtime and leveltime <= mobj.spawnedtime+MOBJGRACETIME) or mobj.type == MT_SSMINE) then return end
	if p.powers[pw_invulnerability] or p.powers[pw_flashing] or p.kartstuff[k_growshrinktimer] > 0 or p.kartstuff[k_squishedtimer] then return end
	for i=1,#mobjtypes
		if mobj.type ~= mobjtypes[i] then continue end
		//local dist = P_AproxDistance(P_AproxDistance(p.mo.x - mobj.x, p.mo.y - mobj.y),p.mo.z - mobj.z)/FRACUNIT
		local dist = quickdist(p.mo,mobj)
		if (not p.IDstuff.IDnearmisscooldown) and collider.lastapproach ~= 0 and collider.lastmobj and collider.lastmobj == mobj and collider.lastapproach < dist then
			if mobj.type == MT_BANANA then
				ID_event("nearmissnovoice",p)
			else
				ID_event("nearmiss",p)
			end
		end
		collider.lastmobj = mobj
		collider.lastapproach = dist
		//print(leveltime..": lastapproach changed to "..dist)
		break
	end
end

addHook("MobjThinker", function(mo)
	if not(mo and mo.valid) then return end
	if not mo.lastvalue1 then mo.lastvalue1 = 0 end
	if mo.extravalue1 == 1 and mo.tracer and mo.tracer.player and not mo.tracer.player.spectator then
		local pl = mo.tracer.player
		if pl.mo.skin == "insurgencydlua" and not pl.IDstuff.IDchasedbyspb then
			pl.IDstuff.IDspbeventdelay = 3*TICRATE
			//ID_event("seekerspb",pl, true)
		end
		pl.IDstuff.IDchasedbyspb = true
		pl.IDstuff.IDchasedbyspbmo = mo
	end
	if mo.lastvalue1 == 0 and mo.extravalue1 == 1 and mo.tracer and mo.tracer.player and mo.tracer.player.skin == "insurgencydlua" then
		pl.IDstuff.IDspbeventdelay = 3*TICRATE
		//ID_event("seekerspb",pl, true)
	end
end, MT_SPB)

addHook("MobjThinker", function(mo)
	if not mo.tracer then return end
	if not(mo and mo.valid) or mo.extravalue1 or mo.IDthinked then return end
	mo.IDthinked = true
	local pl = mo.tracer.player
	if pl.mo.skin == "insurgencydlua" then
		pl.IDstuff.IDchasedbyjawz = true
		pl.IDstuff.IDchasedbyjawzmo = mo
		ID_event("seekerjawz",pl)
	end
end, MT_JAWZ)

addHook("MobjCollide", nearmisscheck, MT_IDCOLLIDER)
addHook("MobjMoveCollide", nearmisscheck, MT_IDCOLLIDER)

addHook("MobjCollide", function(mobj1,mobj2)
	if mobj1 and mobj1.valid and mobj2 and mobj2.valid then
		if not ((mobj1.z >= mobj2.z and mobj1.z < mobj2.z + mobj2.height)
        or (mobj2.z >= mobj1.z and mobj2.z < mobj1.z + mobj1.height)) then
			return
		end
		if (mobj1.player and mobj1.player.kartstuff[k_hyudorotimer]) or
		(mobj2.player and mobj2.player.kartstuff[k_hyudorotimer]) then return end
		if mobj1.player and mobj1.player.skin == "insurgencydlua" then ID_event("bumpplayer",mobj1.player) end
		if mobj2.player and mobj2.player.skin == "insurgencydlua" then ID_event("bumpplayer",mobj2.player) end
	end
end, MT_PLAYER)

for i=1,#mobjtypes
	addHook("MobjThinker", function(mo)
		if mo.thinked then return end
		mo.spawnedtime = leveltime
		//print(leveltime..": new object spawned, must exceed "..leveltime+MOBJGRACETIME.." to become nearmissable")
		mo.thinked = true
	end, mobjtypes[i])
end

local function insurgencydudedamage(p, mo, pmo)
	if p and p.valid and p.mo and p.mo.valid and not p.spectator
		local plr = nil
		if pmo and pmo.player and pmo.player.valid then
			plr = pmo.player
		elseif mo and mo.valid and mo.target and mo.target.player then
			plr = mo.target.player
		end
		if p.mo.skin == "insurgencydlua" and p.IDinit then
			if p == plr then ID_event("damagedself", p) else ID_event("damaged", p) end
			for q in players.iterate
				if not(q and q.valid and q.mo and q.mo.valid and (not q.spectator)) then continue end
				if q.mo.skin ~= "insurgencydlua" then continue end
				if q == p then continue end
				IDlog(q, "fired a nearbyidhit event, relative positions: "..p.kartstuff[k_position].." vs "..q.kartstuff[k_position].." and relative distance is "..quickdist(q.mo, p.mo))
				if q.kartstuff[k_position] >= p.kartstuff[k_position] and quickdist(q.mo, p.mo) <= 2000 then
					ID_event("nearbyidhit", q)
					break
				end
			end
		end
		if p~=plr and plr and plr.mo.skin == "insurgencydlua" then
			ID_event("hitconfirm", plr)
		end
	end
end

addHook("PlayerSpin", insurgencydudedamage)
addHook("PlayerExplode", insurgencydudedamage)
addHook("PlayerSquish", insurgencydudedamage)

local col = "\128"
hud.add(function(v, p, c)
	if not id_stressreadout.value then return end
	if p and p.valid and p.IDinit then
		col = "\128"
		if stresscheck(p) then
			col = "\133"
		elseif p.IDstuff.IDstresslevel > 0 then
			col = "\130"
		end
		v.drawString(50, 150, col.."STRESS: "..p.IDstuff.IDstresslevel.." ["..p.IDstuff.IDstresslevel/TICRATE.."]", 0, "center")
	end
end)