Client LuaCsForBarotrauma
SmokeDetector.cs
1 using System;
2 
4 {
6  {
7  const float FireCheckInterval = 1.0f;
8  private float fireCheckTimer;
9 
10  public bool FireInRange { get; private set; }
11 
12  private int maxOutputLength;
13  [Editable, Serialize(200, IsPropertySaveable.No, description: "The maximum length of the output strings. Warning: Large values can lead to large memory usage or networking issues.")]
14  public int MaxOutputLength
15  {
16  get { return maxOutputLength; }
17  set
18  {
19  maxOutputLength = Math.Max(value, 0);
20  }
21  }
22 
23  private string output;
24  [InGameEditable, Serialize("1", IsPropertySaveable.Yes, description: "The signal the item outputs when it has detected a fire.", alwaysUseInstanceValues: true)]
25  public string Output
26  {
27  get { return output; }
28  set
29  {
30  if (value == null) { return; }
31  output = value;
32  if (output.Length > MaxOutputLength && (item.Submarine == null || !item.Submarine.Loading))
33  {
34  output = output.Substring(0, MaxOutputLength);
35  }
36  }
37  }
38 
39  private string falseOutput;
40  [InGameEditable, Serialize("0", IsPropertySaveable.Yes, description: "The signal the item outputs when it has not detected a fire.", alwaysUseInstanceValues: true)]
41  public string FalseOutput
42  {
43  get { return falseOutput; }
44  set
45  {
46  if (value == null) { return; }
47  falseOutput = value;
48  if (falseOutput.Length > MaxOutputLength && (item.Submarine == null || !item.Submarine.Loading))
49  {
50  falseOutput = falseOutput.Substring(0, MaxOutputLength);
51  }
52  }
53  }
54 
56  : base(item, element)
57  {
58  IsActive = true;
59  }
60 
61  private bool IsFireInRange()
62  {
63  if (item.CurrentHull == null || item.InWater) { return false; }
64 
65  var connectedHulls = item.CurrentHull.GetConnectedHulls(includingThis: true, searchDepth: 10, ignoreClosedGaps: true);
66  foreach (Hull hull in connectedHulls)
67  {
68  foreach (FireSource fireSource in hull.FireSources)
69  {
70  if (fireSource.IsInDamageRange(item.WorldPosition, Math.Max(fireSource.DamageRange * 2.0f, 500.0f))) { return true; }
71  }
72  }
73 
74  return false;
75  }
76 
77  public override void Update(float deltaTime, Camera cam)
78  {
79  fireCheckTimer -= deltaTime;
80  if (fireCheckTimer <= 0.0f)
81  {
82  FireInRange = IsFireInRange();
83  fireCheckTimer = FireCheckInterval;
84  }
85  string signalOut = FireInRange ? Output : FalseOutput;
86  if (!string.IsNullOrEmpty(signalOut)) { item.SendSignal(signalOut, "signal_out"); }
87  }
88  }
89 }
Submarine Submarine
Definition: Entity.cs:53
bool IsInDamageRange(Character c, float damageRange)
IEnumerable< Hull > GetConnectedHulls(bool includingThis, int? searchDepth=null, bool ignoreClosedGaps=false)
void SendSignal(string signal, string connectionName)
The base class for components holding the different functionalities of the item
SmokeDetector(Item item, ContentXElement element)
override void Update(float deltaTime, Camera cam)