//from clairebun
//https://wiki.srb2.org/wiki/User:Clairebun/Sandbox/Common_Lua_Functions#L_Choose


local function choosething(...)
	local args = {...}
	local choice = P_RandomRange(1,#args)
	return args[choice]
end

//sorry but you may not copy this function or the afterimage object
//add the afterimage object thinker! please, wait for the next soap update
local function creatafterimage(p,me,color1,color2)
	local ghost = P_SpawnMobjFromMobj(me,0,0,0,MT_PTAI_AFTERIMAGE_OBJECT)
	ghost.target = me

	ghost.skin = me.skin
	ghost.scale = me.scale

	ghost.sprite = me.sprite

	ghost.state = me.state
	ghost.sprite2 = me.sprite2
	ghost.frame = FF_TRANS90|me.frame

	ghost.angle = p.drawangle


	ghost.color = choosething(color1,color2)
	ghost.spritexscale,ghost.spriteyscale = me.spritexscale, me.spriteyscale

	return ghost

end
//after image
local afterimage = function(ai)
	if not ai
	or not ai.valid
		return
	end

	//we need a thing to follow
	if not ai.target
	or not ai.target.valid
		P_RemoveMobj(ai)
		return
	end


	if (leveltime % 6) > 3
//		ai.frame = $|TR_TRANS70
		ai.flags2 = $|MF2_DONTDRAW
	else
//		ai.frame = $ &~TR_TRANS70
		ai.flags2 = $ &~MF2_DONTDRAW
	end
	
	local p = ai.target.player
	local me = p.mo
	
	ai.spritexoffset = me.spritexoffset
	ai.spriteyoffset = me.spriteyoffset
	
	ai.colorized = true
	if not ai.timealive
		ai.timealive = 2
	else
		ai.timealive = $+1
	end
	
	//no interpolation please fingers crossed???????
	//i said PLEASE`
	P_SetOrigin(ai, ai.x, ai.y, ai.z)
	
	local chasecam = CV_FindVar("chasecam")
	
	if not chasecam.value
		ai.flags2 = $|MF2_DONTDRAW
	end
	
	local fuselimit = 3

/*
	if ai.soapsupertauntclone
		fuselimit = SoapFetchConstant("afterimages_st_fuse")
		ai.tics = -1
	end
*/
	
	//because fuse doesnt wanna work
	if ai.timealive > fuselimit
		P_RemoveMobj(ai)
		return
	end
	
end
addHook("MobjThinker", afterimage, MT_PTAI_AFTERIMAGE_OBJECT)

//do afterimaging
local afterimaging = function(p)
if not p.valid
return
end

	if p.mo and p.mo.valid
	and p.mo.skin == "nick"
	local me = p.mo
	local skin = skins[me.skin]
		p.accSpeed = abs(FixedHypot(p.rmomx,p.rmomy)) //more accurate speed thing
		p.ptaispecialskin = 0
			
//		PizzaTowerAICheckForSpecialSkins(p, me)
		p.ptaispecialskin = "nick"
		
			
		//make after images when we're going fast!!!
		if p.powers[pw_sneakers]
		--if PTSR.pizzatime --pizza time check 
		--and PTSR.timeleft <= 0 -- In overtime
			if (p.playerstate == PST_LIVE) -- is even alive
			--and (p.mo.state == S_PLAY_DASH) --Only make it appear when running
			and gametype == GT_PTSPICER --Exclusive on Pizza Time Spice Runners
			and (p.accSpeed > 40*FU)
				p.ptaiframing = 1
				local color = SKINCOLOR_AIREALLYRED
				local color2 = SKINCOLOR_AIREALLYGREEN
				creatafterimage(p,me,color,color2)
				p.nickmachison = true
			end
		else
			p.nickmachison = false
		end
	end
end
addHook("PlayerThink", afterimaging)