-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathLootModItem.cs
552 lines (482 loc) · 17.1 KB
/
LootModItem.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
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using Loot.Api.Core;
using Loot.Api.Ext;
using Loot.Api.Graphics;
using Loot.Hacks;
using Loot.IO;
using Terraria;
using Terraria.ModLoader;
using Terraria.ModLoader.IO;
namespace Loot
{
/// <summary>
/// Defines an item that may be modified by modifiers from mods
/// </summary>
public sealed class LootModItem : GlobalItem
{
private const int SAVE_VERSION = 14;
public static LootModItem GetInfo(Item item) => item.GetGlobalItem<LootModItem>();
public static List<Modifier> GetActivePool(Item item) => GetInfo(item).Modifiers?.Modifiers ?? new List<Modifier>();
public override bool InstancePerEntity => true;
public override bool CloneNewInstances => true;
public ModifierRarity Rarity { get; internal set; } // the current rarity
public FiniteModifierPool Modifiers { get; internal set; } // the current pool of mods.
public bool HasRolled { get; internal set; } // has rolled a pool
public bool SealedModifiers { get; internal set; } // are modifiers unchangeable
// Non saved
public bool JustTinkerModified { get; internal set; } // is just tinker modified: e.g. armor hacked
public bool SlottedInUI { get; internal set; } // is currently in cube UI slot
/// <summary>
/// Keeps track of if the particular item was activated ('delegated')
/// Specific usecase see CursedEffect and modifier
/// </summary>
public bool IsActivated { get; internal set; }
private void InvalidateRolls()
{
Rarity = mod.GetNullModifierRarity();
Modifiers = mod.GetNullModifierPool();
}
/// <summary>
/// Attempts to roll new modifiers
/// Has a set chance to hit a predefined pool of modifiers
/// </summary>
//public ModifierPool RollNewPool(ModifierContext ctx, RollingStrategyProperties rollingStrategyProperties = null)
//{
// if (rollingStrategyProperties == null)
// {
// rollingStrategyProperties = new RollingStrategyProperties();
// }
// HasRolled = true;
// bool noForce = true;
// // Custom rarity provided
// if (rollingStrategyProperties.OverrideRollModifierRarity != null)
// {
// Rarity = rollingStrategyProperties.OverrideRollModifierRarity.Invoke();
// }
// else if (rollingStrategyProperties.ForceModifierRarity != null)
// {
// Rarity = rollingStrategyProperties.ForceModifierRarity;
// }
// else if (Rarity == null || Rarity.Type == 0)
// {
// Rarity = mod.GetModifierRarity<CommonRarity>();
// }
// ctx.Rarity = Rarity;
// // Upgrade rarity
// if (rollingStrategyProperties.CanUpgradeRarity(ctx)
// && Main.rand.NextFloat() <= (Rarity.UpgradeChance ?? 0f))
// {
// var newRarity = Rarity.Upgrade;
// var newFromLoader = ModUtils.GetModifierRarity(newRarity);
// if (newRarity != null && newFromLoader != null)
// {
// Rarity = newFromLoader;
// }
// }
// // Downgrade rarity
// else if (rollingStrategyProperties.CanDowngradeRarity(ctx)
// && Main.rand.NextFloat() <= (Rarity.DowngradeChance ?? 0f))
// {
// var newRarity = Rarity.Downgrade;
// var newFromLoader = ModUtils.GetModifierRarity(newRarity);
// if (newRarity != null && newFromLoader != null)
// {
// Rarity = newFromLoader;
// }
// }
// ctx.Rarity = Rarity;
// // Custom pool provided
// if (rollingStrategyProperties.OverrideRollModifierPool != null)
// {
// ModifierPool = rollingStrategyProperties.OverrideRollModifierPool.Invoke();
// noForce = !ModifierPool?._CanRoll(ctx) ?? true;
// }
// // No behavior provided
// if (noForce)
// {
// // A pool is forced to roll
// if (rollingStrategyProperties.ForceModifierPool != null)
// {
// ModifierPool = ModUtils.GetModifierPool(rollingStrategyProperties.ForceModifierPool.GetType());
// noForce = !ModifierPool?._CanRoll(ctx) ?? true;
// }
// // No pool forced to roll or it's not valid
// if (noForce)
// {
// // Try rolling a predefined (weighted) pool
// bool rollPredefinedPool = Main.rand.NextFloat() <= rollingStrategyProperties.RollPredefinedPoolChance;
// noForce = !rollPredefinedPool;
// if (rollPredefinedPool)
// {
// // GetWeightedPool already checks _CanRoll
// ModifierPool = ContentLoader.ModifierPool.GetWeightedPool(ctx);
// noForce = ModifierPool == null || !ModifierPool._CanRoll(ctx);
// }
// // Roll from all modifiers
// if (noForce)
// {
// ModifierPool = mod.GetAllModifiersPool();
// if (!ModifierPool._CanRoll(ctx))
// {
// InvalidateRolls();
// return null;
// }
// }
// }
// }
// if (ctx.Strategy != null)
// {
// // Attempt rolling modifiers
// if (!ctx.Strategy.Roll(ctx, new RollingStrategyContext(ctx.Item, rollingStrategyProperties)))
// {
// InvalidateRolls();
// }
// }
// ctx.Item.GetGlobalItem<GraphicsGlobalItem>().NeedsUpdate = true;
// return ModifierPool;
//}
public override GlobalItem Clone(Item item, Item itemClone)
{
LootModItem clone = (LootModItem)base.Clone(item, itemClone);
clone.Rarity = (ModifierRarity)Rarity?.Clone() ?? mod.GetNullModifierRarity();
clone.Modifiers = (FiniteModifierPool)Modifiers?.Clone() ?? mod.GetNullModifierPool();
// there is no need to apply here, we already cloned the item which stats are already modified by its pool
return clone;
}
public override void Load(Item item, TagCompound tag)
{
// enforce illegitimate rolls to go away (needed for earliest versions saves)
if (!item.IsModifierRollableItem())
{
InvalidateRolls();
}
else if (tag.ContainsKey("SaveVersion"))
{
int saveVersion = tag.GetInt("SaveVersion");
if (saveVersion < 14)
{
InvalidateRolls();
}
else
{
Rarity = ModifierRarityIO.Load(item, tag.GetCompound("ModifierRarity"));
Modifiers = ModifierPoolIO.Load(item, tag.GetCompound("ModifierPool"));
SealedModifiers = tag.GetBool("SealedModifiers");
HasRolled = tag.GetBool("HasRolled");
Modifiers.Apply(item);
GraphicsGlobalItem.UpdateGraphicsEntities(Modifiers.Modifiers, item);
}
}
}
public override TagCompound Save(Item item)
{
TagCompound tag = new TagCompound
{
// SaveVersion saved since SaveVersion 2, version 1 not present
{"SaveVersion", SAVE_VERSION},
{"SealedModifiers", SealedModifiers},
{"HasRolled", HasRolled},
{"ModifierRarity", ModifierRarityIO.Save(item, Rarity)},
{"ModifierPool", ModifierPoolIO.Save(item, Modifiers)}
};
return tag;
}
public override bool NeedsSaving(Item item)
=> Modifiers != null || HasRolled;
public override void NetReceive(Item item, BinaryReader reader)
{
bool hasPool = reader.ReadBoolean();
if (hasPool)
{
Rarity = ModifierRarityIO.NetReceive(item, reader);
Modifiers = ModifierPoolIO.NetReceive(item, reader);
}
HasRolled = reader.ReadBoolean();
SealedModifiers = reader.ReadBoolean();
JustTinkerModified = reader.ReadBoolean();
item.UpdateModifiers(Modifiers.Modifiers);
}
public override void NetSend(Item item, BinaryWriter writer)
{
bool hasPool = Modifiers != null;
writer.Write(hasPool);
if (hasPool)
{
ModifierRarityIO.NetSend(Rarity, item, writer);
ModifierPoolIO.NetSend(Modifiers, item, writer);
}
writer.Write(HasRolled);
writer.Write(SealedModifiers);
writer.Write(JustTinkerModified);
}
//public override void OnCraft(Item item, Recipe recipe)
//{
// ModifierPool pool = GetInfo(item).ModifierPool;
// if (!HasRolled && pool == null)
// {
// ModifierContext ctx = new ModifierContext
// {
// Method = ModifierContextMethod.OnCraft,
// Item = item,
// Player = Main.LocalPlayer,
// Recipe = recipe
// };
// pool = RollNewPool(ctx);
// pool?.ApplyModifiers(item);
// }
// base.OnCraft(item, recipe);
//}
//public override bool OnPickup(Item item, Player player)
//{
// ModifierPool pool = GetInfo(item).ModifierPool;
// if (!HasRolled && pool == null)
// {
// ModifierContext ctx = new ModifierContext
// {
// Method = ModifierContextMethod.OnPickup,
// Item = item,
// Player = player,
// Strategy = RollingUtils.Strategies.Normal
// };
// pool = RollNewPool(ctx);
// pool?.ApplyModifiers(item);
// }
// return base.OnPickup(item, player);
//}
//public override void PostReforge(Item item)
//{
// if (!SealedModifiers)
// {
// ModifierContext ctx = new ModifierContext
// {
// Method = ModifierContextMethod.OnReforge,
// Item = item,
// Player = Main.LocalPlayer,
// Strategy = RollingUtils.Strategies.Normal
// };
// ModifierPool pool = RollNewPool(ctx);
// pool?.ApplyModifiers(item);
// }
//}
private string GetPrefixNormString(float cpStat, float rStat, ref double num, ref Color? color)
{
//float num19 = (float)Main.mouseTextColor / 255f;
//patch file: num20
float defColorVal = Main.mouseTextColor / 255f;
int alphaColor = Main.mouseTextColor;
if (cpStat == 0f && rStat != 0f)
{
num = 1;
if (rStat > 0f)
{
color = new Color((byte)(120f * defColorVal), (byte)(190f * defColorVal), (byte)(120f * defColorVal), alphaColor);
return "+" + rStat.ToString(CultureInfo.InvariantCulture); /* + Lang.tip[39].Value;*/
}
color = new Color((byte)(190f * defColorVal), (byte)(120f * defColorVal), (byte)(120f * defColorVal), alphaColor);
return rStat.ToString(CultureInfo.InvariantCulture); /* + Lang.tip[39].Value;*/
}
double diffStat = rStat - cpStat;
diffStat = diffStat / cpStat * 100.0;
diffStat = Math.Round(diffStat);
num = diffStat;
// for some reason - is handled automatically, but + is not
if (diffStat > 0.0)
{
color = new Color((byte)(120f * defColorVal), (byte)(190f * defColorVal), (byte)(120f * defColorVal), alphaColor);
return "+" + diffStat.ToString(CultureInfo.InvariantCulture); /* + Lang.tip[39].Value;*/
}
color = new Color((byte)(190f * defColorVal), (byte)(120f * defColorVal), (byte)(120f * defColorVal), alphaColor);
return diffStat.ToString(CultureInfo.InvariantCulture); /* + Lang.tip[39].Value;*/
//if (num12 < 0.0)
//{
// array3[num4] = true;
//}
}
/// <summary>
/// Will modify vanilla tooltips to add additional information for the affected item's modifiers
/// </summary>
public override void ModifyTooltips(Item item, List<TooltipLine> tooltips)
{
var pool = Modifiers;
if (pool != null)
{
// the following part, recalculate the vanilla prefix tooltips
// this is because our mods modify the stats, which was never intended by vanilla, causing the differences to be innacurate and bugged
// RECALC START
var vanillaTooltips = tooltips.Where(x => x.mod.Equals("Terraria")).ToArray();
var baseItem = new Item();
baseItem.netDefaults(item.netID);
// the item with just the modifiers applied
// var poolItem = baseItem.CloneWithModdedDataFrom(item);
// GetItemInfo(poolItem)?.ModifierPool.ApplyModifiers(poolItem);
// the item with just the prefix applied
var prefixItem = baseItem.Clone();
prefixItem.Prefix(item.prefix);
try
{
foreach (var tooltipLine in vanillaTooltips)
{
double outNumber = 0d;
string newTooltipLine = tooltipLine.text;
Color? newColor = tooltipLine.overrideColor;
string tooltipEndText =
new string(tooltipLine.text
.Reverse()
.TakeWhile(x => !char.IsDigit(x))
.Reverse()
.ToArray());
//private string[] _prefixTooltipLines = {
// "PrefixDamage", "PrefixSpeed", "PrefixCritChance", "PrefixUseMana", "PrefixSize",
// "PrefixShootSpeed", "PrefixKnockback", "PrefixAccDefense", "PrefixAccMaxMana",
// "PrefixAccCritChance", "PrefixAccDamage", "PrefixAccMoveSpeed", "PrefixAccMeleeSpeed"
// };
if (tooltipLine.Name.Equals("PrefixDamage"))
{
newTooltipLine = baseItem.damage > 0
? GetPrefixNormString(baseItem.damage, prefixItem.damage, ref outNumber, ref newColor)
: GetPrefixNormString(prefixItem.damage, baseItem.damage, ref outNumber, ref newColor);
}
else if (tooltipLine.Name.Equals("PrefixSpeed"))
{
newTooltipLine = baseItem.useAnimation <= 0
? GetPrefixNormString(baseItem.useAnimation, prefixItem.useAnimation, ref outNumber, ref newColor)
: GetPrefixNormString(prefixItem.useAnimation, baseItem.useAnimation, ref outNumber, ref newColor);
}
else if (tooltipLine.Name.Equals("PrefixCritChance"))
{
outNumber = prefixItem.crit - baseItem.crit;
float defColorVal = Main.mouseTextColor / 255f;
int alphaColor = Main.mouseTextColor;
newTooltipLine = "";
if (outNumber >= 0)
{
newTooltipLine += "+";
newColor = new Color((byte)(120f * defColorVal), (byte)(190f * defColorVal), (byte)(120f * defColorVal), alphaColor);
}
else
{
newColor = new Color((byte)(190f * defColorVal), (byte)(120f * defColorVal), (byte)(120f * defColorVal), alphaColor);
}
newTooltipLine += outNumber.ToString(CultureInfo.InvariantCulture);
}
else if (tooltipLine.Name.Equals("PrefixUseMana"))
{
if (baseItem.mana != 0)
{
float defColorVal = Main.mouseTextColor / 255f;
int alphaColor = Main.mouseTextColor;
newTooltipLine = GetPrefixNormString(baseItem.mana, prefixItem.mana, ref outNumber, ref newColor);
newColor = prefixItem.mana < baseItem.mana
? new Color((byte)(120f * defColorVal), (byte)(190f * defColorVal), (byte)(120f * defColorVal), alphaColor)
: new Color((byte)(190f * defColorVal), (byte)(120f * defColorVal), (byte)(120f * defColorVal), alphaColor);
}
}
else if (tooltipLine.Name.Equals("PrefixSize"))
{
newTooltipLine = baseItem.scale > 0
? GetPrefixNormString(baseItem.scale, prefixItem.scale, ref outNumber, ref newColor)
: GetPrefixNormString(prefixItem.scale, baseItem.scale, ref outNumber, ref newColor);
}
else if (tooltipLine.Name.Equals("PrefixShootSpeed"))
{
newTooltipLine = baseItem.shootSpeed > 0
? GetPrefixNormString(baseItem.shootSpeed, prefixItem.shootSpeed, ref outNumber, ref newColor)
: GetPrefixNormString(prefixItem.shootSpeed, baseItem.shootSpeed, ref outNumber, ref newColor);
}
else if (tooltipLine.Name.Equals("PrefixKnockback"))
{
newTooltipLine = baseItem.knockBack > 0
? GetPrefixNormString(baseItem.knockBack, prefixItem.knockBack, ref outNumber, ref newColor)
: GetPrefixNormString(prefixItem.knockBack, baseItem.knockBack, ref outNumber, ref newColor);
}
else
{
continue;
}
int ttlI = tooltips.FindIndex(x => x.mod.Equals(tooltipLine.mod) && x.Name.Equals(tooltipLine.Name));
if (ttlI == -1)
{
continue;
}
if (outNumber == 0d)
{
tooltips.RemoveAt(ttlI);
}
else
{
tooltips[ttlI].text = $"{newTooltipLine}{tooltipEndText}";
tooltips[ttlI].overrideColor = newColor;
}
}
}
catch (Exception e)
{
Loot.Logger.Error(
$"A problem occurred during modification of the item's tooltip." +
$"\nItem in question: {item.AffixName()}",
e);
}
// RECALC END
// Modifies the tooltips, to insert generic mods data
int i = tooltips.FindIndex(x => x.mod == "Terraria" && x.Name == "ItemName");
if (i != -1)
{
var namelayer = tooltips[i];
if (Rarity.ItemPrefix != null)
{
namelayer.text = $"{Rarity.ItemPrefix} {namelayer.text}";
}
if (Rarity.ItemSuffix != null)
{
namelayer.text = $"{namelayer.text} {Rarity.ItemSuffix}";
}
if (Rarity.OverrideNameColor != null)
{
namelayer.overrideColor = Rarity.OverrideNameColor;
}
tooltips[i] = namelayer;
}
CheatedItemHackGlobalItem cheatedItemHackGlobalItem = CheatedItemHackGlobalItem.GetInfo(item);
bool isVanityIgnored = cheatedItemHackGlobalItem.ShouldBeIgnored(item, Main.LocalPlayer);
Color? inactiveColor = isVanityIgnored ? (Color?)Color.DarkSlateGray : null;
// Insert modifier rarity
if (!(Rarity is NullModifierRarity))
{
i = tooltips.Count;
tooltips.Insert(i, new TooltipLine(mod, "Loot: Modifier:Rarity", $"[{Rarity.RarityName}]{(isVanityIgnored ? " [IGNORED]" : "")}")
{
overrideColor = inactiveColor ?? Rarity.Color * Main.inventoryScale
});
}
i = tooltips.Count - 1;
foreach (var modifier in pool.Modifiers)
{
foreach (var tt in modifier.GetTooltip().Build())
{
tooltips.Insert(++i, new TooltipLine(mod, $"Loot: Modifier:Line:{i}", $"{modifier.GetFormattedUniqueName()} {tt.Text}".TrimStart())
{
overrideColor = inactiveColor ?? (tt.Color ?? Color.White) * Main.inventoryScale
});
}
}
if (SealedModifiers)
{
var ttl = new TooltipLine(mod, "Loot: Modifier:Sealed", "Modifiers cannot be changed")
{
overrideColor = inactiveColor ?? Color.Cyan
};
tooltips.Insert(++i, ttl);
}
foreach (var e in pool.Modifiers)
{
e.ModifyTooltips(item, tooltips);
}
}
}
}
}