------Mood------


local faces = myHead.Faces
status:set("currentFace",myHead.faceIdle)

local idleIndex = 5
function movementLogic()
	local target = status:get("currentTarget")
	local dist = core.checkDist(target,myRoot)
	local canSee = core.checkSight(target)
	
	idleIndex -= 1
	if canSee then
		idleIndex = 5 --100
	end
	if ((dist > 50 or not canSee) and (myRoot.Position.Y < target.Position.Y-10 or idleIndex <= 0)) then
		movement.pathToLocation(target)
	elseif (dist > 30 or not canSee) and core.checkPotentialSight(target) then
		myHuman:MoveTo(movement.slowAdvance())
		wait(0.3)
	elseif dist > 50 and dist < 90 and canSee and status:get("m4Equipped") then
		myHuman:MoveTo(movement.strafe())
		wait(0.5)
	elseif dist > 4 and dist < 50 and canSee and math.abs(myRoot.Position.Y - target.Position.Y) < 2 and status:get("weaponAimed") then
		myHuman:MoveTo(movement.retreat())
		wait(0.2)
	elseif (dist < 4 and canSee) or status:get("knifeEquipped") then
		myHuman:MoveTo(target.Position)
	end
end


function movementLoop()
	while myHuman.Health>0 do
		if status:get("currentTarget") then
			movementLogic()
		else
			if mySettings.Wander.Value and math.random(4) == 3 then
				movement.walkRandom()
			end
			wait(3)
		end
		wait(0.1)
	end
end

function getIncomingAttackers()
	
	local targets = {}
	
	local params = RaycastParams.new()
	params.FilterDescendantsInstances = {marine}
	for i = -20, 20, 2 do
		local origin = myRoot.Position + Vector3.new(-20,0,i)
		local dir = Vector3.new(40,0,0)
		troubleshoot.createBeam(origin,dir)
		local result = workspace:Raycast(origin, dir)
		if result then
			local human = core.getHuman(result.Instance.Parent)
			local root = result.Instance.Parent:FindFirstChild("HumanoidRootPart")
			if human and human.Health>0 and root and not core.isAlly(result.Instance.Parent) then
				if core.checkSight(root) then
					table.insert(targets,root)
				end
			end
		end
	end
	
	--Sort through targets to target the closest one to us
	local dist = math.huge
	local tempTarget = nil
	for _,potentialTarget in ipairs(targets) do
		if core.checkDist(potentialTarget,myRoot) < dist then
			dist = core.checkDist(potentialTarget,myRoot)
			tempTarget = potentialTarget
		end
	end
	
	if tempTarget then
		status:set("currentTarget",tempTarget)
	end
end

function searchTargetLoop()
	while myHuman.Health>0 do
		getIncomingAttackers()
		if not status:get("currentTarget") or not status:get("currentTarget").Parent or status:get("currentTarget").Parent.Humanoid.Health <=0 or status:get("tookDamage") then
			status:set("tookDamage",false)
			status:set("currentTarget",nil)
			targeting.findTarget()
		elseif not core.checkSight(status:get("currentTarget")) then
			targeting.findTarget()
			if not status:get("currentTarget") then
				wait(3)
			end
		end
		wait(0.5)
	end
end

function aimingLoop()
	while myHuman.Health>0 do
		if status:get("currentTarget") then
			if core.checkSight(status:get("currentTarget")) and status:get("m4Equipped") then
				actions.aim(status:get("currentTarget"))
			else
				wait(0.5)
			end
		else
			wait(2)
		end
		wait(0.1)
	end
end

function attackLogic()
	actions.updateFace("Attacking")
	
	local target = status:get("currentTarget")
	
	local distance = core.checkDist(myRoot,target)
	if distance > 7 then
		
		--If there is a cluster of enemies far enough away throw grenade.
		if combat.checkCluster(target) and distance < 100 and distance > 40 and status:get("grenadeCool") then
			combat.throwGrenade(target)
		elseif status:get("m4Equipped") then
			if core.facingTarget() and core.checkShot(target) then
				combat.shoot(target)
			end
		else
			actions.drawM4()
		end
	else
		actions.drawKnife()
		combat.stab()
	end
end

function attackLoop()
	while myHuman.Health>0 do
		
		local target = status:get("currentTarget")
		
		if target and target.Parent then
			actions.updateFace("Hunting")
			
			--If we have a target we can see
			if core.checkSight(status:get("currentTarget")) and core.checkDist(myRoot,target) < mySettings.M4.Range.Value then
				attackLogic()
			elseif status:get("m4Equipped") and not status:get("weaponAimed") then
				status:set("weaponAimed",false)
				actions.lowerM4()
			end
		else
			actions.updateFace("Idle")
			actions.yieldWeapons()
			wait(2)
		end
		wait(0.1)
	end
end

core.spawn(searchTargetLoop)
core.spawn(attackLoop)
core.spawn(movementLoop)
core.spawn(aimingLoop)