-
Notifications
You must be signed in to change notification settings - Fork 5
/
SpeedyPaths.cs
384 lines (347 loc) · 20.7 KB
/
SpeedyPaths.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
using System.Collections.Generic;
using BepInEx;
using AuthoritativeConfig;
using HarmonyLib;
using UnityEngine;
using System;
namespace SpeedyPaths
{
[BepInPlugin("nex.SpeedyPaths", "Speedy Paths Mod", "1.0.8")]
public class SpeedyPathsClientMod : BaseUnityPlugin {
public enum GroundType
{
Untamed,
PathDirt,
PathStone,
Cultivated,
StructureWood,
StructureHardWood,
StructureStone,
StructureIron,
StructureMarble
}
private static Dictionary<GroundType, ConfigEntry<float>> _speedModifiers = new Dictionary<GroundType, ConfigEntry<float>>();
private static Dictionary<GroundType, ConfigEntry<float>> _staminaModifiers = new Dictionary<GroundType, ConfigEntry<float>>();
private static Dictionary<Heightmap.Biome, ConfigEntry<float>> _untamedSpeedModifiers = new Dictionary<Heightmap.Biome, ConfigEntry<float>>();
private static Dictionary<Heightmap.Biome, ConfigEntry<float>> _untamedStaminaModifiers = new Dictionary<Heightmap.Biome, ConfigEntry<float>>();
private static ConfigEntry<bool> _showHudStatus;
private static ConfigEntry<string> _hudStatusText;
private static ConfigEntry<bool> _hudDynamicStatusText;
private static ConfigEntry<bool> _hudShowEffectPercent;
private static List<ConfigEntry<float>> _hudPosIconThresholds = new List<ConfigEntry<float>>();
private static List<ConfigEntry<float>> _hudNegIconThresholds = new List<ConfigEntry<float>>();
private static ConfigEntry<float> _groundSensorUpdateInterval;
private static ConfigEntry<int> _groundSensorRadius;
//Display Strings
private static Dictionary<GroundType, ConfigEntry<string>> _groundTypeStrings = new Dictionary<GroundType, ConfigEntry<string>>();
private static Dictionary<Heightmap.Biome, ConfigEntry<string>> _biomeTypeStrings = new Dictionary<Heightmap.Biome, ConfigEntry<string>>();
private static int m_pieceLayer;
private static object[] _worldToVertexArgs = new object[]{ Vector3.zero, null, null };
private static List<Sprite> _speedSprites = new List<Sprite>();
AssetBundle _speedyassets;
private static float m_sensorTime;
private static GroundType m_cachedGroundType;
public new static BepInEx.Logging.ManualLogSource Logger;
public new AuthoritativeConfig.Config Config
{
get { return AuthoritativeConfig.Config.Instance; }
set {}
}
void Awake() {
Logger = base.Logger;
_speedyassets = AssetBundle.LoadFromMemory(Properties.Resources.speedyassets);
if (!_speedyassets)
{
Logger.LogError($"Failed to read AssetBundle stream");
return;
}
Config.init( this, true );
_hudDynamicStatusText = Config.Bind("Hud", "EnableDynamicStatusText", true, "Show surface type text as status effect name", false);
_hudShowEffectPercent = Config.Bind("Hud", "ShowStatusEffectPercent", true, "Show the status effect buff/debuff %", false);
_showHudStatus = Config.Bind("Hud", "ShowStatusIcon", true, "Show the speedypaths status icon in the hud", false);
_hudStatusText = Config.Bind("Hud", "StatusText", "Speedy Path", "Text shown above the status icon", false);
_hudPosIconThresholds.Add( Config.Bind("Hud", "BuffIcon +1", 1.0f, "Speed buff threshold to show lvl 1 buff icon") );
_hudPosIconThresholds.Add( Config.Bind("Hud", "BuffIcon +2", 1.39f, "Speed buff threshold to show lvl 2 buff icon") );
_hudNegIconThresholds.Add( Config.Bind("Hud", "DebuffIcon -1", 1.0f, "Speed buff threshold to show lvl 1 debuff icon") );
_hudNegIconThresholds.Add( Config.Bind("Hud", "DebuffIcon -2", 0.79f, "Speed buff threshold to show lvl 2 debuff icon") );
_groundSensorUpdateInterval = Config.Bind("Performance", "Ground Sensor Interval", 1.0f, "Interval between in seconds between ground type checks. Lower number, more accute ground detection.", true);
_groundSensorRadius = Config.Bind("Performance", "Ground Sensor Radius", 1, "Radius of ground pixels to sample when checking under the player", true);
_speedModifiers[GroundType.PathDirt] = Config.Bind("SpeedModifiers", "DirtPathSpeed", 1.15f, "Modifier for speed while on dirt paths");
_speedModifiers[GroundType.PathStone] = Config.Bind("SpeedModifiers", "StonePathSpeed", 1.4f, "Modifier for speed while on stone paths");
_speedModifiers[GroundType.Cultivated] = Config.Bind("SpeedModifiers", "CultivatedSpeed", 1.0f, "Modifier for speed while on Cultivated land");
_speedModifiers[GroundType.StructureWood] = Config.Bind("SpeedModifiers", "StructureWoodSpeed", 1.15f, "Modifier for speed while on wood structures");
_speedModifiers[GroundType.StructureHardWood] = Config.Bind("SpeedModifiers", "StructureHardWoodSpeed", 1.15f, "Modifier for speed while on core wood structures");
_speedModifiers[GroundType.StructureStone] = Config.Bind("SpeedModifiers", "StructureStoneSpeed", 1.4f, "Modifier for speed while on stone structures");
_speedModifiers[GroundType.StructureIron] = Config.Bind("SpeedModifiers", "StructureIronSpeed", 1.4f, "Modifier for speed while on ironwood structures");
_speedModifiers[GroundType.StructureMarble] = Config.Bind("SpeedModifiers", "StructureMarbleSpeed", 1.4f, "Modifier for speed while on black marble structures");
_staminaModifiers[GroundType.PathDirt] = Config.Bind("StaminaModifiers", "DirtPathStamina", 0.8f, "Modifier for stamina while on dirt paths");
_staminaModifiers[GroundType.PathStone] = Config.Bind("StaminaModifiers", "StonePathStamina", 0.7f, "Modifier for stamina while on stone paths");
_staminaModifiers[GroundType.Cultivated] = Config.Bind("StaminaModifiers", "CultivatedStamina", 1.0f, "Modifier for stamina while on Cultivated land");
_staminaModifiers[GroundType.StructureWood] = Config.Bind("StaminaModifiers", "StructureWoodStamina", 0.8f, "Modifier for stamina while on wood structures");
_staminaModifiers[GroundType.StructureHardWood] = Config.Bind("StaminaModifiers", "StructureHardWoodStamina", 0.8f, "Modifier for stamina while on core wood structures");
_staminaModifiers[GroundType.StructureStone] = Config.Bind("StaminaModifiers", "StructureStoneStamina", 0.7f, "Modifier for stamina while on stone structures");
_staminaModifiers[GroundType.StructureIron] = Config.Bind("StaminaModifiers", "StructureIronStamina", 0.7f, "Modifier for stamina while on ironwood structures");
_staminaModifiers[GroundType.StructureMarble] = Config.Bind("StaminaModifiers", "StructureMarbleStamina", 0.7f, "Modifier for stamina while on black marble structures");
//Should handle new biomes automagically
foreach(Heightmap.Biome biome in Enum.GetValues(typeof(Heightmap.Biome)))
{
_untamedSpeedModifiers[biome] = Config.Bind("SpeedModifiers_Biomes", "Untamed_" + biome.ToString() + "_Speed", 1.0f, "Speed modifier for uncleared ground in " + biome.ToString() + " Biomes");
_untamedStaminaModifiers[biome] = Config.Bind("StaminaModifiers_Biomes", "Untamed_" + biome.ToString()+ "_Stamina", 1.0f, "Stamina modifier for uncleared ground in " + biome.ToString() + " Biomes");
_biomeTypeStrings[biome] = Config.Bind("Strings", "Biome_"+biome.ToString(), "default", "Dynamic status mapping for "+ biome.ToString() +" groundcover. 'default' uses localized biome name.");
}
_groundTypeStrings[GroundType.PathDirt] = Config.Bind("Strings", "PathDirt", "Dirt Path", "Dynamic status mapping for dirt paths");
_groundTypeStrings[GroundType.PathStone] = Config.Bind("Strings", "PathStone", "Stone Path", "Dynamic status mapping for stone paths");
_groundTypeStrings[GroundType.Cultivated] = Config.Bind("Strings", "Cultivated", "Cultivated", "Dynamic status mapping for Cultivated land");
_groundTypeStrings[GroundType.StructureWood] = Config.Bind("Strings", "StructureWood", "Wood", "Dynamic status mapping for wood structures");
_groundTypeStrings[GroundType.StructureHardWood] = Config.Bind("Strings", "StructureHardWood", "Hardwood", "Dynamic status mapping for core wood structures");
_groundTypeStrings[GroundType.StructureStone] = Config.Bind("Strings", "StructureStone", "Stone", "MDynamic status mapping for stone structures");
_groundTypeStrings[GroundType.StructureIron] = Config.Bind("Strings", "StructureIron", "Iron", "Dynamic status mapping for ironwood structures");
_groundTypeStrings[GroundType.StructureMarble] = Config.Bind("Strings", "StructureMarble", "Marble", "Dynamic status mapping for black marble structures");
if (m_pieceLayer == 0)
{
m_pieceLayer = LayerMask.NameToLayer("piece");
}
_speedSprites.Add( _speedyassets.LoadAsset<Sprite>("assets/nex.speedypaths/speedypaths_1.png") );
_speedSprites.Add( _speedyassets.LoadAsset<Sprite>("assets/nex.speedypaths/speedypaths_2.png") );
_speedSprites.Add( _speedyassets.LoadAsset<Sprite>("assets/nex.speedypaths/speedypaths_n1.png") );
_speedSprites.Add( _speedyassets.LoadAsset<Sprite>("assets/nex.speedypaths/speedypaths_n2.png") );
m_sensorTime = 0f;
m_cachedGroundType = GroundType.Untamed;
Harmony.CreateAndPatchAll(typeof(SpeedyPathsClientMod));
}
private static float _activeSpeedModifier = 1.0f;
private static float _activeStaminaModifier = 1.0f;
private static String _activeStatusText;
private static Sprite _activeStatusSprite;
//hooks
[HarmonyPatch(typeof(Player), "FixedUpdate")]
[HarmonyPrefix]
static void UpdateModifiers( Player __instance )
{
m_sensorTime -= Time.fixedDeltaTime;
if( Player.m_localPlayer == __instance && !__instance.IsDead() )
{
UpdateGroundTypeCache( __instance );
_activeSpeedModifier = GetSpeedyPathModifier( __instance );
_activeStaminaModifier = GetSpeedyPathStaminaModifier(__instance);
if( _activeSpeedModifier > 1.0f )
{
for( int i = 0; i < _hudPosIconThresholds.Count; ++i )
{
if( _activeSpeedModifier > _hudPosIconThresholds[i].Value )
{
_activeStatusSprite = _speedSprites[i];
}
}
}
else
{
for( int i = 0; i < _hudNegIconThresholds.Count; ++i )
{
if( _activeSpeedModifier < _hudNegIconThresholds[i].Value )
{
_activeStatusSprite = _speedSprites[_hudPosIconThresholds.Count + i];
}
}
}
}
}
[HarmonyPatch(typeof(Player), "CheckRun")]
[HarmonyPrefix]
static void CheckRunPrefixStaminaMod( Player __instance, out float __state )
{
__state = __instance.m_runStaminaDrain;
__instance.m_runStaminaDrain *= _activeStaminaModifier;
}
[HarmonyPatch(typeof(Player), "CheckRun")]
[HarmonyPostfix]
static void CheckRunPostfixStaminaMod( Player __instance, float __state )
{
__instance.m_runStaminaDrain = __state;
}
[HarmonyPatch(typeof(Player), "GetJogSpeedFactor")]
[HarmonyPostfix]
static void JogSpeedPathFactor( Player __instance, ref float __result )
{
__result *= _activeSpeedModifier;
}
[HarmonyPatch(typeof(Player), "GetRunSpeedFactor")]
[HarmonyPostfix]
static void RunSpeedPathFactor( Player __instance, ref float __result )
{
__result *= _activeSpeedModifier;
}
private static StatusEffect _pathBuffSEDummy;
private static float _prevModValue = 1.0f;
[HarmonyPatch(typeof(Hud), "UpdateStatusEffects")]
[HarmonyPrefix]
static void UpdatePathIcon( Hud __instance, List<StatusEffect> statusEffects )
{
if( !Player.m_localPlayer.IsDead() && _showHudStatus.Value && _activeSpeedModifier != 1.0f )
{
if( _pathBuffSEDummy == null)
{
_pathBuffSEDummy = ScriptableObject.CreateInstance(typeof(StatusEffect)) as StatusEffect;
}
_pathBuffSEDummy.m_name = _hudDynamicStatusText.Value ? _activeStatusText : _hudStatusText.Value;
_pathBuffSEDummy.m_icon = _activeStatusSprite;
if( _activeSpeedModifier != 1.0f )
{
if( _activeSpeedModifier != _prevModValue )
{
_pathBuffSEDummy.m_isNew = true;
}
statusEffects.Add(_pathBuffSEDummy);
}
_prevModValue = _activeSpeedModifier;
}
}
[HarmonyPatch(typeof(StatusEffect), "GetIconText")]
[HarmonyPostfix]
static void SpeedyPathsStatusEffect_GetIconText( StatusEffect __instance, ref string __result )
{
if( __instance == _pathBuffSEDummy && _hudShowEffectPercent.Value )
{
__result = (_activeSpeedModifier - 1.0f).ToString("P0");
}
}
//util funcs
static float GetSpeedyPathModifier( Player player )
{
if( !player.IsSwimming() && !player.InInterior() )
{
if( _speedModifiers.ContainsKey(m_cachedGroundType) )
{
_activeStatusText = _groundTypeStrings[m_cachedGroundType].Value;
return _speedModifiers[m_cachedGroundType].Value;
}
//fallback to biome speed
Heightmap.Biome playerBiome = player.GetCurrentBiome();
//Handle new biomes "gracefully"
if( !_untamedSpeedModifiers.ContainsKey(playerBiome) )
{
Logger.LogWarning( $"New biome {playerBiome.ToString()}. Unsure how to Handle. Falling back to None." );
playerBiome = Heightmap.Biome.None;
}
_activeStatusText = "$biome_" + playerBiome.ToString().ToLower();
if( _biomeTypeStrings.ContainsKey(playerBiome) && _biomeTypeStrings[playerBiome].Value != "default" )
{
_activeStatusText = _biomeTypeStrings[playerBiome].Value;
}
return _untamedSpeedModifiers[playerBiome].Value;
}
return 1.0f;
}
static float GetSpeedyPathStaminaModifier( Player player )
{
if( !player.IsSwimming() && !player.InInterior() )
{
if( _staminaModifiers.ContainsKey(m_cachedGroundType) )
{
return _staminaModifiers[m_cachedGroundType].Value;
}
//fallback to biome stamina
Heightmap.Biome playerBiome = player.GetCurrentBiome();
//Handle new biomes "gracefully"
if( !_untamedStaminaModifiers.ContainsKey(playerBiome) )
{
Logger.LogWarning( $"New biome {playerBiome.ToString()}. Unsure how to Handle. Falling back to None." );
playerBiome = Heightmap.Biome.None;
}
return _untamedStaminaModifiers[playerBiome].Value;
}
return 1.0f;
}
static void UpdateGroundTypeCache( Player player )
{
if(m_sensorTime > 0)
{
return;
}
m_sensorTime = _groundSensorUpdateInterval.Value;
Collider lastGroundCollider = player.GetLastGroundCollider();
if ((bool)lastGroundCollider)
{
//this is extremely generous, a kiln road will work...
//todo: piece whitelist / blacklist?
//Do this check first, so we can potentially break out from expensive terrain check
if (lastGroundCollider.gameObject.layer == m_pieceLayer)
{
WearNTear componentInParent = lastGroundCollider.GetComponentInParent<WearNTear>();
if ((bool)componentInParent)
{
switch (componentInParent.m_materialType)
{
case WearNTear.MaterialType.Wood:
m_cachedGroundType = GroundType.StructureWood;
return;
case WearNTear.MaterialType.Stone:
m_cachedGroundType = GroundType.StructureStone;
return;
case WearNTear.MaterialType.HardWood:
m_cachedGroundType = GroundType.StructureHardWood;
return;
case WearNTear.MaterialType.Iron:
m_cachedGroundType = GroundType.StructureIron;
return;
case WearNTear.MaterialType.Marble:
m_cachedGroundType = GroundType.StructureMarble;
return;
}
}
}
Heightmap hmap = lastGroundCollider.GetComponent<Heightmap>();
if (hmap != null)
{
Texture2D paintMask = Traverse.Create(hmap).Field("m_paintMask").GetValue() as Texture2D;
_worldToVertexArgs[0] = Traverse.Create(player).Field("m_lastGroundPoint").GetValue() as Vector3?;
AccessTools.Method(typeof(Heightmap), "WorldToVertex").Invoke(hmap, _worldToVertexArgs);
int sample_radius = _groundSensorRadius.Value;
Color hmcl_pixel = new Color();
//optimize ground sampling into a single call to getPixels
//These are stitched together, we only check the Heightmap the player is currently on. So we need to be mindful of the edges.
int x_range_min = Math.Max((int)_worldToVertexArgs[1] - sample_radius, 0);
int x_range_max = sample_radius * 2;
if(x_range_max + (int)_worldToVertexArgs[1] >= paintMask.width)
{
x_range_max = paintMask.width - x_range_min - 1;
}
int y_range_min = Math.Max((int)_worldToVertexArgs[2] - sample_radius, 0);
int y_range_max = sample_radius * 2;
if(y_range_max + (int)_worldToVertexArgs[2] >= paintMask.height)
{
y_range_max = paintMask.height - y_range_min - 1;
}
//Logger.LogInfo($"Fetching Pixel range x: {x_range_min.ToString()} + {x_range_max.ToString()} y: {y_range_min.ToString()} + {y_range_max.ToString()}");
var samples = paintMask.GetPixels(x_range_min, y_range_min, x_range_max, y_range_max, 0);
//Logger.LogInfo($"Got Samples: {samples.Length.ToString()}");
foreach(var pixel in samples)
{
hmcl_pixel += pixel;
//Logger.LogInfo($"Sample: {pixel.ToString()}");
}
hmcl_pixel = new Color(hmcl_pixel.r / samples.Length, hmcl_pixel.g / samples.Length, hmcl_pixel.b / samples.Length);
//Logger.LogInfo($"Avg ground pixel {hmcl_pixel.ToString()} from {samples.ToString()} Samples");
//Single Pixel
//Color hmcl_pixel = paintMask.GetPixel( (int)_worldToVertexArgs[1], (int)_worldToVertexArgs[2] );
if( hmcl_pixel.b > 0.4f )
{
m_cachedGroundType = GroundType.PathStone;
return;
}
else if( hmcl_pixel.r > 0.4f )
{
m_cachedGroundType = GroundType.PathDirt;
return;
}
else if( hmcl_pixel.g > 0.4f )
{
m_cachedGroundType = GroundType.Cultivated;
return;
}
}
}
m_cachedGroundType = GroundType.Untamed;
}
}
}