-
Notifications
You must be signed in to change notification settings - Fork 8
/
ModEntry.cs
487 lines (437 loc) · 21.9 KB
/
ModEntry.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
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Netcode;
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewValley;
using Object = StardewValley.Object;
namespace LadderLocator
{
internal class ModEntry : Mod
{
private static List<int> _selectedNodeTypeIndices;
private static Dictionary<Vector2, Stone> _nodeStones;
private static ModConfig _config;
private static Texture2D _pixelTexture;
private static Texture2D _imageTexture;
private static List<LadderStone> _ladderStones;
private static bool _nextIsShaft;
public override void Entry(IModHelper helper)
{
_config = Helper.ReadConfig<ModConfig>();
_pixelTexture = new Texture2D(Game1.graphics.GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
var colorArray = Enumerable.Range(0, 1).Select(i => Color.White).ToArray();
_pixelTexture.SetData(colorArray);
_imageTexture = helper.ModContent.Load<Texture2D>(_config.HighlightImageFilename);
_ladderStones = new List<LadderStone>();
_selectedNodeTypeIndices = RadarNode.All.Where(radarNode => _config.NodeTypes.Contains(radarNode.type))
.OrderBy(radarNode => radarNode.value).Select(radarNode => radarNode.spriteIndex).ToList();
_nodeStones = new Dictionary<Vector2, Stone>();
Helper.Events.World.ObjectListChanged += OnObjectListChanged;
if (_config.HighlightTypes.Count > 0) Helper.Events.Display.RenderedWorld += OnRenderedWorld;
if (_config.NodeRadar) Helper.Events.Display.RenderingHud += OnRenderingHud;
Helper.Events.Input.ButtonPressed += OnButtonPressed;
Helper.Events.Player.Warped += OnWarped;
Helper.Events.GameLoop.GameLaunched += OnGameLaunched;
}
private void OnObjectListChanged(object sender, ObjectListChangedEventArgs e)
{
if (!e.IsCurrentLocation) return;
if (e.Location is StardewValley.Locations.MineShaft mine)
{
var ladderHasSpawned = Helper.Reflection.GetField<bool>(mine, "ladderHasSpawned").GetValue();
if (ladderHasSpawned) _ladderStones.Clear();
else if (_ladderStones.Count <= 0) FindLadders(mine);
}
if (LocationHasNodes(e.Location))
foreach (var pair in e.Removed)
_nodeStones.Remove(pair.Key);
}
private void OnWarped(object sender, WarpedEventArgs e)
{
if (!e.IsLocalPlayer) return;
_ladderStones.Clear();
_nodeStones.Clear();
_nextIsShaft = false;
if (LocationHasNodes(e.NewLocation)) FindNodes(e.NewLocation);
if (!(e.NewLocation is StardewValley.Locations.MineShaft mine && FindLadders(mine)))
{
Helper.Events.GameLoop.OneSecondUpdateTicked += OnUpdateTicked;
}
}
private void OnUpdateTicked(object sender, OneSecondUpdateTickedEventArgs e)
{
if (Game1.mine == null) return;
if (_ladderStones.Count > 0 || FindLadders(Game1.mine))
Helper.Events.GameLoop.OneSecondUpdateTicked -= OnUpdateTicked;
}
private bool FindLadders(StardewValley.Locations.MineShaft mine)
{
var ladderHasSpawned = Helper.Reflection.GetField<bool>(mine, "ladderHasSpawned").GetValue();
if (ladderHasSpawned || mine.mustKillAllMonstersToAdvance() || !mine.shouldCreateLadderOnThisLevel()) return true;
var netStonesLeftOnThisLevel = Helper.Reflection
.GetField<NetIntDelta>(mine, "netStonesLeftOnThisLevel").GetValue().Value;
var chance = 0.02 + 1.0 / Math.Max(1, netStonesLeftOnThisLevel) + Game1.player.LuckLevel / 100.0 +
Game1.player.DailyLuck / 5.0;
if (mine.EnemyCount == 0) chance += 0.04;
foreach (var pair in mine.Objects.Pairs.Where(pair => pair.Value.Name.Equals("Stone")))
{
var obj = pair.Value;
var r = Utility.CreateDaySaveRandom(pair.Key.X * 1000, pair.Key.Y, mine.mineLevel);
r.NextDouble();
var next = r.NextDouble();
if ((netStonesLeftOnThisLevel == 0 || next < chance) && obj.ParentSheetIndex / 24 < Game1.objectSpriteSheet.Height) _ladderStones.Add(new LadderStone(obj));
}
if (_config.ForceShafts && mine.getMineArea() == StardewValley.Locations.MineShaft.desertArea
&& !_nextIsShaft && _ladderStones.Count > 0) ForceShaft(mine);
return _ladderStones.Count > 0;
}
private void ForceShaft(StardewValley.Locations.MineShaft mine)
{
var mineRandom = Helper.Reflection.GetField<Random>(mine, "mineRandom").GetValue();
var r = Cloner.Clone(mineRandom);
var next = r.NextDouble();
while (next >= 0.2)
{
next = r.NextDouble();
mineRandom.NextDouble();
}
_nextIsShaft = true;
}
private bool LocationHasNodes(GameLocation loc)
{
return loc is StardewValley.Locations.MineShaft || loc is StardewValley.Locations.VolcanoDungeon || loc is StardewValley.Locations.Mountain;
}
private void FindNodes(GameLocation loc)
{
foreach (var pair in loc.objects.Pairs.Where(pair => pair.Value.Name.Equals("Stone") && _selectedNodeTypeIndices.Contains(pair.Value.ParentSheetIndex)))
_nodeStones.Add(pair.Key, new Stone(pair.Value));
}
private void OnRenderedWorld(object sender, RenderedWorldEventArgs e)
{
if (!Context.IsWorldReady) return;
if (_ladderStones.Count > 0)
{
foreach (var obj in _ladderStones)
{
var rect = obj.BoundingBox;
rect.Offset(-Game1.viewport.X, -Game1.viewport.Y);
var rectColor = (_config.HighlightUsesStoneTint ? obj.Tint : _config.HighlightRectangleRGBA) * Convert.ToSingle(_config.HighlightAlpha);
if (_config.HighlightTypes.Contains(HighlightType.Rectangle)) DrawRectangle(rect, rectColor);
var imageColor = (_config.HighlightUsesStoneTint ? obj.Tint : Color.Black) * Convert.ToSingle(_config.HighlightAlpha);
if (_config.HighlightTypes.Contains(HighlightType.Image)) DrawImage(rect, imageColor, obj.SpriteIndex, obj.Flipped);
if (_config.HighlightTypes.Contains(HighlightType.Sprite)) DrawSprite(rect, imageColor, obj.SpriteIndex, obj.Flipped);
}
}
}
private void OnRenderingHud(object sender, RenderingHudEventArgs e)
{
if (!Game1.eventUp && _nodeStones.Count > 0)
{
int i = 0;
foreach (var nodeType in _selectedNodeTypeIndices.Where(nodeType => _nodeStones.Values.Select(node => node.SpriteIndex).Contains(nodeType)))
{
Game1.spriteBatch.Draw(Game1.objectSpriteSheet,
new Vector2(Game1.graphics.GraphicsDevice.Viewport.TitleSafeArea.Right - (Game1.showingHealth ? 120 : 64), Game1.graphics.GraphicsDevice.Viewport.TitleSafeArea.Bottom - 16 - Convert.ToInt32(16 * Convert.ToSingle(_config.RadarScale) * i)),
new Rectangle((nodeType % 24) * 16, (int)(nodeType / 24) * 16, 16, 16), Color.White, 0.0f, new Vector2(16, 16), Convert.ToSingle(_config.RadarScale), SpriteEffects.None, 1.0f);
++i;
}
}
}
private static void DrawRectangle(Rectangle rect, Color color)
{
Game1.InUIMode(() =>
{
Game1.spriteBatch.Draw(_pixelTexture, new Rectangle(rect.Left + 3, rect.Top, rect.Width - 6, 3), color);
Game1.spriteBatch.Draw(_pixelTexture, new Rectangle(rect.Left + 3, rect.Bottom - 3, rect.Width - 6, 3), color);
Game1.spriteBatch.Draw(_pixelTexture, new Rectangle(rect.Left, rect.Top + 3, 3, rect.Height - 6), color);
Game1.spriteBatch.Draw(_pixelTexture, new Rectangle(rect.Right - 3, rect.Top + 3, 3, rect.Height - 6), color);
});
}
private static void DrawSprite(Rectangle rect, Color color, int spriteIndex, bool flipped)
{
Game1.InUIMode(() =>
{
Game1.spriteBatch.Draw(Game1.objectSpriteSheet, rect, new Rectangle((spriteIndex % 24) * 16, (int)(spriteIndex / 24) * 16, 16, 16), color, 0.0f, Vector2.Zero, flipped ? SpriteEffects.FlipHorizontally : SpriteEffects.None, 0.0f);
});
}
private static void DrawImage(Rectangle rect, Color color, int spriteIndex, bool flipped)
{
Game1.InUIMode(() =>
{
Game1.spriteBatch.Draw(_imageTexture, rect, null, color, 0.0f, Vector2.Zero, flipped ? SpriteEffects.FlipHorizontally : SpriteEffects.None, 0.0f);
});
}
private void OnButtonPressed(object sender, ButtonPressedEventArgs e)
{
if (_config.ToggleNodeRadar.JustPressed())
{
_config.NodeRadar = !_config.NodeRadar;
Game1.addHUDMessage(new HUDMessage($"Node Radar toggled {(_config.NodeRadar ? "on" : "off")}", 2));
Helper.WriteConfig(_config);
Helper.Events.Display.RenderingHud -= OnRenderingHud;
if (_config.NodeRadar) Helper.Events.Display.RenderingHud += OnRenderingHud;
}
else if (_config.CycleAlpha.JustPressed())
{
_config.HighlightAlpha = Math.Round((_config.HighlightAlpha + 0.15M) % 1.0M, 2);
Game1.addHUDMessage(new HUDMessage($"Highlight alpha now {_config.HighlightAlpha}", 2));
Helper.WriteConfig(_config);
}
else if (_config.ToggleTint.JustPressed())
{
_config.HighlightUsesStoneTint = !_config.HighlightUsesStoneTint;
Game1.addHUDMessage(new HUDMessage("Highlight using stone tint toggled " + (_config.HighlightUsesStoneTint ? "on" : "off"), 2));
Helper.WriteConfig(_config);
}
else if (_config.ToggleHighlightTypeKey.JustPressed())
{
if (_config.HighlightTypes.Contains(HighlightType.Rectangle))
{
if (_config.HighlightTypes.Contains(HighlightType.Image))
ToggleHighlightType(HighlightType.Sprite);
ToggleHighlightType(HighlightType.Image);
}
ToggleHighlightType(HighlightType.Rectangle);
Game1.addHUDMessage(_config.HighlightTypes.Count > 0
? new HUDMessage("Ladder highlight: " + string.Join(" + ", _config.HighlightTypes), 2)
: new HUDMessage("Ladder highlight disabled", 2));
Helper.WriteConfig(_config);
Helper.Events.Display.RenderedWorld -= OnRenderedWorld;
if (_config.HighlightTypes.Count > 0) Helper.Events.Display.RenderedWorld += OnRenderedWorld;
}
else if (_config.ToggleShaftsKey.JustPressed())
{
_config.ForceShafts = !_config.ForceShafts;
Game1.addHUDMessage(_config.ForceShafts
? new HUDMessage("Force shafts toggled on", 2)
: new HUDMessage("Force shafts toggled off", 2));
Helper.WriteConfig(_config);
}
}
private static void ToggleHighlightType(HighlightType type)
{
if (_config.HighlightTypes.Contains(type)) _config.HighlightTypes.Remove(type);
else _config.HighlightTypes.Add(type);
}
class Stone
{
public Stone(Object obj)
{
SpriteIndex = obj.ParentSheetIndex;
this.obj = obj;
}
public Object obj { get; }
public int SpriteIndex { get; }
}
class LadderStone : Stone
{
public LadderStone(Object obj) : base(obj)
{
BoundingBox = obj.GetBoundingBox();
Flipped = obj.Flipped;
Tint = GetObjectSpriteAverageColor(SpriteIndex);
}
public Rectangle BoundingBox { get; }
public bool Flipped { get; }
public Color Tint { get; }
}
/// <summary>
/// Gets the average color of a particular stone from the center 8x8 square of its pixels.
/// </summary>
/// <param name="spriteIndex">Index of sprite to get color of from SDV object sprite sheet</param>
/// <returns>average color of given sprite</returns>
private static Color GetObjectSpriteAverageColor(int spriteIndex)
{
Color[] colors = new Color[8 * 8];
Game1.objectSpriteSheet.GetData(0, new Rectangle((spriteIndex % 24) * 16 + 4, (int)(spriteIndex / 24) * 16 + 4, 8, 8), colors, 0, 8 * 8);
var average = new Color(Convert.ToByte(colors.Sum(c => c.R) / colors.Count()), Convert.ToByte(colors.Sum(c => c.G) / colors.Count()), Convert.ToByte(colors.Sum(c => c.B) / colors.Count()));
return average;
}
/// <summary>
/// Initialize GMCM settings when the game launches.
/// </summary>
private void OnGameLaunched(object sender, GameLaunchedEventArgs e)
{
var gmcmApi = Helper.ModRegistry.GetApi<IGenericModConfigMenuApi>("spacechase0.GenericModConfigMenu");
if (gmcmApi != null)
{
RegisterModConfigMenu(gmcmApi);
}
else
{
Monitor.Log("Failed to access GMCM API. Is the Generic Mod Config Menu installed?", LogLevel.Warn);
}
}
/// <summary>
/// Register the mod configuration menu using GMCM.
/// </summary>
private void RegisterModConfigMenu(IGenericModConfigMenuApi api)
{
api.Register(
mod: ModManifest,
reset: ResetConfig,
save: SaveConfig
);
AddOptions(api);
}
private void ResetConfig()
{
_config = new ModConfig();
UpdateHudDisplay();
}
private void SaveConfig()
{
Helper.WriteConfig(_config);
UpdateHudDisplay();
}
/// <summary>
/// Add all mod configuration options to the GMCM menu.
/// </summary>
private void AddOptions(IGenericModConfigMenuApi api)
{
api.AddKeybindList(
mod: ModManifest,
getValue: () => _config.ToggleShaftsKey,
setValue: value => _config.ToggleShaftsKey = value,
name: () => Helper.Translation.Get("toggle-shafts-key"),
tooltip: () => Helper.Translation.Get("toggle-shafts-key-tooltip")
);
api.AddBoolOption(
mod: ModManifest,
getValue: () => _config.ForceShafts,
setValue: value => _config.ForceShafts = value,
name: () => Helper.Translation.Get("force-shafts"),
tooltip: () => Helper.Translation.Get("force-shafts-tooltip")
);
api.AddKeybindList(
mod: ModManifest,
getValue: () => _config.ToggleHighlightTypeKey,
setValue: value => _config.ToggleHighlightTypeKey = value,
name: () => Helper.Translation.Get("toggle-highlight-type-key"),
tooltip: () => Helper.Translation.Get("toggle-highlight-type-key-tooltip")
);
api.AddTextOption(
mod: ModManifest,
getValue: () => string.Join(", ", _config.HighlightTypes),
setValue: value => _config.HighlightTypes = new HashSet<HighlightType>(value.Split(',').Select(Enum.Parse<HighlightType>)),
name: () => Helper.Translation.Get("highlight-types"),
tooltip: () => Helper.Translation.Get("highlight-types-tooltip"),
allowedValues: Enum.GetNames(typeof(HighlightType))
);
api.AddNumberOption(
mod: ModManifest,
getValue: () => _config.HighlightRectangleRGBA.R,
setValue: value => _config.HighlightRectangleRGBA = new Color(value, _config.HighlightRectangleRGBA.G, _config.HighlightRectangleRGBA.B, _config.HighlightRectangleRGBA.A),
min: 0,
max: 255,
name: () => Helper.Translation.Get("highlight-color-r"),
tooltip: () => Helper.Translation.Get("highlight-color-r-tooltip")
);
api.AddNumberOption(
mod: ModManifest,
getValue: () => _config.HighlightRectangleRGBA.G,
setValue: value => _config.HighlightRectangleRGBA = new Color(_config.HighlightRectangleRGBA.R, value, _config.HighlightRectangleRGBA.B, _config.HighlightRectangleRGBA.A),
min: 0,
max: 255,
name: () => Helper.Translation.Get("highlight-color-g"),
tooltip: () => Helper.Translation.Get("highlight-color-g-tooltip")
);
api.AddNumberOption(
mod: ModManifest,
getValue: () => _config.HighlightRectangleRGBA.B,
setValue: value => _config.HighlightRectangleRGBA = new Color(_config.HighlightRectangleRGBA.R, _config.HighlightRectangleRGBA.G, value, _config.HighlightRectangleRGBA.A),
min: 0,
max: 255,
name: () => Helper.Translation.Get("highlight-color-b"),
tooltip: () => Helper.Translation.Get("highlight-color-b-tooltip")
);
api.AddNumberOption(
mod: ModManifest,
getValue: () => _config.HighlightRectangleRGBA.A,
setValue: value => _config.HighlightRectangleRGBA = new Color(_config.HighlightRectangleRGBA.R, _config.HighlightRectangleRGBA.G, _config.HighlightRectangleRGBA.B, value),
min: 0,
max: 255,
name: () => Helper.Translation.Get("highlight-color-a"),
tooltip: () => Helper.Translation.Get("highlight-color-a-tooltip")
);
api.AddKeybindList(
mod: ModManifest,
getValue: () => _config.ToggleTint,
setValue: value => {
_config.ToggleTint = value;
_config.HighlightUsesStoneTint = !_config.HighlightUsesStoneTint;
Helper.WriteConfig(_config);
},
name: () => Helper.Translation.Get("toggle-tint-key"),
tooltip: () => Helper.Translation.Get("toggle-tint-key-tooltip")
);
api.AddTextOption(
mod: ModManifest,
getValue: () => _config.HighlightImageFilename,
setValue: value => _config.HighlightImageFilename = value,
name: () => Helper.Translation.Get("highlight-image-filename"),
tooltip: () => Helper.Translation.Get("highlight-image-filename-tooltip")
);
api.AddNumberOption(
mod: ModManifest,
getValue: () => (float)_config.HighlightAlpha,
setValue: value => _config.HighlightAlpha = (decimal)value,
min: 0.0f,
max: 1.0f,
interval: 0.05f,
name: () => Helper.Translation.Get("highlight-alpha"),
tooltip: () => Helper.Translation.Get("highlight-alpha-tooltip")
);
api.AddKeybindList(
mod: ModManifest,
getValue: () => _config.CycleAlpha,
setValue: value => _config.CycleAlpha = value,
name: () => Helper.Translation.Get("cycle-alpha-key"),
tooltip: () => Helper.Translation.Get("cycle-alpha-key-tooltip")
);
api.AddBoolOption(
mod: ModManifest,
getValue: () => _config.HighlightUsesStoneTint,
setValue: value => _config.HighlightUsesStoneTint = value,
name: () => Helper.Translation.Get("highlight-uses-stone-tint"),
tooltip: () => Helper.Translation.Get("highlight-uses-stone-tint-tooltip")
);
api.AddBoolOption(
mod: ModManifest,
getValue: () => _config.NodeRadar,
setValue: value => _config.NodeRadar = value,
name: () => Helper.Translation.Get("node-radar"),
tooltip: () => Helper.Translation.Get("node-radar-tooltip")
);
api.AddKeybindList(
mod: ModManifest,
getValue: () => _config.ToggleNodeRadar,
setValue: value => _config.ToggleNodeRadar = value,
name: () => Helper.Translation.Get("toggle-node-radar-key"),
tooltip: () => Helper.Translation.Get("toggle-node-radar-key-tooltip")
);
api.AddNumberOption(
mod: ModManifest,
getValue: () => (float)_config.RadarScale,
setValue: value => _config.RadarScale = (decimal)value,
min: 0.1f,
max: 5.0f,
interval: 0.1f,
name: () => Helper.Translation.Get("radar-scale"),
tooltip: () => Helper.Translation.Get("radar-scale-tooltip")
);
}
private void UpdateHudDisplay()
{
Helper.Events.Display.RenderingHud -= OnRenderingHud;
if (_config.NodeRadar)
{
Helper.Events.Display.RenderingHud += OnRenderingHud;
}
}
}
}