(83868bb0e) Refactor AIObjectiveExtinguishFires to use the AIObjectiveLoop class. Significant refactoring of the other looping objectives. Reduce the duplicate code.
This commit is contained in:
@@ -1108,6 +1108,8 @@ namespace Barotrauma
|
||||
|
||||
private bool IsProperlyLatchedOnSub => LatchOntoAI != null && LatchOntoAI.IsAttachedToSub && SelectedAiTarget?.Entity == wallTarget?.Structure;
|
||||
|
||||
private bool IsProperlyLatchedOnSub => LatchOntoAI != null && LatchOntoAI.IsAttachedToSub && SelectedAiTarget?.Entity == wallTarget?.Structure;
|
||||
|
||||
//goes through all the AItargets, evaluates how preferable it is to attack the target,
|
||||
//whether the Character can see/hear the target and chooses the most preferable target within
|
||||
//sight/hearing range
|
||||
|
||||
@@ -464,7 +464,7 @@ namespace Barotrauma
|
||||
// Even the smallest fire reduces the safety by 50%
|
||||
float fire = hull.FireSources.Count * 0.5f + hull.FireSources.Sum(fs => fs.DamageRange) / hull.Size.X;
|
||||
float fireFactor = ignoreFire ? 1 : MathHelper.Lerp(1, 0, MathHelper.Clamp(fire, 0, 1));
|
||||
int enemyCount = Character.CharacterList.Count(e =>
|
||||
int enemyCount = Character.CharacterList.Count(e =>
|
||||
e.CurrentHull == hull && !e.IsDead && !e.IsUnconscious &&
|
||||
(e.AIController is EnemyAIController || (e.TeamID != character.TeamID && character.TeamID != Character.TeamType.FriendlyNPC && e.TeamID != Character.TeamType.FriendlyNPC)));
|
||||
// The hull safety decreases 90% per enemy up to 100% (TODO: test smaller percentages)
|
||||
|
||||
+13
-21
@@ -22,37 +22,29 @@ namespace Barotrauma
|
||||
|
||||
protected override void FindTargets()
|
||||
{
|
||||
foreach (Item item in Item.ItemList)
|
||||
{
|
||||
if (item.Prefab.Identifier != "battery" && !item.HasTag("battery")) { continue; }
|
||||
if (item.Submarine == null) { continue; }
|
||||
if (item.Submarine.TeamID != character.TeamID) { continue; }
|
||||
if (character.Submarine != null && !character.Submarine.IsEntityFoundOnThisSub(item, true)) { continue; }
|
||||
var battery = item.GetComponent<PowerContainer>();
|
||||
if (battery != null)
|
||||
{
|
||||
if (!ignoreList.Contains(battery))
|
||||
{
|
||||
if (!targets.Contains(battery))
|
||||
{
|
||||
targets.Add(battery);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
base.FindTargets();
|
||||
if (targets.None())
|
||||
{
|
||||
character.Speak(TextManager.Get("DialogNoBatteries"), null, 4.0f, "nobatteries", 10.0f);
|
||||
}
|
||||
else
|
||||
{
|
||||
targets.Sort((x, y) => x.ChargePercentage.CompareTo(y.ChargePercentage));
|
||||
// Sorting should be handled by the objective manager, because the targets should be subobjectives.
|
||||
//targets.Sort((x, y) => x.ChargePercentage.CompareTo(y.ChargePercentage));
|
||||
}
|
||||
}
|
||||
|
||||
protected override bool Filter(PowerContainer battery) => true;
|
||||
protected override bool Filter(PowerContainer battery)
|
||||
{
|
||||
var item = battery.Item;
|
||||
if (item.Submarine == null) { return false; }
|
||||
if (item.Submarine.TeamID != character.TeamID) { return false; }
|
||||
if (character.Submarine != null && !character.Submarine.IsEntityFoundOnThisSub(item, true)) { return false; }
|
||||
return true;
|
||||
}
|
||||
|
||||
protected override float TargetEvaluation() => targets.Max(t => 100 - t.ChargePercentage);
|
||||
protected override IEnumerable<PowerContainer> GetList() => batteryList;
|
||||
protected override AIObjective ObjectiveConstructor(PowerContainer battery) => new AIObjectiveOperateItem(battery, character, Option, false) { IsLoop = true };
|
||||
protected override AIObjective ObjectiveConstructor(PowerContainer battery) => new AIObjectiveOperateItem(battery, character, Option, false, priorityModifier: PriorityModifier) { IsLoop = true };
|
||||
}
|
||||
}
|
||||
|
||||
+4
-3
@@ -31,9 +31,10 @@ namespace Barotrauma
|
||||
// Vertical distance matters more than horizontal (climbing up/down is harder than moving horizontally)
|
||||
float dist = Math.Abs(character.WorldPosition.X - targetHull.WorldPosition.X) + Math.Abs(character.WorldPosition.Y - targetHull.WorldPosition.Y) * 2.0f;
|
||||
float distanceFactor = MathHelper.Lerp(1, 0.1f, MathUtils.InverseLerp(0, 10000, dist));
|
||||
float severityFactor = MathHelper.Lerp(0, 1, MathHelper.Clamp(targetHull.FireSources.Sum(fs => fs.Size.X) / targetHull.Size.X, 0, 1));
|
||||
// Devotion?
|
||||
return MathHelper.Lerp(0, 100, severityFactor * distanceFactor);
|
||||
float severity = AIObjectiveExtinguishFires.GetFireSeverity(targetHull);
|
||||
float severityFactor = MathHelper.Lerp(0, 1, severity / 100);
|
||||
float devotion = Math.Max(Priority, 10) / 100;
|
||||
return MathHelper.Lerp(0, 100, MathHelper.Clamp(devotion + severityFactor * distanceFactor, 0, 1));
|
||||
}
|
||||
|
||||
public override bool IsCompleted()
|
||||
|
||||
+23
-52
@@ -1,71 +1,42 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using Barotrauma.Extensions;
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
// TODO: use objective loop and sort the targets by severity
|
||||
class AIObjectiveExtinguishFires : AIObjective
|
||||
class AIObjectiveExtinguishFires : AIObjectiveLoop<Hull>
|
||||
{
|
||||
public override string DebugTag => "extinguish fires";
|
||||
public override bool ForceRun => true;
|
||||
public override bool KeepDivingGearOn => true;
|
||||
|
||||
private Dictionary<Hull, AIObjectiveExtinguishFire> extinguishObjectives = new Dictionary<Hull, AIObjectiveExtinguishFire>();
|
||||
|
||||
public AIObjectiveExtinguishFires(Character character, float priorityModifier = 1) : base(character, "", priorityModifier) { }
|
||||
|
||||
public override float GetPriority(AIObjectiveManager objectiveManager)
|
||||
protected override void FindTargets()
|
||||
{
|
||||
if (character.Submarine == null) { return 0; }
|
||||
float referenceSize = 100;
|
||||
// If the fire sources of inside a hull are half the reference size in total(e.g.), the severity for that hull should be 50.
|
||||
// The severity values for all hulls are added together. So three hulls with 50 severity, would be 150 in total.
|
||||
// Thus the max priority of 100 is reached when one hull has firesources of size 100 in width, if the priority modifier is 1.
|
||||
float severity = character.Submarine.GetHulls(true).Sum(h => h.FireSources.Sum(fs => fs.Size.X) / referenceSize * 100);
|
||||
if (severity < 1) { return 0; }
|
||||
if (objectiveManager.CurrentOrder == this)
|
||||
{
|
||||
return AIObjectiveManager.OrderPriority;
|
||||
}
|
||||
float basePriority = MathHelper.Clamp(Priority, 0, 10);
|
||||
return MathHelper.Clamp(basePriority + severity * PriorityModifier, 0, 99);
|
||||
}
|
||||
|
||||
public override bool IsCompleted() => false;
|
||||
public override bool CanBeCompleted => true;
|
||||
|
||||
public override bool IsLoop { get => true; set => throw new System.Exception("Trying to set the value for IsLoop from: " + System.Environment.StackTrace); }
|
||||
|
||||
public override bool IsDuplicate(AIObjective otherObjective)
|
||||
{
|
||||
return otherObjective is AIObjectiveExtinguishFires;
|
||||
}
|
||||
|
||||
protected override void Act(float deltaTime)
|
||||
{
|
||||
SyncRemovedObjectives(extinguishObjectives, Hull.hullList);
|
||||
if (character.Submarine == null) { return; }
|
||||
foreach (Hull hull in Hull.hullList)
|
||||
{
|
||||
if (hull.FireSources.None()) { continue; }
|
||||
if (hull.Submarine == null) { continue; }
|
||||
if (hull.Submarine.TeamID != character.TeamID) { continue; }
|
||||
// If the character is inside, only take connected hulls into account.
|
||||
if (character.Submarine != null && !character.Submarine.IsEntityFoundOnThisSub(hull, true)) { continue; }
|
||||
if (!extinguishObjectives.TryGetValue(hull, out AIObjectiveExtinguishFire objective))
|
||||
{
|
||||
objective = new AIObjectiveExtinguishFire(character, hull);
|
||||
extinguishObjectives.Add(hull, objective);
|
||||
AddSubObjective(objective);
|
||||
}
|
||||
}
|
||||
if (extinguishObjectives.None())
|
||||
base.FindTargets();
|
||||
if (targets.None())
|
||||
{
|
||||
character?.Speak(TextManager.Get("DialogNoFire"), null, 3.0f, "nofire", 30.0f);
|
||||
}
|
||||
}
|
||||
|
||||
protected override bool Filter(Hull target)
|
||||
{
|
||||
if (target.FireSources.None()) { return false; }
|
||||
if (target.Submarine == null) { return false; }
|
||||
if (target.Submarine.TeamID != character.TeamID) { return false; }
|
||||
if (character.Submarine != null && !character.Submarine.IsEntityFoundOnThisSub(target, true)) { return false; }
|
||||
return true;
|
||||
}
|
||||
|
||||
protected override float TargetEvaluation() => (HumanAIController.ObjectiveManager.CurrentObjective == this || HumanAIController.ObjectiveManager.CurrentOrder == this) ? 100 : targets.Sum(t => GetFireSeverity(t));
|
||||
|
||||
public static float GetFireSeverity(Hull hull) => hull.FireSources.Sum(fs => fs.Size.X);
|
||||
|
||||
public override bool IsDuplicate(AIObjective otherObjective) => otherObjective is AIObjectiveExtinguishFires;
|
||||
protected override IEnumerable<Hull> GetList() => Hull.hullList;
|
||||
|
||||
protected override AIObjective ObjectiveConstructor(Hull target) => new AIObjectiveExtinguishFire(character, target, PriorityModifier);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,9 +39,13 @@ namespace Barotrauma
|
||||
public override float GetPriority(AIObjectiveManager objectiveManager)
|
||||
{
|
||||
if (leak.Open == 0.0f) { return 0.0f; }
|
||||
// Vertical distance matters more than horizontal (climbing up/down is harder than moving horizontally)
|
||||
float dist = Math.Abs(character.WorldPosition.X - leak.WorldPosition.X) + Math.Abs(character.WorldPosition.Y - leak.WorldPosition.Y) * 2.0f;
|
||||
float distanceFactor = MathHelper.Lerp(1, 0.25f, MathUtils.InverseLerp(0, 10000, dist));
|
||||
float severity = AIObjectiveFixLeaks.GetLeakSeverity(leak);
|
||||
float max = MathHelper.Min((AIObjectiveManager.OrderPriority - 1), 90);
|
||||
return MathHelper.Clamp(Priority + severity * PriorityModifier, 0, max);
|
||||
float max = Math.Min((AIObjectiveManager.OrderPriority - 1), 90);
|
||||
float devotion = Math.Min(Priority, 10) / 100;
|
||||
return MathHelper.Lerp(0, max, MathHelper.Clamp(devotion + severity * distanceFactor * PriorityModifier, 0, 1));
|
||||
}
|
||||
|
||||
public override bool IsDuplicate(AIObjective otherObjective)
|
||||
|
||||
@@ -17,29 +17,21 @@ namespace Barotrauma
|
||||
protected override void FindTargets()
|
||||
{
|
||||
base.FindTargets();
|
||||
targets.Sort((x, y) => GetLeakFixPriority(y).CompareTo(GetLeakFixPriority(x)));
|
||||
// Sorting should be handled by the objective manager, because the targets should be subobjectives.
|
||||
//targets.Sort((x, y) => GetLeakFixPriority(y).CompareTo(GetLeakFixPriority(x)));
|
||||
// TODO: Add a dialog when no leaks are found.
|
||||
}
|
||||
|
||||
protected override bool Filter(Gap gap)
|
||||
{
|
||||
bool ignore = ignoreList.Contains(gap) || gap.ConnectedWall == null || gap.ConnectedDoor != null || gap.Open <= 0 || gap.linkedTo.All(l => l == null);
|
||||
bool ignore = gap.ConnectedWall == null || gap.ConnectedDoor != null || gap.Open <= 0 || gap.linkedTo.All(l => l == null);
|
||||
if (!ignore)
|
||||
{
|
||||
if (gap.Submarine == null) { ignore = true; }
|
||||
else if (gap.Submarine.TeamID != character.TeamID) { ignore = true; }
|
||||
else if (character.Submarine != null && !character.Submarine.IsEntityFoundOnThisSub(gap, true)) { ignore = true; }
|
||||
}
|
||||
return ignore;
|
||||
}
|
||||
|
||||
private float GetLeakFixPriority(Gap leak)
|
||||
{
|
||||
if (leak == null) { return 0; }
|
||||
float severity = GetLeakSeverity(leak);
|
||||
// Vertical distance matters more than horizontal (climbing up/down is harder than moving horizontally)
|
||||
float dist = Math.Abs(character.WorldPosition.X - leak.WorldPosition.X) + Math.Abs(character.WorldPosition.Y - leak.WorldPosition.Y) * 2.0f;
|
||||
float distanceFactor = MathHelper.Lerp(1, 0.25f, MathUtils.InverseLerp(0, 10000, dist));
|
||||
return severity * distanceFactor;
|
||||
return !ignore;
|
||||
}
|
||||
|
||||
public static float GetLeakSeverity(Gap leak)
|
||||
@@ -54,6 +46,6 @@ namespace Barotrauma
|
||||
public override bool IsDuplicate(AIObjective otherObjective) => otherObjective is AIObjectiveFixLeaks;
|
||||
protected override float TargetEvaluation() => targets.Max(t => GetLeakSeverity(t));
|
||||
protected override IEnumerable<Gap> GetList() => Gap.GapList;
|
||||
protected override AIObjective ObjectiveConstructor(Gap gap) => new AIObjectiveFixLeak(gap, character);
|
||||
protected override AIObjective ObjectiveConstructor(Gap gap) => new AIObjectiveFixLeak(gap, character, PriorityModifier);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,7 +91,8 @@ namespace Barotrauma
|
||||
{
|
||||
if (character.Submarine == null) { return 0; }
|
||||
if (targets.None()) { return 0; }
|
||||
float targetValue = MathHelper.Clamp(TargetEvaluation(), 0, 100);
|
||||
// Allow the target value to be more than 100.
|
||||
float targetValue = TargetEvaluation();
|
||||
// If the target value is less than 1% of the max value, let's just treat it as zero.
|
||||
if (targetValue < 1) { return 0; }
|
||||
if (objectiveManager.CurrentOrder == this)
|
||||
@@ -100,7 +101,7 @@ namespace Barotrauma
|
||||
}
|
||||
float max = MathHelper.Min(AIObjectiveManager.OrderPriority - 1, 90);
|
||||
float devotion = MathHelper.Min(10, Priority);
|
||||
float value = MathHelper.Min((devotion + targetValue * PriorityModifier) / 100, 1);
|
||||
float value = MathHelper.Clamp((devotion + targetValue * PriorityModifier) / 100, 0, 1);
|
||||
return MathHelper.Lerp(0, max, value);
|
||||
}
|
||||
|
||||
@@ -115,8 +116,8 @@ namespace Barotrauma
|
||||
{
|
||||
foreach (T item in GetList())
|
||||
{
|
||||
if (Filter(item)) { continue; }
|
||||
if (!targets.Contains(item))
|
||||
if (!Filter(item)) { continue; }
|
||||
if (!ignoreList.Contains(item) && !targets.Contains(item))
|
||||
{
|
||||
targets.Add(item);
|
||||
}
|
||||
@@ -141,10 +142,6 @@ namespace Barotrauma
|
||||
/// </summary>
|
||||
protected abstract IEnumerable<T> GetList();
|
||||
|
||||
/// <summary>
|
||||
/// 0 to 100.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected abstract float TargetEvaluation();
|
||||
|
||||
protected abstract AIObjective ObjectiveConstructor(T target);
|
||||
|
||||
+18
-28
@@ -22,36 +22,26 @@ namespace Barotrauma
|
||||
protected override void FindTargets()
|
||||
{
|
||||
if (option == null) { return; }
|
||||
foreach (Item item in Item.ItemList)
|
||||
{
|
||||
if (item.HasTag("ballast")) { continue; }
|
||||
if (item.Submarine == null) { continue; }
|
||||
if (item.Submarine.TeamID != character.TeamID) { continue; }
|
||||
if (character.Submarine != null && !character.Submarine.IsEntityFoundOnThisSub(item, true)) { continue; }
|
||||
var pump = item.GetComponent<Pump>();
|
||||
if (pump != null)
|
||||
{
|
||||
if (!ignoreList.Contains(pump))
|
||||
{
|
||||
if (option == "stoppumping")
|
||||
{
|
||||
if (!pump.IsActive || pump.FlowPercentage == 0.0f) { continue; }
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!pump.Item.InWater) { continue; }
|
||||
if (pump.IsActive && pump.FlowPercentage <= -90.0f) { continue; }
|
||||
}
|
||||
if (!targets.Contains(pump))
|
||||
{
|
||||
targets.Add(pump);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
base.FindTargets();
|
||||
}
|
||||
|
||||
protected override bool Filter(Pump pump) => true;
|
||||
protected override bool Filter(Pump pump)
|
||||
{
|
||||
if (pump.Item.HasTag("ballast")) { return false; }
|
||||
if (pump.Item.Submarine == null) { return false; }
|
||||
if (pump.Item.Submarine.TeamID != character.TeamID) { return false; }
|
||||
if (character.Submarine != null && !character.Submarine.IsEntityFoundOnThisSub(pump.Item, true)) { return false; }
|
||||
if (option == "stoppumping")
|
||||
{
|
||||
if (!pump.IsActive || pump.FlowPercentage == 0.0f) { return false; }
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!pump.Item.InWater) { return false; }
|
||||
if (pump.IsActive && pump.FlowPercentage <= -90.0f) { return false; }
|
||||
}
|
||||
return true;
|
||||
}
|
||||
protected override IEnumerable<Pump> GetList() => pumpList;
|
||||
protected override AIObjective ObjectiveConstructor(Pump pump) => new AIObjectiveOperateItem(pump, character, Option, false) { IsLoop = true };
|
||||
protected override float TargetEvaluation() => targets.Max(t => MathHelper.Lerp(100, 0, t.CurrFlow / t.MaxFlow));
|
||||
|
||||
+2
-2
@@ -36,9 +36,9 @@ namespace Barotrauma
|
||||
float damagePriority = MathHelper.Lerp(1, 0, Item.Condition / Item.MaxCondition);
|
||||
float successFactor = MathHelper.Lerp(0, 1, Item.Repairables.Average(r => r.DegreeOfSuccess(character)));
|
||||
float isSelected = character.SelectedConstruction == Item ? 50 : 0;
|
||||
float devotion = Math.Max(Priority + isSelected, 1);
|
||||
float devotion = (Math.Min(Priority, 10) + isSelected) / 100;
|
||||
float max = MathHelper.Min(AIObjectiveManager.OrderPriority - 1, 90);
|
||||
return MathHelper.Clamp(devotion * damagePriority * distanceFactor * successFactor * PriorityModifier, 0, max);
|
||||
return MathHelper.Lerp(0, max, MathHelper.Clamp(devotion + damagePriority * distanceFactor * successFactor * PriorityModifier, 0, 1));
|
||||
}
|
||||
|
||||
public override bool CanBeCompleted => !abandon;
|
||||
|
||||
+3
-3
@@ -38,7 +38,7 @@ namespace Barotrauma
|
||||
|
||||
protected override bool Filter(Item item)
|
||||
{
|
||||
bool ignore = ignoreList.Contains(item) || item.IsFullCondition;
|
||||
bool ignore = item.IsFullCondition;
|
||||
if (!ignore)
|
||||
{
|
||||
if (item.Submarine == null) { ignore = true; }
|
||||
@@ -64,11 +64,11 @@ namespace Barotrauma
|
||||
}
|
||||
}
|
||||
}
|
||||
return ignore;
|
||||
return !ignore;
|
||||
}
|
||||
|
||||
protected override float TargetEvaluation() => targets.Max(t => 100 - t.ConditionPercentage);
|
||||
protected override IEnumerable<Item> GetList() => Item.ItemList;
|
||||
protected override AIObjective ObjectiveConstructor(Item item) => new AIObjectiveRepairItem(character, item);
|
||||
protected override AIObjective ObjectiveConstructor(Item item) => new AIObjectiveRepairItem(character, item, PriorityModifier);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2714,6 +2714,10 @@ namespace Barotrauma
|
||||
GameMain.GameSession?.CrewManager?.RemoveCharacter(this);
|
||||
#endif
|
||||
|
||||
#if CLIENT
|
||||
GameMain.GameSession?.CrewManager?.RemoveCharacter(this);
|
||||
#endif
|
||||
|
||||
#if CLIENT
|
||||
GameMain.GameSession?.CrewManager?.RemoveCharacter(this);
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user