-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathPerformLightningUpdatePatch.cs
131 lines (127 loc) · 7.04 KB
/
PerformLightningUpdatePatch.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
using Harmony;
using Microsoft.Xna.Framework;
using Netcode;
using StardewValley;
using StardewValley.TerrainFeatures;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using Object = StardewValley.Object;
namespace SafeLightning
{
/// <summary>Patches <see cref="Utility.performLightningUpdate"/>.</summary>
[HarmonyPatch]
internal class PerformLightningUpdatePatch
{
/*********
** Private methods
*********/
/// <summary>The method to be patched.</summary>
/// <returns><see cref="Utility.performLightningUpdate"/>.</returns>
[SuppressMessage("ReSharper", "UnusedMember.Local", Justification = "Method names are defined by Harmony.")]
private static MethodBase TargetMethod()
{
return typeof(Utility).GetMethod(nameof(Utility.performLightningUpdate));
}
/// <summary>The code to run before the original method.</summary>
/// <returns>Whether to run the original method or not.</returns>
[SuppressMessage("ReSharper", "UnusedMember.Local", Justification = "Method names are defined by Harmony.")]
private static bool Prefix()
{
PerformLightningUpdatePatch.PerformLightningUpdate();
return false;
}
/// <summary>Performs a lightning update, not harming objects in the world.</summary>
/// <remarks>Copied from the decompiled vanilla method, with as few changes as possible to make updating easier.</remarks>
[SuppressMessage("ReSharper", "All", Justification = "Copied from the vanilla decompilation.")]
[SuppressMessage("SMAPI.CommonErrors", "AvoidNetField", Justification = "Copied from the vanilla decompilation.")]
[SuppressMessage("SMAPI.CommonErrors", "AvoidImplicitNetFieldCast", Justification = "Copied from the vanilla decompilation.")]
private static void PerformLightningUpdate()
{
Random random = new Random((int)Game1.uniqueIDForThisGame + (int)Game1.stats.DaysPlayed + Game1.timeOfDay);
if (random.NextDouble() < 0.125 + Game1.player.team.AverageDailyLuck((GameLocation)null) + Game1.player.team.AverageLuckLevel((GameLocation)null) / 100.0)
{
Farm.LightningStrikeEvent lightningStrikeEvent = new Farm.LightningStrikeEvent();
lightningStrikeEvent.bigFlash = true;
Farm locationFromName = Game1.getLocationFromName("Farm") as Farm;
List<Vector2> source = new List<Vector2>();
foreach (KeyValuePair<Vector2, Object> pair in locationFromName.objects.Pairs)
{
if ((bool)((NetFieldBase<bool, NetBool>)pair.Value.bigCraftable) && pair.Value.ParentSheetIndex == 9)
source.Add(pair.Key);
}
if (source.Count > 0)
{
for (int index1 = 0; index1 < 2; ++index1)
{
Vector2 index2 = source.ElementAt<Vector2>(random.Next(source.Count));
if (locationFromName.objects[index2].heldObject.Value == null)
{
locationFromName.objects[index2].heldObject.Value = new Object(787, 1, false, -1, 0);
locationFromName.objects[index2].minutesUntilReady.Value = Utility.CalculateMinutesUntilMorning(Game1.timeOfDay);
locationFromName.objects[index2].shakeTimer = 1000;
lightningStrikeEvent.createBolt = true;
lightningStrikeEvent.boltPosition = index2 * 64f + new Vector2(32f, 0.0f);
locationFromName.lightningStrikeEvent.Fire(lightningStrikeEvent);
return;
}
}
}
if (random.NextDouble() < 0.25 - Game1.player.team.AverageDailyLuck((GameLocation)null) - Game1.player.team.AverageLuckLevel((GameLocation)null) / 100.0)
{
try
{
KeyValuePair<Vector2, TerrainFeature> keyValuePair = locationFromName.terrainFeatures.Pairs.ElementAt(random.Next(locationFromName.terrainFeatures.Count()));
if (!(keyValuePair.Value is FruitTree))
{
int num = !(keyValuePair.Value is HoeDirt) || (keyValuePair.Value as HoeDirt).crop == null ? 0 : (!(bool)((NetFieldBase<bool, NetBool>)(keyValuePair.Value as HoeDirt).crop.dead) ? 1 : 0);
if (keyValuePair.Value.performToolAction((Tool)null, 50, keyValuePair.Key, (GameLocation)locationFromName))
{
//lightningStrikeEvent.destroyedTerrainFeature = true;
lightningStrikeEvent.createBolt = true;
//locationFromName.terrainFeatures.Remove(keyValuePair.Key);
lightningStrikeEvent.boltPosition = keyValuePair.Key * 64f + new Vector2(32f, (float)sbyte.MinValue);
}
if (num != 0)
{
if (keyValuePair.Value is HoeDirt)
{
if ((keyValuePair.Value as HoeDirt).crop != null)
{
if ((bool)((NetFieldBase<bool, NetBool>)(keyValuePair.Value as HoeDirt).crop.dead))
{
lightningStrikeEvent.createBolt = true;
lightningStrikeEvent.boltPosition = keyValuePair.Key * 64f + new Vector2(32f, 0.0f);
}
}
}
}
}
else if (keyValuePair.Value is FruitTree)
{
//(keyValuePair.Value as FruitTree).struckByLightningCountdown.Value = 4;
//(keyValuePair.Value as FruitTree).shake(keyValuePair.Key, true, (GameLocation)locationFromName);
lightningStrikeEvent.createBolt = true;
lightningStrikeEvent.boltPosition = keyValuePair.Key * 64f + new Vector2(32f, (float)sbyte.MinValue);
}
}
catch (Exception ex)
{
}
}
locationFromName.lightningStrikeEvent.Fire(lightningStrikeEvent);
}
else
{
if (random.NextDouble() >= 0.1)
return;
(Game1.getLocationFromName("Farm") as Farm).lightningStrikeEvent.Fire(new Farm.LightningStrikeEvent()
{
smallFlash = true
});
}
}
}
}