if (patrolbugs_initialized) then return end -- Check if the script has already been run

--Sawblade lua (Modified for Patrol Bugs)
--by Callmore (Mucked with by Ivo)
-- ch1ck4d33 wuz h3r3 stu1pd fuk3rs L0L0L0L0L0L

freeslot("MT_MIDNIGHTMOTORIST")--, "S_MMSAWBLADE2")

local function mmMotobugSpawn(mo)
	mo.scale = $*1
end

addHook("MobjSpawn", mmMotobugSpawn, MT_MIDNIGHTMOTORIST)

local function mmMotoristThink(mo)
	local speed = FixedMul(mo.info.speed, mo.scale)
	local xdir = FixedMul(sin(mo.angle - ANGLE_90), speed)
	local ydir = FixedMul(cos(mo.angle + ANGLE_90), speed)
	
	local didMove
	if mo.currentmovement then
		didMove = P_TryMove(mo, mo.x + xdir, mo.y + ydir)
	else
		didMove = P_TryMove(mo, mo.x - xdir, mo.y - ydir)
	end
	if not didMove then
		--print("Flipping sawblade")
		--mo.currentmovement = not $
		mo.angle = $+ANGLE_180
	end

end

addHook("MobjThinker", mmMotoristThink, MT_MIDNIGHTMOTORIST)

rawset(_G, "patrolbugs", true) -- The script ran, don't run it again
