-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShareTheRoad.cs
More file actions
337 lines (310 loc) · 10.3 KB
/
ShareTheRoad.cs
File metadata and controls
337 lines (310 loc) · 10.3 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
using System;
using Facepunch;
using System.Collections.Generic;
using UnityEngine;
namespace Oxide.Plugins;
[Info("ShareTheRoad", "HunterZ", "1.0.0")]
public class ShareTheRoad : RustPlugin
{
// set of currently-active Bradleys
private static readonly HashSet<NetworkableId> ActiveBradleys = new();
// set of currently-active Vendors
private static readonly HashSet<NetworkableId> ActiveVendors = new();
// set of defunct Bradleys to be removed from ActiveBradleys and/or
// ActiveWatches
private static readonly HashSet<NetworkableId> DeadBradleys = new();
// set of defunct Vendors to be removed from ActiveVendors and/or
// ActiveWatches
private static readonly HashSet<NetworkableId> DeadVendors = new();
// set of defunct Bradley-Vendor pairs to be removed from ActiveWatches
private static readonly HashSet<(NetworkableId, NetworkableId)> DeadWatches
= new();
// Bradley-Vendor pairs that are close to each other
private static readonly HashSet<(NetworkableId, NetworkableId)>
ActiveWatches = new();
// timer used to drive CheckEntities() processing
private static Timer _watchTimer;
#region Plugin API
private void Init()
{
Reset();
Unsubscribe(nameof(OnEntityKill));
Unsubscribe(nameof(OnEntitySpawned));
}
private void OnServerInitialized()
{
foreach (var serverEntity in BaseNetworkable.serverEntities)
{
switch (serverEntity)
{
case BradleyAPC bradley: OnEntitySpawned(bradley); break;
case TravellingVendor vendor: OnEntitySpawned(vendor); break;
}
}
Subscribe(nameof(OnEntitySpawned));
Puts($"OnServerInitialized(): Found {ActiveBradleys.Count} active bradley(s) and {ActiveVendors.Count} vendor(s).");
}
private void Unload()
{
Reset();
foreach (var serverEntity in BaseNetworkable.serverEntities)
{
if (serverEntity is not BradleyAPC bradley) continue;
bradley.CancelInvoke(nameof(STR_TargetScarecrows));
}
}
#endregion Plugin API
#region Hook Handlers
private void OnEntitySpawned(BradleyAPC bradley)
{
if (!bradley || null == bradley.net) return;
ActiveBradleys.Add(bradley.net.ID);
if (1 == ActiveBradleys.Count && 0 == ActiveVendors.Count)
{
Puts("Subscribing to OnEntityKill hook");
Subscribe(nameof(OnEntityKill));
}
ManageTimer();
CH47AIBrain p;
bradley.InvokeRepeating(
() => STR_TargetScarecrows(bradley), 0.0f, bradley.searchFrequency);
}
private void OnEntitySpawned(TravellingVendor vendor)
{
if (!vendor || null == vendor.net) return;
ActiveVendors.Add(vendor.net.ID);
if (1 == ActiveVendors.Count && 0 == ActiveBradleys.Count)
{
Puts("Subscribing to OnEntityKill hook");
Subscribe(nameof(OnEntityKill));
}
ManageTimer();
}
private void OnEntityKill(BradleyAPC bradley)
{
if (!bradley || null == bradley.net) return;
var bradleyID = bradley.net.ID;
ActiveBradleys.Remove(bradleyID);
DeadBradleys.Add(bradleyID);
CheckDeadEntities(false);
if (0 == ActiveBradleys.Count && 0 == ActiveVendors.Count)
{
Puts("Unsubscribing from OnEntityKill hook");
Unsubscribe(nameof(OnEntityKill));
}
ManageTimer();
bradley.CancelInvoke(nameof(STR_TargetScarecrows));
}
private void OnEntityKill(TravellingVendor vendor)
{
if (!vendor || null == vendor.net) return;
var vendorID = vendor.net.ID;
ActiveVendors.Remove(vendorID);
DeadVendors.Add(vendorID);
CheckDeadEntities(false);
if (0 == ActiveBradleys.Count && 0 == ActiveVendors.Count)
{
Puts("Unsubscribing from OnEntityKill hook");
Unsubscribe(nameof(OnEntityKill));
}
ManageTimer();
}
#endregion Hook Handlers
#region Utilities
// check for any data records referencing net IDs in the dead entity sets
// the sets are cleared at the end
private void CheckDeadEntities(bool warn)
{
// bradleys
foreach (var deadBradleyID in DeadBradleys)
{
if (ActiveBradleys.Remove(deadBradleyID) && warn)
{
Puts($"WARNING: Removed defunct BradleyID {deadBradleyID} from active Bradleys set");
}
foreach (var watchPair in ActiveWatches)
{
if (watchPair.Item1 == deadBradleyID)
{
DeadWatches.Add(watchPair);
}
}
}
// vendors
foreach (var deadVendorID in DeadVendors)
{
if (ActiveVendors.Remove(deadVendorID) && warn)
{
Puts($"WARNING: Removed defunct VendorID {deadVendorID} from active Vendors set");
}
foreach (var watchPair in ActiveWatches)
{
if (watchPair.Item2 == deadVendorID)
{
DeadWatches.Add(watchPair);
}
}
}
// pairs
foreach (var deadWatchPair in DeadWatches)
{
if (ActiveWatches.Remove(deadWatchPair) && warn)
{
Puts($"WARNING: Removed defunct watch pair {deadWatchPair} from watch set");
}
}
DeadBradleys.Clear();
DeadVendors.Clear();
DeadWatches.Clear();
}
private void CheckEntities()
{
DeadBradleys.Clear();
DeadVendors.Clear();
DeadWatches.Clear();
// check each Bradley-Vendor pair to see if they're too close
foreach (var bradleyID in ActiveBradleys)
{
var bradley =
BaseNetworkable.serverEntities.Find(bradleyID) as BradleyAPC;
if (!bradley)
{
DeadBradleys.Add(bradleyID);
continue;
}
foreach (var vendorID in ActiveVendors)
{
var vendor =
BaseNetworkable.serverEntities.Find(vendorID) as TravellingVendor;
if (!vendor)
{
DeadVendors.Add(vendorID);
continue;
}
// we now have a valid Bradley-Vendor entity pair
var watchPair = (bradleyID, vendorID);
// check whether they're currently too close
var distance = Vector3.Distance(
bradley.transform.position, vendor.transform.position);
var close = distance < 10.0f;
// check whether this pair was previously too close
var watching = ActiveWatches.Remove(watchPair);
// not close - do nothing (already removed from watch set)
if (!close)
{
if (watching)
{
Puts($"Stopped watching Bradley-Vendor pair {watchPair} near grid {GetGrid(bradley.transform.position)} due to distance={distance}");
}
// else
// {
// Puts($"Not watching Bradley-Vendor pair {watchPair} near grid {GetGrid(bradley.transform.position)} due to distance={distance}");
// }
continue;
}
if (watching)
{
// close for 2 consecutive checks; swap positions
(bradley.transform.position, vendor.transform.position) =
(vendor.transform.position, bradley.transform.position);
Puts($"Swapped Bradley-Vendor pair {watchPair} positions near grid {GetGrid(bradley.transform.position)} due to distance={distance}");
// also ensure they don't fall under the terrain
FixEntityPosition(bradley);
FixEntityPosition(vendor);
// NOTE: already removed from watch set, which is what we want
// because otherwise they might erroneously keep swapping
continue;
}
// just became close; add to watch set
ActiveWatches.Add(watchPair);
Puts($"Started watching Bradley-Vendor pair {watchPair} near grid {GetGrid(bradley.transform.position)} due to distance={distance}");
}
// handle any dead entities, with warnings since there shouldn't be any
CheckDeadEntities(true);
}
}
// ensure entity is at or above terrain height (hopefully avoids falling
// through the world on swap
private void FixEntityPosition(BaseEntity entity)
{
if (!entity || null == entity.net) return;
var groundY = TerrainMeta.HeightMap.GetHeight(entity.transform.position);
if (entity.transform.position.y >= groundY) return;
Puts($"Moving entity {entity} near grid {GetGrid(entity.transform.position)} to terrain height={groundY} from current height={entity.transform.position.y}");
entity.transform.position.Set(
entity.transform.position.x, groundY, entity.transform.position.z);
}
private void StartWatching() =>
_watchTimer ??= timer.Every(10.0f, CheckEntities);
private void StopWatching()
{
if (null == _watchTimer) return;
_watchTimer.Destroy();
_watchTimer = null;
ActiveWatches.Clear();
}
// start or stop timer processing based on whether there is at least one
// Bradley and one Vendor active
private void ManageTimer()
{
var currentTimerState = null != _watchTimer;
var idealTimerState =
ActiveBradleys.Count > 0 && ActiveVendors.Count > 0;
// abort if timer is already in ideal state
if (currentTimerState == idealTimerState) return;
// not in ideal state; start or stop timer
if (idealTimerState)
{
Puts("Starting proximity check timer");
StartWatching();
}
else
{
Puts("Stopping proximity check timer");
StopWatching();
}
}
private static string GetGrid(Vector3 pos) =>
MapHelper.PositionToString(pos);
/*
private static void ResetBrain(ScarecrowNPC scarecrow, bool movementTick)
{
if (null == scarecrow) return;
scarecrow.StopAttacking();
var brain = scarecrow.Brain;
if (null == brain) return;
if (!movementTick) brain.StopMovementTick();
var defaultStateContainer = brain.AIDesign?.GetDefaultStateContainer();
if (null != defaultStateContainer)
{
brain.SwitchToState(
defaultStateContainer.State, defaultStateContainer.ID);
}
if (movementTick) brain.StartMovementTick();
}
*/
private void Reset()
{
StopWatching();
ActiveBradleys.Clear();
ActiveVendors.Clear();
DeadBradleys.Clear();
DeadVendors.Clear();
DeadWatches.Clear();
ActiveWatches.Clear();
}
private static void STR_TargetScarecrows(BradleyAPC bradley)
{
// find all scarecrows in search range
var list = Pool.Get<List<ScarecrowNPC>>();
Vis.Entities(
bradley.transform.position, bradley.searchRange, list, 133120 /*0x020800*/);
foreach (var scarecrow in list)
{
// method will protect from duplicates
bradley.AddOrUpdateTarget(scarecrow, scarecrow.transform.position);
}
Pool.FreeUnmanaged(ref list);
}
#endregion Utilities
}