-
Notifications
You must be signed in to change notification settings - Fork 1
/
Food_Patch.cs
398 lines (368 loc) · 17.2 KB
/
Food_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
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
using HarmonyLib;
using System;
using System.Collections.Generic;
using System.Collections;
using UnityEngine;
using static ErrorMessage;
namespace Tweaks_Fixes
{
class Food_Patch : MonoBehaviour
{
static float foodCons = .5f; // vanilla 0.4
static float waterCons = .5f; // vanilla 0.55
//static float updateHungerInterval { get { return Main.config.hungerUpdateInterval / DayNightCycle.main.dayNightSpeed; } }
public static float hungerUpdateTime = 0f;
public static HashSet<Pickupable> cookedFish = new HashSet<Pickupable> { };
public static void UpdateStats(Survival __instance)
{
//AddDebug("dayNightSpeed " + DayNightCycle.main.dayNightSpeed);
//AddDebug("UpdateStats timeSprinted " + Player_Movement.timeSprinted);
//AddDebug("UpdateStats updateHungerInterval " + (int)updateHungerInterval);
float oldFood = __instance.food;
float oldWater = __instance.water;
if (Player_Movement.timeSprinted > 0f)
{
float sprintFoodCons = foodCons * Player_Movement.timeSprinted * ConfigMenu.hungerUpdateInterval.Value * .01f;
//AddDebug("UpdateStats sprintFoodCons " + sprintFoodCons);
__instance.food -= sprintFoodCons;
__instance.water -= sprintFoodCons;
Player_Movement.timeSprintStart = 0f;
Player_Movement.timeSprinted = 0f;
}
__instance.food -= foodCons;
__instance.water -= waterCons;
float foodDamage = 0f;
if (__instance.food < -100f)
{
foodDamage = Mathf.Abs(__instance.food + 100f);
__instance.food = -100f;
}
if (__instance.water < -100f)
{
foodDamage += Mathf.Abs(__instance.water + 100f);
__instance.water = -100f;
}
if (foodDamage > 0)
Player.main.liveMixin.TakeDamage(foodDamage, Player.main.gameObject.transform.position, DamageType.Starve);
float threshold1 = ConfigMenu.newHungerSystem.Value ? 0f : 20f;
float threshold2 = ConfigMenu.newHungerSystem.Value ? -50f : 10f;
__instance.UpdateWarningSounds(__instance.foodWarningSounds, __instance.food, oldFood, threshold1, threshold2);
__instance.UpdateWarningSounds(__instance.waterWarningSounds, __instance.water, oldWater, threshold1, threshold2);
hungerUpdateTime = Time.time + ConfigMenu.hungerUpdateInterval.Value;
//AddDebug("Invoke hungerUpdateInterval " + Main.config.hungerUpdateInterval);
//AddDebug("Invoke dayNightSpeed " + DayNightCycle.main.dayNightSpeed);
//__instance.Invoke("UpdateHunger", updateHungerInterval);
}
[HarmonyPatch(typeof(Survival))]
class Survival_Start_patch
{
private const float bladderFishOxygen = 15f;
[HarmonyPostfix]
[HarmonyPatch("Start")]
public static void StartPostfix(Survival __instance)
{
__instance.CancelInvoke();
}
[HarmonyPostfix]
[HarmonyPatch("UpdateHunger")]
static void UpdateHungerPostfix(Survival __instance)
{
//AddDebug("UpdateHunger ");
hungerUpdateTime = Time.time + ConfigMenu.hungerUpdateInterval.Value;
}
//!!! //[HarmonyPrefix] // does not run
//[HarmonyPatch("GetWeaknessSpeedScalar")]
public static bool GetWeaknessSpeedScalarPrefix(Survival __instance, ref float __result)
{
if (!ConfigMenu.newHungerSystem.Value)
return true;
float foodMult = 1f;
float waterMult = 1f;
if (Main.survival.food < 0f)
{
foodMult = Mathf.Abs(Main.survival.food / 100f);
foodMult = 1f - foodMult;
}
if (Main.survival.water < 0f)
{
waterMult = Mathf.Abs(Main.survival.water / 100f);
waterMult = 1f - waterMult;
}
__result = (foodMult + waterMult) * .5f;
//AddDebug("WeaknessSpeedScalar " + __result);
return false;
}
[HarmonyPrefix]
[HarmonyPatch("Eat")]
public static bool EatPrefix(Survival __instance, GameObject useObj, ref bool __result)
{
if (ConfigMenu.eatRawFish.Value == ConfigMenu.EatingRawFish.Vanilla && !ConfigMenu.newHungerSystem.Value)
return true;
Eatable eatable = useObj.GetComponent<Eatable>();
int food = (int)eatable.foodValue;
int water = (int)eatable.waterValue;
int playerMinFood = ConfigMenu.newHungerSystem.Value ? -100 : 0;
float playerMaxWater = ConfigMenu.newHungerSystem.Value ? 200f : 100f;
float playerMaxFood = 200f;
int minFood = food;
int maxFood = food;
int minWater = water;
int maxWater = water;
TechType techType = CraftData.GetTechType(useObj);
if (techType == TechType.None)
{
Pickupable p = useObj.GetComponent<Pickupable>();
if (p)
techType = p.GetTechType();
}
if (Util.IsEatableFish(useObj))
{
if (food > 0)
{
if (ConfigMenu.eatRawFish.Value == ConfigMenu.EatingRawFish.Vanilla)
{
minFood = food;
maxFood = food;
}
else if (ConfigMenu.eatRawFish.Value == ConfigMenu.EatingRawFish.Harmless)
{
minFood = 0;
maxFood = food;
}
else if (ConfigMenu.eatRawFish.Value == ConfigMenu.EatingRawFish.Risky)
{
minFood = -food;
maxFood = food;
}
else if (ConfigMenu.eatRawFish.Value == ConfigMenu.EatingRawFish.Harmful)
{
minFood = -food;
maxFood = 0;
}
}
if (water > 0)
{
if (ConfigMenu.eatRawFish.Value == ConfigMenu.EatingRawFish.Vanilla)
{
minWater = water;
maxWater = water;
}
else if (ConfigMenu.eatRawFish.Value == ConfigMenu.EatingRawFish.Harmless)
{
minWater = 0;
maxWater = water;
}
else if (ConfigMenu.eatRawFish.Value == ConfigMenu.EatingRawFish.Risky)
{
minWater = -water;
maxWater = water;
}
else if (ConfigMenu.eatRawFish.Value == ConfigMenu.EatingRawFish.Harmful)
{
minWater = -water;
maxWater = 0;
}
}
}
int rndFood = Main.rndm.Next(minFood, maxFood);
float finalFood = Mathf.Min(food, rndFood);
if (ConfigMenu.newHungerSystem.Value && __instance.food > 100f && finalFood > 0)
{
float mult = (playerMaxFood - __instance.food) * .01f;
finalFood *= mult;
}
int rndWater = Main.rndm.Next(minWater, maxWater);
float finalWater = Mathf.Min(water, rndWater);
if (ConfigMenu.newHungerSystem.Value && __instance.water > 100f && finalWater > 0)
{
float mult = (playerMaxWater - __instance.water) * .01f;
finalWater *= mult;
}
if (finalFood < 0 && __instance.food + finalFood < playerMinFood)
{
int foodDamage = Mathf.Abs((int)(__instance.food + finalFood - playerMinFood));
//AddDebug("foodDamage " + foodDamage);
Player.main.liveMixin.TakeDamage(foodDamage, Player.main.gameObject.transform.position, DamageType.Starve);
}
if (finalWater < 0 && __instance.water + finalWater < playerMinFood)
{
int waterDamage = Mathf.Abs((int)(__instance.water + finalWater - playerMinFood));
//AddDebug("waterDamage " + waterDamage);
Player.main.liveMixin.TakeDamage(waterDamage, Player.main.gameObject.transform.position, DamageType.Starve);
}
__instance.onEat.Trigger((float)finalFood);
__instance.food += finalFood;
__instance.onDrink.Trigger((float)finalWater);
__instance.water += finalWater;
//AddDebug("finalWater " + finalWater);
//AddDebug("finalFood " + finalFood);
if (finalFood > 0f)
GoalManager.main.OnCustomGoalEvent("Eat_Something");
if (finalWater > 0f)
GoalManager.main.OnCustomGoalEvent("Drink_Something");
if (techType == TechType.Bladderfish)
Player.main.GetComponent<OxygenManager>().AddOxygen(bladderFishOxygen);
Mathf.Clamp(__instance.water, playerMinFood, playerMaxWater);
Mathf.Clamp(__instance.food, playerMinFood, playerMaxFood);
int warn = ConfigMenu.newHungerSystem.Value ? 0 : 20;
if (finalWater > 0 && __instance.water > warn && __instance.water - finalWater < warn)
__instance.vitalsOkNotification.Play();
else if (finalFood > 0 && __instance.food > warn && __instance.food - finalWater < warn)
__instance.vitalsOkNotification.Play();
FMODUWE.PlayOneShot(CraftData.GetUseEatSound(techType), Player.main.transform.position);
__result = true;
return false;
}
}
[HarmonyPatch(typeof(Eatable))]
class Eatable_patch
{
[HarmonyPrefix]
[HarmonyPatch("Awake")]
public static void AwakePrefix(Eatable __instance)
{
if (ConfigMenu.foodDecayRateMult.Value == 0)
{ // does not work for dead fish
__instance.decomposes = false;
}
}
[HarmonyPostfix]
[HarmonyPatch("Awake")]
public static void AwakePostfix(Eatable __instance)
{
//if (!Main.loadingDone)
//{
// EcoTarget ecoTarget = __instance.GetComponent<EcoTarget>();
// if (ecoTarget && ecoTarget.type == EcoTargetType.DeadMeat && ecoTarget.transform.parent.name == "CellRoot(Clone)")
// {
//AddDebug("DeadMeat " + ecoTarget.name + " PARENT " + ecoTarget.transform.parent.name);
//Destroy(__instance.gameObject);
if (__instance.decomposes)
{
__instance.kDecayRate *= ConfigMenu.foodDecayRateMult.Value;
}
if (ConfigMenu.fishFoodWaterRatio.Value > 0)
{
if (Util.IsEatableFish(__instance.gameObject) && __instance.foodValue > 0)
__instance.waterValue = __instance.foodValue * ConfigMenu.fishFoodWaterRatio.Value;
}
//TechType tt = CraftData.GetTechType(__instance.gameObject);
//Main.logger.LogMessage("Eatable awake " + tt + " eatableFoodValue.ContainsKey " + Main.config.eatableFoodValue.ContainsKey(tt));
//if (Main.config.eatableFoodValue.ContainsKey(tt))
// __instance.foodValue = Main.config.eatableFoodValue[tt];
//if (Main.config.eatableWaterValue.ContainsKey(tt))
// __instance.waterValue = Main.config.eatableWaterValue[tt];
}
[HarmonyPrefix]
[HarmonyPatch("SetDecomposes")]
public static void SetDecomposesPrefix(Eatable __instance, ref bool value)
{ // SetDecomposes runs when fish killed
if (value && ConfigMenu.foodDecayRateMult.Value == 0)
value = false;
}
}
[HarmonyPatch(typeof(EcoTarget), "OnEnable")]
class EcoTarget_OnEnable_patch
{
public static void Postfix(EcoTarget __instance)
{
if (ConfigToEdit.removeCookedFishOnReload.Value && !Main.gameLoaded && __instance.type == EcoTargetType.DeadMeat)
{ // remove cooked fish from lava geysers
Pickupable p = __instance.GetComponent<Pickupable>();
if (p)// p.inventoryItem is null
cookedFish.Add(p);
}
}
}
[HarmonyPatch(typeof(Plantable), "OnProtoDeserialize")]
class Inventory_OnProtoDeserialize_patch
{
public static void Postfix(Plantable __instance)
{
//AddDebug(" OnProtoDeserialize " + __instance.plantTechType);
if (!ConfigToEdit.canReplantMelon.Value)
{
TechType tt = __instance.plantTechType;
if (tt == TechType.Melon || tt == TechType.SmallMelon || tt == TechType.JellyPlant)
Destroy(__instance);
}
}
}
//[HarmonyPatch(typeof(ItemsContainer), "NotifyRemoveItem")]
class ItemsContainer_NotifyRemoveItem_patch
{
public static void Postfix(ItemsContainer __instance, InventoryItem item)
{
//if (crafterOpem && Inventory.main._container == __instance)
//if (Main.config.foodTweaks && Main.crafterOpen)
{ // cooking fish
//TechType tt = item.item.GetTechType();
if (Util.IsEatableFish(item.item.gameObject))
{
Eatable eatable = item.item.GetComponent<Eatable>();
//AddDebug(" NotifyRemoveItem timeDecayStart " + eatable.timeDecayStart);
//AddDebug(" NotifyRemoveItem waterValue " + eatable.GetWaterValue() + " " + eatable.GetFoodValue());
//waterValueMult = eatable.GetWaterValue() / eatable.waterValue;
//foodValueMult = eatable.GetFoodValue() / eatable.foodValue;
//timeDecayStart = eatable.timeDecayStart;
}
//else
// timeDecayStart = 0f;
//{
// waterValueMult = 1f;
// foodValueMult = 1f;
//}
}
}
}
//[HarmonyPatch(typeof(Inventory), "ConsumeResourcesForRecipe")]
class Inventory_ConsumeResourcesForRecipe_patch
{
public static void Postfix(Inventory __instance, TechType techType)
{
ITechData techData = CraftData.Get(techType);
if (techData == null)
return;
int index = 0;
for (int ingredientCount = techData.ingredientCount; index < ingredientCount; ++index)
{
IIngredient ingredient = techData.GetIngredient(index);
TechType ingredientTT = ingredient.techType;
}
}
}
//[HarmonyPatch(typeof(CrafterLogic), "ConsumeResources")]
class CrafterLogic_ConsumeResources_patch
{
public static void Postfix(CrafterLogic __instance, TechType techType)
{
//TechType tt = item.item.GetTechType();
}
}
//[HarmonyPatch(typeof(Crafter), "OnCraftingBegin")]
class Crafter_OnCraftingBegin_patch
{
public static void Prefix(Crafter __instance, TechType techType)
{
//TechType tt = item.item.GetTechType();
}
}
//[HarmonyPatch(typeof(Crafter), "Craft")]
class Crafter_Craft_patch
{
public static void Prefix(Crafter __instance, TechType techType)
{
//TechType tt = item.item.GetTechType();
}
}
//[HarmonyPatch(typeof(uGUI_CraftingMenu), "Action")]
class CraftingAnalytics_OnCraft_patch
{
public static void Postfix(uGUI_CraftingMenu __instance)
{
//if (sender.action == TreeAction.Craft)
//AddDebug(" uGUI_CraftingMenu Craft " );
}
}
}
}