/
AnimalTurret.cs
executable file
·364 lines (322 loc) · 13.4 KB
/
AnimalTurret.cs
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#region License (GPL v2)
/*
AnimalTurret - Make Rust autoturrets target animals (again)
Copyright (c) 2021-2023 RFC1920 <desolationoutpostpve@gmail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License v2.0.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License (GPL v2)
using Newtonsoft.Json;
using Oxide.Core;
using Oxide.Core.Libraries.Covalence;
using Oxide.Core.Plugins;
using System.Collections.Generic;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("AnimalTurret", "RFC1920", "1.0.8")]
[Description("Make (npc)autoturrets target animals in range")]
internal class AnimalTurret : RustPlugin
{
private ConfigData configData;
private List<uint> disabledTurrets = new List<uint>();
public static AnimalTurret Instance;
private bool enabled;
[PluginReference]
private readonly Plugin Friends, Clans, RustIO;
#region Message
private string Lang(string key, string id = null, params object[] args) => string.Format(lang.GetMessage(key, this, id), args);
private void Message(IPlayer player, string key, params object[] args) => player.Message(Lang(key, player.Id, args));
protected override void LoadDefaultMessages()
{
lang.RegisterMessages(new Dictionary<string, string>
{
["enabled"] = "Animal targeting enabled for this autoturret",
["disabled"] = "Animal targeting disabled for this autoturret"
}, this);
}
#endregion
#region Commands
[ChatCommand("noat")]
private void cmdDisableTurret(BasePlayer player, string command, string[] args)
{
List<AutoTurret> foundTurrets = new List<AutoTurret>();
Vis.Entities(player.transform.position, 3f, foundTurrets);
foreach (AutoTurret t in foundTurrets)
{
if (!disabledTurrets.Contains((uint)t.net.ID.Value))
{
if (player.userID == t.OwnerID || IsFriend(player.userID, t.OwnerID))
{
disabledTurrets.Add((uint)t.net.ID.Value);
AnimalTarget at = t.gameObject.GetComponent<AnimalTarget>();
if (at != null) UnityEngine.Object.Destroy(at);
SaveData();
Message(player.IPlayer, "disabled");
}
}
}
}
[ChatCommand("doat")]
private void cmdEnableTurret(BasePlayer player, string command, string[] args)
{
List<AutoTurret> foundTurrets = new List<AutoTurret>();
Vis.Entities(player.transform.position, 3f, foundTurrets);
foreach(AutoTurret t in foundTurrets)
{
if (disabledTurrets.Contains((uint)t.net.ID.Value))
{
if (player.userID == t.OwnerID || IsFriend(player.userID, t.OwnerID))
{
disabledTurrets.Remove((uint)t.net.ID.Value);
t.gameObject.AddComponent<AnimalTarget>();
SaveData();
Message(player.IPlayer, "enabled");
}
}
}
}
private bool IsFriend(ulong playerid, ulong ownerid)
{
if (!configData.HonorRelationships) return false;
if (configData.useFriends && Friends != null)
{
object fr = Friends?.CallHook("AreFriends", playerid, ownerid);
if (fr != null && (bool)fr)
{
return true;
}
}
if (configData.useClans && Clans != null)
{
string playerclan = (string)Clans?.CallHook("GetClanOf", playerid);
string ownerclan = (string)Clans?.CallHook("GetClanOf", ownerid);
if (playerclan != null && ownerclan != null)
{
if (playerclan == ownerclan)
{
return true;
}
}
}
if (configData.useTeams)
{
RelationshipManager.PlayerTeam playerTeam = RelationshipManager.ServerInstance.FindPlayersTeam(playerid);
if (playerTeam?.members.Contains(ownerid) == true)
{
return true;
}
}
return false;
}
#endregion
private void OnServerInitialized()
{
Instance = this;
LoadConfigVariables();
LoadData();
enabled = true;
List<uint> processed = new List<uint>();
if (configData.npcTurrets)
{
foreach (NPCAutoTurret t in UnityEngine.Object.FindObjectsOfType<NPCAutoTurret>())
{
if (!processed.Contains((uint)t.net.ID.Value))
{
if (configData.debug) Puts($"Arming NPC turret {t.net.ID} for animal targeting.");
t.gameObject.AddComponent<AnimalTargetNPC>();
processed.Add((uint)t.net.ID.Value);
}
}
}
foreach (AutoTurret t in UnityEngine.Object.FindObjectsOfType<AutoTurret>())
{
if (processed.Contains((uint)t.net.ID.Value)) continue;
if (disabledTurrets.Contains((uint)t.net.ID.Value)) continue;
if (configData.defaultEnabled && !processed.Contains((uint)t.net.ID.Value))
{
if (configData.debug) Puts($"Arming turret {t.net.ID} for animal targeting.");
t.gameObject.AddComponent<AnimalTarget>();
processed.Add((uint)t.net.ID.Value);
}
}
}
private void Unload()
{
foreach (AutoTurret t in UnityEngine.Object.FindObjectsOfType<AutoTurret>())
{
if (t != null)
{
AnimalTarget at = t.gameObject.GetComponent<AnimalTarget>();
if (at != null) UnityEngine.Object.Destroy(at);
}
}
foreach (NPCAutoTurret t in UnityEngine.Object.FindObjectsOfType<NPCAutoTurret>())
{
if (t != null)
{
AnimalTargetNPC at = t.gameObject.GetComponent<AnimalTargetNPC>();
if (at != null) UnityEngine.Object.Destroy(at);
}
}
}
private void OnEntitySpawned(NPCAutoTurret turret)
{
if (!enabled) return;
if (configData.npcTurrets)
{
if (turret == null) return;
turret.gameObject.AddComponent<AnimalTargetNPC>();
}
}
private void OnEntitySpawned(AutoTurret turret)
{
if (!enabled) return;
if (turret.OwnerID == 0) return;
BasePlayer player = BasePlayer.Find(turret.OwnerID.ToString());
if (configData.defaultEnabled)
{
turret.gameObject.AddComponent<AnimalTarget>();
Message(player.IPlayer, "enabled");
}
else
{
Message(player.IPlayer, "disabled");
}
}
private void LoadData()
{
disabledTurrets = Interface.GetMod().DataFileSystem.ReadObject<List<uint>>(Name + "/disabledTurrets");
}
private void SaveData()
{
Interface.GetMod().DataFileSystem.WriteObject(Name + "/disabledTurrets", disabledTurrets);
}
private class AnimalTargetNPC : MonoBehaviour
{
private NPCAutoTurret turret;
private void Awake()
{
turret = GetComponent<NPCAutoTurret>();
//turret.SetFlag(BaseEntity.Flags.Reserved1, true); // If false they will attack players regardless of hostility.
float period = Instance.configData.scanPeriod > 0 ? Instance.configData.scanPeriod : 5f;
if (turret != null) InvokeRepeating("FindTargets", 5f, period);
}
internal void FindTargets()
{
if (Instance.disabledTurrets.Contains((uint)turret.net.ID.Value)) return;
if (turret.target == null)
{
List<BaseAnimalNPC> localpig = new List<BaseAnimalNPC>();
Vis.Entities(turret.eyePos.transform.position, 30f, localpig);
foreach (BaseAnimalNPC bce in localpig)
{
if (string.IsNullOrEmpty(bce.ShortPrefabName)) continue;
if (turret.ObjectVisible(bce) && !Instance.configData.exclusions.Contains(bce.ShortPrefabName))
{
if (Instance.configData.debug) Instance.Puts($"NPCAutoturret {turret.net.ID} targeting {bce.ShortPrefabName}({bce.net.ID})");
turret.target = bce;
bce.MarkHostileFor();
break;
}
}
}
}
}
private class AnimalTarget : MonoBehaviour
{
private AutoTurret turret;
private void Awake()
{
turret = GetComponent<AutoTurret>();
float period = Instance.configData.scanPeriod > 0 ? Instance.configData.scanPeriod : 5f;
if (turret != null) InvokeRepeating("FindTargets", 5f, period);
}
internal void FindTargets()
{
if (Instance.disabledTurrets.Contains((uint)turret.net.ID.Value)) return;
if (turret.target == null && turret.IsPowered())
{
List<BaseAnimalNPC> localpig = new List<BaseAnimalNPC>();
Vis.Entities(turret.eyePos.transform.position, 30f, localpig);
foreach (BaseAnimalNPC bce in localpig)
{
if (string.IsNullOrEmpty(bce.ShortPrefabName)) continue;
if (turret.ObjectVisible(bce) && !Instance.configData.exclusions.Contains(bce.ShortPrefabName))
{
if (Instance.configData.debug) Instance.Puts($"Autoturret {turret.net.ID} targeting {bce.ShortPrefabName}({bce.net.ID})");
turret.target = bce;
if (!turret.PeacekeeperMode())
{
bce.MarkHostileFor();
}
break;
}
}
}
}
}
#region config
private void LoadConfigVariables()
{
configData = Config.ReadObject<ConfigData>();
if (configData.scanPeriod == 0)
{
configData.scanPeriod = 5f;
}
configData.Version = Version;
SaveConfig(configData);
}
protected override void LoadDefaultConfig()
{
Puts("Creating new config file.");
ConfigData config = new ConfigData
{
defaultEnabled = true,
npcTurrets = false,
HonorRelationships = false,
useFriends = false,
useClans = false,
useTeams = false,
exclusions = new List<string>() { "chicken" },
scanPeriod = 5f,
Version = Version
};
SaveConfig(config);
}
private void SaveConfig(ConfigData config)
{
Config.WriteObject(config, true);
}
private class ConfigData
{
[JsonProperty(PropertyName = "Animal targeting enabled by default")]
public bool defaultEnabled;
[JsonProperty(PropertyName = "Animal targeting by NPC AutoTurrets")]
public bool npcTurrets;
[JsonProperty(PropertyName = "Animals to exclude")]
public List<string> exclusions;
[JsonProperty(PropertyName = "Honor Friends/Clans/Teams for commands")]
public bool HonorRelationships;
[JsonProperty(PropertyName = "Use Friends plugins for commands")]
public bool useFriends;
[JsonProperty(PropertyName = "Use Clans plugins for commands")]
public bool useClans;
[JsonProperty(PropertyName = "Use Rust teams for commands")]
public bool useTeams;
[JsonProperty(PropertyName = "Update period for turrets")]
public float scanPeriod;
public bool debug;
public VersionNumber Version;
}
#endregion
}
}