-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathScanCommand.cs
More file actions
247 lines (228 loc) · 8.74 KB
/
Copy pathScanCommand.cs
File metadata and controls
247 lines (228 loc) · 8.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
using System.Collections.Generic;
using System.Linq;
using ChatCommandAPI;
using GameNetcodeStuff;
using HarmonyLib;
using UnityEngine;
namespace LethalScanCommand;
public class ScanCommand : ServerCommand
{
public override string[] Commands => ["scan", Name];
public override string Description => "Scans for items in- or outside the ship";
public override bool Invoke(
ref PlayerControllerB? caller,
string[] args,
Dictionary<string, string> kwargs,
out string? error
)
{
error = "caller is null";
if (
caller == null
|| !GetString(out var text, out error, LethalScanCommand.CompactResponse)
)
return false;
ChatCommandAPI.ChatCommandAPI.Print(caller, text);
return true;
}
public static bool GetString(out string text, out string? error, bool compact = true)
{
text = string.Empty;
LethalScanCommand.Logger.LogDebug($">> GetString(compact:{compact})");
if (
!Countitems(
out error,
out var inShip,
out var inShipBees,
out var inShipValue,
out var onShip,
out var onShipBees,
out var onShipValue,
out var onCruiser,
out var onCruiserBees,
out var onCruiserValue,
out var outsideShip,
out var outsideShipBees,
out var outsideShipValue,
out var carValueApproximated
)
)
{
LethalScanCommand.Logger.LogDebug($"<< CountItems returned false error:{error}");
return false;
}
if (compact)
{
List<string> lines = [];
if (inShip > 0 || inShipBees > 0)
lines.Add(
$"In ship: {inShip}/{inShipBees} items worth {c(LethalScanCommand.ApproximateValueInShip, inShipValue)}"
);
if (onShip > 0 || onShipBees > 0)
lines.Add(
$"On ship: {onShip}/{onShipBees} items worth {c(LethalScanCommand.ApproximateValueOnShip, onShipValue)}"
);
if (onCruiser > 0 || onCruiserBees > 0)
lines.Add(
$"On cruiser: {onCruiser}/{onCruiserBees} items worth {c(carValueApproximated, onCruiserValue)}"
);
if (!StartOfRound.Instance.inShipPhase)
lines.Add(
$"Outside ship: {outsideShip}/{outsideShipBees} items{(outsideShip > 0 || outsideShipBees > 0 ? $" worth {c(LethalScanCommand.ApproximateValueOutsideShip, outsideShipValue)}" : string.Empty)}"
);
LethalScanCommand.Logger.LogDebug($" lines:{d(lines)}");
text = lines.Count == 0 ? "No items" : lines.Join(null, "\n");
}
else
{
List<string> strings = [];
int first = 0;
if (inShip > 0 || inShipBees > 0)
strings.Add(
$"{a(inShip, inShipBees, ref first, strings.Count == 0)} worth {c(LethalScanCommand.ApproximateValueInShip, inShipValue)} in the ship"
);
if (onShip > 0 || onShipBees > 0)
strings.Add(
$"{a(onShip, onShipBees, ref first, strings.Count == 0)} worth {c(LethalScanCommand.ApproximateValueOnShip, onShipValue)} on the ship"
);
if (onCruiser > 0 || onCruiserBees > 0)
strings.Add(
$"{a(onCruiser, onCruiserBees, ref first, strings.Count == 0)} worth {c(carValueApproximated, onCruiserValue)} on the cruiser"
);
if (!StartOfRound.Instance.inShipPhase)
strings.Add(
$"{a(outsideShip, outsideShipBees, ref first, strings.Count == 0)}{(outsideShip > 0 ? $" worth {c(LethalScanCommand.ApproximateValueOutsideShip, outsideShipValue)}" : string.Empty)} outside the ship"
);
LethalScanCommand.Logger.LogDebug($" strings:{d(strings)}");
text = strings.Count switch
{
0 => "There are no items",
1 => $"There {f(first)} " + strings[0],
_ => $"There {f(first)} "
+ strings.GetRange(0, strings.Count - 1).Join()
+ " and "
+ strings.Last(),
};
}
return true;
string a(int items, int bees, ref int first, bool isFirst)
{
if (isFirst)
first = items > 0 ? items : bees;
return items > 0 ? $"{items} item{e(items)}{b(bees)}" : $"{bees} beehive{e(bees)}";
}
string b(int bees) => bees > 0 ? $" and {bees} beehive{e(bees)}" : string.Empty;
string c(bool approx, int val) => $"{(approx ? "~" : string.Empty)}{val}$";
string d(List<string> list) =>
$"List<string>[{list.Count}]: [{list.Join(i => $"\"{i}\"")}]";
string e(int val) => val == 1 ? string.Empty : "s";
string f(int first) => first == 1 ? "is" : "are";
}
public static bool Countitems(
out string? error,
out int inShip,
out int inShipBees,
out int inShipValue,
out int onShip,
out int onShipBees,
out int onShipValue,
out int onCruiser,
out int onCruiserBees,
out int onCruiserValue,
out int outsideShip,
out int outsideShipBees,
out int outsideShipValue,
out bool carValueApproximated
)
{
inShip = 0;
inShipBees = 0;
inShipValue = 0;
onShip = 0;
onShipBees = 0;
onShipValue = 0;
onCruiser = 0;
onCruiserBees = 0;
onCruiserValue = 0;
outsideShip = 0;
outsideShipBees = 0;
outsideShipValue = 0;
carValueApproximated = false;
error = "items is null";
var items = Object.FindObjectsOfType<GrabbableObject>();
if (items == null)
return false;
error = "ship is null";
var ship = GameObject.Find("Environment/HangarShip");
if (ship == null)
return false;
error = "vehicles is null";
var vehicles = Object.FindObjectsOfType<VehicleController>();
if (vehicles == null)
return false;
carValueApproximated = b(
LethalScanCommand.ApproximateValueOnCruiser,
vehicles.Any(i => i.magnetedToShip)
);
var random = new System.Random(StartOfRound.Instance.randomMapSeed + 91);
for (var i = 0; i < items.Length; i++)
{
var item = items[i];
if (!item.itemProperties.isScrap)
continue;
if (item.isInShipRoom)
{
if (IsBees(item))
inShipBees++;
else
inShip++;
inShipValue += a(LethalScanCommand.ApproximateValueInShip, item, i);
}
else if (item.transform.IsChildOf(ship.transform))
{
if (IsBees(item))
onShipBees++;
else
onShip++;
onShipValue += a(LethalScanCommand.ApproximateValueOnShip, item, i);
}
else if (vehicles.Any(_car => item.transform.IsChildOf(_car.transform)))
{
if (IsBees(item))
onCruiserBees++;
else
onCruiser++;
onCruiserValue += a(carValueApproximated, item, i);
}
else
{
if (IsBees(item))
outsideShipBees++;
else
outsideShip++;
outsideShipValue += a(LethalScanCommand.ApproximateValueOutsideShip, item, i);
}
}
return true;
int a(bool randomize, GrabbableObject item, int i) =>
randomize
? Mathf.Clamp(
random.Next(item.itemProperties.minValue, item.itemProperties.maxValue),
item.scrapValue - 6 * i,
item.scrapValue + 9 * i
)
: item.scrapValue;
bool b(LethalScanCommand.ApproximateValueOnCruiserOptions value, bool carMagnetedToShip) =>
value switch
{
LethalScanCommand.ApproximateValueOnCruiserOptions.Always => true,
LethalScanCommand.ApproximateValueOnCruiserOptions.Never => false,
_ => !carMagnetedToShip,
};
}
private static bool IsBees(GrabbableObject item)
{
LethalScanCommand.Logger.LogDebug($">> IsBees({item}) name:{item.name}");
return item.name == "RedLocustHive(Clone)";
}
}