Client LuaCsForBarotrauma
ShipIssueWorkerOperateWeapons.cs
2 using Microsoft.Xna.Framework;
3 using System;
4 using System.Collections.Generic;
5 using System.Linq;
6 
7 namespace Barotrauma
8 {
10  {
11  public override float RedundantIssueModifier => 0.8f;
12  private readonly List<float> targetingImportances = new List<float>();
13 
14  public override bool AllowEasySwitching => true;
15 
17 
18  float GetTargetingImportance(Entity entity)
19  {
20  float currentDistanceToEnemy = Vector2.Distance(entity.WorldPosition, TargetItem.WorldPosition);
21  if (currentDistanceToEnemy > Sonar.DefaultSonarRange) { return 0.0f; }
22  float importance = MathHelper.Clamp(100 - (currentDistanceToEnemy / 100f), MaxImportance * 0.1f, MaxImportance * 0.5f);
23  if (TargetItem.Submarine != null && importance > 0.0f)
24  {
25  if (TargetItemComponent is Turret turret)
26  {
27  if (!turret.IsWithinAimingRadius(entity.WorldPosition))
28  {
29  importance *= 0.1f;
30  }
31  }
32  else
33  {
34  Vector2 dir = entity.WorldPosition - TargetItem.WorldPosition;
36  if (Vector2.Dot(dir, submarineDir) < 0)
37  {
38  //direction from the weapon to the target is opposite to the direction from the sub to the weapon
39  // = the turret is most likely on the wrong side of the sub, reduce importance
40  importance *= 0.1f;
41  }
42  }
43 
44  }
45  return importance;
46  }
47 
48  public override void CalculateImportanceSpecific()
49  {
50  targetingImportances.Clear();
51  foreach (Character character in shipCommandManager.EnemyCharacters)
52  {
53  targetingImportances.Add(GetTargetingImportance(character));
54  }
55  // there should maybe be additional logic for targeting and destroying spires, because they currently cause some issues with pathing
56  if (targetingImportances.Any(i => i > 0))
57  {
58  targetingImportances.Sort();
59  Importance = Math.Max(targetingImportances.TakeLast(3).Sum(), ShipCommandManager.MinimumIssueThreshold);
60  }
61  if (TargetItemComponent is Turret turret && !turret.HasPowerToShoot())
62  {
63  //operate (= recharge the turrets) with low priority if they're out of power
64  //if something else (issues with reactor or the electrical grid) is preventing them from being charged, fixing those issues should take priority
66  return;
67  }
68  }
69  }
70 }
virtual Vector2 WorldPosition
Definition: Entity.cs:49
Submarine Submarine
Definition: Entity.cs:53
readonly List< Character > EnemyCharacters
ItemComponent TargetItemComponent
readonly ShipCommandManager shipCommandManager
ShipIssueWorkerOperateWeapons(ShipCommandManager shipCommandManager, Order order)