/*/

ACROBATICS MECHANICS BLOCK

/*/

if not acrofunc_loaded then return end --LOAD NO FURTHER CRIMINAL SCUM

/*

Optimization via Hannu 

Listen. This looks terrible, but this is faster. I know that sounds crazy.

Blua lives in hell and I have cohabited its residence.

*/
local TICRATE = TICRATE
local FRACUNIT = FRACUNIT
local ANGLE_22h = ANGLE_22h
local FRACBITS = FRACBITS
local k_sneakertimer = k_sneakertimer
local k_spinouttimer = k_spinouttimer
local k_wipeoutslow = k_wipeoutslow
local k_driftboost = k_driftboost
local k_driftcharge = k_driftcharge
local k_floorboost = k_floorboost
local k_startboost = k_startboost
local k_itemamount = k_itemamount
local k_itemtype = k_itemtype
local k_rocketsneakertimer = k_rocketsneakertimer
local k_hyudorotimer = k_hyudorotimer
local k_drift = k_drift
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

rawset(_G, "acrobatics", "v0.5") --hello?
local starttime = 6*TICRATE + (3*TICRATE/4) --fuck
local showmetrick = false

local trickremind = 0
local thisisindev = false --if this is still true on an official build i was being a certified dingbat and please let me know WHOOPS
-----------|
---CVARS---|
-----------|

local ac_toggled --we mostly use this for memory so acro doesn't turn off midmap [that's BAD]

local ac_forcetrick = CV_RegisterVar({ --for server owners who wanna force one trick style on everyone - keep it on "freestyle" for free trick switches
    name = "ac_forcetrick",
    defaultvalue = "freestyle",
    flags = CV_NETVAR|CV_SHOWMODIF, 
    PossibleValue = {freestyle = -1, burst = 0, store = 1, augment = 2},
})

local ac_trickreminder = CV_RegisterVar({
	name = "ac_trickreminder",
	defaultvalue = "On",
	flags = CV_SHOWMODIF,
	possiblevalue = CV_OnOff
})

local ac_debugbtn = CV_RegisterVar({
	name = "ac_debugbtn",
	defaultvalue = "Off",
	flags = CV_NETVAR|CV_SHOWMODIF|CV_NOSHOWHELP|CV_CHEAT,
	possiblevalue = CV_OnOff
})

local ac_acrohell = CV_RegisterVar({ -- -1 to have on all maps, 0 to disable completely, 1 for only hell maps
    name = "ac_acrohell",
    defaultvalue = "hellmap",
    flags = CV_NETVAR|CV_CHEAT|CV_SHOWMODIF, 
    PossibleValue = {forced = -1, disabled = 0, hellmap = 1}
})

local ac_SPBlock = CV_RegisterVar({ --curbing the frontrun in any way we can
  name = "ac_SPBlock",
  defaultvalue = "On",
  flags = CV_NETVAR|CV_NOSHOWHELP|CV_CHEAT,
  PossibleValue = CV_OnOff
})

local ac_prefstyletext = {[0] = "\x85 BURST", [1] = "\x88 STORE", [2] = "\x83 AUGMENT"} 
local errstr
--Easter Eggs inbound
local tslookup = {
	["0"] = TS_BURST, --WHY DO I HAVE TO PUT THE DAMNED ZERO IN QUOTES? I HATE THIS.
	burst = TS_BURST,
	b = TS_BURST,
	bur = TS_BURST,
	fayt = TS_BURST, --i hope halfpipe comes out before this does oops
	eldog = TS_BURST, --play "mew"pack
	freeman = TS_BURST, --death immortalized
	chad = TS_BURST, --there is no other option
	marsh = TS_BURST, --"i like it because it's like sonic riders" sHUT
	red = TS_BURST, --are you fucking 5 years old
	
	["1"] = TS_STORE,
	store = TS_STORE,
	s = TS_STORE,
	sto = TS_STORE,
	storedog = TS_STORE, --The OC pick
	ashnal = TS_STORE, --muh chaining
	jazz = TS_STORE, --s/o to casarm
	blue = TS_STORE, -- :|
	gashoarder = TS_STORE, --related words
	himie = TS_STORE, --watch precure
	
	["2"] = TS_AUGMENT,
	augment = TS_AUGMENT,
	a = TS_AUGMENT,
	aug = TS_AUGMENT,
	ivo = TS_AUGMENT, --motobug says hi
	logman = TS_AUGMENT, --if you see a flashing green flicky kill it at all costs
	vibe = TS_AUGMENT, --as coined by Nio Rose 
	star = TS_AUGMENT, --VERY VERY LONG K TURN RIGHT
	green = TS_AUGMENT, --now you're just takin the piss at this point
	
	yellow = -1, --well,
	ang = -1, --i haven't finished this...
	angular = -1 --...yet.
}
local function changetrickstyle(p, arg, ss)
	if ac_forcetrick.value > -1
		CONS_Printf(p, "\131*The server owner has forced the" .. ac_prefstyletext[ac_forcetrick.value] .. "\131 style on this server.")
		p.trickstyle = ac_forcetrick.value --paranoiaaaAAAAAAAAA
		S_StartSound(nil, sfx_acpick, p) --beep	
	else
		if arg == nil
			p.trickstyle = $ or TS_BURST --early init if you're an eager mcbeaver
			CONS_Printf(p, "\131*Your current trick style is" .. ac_prefstyletext[p.trickstyle] .. "\131 - type in another trick style to switch to it.")
			S_StartSound(nil, sfx_acpick, p) --beep
		else	
			arg = tostring(arg):lower() 
			if (not G_BattleGametype())
				if (tslookup[arg] ~= nil and tslookup[arg] ~= -1) then --ang learns about lookup table from callmore part 67
					p.trickmemory = tslookup[arg]
					if leveltime > starttime
						CONS_Printf(p, "\131*Trick style will be changed to" .. ac_prefstyletext[p.trickmemory] .. "\131 next race.")
					else
						CONS_Printf(p, "\131*Trick style has changed to" .. ac_prefstyletext[p.trickmemory] .. "\131.")
						p.trickstyle = p.trickmemory
					end
					if acrohell then CONS_Printf(p, "\131*But until then, \x85tough it out!") end
					S_StartSound(nil, sfx_acpick, p) --beep
				else
					if tslookup[arg] == -1
						CONS_Printf(p, "\131*Hm, this doesn't do anything yet, so...")
					end
					CONS_Printf(p, "\131*Pick a valid style -\x85 BURST\131,\x88 STORE\131, or\x83 AUGMENT\131.")
					S_StartSound(nil, sfx_trifal, p)
				end 
			else
				if G_BattleGametype() then errstr = "\131*Battle Mode tricks are locked to\x89 STOMP\131."
				else errstr = "\x85*You shouldn't be able to see this?" end
				CONS_Printf(p, errstr)
				S_StartSound(nil, sfx_trifal, p)
			end
		end
	end
end
COM_AddCommand("ac_trickstyle", changetrickstyle, 0)
COM_AddCommand("trickstyle", changetrickstyle, 0) 
COM_AddCommand("changetrick", changetrickstyle, 0)
COM_AddCommand("trickchange", changetrickstyle, 0)
COM_AddCommand("trick", changetrickstyle, 0)
COM_AddCommand("ts", changetrickstyle, 0)

local function trickchangebutton(p, arg)
	arg = tostring(arg):lower()
	if(arg == "brake" or (arg == "custom" or arg=="custom1")) then --NO REGEX AAAAAAAAAAAAAAAA
		if(arg == "brake") then 
			p.trickbtnmemory = BT_BRAKE
		elseif(arg == "custom" or arg == "custom1") then 
			p.trickbtnmemory = BT_CUSTOM1
			arg = "custom1"
		end 
		if tsrb2kr --TEAM SONIC RACING: Uses all the Customs, so you shouldn't change this, probably.
			CONS_Printf(p, "\133*WARNING:\131 TSRB2KR is active, CUSTOM1 cannot be used!")
			CONS_Printf(p, "\131* Trick button force changed to \130BRAKE\131.")
			p.trickbtnmemory = BT_BRAKE
			p.trick_button = p.trickbtnmemory
		else
			p.trick_button = p.trickbtnmemory
			CONS_Printf(p, "\131* Trick button changed to \130" .. arg:upper() .. "\131.")
		end
	else 
		CONS_Printf(p, "\135* Please choose between \130BRAKE \135or \130CUSTOM.")
	end 
	S_StartSound(nil, sfx_acpick, p) 
end
COM_AddCommand("ac_trickbtn", trickchangebutton, 0)
COM_AddCommand("changetrickbutton", trickchangebutton, 0)
COM_AddCommand("trickbutton", trickchangebutton, 0)
COM_AddCommand("trickbtn", trickchangebutton, 0)
COM_AddCommand("tbtn", trickchangebutton, 0)

---INIT----|
---AND-----|
---SYNC----|

local function ac_initacro(p)	
--PRETRICKS
    p.trickready = 1 -- In case a player joins in the MIDDLE of a netgame and these variables haven't been set. 
	p.trick_button = (p.trickbtnmemory ~= nil) and p.trickbtnmemory or BT_BRAKE -- Gotta call it somewhere.
	p.airtime = 0 --Airtimebuffer is important so tricks don't activate on tiny sized slope errors
	p.trickstun = false --trickfailstun
	p.trickdenial = false -- spb denial
---MIDTRICKS
	p.chaincount = 0 -- Counter for stomp - int
	p.trickcharge = 0 --measure your strength - int
	p.trickactive = false --is it? - bool
---AFTEREFFECTS
	p.trickmemory = $ or 0 --Use this to carry over trick preferences map-to-map.
	p.trickstyle = (ac_forcetrick.value > -1 and ac_forcetrick.value) or p.trickmemory
	p.trickboost = 0 -- Time-based [Low speed, max length] - int
	p.trickbooststr = ac_rescale(p.kartweight*FRACUNIT, 9*FRACUNIT, FRACUNIT, 3*FRACUNIT/20, 7*FRACUNIT/20) --Strength based [Low weight better] - fixed
	p.tricksneakerbuffer = 0 --Running buffer for true sneakers - int
---BURST SPECIFIC
	p.trickboostburst = 0 --Level 2 baby! - int
	p.zipdrift = 0 --Counts for zipdrifts - int
	p.burstvfx = nil
---STORE SPECIFIC (see storeThink)
	p.acrostore = 0 --Boost storage - int
	p.kspdfactor = 85+(p.kartspeed*12) --Faster chars will need more store to get the big boy boost!
---AUGMENT SPECIFIC (see applyAugment)
	p.basebonus = 0 --modifies as you pull successive tricks...! - fixed
	p.maxAug = ac_rescale(p.kartspeed*FRACUNIT, 9*FRACUNIT, FRACUNIT, FRACUNIT/10, FRACUNIT/5) 
	p.decaytimer = 0 --Don't go without tricking for too long...
	p.decaybuffer = 70 --Buffer so you don't lose every fucking augment at once
---HUD STUFF
	p.sincelasttrick = 0 --Timer for style messages. - int
	p.trickgrade = 0 --For style messages - int
	p.acrostoregrade = 0 -- previous bonus granted. - int
	p.basebonusgrade  = 0 --previous bonus granted. - fixed
---PREVTIC STUFF --Info stored in the previous tic for next tic comparisons
	p.ac_PTitem = 0
	p.ac_PTitemamount = 0
	p.ac_PTeggmanheld = 0
	p.ac_PTeggmanexplode = 0
	p.ac_PTsneakertimer = 0
	p.ac_PTrocketsneakertimer = 0
	p.ac_PTitembtn = 0
	p.ac_PTdriftbtn = 0
	p.ac_PTdriftcharge = 0
	p.ac_PTtrickboost = 0 
	p.ac_PTtrickbooststr = 0
	p.ac_PTmoz = p.mo.z
	p.ac_PTtrickcharge = 0
end 


local function ac_trickstyleHandler(p) --this entire function is a result of my paranoia
	if leveltime < 1 then return end
	
	if ac_forcetrick.value > -1 --a HARD force
		if not(p.trickstyle == ac_forcetrick.value)
			CONS_Printf(p, "\131*The server owner has forced the" .. ac_prefstyletext[ac_forcetrick.value] .. "\131 style on this server.")
		end
		p.trickstyle = ac_forcetrick.value
		return
	end
	
	if leveltime < starttime -- we don't wanna let 'em change stuff midrace, honestly.
		p.trickstyle = p.trickmemory
	end
	
end

-----------|
---PRETRICK|
-----------|
local pref_btn_str = "BRAKE"
local stylereminders =
{
	[TS_BURST] = "Landing a trick will \132rocket you forward!\128 Charge and release a drift to \132zipboost!",
	[TS_STORE] = "Landing a trick will \132store your boost.\128 Press \130ITEM \128without an item to use it!",
	[TS_AUGMENT] = "Landing a trick will \132raise your top speed.\128 Don't bump walls or die!"
}
local function ac_trickNag(p)
	if leveltime < 2*TICRATE then return end
	if p.trickbtnmemory == BT_CUSTOM1 --there's a better way to do this and i don't know what it is
		pref_btn_str = "CUSTOM1"
	else
		pref_btn_str = "BRAKE"
	end
	if showmetrick == false and leveltime > 4
		chatprintf(p, "*This server is running Acrobatics " .. acrobatics .. "\128: Type \131ac_help \128for a \131command list \128and \131other info!", true)
		if thisisindev then chatprintf(p,"*This version of Acrobatics is an indev build. \133Things may break!", true) end
		showmetrick = true
	end
		
	--Text Reminder.
	if(leveltime == 2*TICRATE and ac_trickreminder.value == 1 and trickremind >= 2)
		if not G_BattleGametype()
			if acrohell
				chatprintf(p, "\x85***ACROHELL is ACTIVE.\128 Spin and get randomized tricks... \x85\n ...but beware of failures!", true)
			else
				chatprintf(p, "*REMEMBER: You're running" .. ac_prefstyletext[p.trickstyle] .. "\128 style:\n" .. stylereminders[p.trickstyle], true)
				if ac_forcetrick.value == -1 then chatprintf(p, "*...and you can type \130ts #\128 in the console to change your \132trick style!", true) end
			end
			
		else
			chatprintf(p, "*REMEMBER: Press \130" .. pref_btn_str .. "\128 in midair to \132hop and stomp!", true)
		end
			trickremind = 0 --reset
	end
end

local function ac_acrohellHandler(p)
	if not(leveltime == 2*TICRATE) or acrohell or G_BattleGametype() then return end --once 
	
	if ((mapheaderinfo[gamemap].menuflags % 2 >= 1 and ac_acrohell.value) or --hellmapcheck
	ac_acrohell.value == -1) and --or you forced it you sick fuck
	ac_forcetrick.value < 0 --AND confirmed that you really do want to suffer
		acrohell = true --[sigh]
		S_StartSound(nil, sfx_noooo2)
	end
end

local function ac_trickCheck(p) --vibe check
    return (ac_toggled and --is the mod on [here because there's some dependencies within the mod that other mods reference, fuck me]
	not P_IsObjectOnGround(p.mo) and --Are you in the air?
	not p.kartstuff[k_respawn] and  --and not respawning
	p.kartstuff[k_spinouttimer] == 0 and --and not spinning out
	p.playerstate ~= PST_DEAD and --and not fucking dead
	not p.mo.drifthold and  --fucking mario kart
	not p.trickstun) --trickfailstun
end 


local function ac_trickStart(p)
	if not (G_BattleGametype())
		if ((p.mo.z > p.ac_PTmoz) or (p.mo.z < p.ac_PTmoz and p.mo.eflags & MFE_VERTICALFLIP)) then -- shoutouts to simpleflipgoop
			if p.trickdenial
				p.trickstun = true
				S_StartSound(p.mo, sfx_khurt1+P_RandomRange(0, 1))
				S_StartSound(p.mo, sfx_itrole, p)
				ac_sparkring(p, 8, true)
				return
			end
			p.trickactive = true
			if p.trickboost == 0
				S_StartSound(p.mo, sfx_tric)
			else
				S_StartSound(p.mo, sfx_trice)
			end
			p.basebonusgrade = 0 --reset % gains
			p.acrostoregrade = 0
		end
	elseif p.airtime > 3
		p.trickactive = true
		S_StartSound(p.mo, sfx_tric)
		p.mo.eflags = $|MFE_SPRUNG
		p.mo.momz = 28*FRACUNIT --Reset momentum
		p.kartstuff[k_pogospring] = 1
		p.chaincount = 1
		ac_sparkring(p, 32)
		p.airtime = 0
	end	
end

-----------|
---MIDTRICK|
-----------|
local chargetier, tcreduced
local trickcharged = {
        [0] = -1, --i hate blua so much
        [1] = 1, 
        [15] = 2, 
        [22] = 3,
        [27] = 4, 
        [29] = 5,
        [30] = 4,
        [32] = 1,
        [33] = 0
    }
local function ac_chargeCheck(p) --returns 1-5 depending on charge levels, or 0 if you don't get it.
    chargetier = nil
    tcreduced = p.trickcharge / 10
    if p.trickcharge and (p.trickcharge < 10) then tcreduced = 1 end
	
    while chargetier == nil do
		if tcreduced >= 33 then return 0 end --remove this and witness purgatory 
        if trickcharged[tcreduced] then chargetier = trickcharged[tcreduced] end
        tcreduced = $ - 1
    end
    return chargetier
end 

local spinfactor, spindiff
local function ac_trickSpin(p) --The actual spinning.
	ac_tricksparkle(p, 2, 4)
	if G_BattleGametype() then return end --nah
	if p.kartstuff[k_pogospring]
		p.frameangle = p.mo.angle -- let's reset this
	end
	if ac_chargeCheck(p) >= 4
		spinfactor = 2*max(p.airtime, 1)
		p.mo.colorized = true
	elseif ac_chargeCheck(p) > 0
		spinfactor = max(p.airtime, 1) --jesus don't look at this garbage
	else -- out of control spin.
		spinfactor = 4*max(p.airtime, 1)
	end
	spindiff = ANGLE_22h*spinfactor
	p.frameangle = $ + spindiff
	
end 

local trickroul
local function ac_acrohellSwitch(p) --dear lord.
	if not acrohell then return end
	trickroul = leveltime%3 --The world's worst PRNG function
	p.trickstyle = trickroul
	ac_debug("======LET'S MIX IT UP")
end


local function ac_chargeHandler(p)
	--RACE: Meter Handler
	if p.trickactive == true and not(G_BattleGametype())
		ac_acrohellSwitch(p)
		ac_trickSpin(p)
		if (ac_styleCheck(p, TS_BURST, true)) and p.chaincount > 0
			p.trickboost = max($, 5) --Maintain combo!
		end
		if ac_btnPress(p, 0)
			p.trickcharge = min($ + 10, 400) 
		else
			p.trickcharge = max($ - 5, 0) 
		end
		--Charge Sound
		if p.ac_PTtrickcharge ~= p.trickcharge
			if p.trickcharge%40 == 0 then S_StartSound(p.mo, _G["sfx_tchg" .. p.trickcharge/40], p) end
			if p.ac_PTtrickcharge > p.trickcharge
				S_StartSound(p.mo, _G["sfx_td" .. p.trickstyle], p)
			else
				S_StartSound(p.mo, _G["sfx_tu" .. p.trickstyle], p)
			end
		end
	end
				
	---BATTLE: Count Handler
	if G_BattleGametype() and p.chaincount --spin is handled by pogospring lol
		if p.chaincount == 1	
			ac_tricksparkle(p, 2)
			if ac_btnPress(p, 0) and p.airtime > 18 and p.kartstuff[k_bumper]
				P_InstaThrust(p.mo, p.mo.angle, 0) --STOP!
				p.mo.momz = -45*FRACUNIT --Hard force it downward
				S_StartSound(p.mo, sfx_trstgo)
				p.chaincount = 2 
				--p.kartstuff[k_pogospring] = 0
				if(P_RandomChance(FRACUNIT/2)) then K_PlayAttackTaunt(p.mo) end
			end
		else
			ac_sparkring(p, P_RandomRange(4, 8))
			ac_ghosttrail(p.mo, true)
		end		
	end	
	
end
-----------|
--POSTTRICK|
-----------|
local uh
local function ac_getBonusboost(p)

	if ac_styleCheck(p, TS_BURST, false) --BURST
		uh = ac_rescale(p.kartspeed<<FRACBITS, 9<<FRACBITS, 1<<FRACBITS, 18<<FRACBITS, 35<<FRACBITS, true)
	elseif ac_styleCheck(p, TS_STORE, false) --STORE
		uh = 30 --we're returning store, not trickboost in this case!
	elseif ac_styleCheck(p, TS_AUGMENT, false) --AUGMENT
		uh = ac_rescale(p.kartspeed<<FRACBITS, 9<<FRACBITS, 1<<FRACBITS, 10<<FRACBITS, 18<<FRACBITS, true) 
	end
	
	if ac_chargeCheck(p) > 0
		return uh*ac_chargeCheck(p)
	end
	
	return 0 --default for a blowout
	
end

local function ac_trickLand(p, tstyle)
	
	p.decaytimer = 0 --Reset decay timer.
	p.decaybuffer = TICRATE --I might be sadistic for letting you lose it 0.001 seconds after landing, so let's not.	
	if tstyle == TS_BURST or tstyle == nil--Burst - It's all going to a full-on rush!
		ac_playtrickconfirm(p, ac_chargeCheck(p), ac_chargeCheck(p)) --Play a juicy sound
		p.trickboost = $ + ac_getBonusboost(p) --Apply bonuses across the board
		p.trickboostburst = p.trickboost/2
		p.kartstuff[k_destboostcam] = FixedMul(FRACUNIT, FixedDiv(FRACUNIT/2 - ((6 - ac_chargeCheck(p))*FRACUNIT)/10, FRACUNIT/2) ) --This number doesn't make fucking sense but it works
		ac_trickdust(p, 3, true) --Particularly large boosh			
		p.trickgrade = ac_chargeCheck(p)
		p.zipdrift = 0 --Reset zipdrift count
		if p.chaincount > 1
			p.trickboost = $ + max(p.airtime/2, 20)
		end
		p.chaincount = $+1
		if ac_chargeCheck(p) == 5
			K_PlayPowerGloatSound(p.mo) --You earned it, champ
		else
			if(P_RandomChance(FRACUNIT / (7 - ac_chargeCheck(p) ))) then K_PlayBoostTaunt(p.mo) end --I'm still not sure if play taunt takes 'tasteful' into account
		end
	end						
	
	if tstyle == TS_STORE --Store - Save it for later.
		ac_playtrickconfirm(p, 3, ac_chargeCheck(p), sfx_s3k78)
		p.trickboost = 5
		p.trickboostburst = 5
		if p.acrostore == maxStore
			p.trickgrade = min(ac_chargeCheck(p), 4)
			if(P_RandomChance(FRACUNIT/(7 - ac_chargeCheck(p)))) then K_PlayBoostTaunt(p.mo) end 
		else
			p.acrostoregrade = ac_getBonusboost(p)
			p.acrostore = min($ + p.acrostoregrade, maxStore)
			if p.acrostore == maxStore
				p.trickgrade = 6
				K_PlayPowerGloatSound(p.mo)
			else
				p.trickgrade = 7
			end
		end
	end
					
	if tstyle == TS_AUGMENT --Augment - A bit of a boost for stats.
		if p.basebonus < p.maxAug
			p.trickboost = ac_getBonusboost(p)
			p.trickboostburst = p.trickboost 
											
			p.basebonusgrade = ac_chargeCheck(p)*p.maxAug*max(p.kartspeed/3, 1)/20
			p.basebonus = min($ + p.basebonusgrade, p.maxAug)
			if p.basebonus == p.maxAug
				p.trickgrade = 6
				K_PlayOvertakeSound(p.mo)
				p.trickboost = ac_chargeCheck(p)* 3
				p.trickboostburst = p.trickboost/4
				ac_playtrickconfirm(p, ac_chargeCheck(p), 5, sfx_augadd)
			else
				p.trickgrade = ac_chargeCheck(p)
				ac_playtrickconfirm(p, ac_chargeCheck(p), ac_chargeCheck(p), sfx_augadd)
				--LOOK, A NUT
				if p.basebonus > p.maxAug*4/5 and P_RandomChance(FRACUNIT/1000)
					S_StartSound(p.mo, sfx_apprso) --if one person hears this and questions their life at night i've done my job
				end
			end
			
		else --Already at max? Get some deferred trickboosts.
			ac_playtrickconfirm(p, ac_chargeCheck(p), ac_chargeCheck(p))
			p.trickgrade = ac_chargeCheck(p)
			p.trickboost = ac_chargeCheck(p)* 4 --Unstackable by design
			p.trickboostburst = p.trickboost/2
			if(P_RandomChance(FRACUNIT/(7 - ac_chargeCheck(p)))) then K_PlayBoostTaunt(p.mo) end 
		end			
		local augRing = P_SpawnMobj(p.mo.x, p.mo.y, p.mo.z, MT_GHOST)
		augRing.state = S_AUGRING
		augRing.fuse = 20
		augRing.scale = p.mo.scale/3
		augRing.scalespeed = FRACUNIT>>2
		augRing.destscale = p.mo.scale*2
		augRing.angle = R_PointToAngle2(0, 0, p.mo.momx, p.mo.momy) + ANGLE_90
		augRing.momx = 2*p.mo.momx/3
		augRing.momy = 2*p.mo.momy/3
		augRing.momz = p.mo.momz
		K_MatchGenericExtraFlags(augRing, p.mo)
	end
	
end

local failgrade
local function ac_failCheck(p) --Wipeout check - TRUE if you wiped, FALSE if you didn't
	if ac_chargeCheck(p) <= 0  
		S_StartSound(p.mo, sfx_acgr0, p)
		ac_microSpinout(p, 3)
		p.trickgrade = 0 
		ac_debug("=====kaboom")
		return true
	end	
	if ac_btnPress(p, 0) --Don't have the trick button down.
		if ac_chargeCheck(p) >= 4
			S_StartSound(p.mo, sfx_khurt1+P_RandomRange(0, 1))
			ac_microSpinout(p, 2)
		end
		p.trickgrade = 0 
		p.trickboost = 0
		p.kartstuff[k_driftboost] = 0 --Forfeited Driftstore!
		p.kartstuff[k_driftcharge] = 0
		if p.tricksneakerbuffer
			p.kartstuff[k_sneakertimer] = min(46, $)
		else
			p.kartstuff[k_sneakertimer] = 0
		end
		ac_playtrickconfirm(p, nil, 1)
		ac_colorcorrect(p)
		return true
	end
	return false --oh, you passed!
end

-----------|
--EFFECTS--|
-----------|

--Adapted from FirmSneaker by Tyron, used and mangled with permission
local function ac_sneakerCheck(p)

	if p.kartstuff[k_rocketsneakertimer] < p.ac_PTrocketsneakertimer-1 --Using the rocket sneaks bites a HUGE chunk out of the timer
		p.tricksneakerbuffer = p.kartstuff[k_sneakertimer]+1 --A buffer that means "we're in a sneaker now don't disturb"
	end

	if p.kartstuff[k_floorboost]
		p.tricksneakerbuffer = p.kartstuff[k_sneakertimer]+1
	end
		
	if p.ac_PTitem ~= nil and --Sneaker Item. 
		((p.ac_PTitem == KITEM_SNEAKER and p.kartstuff[k_itemtype] ~= KITEM_SNEAKER) or --1 Sneaker
		(p.kartstuff[k_itemtype] == KITEM_SNEAKER and p.kartstuff[k_itemamount] < p.ac_PTitemamount)) --2+ Sneaker
		p.tricksneakerbuffer = p.kartstuff[k_sneakertimer]+1
	end
	
	if (p.kartstuff[k_sneakertimer] > p.ac_PTsneakertimer) and (p.trickboost < p.ac_PTtrickboost) --Something else is making us sneaker -- and the sneaker timer didn't come with us?
		p.tricksneakerbuffer = p.kartstuff[k_sneakertimer]+1
	end
	
	if ac_styleCheck(p, TS_BURST, true) and p.trickboost --BURST chaining on sneakertimers.
		p.trickboost = max(p.tricksneakerbuffer, $)
	end
	
end

--BURST affects drift more than anything.
local zipadjust, zipclamp, dSpark, dSparkAdj, bFlame
local function ac_burstDrift(p)
	if not(ac_styleCheck(p, TS_BURST, true)) then return end
	if p.kartstuff[k_offroad] then return end
	
	dSpark = K_GetKartDriftSparkValue(p)

	if p.ac_PTdriftcharge < p.kartstuff[k_driftcharge] and 
	p.ac_PTdriftcharge < (dSpark - 1) and 
	p.kartstuff[k_driftcharge] < (dSpark - 1) and
	(p.cmd.buttons & BT_DRIFT) then
		dSparkAdj = p.kartweight*2
		p.kartstuff[k_driftcharge] = min($ + 4*dSparkAdj, dSpark - 1) --i'm gonna let vanilla handle the sound charge LOL
		ac_debug("1000% SPARKING")
	end
	
	if p.kartstuff[k_driftboost] and
	p.ac_PTdriftcharge > p.kartstuff[k_driftcharge] and  --Previously you had drift charge
	p.ac_PTdriftcharge >= dSpark then --and it was eligible for sparks
		zipclamp = K_GetKartSpeed(p, true)
		if not(P_IsObjectOnGround(p.mo))
			zipclamp = $/2
			p.burstvfx = P_SpawnMobj(p.mo.x, p.mo.y, p.mo.z, MT_BURSTAURA)
			p.burstvfx.target = p.mo
		end
		zipadjust =  max(min(p.speed/2, zipclamp), 40*FRACUNIT) --i guess idfk
		P_Thrust(p.mo, p.mo.angle+(-266*p.kartstuff[k_driftend]), zipadjust)
		S_StartSound(p.mo, sfx_cdfm62)
		S_StartSound(p.mo, sfx_s3k48)
		p.zipdrift = $ + 1
		--p.kartstuff[k_pogospring] = 0
		ac_trickdust(p, 3, true) --Particularly large boosh
		if P_RandomChance(FRACUNIT/4) then K_PlayAttackTaunt(p.mo) end
		p.trickboostburst = $ + min(p.kartstuff[k_driftboost], 125) --cap it at a rainbow, that's real plenty!
		p.trickboost = max($, p.trickboostburst) --Bring up the total with you.
		ac_debug("FLYING OFF THE HANDLEBARS")
		bFlame = P_SpawnMobj(p.mo.x, p.mo.y, p.mo.z, MT_BOOSTFLAME)
		bFlame.target = p.mo
		P_SetScale(bFlame, p.mo.scale)
		ac_FlipFromObject(bFlame, p.mo)
	end

end

local gSpeedAdj = { --ratios hastily derived from source
	[0] = 8*FRACUNIT/5,
	[1] = FRACUNIT,
	[2] = 2*FRACUNIT/3
}
local burstDiff, finaltrickboost
local function ac_applyTrickboost(p)
	if G_BattleGametype() then return end 
	if p.trickboost > 0 
		if not(p.tricksneakerbuffer) then p.kartstuff[k_sneakertimer] = max($, p.trickboost-1) end
		
		p.trickbooststr =  FixedMul((ac_rescale(p.kartweight*FRACUNIT, 9*FRACUNIT, FRACUNIT, 4*FRACUNIT/25, 2*FRACUNIT/5)), gSpeedAdj[gamespeed])
		burstDiff = p.trickbooststr/5
		if p.trickboostburst --I'm you but stronger
			p.trickbooststr = $ + burstDiff
			ac_ghosttrail(p.mo, true) --Effect?
			ac_trickdust(p, 0, false)
		end
		
		finaltrickboost = p.trickbooststr
		--Applying the boost is less straightforward because a trickboost may be less then a sneaker boost [which is being applied by sneakertimer]
		if not(p.tricksneakerbuffer) 
			
			if p.ac_PTtrickbooststr > p.trickbooststr --manual application of value smoothing - if you're gonna hijack the boost func, you gotta own up to it
				finaltrickboost = p.ac_PTtrickbooststr + (p.trickbooststr - p.ac_PTtrickbooststr)/(TICRATE/2);
			else 
				finaltrickboost = p.trickbooststr
			end
			
			--Offroad penalties are harsh - let's keep our grass tall
			if p.kartstuff[k_offroad] and not(ac_statuscheck(p, false))
				finaltrickboost = 0 
				p.kartstuff[k_sneakertimer] = 0 --Reapply offroad pain if no true sneaker
				ac_colorcorrect(p)
				p.kartstuff[k_accelboost] = 0
				--kmp_offroad 2: revenge of store
				if p.kartstuff[k_offroad] > FRACUNIT and ac_styleCheck(p, TS_STORE, true) then 
					p.kartstuff[k_driftcharge] = 0 --back to home depot with you.
				end
			elseif not(ac_statuscheck(p, false))
				if not(tsrb2kr and p.mo.tsr_ultimateon) or not(p.trickboostburst) --jesus.
					p.kartstuff[k_accelboost] = $ - 8*FRACUNIT 
				end
				p.kartstuff[k_accelboost] = max($, p.trickboostburst and 10*FRACUNIT or 2*FRACUNIT) --200% accel increase.
				if tsrb2kr and p.mo.tsr_ultimateon 
					p.kartstuff[k_speedboost] = $ + (p.trickbooststr/10) --What, is wipezones level speed not enough for you?
				else
					p.kartstuff[k_speedboost] = finaltrickboost --forced here deliberately.
				end
			end	

		end
		ac_burstDrift(p)--BURST effects.
		ac_lockon_trickboostthink(p) --lockon some extra shit
		ac_tricksparkle(p, 0)
		if p.trickboost == 1  
			ac_colorcorrect(p)
		end
		
	else
		p.chaincount = 0
		p.zipdrift = 0 --Reset zipdrift count
	end

end

--STORE 
local function ac_storeThink(p)

	if not(ac_styleCheck(p, TS_STORE, true)) or p.exiting then return end --go no further for no store.
	
	--THE FUCKING TERRIBLE ITEM BUTTON PRESS CHECK:tm:
	if not(ac_btnPress(p, 1)) --item btn pressed!
	or ((p.kartstuff[k_itemroulette] or --AND no roulette going
	p.kartstuff[k_itemtype] or --AAAAND not having an item
	p.kartstuff[k_itemheld] or --AAAAAAND not HOLDING an item
	p.kartstuff[k_rocketsneakertimer] or --ORROCKETSNEAKERSAAAAAAAAAAAAAAAAAAAAAAA
	p.kartstuff[k_eggmanheld] or --WHY IS THIS A SEPARATE HOLD CHECK FUUUUUUUUU
	p.ac_PTeggmanheld or --what's with eggbombs getting the royal treatment?
	p.kartstuff[k_eggmanexplode] or --because early explosions wasted them
	p.ac_PTeggmanexplode) or --seriously? come on.
	p.ac_PTrocketsneakertimer or --this is getting ridiculous
	p.ac_PTitem ~= KITEM_NONE) or --accidential activation of an item
	p.kartstuff[k_respawn] --respawn?
		return
	end
	
	if booststacknerf and p.trickboost then return end --I don't really fundmentally agree with this for consistency's sake but it'll do.
	
	if p.acrostore == 0 or p.kartstuff[k_offroad] > FRACUNIT --couple of related functions to empty boost meter asnd offroad.
		if P_IsObjectOnGround(p.mo) and not(p.ac_PTitembtn) 
			S_StartSound(p.mo, sfx_cdfm50)
			ac_trickdust(p, 1, true)
			if not(ac_fastcheck(p, 1)) and not(p.exiting) then P_Thrust(p.mo, p.mo.angle, FRACUNIT*4) end --lol
			ac_debug("==you can (not) boost")
		end
	else --otherwise, we're in the clear...
		if not(p.ac_PTitembtn) and p.ac_PTitem == KITEM_NONE --The initial storeboost is pretty strong!
			if p.acrostore > p.kspdfactor ---Provided you got enough.
				p.trickboost = $ + 100
				p.trickboostburst = max($, 25)
				p.acrostore = max($ - p.kspdfactor, 0)
				S_StartSound(p.mo, sfx_acgd2)
				S_StartSound(p.mo, sfx_storel)
				K_PlayBoostTaunt(p.mo)
				ac_debug("==next person who says they're rings gets thwacked [".. p.kspdfactor .." used]")
			else --or get an itty bitty one.
				p.trickboost = $ + min(max(p.acrostore, 25), 50) --really want a mid() function now
				p.trickboostburst = max($, 10)
				p.acrostore = max($ - 40, 0)
				S_StartSound(p.mo, sfx_acgd2)
				S_StartSound(p.mo, sfx_storel)
				ac_debug("==congrats you played yourself[".. p.kspdfactor - p.acrostore .." from fullboost]")
			end
			ac_sparkring(p, 10) 
			p.powers[pw_flashing] = 0 --Forfeit your flashtics on store.
		elseif p.ac_PTitembtn%40 == 39 and p.kartstuff[k_growcancel] < 0 --WHY is this -1?
			ac_debug("==PUT IN " .. min(p.acrostore, 40) .. " ACROSTORE")
			p.acrostore = max($ - 40, 0) --the last boost is a lagniappe, lucky you
			p.trickboost = $ + 46 --Like a weakass sneak
			ac_sparkring(p, 6) 
			ac_trickdust(p, 2, true)
			ac_debug("==CONVERTED TO 40 TICS O' BOOST")
			S_StartSound(p.mo, sfx_acgd1, p)
		end
	end
	
end


local augDecmax, finalAug, decayTime
--The AUGMENT style adds a tiny base speed bonus.
local function ac_applyAugment(p)

	--Here so it persists on character selection.
	p.maxAug = ac_rescale(p.kartspeed*FRACUNIT, 9*FRACUNIT, FRACUNIT, FRACUNIT/10, FRACUNIT/5) 
	
	if not(ac_styleCheck(p, TS_AUGMENT, true)) or p.exiting then return end
	
	augDecmax = p.maxAug/3
	if p.basebonus
		p.basebonus = min($, p.maxAug) --paranoia?
		--bumpcheck
		if (p.mo.eflags & MFE_JUSTBOUNCEDWALL) and not(p.trickactive) and not(p.decaybuffer) and not(ac_statuscheck(p, false))
			ac_debug("=====:b:onk :b:enalty")
			p.basebonus = max($*3/4, 0) -- -25% penalty.
			if p.basebonus < FRACUNIT/100 then p.basebonus = 0 end --dumb rounding
			if p.basebonus == 0 then ac_microSpinout(p, 1) end
			S_StartSound(p.mo, sfx_augmin)
			p.trickgrade = 0 --lol
			ac_sparkring(p, 8, true) 
			p.decaybuffer = 51 -- gets -1 on the same tic lol	
			p.decaytimer = 0	   
		end
		
		if not(ac_statuscheck(p, false))
			--Decay rate is tied to WEIGHT. AHAHAHAHAHAHAHA
			decayTime = (16 - p.kartweight/2)*TICRATE 
			if p.basebonus > augDecmax -- If you're eligibile for decay, which is anything over 1/4th your total augment
				if not(ac_fastcheck(p, 50))
					p.decaytimer = $ + 1 
					ac_debug("GOTTA STAY FAST - " .. p.decaytimer)
				end
				if p.kartstuff[k_offroad] > FRACUNIT --double penalty for offroad!!
					p.decaytimer = $ + 1 
				end
				if p.decaytimer > (decayTime - 105) and p.decaytimer <= decayTime and leveltime%14 == 1 -- WARNING
					ac_sparkring(p, 1, true) 
					S_StartSound(p.mo, sfx_s3kc3s) 
				end
				if p.decaytimer > decayTime --now you've done it
					S_StartSound(p.mo, sfx_augmin) 
					p.basebonus = max(augDecmax, $ - p.maxAug/4)
					p.decaytimer = 0
					ac_debug("CRAWWWLWLING IN MY SKIIIIIN")
					ac_sparkring(p, 8, true) 
					p.decaybuffer = 71  
				end
			else
				p.decaytimer = 0
			end
			
		end
	end 
	
	--For the actual augment... k_boostpower editing for base speed adjusts. How forbidden.
	finalAug = FRACUNIT + min(p.basebonus, p.maxAug)

	--might as well make sure i don't screw my own mods over
	if citytrial and p.ct_stats.booststat then finalAug = $ + (p.ct_stats.booststat*FRACUNIT)/100 end
	if waytoofast and p.ccboost then finalAug = $ + p.ccboost end
			
	--reapply the offroad penalties ourselves.
	if p.kartstuff[k_bananadrag] > TICRATE 
		finalAug = 4*FRACUNIT/5 --hardforced
	end
				
	if p.kartstuff[k_offroad] > 0 and not(p.kartstuff[k_invincibilitytimer] or p.kartstuff[k_hyudorotimer] or p.kartstuff[k_sneakertimer])
		finalAug = FixedDiv(min($, FRACUNIT), p.kartstuff[k_offroad] + FRACUNIT)
	end
		
	p.kartstuff[k_boostpower] = finalAug 
	
	--Related visuals
	if finalAug > FRACUNIT
		if (finalAug == FRACUNIT + p.maxAug) and ac_fastcheck(p, 35) --gotta go fast!
			ac_ghosttrail(p.mo)
		end
		ac_tricksparkle(p, 0)
		if leveltime%12 == 1 then ac_trickVfx(p, TS_AUGMENT) end
	elseif finalAug < FRACUNIT
		ac_colorcorrect(p)
	end
	
end

-- We'll come back to this in a second-
local ORIG_FRICTION = 62914 --hard code numbers lololol
local function ac_frictionRestore(mo)
	if not(mo and mo.valid and mo.player) then return end
	/*TODO: you'll need to tackle this demon eventually.*/
end

-----------|
--TIMERS-&-|
--PREVSTORE|


local function ac_storePTvars(p) --Decrement all tic timers, and store information for next tic to make comparisons
	p.trickboost = max($ - 1, 0) 
	p.trickboostburst = max($ - 1, 0) 
	p.tricksneakerbuffer = max($ - 1, 0) 
	p.sincelasttrick = max($ - 1, 0)  
	p.decaybuffer = max($ - 1, 0)
	p.ac_PTmoz = p.mo.z 
	p.ac_PTitem = p.kartstuff[k_itemtype]
	p.ac_PTitemamount = p.kartstuff[k_itemamount]
	p.ac_PTsneakertimer = p.kartstuff[k_sneakertimer]
	p.ac_PTitembtn = (p.cmd.buttons & BT_ATTACK) and ($+1) or 0
	p.ac_PTdriftbtn = (p.cmd.buttons & BT_DRIFT) and ($+1) or 0
	p.ac_PTdriftcharge = p.kartstuff[k_driftcharge]
	p.ac_PTeggmanheld = p.kartstuff[k_eggmanheld]
	p.ac_PTeggmanexplode = p.kartstuff[k_eggmanexplode]
	p.ac_PTsneakertimer = p.kartstuff[k_sneakertimer]
	p.ac_PTrocketsneakertimer = p.kartstuff[k_rocketsneakertimer]
	p.ac_PTtrickboost = p.trickboost
	p.ac_PTtrickbooststr = p.trickbooststr 
	p.ac_PTtrickcharge = p.trickcharge
end


-----------|
--OOFOUCHOW|
--(Damage)-|

local function ac_trickHurt(p, inf, s) 
	ac_debug("it wasn't me, it was the one armed man")
	ac_trickReset(p)
	return
end

--ShouldDamage hooks - invulnerablility while stomping.
local function ac_powerStomp(p, inf, s)
	if not(G_BattleGametype()) then return end--Aggrobatics only.
	if p.chaincount == 2
		K_DoInstashield(p)
		p.kartstuff[k_instashield] = 70
		ac_debug("STOMP THE YARD")
		return false
	else
		ac_debug("demoman's delight")
	end
end

local function ac_trickDeath(mo)
	if not mo or not mo.valid or not(mo.player) then return end --love validity checks :x
	ac_trickReset(mo.player)
	ac_debug("kart empty no thoughts")
end

-------||
--ANIMS||
--VFX--||

--Fake some states for the sake of it.
local function ac_animationHandler(p)
	 
	 ac_trickVfx(p, p.trickstyle)
	 
	 if G_BattleGametype() and p.chaincount == 2 
		p.mo.state = p.cmd.driftturn < 0 and S_KART_DRIFT2_R or S_KART_DRIFT2_L
		p.frameangle = p.mo.angle
		return
	 end
	 
	--This should help with conveyance a smidge.
	if p.trickactive and not(G_BattleGametype())
		if ac_btnPress(p, 0)
			if ac_chargeCheck(p) == 0
				p.mo.state = S_KART_PAIN
			else
				if p.trickframes --Trickframe Support.
					p.mo.frame = T
					ac_debug("FRAMED FOR CRIMES I DIDN'T COMMIT")
				else
					p.mo.state = p.cmd.driftturn < 0 and S_KART_DRIFT2_R or S_KART_DRIFT2_L
				end
			end
		else
			p.frameangle = p.mo.angle
		end
	end
	
	if p.trickstun --faux spinout.
		p.mo.state = S_KART_PAIN
		if not(p.kartstuff[k_pogospring])
			p.frameangle = $ + ANGLE_22h*leveltime
		end
	end
	
end

-------||
--MOBJ-||
--EDITS||

--it's literally easier to just remove the vfx
--this is hacky and terrible but still preferable to actually handling, uh, handling.
local function cancelVfx(mo)
	if mo and mo.valid
		if not(mo.lifetime) then mo.lifetime = 0 end
		mo.lifetime = $ + 1
		if mo.target and mo.target.valid and mo.target.player and (ac_active and not(mo.target.player.tricksneakerbuffer)) and not(mo.target.player.kartstuff[k_driftboost]) and mo.lifetime < 2
			P_RemoveMobj(mo)
		end
	end
end
--addHook("MobjThinker", cancelVfx, MT_BOOSTFLAME)
addHook("MobjThinker", cancelVfx, MT_FASTLINE)
addHook("MobjThinker", cancelVfx, MT_SNEAKERTRAIL)

local saveW
local function ac_burstRam(mo, mo2)
	if mo and mo2 and mo.valid and mo2.valid
		if (mo.z > mo2.z + mo2.height) or (mo.z + mo.height < mo2.z) -- BECAUSE I GOTTA DO THIS SHIT MYSELF.
			return 
		end
		if mo.type == MT_PLAYER and mo2.type == MT_PLAYER
			ac_debug("BECKY LEMME")
			if mo.player.trickboostburst
				saveW = mo.player.kartweight
				mo.player.kartweight = 20 --you're in for it NOW
				K_KartBouncing(mo, mo2) --RIP AND TEAR
				ac_debug("SMASHHHHHH")
				mo.player.kartweight = saveW
			end
		end
	end
end

--SPB Denial for Frontrunners.
function A_SPBChase(actor, var1, var2)
	--Let's run the original func first.
	super(actor, var1, var2)
	if not(ac_SPBlock.value) then return end
	if actor.threshold then return end --let's not get ahead of ourselves
	if actor.extravalue1 == 1 
		if actor.tracer and actor.tracer.player
			actor.tracer.player.trickdenial = true 
			ac_debug("A RIVER IN EGYPT")
		end
	else
		for v in players.iterate -- there's absolutely a better way to do this.
			v.trickdenial = false
		end
		ac_debug("A FRESH START")
	end
	
end

local function ac_removeDenial(mo)
	for v in players.iterate -- there's absolutely ABSOLUTELY a better way to do this.
		v.trickdenial = false
	end
end



-------||
--HOOKS||
-------||

addHook("NetVars", function(n)
	acrohell = n($) --nngh
	ac_toggled = n($) --nnnnngh
	booststacknerf = n($) --nnnnnnnngh
end)

addHook("MapLoad", do 
	acrohell = false
	trickremind = $ + 1 --localized
	ac_toggled = ac_active.value
																		   
	for p in players.iterate do	
		p.acrostore, p.basebonus = 0, 0 --you REALLY THINK
		p.trickready = nil
	end
	ac_debug("don't believe the lies")
end)

local airtimebuffer = 10 --adjust accordingly.
addHook("ThinkFrame", do 
	
	for p in players.iterate do	
        if not p.mo or not p.mo.valid continue end 
		if not (p.trickready) then --Mid netgame syncing
			ac_initacro(p)
		end
		ac_trickstyleHandler(p) 
		ac_acrohellHandler(p)
		ac_trickNag(p)
		if leveltime <= starttime or p.spectator continue end

		if (p.kartstuff[k_spinouttimer] > 0) --continue timer tics for the sake of it 
			ac_storePTvars(p) 
			continue 
		end 
		if P_IsObjectOnGround(p.mo) or p.kartstuff[k_respawn] then p.trickstun = false end --this is here for uh, reasons.
		ac_lockon_prethink(p)
		
		if(ac_trickCheck(p))	
			p.airtime = $ + 1 -- increase airtime buffer
			if not(p.trickactive) and ac_btnPress(p, 0) 
				ac_trickStart(p)
			else 
				ac_chargeHandler(p)
            end 
        end
		
        if P_IsObjectOnGround(p.mo)
			if p.trickactive
				if not(G_BattleGametype())
					if p.airtime > airtimebuffer --prevent accidential tricks
						if not(ac_failCheck(p)) --Failcheck Block.
							ac_trickLand(p, p.trickstyle) --Success Block.
						end
						p.sincelasttrick = TICRATE
					else
						S_StartSound(p.mo, sfx_dbgsal) --doing this outside the usual block just so we don't have to bother.
						ac_debug("what sick man sends BABY to do tricks")
						ac_sparkring(p, 1, true)
						ac_colorcorrect(p)
					end
				end
				
				if G_BattleGametype()
					if p.chaincount == 2 
						S_StopSoundByID(p.mo, sfx_trstgo)
						S_StartSound(p.mo, sfx_trstla)
						ac_sparkring(p, 32)
						if not battleplus then ac_stompPush(p, stompDist, 70*FRACUNIT) end--better than thunder shield
					end
					p.chaincount = 0
					ac_colorcorrect(p)
				end
				p.trickcharge = 0
				p.trickactive = false 
				p.frameangle = p.mo.angle --Reset your looks
				ac_lockon_land(p) --mod specifics in lua_acrobridge		
			end
			p.airtime = 0 
			p.burstvfx = nil
			if p.trickstun == true then ac_colorcorrect(p) end --how many fucking edge cases do i NEED
			if G_BattleGametype() and p.chaincount == 1 then p.chaincount = 0 end
		end 
		ac_sneakerCheck(p)
		--ac_frictionRestore(p)
		ac_storeThink(p) 
		ac_applyAugment(p) 
		ac_applyTrickboost(p) 
		ac_animationHandler(p) 
		ac_storePTvars(p)
		if ac_debugbtn.value
			if p.cmd.buttons & BT_CUSTOM1
				if ac_styleCheck(p, TS_BURST, true) then p.trickboost = 9999 end
				if ac_styleCheck(p, TS_STORE, true) then p.acrostore = maxStore end
				if ac_styleCheck(p, TS_AUGMENT, true) then p.basebonus = p.maxAug end
				S_StartSound(p.mo, sfx_dbgsal)
			end
			if p.cmd.buttons & BT_CUSTOM3
				S_StartSound(p.mo, sfx_dbgsal)
				p.kartstuff[k_sneakertimer] = 0
				p.trickboost = 0
			end
		end
    end
end)


addHook("ShouldExplode", ac_powerStomp)
addHook("PlayerSpin", ac_trickHurt)
addHook("PlayerSquish", ac_trickHurt)
addHook("PlayerExplode", ac_trickHurt)
addHook("MobjDeath", ac_trickDeath, MT_PLAYER)								   
addHook("MobjRemoved", ac_removeDenial, MT_SPB)
addHook("MobjCollide", ac_burstRam, MT_PLAYER)
addHook("MobjMoveCollide", ac_burstRam, MT_PLAYER)

hud.add(function(v, p, c) --Actual visual real HUD is in LUA_ACROHUD  - debug's here for the sake of sanity checks and not actual use lol
	if p.trickready == nil or p.exiting then return end --let's not get hasty here
	
	--this is to make sure i stop being a fucking moron
	if ac_debugbtn.value
		v.drawString(0,20," ACRODEBUG - NOTICE",V_REDMAP|V_HUDTRANSHALF|V_SNAPTOLEFT, "thin")
		v.drawString(0,32," [CUST1-3] GIVE FREE BOOSTS\n TESTING PURPOSES ONLY",V_REDMAP|V_HUDTRANSHALF|V_SNAPTOLEFT, "thin")
		if (netgame) then v.drawString(0,54," YOU CHEATING BING", V_REDMAP|V_HUDTRANSHALF|V_SNAPTOLEFT, "thin") end 
	end
	
	---Debug shit; use debugang to find the goods:tm:
	if debugang.value == 1 --Mechanics
		v.drawString(12,65, ((p.trickstun and "STUN") or "FREE"),V_SNAPTOLEFT,"left")
		v.drawString(12,75, "airtime: " .. p.airtime, V_SNAPTOLEFT, "left")
		if not(G_BattleGametype())
			if p.trickstyle == 0
				v.drawString(12,85, "KSPEED: " .. K_GetKartSpeed(p, true)>>FRACBITS,V_SNAPTOLEFT,"left")
				v.drawString(12,95, "driftcharge: " .. p.kartstuff[k_driftcharge],V_SNAPTOLEFT,"left")
				v.drawString(12,105, "driftbst: " .. p.kartstuff[k_driftboost],V_SNAPTOLEFT,"left")
			end
			if p.trickstyle == 1
				v.drawString(12,85, "growcan: " .. p.kartstuff[k_growcancel],V_SNAPTOLEFT,"left")
				v.drawString(12,95, "BTN BUFFER: " .. p.ac_PTitembtn,V_SNAPTOLEFT,"left")
				v.drawString(12,105, ((p.acrostore > p.kspdfactor and "BIG") or "NO BIG"),V_SNAPTOLEFT,"left")
				v.drawString(90, 175, p.acrostore .. "/" .. maxStore, V_SKYMAP|V_HUDTRANS)
			end
			if p.trickstyle == 2
				v.drawString(12,85, "decadd: " .. p.decaytimer,V_SNAPTOLEFT,"left")
				v.drawString(12,95, "decbfr: " .. p.decaybuffer,V_SNAPTOLEFT,"left")
				v.drawString(12,105, "RAW SPD: " .. K_GetKartSpeed(p, true)>>FRACBITS,V_SNAPTOLEFT,"left")
			end
			v.drawString(12,115, "TBST: " .. p.trickboost,V_SNAPTOLEFT,"left")
			v.drawString(12,125, "TBST-BUR: " .. p.trickboostburst,V_SNAPTOLEFT,"left")
		else
			v.drawString(12,85, "combocount: " .. p.chaincount,V_SNAPTOLEFT,"left")
		end
		v.drawString(12,135, "bst: " .. FixedMul(p.kartstuff[k_boostpower], 100*FRACUNIT)>>FRACBITS,V_SNAPTOLEFT,"left")
		v.drawString(12,145, "spd: " .. FixedMul(p.kartstuff[k_speedboost], 100*FRACUNIT)>>FRACBITS,V_SNAPTOLEFT,"left")

	end
	
	if debugang.value == 2 --Trick Memory, Inputs 
		v.drawString(12,55, "offroad:" .. p.kartstuff[k_offroad], V_SNAPTOLEFT,"left")
		v.drawString(12,65, "acktually:" .. p.trickstyle, V_SNAPTOLEFT,"left")
		v.drawString(12,75, "TSFLAVBACKLEFT" .. p.trickgrade,V_SNAPTOLEFT,"left")
		v.drawString(12,85, "fwd:" .. p.cmd.forwardmove, V_SNAPTOLEFT,"left")
		v.drawString(12,95, ((ac_btnPress(p, 0) and "PRESS") or "UNPRESS"),V_SNAPTOLEFT,"left")
		v.drawString(12,105, ((p.trick_button == BT_BRAKE and "BRAKE") or "CUSTOM1"),V_SNAPTOLEFT,"left")
		v.drawString(12,115, ((ac_btnPress(p, 1) and "ITEM") or "UNITEM"),V_SNAPTOLEFT,"left")
		v.drawString(12,125, ((ac_fastcheck(p, 45) and "FAST") or "UNFAST"),V_SNAPTOLEFT,"left")
		
	end
	
	if debugang.value == 3 --Boost Detection
		v.drawString(12,75, "BUFFER: " .. p.tricksneakerbuffer, V_SNAPTOLEFT,"left")
		v.drawString(12,85, "SNTIMR: " .. p.kartstuff[k_sneakertimer], V_SNAPTOLEFT,"left")
		v.drawString(12,95, "prevboost: " .. FixedMul(p.ac_PTtrickbooststr, 100*FRACUNIT)>>FRACBITS,V_SNAPTOLEFT,"left")
		v.drawString(12,105, "FRIC: " .. p.mo.friction, V_SNAPTOLEFT,"left")
		v.drawString(12,115, "trickboost: " .. p.trickboost,V_SNAPTOLEFT,"left")
		v.drawString(12,125, "spd bonus: " .. FixedMul(p.trickbooststr, 100*FRACUNIT)>>FRACBITS,V_SNAPTOLEFT,"left")
		v.drawString(12,135, "spd: " .. FixedMul(p.kartstuff[k_speedboost], 100*FRACUNIT)>>FRACBITS,V_SNAPTOLEFT,"left")
		v.drawString(12,145, "acc: " .. FixedMul(p.kartstuff[k_accelboost], 100*FRACUNIT)>>FRACBITS,V_SNAPTOLEFT,"left")
	end
end)