local function DollBullets(mo, doaction)
	local player = mo.player
	
	if player.tdactioncolorized
		mo.color = player.powers[pw_shield] & SH_FIREFLOWER and SKINCOLOR_WHITE or player.skincolor
		mo.colorized = false
		player.tdactioncolorized = false
	end
	
	if player.dolldelay or (player.dollshield and player.dollshield.valid)
		if doaction == 1 then S_StartSound(mo,sfx_s3k8c,player) end
		player.canguard = false
		return
	end
	
	player.actiontext = "Bullets"
	player.actionrings = 10
	
	if not(CBW_Battle.CanDoAction(player))
		S_StopSoundByID(mo,sfx_tdchrg)
		S_StopSoundByID(mo,sfx_tdchg2)
		player.actionstate = 0
		return
	end
	
	if(player.actionstate)
		player.canguard = false
	end
	
	if not(player.actionstate)
		if(doaction == 1)
			CBW_Battle.PayRings(player)
			player.actionstate = 1
			player.secondjump = 0
			player.drawangle = mo.angle
			if player.tdsecret
				S_StartSound(mo,sfx_tdchg2)
			else
				S_StartSound(mo,sfx_tdchrg)
			end
		end
	end
	
	if(player.actionstate)
	
		// Spawning bullets!
		player.actionstate = $ + 1
		
		if player.actionstate < TICRATE*2/3
			if player.actionstate%2
				mo.color = CBW_Battle.Choose(SKINCOLOR_WHITE,SKINCOLOR_RED,SKINCOLOR_YELLOW,SKINCOLOR_ORANGE)
				mo.colorized = true
				player.tdactioncolorized = true
			end
		elseif not (player.actionstate%(TICRATE/6))
			local t = MK_LookForEnemies(mo, player, RING_DIST*3)
			local dist = mo.scale*4
			local x = mo.x+P_ReturnThrustX(mo,mo.angle,dist)
			local y = mo.y+P_ReturnThrustY(mo,mo.angle,dist)
			local z = mo.z+(P_MobjFlip(mo) == 1 and mo.height or 0)+mo.height/8*P_MobjFlip(mo)
			local bullet
			if t and t.valid
				bullet = P_SpawnXYZMissile(mo,t,MT_JETTBULLET,x,y,z)
			else
				bullet = P_SpawnPointMissile(mo,x+P_ReturnThrustX(mo,mo.angle,dist*2),
				y+P_ReturnThrustY(mo,mo.angle,dist*2),z-mo.height/8*P_MobjFlip(mo),MT_JETTBULLET,x,y,z)
			end
			if bullet and bullet.valid
				bullet.flags2 = ($&~MF2_OBJECTFLIP)|(mo.flags2&MF2_OBJECTFLIP)
				bullet.eflags = ($&~MFE_VERTICALFLIP)|(mo.eflags&MFE_VERTICALFLIP)
				bullet.momx = $*2
				bullet.momy = $*2
				bullet.momz = $*2
				bullet.color = GetTDShieldColor(player)
				bullet.colorized = true
			end
			P_Thrust(mo, mo.angle, -dist)
			if player.tdsecret
				S_StartSound(mo,sfx_tdlazr)
			else
				S_StartSound(mo,sfx_s3k4d)
			end
		end
		
		if(player.actionstate >= TICRATE)
			player.actionstate = 0
			CBW_Battle.ApplyCooldown(player, TICRATE)
		else
			mo.momx = $*19/20
			mo.momy = $*19/20
			mo.momz = mo.scale*P_MobjFlip(mo)
			player.powers[pw_nocontrol] = max($1, 1)
			player.drawangle = player.mo.angle
		end
	end
end

local TDExhaust = function(player)
	if player.secondjump
		local maxtime = 2*TICRATE
		player.exhaustmeter = max(0,$-FRACUNIT/maxtime)
	elseif P_IsObjectOnGround(player.mo)
		player.exhaustmeter = FRACUNIT
	end
	return true
end

local addedtailsdoll = false
local battlefirst = false
if CBW_Battle
	battlefirst = true
end

// Add Tails Doll!
addHook("ThinkFrame", function()
	if(CBW_Battle and not addedtailsdoll)
		local function GarbagePriority6(player)
			if player.dollshield and player.dollshield.valid
				CBW_Battle.SetPriority(player,1,1,nil,1,1,"poly barrier")
			end
		end
		
		CBW_Battle.SkinVars["tailsdoll"] = {
			flags = SKINVARS_GUARD,
			weight = 70,
			shields = 1,
			guard_frame = 1,
			special = DollBullets,
			func_priority_ext = GarbagePriority6,
			func_collide = TDCollide,
			func_exhaust = TDExhaust,
		}
		
		if SKINVARS_NOSPINSHIELD
			CBW_Battle.SkinVars["tailsdoll"].flags = $|SKINVARS_NOSPINSHIELD
		end
		
		print("\x87 Tails Doll's\x80 battle ability has been added!")
		if battlefirst
			print("\x82 WARNING:\x80 Adding BattleMod first will make Tails Doll not able to reflect certain projectiles. Add him before anything else to guarantee proper reflection!")
		end
		addedtailsdoll = true
	end
end)