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