//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
//and the afterimage object thinker! please, wait for the next soap update
//dw about that its reusable now
local function creatafterimage(p,me,color1,color2)
	local ghost = P_SpawnMobjFromMobj(me,0,0,0,MT_NOTSOAPAI_AFTERIMAGE_OBJECT)
	ghost.target = me
	
	ghost.skin = me.skin
	ghost.scale = me.scale
	
	ghost.sprite = me.sprite
	
	ghost.tics = -1
	ghost.state = me.state
	if p.powers[pw_super]
		ghost.sprite2 = me.sprite2 | FF_SPR2SUPER
	else
		ghost.sprite2 = me.sprite2
	end
	ghost.frame = me.frame
	ghost.rollangle = me.rollangle
	ghost.roll = me.roll
	ghost.pitch = me.pitch
	
	ghost.angle = p.drawangle
	
	
	ghost.color = choosething(color1,color2)
	ghost.spritexscale,ghost.spriteyscale = me.spritexscale, me.spriteyscale
	
	return ghost

end
//after image
addHook("MobjThinker", 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 = 1
	else
		ai.timealive = $+1
	end
	if p and p.powers[pw_super]
		ai.sprite2 = $|FF_SPR2SUPER
		ai.frame = $|FF_SPR2SUPER
	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, MT_NOTSOAPAI_AFTERIMAGE_OBJECT)

//io
addHook("PlayerSpawn", function(p)
	if io and p == consoleplayer and p.notsoapaistyle == nil and p.notsoapaispeed == nil
		
		local file = io.openlocal("client/notsoapai/style.dat")
		if file
	//		print("Opening style.dat...")
			local string = file:read("*a")
			if string == "3"
				COM_BufInsertText(p, "notsoapai_style 3")
			elseif string == "2"
				COM_BufInsertText(p, "notsoapai_style 2")
			elseif string == "1" or string == nil
				COM_BufInsertText(p, "notsoapai_style 1")
			end
			file:close()
//			print("Closed style.dat.")
		else
			COM_BufInsertText(p, "notsoapai_style 1")
		end
		
		local file = io.openlocal("client/notsoapai/activation.dat")
		if file
			local string = file:read("*a")
			if string == "0"
				COM_BufInsertText(p, "notsoapai_active 0")
			elseif string == "1" or string == nil
				COM_BufInsertText(p, "notsoapai_active 1")
			end
			file:close()
		else
			//no file? activeness set to nil? lets fix that here!
			p.notsoapaispeed = 0
		end
		
		local file = io.openlocal("client/notsoapai/color1.dat")
		if file
			COM_BufInsertText(p, "notsoapai_color1 \""..file:read("*a").."\" s")
		else
			COM_BufInsertText(p, "notsoapai_color1 red s")
		end

		local file = io.openlocal("client/notsoapai/color2.dat")
		if file
			COM_BufInsertText(p, "notsoapai_color2 \""..file:read("*a").."\" s")
		else
			COM_BufInsertText(p, "notsoapai_color2 green s")
		end

	end
	
end)

//do afterimaging
addHook("PlayerThink", function(p, player)
if not p.valid
return
end

if not p.notsoapaiinit
	notsoapaiinit(p)
return
end

	//this is kind stupid but whatever
	//soap
    if (p == server) or (p == admin)
	or IsPlayerAdmin(p)
        p.notsoapaielevated = 1
    else
        p.notsoapaielevated = 0
    end

    if p and p.mo and p.mo.valid
	and NSATAICandidates[p.mo.skin]
    local me = p.mo
	local skin = skins[me.skin]
		p.accSpeed = abs(FixedHypot(p.rmomx,p.rmomy)) //more accurate speed thing
		p.notsoapaispecialskin = 0
			//soap already does this. the only thing we need from here is p.notsoapaistyle
			if me.skin == "soapthehedge"
			and p.soaptable
				if not (17 <= #p.soaptable) 
					return
				end
			end
			
//			PizzaTowerAICheckForSpecialSkins(p, me)
			p.notsoapaispecialskin = NSATAICandidates[me.skin][1]
			
			//make after images when we're going fast!!!
			if (p.dashmode >= (3*TICRATE)) 
			or ((p.powers[pw_sneakers]) and (p.accSpeed > p.runspeed)) 
			//some characters dont have "real" dashmode (ex: junio, blaze, chrispychars),
			//so we'll need an extra check for those skins
			or ((p.accSpeed > skin.normalspeed) and (p.notsoapaispeed or p.notsoapaispecialskin))
			and not (p.pflags & PF_STARTDASH)
			or (p.mo.state == S_NOTSOAP_UPPERCUT)
			or (p.nottakisboost == 1)
			and (p.playerstate == PST_LIVE)
				
				p.notsoapaiframing = 1
				
				local color = SKINCOLOR_AIREALLYRED
				local color2 = SKINCOLOR_AIREALLYGREEN
				
				if not (NSATAICandidates[me.skin][3])
					if p.notsoapaistyle and (p.notsoapaistyle == 2)
						
						color = SKINCOLOR_AIREALLYBLUE
						color2 = SKINCOLOR_AIREALLYPINK
						
					elseif p.notsoapaistyle and (p.notsoapaistyle == 3)
					and ((p.notsoapaicolor1) and (p.notsoapaicolor2))
					and NSATAICandidates[me.skin][2]
					
						color = p.notsoapaicolor1
						color2 = p.notsoapaicolor2
						
					end
				else
					color = NSATAICandidates[me.skin][4]
					color2 = NSATAICandidates[me.skin][5]
				end
				
				creatafterimage(p,me,color,color2)
			else
				p.notsoapaiframing = 0
			end
			

	end
end)