-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBeastsOfBurden.cs
527 lines (476 loc) · 20 KB
/
BeastsOfBurden.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
using BepInEx;
using BepInEx.Logging;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;
namespace BeastsOfBurden
{
[BepInPlugin(pluginGUID, pluginName, pluginVersion)]
public class BeastsOfBurden : BaseUnityPlugin
{
const string pluginGUID = "org.bepinex.plugins.beasts_of_burden";
const string pluginName = "BeastsOfBurden";
public const string pluginVersion = "1.0.4";
public static ManualLogSource logger;
private readonly Harmony harmony = new Harmony(pluginGUID);
static private ConfigEntry<bool> commandWolf;
static private ConfigEntry<bool> commandBoar;
static private ConfigEntry<bool> commandLox;
static private ConfigEntry<bool> attachToWolf;
static private ConfigEntry<bool> attachToBoar;
static private ConfigEntry<bool> attachToLox;
static private ConfigEntry<bool> attachToOtherTamed;
static private ConfigEntry<float> detachDistanceFactor;
static private ConfigEntry<float> detachDistancePlayer;
static private ConfigEntry<float> followDistanceLox;
static private ConfigEntry<float> followDistanceMediumAnimal;
static private ConfigEntry<bool> preventTamedFearOfFire;
void Awake()
{
logger = Logger;
Logger.LogInfo($"Loading Beasts of Burden ");
commandWolf = Config.Bind(pluginName,
nameof(commandWolf),
true,
"Makes Wolf Commandable (as it is in the normal game)");
commandBoar = Config.Bind(pluginName,
nameof(commandBoar),
true,
"Makes Boar Commandable");
commandLox = Config.Bind(pluginName,
nameof(commandLox),
true,
"Makes Lox Commandable");
attachToWolf = Config.Bind(pluginName,
nameof(attachToWolf),
true,
"Allow cart to attach to Wolf");
attachToBoar = Config.Bind(pluginName,
nameof(attachToBoar),
true,
"Allow cart to attach to Boar");
attachToLox = Config.Bind(pluginName,
nameof(attachToLox),
true,
"Allow cart to attach to Lox");
attachToOtherTamed = Config.Bind(pluginName,
nameof(attachToOtherTamed),
true,
"Experimental: Allow cart to attach to other types of tamed animals. ");
detachDistancePlayer = Config.Bind(pluginName,
nameof(detachDistancePlayer),
2f,
new ConfigDescription("How far the player has to be from the cart.",
new AcceptableValueRange<float>(1f, 5f)));
detachDistanceFactor = Config.Bind(pluginName,
nameof(detachDistanceFactor),
3.5f,
new ConfigDescription("How far something has to be from the cart to use it a multiple of their radius",
new AcceptableValueRange<float>(1f, 5f)));
followDistanceLox = Config.Bind(pluginName, nameof(followDistanceLox),
8f, new ConfigDescription("How close the lox will follow behind the player.",
new AcceptableValueRange<float>(1f, 30f)));
followDistanceMediumAnimal = Config.Bind(pluginName, nameof(followDistanceMediumAnimal),
3f, new ConfigDescription("How close medium animals (wolf and boar) will follow behind the player.",
new AcceptableValueRange<float>(1f, 30f)));
preventTamedFearOfFire = Config.Bind<bool>(pluginName, nameof(preventTamedFearOfFire),
false, new ConfigDescription("Prevent tamed animals from being afraid of fire."));
harmony.PatchAll();
}
void OnDestroy()
{
harmony.UnpatchSelf();
}
/// <summary>
/// An enum describing what is getting attached to the cart
/// </summary>
public enum Beasts
{
lox,
wolf,
boar,
player,
other
}
/// <summary>
/// Parses a character into a Beasts
/// </summary>
/// <param name="c"></param>
/// <returns></returns>
public static Beasts ParseCharacterType(Character c)
{
if (c.IsPlayer())
{
return Beasts.player;
}
if (c.m_nview.IsValid())
{
switch (ZNetScene.instance.GetPrefab(c.m_nview.GetZDO().GetPrefab()).name)
{
case "Lox":
return Beasts.lox;
case "Wolf":
return Beasts.wolf;
case "Boar":
return Beasts.boar;
default:
return Beasts.other;
}
}
else
{
logger.LogDebug($"Character has invalid m_nview.");
return Beasts.other;
}
}
/// <summary>
/// Different sized characters require different attachment offsets for the cart.
/// This will return the appropriate offset.
/// </summary>
/// <param name="c">to be attached to the cart</param>
/// <returns>vector of where the cart should attach</returns>
public static Vector3 GetCartOffsetVectorForCharacter(Character c)
{
if (c)
{
return new Vector3(0f, 0.8f, 0f - c.GetRadius());
}
return new Vector3(0f, 0.8f, 0f);
}
/// <summary>
/// Allows the types of animals attached to to be configurable
/// </summary>
/// <param name="c"></param>
/// <returns>if cart can be attached to character type</returns>
public static bool IsAttachableCharacter(Character c)
{
switch (ParseCharacterType(c))
{
case Beasts.lox:
return attachToLox.Value;
case Beasts.wolf:
return attachToWolf.Value;
case Beasts.boar:
return attachToBoar.Value;
case Beasts.player:
return true;
default:
return attachToOtherTamed.Value;
}
}
/// <summary>
/// Different character types should be different distances to the cart for it to attach.
/// </summary>
/// <param name="c"></param>
/// <returns>the appropriate attach/detach distance for the provided character</returns>
public static float GetCartDetachDistance(Character c)
{
if (c)
{
if (c.IsPlayer())
{
return detachDistancePlayer.Value;
}
else
{
return c.GetRadius() * detachDistanceFactor.Value;
}
}
logger.LogError("Character pass was null");
return 0f;
}
/// <summary>
/// Searches nearby animals and finds the closest one to the cart that could be attached.
/// </summary>
/// <param name="cart"></param>
/// <returns>Closest character to the cart that can attach to it, null if no character available</returns>
static Character FindClosestAttachableAnimal(Vagon cart)
{
if (!cart)
{
logger.LogError("Cart pointer is null");
return null;
}
Transform attachPoint = cart.m_attachPoint;
Character closest_animal = null;
float closest_distance = float.MaxValue;
if (!cart.m_attachPoint)
{
logger.LogError("cart.m_attachPoint is null.");
return null;
}
foreach (Character currentCharacter in Character.GetAllCharacters())
{
if(currentCharacter)
{
if (!currentCharacter.IsPlayer() && currentCharacter.IsTamed() && IsAttachableCharacter(currentCharacter))
{
Vector3 cartOffset = GetCartOffsetVectorForCharacter(currentCharacter);
Vector3 animalPosition = currentCharacter.transform.position;
float distance = Vector3.Distance(animalPosition + cartOffset, attachPoint.position);
float detachDistance = GetCartDetachDistance(currentCharacter);
if (distance < detachDistance && distance < closest_distance)
{
closest_animal = currentCharacter;
closest_distance = distance;
}
}
}
else
{
logger.LogWarning("null character returned by Character.GetAllCharacter() in method FindClosestTamedAnimal");
}
}
if (closest_animal != null)
{
logger.LogDebug($"Closest animal is {closest_animal.m_name} at a distance of {closest_distance}");
}
return closest_animal;
}
/// <summary>
/// Helper method to access the character currently attached to a cart
/// </summary>
/// <param name="cart"></param>
/// <returns>Character currently attached</returns>
static Character AttachedCharacter(Vagon cart)
{
if(cart && cart.IsAttached())
{
return cart.m_attachJoin.connectedBody.gameObject.GetComponent<Character>();
}
return null;
}
/// <summary>
/// Logs the contents of a given cart to the debug logger.
/// Used during debugging to easily differentiate between carts.
/// </summary>
/// <param name="cart"></param>
static void LogCartContents(Vagon cart)
{
Container c = cart.m_container;
logger.LogDebug($"Cart contents:");
foreach (ItemDrop.ItemData item in c.GetInventory().GetAllItems())
{
logger.LogDebug($"\t * {item.m_shared.m_name}");
}
}
/// <summary>
/// This method is similar to Vagon.AttachTo except we don't call DetachAll as the first operation.
/// </summary>
/// <param name="attachTarget"></param>
/// <param name="cart"></param>
static void AttachCartTo(Character attachTarget, Vagon cart)
{
cart.m_attachOffset = GetCartOffsetVectorForCharacter(attachTarget);
cart.m_attachJoin = cart.gameObject.AddComponent<ConfigurableJoint>();
((Joint)cart.m_attachJoin).autoConfigureConnectedAnchor = false;
((Joint)cart.m_attachJoin).anchor = cart.m_attachPoint.localPosition;
((Joint)cart.m_attachJoin).connectedAnchor = cart.m_attachOffset;
((Joint)cart.m_attachJoin).breakForce = cart.m_breakForce;
cart.m_attachJoin.xMotion = ((ConfigurableJointMotion)1);
cart.m_attachJoin.yMotion = ((ConfigurableJointMotion)1);
cart.m_attachJoin.zMotion = ((ConfigurableJointMotion)1);
SoftJointLimit linearLimit = default(SoftJointLimit);
linearLimit.limit = 0.001f;
cart.m_attachJoin.linearLimit = linearLimit;
SoftJointLimitSpring linearLimitSpring = default(SoftJointLimitSpring);
linearLimitSpring.spring = cart.m_spring;
linearLimitSpring.damper = cart.m_springDamping;
cart.m_attachJoin.linearLimitSpring = linearLimitSpring;
cart.m_attachJoin.zMotion = ((ConfigurableJointMotion)0);
cart.m_attachJoin.connectedBody = (attachTarget.gameObject.GetComponent<Rigidbody>());
}
/// <summary>
/// Patch for Vagon.LateUpdate that handles a situation where the attached animal is killed.
/// </summary>
[HarmonyPatch(typeof(Vagon), "LateUpdate")]
class LateUpdate_Vagon_Patch
{
static void Prefix(ref Vagon __instance, ref ConfigurableJoint ___m_attachJoin, ref Rigidbody ___m_body)
{
if (___m_attachJoin != null && ___m_attachJoin.connectedBody == null)
{
__instance.Detach();
}
}
}
/// <summary>
/// Patch overriding InUse that will correctly return false if an animal is the one attached to a cart
/// </summary>
[HarmonyPatch(typeof(Vagon), nameof(Vagon.InUse))]
class InUse_Vagon_Patch
{
static bool Prefix(ref bool __result, ref Vagon __instance)
{
if ((bool)__instance.m_container && __instance.m_container.IsInUse())
{
__result = true;
}
else if (__instance.IsAttached())
{
__result = (bool)__instance.m_attachJoin.connectedBody.gameObject.GetComponent<Player>();
}
else
{
__result = false;
}
return false;
}
}
/// <summary>
/// Patch to FixedUpdate that will attempt to attach cart to animal if there is an appropriate one nearby.
/// </summary>
[HarmonyPatch(typeof(Vagon), "FixedUpdate")]
class Vagon_FixedUpdate_Patch
{
static bool Prefix(Vagon __instance)
{
if (!__instance.m_nview.IsValid())
{
logger.LogDebug("m_nview invalid");
return false;
}
// Attempt to attach the cart
__instance.UpdateAudio(Time.fixedDeltaTime);
if (__instance.m_nview.IsOwner())
{
if ((bool)__instance.m_useRequester)
{
if (__instance.IsAttached())
{
// If attached detach
__instance.Detach();
}
else
{
/// Determine if there is a valid animal in range and if so attempt to attach to it.
/// If not attempt to attach to player
Character closest_tamed = FindClosestAttachableAnimal(__instance);
if (closest_tamed != null)
{
AttachCartTo(closest_tamed, __instance);
}
else if (__instance.CanAttach(__instance.m_useRequester.gameObject))
{
AttachCartTo(__instance.m_useRequester, __instance);
}
else
{
__instance.m_useRequester.Message(MessageHud.MessageType.Center, "Not in the right position");
}
}
__instance.m_useRequester = null;
}
if (__instance.IsAttached()){
// Update detach distance before check if it should be detached
__instance.m_detachDistance = GetCartDetachDistance( AttachedCharacter(__instance) );
if (!__instance.CanAttach(((Component)(object)((Joint)__instance.m_attachJoin).connectedBody).gameObject))
{
__instance.Detach();
logger.LogDebug("Cart no longer attachable.");
}
}
}
else if (__instance.IsAttached())
{
__instance.Detach();
}
return false;
}
}
/// <summary>
/// Changes to BaseAI UpdateAI patch.
///
/// Some of these could potentially be moved into a BaseAI.Awake but for
/// now I'm putting them here so that if you change the value while playing
/// it'll be automatically updated.
/// </summary>
[HarmonyPatch(typeof(BaseAI), nameof(BaseAI.UpdateAI))]
class BaseAI_UpdateAI_patch
{
static bool Prefix(float dt, ref BaseAI __instance)
{
if (__instance.m_character.IsTamed() && preventTamedFearOfFire.Value)
{
__instance.m_afraidOfFire = false;
}
return true;
}
}
/// <summary>
/// Patch for follow logic that allows for a greater follow distance.
/// This is necessary because the lox tries to follow the player so closely that it constantly pushes the player
/// Future use could include randomizing follow distance so multiple cart pulling animals are less likely to collide.
/// </summary>
[HarmonyPatch(typeof(BaseAI), nameof(BaseAI.Follow))]
class Tamed_Follow_patch
{
static bool Prefix(GameObject go, float dt, ref BaseAI __instance)
{
/// Allow normal follow code to run if character isn't tamed
if (!__instance.m_character.IsTamed())
{
return true;
}
// If the character isn't following a player allow the normal follow code to run
if ((__instance as MonsterAI).GetFollowTarget().GetComponent<Player>() == null)
{
return true;
}
float distance = Vector3.Distance(go.transform.position, __instance.transform.position);
float followDistance;
switch (ParseCharacterType(__instance.m_character))
{
case Beasts.lox:
followDistance = followDistanceLox.Value;
break;
case Beasts.wolf:
case Beasts.boar:
followDistance = followDistanceMediumAnimal.Value;
break;
default:
// Kick it back to the original method for unknown creature types
return true;
}
bool run = distance > followDistance * 3;
if (distance < followDistance)
{
__instance.StopMoving();
}
else
{
__instance.MoveTo(dt, go.transform.position, 0f, run);
}
return false;
}
}
/// <summary>
/// Patch that allows this mod to specify which tamed animals are commandable.
/// </summary>
[HarmonyPatch(typeof(Tameable), nameof(Tameable.Interact))]
class Command_Patch
{
/// <summary>
/// Sets m_commandable to true for Tameable animals allowing commands to be issued to Boar, Wolf, and Lox
/// </summary>
/// <param name="___m_commandable"></param>
static void Prefix(ref bool ___m_commandable, ref Character ___m_character)
{
switch (ParseCharacterType(___m_character))
{
case Beasts.lox:
___m_commandable = commandLox.Value;
return;
case Beasts.wolf:
___m_commandable = commandWolf.Value;
return;
case Beasts.boar:
___m_commandable = commandBoar.Value;
return;
default:
return;
}
}
}
}
}