-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathItemEnchantManager.cs
More file actions
532 lines (456 loc) · 22.1 KB
/
ItemEnchantManager.cs
File metadata and controls
532 lines (456 loc) · 22.1 KB
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
using Maple2.Model.Enum;
using Maple2.Model.Error;
using Maple2.Model.Game;
using Maple2.Model.Metadata;
using Maple2.Server.Game.Packets;
using Maple2.Server.Game.Session;
using Maple2.Server.Game.Util;
using Serilog;
namespace Maple2.Server.Game.Manager;
public class ItemEnchantManager {
private const int MaxRate = 100;
private const int MaxFodderRate = 30;
private const int MaxExp = 10000;
private const int ChargeRate = 1;
private static readonly int[] RequireFodder = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 3, 3, 4];
private static readonly int[] GainExp = [10000, 10000, 10000, 5000, 5000, 5000, 2500, 2500, 2500, 2000, 3334, 2000, 2000, 1250, 1250];
private static readonly int[] SuccessRate = [100, 100, 100, 95, 90, 80, 70, 60, 50, 40, 30, 20, 15, 10, 5];
private static readonly int[] FodderRate = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 7, 5, 4, 2];
private static readonly int[] FailCharge = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 4, 5];
private static readonly IngredientInfo[][] PeachyCost = new IngredientInfo[15][];
// TODO: Dynamic catalysts
static ItemEnchantManager() {
PeachyCost[0] = Build(802, 4, 20); // 1x
PeachyCost[1] = Build(802, 4, 20); // 1x
PeachyCost[2] = Build(802, 4, 20); // 1x
PeachyCost[3] = Build(579, 2, 15); // 2x
PeachyCost[4] = Build(611, 2, 16); // 2x
PeachyCost[5] = Build(872, 4, 21); // 2x
PeachyCost[6] = Build(604, 2, 15); // 4x
PeachyCost[7] = Build(826, 3, 21); // 4x
PeachyCost[8] = Build(1133, 3, 29); // 4x
PeachyCost[9] = Build(2238, 6, 31); // 5x
PeachyCost[10] = Build(4828, 13, 78); // 3x
PeachyCost[11] = Build(6148, 16, 66); // 5x
PeachyCost[12] = Build(7347, 19, 79); // 5x
PeachyCost[13] = Build(6374, 17, 69); // 8x
PeachyCost[14] = Build(7784, 20, 84); // 8x
IngredientInfo[] Build(int onyx, int chaosOnyx, int crystalFragment) {
return [
new IngredientInfo(ItemTag.Onix, onyx),
new IngredientInfo(ItemTag.ChaosOnix, chaosOnyx),
new IngredientInfo(ItemTag.CrystalPiece, crystalFragment),
];
}
}
private readonly GameSession session;
private readonly ILogger logger = Log.Logger.ForContext<ItemEnchantManager>();
public EnchantType Type { get; private set; }
private Item? upgradeItem;
private readonly List<IngredientInfo> catalysts;
private readonly Dictionary<long, Item> fodders;
private readonly Dictionary<BasicAttribute, BasicOption> attributeDeltas;
private readonly EnchantRates rates;
private int fodderWeight;
private int useCharges;
private EnchantResult enchantResult;
// Limit break values only
private long mesoCost;
private Item? limitBreakItemUpgrade;
private bool upgraded;
public ItemEnchantManager(GameSession session) {
this.session = session;
catalysts = [];
fodders = new Dictionary<long, Item>();
attributeDeltas = new Dictionary<BasicAttribute, BasicOption>();
rates = new EnchantRates();
}
public void Reset() {
Type = EnchantType.None;
upgradeItem = null;
catalysts.Clear();
fodders.Clear();
attributeDeltas.Clear();
rates.Clear();
fodderWeight = 0;
useCharges = 0;
enchantResult = EnchantResult.None;
mesoCost = 0;
limitBreakItemUpgrade = null;
upgraded = false;
}
public bool StageItem(EnchantType enchantType, long itemUid) {
if (enchantType is not (EnchantType.Ophelia or EnchantType.Peachy)) {
return false;
}
Item? item = session.Item.GetGear(itemUid);
if (item == null || !item.Metadata.Limit.EnableEnchant) {
session.Send(ItemEnchantPacket.Error(ItemEnchantError.s_itemenchant_invalid_item));
NpcTalkEvent(ScriptEventType.EnchantFail, ItemEnchantError.s_itemenchant_invalid_item);
return false;
}
Reset();
Type = enchantType;
upgradeItem = item;
// Ensure this item has an Enchant field set.
upgradeItem.Enchant ??= new ItemEnchant();
int enchants = Math.Clamp(upgradeItem.Enchant.Enchants, 0, 14);
int minFodder = RequireFodder[enchants];
foreach (IngredientInfo ingredient in GetRequiredCatalysts()) {
catalysts.Add(ingredient);
}
foreach ((BasicAttribute attribute, BasicOption option) in GetEnchant(session, upgradeItem).BasicOptions) {
attributeDeltas[attribute] = option;
}
switch (Type) {
case EnchantType.Ophelia:
rates.Success = SuccessRate[enchants];
break;
case EnchantType.Peachy:
rates.Success = MaxRate;
break;
}
NpcTalkEvent(ScriptEventType.EnchantSelect);
session.Send(ItemEnchantPacket.StageItem(Type, upgradeItem, catalysts, attributeDeltas, rates, minFodder));
return true;
}
public bool UpdateFodder(long itemUid, bool add) {
if (upgradeItem == null) {
return false;
}
int enchants = Math.Clamp(upgradeItem.Enchant?.Enchants ?? 0, 0, 14);
if (FodderRate[enchants] <= 0) {
return false; // If adding fodder does not improve rate, restrict it.
}
if (add) {
// Prevent adding more fodder if it won't help.
if (Type is EnchantType.Ophelia && rates.Total >= MaxRate) {
NpcTalkEvent(ScriptEventType.EnchantFail, ItemEnchantError.max_fodder);
return false;
}
// Can't go over 30% rate with fodders
if (Type is EnchantType.Ophelia && fodders.Count >= RequireFodder[enchants] && rates.Success + rates.Fodder >= MaxFodderRate) {
NpcTalkEvent(ScriptEventType.EnchantFail, ItemEnchantError.max_fodder);
return false;
}
// Cannot add the same fodder twice.
if (fodders.ContainsKey(itemUid)) {
return false;
}
Item? item = session.Item.Inventory.Get(itemUid);
if (item == null) {
session.Send(ItemEnchantPacket.Error(ItemEnchantError.s_itemenchant_lack_ingredient));
NpcTalkEvent(ScriptEventType.EnchantFail, ItemEnchantError.s_itemenchant_lack_ingredient);
return false;
}
if (!IsValidFodder(upgradeItem, item)) {
logger.Debug("Can't add fodder, invalid item: {Id}", item.Id);
return false;
}
fodders.Add(itemUid, item);
fodderWeight += GetFodderWeight(upgradeItem, item);
} else {
if (!fodders.Remove(itemUid, out Item? removed)) {
return false;
}
fodderWeight -= GetFodderWeight(upgradeItem, removed);
}
switch (Type) {
case EnchantType.Ophelia:
int extra = fodderWeight - RequireFodder[enchants];
int maxFodder = Math.Max(0, MaxFodderRate - rates.Success);
rates.Fodder = Math.Clamp(extra * FodderRate[enchants], 0, maxFodder);
// Recompute charges in case we are over max.
SetCharges(useCharges);
session.Send(ItemEnchantPacket.UpdateCharges(fodders.Keys, useCharges, fodderWeight, rates));
break;
case EnchantType.Peachy:
session.Send(ItemEnchantPacket.UpdateFodder(fodders.Keys));
break;
}
return true;
}
public bool UpdateCharges(int count) {
if (upgradeItem == null || count < 0 || count > upgradeItem.Enchant?.Charges) {
return false;
}
SetCharges(count);
session.Send(ItemEnchantPacket.UpdateCharges(fodders.Keys, useCharges, fodderWeight, rates));
return true;
}
public bool Enchant(long itemUid) {
Item? item = session.Item.GetGear(itemUid);
if (item == null || upgradeItem == null || upgradeItem.Uid != item.Uid) {
return false;
}
upgradeItem.Enchant ??= new ItemEnchant();
int enchants = Math.Clamp(upgradeItem.Enchant.Enchants, 0, 14);
if (fodderWeight < RequireFodder[enchants]) {
NpcTalkEvent(ScriptEventType.EnchantFail, ItemEnchantError.not_enough_fodder);
return false;
}
if (!ConsumeMaterial()) {
NpcTalkEvent(ScriptEventType.EnchantFail, ItemEnchantError.s_itemenchant_lack_ingredient);
return false;
}
void ApplyEnchantStats() {
// GetBasicOptions() again to ensure rates match those in table.
// This *MUST* be called before incrementing Enchants.
foreach ((BasicAttribute attribute, BasicOption option) in GetEnchant(session, upgradeItem).BasicOptions) {
if (upgradeItem.Enchant.BasicOptions.TryGetValue(attribute, out BasicOption existing)) {
upgradeItem.Enchant.BasicOptions[attribute] = option + existing;
} else {
upgradeItem.Enchant.BasicOptions[attribute] = option;
}
}
upgradeItem.Enchant.Enchants++;
}
switch (Type) {
case EnchantType.Ophelia:
float roll = Random.Shared.NextSingle() * MaxRate;
int totalRate = rates.Total;
enchantResult = roll < totalRate ? EnchantResult.Success : EnchantResult.Fail;
logger.Debug("Enchant result: {Roll} / {Total} = {Result}", roll, totalRate, enchantResult.ToString());
if (enchantResult == EnchantResult.Success) {
ApplyEnchantStats();
session.ConditionUpdate(ConditionType.enchant_result, codeLong: (int) EnchantResult.Success, targetLong: upgradeItem.Enchant.Enchants);
session.Send(ItemEnchantPacket.Success(upgradeItem, attributeDeltas));
ChatUtil.SendEnchantAnnouncement(session, upgradeItem);
} else {
session.ConditionUpdate(ConditionType.enchant_result, codeLong: (int) EnchantResult.Fail, targetLong: upgradeItem.Enchant.Enchants);
upgradeItem.Enchant.Charges += FailCharge[enchants];
session.Send(ItemEnchantPacket.Failure(upgradeItem, FailCharge[enchants]));
}
return true;
case EnchantType.Peachy:
upgradeItem.Enchant.EnchantExp += GainExp[enchants];
if (upgradeItem.Enchant.EnchantExp >= MaxExp) {
upgradeItem.Enchant.EnchantExp = 0;
ApplyEnchantStats();
session.ConditionUpdate(ConditionType.enchant_result, codeLong: (int) EnchantResult.Success, targetLong: upgradeItem.Enchant.Enchants);
session.Send(ItemEnchantPacket.Success(upgradeItem, attributeDeltas));
session.Send(ItemEnchantPacket.UpdateExp(itemUid, 0));
ChatUtil.SendEnchantAnnouncement(session, upgradeItem);
Reset();
} else {
// Just clear the fodder as the same details are reused.
fodders.Clear();
fodderWeight = 0;
session.Send(ItemEnchantPacket.UpdateExp(itemUid, upgradeItem.Enchant.EnchantExp));
}
return true;
default:
return false;
}
}
// session.Item must be locked before calling.
// Consumes materials and charges for this upgrade.
private bool ConsumeMaterial() {
if (upgradeItem?.Enchant == null) {
return false;
}
lock (session.Item) {
// Validate charges, fodder, catalyst
if (upgradeItem.Enchant.Charges < useCharges) {
return false;
}
foreach ((long fodderUid, _) in fodders) {
Item? item = session.Item.Inventory.Get(fodderUid);
if (item == null) {
session.Send(ItemEnchantPacket.Error(ItemEnchantError.s_itemenchant_lack_ingredient));
return false;
}
}
// Note: All validation should be above this point.
if (!session.Item.Inventory.Consume(catalysts)) {
session.Send(ItemEnchantPacket.Error(ItemEnchantError.s_itemenchant_lack_ingredient));
return false;
}
foreach ((long fodderUid, _) in fodders) {
if (!session.Item.Inventory.Consume(fodderUid, 1)) {
logger.Fatal("Failed to consume item {ItemUid}", fodderUid);
throw new InvalidOperationException($"Fatal: Consuming item: {fodderUid}");
}
}
upgradeItem.Enchant.Charges -= useCharges;
return true;
}
}
private IEnumerable<IngredientInfo> GetRequiredCatalysts() {
if (upgradeItem == null) {
yield break;
}
int enchants = upgradeItem.Enchant?.Enchants ?? 0;
IEnumerable<IngredientInfo> costs = Type == EnchantType.Peachy ? PeachyCost[enchants] : Core.Formulas.Enchant.GetEnchantCost(upgradeItem);
foreach (IngredientInfo cost in costs) {
yield return cost;
}
}
public static ItemEnchant GetEnchant(GameSession session, Item upgradeItem, int target = -1) {
return GetEnchant(session.ServerTableMetadata.EnchantOptionTable, upgradeItem, target);
}
public static ItemEnchant GetEnchant(EnchantOptionTable table, Item upgradeItem, int target = -1) {
int enchantLevel = Math.Clamp(target >= 0 ? target : (upgradeItem.Enchant?.Enchants ?? 0) + 1, 1, 15);
var itemEnchant = new ItemEnchant();
EnchantOptionMetadata? entry = table.Entries.Values.FirstOrDefault(
e => e.Slot == upgradeItem.Type.Type &&
e.EnchantLevel == enchantLevel &&
e.Rarity == upgradeItem.Rarity &&
e.MinLevel <= upgradeItem.Metadata.Limit.Level &&
e.MaxLevel >= upgradeItem.Metadata.Limit.Level);
if (entry == null) {
return itemEnchant;
}
foreach (BasicAttribute attribute in entry.Attributes) {
itemEnchant.BasicOptions[attribute] = new BasicOption(entry.Rate);
}
itemEnchant.Enchants = enchantLevel;
return itemEnchant;
}
// Prevent user from using more charges than needed
private void SetCharges(int count) {
// Charges are only relevant to Ophelia.
if (Type != EnchantType.Ophelia) {
useCharges = 0;
return;
}
int rateWithoutCharges = Math.Clamp(rates.Total - rates.Charge, 0, MaxRate);
int maxCharges = (int) Math.Ceiling((MaxRate - rateWithoutCharges) / (float) ChargeRate);
useCharges = Math.Clamp(count, 0, maxCharges);
rates.Charge = Math.Clamp(useCharges * ChargeRate, 0, MaxRate);
}
private static int GetFodderWeight(Item item, Item fodder) {
if (!item.Type.IsDagger && !item.Type.IsThrowingStar) {
return 1;
}
// For Dagger/ThrowingStar, Toad's Toolkit is valued as 2 fodder.
return IsValidToolkit(item, fodder) ? 2 : 1;
}
private static bool IsValidFodder(Item item, Item fodder) {
return fodder.Id == item.Id && fodder.Rarity == item.Rarity || IsValidToolkit(item, fodder);
}
private static bool IsValidToolkit(Item item, Item fodder) {
return fodder.Metadata.Property.Tag switch {
ItemTag.EnchantJockerItemNormal => item.Rarity == 1,
ItemTag.EnchantJockerItemRare => item.Rarity == 2,
ItemTag.EnchantJockerItemElite => item.Rarity == 3,
ItemTag.EnchantJockerItemExcellent => item.Rarity == 4,
ItemTag.EnchantJockerItemLegendary => item.Rarity == 5,
ItemTag.EnchantJockerItemEpic => item.Rarity == 6,
_ => false,
};
}
public void NpcTalkEvent(ScriptEventType scriptEventType, ItemEnchantError error = ItemEnchantError.s_itemenchant_unknown_err) {
session.NpcScript?.EnchantResultScript(scriptEventType, enchantResult, error, upgradeItem?.Enchant?.Enchants ?? 0, (short) (upgradeItem?.Rarity ?? 0));
if (scriptEventType == ScriptEventType.EnchantComplete) {
Reset();
}
}
#region Limit Break
public bool StageLimitBreakItem(long itemUid) {
// Must happen due to this function triggering automatically upon upgrading. Without it, the animation does not play.
if (upgraded) {
upgraded = false;
return false;
}
Item? item = session.Item.GetGear(itemUid);
if (item == null || item.Metadata.Property.LimitBreakMaxLevel == 0 ||
(item.LimitBreak?.Level == 0 && item.Enchant?.Enchants < 15)) {
session.Send(LimitBreakPacket.Error(LimitBreakError.s_unlimited_enchant_err_invalid_item));
return false;
}
item.LimitBreak ??= new ItemLimitBreak();
if (item.LimitBreak.Level >= item.Metadata.Property.LimitBreakMaxLevel) {
session.Send(LimitBreakPacket.Error(LimitBreakError.s_unlimited_enchant_err_max_unlimited_grade));
return false;
}
Reset();
upgradeItem = item;
foreach (IngredientInfo ingredient in Core.Formulas.LimitBreak.GetCatalysts(item.LimitBreak.Level)) {
catalysts.Add(ingredient);
}
mesoCost = Core.Formulas.LimitBreak.MesoCost(item.LimitBreak.Level);
limitBreakItemUpgrade = item.Clone();
if (!session.ServerTableMetadata.UnlimitedEnchantOptionTable.Entries.TryGetValue(limitBreakItemUpgrade.Type.Type, out Dictionary<int, UnlimitedEnchantOptionTable.Option>? levelDictionary) ||
!levelDictionary.TryGetValue(limitBreakItemUpgrade.LimitBreak!.Level + 1, out UnlimitedEnchantOptionTable.Option? optionMetadata)) {
session.Send(LimitBreakPacket.Error(LimitBreakError.s_unlimited_enchant_err_invalid_item));
return false;
}
foreach ((BasicAttribute attribute, int value) in optionMetadata.Values) {
if (limitBreakItemUpgrade.LimitBreak.BasicOptions.TryGetValue(attribute, out BasicOption existing)) {
limitBreakItemUpgrade.LimitBreak.BasicOptions[attribute] = existing + new BasicOption(value);
} else {
limitBreakItemUpgrade.LimitBreak.BasicOptions[attribute] = new BasicOption(value);
}
}
foreach ((BasicAttribute attribute, float rate) in optionMetadata.Rates) {
if (limitBreakItemUpgrade.LimitBreak.BasicOptions.TryGetValue(attribute, out BasicOption existing)) {
limitBreakItemUpgrade.LimitBreak.BasicOptions[attribute] = existing + new BasicOption(rate);
} else {
limitBreakItemUpgrade.LimitBreak.BasicOptions[attribute] = new BasicOption(rate);
}
}
foreach ((SpecialAttribute attribute, int value) in optionMetadata.SpecialValues) {
if (limitBreakItemUpgrade.LimitBreak.SpecialOptions.TryGetValue(attribute, out SpecialOption existing)) {
limitBreakItemUpgrade.LimitBreak.SpecialOptions[attribute] = existing + new SpecialOption(value);
} else {
limitBreakItemUpgrade.LimitBreak.SpecialOptions[attribute] = new SpecialOption(value);
}
}
foreach ((SpecialAttribute attribute, float rate) in optionMetadata.SpecialRates) {
if (limitBreakItemUpgrade.LimitBreak.SpecialOptions.TryGetValue(attribute, out SpecialOption existing)) {
limitBreakItemUpgrade.LimitBreak.SpecialOptions[attribute] = existing + new SpecialOption(rate);
} else {
limitBreakItemUpgrade.LimitBreak.SpecialOptions[attribute] = new SpecialOption(rate);
}
}
limitBreakItemUpgrade.LimitBreak.Level++;
limitBreakItemUpgrade.Enchant!.Enchants = 0;
session.Send(LimitBreakPacket.StageItem(item, limitBreakItemUpgrade, mesoCost, catalysts));
return true;
}
public bool LimitBreakEnchant(long itemUid) {
Item? item = session.Item.GetGear(itemUid);
if (item == null || upgradeItem == null || limitBreakItemUpgrade?.LimitBreak == null || upgradeItem.Uid != item.Uid ||
item.Metadata.Property.LimitBreakMaxLevel == 0 ||
(item.LimitBreak?.Level == 0 && item.Enchant?.Enchants < 15)) {
session.Send(LimitBreakPacket.Error(LimitBreakError.s_unlimited_enchant_err_invalid_item));
return false;
}
item.LimitBreak ??= new ItemLimitBreak();
if (item.LimitBreak.Level >= item.Metadata.Property.LimitBreakMaxLevel) {
session.Send(LimitBreakPacket.Error(LimitBreakError.s_unlimited_enchant_err_max_unlimited_grade));
return false;
}
if (!ConsumeLimitBreakMaterial()) {
return false;
}
upgradeItem.LimitBreak = limitBreakItemUpgrade.LimitBreak.Clone();
upgradeItem.Enchant!.Enchants = 0;
session.Send(LimitBreakPacket.LimitBreak(upgradeItem));
if (upgradeItem.LimitBreak.Level == upgradeItem.Metadata.Property.LimitBreakMaxLevel) {
session.Send(LimitBreakPacket.Error(LimitBreakError.s_unlimited_enchant_err_max_unlimited_grade));
}
session.ConditionUpdate(ConditionType.unlimited_enchant);
upgraded = true;
return true;
}
private bool ConsumeLimitBreakMaterial() {
if (upgradeItem?.LimitBreak == null) {
return false;
}
lock (session.Item) {
if (!session.Item.Inventory.Consume(catalysts)) {
session.Send(LimitBreakPacket.Error(LimitBreakError.s_unlimited_enchant_err_lack_ingredient));
return false;
}
if (session.Currency.CanAddMeso(-mesoCost) != -mesoCost) {
session.Send(LimitBreakPacket.Error(LimitBreakError.s_unlimited_enchant_err_lack_meso));
return false;
}
session.Currency.Meso -= mesoCost;
return true;
}
}
#endregion
}