forked from hajsdfgj/modular-overhaul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVanillaSkill.cs
340 lines (285 loc) · 12 KB
/
VanillaSkill.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
#region global using directives
#pragma warning disable SA1200 // Using directives should be placed correctly
global using Skill = DaLion.Overhaul.Modules.Professions.VanillaSkill;
#pragma warning restore SA1200 // Using directives should be placed correctly
#endregion global using directives
namespace DaLion.Overhaul.Modules.Professions;
#region using directives
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Ardalis.SmartEnum;
using DaLion.Overhaul.Modules.Professions.Configs;
using DaLion.Overhaul.Modules.Professions.Extensions;
using DaLion.Shared.Extensions;
using DaLion.Shared.Extensions.Collections;
using DaLion.Shared.Extensions.Stardew;
using Microsoft.Xna.Framework;
using StardewValley;
using StardewValley.Menus;
#endregion using directives
/// <summary>Represents a vanilla skill.</summary>
/// <remarks>
/// Despite including a <see cref="Ardalis.SmartEnum"/> entry for the Luck skill, that skill is treated specially
/// by its own implementation (see <see cref="LuckSkill"/>).
/// </remarks>
public class VanillaSkill : SmartEnum<Skill>, ISkill
{
#region enum entries
/// <summary>The Farming skill.</summary>
public static readonly Skill Farming = new("Farming", Farmer.farmingSkill);
/// <summary>The Fishing skill.</summary>
public static readonly Skill Fishing = new("Fishing", Farmer.fishingSkill);
/// <summary>The Foraging skill.</summary>
public static readonly Skill Foraging = new("Foraging", Farmer.foragingSkill);
/// <summary>The Mining skill.</summary>
public static readonly Skill Mining = new("Mining", Farmer.miningSkill);
/// <summary>The Combat skill.</summary>
public static readonly Skill Combat = new("Combat", Farmer.combatSkill);
/// <summary>The Luck skill, if loaded.</summary>
[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1401:FieldsMustBePrivate", Justification = "Enum value must be field.")]
public static Skill Luck = new("Luck", Farmer.luckSkill);
#endregion enum entries
/// <summary>Initializes a new instance of the <see cref="Skill"/> class.</summary>
/// <param name="name">The skill name.</param>
/// <param name="value">The skill index.</param>
protected VanillaSkill(string name, int value)
: base(name, value)
{
if (value == Farmer.luckSkill)
{
this.StringId = null!; // set in child class
this.DisplayName = null!; // set in child class
return;
}
this.StringId = this.Name;
this.DisplayName = value switch
{
Farmer.farmingSkill => Game1.content.LoadString("Strings\\StringsFromCSFiles:SkillsPage.cs.11604"),
Farmer.fishingSkill => Game1.content.LoadString("Strings\\StringsFromCSFiles:SkillsPage.cs.11607"),
Farmer.foragingSkill => Game1.content.LoadString("Strings\\StringsFromCSFiles:SkillsPage.cs.11606"),
Farmer.miningSkill => Game1.content.LoadString("Strings\\StringsFromCSFiles:SkillsPage.cs.11605"),
Farmer.combatSkill => Game1.content.LoadString("Strings\\StringsFromCSFiles:SkillsPage.cs.11608"),
_ => string.Empty,
};
for (var i = 0; i < 6; i++)
{
this.Professions.Add(Profession.FromValue((value * 6) + i));
}
this.ProfessionPairs[-1] = new ProfessionPair(this.Professions[0], this.Professions[1], null, 5);
this.ProfessionPairs[this.Professions[0].Id] =
new ProfessionPair(this.Professions[2], this.Professions[3], this.Professions[0], 10);
this.ProfessionPairs[this.Professions[1].Id] =
new ProfessionPair(this.Professions[4], this.Professions[5], this.Professions[1], 10);
}
/// <summary>Gets an enumeration of the vanilla <see cref="Skill"/> instances.</summary>
/// <remarks>In other words, excludes <see cref="LuckSkill"/>.</remarks>
public static IEnumerable<Skill> ListVanilla => List.Except(Luck.Collect());
/// <inheritdoc />
public string StringId { get; protected set; }
/// <inheritdoc />
public string DisplayName { get; protected set; }
/// <inheritdoc />
public int CurrentExp => Game1.player.experiencePoints[this.Value];
/// <inheritdoc />
public int CurrentLevel => Game1.player.GetUnmodifiedSkillLevel(this.Value);
/// <inheritdoc />
public virtual int MaxLevel => this.CanGainPrestigeLevels() ? 20 : 10;
/// <inheritdoc />
public float BaseExperienceMultiplier => ProfessionsModule.Config.Experience.Multipliers[this.Name];
/// <inheritdoc />
public IEnumerable<int> NewLevels =>
Game1.player.newLevels
.Where(p => p.X == this.Value)
.Select(p => p.Y);
/// <inheritdoc />
public IList<IProfession> Professions { get; } = new List<IProfession>();
/// <inheritdoc />
public IDictionary<int, ProfessionPair> ProfessionPairs { get; } = new Dictionary<int, ProfessionPair>();
/// <summary>Gets the range of indices corresponding to vanilla skills.</summary>
/// <returns>A <see cref="IEnumerable{T}"/> of all vanilla skill indices.</returns>
public static IEnumerable<int> GetRange()
{
return Enumerable.Range(0, 5);
}
/// <inheritdoc />
public void AddExperience(int amount)
{
Game1.player.experiencePoints[this] = Math.Min(this.CurrentExp + amount, ISkill.ExperienceCurve[this.MaxLevel]);
}
/// <summary>Sets the experience points for this skill.</summary>
/// <param name="experience">The new amount of experience points.</param>
public void SetExperience(int experience)
{
Game1.player.experiencePoints[this] = experience;
}
/// <inheritdoc />
public void SetLevel(int level)
{
level = Math.Min(level, this.MaxLevel);
var farmer = Game1.player;
for (var l = this.CurrentLevel + 1; l <= level; l++)
{
var point = new Point(this.Value, level);
if (!farmer.newLevels.Contains(point))
{
farmer.newLevels.Add(point);
}
}
this
.When(Farming).Then(() => farmer.farmingLevel.Value = level)
.When(Fishing).Then(() => farmer.fishingLevel.Value = level)
.When(Foraging).Then(() => farmer.foragingLevel.Value = level)
.When(Mining).Then(() => farmer.miningLevel.Value = level)
.When(Combat).Then(() => farmer.combatLevel.Value = level)
.When(Luck).Then(() => farmer.luckLevel.Value = level);
Game1.player.experiencePoints[this] = ISkill.ExperienceCurve[level];
}
/// <inheritdoc />
public void Reset()
{
var farmer = Game1.player;
// reset skill level
this
.When(Farming).Then(() => farmer.farmingLevel.Value = 0)
.When(Fishing).Then(() => farmer.fishingLevel.Value = 0)
.When(Foraging).Then(() => farmer.foragingLevel.Value = 0)
.When(Mining).Then(() => farmer.miningLevel.Value = 0)
.When(Combat).Then(() => farmer.combatLevel.Value = 0)
.When(Luck).Then(() => farmer.luckLevel.Value = 0);
// reset new levels
for (var i = 0; i < farmer.newLevels.Count; i++)
{
var level = farmer.newLevels[i];
if (level.X == this.Value)
{
farmer.newLevels.Remove(level);
}
}
// reset skill experience
farmer.experiencePoints[this] = 0;
// forget recipes
if (ProfessionsModule.Config.Prestige.ForgetRecipesOnSkillReset && this < Luck)
{
this.ForgetRecipes();
}
// revalidate health
if (this.Value == Farmer.combatSkill)
{
LevelUpMenu.RevalidateHealth(farmer);
}
}
/// <inheritdoc />
public void ForgetRecipes(bool saveForRecovery = true)
{
if (this.Value == Farmer.luckSkill)
{
return;
}
var farmer = Game1.player;
var forgottenRecipesDict = farmer.Read(DataKeys.ForgottenRecipesDict)
.ParseDictionary<string, int>();
// remove associated crafting recipes
var knownCraftingRecipes =
farmer.craftingRecipes.Keys.ToDictionary(
key => key,
key => farmer.craftingRecipes[key]);
foreach (var (key, value) in CraftingRecipe.craftingRecipes)
{
if (!value.SplitWithoutAllocation('/')[4].Contains(this.StringId, StringComparison.Ordinal) ||
!knownCraftingRecipes.ContainsKey(key))
{
continue;
}
if (saveForRecovery)
{
if (!forgottenRecipesDict.TryAdd(key, knownCraftingRecipes[key]))
{
forgottenRecipesDict[key] += knownCraftingRecipes[key];
}
}
farmer.craftingRecipes.Remove(key);
}
// remove associated cooking recipes
var knownCookingRecipes =
farmer.cookingRecipes.Keys.ToDictionary(
key => key,
key => farmer.cookingRecipes[key]);
foreach (var (key, value) in CraftingRecipe.cookingRecipes)
{
if (!value.SplitWithoutAllocation('/')[3].Contains(this.StringId, StringComparison.Ordinal) ||
!knownCookingRecipes.ContainsKey(key))
{
continue;
}
if (saveForRecovery)
{
if (!forgottenRecipesDict.TryAdd(key, knownCookingRecipes[key]))
{
forgottenRecipesDict[key] += knownCookingRecipes[key];
}
}
farmer.cookingRecipes.Remove(key);
}
if (saveForRecovery)
{
farmer.Write(DataKeys.ForgottenRecipesDict, forgottenRecipesDict.Stringify());
}
}
/// <inheritdoc />
public bool CanGainPrestigeLevels()
{
return ProfessionsModule.Config.Prestige.Mode == PrestigeConfig.PrestigeMode.Streamlined ||
(ProfessionsModule.Config.Prestige.Mode == PrestigeConfig.PrestigeMode.Standard &&
((ISkill)this).AcquiredProfessions.Length >= 4) ||
(ProfessionsModule.Config.Prestige.Mode == PrestigeConfig.PrestigeMode.Challenge &&
Game1.player.HasAllProfessions());
}
/// <inheritdoc />
public virtual void Revalidate()
{
var maxLevel = this.MaxLevel;
if (this.CurrentLevel > maxLevel)
{
this.SetLevel(this.MaxLevel);
}
if (this.CurrentLevel == maxLevel && this.CurrentExp > ISkill.ExperienceCurve[maxLevel])
{
this.SetExperience(ISkill.ExperienceCurve[maxLevel]);
return;
}
var expectedLevel = 0;
var level = 1;
while (level <= maxLevel && this.CurrentExp >= ISkill.ExperienceCurve[level])
{
level++;
expectedLevel++;
}
if (this.CurrentLevel == expectedLevel)
{
Log.D($"{this} skill's level has the expected value.");
return;
}
var farmer = Game1.player;
if (this.CurrentLevel < expectedLevel)
{
Log.W(
$"{this} skill's ({this.CurrentLevel}) is below the expected value ({expectedLevel}). New levels will be added to correct the difference.");
for (var levelUp = this.CurrentLevel + 1; levelUp <= expectedLevel; levelUp++)
{
var point = new Point(this, levelUp);
if (!farmer.newLevels.Contains(point))
{
farmer.newLevels.Add(point);
}
}
}
else
{
Log.W(
$"{this} skill's current level ({this.CurrentLevel}) is above the expected value ({expectedLevel}). The current level and experience will be downgraded to correct the difference.");
this.SetExperience(ISkill.ExperienceCurve[expectedLevel]);
}
this.SetLevel(expectedLevel);
}
}