Files
LuaCsForBarotraumaEP/Barotrauma/BarotraumaShared/Source/Characters/AI/Objectives/AIObjectiveManager.cs
Joonas Rikkonen 604fc65154 Human AI improvements & fixes:
- 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).
2017-12-21 19:49:26 +02:00

115 lines
3.3 KiB
C#

using System.Collections.Generic;
using System.Linq;
namespace Barotrauma
{
class AIObjectiveManager
{
public const float OrderPriority = 50.0f;
private List<AIObjective> objectives;
private Character character;
private AIObjective currentOrder;
public AIObjective CurrentOrder
{
get { return currentOrder; }
}
public AIObjective CurrentObjective
{
get;
private set;
}
public AIObjectiveManager(Character character)
{
this.character = character;
objectives = new List<AIObjective>();
}
public void AddObjective(AIObjective objective)
{
if (objectives.Find(o => o.IsDuplicate(objective)) != null) return;
objectives.Add(objective);
}
public T GetObjective<T>() where T : AIObjective
{
foreach (AIObjective objective in objectives)
{
if (objective is T) return (T)objective;
}
return null;
}
public float GetCurrentPriority(Character character)
{
if (CurrentOrder != null &&
(objectives.Count == 0 || currentOrder.GetPriority(this) > objectives[0].GetPriority(this)))
{
return CurrentOrder.GetPriority(this);
}
return objectives.Count == 0 ? 0.0f : objectives[0].GetPriority(this);
}
public void UpdateObjectives()
{
if (!objectives.Any()) return;
//remove completed objectives and ones that can't be completed
objectives = objectives.FindAll(o => !o.IsCompleted() && o.CanBeCompleted);
//sort objectives according to priority
objectives.Sort((x, y) => y.GetPriority(this).CompareTo(x.GetPriority(this)));
}
public void DoCurrentObjective(float deltaTime)
{
if (currentOrder != null && (!objectives.Any() || objectives[0].GetPriority(this) < currentOrder.GetPriority(this)))
{
CurrentObjective = currentOrder;
currentOrder.TryComplete(deltaTime);
return;
}
if (!objectives.Any()) return;
objectives[0].TryComplete(deltaTime);
CurrentObjective = objectives[0];
}
public void SetOrder(Order order, string option)
{
if (order == null) return;
currentOrder = null;
switch (order.Name.ToLowerInvariant())
{
case "follow":
currentOrder = new AIObjectiveGoTo(Character.Controlled, character, true);
break;
case "wait":
currentOrder = new AIObjectiveGoTo(character, character, true);
break;
case "fixleaks":
case "fix leaks":
currentOrder = new AIObjectiveFixLeaks(character);
break;
default:
if (order.TargetItem == null) return;
currentOrder = new AIObjectiveOperateItem(order.TargetItem, character, option, false, null, order.UseController);
break;
}
}
}
}