-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChemistryClassItem.cs~
646 lines (423 loc) · 19.3 KB
/
ChemistryClassItem.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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.IO;
using System.Linq;
using ChemistryClass.ModUtils;
using ChemistryClass.UI;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Steamworks;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
using Terraria.ModLoader.IO;
using Terraria.Utilities;
namespace ChemistryClass {
public abstract class ChemistryClassItem : ModItem {
//Clone new instances
public override bool CloneNewInstances => true;
//Purity
public float purity = 1f;
public bool Impure => purity == 0f;
//Decay basic stats
public float minutesToDecay = 3;
private float DecayRateReal => item.useTime / (3600f * minutesToDecay);
//Prefix value trackers
private float decayRateMult = 1f;
//Purity loss instance values
private float curDecayRate;
//Item value trackers
public int currentDamage { get; private set; }
public int currentCrit { get; private set; }
public float currentKB { get; private set; }
//DECAY STATS
private float curMinsToDecay
=> item.useTime / (curDecayRate * 3600f);
//Purity effect multipliers
public float maxPurityMult = 1.1f;
public float minPurityMult = 0.7f;
public float impureMult = 0.2f;
//Refinement data
public List<RefinementItem> refinementData;
//Fan name
public string fanName = null;
//Purity callout values
private float previousCallout = 1f;
public float calloutDivisor = 5f;
//Static values
public static Color CalloutColor { get; } = new Color(120, 0xFF, 0xFF);
public static Color FanColor { get; } = new Color(0xFF, 0x50, 0xA0);
//Valid prefixes (universal, common, and chemical)
private List<byte> _validPrefixes = null;
public List<byte> ValidPrefixes {
get {
if (_validPrefixes == null) _getPrefixes();
return _validPrefixes;
}
}
//Chemistry prefixes
private List<byte> _chemistryPrefixes = null;
public List<byte> ChemistryPrefixes {
get {
if (_chemistryPrefixes == null) _getPrefixes();
return _chemistryPrefixes;
}
}
//prefix getter
private void _getPrefixes() {
_validPrefixes = new List<byte>();
_chemistryPrefixes = new List<byte>();
//Main.NewText( string.Join(" :: ", ModPrefix.GetPrefixesInCategory(ChemistryClassPrefix.prefixCategory)) );
foreach (ModPrefix pre in ModPrefix.GetPrefixesInCategory(ChemistryClassPrefix.prefixCategory)) {
//skip if not a chemical prefix
if (!pre.GetType().IsSubclassOf(typeof(ChemistryClassPrefix))) continue;
//skip basic chemical prefix
if (pre.Name == new ChemistryClassPrefix().Name) continue;
_validPrefixes.Add(pre.Type);
_chemistryPrefixes.Add(pre.Type);
//DEBUGGING
//if (pre.GetType() != typeof(Prefixes.ReactivePrefs)) continue;
//Main.NewText(pre as ChemistryClassPrefix);
}
//DEBUGGING
/*List<byte> bChem = new List<byte>();
foreach( var chem in _chemistryPrefixes ) { bChem.Add(chem.Type); }
Main.NewText(string.Join(" ::: ", bChem));
Main.NewText(string.Join(" ::: ", _validPrefixes));*/
//Add universal & common prefixes
_validPrefixes.Add(PrefixID.Keen);
_validPrefixes.Add(PrefixID.Superior);
_validPrefixes.Add(PrefixID.Forceful);
_validPrefixes.Add(PrefixID.Broken);
_validPrefixes.Add(PrefixID.Damaged);
_validPrefixes.Add(PrefixID.Shoddy);
_validPrefixes.Add(PrefixID.Unpleasant);
_validPrefixes.Add(PrefixID.Weak);
_validPrefixes.Add(PrefixID.Ruthless);
_validPrefixes.Add(PrefixID.Godly);
_validPrefixes.Add(PrefixID.Demonic);
_validPrefixes.Add(PrefixID.Zealous);
_validPrefixes.Add(PrefixID.Quick);
_validPrefixes.Add(PrefixID.Deadly);
_validPrefixes.Add(PrefixID.Agile);
_validPrefixes.Add(PrefixID.Nimble);
_validPrefixes.Add(PrefixID.Murderous);
_validPrefixes.Add(PrefixID.Slow);
_validPrefixes.Add(PrefixID.Sluggish);
_validPrefixes.Add(PrefixID.Lazy);
_validPrefixes.Add(PrefixID.Annoying);
_validPrefixes.Add(PrefixID.Nasty);
}
//Decay qualification
protected string DecayQualifier {
get {
//DEBUGGING
//if(ChemistryClass.UnpausedUpdateCount % 60 == 0)
//Main.NewText(curDecayRate);
if (curMinsToDecay > 10) return "Extremely low";
if (curMinsToDecay > 5) return "Very low";
if (curMinsToDecay > 3) return "Low";
if (curMinsToDecay > 2) return "Average";
if (curMinsToDecay > 1.5) return "High";
if (curMinsToDecay > 1) return "Very high";
return "Extremely high";
}
}
//Setting defaults
public virtual void SafeSetDefaults() { }
public sealed override void SetDefaults() {
SafeSetDefaults();
item.melee = false;
item.ranged = false;
item.magic = false;
item.summon = false;
item.thrown = false;
item.maxStack = 1;
}
public void SetRefinementData(params (int, float)[] idValuePairs) {
refinementData = new List<RefinementItem>();
foreach ((int, float) value in idValuePairs) {
refinementData.Add((RefinementItem)value);
}
}
public bool CanBeRefinedWith(int itemID) {
foreach(RefinementItem item in refinementData) {
if (item.itemID == itemID) return true;
}
return false;
}
public float GetRefinementValue(int itemID) {
foreach (RefinementItem item in refinementData) {
if (item.itemID == itemID) return item.value;
}
return 0f;
}
//Purity textification
private double roundOut(double i)
=> Math.Round(i * 10000) / 10000;
public string DisplayPurity
=> purity == 0f ? "Impure" : roundOut(Math.Round(purity * 100 / calloutDivisor) * calloutDivisor) + "%";
public string TooltipPurity
=> purity == 0f ? "Impure" : Math.Ceiling(purity * 1000) / 10 + "% pure";
//Modifying tooltip
public virtual void SafeModifyTooltips(List<TooltipLine> tooltips) { }
private void ModifyDamageTooltip(List<TooltipLine> tooltips) {
int dmgIndex = tooltips.FindIndex(t => t.Name == "Damage" && t.mod == "Terraria");
if (dmgIndex < 0) return;
string[] splitTooltip = tooltips[dmgIndex].text.Split(' ');
string localDamageValue = splitTooltip.First();
string localDamageWord = splitTooltip.Last();
tooltips[dmgIndex].text = localDamageValue + " chemical " + localDamageWord;
}
private void AddDecayTooltips(List<TooltipLine> tooltips) {
string dRText = DecayQualifier + " rate of decay";
TooltipLine decayRateT = new TooltipLine(mod, "DecayRate", dRText);
int targetIndex = tooltips.FindIndex(line => line.Name == "Knockback" && line.mod == "Terraria");
if (targetIndex < 0) return;
tooltips.Insert(targetIndex + 1, decayRateT);
}
private void AddPurityTooltip(List<TooltipLine> tooltips) {
TooltipLine tooltip = new TooltipLine(mod, "Purity", TooltipPurity) {
overrideColor = CalloutColor
};
int targetIndex = tooltips.FindIndex(line => line.isModifier || line.isModifierBad);
if (targetIndex < 0) tooltips.Add(tooltip);
else tooltips.Insert(targetIndex, tooltip);
}
private void AddRefinementTooltips(List<TooltipLine> tooltips) {
string text = "Can be refined with:";
if (refinementData == null || refinementData.Count < 1) {
text = "Cannot be refined by normal means.";
goto RETURN;
}
foreach (var entry in refinementData) {
Item item = new Item();
item.SetDefaults(entry.itemID);
text += "\n- " + item.Name;
item = null;
}
RETURN:
TooltipLine tooltip = new TooltipLine(mod, "Refinement", text) {
overrideColor = CalloutColor
};
int targetIndex = tooltips.FindIndex(line => line.Name == "Purity");
tooltips.Insert(targetIndex + 1, tooltip);
}
private void AddPrefixTooltips(List<TooltipLine> tooltips) {
TooltipLine line;
float value;
if (decayRateMult != 1f) {
value = 100 * (decayRateMult - 1f);
line = new TooltipLine(
mod,
"PrefixPurityLoss",
(value >= 0 ? "+" : "") + Math.Round(value) + "% purity loss"
) { isModifierBad = value >= 0, isModifier = true };
tooltips.Add(line);
}
}
private void AddFanByTootlip(List<TooltipLine> tooltips) {
if (fanName != null) {
tooltips.Insert(
1,
new TooltipLine(mod, "FanBy", $"[Fan idea by {fanName}]")
{ overrideColor = FanColor }
);
}
}
public sealed override void ModifyTooltips(List<TooltipLine> tooltips) {
SafeModifyTooltips(tooltips);
ModifyDamageTooltip(tooltips);
AddDecayTooltips(tooltips);
AddPurityTooltip(tooltips);
AddRefinementTooltips(tooltips);
AddPrefixTooltips(tooltips);
AddFanByTootlip(tooltips);
}
//Decay statistics
private void ClampPurity() => purity.Clamp(0, 1);
public void RefreshStats() {
GetDecayStats(Main.LocalPlayer);
GetPrefixValues();
}
private void GetDecayStats(Player player) {
//get bases
curDecayRate = DecayRateReal;
//get player & factor in prefix
curDecayRate *= player.Chemistry().decayRateMult * decayRateMult;
ModifyDecayStats(ref curDecayRate, player);
//limit stats
float minValue = (float)Math.Pow(10, -5);
curDecayRate.EnforceMin(minValue);
}
private void GetPrefixValues() {
decayRateMult = 1f;
//DEBUGGING
//Main.NewText(item.prefix);
byte prefixType = item.prefix;
ChemistryClassPrefix prefix = ModPrefix.GetPrefix(prefixType) as ChemistryClassPrefix;
if (prefix != null) {
prefix.SetDecayStats(ref decayRateMult);
}
}
public virtual void ModifyDecayStats(ref float decay, Player player) { }
//Purity to multiplier mapping
protected float MapPurity(float min, float max)
=> purity.Map(0, 1, min, max);
protected float DefaultMapPurity => MapPurity(minPurityMult, maxPurityMult);
public virtual float PurityDamageMult => !Impure ? DefaultMapPurity : impureMult;
public virtual float PurityKnockbackMult => !Impure ? DefaultMapPurity : impureMult;
public virtual float PurityCritMult => !Impure ? DefaultMapPurity : impureMult;
//Damage modifiction and purity stat retreiving
public virtual void SafeModifyWeaponDamage(Player player, ref float add, ref float mult, ref float flat) { }
public sealed override void ModifyWeaponDamage(Player player, ref float add, ref float mult, ref float flat) {
//RESFRESH STATS
RefreshStats();
add += player.Chemistry().chemicalDamageAdd;
mult *= player.Chemistry().chemicalDamageMult * PurityDamageMult;
SafeModifyWeaponDamage(player, ref add, ref mult, ref flat);
currentDamage = (int)(item.damage * add * mult + flat);
}
//Get other stats
public virtual void SafeGetWeaponKnockback(Player player, ref float knockback) { }
public sealed override void GetWeaponKnockback(Player player, ref float knockback) {
float add = 1;
float mult = 1;
add += player.Chemistry().chemicalKnockbackAdd;
mult *= player.Chemistry().chemicalKnockbackMult * PurityKnockbackMult;
knockback *= add;
knockback *= mult;
SafeGetWeaponKnockback(player, ref knockback);
currentKB = knockback;
}
public virtual void SafeGetWeaponCrit(Player player, ref int crit) { }
public sealed override void GetWeaponCrit(Player player, ref int crit) {
float add = 1;
float mult = 1;
add += player.Chemistry().chemicalCritAdd;
mult *= player.Chemistry().chemicalCritMult * PurityCritMult;
float tempCrit = crit;
tempCrit += add;
tempCrit *= mult;
crit = (int)Math.Ceiling(tempCrit);
SafeGetWeaponCrit(player, ref crit);
currentCrit = crit;
}
//Call out purity when it hits a landmark
//IMPORTANT: DO NOT CALL EITHER OF THESE BEFORE GetPurityLoss!!
private float RealCalloutDiv => calloutDivisor / 100f;
private bool PurityCloseToLandmark
=> purity % RealCalloutDiv < curDecayRate / 2 ||
purity % RealCalloutDiv > RealCalloutDiv - curDecayRate / 2;
private void TryCallout(Player player) {
if (ChemistryClass.Configuration.DecayDisplay == DecayDisplayMode.Meter) return;
if (PurityCloseToLandmark && purity != previousCallout) {
Rectangle spawn = new Rectangle(0, 0, 20, 20);
Vector2 position = player.position;
spawn.X = (int)position.X;
spawn.Y = (int)(position.Y - (ChemistryClass.Configuration.DecayDisplay == DecayDisplayMode.Both ? Main.screenHeight / 18f : 0));
CombatText.NewText(spawn, CalloutColor, DisplayPurity, true);
previousCallout = purity;
}
}
//DEBUGGING
/*public override bool PreDrawTooltip(ReadOnlyCollection<TooltipLine> lines, ref int x, ref int y) {
string text = "";
foreach (var i in lines) text += i.Name + " :: ";
Main.NewText(text);
return base.PreDrawTooltip(lines, ref x, ref y);
}*/
//Get values, reduce purity, and fix damage issues when item is used
public virtual void SafeUseStyle(Player player) { }
public virtual bool PrePurityOnWeaponUse(Player player) => true;
public virtual void UseItemExact(Player player) { }
public sealed override void UseStyle(Player player) {
SafeUseStyle(player);
if (player.itemAnimation == player.itemAnimationMax - 1) {
if (PrePurityOnWeaponUse(player))
UsePurity(ref player);
UseItemExact(player);
}
}
//Use up purity
public void UsePurity(ref Player player) {
purity -= curDecayRate;
ClampPurity();
TryAutoRefine(ref player);
TryCallout(player);
}
//Refinement
internal void TryAutoRefine(ref Player player) {
ChemistryClassPlayer chemPlayer = player.Chemistry();
RefinementMenuState availableMenu = ChemistryClass.refinementMenu;
if (!chemPlayer.autoRefine) return;
chemPlayer.autoRefineItem = availableMenu.menu.autoRefineSlot.Item;
if (!CanBeRefinedWith(chemPlayer.autoRefineItem.type)) return;
if (purity > 0.5f) return;
ChemistryClassItem newItem = this;
Item newAutoRefineItem = chemPlayer.autoRefineItem;
Refine(ref newItem, ref newAutoRefineItem, 0.5f);
purity = newItem.purity;
player.Chemistry().autoRefineItem = newAutoRefineItem;
Main.PlaySound(SoundID.Item37);
if (availableMenu == null) return;
availableMenu.menu.autoRefineSlot.Item = newAutoRefineItem;
}
public static bool Refine(ref ChemistryClassItem chemItem, ref Item item, float maxPurity = 1f) {
//DEBUG
//Main.NewText(chemItem.ToString());
//Main.NewText(item.ToString());
//Main.NewText(indexOfItem);
if (!chemItem.CanBeRefinedWith(item.type)) return false;
float refinement = chemItem.GetRefinementValue(item.type);
float refinementNeeded = maxPurity - chemItem.purity;
//DEBUG
//Main.NewText(refinementNeeded);
//Fail if no purification is needed
if (refinementNeeded == 0) return false;
//Clamp down the stack size if the original size would over-purify.
if (refinementNeeded < refinement * item.stack) {
int useAmt = (int)(Math.Ceiling(refinementNeeded / refinement) + 0.5f);
item.stack -= useAmt;
chemItem.purity += useAmt * refinement;
chemItem.purity.EnforceMax(1);
//DEBUG
//Main.NewText(item.stack);
return true;
}
//Add the original stack size purification value if the above two
//conditions fail.
chemItem.purity += refinement * item.stack;
item.TurnToAir();
return true;
}
public static bool Refine(ref Item chemItem, ref Item item, float maxPurity = 1f) {
ChemistryClassItem ccI = chemItem.Chemistry();
bool ret = Refine(ref ccI, ref item, maxPurity);
chemItem = ccI.item;
return ret;
}
//Prefix choosing
public override int ChoosePrefix(UnifiedRandom rand) {
//DEBUGGING
//Main.NewText(string.Join(" ::: ", ValidPrefixes));
//DEBUGGING
//Main.NewText(ValidPrefixes.FindIndex(b => ModPrefix.GetPrefix(b).Name.Contains("Reactive")));
//return ValidPrefixes.FindIndex(b => ModPrefix.GetPrefix(b).Name.Contains("Reactive"));
//Main.NewText(ValidPrefixes[3]);
return rand.Next(ValidPrefixes);
}
//Purity saving
public override TagCompound Save()
=> new TagCompound { [nameof(purity)] = purity, };
public override void NetSend(BinaryWriter writer)
=> writer.Write(purity);
//Purity loading
public override void Load(TagCompound tag)
=> purity = tag.GetFloat(nameof(purity));
public override void NetRecieve(BinaryReader reader)
=> purity = reader.ReadSingle();
}
}