- Replaced item name comparisons with Prefab.NameMatches (-> item names can be changed without breaking the AIs). - Having an AIObjective set as the current order of the character doesn't automatically cause it to have a high priority. For example, the order to fix leaks has a low priority if there are no leaks to fix. - AIObjectiveFixLeaks makes sure the character is wearing a diving suit before going to fix a leak. The characters used to run in and out of flooded rooms because the AIObjectiveFindSafety objective would become active as soon as the character entered the room, causing the character to run out, and then immediately run back because they are no longer in immediate danger of drowning, making the FixLeaks objective the most high-priority one. - Characters attempt to find a room with no water in AIObjectiveIdle even if the character is wearing a diving suit. - AIObjectiveFindSafety considers flooded rooms dangerous even if the character is wearing a diving suit (-> the character attempts to go into a more dry room instead of happily idling in the flooded one). - Distance to a hull doesn't decrease its desirability nearly as much in AIObjectiveFindSafety (-> fixes characters not bothering to move into a non-flooded room if it's far away). - AIObjectiveOperateItem makes sure the item is equipped before using it (-> characters can't weld leaks with the welder in their inventory).
157 lines
5.6 KiB
C#
157 lines
5.6 KiB
C#
using Microsoft.Xna.Framework;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace Barotrauma
|
|
{
|
|
class AIObjectiveIdle : AIObjective
|
|
{
|
|
const float WallAvoidDistance = 150.0f;
|
|
|
|
private AITarget currentTarget;
|
|
private float newTargetTimer;
|
|
|
|
private AIObjectiveFindSafety findSafety;
|
|
|
|
public AIObjectiveIdle(Character character) : base(character, "")
|
|
{
|
|
}
|
|
|
|
public override bool IsCompleted()
|
|
{
|
|
return false;
|
|
}
|
|
|
|
public override float GetPriority(AIObjectiveManager objectiveManager)
|
|
{
|
|
return 1.0f;
|
|
}
|
|
|
|
protected override void Act(float deltaTime)
|
|
{
|
|
|
|
var pathSteering = character.AIController.SteeringManager as IndoorsSteeringManager;
|
|
|
|
if (pathSteering==null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (character.AnimController.InWater)
|
|
{
|
|
//attempt to find a safer place if in water
|
|
if (findSafety == null) findSafety = new AIObjectiveFindSafety(character);
|
|
findSafety.TryComplete(deltaTime);
|
|
return;
|
|
}
|
|
|
|
if (newTargetTimer <= 0.0f)
|
|
{
|
|
currentTarget = FindRandomTarget();
|
|
|
|
if (currentTarget != null)
|
|
{
|
|
Vector2 pos = character.SimPosition;
|
|
if (character != null && character.Submarine == null) pos -= Submarine.MainSub.SimPosition;
|
|
|
|
var path = pathSteering.PathFinder.FindPath(pos, currentTarget.SimPosition);
|
|
if (path.Cost > 200.0f && character.AnimController.CurrentHull!=null) return;
|
|
|
|
pathSteering.SetPath(path);
|
|
}
|
|
|
|
|
|
newTargetTimer = currentTarget == null ? 5.0f : 15.0f;
|
|
}
|
|
|
|
newTargetTimer -= deltaTime;
|
|
|
|
|
|
//wander randomly
|
|
// - if reached the end of the path
|
|
// - if the target is unreachable
|
|
// - if the path requires going outside
|
|
if (pathSteering==null || (pathSteering.CurrentPath != null &&
|
|
(pathSteering.CurrentPath.NextNode == null || pathSteering.CurrentPath.Unreachable || pathSteering.CurrentPath.HasOutdoorsNodes)))
|
|
{
|
|
//steer away from edges of the hull
|
|
if (character.AnimController.CurrentHull!=null)
|
|
{
|
|
float leftDist = character.Position.X - character.AnimController.CurrentHull.Rect.X;
|
|
float rightDist = character.AnimController.CurrentHull.Rect.Right - character.Position.X;
|
|
|
|
if (leftDist < WallAvoidDistance && rightDist < WallAvoidDistance)
|
|
{
|
|
if (Math.Abs(rightDist - leftDist) > WallAvoidDistance / 2)
|
|
{
|
|
pathSteering.SteeringManual(deltaTime, Vector2.UnitX * Math.Sign(rightDist - leftDist));
|
|
}
|
|
else
|
|
{
|
|
pathSteering.Reset();
|
|
return;
|
|
}
|
|
}
|
|
else if (leftDist < WallAvoidDistance)
|
|
{
|
|
pathSteering.SteeringManual(deltaTime, Vector2.UnitX * (WallAvoidDistance-leftDist)/WallAvoidDistance);
|
|
pathSteering.WanderAngle = 0.0f;
|
|
return;
|
|
}
|
|
else if (rightDist < WallAvoidDistance)
|
|
{
|
|
pathSteering.SteeringManual(deltaTime, -Vector2.UnitX * (WallAvoidDistance-rightDist)/WallAvoidDistance);
|
|
pathSteering.WanderAngle = MathHelper.Pi;
|
|
return;
|
|
}
|
|
}
|
|
|
|
character.AIController.SteeringManager.SteeringWander();
|
|
//reset vertical steering to prevent dropping down from platforms etc
|
|
character.AIController.SteeringManager.ResetY();
|
|
|
|
return;
|
|
}
|
|
|
|
if (currentTarget == null) return;
|
|
character.AIController.SteeringManager.SteeringSeek(currentTarget.SimPosition, 2.0f);
|
|
}
|
|
|
|
private AITarget FindRandomTarget()
|
|
{
|
|
if (Rand.Int(5)==1)
|
|
{
|
|
var idCard = character.Inventory.FindItem("ID Card");
|
|
if (idCard==null) return null;
|
|
|
|
foreach (WayPoint wp in WayPoint.WayPointList)
|
|
{
|
|
if (wp.SpawnType != SpawnType.Human || wp.CurrentHull==null) continue;
|
|
|
|
foreach (string tag in wp.IdCardTags)
|
|
{
|
|
if (idCard.HasTag(tag)) return wp.CurrentHull.AiTarget;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
List<Hull> targetHulls = new List<Hull>(Hull.hullList);
|
|
//ignore all hulls with fires or water in them
|
|
targetHulls.RemoveAll(h => h.FireSources.Any() || h.WaterVolume / h.Volume > 0.1f);
|
|
if (!targetHulls.Any()) return null;
|
|
|
|
return targetHulls[Rand.Range(0, targetHulls.Count)].AiTarget;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public override bool IsDuplicate(AIObjective otherObjective)
|
|
{
|
|
return (otherObjective is AIObjectiveIdle);
|
|
}
|
|
}
|
|
}
|