-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKnife_Patch.cs
262 lines (237 loc) · 11.2 KB
/
Knife_Patch.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
using HarmonyLib;
using Nautilus.Handlers;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UWE;
using static ErrorMessage;
namespace Tweaks_Fixes
{
internal class Knife_Patch
{
public static bool giveResourceOnDamage;
static float knifeRangeDefault = 0f;
static float knifeDamageDefault = 0f;
static ParticleSystem[] heatBladeParticles;
[HarmonyPatch(typeof(PlayerTool))]
public class PlayerTool_Patch
{
[HarmonyPostfix]
[HarmonyPatch("OnDraw")]
public static void OnDrawPostfix(PlayerTool __instance)
{
//TechType tt = CraftData.GetTechType(__instance.gameObject);
Knife knife = __instance as Knife;
if (knife)
{
if (knifeRangeDefault == 0f)
knifeRangeDefault = knife.attackDist;
if (knifeDamageDefault == 0f)
knifeDamageDefault = knife.damage;
knife.attackDist = knifeRangeDefault * ConfigMenu.knifeRangeMult.Value;
knife.damage = knifeDamageDefault * ConfigMenu.knifeDamageMult.Value;
//AddDebug(" attackDist " + knife.attackDist);
//AddDebug(" damage " + knife.damage);
}
}
}
[HarmonyPatch(typeof(Knife))]
class Knife_Patch_
{
[HarmonyPrefix]
[HarmonyPatch("OnToolUseAnim")]
public static bool OnToolUseAnimPrefix(Knife __instance, GUIHand hand)
{
Vector3 position = new Vector3();
GameObject closestObj = null;
UWE.Utils.TraceFPSTargetPosition(Player.main.gameObject, __instance.attackDist, ref closestObj, ref position);
//if (closestObj)
//{
//AddDebug("OnToolUseAnim closestObj " + closestObj.name);
//AddDebug("OnToolUseAnim closestObj parent " + closestObj.transform.parent.name);
//AddDebug("OnToolUseAnim closestObj parent parent " + closestObj.transform.parent.parent.name);
//}
//else
// AddDebug("OnToolUseAnim closestObj null");
if (closestObj == null)
{
InteractionVolumeUser ivu = Player.main.gameObject.GetComponent<InteractionVolumeUser>();
if (ivu != null && ivu.GetMostRecent() != null)
{
closestObj = ivu.GetMostRecent().gameObject;
//AddDebug("OnToolUseAnim GetMostRecent " + closestObj.name);
}
}
if (closestObj)
{
Utils.PlayFMODAsset(__instance.attackSound, __instance.transform);
VFXSurface vfxSurface = closestObj.GetComponentInParent<VFXSurface>();
//if (vfxSurface)
// AddDebug("OnToolUseAnim vfxSurface " + vfxSurface.surfaceType);
//else
// AddDebug("OnToolUseAnim no vfxSurface ");
Vector3 euler = MainCameraControl.main.transform.eulerAngles + new Vector3(300f, 90f, 0f);
ParticleSystem particleSystem = VFXSurfaceTypeManager.main.Play(vfxSurface, __instance.vfxEventType, position, Quaternion.Euler(euler), Player.main.transform);
LiveMixin liveMixin = closestObj.GetComponentInParent<LiveMixin>();
bool validTarget = liveMixin == null || Knife.IsValidTarget(liveMixin);
//AddDebug("OnToolUseAnim IsValidTarget " + validTarget);
if (validTarget)
{
if (liveMixin)
{
bool wasAlive = liveMixin.IsAlive();
liveMixin.TakeDamage(__instance.damage, position, __instance.damageType, Player.main.gameObject);
__instance.GiveResourceOnDamage(closestObj, liveMixin.IsAlive(), wasAlive);
}
}
else
closestObj = null;
}
if (closestObj || hand.GetActiveTarget())
return false;
if (Player.main.IsUnderwater())
Utils.PlayFMODAsset(__instance.underwaterMissSound, __instance.transform);
else
Utils.PlayFMODAsset(__instance.surfaceMissSound, __instance.transform);
return false;
}
[HarmonyPostfix]
[HarmonyPatch("OnToolUseAnim")]
public static void OnToolUseAnimPostfix(Knife __instance)
{
if (!Player.main.guiHand.activeTarget)
return;
BreakableResource breakableResource = Player.main.guiHand.activeTarget.GetComponent<BreakableResource>();
if (breakableResource)
{
breakableResource.BreakIntoResources();
//AddDebug("BreakableResource");
}
Pickupable pickupable = Player.main.guiHand.activeTarget.GetComponent<Pickupable>();
if (pickupable)
{
TechType techType = pickupable.GetTechType();
if (PickupablePatch.notPickupableResources.Contains(techType))
{
Rigidbody rb = pickupable.GetComponent<Rigidbody>();
if (rb && rb.isKinematic) // attached to wall
pickupable.OnHandClick(Player.main.guiHand);
}
}
}
[HarmonyPrefix]
[HarmonyPatch("GiveResourceOnDamage")]
public static void GiveResourceOnDamagePrefix(Knife __instance, GameObject target, bool isAlive, bool wasAlive)
{
giveResourceOnDamage = true;
}
//[HarmonyPostfix]
//[HarmonyPatch("GiveResourceOnDamage")]
public static void GiveResourceOnDamageMy(Knife __instance, GameObject target, bool isAlive, bool wasAlive)
{
if (isAlive || wasAlive)
return;
//TechType techType = CraftData.GetTechType(target);
//string name = techType.AsString();
//if (Main.config.deadCreatureLoot.ContainsKey(name))
//{
// Creature creature = target.GetComponent<Creature>();
// if (creature == null)
// return;
// if (deadCreatureLoot.ContainsKey(creature))
// {
// foreach (var pair in Main.config.deadCreatureLoot[name])
// {
// TechType loot = pair.Key;
// int max = pair.Value;
// if (deadCreatureLoot[creature].ContainsKey(loot) && deadCreatureLoot[creature][loot] < max)
// {
// CraftData.AddToInventory(loot);
// deadCreatureLoot[creature][loot]++;
// }
// }
// }
// else
// {
// foreach (var pair in Main.config.deadCreatureLoot[name])
// {
// CraftData.AddToInventory(pair.Key);
// deadCreatureLoot.Add(creature, new Dictionary<TechType, int> { { pair.Key, 1 } });
// }
// }
//}
}
}
public static void AddToInventoryOrSpawn(TechType techType, int num)
{
Vector3 spawnPos = default;
for (int i = 0; i < num; ++i)
{
if (!ConfigToEdit.alwaysSpawnWhenKnifeHarvesting.Value && Inventory.main.HasRoomFor(techType))
CraftData.AddToInventory(techType);
else
{ // spawn position from AddToInventory can be behind object
if (!ConfigToEdit.alwaysSpawnWhenKnifeHarvesting.Value)
AddError(Language.main.Get("InventoryFull"));
if (spawnPos == default)
{
Transform camTr = MainCamera.camera.transform;
float dist = knifeRangeDefault * ConfigMenu.knifeRangeMult.Value;
RaycastHit hitIData = default;
Physics.Raycast(camTr.position, camTr.forward, out hitIData, dist);
if (hitIData.point != default)
spawnPos = Vector3.LerpUnclamped(hitIData.point, camTr.position, .2f);
else
spawnPos = camTr.position;
}
CoroutineHost.StartCoroutine(Util.Spawn(techType, spawnPos));
}
}
}
[HarmonyPatch(typeof(CraftData), "AddToInventory")]
class CraftData_AddToInventory_Patch
{
static void Prefix(CraftData __instance, TechType techType, ref int num, bool noMessage, bool spawnIfCantAdd)
{
//AddDebug("AddToInventory Prefix " + techType + " " + num + " spawnIfCantAdd " + spawnIfCantAdd + " giveResourceOnDamage " + giveResourceOnDamage);
if (giveResourceOnDamage && !spawnIfCantAdd)
{
AddToInventoryOrSpawn(techType, num);
giveResourceOnDamage = false;
num = 0;
}
}
}
[HarmonyPatch(typeof(VFXLateTimeParticles))]
public class VFXLateTimeParticles_Patch
{
[HarmonyPostfix]
[HarmonyPatch("Play")]
public static void PlayPostfix(VFXLateTimeParticles __instance)
{
if (__instance.name != "xHeatBlade_Bubbles(Clone)")
return;
//AddDebug("VFXLateTimeParticles Play");
heatBladeParticles = __instance.psChildren;
FixHeatBlade();
}
}
public static void FixHeatBlade()
{ // fix heatblade underwater particles
if (heatBladeParticles == null || heatBladeParticles.Length != 3 || heatBladeParticles[0] == null || heatBladeParticles[0].gameObject == null || !heatBladeParticles[0].gameObject.activeInHierarchy)
return;
//AddDebug("FixHeatBlade");
bool underwater = Player.main.isUnderwater.value;
heatBladeParticles[1].EnableEmission(!underwater); // xSmk
heatBladeParticles[0].EnableEmission(underwater); // xHeatBlade_Bubbles(Clone)
heatBladeParticles[2].EnableEmission(underwater); // xRefract
}
public static void OnPlayerUnderwaterChanged(Utils.MonitoredValue<bool> isUnderwaterForSwimming)
{
//AddDebug(" OnPlayerUnderwaterChanged " + Player.main.IsUnderwater());
FixHeatBlade();
}
}
}