-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathComponent.cs
More file actions
338 lines (286 loc) · 12.2 KB
/
Copy pathComponent.cs
File metadata and controls
338 lines (286 loc) · 12.2 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
338
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Timers;
using AIlimit;
using BepInEx.Logging;
using Comfort.Common;
using dvize.AILimit;
using EFT;
using UnityEngine;
namespace AILimit
{
public class AILimitComponent : MonoBehaviour
{
internal static float botDistance;
private static int botCount;
private static GameWorld gameWorld;
private int frameCounter = 0;
private List<botPlayer> disabledBotsLastFrame = new List<botPlayer>();
private static Dictionary<int, PlayerInfo> playerInfoMapping = new Dictionary<int, PlayerInfo>();
private static List<botPlayer> botList = new List<botPlayer>();
private botPlayer bot;
private Player player;
private static BotSpawner botSpawnerClass;
protected static ManualLogSource Logger
{
get; private set;
}
public AILimitComponent()
{
if (Logger == null)
{
Logger = BepInEx.Logging.Logger.CreateLogSource(nameof(AILimitComponent));
}
}
private void Start()
{
botSpawnerClass.OnBotCreated += OnPlayerAdded;
botSpawnerClass.OnBotRemoved += OnPlayerRemoved;
SetupBotDistanceForMap();
//reset static vars to work with new raid
playerInfoMapping = new Dictionary<int, PlayerInfo>
{
};
botList = new List<botPlayer>
{
};
Logger.LogDebug("Setup Bot Distance for Map: " + botDistance);
}
public static void Enable()
{
if (Singleton<IBotGame>.Instantiated)
{
gameWorld = Singleton<GameWorld>.Instance;
gameWorld.GetOrAddComponent<AILimitComponent>();
//botspawner is wrong class. bots being enabled here will limit bots spawned.
botSpawnerClass = (Singleton<IBotGame>.Instance).BotsController.BotSpawner;
Logger.LogDebug("AILimit Enabled");
}
}
private void OnEnable()
{
// Map distance changes all handled by the same method
ConfigManager.OnFactoryDistanceChanged += newValue => SettingsHandler.HandleMapDistanceChange("factory", newValue);
ConfigManager.OnGroundZeroDistanceChanged += newValue => SettingsHandler.HandleMapDistanceChange("groundzero", newValue);
ConfigManager.OnInterchangeDistanceChanged += newValue => SettingsHandler.HandleMapDistanceChange("interchange", newValue);
ConfigManager.OnLaboratoryDistanceChanged += newValue => SettingsHandler.HandleMapDistanceChange("laboratory", newValue);
ConfigManager.OnLighthouseDistanceChanged += newValue => SettingsHandler.HandleMapDistanceChange("lighthouse", newValue);
ConfigManager.OnReserveDistanceChanged += newValue => SettingsHandler.HandleMapDistanceChange("reserve", newValue);
ConfigManager.OnShorelineDistanceChanged += newValue => SettingsHandler.HandleMapDistanceChange("shoreline", newValue);
ConfigManager.OnWoodsDistanceChanged += newValue => SettingsHandler.HandleMapDistanceChange("woods", newValue);
ConfigManager.OnCustomsDistanceChanged += newValue => SettingsHandler.HandleMapDistanceChange("customs", newValue);
ConfigManager.OnTarkovStreetsDistanceChanged += newValue => SettingsHandler.HandleMapDistanceChange("tarkovstreets", newValue);
}
private void SetupBotDistanceForMap()
{
string location = gameWorld.MainPlayer.Location.ToLower();
Logger.LogDebug($"The location detected is: {location}");
switch (location)
{
case "factory4_day":
case "factory4_night":
botDistance = AILimitPlugin.factoryDistance.Value;
break;
case "bigmap":
botDistance = AILimitPlugin.customsDistance.Value;
break;
case "sandbox":
botDistance = AILimitPlugin.groundZeroDistance.Value;
break;
case "interchange":
botDistance = AILimitPlugin.interchangeDistance.Value;
break;
case "rezervbase":
botDistance = AILimitPlugin.reserveDistance.Value;
break;
case "laboratory":
botDistance = AILimitPlugin.laboratoryDistance.Value;
break;
case "lighthouse":
botDistance = AILimitPlugin.lighthouseDistance.Value;
break;
case "shoreline":
botDistance = AILimitPlugin.shorelineDistance.Value;
break;
case "woods":
botDistance = AILimitPlugin.woodsDistance.Value;
break;
case "tarkovstreets":
botDistance = AILimitPlugin.tarkovstreetsDistance.Value;
break;
default:
botDistance = 200.0f;
break;
}
}
private void OnPlayerAdded(BotOwner botOwner)
{
if (!botOwner.GetPlayer.IsYourPlayer)
{
player = botOwner.GetPlayer;
Logger.LogDebug("In OnPlayerAdded Method: " + player.gameObject.name);
if (!playerInfoMapping.ContainsKey(player.Id))
{
var playerInfo = new PlayerInfo
{
Player = player,
Bot = new botPlayer(player.Id)
};
playerInfoMapping.Add(player.Id, playerInfo);
// Add bot to the botList immediately
botList.Add(playerInfo.Bot);
Logger.LogDebug("Added: " + player.Profile.Info.Settings.Role + " - " + player.Profile.Nickname + " to botList");
bot = playerInfo.Bot;
bot.Distance = Vector3.SqrMagnitude(player.Position - gameWorld.MainPlayer.Position);
if (!bot.timer.Enabled && player.CameraPosition != null)
{
bot.timer.Enabled = true;
bot.timer.Start();
}
}
}
}
private void OnPlayerRemoved(BotOwner botOwner)
{
player = botOwner.GetPlayer;
if (playerInfoMapping.ContainsKey(player.Id))
{
var playerInfo = playerInfoMapping[player.Id];
if (botList.Contains(playerInfo.Bot))
{
botList.Remove(playerInfo.Bot);
}
if (disabledBotsLastFrame.Contains(playerInfo.Bot))
{
disabledBotsLastFrame.Remove(playerInfo.Bot);
}
playerInfoMapping.Remove(player.Id);
}
}
private void Update()
{
if (AILimitPlugin.PluginEnabled.Value)
{
frameCounter++;
if (frameCounter >= AILimitPlugin.FramesToCheck.Value)
{
UpdateBots();
frameCounter = 0; // Reset the frame counter
}
else
{
UpdateBotsWithDisabledList();
}
}
}
private void UpdateBots()
{
botCount = 0;
disabledBotsLastFrame.Clear();
botList.Sort((a, b) => a.Distance.CompareTo(b.Distance));
foreach (var bot in botList)
{
player = playerInfoMapping[bot.Id].Player;
if (player == null || !player.HealthController.IsAlive)
{
continue;
}
bot.Distance = Vector3.SqrMagnitude(player.Position - gameWorld.MainPlayer.Position);
if (botCount < AILimitPlugin.BotLimit.Value &&
bot.Distance < botDistance * botDistance &&
bot.eligibleNow)
{
player.gameObject.SetActive(true);
botCount++;
}
else if (bot.eligibleNow && !disabledBotsLastFrame.Contains(bot))
{
// Clear AI decision queue so they don't do anything when they are disabled.
player.AIData.BotOwner.DecisionQueue.Clear();
player.AIData.BotOwner.Memory.GoalEnemy = null;
player.gameObject.SetActive(false);
disabledBotsLastFrame.Add(bot);
}
}
}
private void UpdateBotsWithDisabledList()
{
foreach (var bot in disabledBotsLastFrame)
{
player = playerInfoMapping[bot.Id].Player;
if (player == null || !player.HealthController.IsAlive)
{
continue;
}
if (bot.eligibleNow)
{
player.AIData.BotOwner.DecisionQueue.Clear();
player.AIData.BotOwner.Memory.GoalEnemy = null;
player.gameObject.SetActive(false);
}
}
}
private static async Task<ElapsedEventHandler> EligiblePool(botPlayer botplayer)
{
//async while loop with await until bot actually in game
while (playerInfoMapping[botplayer.Id].Player.CameraPosition == null)
{
await Task.Delay(1000);
}
botplayer.timer.Stop();
botplayer.eligibleNow = true;
Logger.LogDebug("Bot # " + playerInfoMapping[botplayer.Id].Player.gameObject.name + " is now eligible.");
return null;
}
private void OnDisable()
{
// Unsubscribe from map distance changes
ConfigManager.OnFactoryDistanceChanged -= newValue => SettingsHandler.HandleMapDistanceChange("factory", newValue);
ConfigManager.OnGroundZeroDistanceChanged -= newValue => SettingsHandler.HandleMapDistanceChange("groundzero", newValue);
ConfigManager.OnInterchangeDistanceChanged -= newValue => SettingsHandler.HandleMapDistanceChange("interchange", newValue);
ConfigManager.OnLaboratoryDistanceChanged -= newValue => SettingsHandler.HandleMapDistanceChange("laboratory", newValue);
ConfigManager.OnLighthouseDistanceChanged -= newValue => SettingsHandler.HandleMapDistanceChange("lighthouse", newValue);
ConfigManager.OnReserveDistanceChanged -= newValue => SettingsHandler.HandleMapDistanceChange("reserve", newValue);
ConfigManager.OnShorelineDistanceChanged -= newValue => SettingsHandler.HandleMapDistanceChange("shoreline", newValue);
ConfigManager.OnWoodsDistanceChanged -= newValue => SettingsHandler.HandleMapDistanceChange("woods", newValue);
ConfigManager.OnCustomsDistanceChanged -= newValue => SettingsHandler.HandleMapDistanceChange("customs", newValue);
ConfigManager.OnTarkovStreetsDistanceChanged -= newValue => SettingsHandler.HandleMapDistanceChange("tarkovstreets", newValue);
}
private class PlayerInfo
{
public Player Player
{
get; set;
}
public botPlayer Bot
{
get; set;
}
}
private class botPlayer
{
public int Id
{
get; set;
}
public float Distance
{
get; set;
}
public bool eligibleNow
{
get; set;
}
public Timer timer;
public botPlayer(int newID)
{
Id = newID;
eligibleNow = false;
timer = new Timer(AILimitPlugin.TimeAfterSpawn.Value * 1000);
timer.Enabled = false;
timer.AutoReset = false;
timer.Elapsed += (sender, e) => EligiblePool(this);
}
}
}
}