Client LuaCsForBarotrauma
OxygenGenerator.cs
1 using Microsoft.Xna.Framework;
2 using System;
3 using System.Collections.Generic;
4 using System.Linq;
5 
7 {
9  {
10  private float generatedAmount;
11 
12  //key = vent, float = total volume of the hull the vent is in and the hulls connected to it
13  private List<(Vent vent, float hullVolume)> ventList;
14 
15  private float totalHullVolume;
16 
17  private float ventUpdateTimer;
18  const float VentUpdateInterval = 5.0f;
19 
20  public float CurrFlow
21  {
22  get;
23  private set;
24  }
25 
26  [Editable, Serialize(400.0f, IsPropertySaveable.Yes, description: "How much oxygen the machine generates when operating at full power.", alwaysUseInstanceValues: true)]
27  public float GeneratedAmount
28  {
29  get { return generatedAmount; }
30  set { generatedAmount = MathHelper.Clamp(value, -10000.0f, 10000.0f); }
31  }
32 
34  : base(item, element)
35  {
36  //randomize update timer so all oxygen generators don't update at the same time
37  ventUpdateTimer = Rand.Range(0.0f, VentUpdateInterval);
38  IsActive = true;
39  }
40 
41  public override void Update(float deltaTime, Camera cam)
42  {
43  UpdateOnActiveEffects(deltaTime);
44 
45  CurrFlow = 0.0f;
46 
47  if (item.CurrentHull == null) { return; }
48 
49  if (!HasPower && PowerConsumption > 0)
50  {
51  return;
52  }
53 
54  CurrFlow = Math.Min(PowerConsumption > 0 ? Voltage : 1.0f, MaxOverVoltageFactor) * generatedAmount * 100.0f;
55  float conditionMult = item.Condition / item.MaxCondition;
56  //100% condition = 100% oxygen
57  //50% condition = 25% oxygen
58  //20% condition = 4%
59  CurrFlow *= conditionMult * conditionMult;
60 
61  UpdateVents(CurrFlow, deltaTime);
62  }
63 
67  public override float GetCurrentPowerConsumption(Connection connection = null)
68  {
69  if (connection != this.powerIn || !IsActive)
70  {
71  return 0;
72  }
73 
74  float consumption = powerConsumption;
75 
76  //consume more power when in a bad condition
77  item.GetComponent<Repairable>()?.AdjustPowerConsumption(ref consumption);
78  return consumption;
79  }
80 
81  public override void UpdateBroken(float deltaTime, Camera cam)
82  {
83  base.UpdateBroken(deltaTime, cam);
84  CurrFlow = 0.0f;
85  }
86 
87  private void GetVents()
88  {
89  totalHullVolume = 0.0f;
90  ventList ??= new List<(Vent vent, float hullVolume)>();
91  ventList.Clear();
92  foreach (MapEntity entity in item.linkedTo)
93  {
94  if (entity is not Item linkedItem) { continue; }
95 
96  Vent vent = linkedItem.GetComponent<Vent>();
97  if (vent?.Item.CurrentHull == null) { continue; }
98 
99  totalHullVolume += vent.Item.CurrentHull.Volume;
100  ventList.Add((vent, vent.Item.CurrentHull.Volume));
101  }
102 
103  for (int i = 0; i < ventList.Count; i++)
104  {
105  Vent vent = ventList[i].vent;
106  foreach (Hull connectedHull in vent.Item.CurrentHull.GetConnectedHulls(includingThis: false, searchDepth: 3, ignoreClosedGaps: true))
107  {
108  //another vent in the connected hull -> don't add it to this vent's total hull volume
109  if (ventList.Any(v => v.vent != vent && v.vent.Item.CurrentHull == connectedHull)) { continue; }
110  totalHullVolume += connectedHull.Volume;
111  ventList[i] = (ventList[i].vent, ventList[i].hullVolume + connectedHull.Volume);
112  }
113  }
114  }
115 
116  private void UpdateVents(float deltaOxygen, float deltaTime)
117  {
118  if (ventList == null || ventUpdateTimer < 0.0f)
119  {
120  GetVents();
121  ventUpdateTimer = VentUpdateInterval;
122  }
123  ventUpdateTimer -= deltaTime;
124 
125  if (!ventList.Any() || totalHullVolume <= 0.0f) { return; }
126 
127  foreach ((Vent vent, float hullVolume) in ventList)
128  {
129  if (vent.Item.CurrentHull == null) { continue; }
130 
131  vent.OxygenFlow = deltaOxygen * (hullVolume / totalHullVolume);
132  vent.IsActive = true;
133  }
134  }
135 
136  public float GetVentOxygenFlow(Vent targetVent)
137  {
138  if (ventList == null)
139  {
140  GetVents();
141  }
142  foreach ((Vent vent, float hullVolume) in ventList)
143  {
144  if (vent != targetVent) { continue; }
145  return generatedAmount * 100.0f * (hullVolume / totalHullVolume);
146  }
147  return 0.0f;
148  }
149  }
150 }
OxygenGenerator(Item item, ContentXElement element)
override void Update(float deltaTime, Camera cam)
override float GetCurrentPowerConsumption(Connection connection=null)
Power consumption of the Oxygen Generator. Only consume power when active and adjust consumption base...
override void UpdateBroken(float deltaTime, Camera cam)
const float MaxOverVoltageFactor
Maximum voltage factor when the device is being overvolted. I.e. how many times more effectively the ...
float powerConsumption
The maximum amount of power the item can draw from connected items