rawset(_G, "PTAIinit", function(p)
	p.ptaiinit = 1
	p.ptaispecialskin = 0
	p.ptaielevated = 0
	p.ptaiframing = 0
//	p.ptaistyle = 1,
//	p.ptaispeed = 0,
	//we dont actually set these, but its easier to keep track of vars
/*	p.ptaicolor1
	p.ptaicolor2
	p.isptaichar
	
*/	CONS_Printf(p, "Pizza Tower Variables set.")
	CONS_Printf(p, "\x82"+"Check out"+"\x86"+" ptai_help"+"\x82 "+"for options!")

	return true
end)

COM_AddCommand("ptai_help", function(p)
	if not p or not p.ptaiinit or not p.valid
        CONS_Printf(p, "\x85"+"You aren't valid or the Pizza Tower Variables aren't!")
        return
	end
	
	CONS_Printf(p, "PT Afterframes has 4 commands available for you to tinker around with, which are saved automatically!")
	CONS_Printf(p, "\x86ptai_style -\x82 Sets the style of the afterimages.")
	CONS_Printf(p, "\x86ptai_active -\x82 Sets the activation method of the afterimages.")
	CONS_Printf(p, "\x86ptai_color1 -\x82 Sets the color of the first afterimage. Can only be viewed if your style is set to 3.")
	CONS_Printf(p, "\x86ptai_color2 -\x82 Sets the color of the second afterimage. Can only be viewed if your style is set to 3.")
	CONS_Printf(p, "\x86ptai_addskin -\x82 Makes a skin be able to use and emit afterimages.")
	CONS_Printf(p, "\x86ptai_listskins -\x82 Lists all skins able to use and emit afterimages.")
	
end)

COM_AddCommand("ptai_style", function(p, arg)
	if not p or not p.valid
        CONS_Printf(p, "\x85"+"You don't exist yet!")
        return
	end
	
	local prevstyle
	local argV
	
	if arg
		argV = tonumber(arg)
		prevstyle = p.ptaistyle
		p.ptaistyle = argV
		if not argV
		or argV > 3
		or argV < 1
			p.ptaistyle = prevstyle
			CONS_Printf(p, "\x85"+"Value is too low! Choose between 1 or 3.")
			return
		end
		if io and p == consoleplayer
			local file = io.openlocal("client/pizzatowerai/style.dat", "w+")
			file:write(argV)
			file:close()
		end

		CONS_Printf(p, "Style set to "+"\x82"+arg+"\x80"+".")
	else
		CONS_Printf(p, "Changes the colors of the Pizza Tower Afterimages.")
		CONS_Printf(p, "This has 3 styles available:")
		CONS_Printf(p, "1: Pizza Tower") 
		CONS_Printf(p, "2: Sugary Spire")
		CONS_Printf(p, "3: Custom")
		CONS_Printf(p, "Current style is \x82" + p.ptaistyle+"\x80"+".") 
	end
end)

COM_AddCommand("ptai_active", function(p, arg)
	if not p  or not p.valid
        CONS_Printf(p, "\x85"+"You don't exist yet!")
        return
	end
	
	local prevstyle
	local argV
	
	if arg
		argV = tonumber(arg)
		prevstyle = p.ptaispeed
		p.ptaispeed = argV
		if (arg == nil)
		or argV > 1
		or argV < 0
			p.ptaispeed = prevstyle
			CONS_Printf(p, "\x85"+"Value is too high or too low! Choose either 0 or 1.")
			return
		end
		if io and p == consoleplayer
			local file = io.openlocal("client/pizzatowerai/activation.dat", "w+")
			file:write(argV)
			file:close()
		end

		CONS_Printf(p, "Style set to "+"\x82"+argV+"\x80"+".")
	else
		CONS_Printf(p, "Sets what makes afterimages.")
		CONS_Printf(p, "This has 2 styles available:")
		CONS_Printf(p, "0: Default (Shoes and Dashmode only)") 
		CONS_Printf(p, "1: Speed (Shoes, Dashmode and speed)")
		CONS_Printf(p, "Current style is \x82" + p.ptaispeed+"\x80"+".") 
	end
end)

COM_AddCommand("ptai_color1", function(p, arg, s)
	if not p or not p.valid
        CONS_Printf(p, "\x85"+"You don't exist yet!")
        return
	end
	
	local prevstyle
	local c
	
	if p.ptaistyle ~= 3
	and not s
		CONS_Printf(p, "\x8D"+"These can only be viewed if you have your style set to "+"\x82"+"Custom"+"\x8D"+".\n")
	end
	
	if arg and R_GetColorByName(arg) != SKINCOLOR_NONE
		c = R_GetColorByName(arg)
		prevstyle = p.ptaicolor1
		p.ptaicolor1 = c
		if (arg == nil)
			p.ptaicolor1 = prevstyle
			if not s
				CONS_Printf(p, "\x85"+"You have to set a color for this to work.")
			end
			return
		end
		if io and p == consoleplayer
			local file = io.openlocal("client/pizzatowerai/color1.dat", "w+")
			file:write(c)
			file:close()
		end

		if not s
			CONS_Printf(p, "First Color set to "+"\x82"+arg+"\x80"+".")
		end
	else
		CONS_Printf(p, "Sets what color the first afterimage is.")
		CONS_Printf(p, "Please note that custom colors may not save!\n")
		CONS_Printf(p, "Current color is \x82" + p.ptaicolor1+"\x80"+".") 
	end
end)

COM_AddCommand("ptai_color2", function(p, arg, s)
	if not p or not p.valid
        CONS_Printf(p, "\x85"+"You don't exist yet!")
        return
	end
	
	local prevstyle
	local c
	
	if p.ptaistyle ~= 3
	and not s
		CONS_Printf(p, "\x8D"+"These can only be viewed if you have your style set to "+"\x82"+"Custom"+"\x8D"+".\n")
	end
	
	if arg and R_GetColorByName(arg) != SKINCOLOR_NONE
		c = R_GetColorByName(arg)
		prevstyle = p.ptaicolor2
		p.ptaicolor2 = c
		if (arg == nil)
			p.ptaicolor2 = prevstyle
			if not s
				CONS_Printf(p, "\x85"+"You have to set a color for this to work.")
			end
			return
		end
		if io and p == consoleplayer
			local file = io.openlocal("client/pizzatowerai/color2.dat", "w+")
			file:write(c)
			file:close()
		end

		if not s
			CONS_Printf(p, "Second Color set to "+"\x82"+arg+"\x80"+".")
		end
	else
		CONS_Printf(p, "Sets what color the second afterimage is.")
		CONS_Printf(p, "Please note that custom colors may not save!\n")
		CONS_Printf(p, "Current color is \x82" + p.ptaicolor2+"\x80"+".") 
	end
end)


COM_AddCommand("ptai_addskin", function(p, skin, isspec)
	if not p  or not p.valid
        CONS_Printf(p, "\x85"+"You don't exist yet!")
        return
	end
	
	if not p.ptaielevated
        CONS_Printf(p, "\x85"+"You need to be an admin to use this.")
        return	
	end

	if not skin
		CONS_Printf(p, "Adds a skin to emit afterimages.")
		CONS_Printf(p, "Command syntax:")
		CONS_Printf(p, "skinname <string> (The name when you type skin in your console)") 
		CONS_Printf(p, "isspecial <boolean> Put true or false if this skin has custom dashmode") 
		return
	end
	
	if ((isspec ~= "true") and (isspec ~= "false"))
		CONS_Printf(p, "\x85isspecial needs to be true or false!")
		return		
	end
	//referenced from ban skins by Lach
	local skinN = tonumber(skin)
	
	if skinN
		if skinN >= 0 and skinN < 32 and skins[skinN]
			skinN = skins[skinN].name
		else
			CONS_Printf(p,"\x85"+"Skin out of range!")
			return
		end
	else
		skin = $:lower()
	end
	
	if skins[skin]
		local n = #skins[skin] + 1
		
		if skins[skin].name == "soapthehedge"
			CONS_Printf(p,"\x85This skin uses it's own afterimages!")
			return
		end
		
		//we can edit existing candidates
		if PizzaTowerAICandidates[skin]
			//CONS_Printf(p, "\x85This skin already has afterimages!")
			//return
			CONS_Printf(p,"\x86"+"This skin already has afterimages! Editing it!")
			if isspec == "true"
				PizzaTowerAICandidates[skin] = {true, true, false}
			elseif isspec == "false"
				PizzaTowerAICandidates[skin] = {false, true, false}
			end
			print("\x82 Edited "+skins[skin].realname+"'s afterimages!")
			return
		end
		print("\x82 Skin "+skins[skin].realname+" now has afterimages!")
		if isspec == "true"
			PizzaTowerAICandidates[skin] = {true, true, false}
		elseif isspec == "false"
			PizzaTowerAICandidates[skin] = {false, true, false}
		end
		return
	end
	CONS_Printf(p,"\x85You can't add afterimages to a nonexistent skin!")
end)

COM_AddCommand("ptai_listskins", function(p)
	if not p  or not p.valid
        CONS_Printf(p, "\x85"+"You don't exist yet!")
        return
	end

	local prnt = CONS_Printf
	
	prnt(p,"\x82List of PT Afterimages skins:")
	for k,v in pairs(PizzaTowerAICandidates)
		local color = "\x82"
		
		//what is this
		if k == "sonic"
		or k == "tails"
		or k == "knuckles"
		or k == "amy"
		or k == "fang"
		or k == "metalsonic"
		or k == "blaze"
		or k == "marine"
		or k == "surge"
		or k == "adventuresonic"
			color = "\x86"
		end
			
		prnt(p,color..tostring(k) )
	end
	
end)

freeslot("SKINCOLOR_AIREALLYBLUE")
freeslot("SKINCOLOR_AIREALLYPINK")

freeslot("SKINCOLOR_AIREALLYRED")
freeslot("SKINCOLOR_AIREALLYGREEN")
skincolors[SKINCOLOR_AIREALLYRED] = {
	name = "Afterimages Red",
	ramp = {35,35,35,35,35,35,35,35,35,35,35,41,41,41,41,41},
	invcolor = SKINCOLOR_AIREALLYGREEN,
	invshade = 0,
	chatcolor = V_REDMAP,
	accessible = true
}
//freeslot("SKINCOLOR_SOAPAIREALLYGREEN")

skincolors[SKINCOLOR_AIREALLYGREEN] = {
	name = "Afterimages Green",
	ramp = {100,100,100,100,100,100,100,100,100,100,100,191,191,191,191,191},
	invcolor = SKINCOLOR_AIREALLYRED,
	invshade = 0,
	chatcolor = V_GREENMAP,
	accessible = true
}

skincolors[SKINCOLOR_AIREALLYBLUE] = {
    name = "Really Blue",
    ramp = {136,136,136,136,136,136,136,136,136,136,136,138,138,138,138,138},
    invcolor = SKINCOLOR_AIREALLYPINK,
    invshade = 0,
    chatcolor = V_SKYMAP,
    accessible = true
}

skincolors[SKINCOLOR_AIREALLYPINK] = {
    name = "Really Pink",
    ramp = {204,204,204,204,204,204,204,204,204,204,204,71,71,71,71,71},
    invcolor = SKINCOLOR_AIREALLYBLUE,
    invshade = 0,
    chatcolor = V_ROSYMAP,
    accessible = true
}

freeslot("MT_PTAI_AFTERIMAGE_OBJECT")
mobjinfo[MT_PTAI_AFTERIMAGE_OBJECT] = {
	doomednum = -1,
	spawnstate = S_PLAY_WAIT,
	flags = MF_NOCLIP|MF_NOCLIPHEIGHT|MF_NOGRAVITY|MF_NOBLOCKMAP
}

