/*/

ACROBATICS FUNKtion BLOCK [get groovy]

Exposed audio/visual/some check functions just in case one's looking for a good time or wants to hook into acro.

/*/

--A quick failsafe...
if acrobasics --don't load both of these files at once!
	print("\142Acrobasics should not be loaded in tandem with this mod. Restart your game to load this mod!") 
	return --I can only underestimate people's ability to not follow directions.
end

/*FREESLOTS, MY FELLA*/
freeslot("sfx_tric", "sfx_trice", "sfx_trifal", "sfx_acpick", 
"sfx_stocha", "sfx_storel",
"sfx_trstgo", "sfx_trstla", 
"sfx_augadd", "sfx_augmin") 
--trick start, trick combo, trickfail, system pick sfx, stomp, stomp land, augment up, augment down
for tsnd = 0,10 --charge sounds
	freeslot("sfx_tchg".. tsnd)
end

for tusnd = 0,2 --charge sounds
	freeslot("sfx_tu".. tusnd)
end

for tdsnd = 0,2 --charge sounds
	freeslot("sfx_td".. tdsnd)
end

for tsnd = 0, 5 --trick confirms
	freeslot("sfx_acgr".. tsnd)
end

for tsnd = 0, 5 --trick dashes
	freeslot("sfx_acgd".. tsnd)
end

freeslot("sfx_apprso")

--vfx freeslotting
freeslot("MT_BURSTAURA", "S_BURSTTRAIL", "S_BURSTAURA",
"MT_STORELINE", "MT_STORECHARGE", "S_STORELINE", "S_STORECHARGE",
"S_AUGRING", "MT_AUGSPARKLE", "S_AUGSPARKLE")

freeslot("SPR_ACBT", "SPR_ACBA", "SPR_ACST", "SPR_ACSC", "SPR_ACAR", "SPR_AUGR", "SPR_AUGS", "SPR_STOF", "SPR_STOS")

states[S_BURSTTRAIL] = {SPR_ACBT, FF_TRANS20|FF_PAPERSPRITE|FF_FULLBRIGHT, -1, nil, 7, 1, nil}
states[S_BURSTAURA] = {SPR_ACBA, FF_TRANS20|FF_ANIMATE|A, -1, nil, 4, 1, nil}
mobjinfo[MT_BURSTAURA] = {
	spawnstate = S_BURSTAURA,
	flags = MF_NOGRAVITY|MF_DONTENCOREMAP|MF_NOCLIPTHING,
	radius = 1*FRACUNIT,
	height = 1*FRACUNIT
}

states[S_STORELINE] = {SPR_STOF, FF_PAPERSPRITE|FF_FULLBRIGHT|FF_ANIMATE|A, -1, nil, 2, 1, nil}
states[S_STORECHARGE] = {SPR_STOS, FF_FULLBRIGHT, -1, nil, 2, 1, nil}
mobjinfo[MT_STORELINE] = {
	spawnstate = S_STORELINE,
	flags = MF_NOGRAVITY|MF_DONTENCOREMAP|MF_NOCLIPTHING,
	radius = 1*FRACUNIT,
	height = 1*FRACUNIT
}
mobjinfo[MT_STORECHARGE] = {
	spawnstate = S_STORECHARGE,
	flags = MF_NOGRAVITY|MF_DONTENCOREMAP|MF_NOCLIPTHING,
	radius = 1*FRACUNIT,
	height = 1*FRACUNIT
}
states[S_AUGRING] = {SPR_AUGR, FF_TRANS30, -1, nil, 0, 0, nil}
states[S_AUGSPARKLE] = {SPR_AUGS, FF_TRANS10|FF_FULLBRIGHT|FF_ANIMATE|A, -1, nil, 4, 2, nil}
mobjinfo[MT_AUGSPARKLE] = {
	spawnstate = S_AUGSPARKLE,
	flags = MF_NOGRAVITY|MF_DONTENCOREMAP|MF_NOCLIPTHING,
	radius = 1*FRACUNIT,
	height = 1*FRACUNIT
}
-----------|
---GLOBAL--|
----CVARS--|

rawset(_G, "ac_active", CV_RegisterVar({ --Mod toggle
	name = "ac_active",
	defaultvalue = "On",
	flags = CV_NETVAR,
	possiblevalue = CV_OnOff
}))

rawset(_G, "ac_textdebug", CV_RegisterVar({ --touch this if you don't wanna see the left side of your screen
  name = "ac_textdebug",
  defaultvalue = "Off",
  flags = CV_SHOWMODIF,
  PossibleValue = CV_OnOff,
}))

rawset(_G, "ac_debug", function(str)
	if ac_textdebug.value
		print("Acro: " .. str)
	end
end)

--couple of other things
rawset(_G, "stompDist", RING_DIST/2) --For Aggrobatics
rawset(_G, "maxStore", 360) --For STORE, and for hud funcs
rawset(_G, "acrohell", false) --Reserved for 'special' hell map functionality
rawset(_G, "booststacknerf", false) --boost stacking means i basically have to nerf the crap out of some things.
rawset(_G, "TS_BURST", 0)
rawset(_G, "TS_STORE", 1)
rawset(_G, "TS_AUGMENT", 2)
rawset(_G, "TS_HELL", 3)
local trickcolors = {
	[TS_BURST] = SKINCOLOR_LILAC, --kind of sucks that eggbombs take red already
	[TS_STORE] = SKINCOLOR_CYAN,
	[TS_AUGMENT] = SKINCOLOR_LIME,
	[TS_HELL] = P_RandomRange(1,MAXSKINCOLORS-1) --WHAT A THRILL
}

-----------|
---CHECKS--|
-----------|

--check for things like invincibility/growth/eggman stuff. either for colors, or statuses.
--returns TRUE if you're invince or growed [or if you were eggbombed if eggcheck is true]. growed is a word.

rawset(_G, "ac_statuscheck", function (p, eggcheck) --player, check for eggmans too for colorstuff?
	if p.kartstuff[k_invincibilitytimer] or 
	not(p.kartstuff[k_growshrinktimer] == 0) or --grow and shrink have their own thing 
	(eggcheck and p.kartstuff[k_eggmanexplode]) or 
	(tsrb2k and p.mo.tsr_ultimateon)
		return true
	else return false
	end
end)

rawset(_G, "ac_styleCheck", function(p, styCh, hellCh) --player, style to match, acrohell included?
	return p.trickstyle == styCh or (hellCh and acrohell)
end)

--Button check
local activebtn
rawset(_G, "ac_btnPress", function(p, btn)
	if btn == 0
		return (p.cmd.buttons & p.trick_button)
	elseif btn == 1
		return (p.cmd.buttons & BT_ATTACK)
	elseif btn == 2
		return (p.cmd.buttons & BT_DRIFT)
	end
	return (p.cmd.buttons & BT_ACCELERATE) --i dunno how you got here but i just wanna grill
end)

-----------|
--VISUALS--|
-----------|

-- copied from the source code, recreated for lua
-- some other mods have it so making sure i don't conflict is a good idea right
-- this just needs to be exposed already
rawset(_G, "ac_FlipFromObject", function (mo, master) --child mobj, master mobj
	mo.eflags = (mo.eflags & ~MFE_VERTICALFLIP) or (master.eflags & MFE_VERTICALFLIP)
	mo.flags2 = (mo.flags2 & ~MF2_OBJECTFLIP) or (master.flags2 & MF2_OBJECTFLIP)
	if (mo.eflags & MFE_VERTICALFLIP)
		mo.z = $ + master.height - FixedMul(master.scale, mo.height)
	end
end)

--Hardcode recreation.
local baseDrift, driftAng, driftWeight
rawset(_G, "K_GetKartDriftValue", function (p, csteer)
	driftWeight = p.kartweight*14

	if (p.kartstuff[k_drift] == 0) or not(P_IsObjectOnGround(p.mo))
		return 0 --you have to be drifting on the ground, you dingus
	end
	if (p.kartstuff[k_driftend] ~= 0)
		return -266*p.kartstuff[k_drift] --magic hardcode bullshit number
	end

	baseDrift = 83*p.kartstuff[k_drift] - (driftWeight - 14)*p.kartstuff[k_drift]/5 --i am NOT here to ask questions about numbers.
	driftAng = abs((252 - driftWeight)*p.kartstuff[k_drift]/5) --I'm here to recreate hardcode.

	return baseDrift + FixedMul(driftAng, csteer)
end)
/*
another hardcode recreation?
bitch you GUESSED IT

[HOO]
*/
local turnAdj, maxSpd
rawset(_G, "K_GetKartTurnValue", function (p, turnval)
	maxSpd = FixedMul(K_GetKartSpeed(p, false), 3*FRACUNIT)
	turnAdj = FixedDiv((maxSpd>>FRACBITS) - (p.speed>>FRACBITS), (maxSpd>>FRACBITS) + p.kartweight)

	if p.spectator then return turnval end --d'oy

	if p.kartstuff[k_drift] ~= 0 and P_IsObjectOnGround(p.mo)
		--Drifting has a different turn value...
		if (p.kartstuff[k_driftend] == 0)
			--countersteer needs to be accounted for because muh anticheat
			turnval = K_GetKartDriftValue(p, FixedDiv(turnval*FRACUNIT, 800*FRACUNIT))
		else
			turnval = $ + K_GetKartDriftValue(p, FRACUNIT)
		end
		return turnval
	end

	turnval = FixedMul(turnval, turnAdj) --apply weight to turning value.
	--the magic handler for adjust!! and uh, let's add an acro case real quick--
	if (p.kartstuff[k_invincibilitytimer] or p.kartstuff[k_sneakertimer] or p.kartstuff[k_growshrinktimer] > 0 or p.trickboost) --:^)
		turnval = FixedMul($, FixedDiv(5*FRACUNIT, 4*FRACUNIT))
	end
	
	return turnval
	
end)

local tdangle, newx, newy, dust, flash 
--from the source code, gutted and repuposed
rawset(_G, "ac_trickdust", function (p, dustsize, instant) --player, int, bool
	if dustsize == nil then dustsize = 1 end 
	if not(P_IsObjectOnGround(p.mo)) or ac_fastcheck(p, 35) == false or p.kartstuff[k_aizdriftstrat] then return end --yeah
	if acrohell then 
		ac_spawnSpark(p, false, p.mo.color)
		return
	end
	if leveltime%4 == 1 or instant--Kick up some dust every other frame
		for i = 0,1 do
			tdangle = p.mo.angle - (ANGLE_45/5)*p.kartstuff[k_drift]
			newx = p.mo.x + P_ReturnThrustX(p.mo, tdangle + ((i == 0) and -1 or 1)*ANGLE_90, FixedMul(24*FRACUNIT, p.mo.scale))			
			newy = p.mo.y + P_ReturnThrustY(p.mo, tdangle + ((i == 0) and -1 or 1)*ANGLE_90, FixedMul(24*FRACUNIT, p.mo.scale))
			dust = P_SpawnMobj(newx, newy, (verticalflip and (p.mo.ceilingz or -1) or p.mo.floorz), MT_SIGNSPARKLE)
			dust.target = p.mo
			dust.angle = tdangle - ((i == 0) and -1 or 1)*ANGLE_67h
			dust.scale = 4*p.mo.scale/(6-dustsize)
			dust.destscale = 8*p.mo.scale/(7-dustsize)
			dust.scalespeed = p.mo.scale/6
			dust.momx = 2*p.mo.momx/5
			dust.momy = 2*p.mo.momy/5
			dust.momz = 2*p.mo.momz/5
			dust.colorzed = true
			dust.color = acrohell and P_RandomRange(1,MAXSKINCOLORS-1) or trickcolors[p.trickstyle]
		end
	end
end)

--Originally ported to lua from source by PepperDork, "enhanced" by me
local ghost --2spooky
rawset(_G, "ac_ghosttrail", function (mo, intense) --mobj [NOT a player]
	if leveltime%3 > 0 return end
	ghost = P_SpawnMobj(mo.x, mo.y, mo.z, MT_GHOST)
	P_SetScale(ghost, mo.scale*(6/5))
	if(mo.eflags & MFE_VERTICALFLIP) then
		ghost.eflags = $|MFE_VERTICALFLIP
		ghost.z = mo.height - ghost.height
	end
	if mo.player then 
		ghost.angle = mo.player.frameangle 
		ghost.skin = mo.skin
	else 
		ghost.angle = mo.angle 
	end
	ghost.color = mo.color
	ghost.frame = mo.frame
	ghost.colorized = true
	ghost.sprite = mo.sprite
	ghost.frame = (intense and TR_TRANS40) or TR_TRANS70 
	ghost.tics = -1
	ghost.fuse = (intense and TICRATE/3) or TICRATE/7
	if(mo.flags2 & MF2_OBJECTFLIP) then
		ghost.flags2 = $|MF2_OBJECTFLIP
	end
	if(not(mo.flags & MF_DONTENCOREMAP))then
		ghost.flags = $|~MF_DONTENCOREMAP
	end
	
	return ghost
	
end)

local rad, sparkle, sparklex, sparkley, sparklez, blinkfreq


--Gutted from K_SpawnSparkleTrail -Ang
rawset(_G, "ac_tricksparkle", function (p, potential, spfreq, override) --player, var, random colors
/*	Potential:
	0 - Colors Only
	1 - Floor Sparkles
	2 - Full Sparkles [currently unused? it's essentially K_SpawnSparkleTrail.]
*/
	if spfreq == nil then spfreq = 2 end
	if potential == nil then potential = 0 end 
	if leveltime%4 > 2 and ac_statuscheck(p, false) == false
		p.mo.colorized = true
		if not(G_BattleGametype()) then p.mo.color = ((override ~= nil and override) or (acrohell and P_RandomRange(1,MAXSKINCOLORS-1))) or trickcolors[p.trickstyle] end --phew!
	else
		ac_colorcorrect(p)
	end 
	if potential > 0 and leveltime%spfreq == 1
		rad = (p.mo.radius*2)>>FRACBITS
		sparklex = p.mo.x + p.mo.momx + (P_RandomRange(-rad, rad)<<FRACBITS)
		sparkley = p.mo.y + p.mo.momy + (P_RandomRange(-rad, rad)<<FRACBITS)
		if potential == 2
			sparklez = p.mo.z + p.mo.momz + (P_RandomRange(0, p.mo.height>>FRACBITS)<<FRACBITS)
		else
			sparklez = p.mo.z + p.mo.momz + (P_RandomRange(-15, 0)<<FRACBITS)
		end
		sparkle = P_SpawnMobj(sparklex, sparkley, sparklez, MT_SIGNSPARKLE)
		ac_FlipFromObject(sparkle, p.mo)
		sparkle.target = p.mo 
		sparkle.destscale = p.mo.destscale
		P_SetScale(sparkle, p.mo.scale)
		sparkle.color = p.mo.color
		sparkle.colorized = true
		sparkle.state = S_SIGNSPARK5
	end
end)

local vfxX, vfxY, vfxZ, vfxMobj, vfxAngalt
local burstSizemin, burstSizemax = FixedDiv(FRACUNIT, 2*FRACUNIT), FixedDiv(13*FRACUNIT, 4*FRACUNIT)
rawset(_G, "ac_trickVfx", function (p, tstyle)

	if tstyle == TS_BURST
		if p.kartstuff[k_driftcharge] > 0 and p.trickboost 
			--check in on spark rate
			local sparkCheck = FixedDiv(p.kartstuff[k_driftcharge]*2, K_GetKartDriftSparkValue(p)) -- I HATE FIXED POINT MATH PART 6
		
			ac_debug("UH OH, BIG - Multiplier is" .. sparkCheck)
			sparkCheck = FixedMul(min(max($,burstSizemin),burstSizemax), FRACUNIT)
			ac_debug("CLAMP DOWN - Multiplier is" .. sparkCheck)
			
			vfxX = p.mo.x + P_ReturnThrustX(p.mo, p.mo.angle + ANGLE_180, FixedMul(FRACUNIT*30, p.mo.scale))
			vfxY = p.mo.y + P_ReturnThrustY(p.mo, p.mo.angle + ANGLE_180, FixedMul(FRACUNIT*30, p.mo.scale))
			vfxZ = p.mo.z + p.mo.height/2
			vfxMobj = P_SpawnMobj(vfxX, vfxY, vfxZ, MT_GHOST)
			vfxMobj.state = S_BURSTTRAIL
			vfxMobj.fuse = 8
			vfxMobj.scale = FixedMul(sparkCheck, p.mo.scale)
			vfxMobj.angle = R_PointToAngle2(p.mo.x, p.mo.y, p.mo.momx, p.mo.momy)
			vfxMobj.frame = leveltime%8
			vfxMobj.momx = 3*p.mo.momx/4
			vfxMobj.momy = 3*p.mo.momy/4
			vfxMobj.momz = p.mo.momz
			K_MatchGenericExtraFlags(vfxMobj, p.mo)
		end
	elseif tstyle == TS_STORE
		if ac_fastcheck(p, 20) and p.trickboost and leveltime%3 == 1
			vfxX = p.mo.x + (P_RandomRange(-36,36) * p.mo.scale)
			vfxY = p.mo.y + (P_RandomRange(-36,36) * p.mo.scale)
			vfxZ = p.mo.z + (p.mo.height/2) + (P_RandomRange(-20,20) * p.mo.scale)
			vfxMobj = P_SpawnMobj(vfxX, vfxY, vfxZ, MT_STORELINE)
			vfxMobj.angle = R_PointToAngle2(0, 0, p.mo.momx, p.mo.momy)
			vfxMobj.momx = 3*p.mo.momx/4
			vfxMobj.momy = 3*p.mo.momy/4
			vfxMobj.momz = 3*p.mo.momz/4
			vfxMobj.fuse = 8
			vfxMobj.scale = p.trickboostburst and p.mo.scale or P_RandomRange(p.mo.scale/3, 2*p.mo.scale/3)
			K_MatchGenericExtraFlags(vfxMobj, p.mo)
		end
		if p.sincelasttrick and leveltime%3 == 1--HACKY LMFAO
			vfxX, vfxY, vfxZ = p.mo.x, p.mo.y, p.mo.z
			vfxMobj = P_SpawnMobj(vfxX, vfxY, vfxZ, MT_STORECHARGE)
			vfxMobj.scale = p.mo.scale
			vfxMobj.frame = leveltime%5
			vfxMobj.fuse = 1
			vfxMobj.momx = p.mo.momx
			vfxMobj.momy = p.mo.momy
			vfxMobj.momz = p.mo.momz
			K_MatchGenericExtraFlags(vfxMobj, p.mo)
		end
	elseif tstyle == TS_AUGMENT
		--Healing sparkle effect.
		if leveltime%2 == 1 and (p.sincelasttrick or p.trickboost)
			rad = (p.mo.radius*2)>>FRACBITS
			vfxX = p.mo.x + p.mo.momx + (P_RandomRange(-rad, rad)<<FRACBITS)
			vfxY = p.mo.y + p.mo.momy + (P_RandomRange(-rad, rad)<<FRACBITS)
			vfxZ = p.mo.z + p.mo.momz + (P_RandomRange(0, p.mo.height>>FRACBITS)<<FRACBITS)
			vfxMobj = P_SpawnMobj(vfxX, vfxY, vfxZ, MT_AUGSPARKLE)
			--don't pass momentum vars because we're going to force teleport it + interp freakouts LOL
			vfxMobj.fuse = 14
			vfxMobj.target = p.mo
			vfxMobj.xOff, vfxMobj.yOff = p.mo.x - vfxMobj.x, p.mo.y - vfxMobj.y
			K_MatchGenericExtraFlags(vfxMobj, p.mo)
		end
	end

end)

local function ac_augsparkThink(mo)
	if not mo.valid then return end
	if mo.target
		P_TeleportMove(mo, mo.target.x + mo.xOff, mo.target.y + mo.yOff, mo.z)
		P_SetObjectMomZ(mo, FRACUNIT, true) 
		--interp builds REEEEEEEEEEEEEEEEE
		mo.momx, mo.momy = mo.target.momx, mo.target.momy
	end
end
addHook("MobjThinker", ac_augsparkThink, MT_AUGSPARKLE)

local function ac_burstThonk(mo)
	if not(mo.target)
		P_RemoveMobj(mo)
		return
	end
	if P_IsObjectOnGround(mo.target)
		mo.target.player.burstvfx.target = nil --a fucking mess
		P_RemoveMobj(mo)
		return
	else
		local travelangle = mo.target.angle+(ANG1*9)*mo.target.player.kartstuff[k_drift]
		local newx = mo.target.x + P_ReturnThrustX(mo.target, travelangle, FixedMul(32*FRACUNIT, mo.scale))
		local newy = mo.target.y + P_ReturnThrustY(mo.target, travelangle, FixedMul(32*FRACUNIT, mo.scale))
		P_TeleportMove(mo, newx, newy, mo.target.z)
		local auraAfter = ac_ghosttrail(mo, true)
		if auraAfter == nil then return end --this happened once so whatever i guess
		--auraAfter.fuse = 7
		auraAfter.flags = $|FF_FULLBRIGHT
		auraAfter.momx, auraAfter.momy, auraAfter.momz = 99*mo.momx/100, 99*mo.momy/100, mo.momz --god or something idfk
	end
end
addHook("MobjThinker", ac_burstThonk, MT_BURSTAURA)


--it's from, uh, something in the source, i forget what lol
--but it's barely noticable now?
local sparkdivide, sparkscatter
rawset(_G, "ac_sparkring", function (p, sparknum, sparkrand) --player, count of sparks for intensity, ranomdize sparks?
	if sparknum == nil or sparknum == 0 then sparknum = 16 end --fallback on dummy checks
	sparkdivide = FixedAngle(360*FRACUNIT/sparknum) --because ANGLE_MAX just HAS to be -1 in lua, fuckin hell
	for i = 1, sparknum do
		flash = P_SpawnMobj(p.mo.x, p.mo.y, p.mo.z, MT_SIGNSPARKLE)
		flash.target = p.mo
		flash.angle = i*sparkdivide --i*ANGLE_22h
		flash.scale = 4*p.mo.scale
		flash.destscale = 2*p.mo.scale
		flash.scalespeed = p.mo.scale/6
		sparkscatter = (sparkrand and P_RandomRange(-30, 10)*FRACUNIT) or 40*FRACUNIT
		P_InstaThrust(flash, flash.angle, sparkscatter)
		flash.colorized = true
		flash.color = p.mo.color
	end
end)

rawset(_G, "ac_colorcorrect", function (p) --player
	if not (p.mo or p.mo.valid) then return end --VALID CHEEEECKS
	if not(ac_statuscheck(p, true))
		p.mo.colorized = false
		p.mo.color = p.skincolor
	end
end)

--You ever recreate a source function in lua just to change ONE check
local spnewX, spnewY, aSpark, AStrvang
rawset(_G, "ac_spawnSpark", function (p, aironly, forcecolor)
	if (leveltime % 3 > 0) then return end
	
	if (P_IsObjectOnGround(p.mo) and aironly) then return end 

	AStrvang = p.mo.angle-(ANGLE_45/5)*p.kartstuff[k_drift]

	for i = 0, 1 do
		spnewX = p.mo.x + P_ReturnThrustX(p.mo, AStrvang + ((i == 0) and -1 or 1)*ANGLE_135, FixedMul(32*FRACUNIT, p.mo.scale))
		spnewY = p.mo.y + P_ReturnThrustY(p.mo, AStrvang + ((i == 0) and -1 or 1)*ANGLE_135, FixedMul(32*FRACUNIT, p.mo.scale))
		aSpark = P_SpawnMobj(spnewX, spnewY, p.mo.z, MT_DRIFTSPARK)
		aSpark.target = p.mo
		aSpark.angle = AStrvang-(ANGLE_45/5)*p.kartstuff[k_drift]
		aSpark.destscale = p.mo.scale
		P_SetScale(aSpark, 5*p.mo.scale/4)
		aSpark.momx = p.mo.momx/2
		aSpark.momy = p.mo.momy/2
		aSpark.momz = p.mo.momz/2 --we need this now!
		--aSpark.frame = TR_TRANS40
		
		if (p.kartstuff[k_driftcharge] >= K_GetKartDriftSparkValue(p)*4)
			aSpark.color = (1 + (p.airtime % (MAXSKINCOLORS-1)))
		elseif (p.kartstuff[k_driftcharge] >= K_GetKartDriftSparkValue(p)*2)
			if (p.kartstuff[k_driftcharge] <= (K_GetKartDriftSparkValue(p)*2)+(24*3))
				aSpark.color = SKINCOLOR_RASPBERRY --transition color
			else
				aSpark.color = SKINCOLOR_KETCHUP
			end
		else
			aSpark.color = forcecolor or SKINCOLOR_SAPPHIRE
		end
		
		--honestly this is kind of fucked up here ngl
		if ((p.kartstuff[k_drift] > 0 and p.cmd.driftturn > 0) or (p.kartstuff[k_drift] < 0 and p.cmd.driftturn < 0)) // Inward drifts
			if ((p.kartstuff[k_drift] < 0 and (i & 1)) or (p.kartstuff[k_drift] > 0 and !(i & 1)))
				aSpark.state = S_DRIFTSPARK_A1
			elseif ((p.kartstuff[k_drift] < 0 and not(i and 1)) or (p.kartstuff[k_drift] > 0 and (i and 1)))
				aSpark.state = S_DRIFTSPARK_C1
			end
		elseif ((p.kartstuff[k_drift] > 0 and p.cmd.driftturn < 0) or (p.kartstuff[k_drift] < 0 and p.cmd.driftturn > 0)) // Outward drifts
			if ((p.kartstuff[k_drift] < 0 and (i and 1)) or (p.kartstuff[k_drift] > 0 and not(i and 1)))
				aSpark.state = S_DRIFTSPARK_C1
			elseif ((p.kartstuff[k_drift] < 0 and !(i & 1))
				or (p.kartstuff[k_drift] > 0 and (i & 1)))
				aSpark.state = S_DRIFTSPARK_A1
			end
		end
		ac_FlipFromObject(aSpark, p.mo)
	end
	
end)

-----------|
----AUDIO--|
-----------|

rawset(_G, "ac_playtrickconfirm", function (p, dashlvl, jinglelvl, cust)
	if dashlvl
		S_StartSound(p.mo, _G["sfx_acgd" .. dashlvl])
	end
	if jinglelvl
		S_StartSound(p.mo, _G["sfx_acgr" .. jinglelvl], p)
	end
	if cust
		S_StartSound(p.mo, cust)
	end
	if dashlvl == nil and jinglelvl == nil and cust == nil
		ac_debug("HEY NUMBNUTS, MISSING SOMETHING?") --fuckin idiot
	end
end)

----I----|
--HATE---|
--MATH---|

--rescale from ashnal with some extra things
--value, oldmin, oldmax, newmin, newmax are all required.
--intreturn being not nil will return your result as a bitshifted int.
--clamp being true will clamp the function to actual written mins and maxes.
local rescaleresult
rawset(_G, "ac_rescale", function (value, oldmin, oldmax, newmin, newmax, intreturn, clamp) --value to rescale - works best with fixed point because FUCK YOU
	rescaleresult = newmin + FixedMul(FixedDiv( value-oldmin , oldmax-oldmin), newmax-newmin)
	if clamp then rescaleresult = min(max($, newmin), newmax) end
	if not(intreturn)
		return rescaleresult
	else
		return rescaleresult>>FRACBITS
	end
end)

--let the bodies hit the
rawset(_G, "ac_floor10", function (n) --this looks really fucking dumb until you realize it's gonna cauterize the "decimal"
	return (n/10)*10 
end)

--dumb
rawset(_G, "ac_fastcheck", function (p, limit)
	return (FixedDiv(p.speed, (mapheaderinfo[gamemap] and mapheaderinfo[gamemap].mobj_scale or FRACUNIT))/FRACUNIT >= limit)
end)



-----------|
----MISC---|
-----------|
--These may prove useful to someone elsewhere.

--Repurposed from P_Telekinesis  -- Changes being "check only player mobjs," and "vary the thurst power depending how close you ACTUALLY are."
local sourcedist, angpush, stompstr, thrustfactor --fracunit you binch
rawset(_G, "ac_stompPush", function (p, range, thrust) 
	for p2 in players.iterate do	
		if not p2.mo or not p2.mo.valid or p2.spectator then continue end 
		if p2.mo == p.mo then continue end --hello not yourself
		ac_debug("FOUND YOU")
		sourcedist = P_AproxDistance(P_AproxDistance(p.mo.x-p2.mo.x, p.mo.y-p2.mo.y), p.mo.z-p2.mo.z)

		if range < sourcedist then 
			ac_debug("BUT YOU'RE TOO FUCKIN FAR [" .. sourcedist>>FRACBITS .. " FU]") 
			continue 
		end
		
		if not(P_CheckSight(p.mo, p2.mo)) then --might not have
			ac_debug("BUT I CAN'T SEE SHIT CAPTAIN")
			continue 
		end 
		
		if ac_statuscheck(p2, false) or p2.kartstuff[k_respawn] or p2.powers[pw_flashing]
			ac_debug("BUT HE'S TOO FUCKING STRONG")
			continue 		
		end
		
		ac_debug("LOCAL DOOMED SUCKER AT [" .. sourcedist>>FRACBITS .. " FU]") 
		angpush = R_PointToAngle2(p.mo.x, p.mo.y, p2.mo.x, p2.mo.y);
		--we need to adjust the thrust dependent on how far away you are - doesn't make sense if you're at the edge to be pushed 500 fracs away lmao
		if (p2.mo.health > 0)
			P_Thrust(p2.mo, angpush, thrust)
			P_SetObjectMomZ(p2.mo, thrust*2/3, false)
			p2.trickstun = true
			ac_microSpinout(p2, 1)
			ac_debug("PUUUSH [" .. thrust>>FRACBITS .. " FU FORCE]")
		end
	end
end)

local bounceadjust, expl
rawset(_G, "ac_microSpinout", function (p, pain)
	S_StartSound(p.mo, sfx_trifal)
	if ac_statuscheck(p, false) then return false end --growth and invince be damned.
	if pain == nil then pain = 1 end --a little too deep for me
	expl = P_SpawnMobj(p.mo.x + p.mo.momx, p.mo.y + p.mo.momy, p.mo.z, MT_BALLHOGBOOM) 
	expl.color = p.mo.color
	expl.colorized = true
	ac_trickReset(p)
	p.trickstun = true
	bounceadjust = (p.mo.eflags & MFE_UNDERWATER) and 5 or 15 
	if pain < 4
		p.mo.momx = ($*2)/3
		p.mo.momy = ($*2)/3
		P_SetObjectMomZ(p.mo, (FixedMul(bounceadjust*FRACUNIT, p.mo.scale))*pain/3, false) 	
		P_SetScale(expl, p.mo.scale*2)
	else
		p.kartstuff[k_spinouttimer] = TICRATE/2 
		p.mo.momx = ($*3)/4
		p.mo.momy = ($*3)/4
		P_SetObjectMomZ(p.mo, (FixedMul(bounceadjust*FRACUNIT, p.mo.scale))*pain/3, false) 	
		P_SetScale(expl, p.mo.scale*3)
	end
	if pain > 1 
		S_StartSound(p.mo, sfx_khurt1+P_RandomRange(0, 1))
		if not splitscreen and p == displayplayers[0] then
			P_StartQuake(p.kartweight*2*FRACUNIT, TICRATE) --eggquakes are strong
		end
	end 
	if acrohell == true and pain > 1 --Well, you went and done it now
		p.kartstuff[k_spinouttimer] = 70 --Not quite SPB level... but we're getting there
		p.mo.momz = 12*pain*p.mo.scale*P_MobjFlip(p.mo)
		ac_debug("===A literal sink bomb")
	end
	
	ac_colorcorrect(p)
	ac_lockon_trickfail(p, pain)
	
end)

rawset(_G, "ac_trickReset", function (p, inf) --Reset a trick because of a fail of some sort. Takes inflictors, if necessary.
	if not(p) then return end --safeguards are fun
	if p.trickactive
		p.trickgrade = 0 -- Let's rub salt in that wound
		p.sincelasttrick = TICRATE
	end
	p.trickcharge = 0
	p.trickactive = false
	p.trickboost = 0
	p.trickboostburst = 0
	p.kartstuff[k_driftboost] = 0 --keeping charge is okay tho
	p.trickdenial = false
	if p.tricksneakerbuffer
		p.kartstuff[k_sneakertimer] = min(46, $)
	else
		p.kartstuff[k_sneakertimer] = 0
	end
	p.chaincount = 0
	if not(p.decaybuffer)
		if p.basebonus then S_StartSound(p.mo, sfx_augmin) end
		p.basebonus = max($ - (3*FRACUNIT/100), 0)
		p.basebonusgrade = 0
	end
	p.decaybuffer = 71
	ac_colorcorrect(p)
end)


rawset(_G, "acrofunc_loaded", 1) --safeguard