Client LuaCsForBarotrauma
DefendWithPumpState.cs
2 using System.Collections.Generic;
3 using System.Linq;
4 
6 {
8  {
9  private readonly BallastFloraBranch targetBranch;
10  private readonly List<Pump> allAvailablePumps = new List<Pump>();
11  private readonly List<Door> allAvailableDoors = new List<Door>();
12  private readonly List<Pump> targetPumps = new List<Pump>();
13  private readonly List<Door> jammedDoors = new List<Door>();
14 
15  private bool isFinished;
16  private float timer = 10f;
17  private bool filled;
18  private bool tryDrown;
19  private readonly Character attacker;
20 
21  public DefendWithPumpState(BallastFloraBranch branch, IEnumerable<Item> items, Character attacker)
22  {
23  targetBranch = branch;
24  this.attacker = attacker;
25 
26  foreach (Item item in items)
27  {
28  if (item.GetComponent<Pump>() is { } pump)
29  {
30  allAvailablePumps.Add(pump);
31  }
32 
33  if (item.GetComponent<Door>() is { } door)
34  {
35  allAvailableDoors.Add(door);
36  }
37  }
38  }
39 
41  {
42  if (isFinished) { return ExitState.ReturnLast; }
43  if (targetBranch.CurrentHull == null) { return ExitState.ReturnLast; }
44  return timer < 0 ? ExitState.ReturnLast : ExitState.Running;
45  }
46 
47  public void Enter()
48  {
49  foreach (Pump pump in allAvailablePumps)
50  {
51  if (pump.Item.CurrentHull == targetBranch.CurrentHull)
52  {
53  targetPumps.Add(pump);
54  SetPump(pump);
55  pump.Hijacked = true;
56  }
57  }
58 
59  if (!targetPumps.Any() || targetPumps.All(p => !p.HasPower))
60  {
61  isFinished = true;
62  }
63 
64  // lock the doors if the attacker is in the same hull as the ballast flora to try to drown them
65  if (targetBranch.CurrentHull != null && attacker != null && attacker.CurrentHull == targetBranch.CurrentHull)
66  {
67  foreach (Door door in allAvailableDoors)
68  {
69  if (door.LinkedGap != null && door.LinkedGap.linkedTo.Contains(targetBranch.CurrentHull))
70  {
71  door.TrySetState(false, false, true);
72  door.IsJammed = true;
73  jammedDoors.Add(door);
74  }
75  }
76 
77  tryDrown = true;
78  }
79  }
80 
81  public void Exit()
82  {
83  foreach (Pump pump in targetPumps)
84  {
85  pump.Hijacked = false;
86  }
87 
88  foreach (Door door in jammedDoors)
89  {
90  door.IsJammed = false;
91  }
92  }
93 
94  private void SetPump(Pump pump)
95  {
96  if (pump.TargetLevel != null)
97  {
98  pump.TargetLevel = 100f;
99  }
100  else
101  {
102  pump.FlowPercentage = 100f;
103  }
104  }
105 
106  public void Update(float deltaTime)
107  {
108  foreach (Pump pump in targetPumps)
109  {
110  SetPump(pump);
111  }
112 
113  if (tryDrown && !filled)
114  {
115  // keep the ballast filled for extra 10 seconds
116  if (targetBranch.CurrentHull == null || targetBranch.CurrentHull.WaterPercentage >= 95f)
117  {
118  filled = true;
119  timer += 10f;
120  }
121  }
122 
123  timer -= deltaTime;
124  }
125  }
126 }
void TrySetState(bool open, bool isNetworkMessage, bool sendNetworkMessage=false)
DefendWithPumpState(BallastFloraBranch branch, IEnumerable< Item > items, Character attacker)