//SPIIIIIIIIIIIIIIIN
addHook("PlayerThink", function(p)
	local pmo = p.mo
	
	  if pmo.skin == "pizzano"
	  and p.speed >= 32*FRACUNIT 
	  and p.mo.state == S_PLAY_JUMP then
	  p.mo.state = S_PLAY_PZNOMACH2JUMP
	  end
end)

//SPIIIIIIIN 2.0
addHook("PlayerThink", function(p)
	local pmo = p.mo
	
	  if pmo.skin == "pizzano"
	  and p.dashmode >= 3*TICRATE 
	  and p.mo.state == S_PLAY_PZNOMACH2JUMP then
	  p.mo.state = S_PLAY_PZNOMACH3JUMP
	  end
end)

//WAHOOOOOOOOOOO WE ARE EXTREMELY FAST
addHook("PlayerThink", function(p)
	local pmo = p.mo
	
	if not pmo.skin == "pizzano"
		return
		    end
	if p.speed >= 60*FRACUNIT
	and not (p.pflags & PF_THOKKED)
	and not (p.pflags & PF_JUMPED)
	and p.dashmode >= 3*TICRATE then
				p.mo.state = S_PLAY_FLY
			end
	if p.mo.state == S_PLAY_FLY
	   and p.speed <= 50*FRACUNIT then
				p.mo.state = S_PLAY_STND
		end
end)

//the momentum
addHook("PlayerThink", function(player)
    if (player.cmd.forwardmove or player.cmd.sidemove)
	and player.mo.skin == "pizzano"
    and player.normalspeed <= player.speed then
        player.mo.friction = FRACUNIT
    else
        player.mo.friction = 29*FRACUNIT/32
    end
end)

//kungfu
addHook("ThinkFrame", function()
    for player in players.iterate do
        if player.mo.skin == "pizzano"
			if ((player.mo.state == S_PLAY_MELEE_FINISH) or (player.mo.state == S_PLAY_MELEE) or (player.mo.state == S_PLAY_MELEE_LANDING))
			and P_IsObjectOnGround(player.mo) then
					player.randomkungfu = P_RandomRange(1, 6)
					if player.randomkungfu == 1
					    player.mo.state = S_PLAY_KUNGFU1
						S_StartSound(player.mo, sfx_kungf)
					elseif player.randomkungfu == 2
						player.mo.state = S_PLAY_KUNGFU2
						S_StartSound(player.mo, sfx_kungf)
					elseif player.randomkungfu == 3
						player.mo.state = S_PLAY_KUNGFU3
						S_StartSound(player.mo, sfx_kungf)
					elseif player.randomkungfu == 4
					    player.mo.state = S_PLAY_KUNGFU4
						S_StartSound(player.mo, sfx_kungf)
					elseif player.randomkungfu == 5
						player.mo.state = S_PLAY_KUNGFU5
						S_StartSound(player.mo, sfx_kungf)
					elseif player.randomkungfu == 6
						player.mo.state = S_PLAY_KUNGFU6
						S_StartSound(player.mo, sfx_kungf)
				end
			end
		end
	end
end)

//can the taunts and kungfu damage?
addHook("PlayerCanDamage", function(player)
	if player.mo.skin == "pizzano"
	and player.mo.state == S_PLAY_KUNGFU1
	or player.mo.state == S_PLAY_KUNGFU2
	or player.mo.state == S_PLAY_KUNGFU3
	or player.mo.state == S_PLAY_KUNGFU4
	or player.mo.state == S_PLAY_KUNGFU5
	or player.mo.state == S_PLAY_KUNGFU6
	or player.mo.state == S_PLAY_AIRKUNGFU1
	or player.mo.state == S_PLAY_AIRKUNGFU2
	or player.mo.state == S_PLAY_TAUNT1
	or player.mo.state == S_PLAY_TAUNT2
	or player.mo.state == S_PLAY_TAUNT3
	or player.mo.state == S_PLAY_TAUNT4
	or player.mo.state == S_PLAY_TAUNT5
	or player.mo.state == S_PLAY_TAUNT6
	or player.mo.state == S_PLAY_TAUNT7
	or player.mo.state == S_PLAY_TAUNT8
	or player.mo.state == S_PLAY_TAUNT9
		return true
	end
end)

//if you kungfu, you will be strong
addHook("PlayerThink", function(player)
 if player.mo.skin == "pizzano"
 	and player.mo.state == S_PLAY_KUNGFU1
	or player.mo.state == S_PLAY_KUNGFU2
	or player.mo.state == S_PLAY_KUNGFU3
	or player.mo.state == S_PLAY_KUNGFU4
	or player.mo.state == S_PLAY_KUNGFU5
	or player.mo.state == S_PLAY_KUNGFU6
	or player.mo.state == S_PLAY_AIRKUNGFU1
	or player.mo.state == S_PLAY_AIRKUNGFU2 then
	player.powers[pw_strong] = STR_HEAVY|STR_SPRING|STR_SPIKE|STR_ANIM
	end
end)

//the mach sounds
addHook("PlayerThink", function(player)
	    if player.mo.skin == "pizzano"
	    and ((player.mo.state == S_PLAY_DASH) or (player.mo.state == S_PLAY_FIRE) or (player.mo.state == S_PLAY_FIRE_FINISH))
		and not S_SoundPlaying(player.mo, sfx_mach3) then
		S_StartSound(player.mo, sfx_mach3)
	end
	    if player.mo.skin == "pizzano"
	    and not ((player.mo.state == S_PLAY_DASH) or (player.mo.state == S_PLAY_FIRE) or (player.mo.state == S_PLAY_FIRE_FINISH))
		or (player.mo.state == S_PLAY_WALK)
		and S_SoundPlaying(player.mo, sfx_mach3) then
		S_StopSoundByID(player.mo, sfx_mach3)
	end
	    if player.mo.skin == "pizzano"
	    and (player.mo.state == S_PLAY_RUN)	
		and not S_SoundPlaying(player.mo, sfx_mach2) then
		S_StartSound(player.mo, sfx_mach2)
    end
	    if player.mo.skin == "pizzano"
	    and not (player.mo.state == S_PLAY_RUN)
		or (player.mo.state == S_PLAY_WALK)
		and S_SoundPlaying(player.mo, sfx_mach2) then
		S_StopSoundByID(player.mo, sfx_mach2)
	end
	    if player.mo.skin == "pizzano"
	    and (player.mo.state == S_PLAY_WALK)	
		and not S_SoundPlaying(player.mo, sfx_mach1) then
		S_StartSound(player.mo, sfx_mach1)
	end
	    if player.mo.skin == "pizzano"
	    and not (player.mo.state == S_PLAY_WALK)
		and S_SoundPlaying(player.mo, sfx_mach1) then
		S_StopSoundByID(player.mo, sfx_mach1)
	end
	    if player.mo.skin == "pizzano"
	    and (player.mo.state == S_PLAY_FLY)	
		and not S_SoundPlaying(player.mo, sfx_mach4) then
		S_StartSound(player.mo, sfx_mach4)
	end
	    if player.mo.skin == "pizzano"
	    and not (player.mo.state == S_PLAY_FLY)
		or (player.mo.state == S_PLAY_WALK)
		and S_SoundPlaying(player.mo, sfx_mach4) then
		S_StopSoundByID(player.mo, sfx_mach4)
	end
end)

//YOOOOOOOOOO jump anomation!
addHook("PlayerThink", do
	    for player in players.iterate do
		if player.mo.skin == "pizzano"
		and player.mo.state == S_PLAY_JUMP then 
			player.mo.state = S_PLAY_JUMPINITIAL
		end
	end
end)

// it´s sugar rush!! , why are you not move??
addHook("PlayerThink", function(p)
      local pmo = p.mo

  if not pmo.skin == "pizzano"
		return
		end
  if p.mo.state == S_PLAY_STND
	and (PTSR and PTSR.pizzatime)
		or (PizzaTime and PizzaTime.sync and PizzaTime.sync.PizzaTime) then
				p.mo.state = S_PLAY_PANIC
				end 
				
  if p.mo.state == S_PLAY_PANIC
	   and p.speed >= 1*FRACUNIT then
				p.mo.state = S_PLAY_STND
	end
end)

//double jump animation
addHook("PlayerThink", do
	    for player in players.iterate do
		if player.mo and player.mo.skin == "pizzano"
		and not player.powers [pw_shield]
		and player.cmd.buttons & BT_JUMP
		and not P_IsObjectOnGround(player.mo)
		and player.charability == CA_DOUBLEJUMP
		and (player.pflags & PF_THOKKED) then 
			player.mo.state = S_PLAY_ROLL
		end
	end
end)

//superhero´s fly animation
addHook("PlayerThink", do
	    for player in players.iterate do
		if player.mo and player.mo.skin == "pizzano"
		and player.cmd.buttons & BT_JUMP
		and not P_IsObjectOnGround(player.mo)
		and player.charability == CA_FLOAT
		and (player.pflags & PF_THOKKED) then 
			player.mo.state = S_PLAY_FIRE
		end
	end
end)

//taunts
addHook("ThinkFrame", function()
        for player in players.iterate do
        if player.mo.skin == "pizzano" and (player.cmd.buttons & BT_TOSSFLAG) and not ((player.mo.state == S_PLAY_FIRE) or (player.mo.state == S_PLAY_FIRE_FINISH) or (player.mo.state == S_PLAY_ROLL) or (player.mo.state == S_PLAY_AIRKUNGFU1) or (player.mo.state == S_PLAY_AIRKUNGFU2))
				if player.tossdelay == 0
					player.randomtaunt = P_RandomRange(1, 9)
					if player.randomtaunt == 1
						player.mo.state = S_PLAY_TAUNT1
						S_StartSound(player.mo, sfx_taunt)
						player.tossdelay = 15
					elseif player.randomtaunt == 2
						player.mo.state = S_PLAY_TAUNT2
						S_StartSound(player.mo, sfx_taunt)
						player.tossdelay = 15
					elseif player.randomtaunt == 3
						player.mo.state = S_PLAY_TAUNT3
						S_StartSound(player.mo, sfx_taunt)
						player.tossdelay = 15
					elseif player.randomtaunt == 4
						player.mo.state = S_PLAY_TAUNT4
						S_StartSound(player.mo, sfx_chadt)
						player.tossdelay = 15
					elseif player.randomtaunt == 5
						player.mo.state = S_PLAY_TAUNT5
						S_StartSound(player.mo, sfx_taunt)
						player.tossdelay = 15
					elseif player.randomtaunt == 6
						player.mo.state = S_PLAY_TAUNT6
						S_StartSound(player.mo, sfx_taunt)
						player.tossdelay = 15
					elseif player.randomtaunt == 7
						player.mo.state = S_PLAY_TAUNT7
						S_StartSound(player.mo, sfx_taunt)
						player.tossdelay = 15
					elseif player.randomtaunt == 8
						player.mo.state = S_PLAY_TAUNT8
						S_StartSound(player.mo, sfx_taunt)
						player.tossdelay = 15
					elseif player.randomtaunt == 9
						player.mo.state = S_PLAY_TAUNT9
						S_StartSound(player.mo, sfx_taunt)
						player.tossdelay = 15
				end
			end
		end
	end
end)

//kungfu in mid air (don´t work same the kungfu on ground)
addHook("ThinkFrame", function()
        for player in players.iterate do
        if player.mo.skin == "pizzano" and (player.cmd.buttons & BT_SPIN) 
		and ((player.mo.state == S_PLAY_JUMPINITIAL) or (player.mo.state == S_PLAY_FALL) or (player.mo.state == S_PLAY_PZNOMACH2JUMP) or (player.mo.state == S_PLAY_PZNOMACH3JUMP)) then
                 player.randomkungfuair = P_RandomRange(1, 2)
					if player.randomkungfuair == 1
						player.mo.state = S_PLAY_AIRKUNGFU1
						S_StartSound(player.mo, sfx_kungf)
					elseif player.randomkungfuair == 2
						player.mo.state = S_PLAY_AIRKUNGFU2
						S_StartSound(player.mo, sfx_kungf)
			end
		end
	end
end)


//he like the blue licore
addHook("MusicChange", function(oldname, newname, mflags, looping, position, prefadems, fadeinms)
	if splitscreen
		return
	end
	if not (consoleplayer and consoleplayer.valid)
		return
		end
	local snikmus = skins[consoleplayer.skin].name
	if snikmus == "pizzano"
		if newname == "PIZTIM"
			newname = "LP1NO"
		elseif newname == "DEAOLI"
			newname = "LP2NO"
		elseif newname == "PIJORE" or newname == "LAP3LO"
			newname = "LP3NO"
		elseif newname == "GLUWAY"
			newname = "LP4NO"
		elseif newname == "EXTREM"
			newname = "LP4NO"
		end
		return newname, mflags, looping, position, prefadems, fadeinms
	end
end)

//original addon by luigi budd

//heavily based off of alt sonic
addHook("PlayerSpawn", function(p)
	if io and p == consoleplayer and p.ptaistyle == nil and p.ptaispeed == nil
		local file = io.openlocal("client/pizzatowerai/sonkistyle.dat")
		if file
	//		print("Opening style.dat...")
			local string = file:read("*a")
			if string == "2"
				COM_BufInsertText(p, "ptafterimagesstyle 2")
			elseif string == "1" or string == nil
				COM_BufInsertText(p, "ptafterimagesstyle 1")
			end
			file:close()
//			print("Closed style.dat.")
		end
		local file = io.openlocal("client/pizzatowerai/activation.dat")
		if file
			local string = file:read("*a")
			if string == "0"
				COM_BufInsertText(p, "ptafterimagesactive 0")
			elseif string == "1" or string == nil
				COM_BufInsertText(p, "ptafterimagesactive 1")
			end
			file:close()
		end
end
end)

addHook("PlayerThink", function(player)
if player.mo.skin == "pizzano" then
if not player.valid
return
end

if not player.ptaiinit
	PTAIinit(player)
return
end

    if player.mo.valid and player.mo
    local me = player.mo
		player.accSpeed = abs(FixedHypot(player.rmomx,player.rmomy)) //more accurate speed thing

			//make after images when we're going fast!!!
			if (player.dashmode >= (3*TICRATE)) 
			or ((player.powers[pw_sneakers]) and (player.accSpeed > player.runspeed)) 
			or ((player.mo.state == S_PLAY_KUNGFU1) or (player.mo.state == S_PLAY_KUNGFU2) or (player.mo.state == S_PLAY_KUNGFU3) or (player.mo.state == S_PLAY_KUNGFU4) or (player.mo.state == S_PLAY_KUNGFU5) or (player.mo.state == S_PLAY_KUNGFU6) or (player.mo.state == S_PLAY_RUN) or (player.mo.state == S_PLAY_PZNOMACH2JUMP))
			or ((player.accSpeed > player.normalspeed) and player.ptaispeed)
			and not (player.pflags & PF_STARTDASH)
				PizzaTowerAfterImages(player, me, player.ptaicolor)
				if not player.ptaiframewait
					player.ptaiframewait = 3
					if player.ptaicolor == 1
						player.ptaicolor = 2
					else
						player.ptaicolor = 1
					end
				else
					player.ptaiframewait = $ -1
				end
			end
			
end
	end
end)

rawset(_G, "PizzaTowerAfterImages", function(p, me, color)
//	local ghost = P_SpawnGhostMobj(me)
//	ghost.frame = TR_TRANS80
//	ghost.destscale = $*99
	if p.ptaiframewait
		return
	end
	local chasecam = CV_FindVar("chasecam")
	local afteri = P_SpawnGhostMobj(me)
	if p.ptaistyle == 2 or p.ptaistyle == nil
		if color == 1
			afteri.color = SKINCOLOR_AIREALLYBLUE
		elseif color == 2
			afteri.color = SKINCOLOR_AIREALLYPURP
		end
	else
		if color == 1
			afteri.color = SKINCOLOR_AIREALLYRED
		elseif color == 2
			afteri.color = SKINCOLOR_AIREALLYGREEN
		end
	end
	afteri.colorized = true
	afteri.frame = FF_FULLBRIGHT|me.frame|TR_TRANS10
	afteri.fuse = 14
	
	//should you be out of my face?
	if chasecam.value
		afteri.flags2 = $ & ~MF2_DONTDRAW
	else
		afteri.flags2 = $ | MF2_DONTDRAW
	end

end)

rawset(_G, "PTAIinit", function(p)
	p.ptaiinit = 1
	p.ptaiframewait = 0
	p.ptaicolor = 1
//	p.ptaistyle = 1,
//	p.ptaispeed = 0,
	
	CONS_Printf(p, "Adding the funny Pizza Tower Variabls")
	CONS_Printf(p, "\x82"+"Check out"+"\x86"+" ptafterimageshelp"+"\x82 "+"for options!")

	return true
end)

//pretty much just soap_cos_speedometer
COM_AddCommand("ptafterimageshelp", 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 2 options you can choose, which are saved automatically.")
	CONS_Printf(p, "\x86ptafterimagesstyle -\x82 Sets the style of the afterimages.")
	CONS_Printf(p, "\x86ptafterimagesactive -\x82 Sets the activation method of the afterimages.")
	
end)

COM_AddCommand("sonkiafterimagesstyle", 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 > 2
		or argV < 1
			p.ptaistyle = prevstyle
			CONS_Printf(p, "\x85"+"Value is too low! Choose either 1 or 2.")
			return
		end
		if io and p == consoleplayer
			local file = io.openlocal("client/pizzatowerai/sonkistyle.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 2 styles available:")
		CONS_Printf(p, "1: Sonki Blue") 
		CONS_Printf(p, "2: Luigi Budd!?!?!?")
		CONS_Printf(p, "Current style is \x82" + p.ptaistyle+"\x80"+".") 
	end
end)

COM_AddCommand("ptafterimagesactive", 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 not argV
		or argV > 1
		or argV < 0
			p.ptaispeed = prevstyle
			CONS_Printf(p, "\x85"+"Value is 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)


freeslot("SKINCOLOR_AIREALLYRED")
freeslot("SKINCOLOR_AIREALLYGREEN")
freeslot("SKINCOLOR_AIREALLYBLUE")
freeslot("SKINCOLOR_AIREALLYPURP")

skincolors[SKINCOLOR_AIREALLYRED] = {
    name = "Really 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 = "Really 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_AIREALLYPURP,
    invshade = 0,
    chatcolor = V_SKYMAP,
    accessible = true
}

skincolors[SKINCOLOR_AIREALLYPURP] = {
    name = "Really Pink",
    ramp = {163,163,163,163,163,163,163,163,163,163,163,186,186,186,186,186},
    invcolor = SKINCOLOR_AIREALLYBLUE,
    invshade = 0,
    chatcolor = V_ROSYMAP,
    accessible = true
}

//IT´s SUGAR RUSH (no pizza time)
local PTv2loaded = false

addHook("ThinkFrame", function()
	if PizzaTime
	and not PTv2loaded
		PTv2loaded = true
		addPTHook("HUD_PizzaTimeGraphic", function(v, p, c)
			if p.mo.skin ~= "pizzano" return end
			
			return {
				[1] = FixedDiv(320*FU, 2*FU),
				[3] = v.cachePatch("ISGR"+PizzaTime.sync.hud.PizzaTimeFlash)
			}
		end)
	end
end)

addHook("PlayerThink", function(p)
	if not PTSR return end
	
	local ch = customhud
	
	if p.pizzanoptsrhud == nil
		p.pizzanoptsrhud = false
	end
	
	if not (p.mo and p.mo.valid) return end
	
	if p.mo.skin == "pizzano"
		if not ch.enabled("PTSR_itspizzatime")
			print("hi")
			p.pizzanoptsrhud = true
		end
	else
		if ch.enabled("PTSR_itspizzatime")
			p.pizzanoptsrhud = false
		end
	end
end)

addHook("HUD", function(v, p)
	if not PTSR
	or not (PTSR.pizzatime and PTSR.pizzatime_tics)
	or CV_PTSR.homework.value
	or gametype ~= GT_PTSPICER return end
	
	local ch = customhud
	
	if p.mo.skin ~= "pizzano"
		if p.pizzanoptsrhud
			ch.enable("PTSR_itspizzatime")
		end
		return
	end
	
	if not p.pizzanoptsrhud
		ch.disable("PTSR_itspizzatime")
	end
	
	local frame = 1
	if leveltime%10 <= 5
		frame = 2
	end
	
	v.drawScaled(FixedDiv(320*FU, 2*FU), 200*FU-(PTSR.pizzatime_tics*FU)*2, FU/2, v.cachePatch("ISGR"+frame))
end, "game")

//dance (SUS)
addHook("PlayerThink", function(player)
    if player.mo.skin ~= "pizzano"
        return
    end

    if (player.cmd.buttons & BT_FIRENORMAL) and P_IsObjectOnGround(player.mo) 
	and player.speed <= 17*FRACUNIT then
		player.mo.state = S_PLAY_DANCE
        player.pflags = $1|PF_JUMPSTASIS
    end
end)

addHook("PlayerThink",function(player)
if player.mo and player.mo.skin == "pizzano"
and player.dashmode >= 3*TICRATE 
and not P_IsObjectOnGround(player.mo) then
   player.charability = CA_FLOAT
     end

if player.mo and player.mo.skin == "pizzano"
and not P_IsObjectOnGround(player.mo)
and player.speed <= 36*FRACUNIT 
and player.charability == CA_FLOAT then
   player.charability = CA_DOUBLEJUMP
     end
 end)