local normal = 1
local idle = 0
local enemy = 3
local item = 2
local wispobjects = {
"MT_DRILLBOX",
"MT_ASTEROIDBOX",
"MT_BURSTBOX",
"MT_ROCKETBOX",
"MT_LASERBOX",
"MT_BOOSTBOX"}

addHook("MapLoad", do
	for player in players.iterate
		player.idletics = 0
	end
end)

addHook("MapChange", do
	for player in players.iterate
		player.idletics = 0
	end
end)

//Some parts here are ommitted due to uselessness.
//This part is taken and modified from inuyasha's code inside paperbros.wad
local enemy_list = {}

addHook("MapLoad", function()
	enemy_list = {}
	for	mobj in thinkers.iterate("mobj") do
		if (mobj.flags & MF_BOSS)
		or (mobj.flags & MF_ENEMY)then
			table.insert(enemy_list, mobj)
		end
	end
end)


/*
local ring_list = {}
addHook("MapLoad", function()
	ring_list = {}
	for	mobj in thinkers.iterate("mobj") do
		if (mobj.type == MT_RING) or (mobj.type == MT_FLINGRING) or (mobj.type == MT_COIN) or (mobj.type == MT_FLINGCOIN) or (mobj.type == MT_BLUEBALL) then
			table.insert(ring_list, mobj)
		end
	end
end)

local function LookForRings(player)
	if #ring_list <= 0 then
		return false
	end
	local d = INT32_MAX
	for	_,mobj in pairs(ring_list) do
		if not mobj.valid or not mobj.health then
			continue
		end
		local r = R_PointToDist2(player.mo.x, player.mo.y, mobj.x, mobj.y)
		if r < d then
			//player.mo.target = mobj
			d = r
		end
	end
	return mobj
end
*/
local wisp_list = {}
addHook("MapLoad", function()
	wisp_list = {}
	for	mobj in thinkers.iterate("mobj") do
		if (mobj.type == MT_ASTEROIDBOX) or (mobj.type == MT_ROCKETBOX) or (mobj.type == MT_DRILLBOX) or (mobj.type == MT_BURSTBOX) or (mobj.type == MT_BOOSTBOX) or (mobj.type == MT_LASERBOX) then
			table.insert(wisp_list, mobj)
		end
	end
end)

local function LookForAliens(player)
	if #wisp_list <= 0 then
		return false
	end
	local d = INT32_MAX
	for	_,mobj in pairs(wisp_list) do
		if not mobj.valid or not mobj.health then
			continue
		end
		local r = R_PointToDist2(player.mo.x, player.mo.y, mobj.x, mobj.y)
		if r < d then
			//player.mo.target = mobj
			d = r
		end
	end
	return mobj
end

local function LookForEnemyBetterSonic(player)
	-- Find the closest enemy/boss to the player and set the player's target to it.
	-- If there are no enemies left near the player, return false
	if #enemy_list <= 0 then
		return false
	end
	local d = 600*FRACUNIT
	for	_,mobj in pairs(enemy_list) do
		if not mobj.valid or not mobj.health then
			continue
		end
		local r = R_PointToDist2(player.mo.x, player.mo.y, mobj.x, mobj.y)
		if r < d then
			player.mo.target = mobj
			d = r
		end
	end
	return true
end
local function LookForBossSonic(player)
	-- Find the closest enemy/boss to the player and set the player's target to it.
	-- If there are no enemies left near the player, return false
	if #enemy_list <= 0 then
		return false
	end
	for	_,mobj in pairs(enemy_list) do
		if not mobj.valid or not mobj.health then
			continue
		end
		if (mobj.flags & MF_BOSS)
			player.mo.target = mobj
		end
	end
	return true
end

//New Code
addHook("BotAI", function(p1, p2)
	local button = {
		forward = false,
		backward = false,
		left = false,
		right = false,
		strafeleft = false,
		straferight = false,
		jump = false,
		spin = false,
	}
	
	local player = p1.player
	local sonic = p2.player
	
	sonic.custom1 = false
	sonic.custom2 = false
	sonic.custom3 = false
	
	sonic.boostmeter = 64 //always allow sonic to boost, no matter what
	
	if player.powers[pw_super]
		if not sonic.powers[pw_super]
			P_DoSuperTransformation(sonic, true)
			sonic.rings = 50
		end
		if sonic.powers[pw_super]
			sonic.rings = player.rings
		end
		//sonic.powers[pw_invulnerability] = player.powers[pw_super]
	end
	
	local map = G_BuildMapName(mapnum)
	
	if not (sonic.mo.target and sonic.mo.target.valid)
	and not ((map == "MAP03") or (map == "MAP06") or (map == "MAP09") or (map == "MAP12") or (map == "MAP25"))
		LookForEnemyBetterSonic(sonic)
	end
	if not (sonic.mo.target and sonic.mo.target.valid)
	and (player.rings == 0)
	and not ((map == "MAP03") or (map == "MAP06") or (map == "MAP09") or (map == "MAP12") or (map == "MAP25"))
		A_FindTarget(sonic.mo, MT_RING, 0)
	end
	if not (sonic.mo.target and sonic.mo.target.valid)
	and ((map == "MAP03") or (map == "MAP06") or (map == "MAP09") or (map == "MAP12") or (map == "MAP25"))
		A_FindTarget(sonic.mo, MT_RING, 0)
	end
	
	if (sonic.mo.target and sonic.mo.target.valid)
	and not ((sonic.mo.target.flags & MF_ENEMY) or (sonic.mo.target.flags & MF_BOSS))then
		sonic.mo.target = nil //fix for targeting monitors when there are enemies within range.
	end
	if not ((map == "MAP03") or (map == "MAP06") or (map == "MAP09") or (map == "MAP12") or (map == "MAP25"))
	and not (player.cmd.buttons & BT_ATTACK)
		if (sonic.mo.target and sonic.mo.target.valid)
			sonic.mo.tracer = P_SpawnMobj(sonic.mo.target.x + sonic.mo.target.momx*3,
				sonic.mo.target.y + sonic.mo.target.momy*3,
				sonic.mo.target.z + sonic.mo.target.momz*3, MT_THOK)
			sonic.mo.tracer.tics = 1
			sonic.mo.tracer.flags2 = $1|MF2_DONTDRAW
			button.forward = true
			sonic.mo.angle = R_PointToAngle2(sonic.mo.x, sonic.mo.y, sonic.mo.tracer.x, sonic.mo.tracer.y)
			if R_PointToDist2(sonic.mo.x, sonic.mo.y, sonic.mo.target.x, sonic.mo.target.y) < 6*sonic.mo.target.radius
				button.jump = true
			end
			if (R_PointToDist2(sonic.mo.x, sonic.mo.y, sonic.mo.target.x, sonic.mo.target.y) <= 4*sonic.mo.target.radius and R_PointToDist2(sonic.mo.x, sonic.mo.y, sonic.mo.target.x, sonic.mo.target.y) >= 3*sonic.mo.target.radius)
				button.jump = false
			end
		else
			if not (player.idletics >= 3*TICRATE)
				sonic.mo.angle = R_PointToAngle2(sonic.mo.x, sonic.mo.y, player.mo.x, player.mo.y)
				if (sonic.asteroidactive) or (player.asteroidactive)
					if R_PointToDist2(sonic.mo.x, sonic.mo.y, player.mo.x, player.mo.y) > 152*FRACUNIT
						button.forward = true
					end
					if R_PointToDist2(sonic.mo.x, sonic.mo.y, player.mo.x, player.mo.y) < 150*FRACUNIT
						button.backward = true
					end
				else
					if R_PointToDist2(sonic.mo.x, sonic.mo.y, player.mo.x, player.mo.y) > 100*FRACUNIT
						button.forward = true
					end
				end
				if R_PointToDist2(sonic.mo.x, sonic.mo.y, player.mo.x, player.mo.y) > 500*FRACUNIT
					sonic.custom1 = true
				end
				if ((sonic.mo.z*P_MobjFlip(sonic.mo) - player.mo.z*P_MobjFlip(player.mo)) < -sonic.mo.height)
				and not (sonic.powers[pw_carry] == CR_PLAYER)
					button.jump = true
				end
				if (not P_IsObjectOnGround(sonic.mo) and (abs(sonic.mo.momz) == FRACUNIT))
				or (sonic.mo.eflags & MFE_JUSTHITFLOOR)
				or (sonic.powers[pw_carry] == CR_PLAYER) then
					button.jump = false
				end
				if P_RandomByte() == 0
					button.jump = true
				end
			end
			if (player.idletics >= 3*TICRATE)
				if not (sonic.mo.target and sonic.mo.target.valid)
				sonic.mo.angle = R_PointToAngle2(sonic.mo.x, sonic.mo.y, player.mo.x, player.mo.y)
				if R_PointToDist2(sonic.mo.x, sonic.mo.y, player.mo.x, player.mo.y) > 60*FRACUNIT
					button.forward = true
				end
				if ((sonic.mo.z*P_MobjFlip(sonic.mo) - player.mo.z*P_MobjFlip(player.mo)) < -sonic.mo.height)
				and not (sonic.powers[pw_carry] == CR_PLAYER)
					button.jump = true
				end
				if (not P_IsObjectOnGround(sonic.mo) and (abs(sonic.mo.momz) == FRACUNIT))
				or (sonic.mo.eflags & MFE_JUSTHITFLOOR)
				or (sonic.powers[pw_carry] == CR_PLAYER) then
					button.jump = false
				end
				end
				if (sonic.mo.target and sonic.mo.target.valid)
					sonic.mo.angle = R_PointToAngle2(sonic.mo.x, sonic.mo.y, sonic.mo.target.x, sonic.mo.target.y)
					button.forward = true
					if R_PointToDist2(sonic.mo.x, sonic.mo.y, sonic.mo.target.x, sonic.mo.target.y) < 65*FRACUNIT
						button.jump = true
					end
				end
			end
		end
	end
	local mapno = G_BuildMapName(mapnum)
	//WISP AI TIME!
	if not (player.cmd.buttons & BT_ATTACK)
	and not ((mapno == "MAP03") or (mapno == "MAP06") or (mapno == "MAP09") or (mapno == "MAP12") or (mapno == "MAP25"))
	if player.bursting
		if not (sonic.mo.target and sonic.mo.target.valid)
			A_FindTarget(sonic.mo, MT_BURSTBOX, 0)
		end
		if sonic.wisp1 == "burst"
			sonic.custom2 = true
		end
		if sonic.bursting
		and (player.cmd.buttons & BT_JUMP)
			button.jump = true
		end
	end
	if player.rocketon
		if not (sonic.mo.target and sonic.mo.target.valid)
			A_FindTarget(sonic.mo, MT_ROCKETBOX, 0)
		end
		if sonic.wisp1 == "rocket"
			sonic.custom2 = true
		end
		if sonic.rocketon
		and (player.cmd.buttons & BT_JUMP)
			button.jump = true
		end
	end
	if player.asteroidactive
	and not (map == "MAP03") then //Don't mess with the boss ai, bro
		if not (sonic.mo.target and sonic.mo.target.valid)
			A_FindTarget(sonic.mo, MT_ASTEROIDBOX, 0)
		end
		if (sonic.mo.target and sonic.mo.target.valid)
		and (sonic.mo.target.type == MT_ASTEROIDBOX)
			sonic.mo.angle = R_PointToAngle2(sonic.mo.x, sonic.mo.y, sonic.mo.target.x, sonic.mo.target.y)
			button.forward = true
		end
		if sonic.wisp1 == "asteroid"
			sonic.custom2 = true
		end
		if sonic.asteroidactive
			if (player.cmd.buttons & BT_JUMP)
				button.jump = true
			end
			if (player.cmd.buttons & BT_SPIN)
				button.spin = true
			end
		end
	end
	if player.drilling
		if not (sonic.mo.target and sonic.mo.target.valid)
			A_FindTarget(sonic.mo, MT_DRILLBOX, 0)
		end
		if (sonic.mo.target and sonic.mo.target.valid)
		and (sonic.mo.target.type == MT_DRILLBOX)
			sonic.mo.angle = R_PointToAngle2(sonic.mo.x, sonic.mo.y, sonic.mo.target.x, sonic.mo.target.y)
			button.forward = true
		end
		if sonic.wisp1 == "drill"
			sonic.custom2 = true
		end
		if sonic.drilling
			if (player.cmd.buttons & BT_JUMP)
				button.jump = true
			end
			if (player.cmd.buttons & BT_SPIN)
				button.spin = true
			end
		end
	end
	if player.laserphase1
		if not (sonic.mo.target and sonic.mo.target.valid)
			A_FindTarget(sonic.mo, MT_LASERBOX, 0)
		end
		if (sonic.mo.target and sonic.mo.target.valid)
		and (sonic.mo.target.type == MT_LASERBOX)
			sonic.mo.angle = R_PointToAngle2(sonic.mo.x, sonic.mo.y, sonic.mo.target.x, sonic.mo.target.y)
			button.forward = true
		end
		if sonic.wisp1 == "laser"
			sonic.custom2 = true
		end
		if sonic.laserphase1
			sonic.mo.angle = player.mo.angle
			if (player.cmd.buttons & BT_JUMP)
				button.jump = true
			end
			if (player.cmd.buttons & BT_SPIN)
				button.spin = true
			end
			if (player.cmd.buttons & BT_CUSTOM2)
				sonic.custom2 = true
			end
		end
	end
	if P_RandomByte() == 0
		sonic.custom2 = true
	end
	end
	
	//Bot Control - Hold Ring toss and the bot mimics your movements and angle
	//Kinda similar to the Double Cherry powerup in super mario 3d world.
	if (player.cmd.buttons & BT_ATTACK)
		sonic.mo.angle = player.mo.angle
		if (player.cmd.forwardmove > 0)
			button.forward = true
		end
		if (player.cmd.forwardmove < 0)
			button.backward = true
		end
		if (player.cmd.sidemove > 0)
			button.straferight = true
		end
		if (player.cmd.sidemove < 0)
			button.strafeleft = true
		end
		if (player.cmd.buttons & BT_JUMP)
			button.jump = true
		end
		if (player.cmd.buttons & BT_SPIN)
			button.spin = true
		end
		if (player.cmd.buttons & BT_CUSTOM1)
			sonic.custom1 = true
		end
		if (player.cmd.buttons & BT_CUSTOM2)
			sonic.custom2 = true
		end
		if (player.cmd.buttons & BT_CUSTOM3)
			sonic.custom3 = true
		end
	end
	
	if not (player.cmd.buttons & BT_ATTACK)
	if (map == "MAP03")
		//First off, set P1's tracer to eggman to track his HP
		if not (player.mo.tracer and player.mo.tracer.valid)
			A_FindTracer(player.mo, MT_EGGMOBILE, 0)
		end
		
		//Next, grab an asteroid wisp box before heading towards the boss.
		if not (sonic.mo.target and sonic.mo.target.valid)
		and (not (sonic.wisp1 == "asteroid") or (((sonic.asteroidactive) and (sonic.wisptimer <= 4*TICRATE)) and ((sonic.mo.target and sonic.mo.target.valid) and (sonic.mo.target.flags & MF_BOSS))))
		//and (player.mo.tracer and player.mo.tracer.valid)
		and not (player.mo.tracer.flags2 & MF2_BOSSDEAD)
			A_FindTarget(sonic.mo, MT_ASTEROIDBOX, 0)
		end
		
		if (sonic.mo.target and sonic.mo.target.valid)
		and (sonic.mo.target.type == MT_ASTEROIDBOX)
		//and (player.mo.tracer and player.mo.tracer.valid)
		//and not (sonic.mo.target.flags2 & MF2_BOSSFLEE|MF2_BOSSDEAD)
			sonic.mo.tracer = P_SpawnMobj(sonic.mo.target.x + sonic.mo.target.momx*3,
				sonic.mo.target.y + sonic.mo.target.momy*3,
				sonic.mo.target.z + sonic.mo.target.momz*3, MT_THOK)
			sonic.mo.tracer.tics = 1
			sonic.mo.tracer.flags2 = $1|MF2_DONTDRAW
			button.forward = true
			sonic.mo.angle = R_PointToAngle2(sonic.mo.x, sonic.mo.y, sonic.mo.tracer.x, sonic.mo.tracer.y)
			if ((abs(sonic.mo.target.z) - abs(sonic.mo.z)) > sonic.mo.height)
				button.jump = true
				button.spin = true
			end
			if (not P_IsObjectOnGround(sonic.mo) and (abs(sonic.mo.momz) == FRACUNIT))
			or (sonic.mo.eflags & MFE_JUSTHITFLOOR) then
				button.jump = false
			end
			if P_RandomByte() == 0
				button.jump = true
			end
		end
		//Got a wisp? go get eggman then!
		if (sonic.wisp1 == "asteroid")
		and (player.mo.tracer and player.mo.tracer.valid)
		and (player.mo.tracer.flags & MF_BOSS)
		and not (player.mo.tracer.flags2 & MF2_BOSSFLEE|MF2_BOSSDEAD) //Make sure the boss's hit points havent fully depleted first!
			//First things first. Activate asteroid!
			if (sonic.wisp1 == "asteroid")
				sonic.custom2 = true
			else
				sonic.custom2 = false
			end
			if (sonic.wisptimer < 4*TICRATE) //warning! asteroid is running out!
				A_FindTarget(sonic.mo, MT_ASTEROIDBOX, 0)
			end
			
			//Sonic will try to follow eggman's movements by about a second here
			sonic.mo.tracer = P_SpawnMobj(player.mo.tracer.x + player.mo.tracer.momx*TICRATE,
				player.mo.tracer.y + player.mo.tracer.momy*TICRATE,
				player.mo.tracer.z + player.mo.tracer.momz*TICRATE, MT_THOK)
			sonic.mo.tracer.tics = 1
			sonic.mo.tracer.flags2 = $1|MF2_DONTDRAW
			button.forward = true
			sonic.mo.angle = R_PointToAngle2(sonic.mo.x, sonic.mo.y, sonic.mo.tracer.x, sonic.mo.tracer.y)
			
			if ((abs(sonic.mo.target.z) - abs(sonic.mo.z)) > sonic.mo.height)
				button.jump = true
			end
			if (not P_IsObjectOnGround(sonic.mo) and (abs(sonic.mo.momz) == FRACUNIT))
			or (sonic.mo.eflags & MFE_JUSTHITFLOOR) then
				button.jump = false
			end
			if P_RandomByte() == 0
				button.jump = true
			end
			if ((abs(sonic.mo.target.z) - abs(sonic.mo.z)) < sonic.mo.target.height/2)
				button.spin = true
			end
		end
		if  (player.mo.tracer and player.mo.tracer.valid)
		and (player.mo.tracer.flags2 & MF2_BOSSFLEE)
			//the boss is dead, so just return to normal AI
			if not ((false) or (false))
				sonic.mo.angle = R_PointToAngle2(sonic.mo.x, sonic.mo.y, player.mo.x, player.mo.y)
				if (sonic.asteroidactive) or (player.asteroidactive)
					if R_PointToDist2(sonic.mo.x, sonic.mo.y, player.mo.x, player.mo.y) > 152*FRACUNIT
						button.forward = true
					end
					if R_PointToDist2(sonic.mo.x, sonic.mo.y, player.mo.x, player.mo.y) < 150*FRACUNIT
						button.backward = true
					end
				else
					if R_PointToDist2(sonic.mo.x, sonic.mo.y, player.mo.x, player.mo.y) > 100*FRACUNIT
						button.forward = true
					end
				end
				if R_PointToDist2(sonic.mo.x, sonic.mo.y, player.mo.x, player.mo.y) > 500*FRACUNIT
					sonic.custom1 = true
				end
				if ((abs(player.mo.z) - abs(sonic.mo.z)) > sonic.mo.height)
					button.jump = true
				end
				if (not P_IsObjectOnGround(sonic.mo) and (abs(sonic.mo.momz) == FRACUNIT))
				or (sonic.mo.eflags & MFE_JUSTHITFLOOR) then
					button.jump = false
				end
				if P_RandomByte() == 0
					button.jump = true
				end
			end
			if (false)
				button.forward = true
				button.jump = true
			elseif ((false) and (player.mo.tics == 1))
				sonic.mo.angle = R_PointToAngle2(sonic.mo.x, sonic.mo.y, player.mo.x, player.mo.y) + P_RandomRange(-ANGLE_67h, ANGLE_67h)
				button.jump = false
			end
		end
	elseif (map == "MAP06")
		if not (sonic.mo.target and sonic.mo.target.valid)
			LookForBossSonic(sonic)
		end
		if (sonic.mo.target and sonic.mo.target.valid)
		and (sonic.mo.target.flags & MF_BOSS)
		and not (sonic.mo.target.flags2 & MF2_BOSSFLEE)
			//Attack Eggman by jumping with a homing attack in the first phase
			if (sonic.mo.target.health > 3)
				sonic.mo.tracer = P_SpawnMobj(sonic.mo.target.x + sonic.mo.target.momx*TICRATE,
					sonic.mo.target.y + sonic.mo.target.momy*TICRATE,
					sonic.mo.target.z + sonic.mo.target.momz*TICRATE, MT_THOK)
				sonic.mo.tracer.tics = 1
				sonic.mo.tracer.flags2 = $1|MF2_DONTDRAW
				button.forward = true
				sonic.mo.angle = R_PointToAngle2(sonic.mo.x, sonic.mo.y, sonic.mo.tracer.x, sonic.mo.tracer.y)
				if ((abs(sonic.mo.target.z) - abs(sonic.mo.z)) > sonic.mo.height)
					button.jump = true
				end
				if (not P_IsObjectOnGround(sonic.mo) and (abs(sonic.mo.momz) == FRACUNIT))
				or (sonic.mo.eflags & MFE_JUSTHITFLOOR) then
					button.jump = false
				end
				if P_RandomByte() == 0
					button.jump = true
				end
			else
				//Eggman is now in phase 2. Perform phase 2 AI now!
				sonic.mo.tracer = P_SpawnMobj(sonic.mo.target.x + sonic.mo.target.momx*TICRATE,
					sonic.mo.target.y + sonic.mo.target.momy*TICRATE,
					sonic.mo.target.z + sonic.mo.target.momz*TICRATE, MT_THOK)
				sonic.mo.tracer.tics = 1
				sonic.mo.tracer.flags2 = $1|MF2_DONTDRAW
				button.forward = true
				sonic.mo.angle = R_PointToAngle2(sonic.mo.x, sonic.mo.y, sonic.mo.tracer.x, sonic.mo.tracer.y)
				if ((abs(sonic.mo.target.z) - abs(sonic.mo.z)) > sonic.mo.height)
					button.jump = true
				end
				if (not P_IsObjectOnGround(sonic.mo) and (abs(sonic.mo.momz) == FRACUNIT))
				or (sonic.mo.eflags & MFE_JUSTHITFLOOR) then
					button.jump = false
				end
				//Still the same as the first AI, but with no random jump, basically
			end
		end
		if (sonic.mo.target and sonic.mo.target.valid)
		and (sonic.mo.target.flags2 & MF2_BOSSFLEE)
			//the boss is dead, so just follow the player again...
			if not ((false) or (false))
				sonic.mo.angle = R_PointToAngle2(sonic.mo.x, sonic.mo.y, player.mo.x, player.mo.y)
				if (sonic.asteroidactive) or (player.asteroidactive)
					if R_PointToDist2(sonic.mo.x, sonic.mo.y, player.mo.x, player.mo.y) > 152*FRACUNIT
						button.forward = true
					end
					if R_PointToDist2(sonic.mo.x, sonic.mo.y, player.mo.x, player.mo.y) < 150*FRACUNIT
						button.backward = true
					end
				else
					if R_PointToDist2(sonic.mo.x, sonic.mo.y, player.mo.x, player.mo.y) > 100*FRACUNIT
						button.forward = true
					end
				end
				if R_PointToDist2(sonic.mo.x, sonic.mo.y, player.mo.x, player.mo.y) > 500*FRACUNIT
					sonic.custom1 = true
				end
				if ((abs(player.mo.z) - abs(sonic.mo.z)) > sonic.mo.height)
					button.jump = true
				end
				if (not P_IsObjectOnGround(sonic.mo) and (abs(sonic.mo.momz) == FRACUNIT))
				or (sonic.mo.eflags & MFE_JUSTHITFLOOR) then
					button.jump = false
				end
				if P_RandomByte() == 0
					button.jump = true
				end
			end
			if (false)
				button.forward = true
				button.jump = true
			elseif ((false) and (player.mo.tics == 1))
				sonic.mo.angle = R_PointToAngle2(sonic.mo.x, sonic.mo.y, player.mo.x, player.mo.y) + P_RandomRange(-ANGLE_67h, ANGLE_67h)
				button.jump = false
			end
		end
	elseif (map == "MAP09")
		//Sea egg was a difficult one, so sonic just collects rings rather than goes for the boss... sorry, guys!
		if (player.rings == 0)
			if not (sonic.mo.target and sonic.mo.target.valid)
				A_FindTarget(sonic.mo, MT_RING, 0)
			end
		if (sonic.mo.target and sonic.mo.target.valid)
			sonic.mo.tracer = P_SpawnMobj(sonic.mo.target.x + sonic.mo.target.momx*3,
				sonic.mo.target.y + sonic.mo.target.momy*3,
				sonic.mo.target.z + sonic.mo.target.momz*3, MT_THOK)
			sonic.mo.tracer.tics = 1
			sonic.mo.tracer.flags2 = $1|MF2_DONTDRAW
			button.forward = true
			sonic.mo.angle = R_PointToAngle2(sonic.mo.x, sonic.mo.y, sonic.mo.tracer.x, sonic.mo.tracer.y)
			if ((abs(sonic.mo.target.z) - abs(sonic.mo.z)) > sonic.mo.height)
				button.jump = true
			end
			if (not P_IsObjectOnGround(sonic.mo) and (abs(sonic.mo.momz) == FRACUNIT))
			or (sonic.mo.eflags & MFE_JUSTHITFLOOR) then
				button.jump = false
			end
		end
		if P_RandomByte() == 0
			button.jump = true
		end
	elseif (map == "MAP12")
		//Ah, good ol' Eggscalisbur... how beautiful can it get?
		if not (sonic.mo.target and sonic.mo.target.valid)
			LookForBossSonic(sonic)
		end
		if (sonic.mo.target and sonic.mo.target.valid)
			if not (sonic.mo.target.flags2 & MF2_BOSSFLEE|MF2_BOSSDEAD)
				if not sonic.burst
					if not (sonic.mo.tracer and sonic.mo.tracer.valid)
						A_FindTracer(sonic.mo, MT_BURSTBOX, 0)
					else
						sonic.mo.angle = R_PointToAngle2(sonic.mo.x, sonic.mo.y, sonic.mo.tracer.x, sonic.mo.tracer.y)
						button.forward = true
					end
				else
					if not sonic.wisptimer
						sonic.custom2 = true
					end
					if (sonic.wisptimer > 6*TICRATE)
						if (sonic.burstholdtime < 22)
							button.jump = true
						end
					else
						if not (sonic.mo.tracer and sonic.mo.tracer.valid)
							A_FindTracer(sonic.mo, MT_BURSTBOX, 0)
						else
							sonic.mo.angle = R_PointToAngle2(sonic.mo.x, sonic.mo.y, sonic.mo.tracer.x, sonic.mo.tracer.y)
							button.forward = true
						end
					end
				end
			else
				if not ((false) or (false))
					sonic.mo.angle = R_PointToAngle2(sonic.mo.x, sonic.mo.y, player.mo.x, player.mo.y)
					if (sonic.asteroidactive) or (player.asteroidactive)
						if R_PointToDist2(sonic.mo.x, sonic.mo.y, player.mo.x, player.mo.y) > 152*FRACUNIT
							button.forward = true
						end
						if R_PointToDist2(sonic.mo.x, sonic.mo.y, player.mo.x, player.mo.y) < 150*FRACUNIT
							button.backward = true
						end
					else
						if R_PointToDist2(sonic.mo.x, sonic.mo.y, player.mo.x, player.mo.y) > 100*FRACUNIT
							button.forward = true
						end
					end
					if R_PointToDist2(sonic.mo.x, sonic.mo.y, player.mo.x, player.mo.y) > 500*FRACUNIT
						sonic.custom1 = true
					end
					if ((abs(player.mo.z) - abs(sonic.mo.z)) > sonic.mo.height)
						button.jump = true
					end
					if (not P_IsObjectOnGround(sonic.mo) and (abs(sonic.mo.momz) == FRACUNIT))
					or (sonic.mo.eflags & MFE_JUSTHITFLOOR) then
						button.jump = false
					end
					if P_RandomByte() == 0
						button.jump = true
					end
				end
				if (false)
					button.forward = true
					button.jump = true
				elseif ((false) and (player.mo.tics == 1))
					sonic.mo.angle = R_PointToAngle2(sonic.mo.x, sonic.mo.y, player.mo.x, player.mo.y) + P_RandomRange(-ANGLE_67h, ANGLE_67h)
					button.jump = false
				end
			end
		end
	elseif (map == "MAP25")
		//The final boss... here we go
		if not (sonic.mo.target and sonic.mo.target.valid)
			LookForBossSonic(sonic)
		end
		if (sonic.mo.target and sonic.mo.target.valid)
		and (sonic.mo.target.flags & MF_BOSS)
			//no wisp? head to the center!
			if not (sonic.mo.arenacenter and sonic.mo.arenacenter.valid)
				sonic.mo.arenacenter = P_SpawnMobj(0, 0, 0, MT_THOK)
				sonic.mo.arenacenter.tics = 3
				sonic.mo.arenacenter.flags2 = $1|MF2_DONTDRAW
			end
			if (sonic.mo.arenacenter and sonic.mo.arenacenter.valid)
				sonic.mo.angle = R_PointToAngle2(sonic.mo.x, sonic.mo.y, sonic.arenacenter.x, sonic.arenacenter.y)
				button.forward = true
				if R_PointToDist2(sonic.mo.x, sonic.mo.y, sonic.arenacenter.x, sonic.arenacenter.y) <= sonic.arenacenter.radius
					if not (sonic.mo.tracer and sonic.mo.tracer.valid)
						sonic.mo.tracer = LookForAliens(sonic)
					end
					if (sonic.mo.tracer and sonic.mo.tracer.valid)
						if (sonic.mo.tracer.type == MT_BOOSTBOX)
							sonic.mo.angle = R_PointToAngle2(sonic.mo.x, sonic.mo.y, sonic.mo.target.x, sonic.mo.target.y)
							if not (player.mo.tracer and player.mo.tracer.valid)
								A_FindTracer(player.mo, MT_CYBRAKDEMON_ELECTRIC_BARRIER, 0)
							end
							if (player.mo.tracer and player.mo.tracer.valid)
								if ((player.mo.tracer.state >= S_CYBRAKDEMONELECTRICBARRIER_DIE3) and (player.mo.tracer.state <= S_CYBRAKDEMONELECTRICBARRIER_SPARK_RANDOMLOOP))
									button.forward = true
									if (sonic.speed >= sonic.normalspeed/20)
										sonic.custom1 = true
									end
								else
									if R_PointToDist2(sonic.mo.x, sonic.mo.y, sonic.mo.target.x, sonic.mo.target.y) > 500*FRACUNIT
										button.forward = true
									end
									if R_PointToDist2(sonic.mo.x, sonic.mo.y, sonic.mo.target.x, sonic.mo.target.y) > 200*FRACUNIT
										button.backward = true
									end
								end
							end
						end
						if (sonic.mo.tracer.type == MT_LASERBOX)
							//do stuff
						end
						if (sonic.mo.tracer.type == MT_ASTEROIDBOX)
							//do stuff
						end
						if (sonic.mo.tracer.type == MT_BURSTBOX)
							//do stuff
						end
					end
				end
				sonic.mo.arenacenter.tics = 3
				sonic.mo.arenacenter.flags2 = $1|MF2_DONTDRAW
				if sonic.boostmode
					sonic.mo.angle = R_PointToAngle2(sonic.mo.x, sonic.mo.y, sonic.mo.target.x, sonic.mo.target.y)
					//if not
					if R_PointToDist2(sonic.mo.x, sonic.mo.y, sonic.mo.target.x, sonic.mo.target.y) > 500*FRACUNIT
						button.forward = true
					end
					if R_PointToDist2(sonic.mo.x, sonic.mo.y, sonic.mo.target.x, sonic.mo.target.y) > 200*FRACUNIT
						button.backward = true
					end
				end
			end
						
		end
	end
	end
	end
	return button
end, "sonic")

addHook("ThinkFrame", do
	for bot in players.iterate
		if not bot.mo then return end
		if (bot.bot == BOT_2PAI) or (bot.bot == BOT_MPAI)
			bot.boostmeter = 50
			if (bot.cmd.buttons & BT_CUSTOM3)
			and ((All7Emeralds(emeralds)) or (All7Emeralds(bot.powers[pw_emeralds])))
				if not bot.powers[pw_super]
					P_DoSuperTransformation(bot, true)
					bot.rings = 9999
				end
			end
		end
	end
end)

addHook("BotTiccmd", function(player, cmd)
	if player.custom1
		cmd.buttons = $1|BT_CUSTOM1
	end
	if player.custom2
		cmd.buttons = $1|BT_CUSTOM2
	end
	if player.custom3
		cmd.buttons = $1|BT_CUSTOM3
	end
	return false
end)

freeslot("sfx_rspawn")

addHook("MobjThinker", function(box)
	if not box.spawntic then box.spawntic = 3*TICRATE+1 end
	box.spawntic = $1 - 1
	if (box.spawntic == 1)
		local newbox = P_SpawnMobj(box.x, box.y, box.z, MT_LASERBOX)
		S_StartSound(newbox, sfx_rspawn)
		P_RemoveMobj(box)
	end
end, MT_LASERPLACEHOLDER)

addHook("MobjThinker", function(box)
	if not box.spawntic then box.spawntic = 3*TICRATE+1 end
	box.spawntic = $1 - 1
	if (box.spawntic == 1)
		local newbox = P_SpawnMobj(box.x, box.y, box.z, MT_BURSTBOX)
		S_StartSound(newbox, sfx_rspawn)
		P_RemoveMobj(box)
	end
end, MT_BURSTPLACEHOLDER)

addHook("MobjThinker", function(box)
	if not box.spawntic then box.spawntic = 3*TICRATE+1 end
	box.spawntic = $1 - 1
	if (box.spawntic == 1)
		local newbox = P_SpawnMobj(box.x, box.y, box.z, MT_BOOSTBOX)
		S_StartSound(newbox, sfx_rspawn)
		P_RemoveMobj(box)
	end
end, MT_BOOSTPLACEHOLDER)

addHook("MobjThinker", function(box)
	if not box.spawntic then box.spawntic = 3*TICRATE+1 end
	box.spawntic = $1 - 1
	if (box.spawntic == 1)
		local newbox = P_SpawnMobj(box.x, box.y, box.z, MT_ASTEROIDBOX)
		S_StartSound(newbox, sfx_rspawn)
		P_RemoveMobj(box)
	end
end, MT_ASTEROIDPLACEHOLDER)

addHook("MobjThinker", function(box)
	if not box.spawntic then box.spawntic = 3*TICRATE+1 end
	box.spawntic = $1 - 1
	if (box.spawntic == 1)
		local newbox = P_SpawnMobj(box.x, box.y, box.z, MT_ROCKETBOX)
		S_StartSound(newbox, sfx_rspawn)
		P_RemoveMobj(box)
	end
end, MT_ROCKETPLACEHOLDER)


addHook("MobjThinker", function(box)
	if not box.spawntic then box.spawntic = 3*TICRATE+1 end
	box.spawntic = $1 - 1
	if (box.spawntic == 1)
		local newbox = P_SpawnMobj(box.x, box.y, box.z, MT_DRILLBOX)
		S_StartSound(newbox, sfx_rspawn)
		P_RemoveMobj(box)
	end
end, MT_DRILLPLACEHOLDER)
