-- Voidy's Battlemod Stuff --
local ScriptLoaded = false

addHook("ThinkFrame", function()
	if not ScriptLoaded and CBW_Battle then
		ScriptLoaded = true
		local B = CBW_Battle
		local VoidyActions = function (mo, doaction)
			local player = mo.player
			if B.PreRoundWait()
				player.battlewait = true
			else
				player.battlewait = false
			end
			if player.dodgeroll == 1 
			or player.attacking == 1 
			or player.transforming == 1 
			or player.guardtimer > 0 
			or player.powers[pw_super]
				player.doaction = -1
				return
			end
			if player.dodgeroll == 0 
			and player.attacking == 0 
			and player.transforming == 0 
			and player.guardtimer == 0 
			and not B.CanDoAction(player) 
			and not player.powers[pw_super]
				player.actionstate = 0
				return
			end
			if P_IsObjectOnGround(mo)
				if player.rings == 0	
					if player.carneliancollected == 1
						if player.frenzied == 0
							player.actiontext = "Rocket Upper"
						else
							player.actiontext = "Rocket Chaser"
						end
					end
				else
					if not player.stanced
						player.actiontext = "Tail Stance"
					else
						if player.frenzied == 0
							player.actiontext = "Quasar Pulse"
							player.actionrings = 1
						else
							player.actiontext = "Quasar Stream"
							player.actionrings = 5
						end
					end
				end
				if player.stanced
					player.action2text = "Exiting Tail Stance in "..player.stancetimer.."..."
				end
			else
				if player.rings == 0
					if player.frenzied == 0
						player.actiontext = "Demolition"
					else
						player.actiontext = "Annihilation"
					end
				else
					if player.frenzied == 0
						player.actiontext = "Nail Bed"
					else
						player.actiontext = "Heat Wave"
					end
					player.actionrings = 25
				end
			end
			if player.actionstate == 0
				if doaction == 1	
					if (player.rings == 0 and not player.stanced) 
					or player.rings > 0
						player.actionstate = 1
						player.actiontime = 0
					end
					if player.rings > 0 and P_IsObjectOnGround(mo) and not player.stanced
						player.activatedstance = true
					end
					if (player.rings > 0 and not P_IsObjectOnGround(mo)) 
					or (player.rings > 0 and P_IsObjectOnGround(mo) and player.stanced)
						B.PayRings(player)
						player.hadrings = true
					end
				end
				if player.stanced
					player.pflags = $|PF_JUMPSTASIS
					if player.stancetimer > -1 and leveltime%12 == 0
						player.stancetimer = $ - 1
					end
					if player.stancetimer == -1
						player.activatedstance = false
						player.stanced = false
						player.stancetimer = 0
						player.mo.state = S_PLAY_STND
					end
				end
				player.shotpulse = false
				player.shotstream = false
			end
			if player.actionstate == 1
				if P_IsObjectOnGround(mo)
					if not player.activatedstance
						if player.carneliancollected == 1
							if player.frenzied == 0
								mo.state = S_BASIC_AIRFINISHB1
							else
								mo.state = S_FRENZY_AIRFINISHB1
								player.chasertimer = 20
							end
							S_StartSound(mo, sfx_upper)
							player.attacktimer = 20
							player.actionstate = 0
						end
					else
						player.actiontime = $+1
						if not player.stanced
							P_InstaThrust(player.mo, player.drawangle, -5*FRACUNIT)
							player.powers[pw_nocontrol] = 1
							if player.frenzied == 0	
								mo.state = S_BASIC_ENTERSTANCE
							else
								mo.state = S_FRENZY_ENTERSTANCE
							end
							if player.actiontime == 10
								player.stancetimer = 3
								player.stanced = true
								player.actionstate = 0
							end
						else
							if player.hadrings
								if player.frenzied == 0
									player.shotpulse = true
									local pulse = P_SPMAngle(mo, MT_QUASARPULSE, mo.angle, 1)
									if pulse and pulse.valid	
										pulse.scale = FRACUNIT*2
										pulse.colorized = true
										pulse.color = SKINCOLOR_NEON
									end
									mo.state = S_BASIC_TAILSHOOT
								else
									player.shotstream = true
									local pulse = P_SPMAngle(mo, MT_QUASARSTREAM, mo.angle, 1)
									if pulse and pulse.valid	
										pulse.colorized = true
										pulse.color = SKINCOLOR_NEON
									end
									mo.state = S_FRENZY_TAILSHOOT
								end
								player.hadrings = false
								player.stancetimer = 3
							end
							if player.shotpulse
								if player.actiontime == 3 
									player.actionstate = 0
								end
							end
							if player.shotstream
								if player.actiontime == 20
									player.actionstate = 0
								end
							end
						end
					end
				else
					if not player.hadrings
						if player.frenzied == 0
							mo.state = S_BASIC_AIRFINISHA1
						else
							mo.state = S_FRENZY_AIRFINISHA1
						end
						player.attacktimer = 20
						player.actionstate = 0
					else
						player.actiontime = $+1
						P_InstaThrust(mo, mo.angle, 0)
						P_SetObjectMomZ(mo, 0*FRACUNIT)
						player.pflags = ($|PF_SPINNING)&~PF_JUMPED
						if player.actiontime == 1
							mo.state = S_PLAY_ROLL
							S_StartSound(mo, sfx_s3k5a)
						elseif player.actiontime == 30
							player.actionstate = 2
							player.actiontime = 0
						end
					end
				end
			end
			if player.actionstate == 2
				player.actiontime = $+1
				if not (P_IsObjectOnGround(mo) or (mo.eflags & MFE_GOOWATER))
					P_SetObjectMomZ(mo, -30*FRACUNIT)
					local trail = P_SpawnGhostMobj(mo)
					trail.colorized = true
					trail.blendmode = AST_ADD
					trail.color = mo.color
					if player.actiontime == 1
						mo.state = S_PLAY_BATTLEFALL
						S_StartSound(mo, sfx_zoom)
					end
				else
					mo.state = S_PLAY_BATTLELAND
					player.hadrings = false
					player.attacktimer = 20
					player.actionstate = 0
				end
			end
		end
		CBW_Battle.SkinVars["voidy"] = {
			flags = SKINVARS_NOSPINSHIELD,
			weight = 120,
			shields = 0,
			special = VoidyActions,
		}
	end
end)