-
Notifications
You must be signed in to change notification settings - Fork 1
/
Plugin.cs
310 lines (271 loc) · 13.2 KB
/
Plugin.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
using BepInEx;
using HarmonyLib;
using Unity.Netcode;
using System.Reflection;
using UnityEngine;
using BepInEx.Configuration;
using System.Collections.Generic;
using System.IO;
using Unity.Netcode.Components;
using GameNetcodeStuff;
namespace RoombaMines
{
[BepInPlugin(PluginInfo.PLUGIN_GUID, PluginInfo.PLUGIN_NAME, PluginInfo.PLUGIN_VERSION)]
[BepInProcess("Lethal Company.exe")]
public class Plugin : BaseUnityPlugin
{
public static AssetBundle ModAssets;
// config options
public static ConfigEntry<float> roombaMoveSpeed;
public static ConfigEntry<float> roombaTurnSpeed;
public static ConfigEntry<int> roombaUpdateTickLength;
public static ConfigEntry<bool> roombaAllowRotateLeft;
public static ConfigEntry<float> mineBecomesRoombaChance;
public static List<string> roombaNames;
public static string roombaNameFilePath = UnityEngine.Application.persistentDataPath + "/roomba_names.txt";
private void Awake()
{
// Plugin startup logic
Logger.LogInfo($"Plugin {PluginInfo.PLUGIN_GUID} is loaded!");
Logger.LogInfo($"Embedded resources found:");
foreach (string s in GetType().Assembly.GetManifestResourceNames())
{
Logger.LogInfo($"{s}");
}
// load config
roombaMoveSpeed = Config.Bind("Behavior",
"RoombaMoveSpeed",
0.5f,
"Speed of the Roomba when moving forward (measured in m/s)\nNote: Roomba behavior can only be affected by the host. These settings do nothing for the clients.");
roombaTurnSpeed = Config.Bind("Behavior",
"RoombaTurnSpeed",
50f,
"How quickly the Roomba will rotate when it's hit a wall (measured in degrees/s)");
roombaUpdateTickLength = Config.Bind("Behavior",
"RoombaUpdateTickLength",
30,
"Roombas check for obstructions at the beginning of every tick, this determines how long those ticks last (measured in 1/60ths of a second)");
roombaAllowRotateLeft = Config.Bind("Behavior",
"RoombaAllowRotateLeft",
false,
"If true, Roombas will turn left 50% of the time, otherwise they will always turn right");
mineBecomesRoombaChance = Config.Bind("Behavior",
"MineBecomesRoombaChance",
1.0f,
"The probability a mine will behave like a Roomba after it's spawned. A value of 1.0 will result in every mine becoming a Roomba, a value of 0.0 will effectively disable the mod, 0.5 will result in half normal mines, etc.");
// load or create roomba name file
if (!File.Exists(roombaNameFilePath))
{
StreamWriter writeStream = new StreamWriter(roombaNameFilePath);
writeStream.Write("John\nGeorge\nPaul\nRingo\nHenry\nWilliam\nJoshua\nSam\nFred\nVinny\nRoss\nJoey");
writeStream.Close();
}
roombaNames = new List<string>(File.ReadAllText(roombaNameFilePath).Split("\n"));
// for UnityNetCodePatcher
NetcodePatcher();
// patch
Harmony.CreateAndPatchAll(typeof(NetworkObjectManager));
}
private static void NetcodePatcher()
{
var types = Assembly.GetExecutingAssembly().GetTypes();
foreach (var type in types)
{
var methods = type.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
foreach (var method in methods)
{
var attributes = method.GetCustomAttributes(typeof(RuntimeInitializeOnLoadMethodAttribute), false);
if (attributes.Length > 0)
{
method.Invoke(null, null);
}
}
}
}
public class NetworkObjectManager
{
public static GameObject roombaPrefab;
public static int mineIndex = 0;
// load Roomba prefab
[HarmonyPatch(typeof(GameNetworkManager), "Start")]
[HarmonyPostfix]
public static void Init()
{
// allow adding prefabs at runtime
NetworkManager.Singleton.NetworkConfig.ForceSamePrefabs = false;
}
// overwrite landmine prefab in current level
[HarmonyPatch(typeof(RoundManager), "Start")]
[HarmonyPostfix]
public static void MinePrefabPatch()
{
if (roombaPrefab == null)
{
// get a landmine prefab from the RoundManager
for (mineIndex = 0; mineIndex < RoundManager.Instance.spawnableMapObjects.Length; mineIndex++)
{
if (RoundManager.Instance.spawnableMapObjects[mineIndex].prefabToSpawn.GetComponentInChildren<Landmine>())
{
roombaPrefab = RoundManager.Instance.spawnableMapObjects[mineIndex].prefabToSpawn;
break;
}
}
// add relevant components to prefab
roombaPrefab.AddComponent<NetworkTransform>();
roombaPrefab.AddComponent<Roomba>();
// unregister old landmine prefab
NetworkManager.Singleton.RemoveNetworkPrefab(RoundManager.Instance.spawnableMapObjects[mineIndex].prefabToSpawn);
// register modified landmine as new network prefab
NetworkManager.Singleton.AddNetworkPrefab(roombaPrefab);
}
// replace landmine prefab in current level with new one
RoundManager.Instance.spawnableMapObjects[mineIndex].prefabToSpawn = roombaPrefab;
}
}
public class Roomba : NetworkBehaviour
{
public enum MovementState
{
Idle,
Forward,
RotateRight,
RotateLeft
}
public MovementState state;
public Landmine mine;
public string scanName;
private int _tick_timer = 0;
private int _tick_length = roombaUpdateTickLength.Value;
private float _fixed_tick_time = 0;
private float _move_rate = roombaMoveSpeed.Value;
private float _rotate_rate = roombaTurnSpeed.Value;
private readonly float _scale = 0.55f;
private readonly float _scale_vert = 0.2f;
private int _mask = StartOfRound.Instance.allPlayersCollideWithMask;
void Awake()
{
// check if mine is above ground (outside) or whether this mine should behave normally
if (transform.position.y > -50 || Random.Range(0f, 1f) > mineBecomesRoombaChance.Value)
{
// disable roomba behavior
enabled = false;
return;
}
// if the roomba is clipped in a wall, move away from it
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit, _scale * 1.42f, _mask & ~(1 << 21))
|| Physics.Raycast(transform.position, -transform.forward, out hit, _scale * 1.42f, _mask & ~(1 << 21))
|| Physics.Raycast(transform.position, transform.right, out hit, _scale * 1.42f, _mask & ~(1 << 21))
|| Physics.Raycast(transform.position, -transform.right, out hit, _scale * 1.42f, _mask & ~(1 << 21)))
{
Vector3 new_pos = hit.point + hit.normal * _scale * 2;
transform.position = new_pos;
}
}
void Start()
{
state = MovementState.RotateRight;
UnityEngine.Debug.Log("Roomba at position: " + transform.position);
UnityEngine.Debug.Log("Player at position: " + StartOfRound.Instance.localPlayerController.transform.position);
_fixed_tick_time = Time.fixedDeltaTime * _tick_length;
}
public override void OnNetworkSpawn()
{
base.OnNetworkSpawn();
mine = gameObject.GetComponentInChildren<Landmine>();
// increase height of the mesh slightly
mine.transform.localScale = new UnityEngine.Vector3(1, 1, 1.1f);
// set roomba name if host
if (NetworkManager.Singleton.IsHost || NetworkManager.Singleton.IsServer)
{
SetNameServerRPC(roombaNames[UnityEngine.Random.Range(0, roombaNames.Count)]);
}
}
[ServerRpc(RequireOwnership = false)]
public void SetNameServerRPC(string newName)
{
if (!(NetworkManager.Singleton.IsHost || NetworkManager.Singleton.IsServer))
{
return;
}
UnityEngine.Debug.Log("Host: Set Roomba name: " + newName);
SetNameClientRPC(newName);
}
[ClientRpc]
public void SetNameClientRPC(string newName)
{
UnityEngine.Debug.Log("Client: Set Roomba name: " + newName);
// set scannode to show new name
scanName = newName;
mine.transform.parent.GetComponentInChildren<ScanNodeProperties>().headerText = scanName;
}
// tick for Roomba movement
void Update()
{
if (!mine.enabled)
{
Destroy(gameObject);
return;
}
// return if not host or mine is inactive
if (!(NetworkManager.Singleton.IsHost || NetworkManager.Singleton.IsServer) || mine.hasExploded)
{
return;
}
if (state == MovementState.Forward)
{
transform.position += transform.forward * Time.deltaTime * _move_rate;
}
else if (state == MovementState.RotateRight)
{
transform.Rotate(transform.up, Time.deltaTime * _rotate_rate);
}
else if (state == MovementState.RotateLeft)
{
transform.Rotate(-transform.up, Time.deltaTime * _rotate_rate);
}
}
// physics tick for Roomba
void FixedUpdate()
{
// return if not host or mine is inactive
if (!(NetworkManager.Singleton.IsHost || NetworkManager.Singleton.IsServer) || mine.hasExploded)
{
return;
}
_tick_timer += 1;
if (_tick_timer >= _tick_length)
{
_tick_timer = 0;
// if mine spawns in the air somehow, put it on the nearest surface below it
if (!Physics.Raycast(transform.position, -transform.up, 0.1f, _mask, QueryTriggerInteraction.Ignore))
{
RaycastHit hit;
if (Physics.Raycast(transform.position, -transform.up, out hit, 5f, _mask, QueryTriggerInteraction.Ignore))
{
transform.position = hit.point;
UnityEngine.Debug.Log("Moving mine to Y coord " + transform.position.y);
}
}
bool forward_clear = !Physics.CheckBox(transform.position + transform.forward * (_scale + _fixed_tick_time * _move_rate / 2) + transform.up * 0.01f, new UnityEngine.Vector3(_scale, 0.005f, _fixed_tick_time * _move_rate / 2), transform.rotation, _mask, QueryTriggerInteraction.Ignore);
bool grounded = Physics.Raycast(transform.position + transform.forward * (_scale + _fixed_tick_time * _move_rate), -transform.up, 0.1f, _mask, QueryTriggerInteraction.Ignore);
if (forward_clear && grounded)
{
state = MovementState.Forward;
}
else if (state == MovementState.Forward)
{
if (!roombaAllowRotateLeft.Value || UnityEngine.Random.Range(0, 2) == 0)
{
state = MovementState.RotateRight;
}
else
{
state = MovementState.RotateLeft;
}
}
}
}
}
}
}