Even More Modifiers  1.0.0.0
A mod for rolling various bonus stats on items
RandomDebuff.cs
Go to the documentation of this file.
1 using System.Collections.Generic;
2 using System.IO;
3 using System.Linq;
4 using Loot.Api.Core;
5 using Loot.Modifiers.Base;
6 using Terraria;
7 using Terraria.ID;
8 using Terraria.ModLoader.IO;
9 
10 namespace Loot.Modifiers.WeaponModifiers
11 {
15  public sealed class RandomDebuff : WeaponDebuffModifier
16  {
17  private static readonly (int type, int time, float chance)[] BuffPairs =
18  {
19  (BuffID.Confused, 100, 1f),
20  (BuffID.CursedInferno, 100, 1f),
21  (BuffID.Frostburn, 100, 1f),
22  (BuffID.OnFire, 100, 1f),
23  (BuffID.Poisoned, 100, 1f),
24  (BuffID.Ichor, 100, 1f),
25  };
26 
27  private readonly int _len = BuffPairs.GetLength(0);
28 
29  public int GetRolledIndex() => _index;
30  private int _index = -1;
31 
32  public override void NetReceive(Item item, BinaryReader reader)
33  {
34  base.NetReceive(item, reader);
35  _index = reader.ReadInt32();
36  _timeScaleFactor = reader.ReadSingle();
37  }
38 
39  public override void NetSend(Item item, BinaryWriter writer)
40  {
41  base.NetSend(item, writer);
42  writer.Write(_index);
43  writer.Write(_timeScaleFactor);
44  }
45 
46  public override void Save(Item item, TagCompound tag)
47  {
48  base.Save(item, tag);
49  tag.Add("_index", _index);
50  tag.Add("_timeScaleFactor", _timeScaleFactor);
51  }
52 
53  public override void Load(Item item, TagCompound tag)
54  {
55  base.Load(item, tag);
56  _index = tag.GetAsInt("_index");
57  // Showcase rolling a stat properly that previously wasn't present
58  if (tag.ContainsKey("_timeScaleFactor"))
59  {
60  _timeScaleFactor = tag.GetAsShort("_timeScaleFactor");
61  if (_timeScaleFactor <= 0f) RollTimeScaleFactor();
62  }
63  else
64  {
65  RollTimeScaleFactor();
66  }
67  }
68 
69  private void RollTimeScaleFactor()
70  {
71  _timeScaleFactor = (0.5f + Main.rand.NextFloat() * (0.25f)) + Properties.Power / 100f;
72  }
73 
74  public override void Roll(ModifierContext ctx, IEnumerable<Modifier> rolledModifiers)
75  {
76  base.Roll(ctx, rolledModifiers);
77 
78  // Exclude already rolled random debuffs from the list
79  // Try rolling a new random one
80  var similarModsBuffTypes = rolledModifiers.Where(x => x is RandomDebuff).Cast<RandomDebuff>().Select(x => x.BuffType);
81  var rollableBuffTypes = BuffPairs.Select(x => x.type).Except(similarModsBuffTypes);
82  int randBuffIndex = Main.rand.Next(rollableBuffTypes.Count());
83  _index = BuffPairs.ToList().FindIndex(x => x.type == rollableBuffTypes.ElementAt(randBuffIndex));
84  RollTimeScaleFactor();
85  }
86 
87  public override bool PostRoll(ModifierContext ctx, IEnumerable<Modifier> rolledModifiers)
88  {
89  // The roll is only valid, if none other rolled random debuff has the same index
90  return base.PostRoll(ctx, rolledModifiers)
91  && _index != -1
92  && rolledModifiers
93  .Select(x => x as RandomDebuff)
94  .All(x => x?.GetRolledIndex() != _index);
95  }
96 
97  private float _timeScaleFactor = 1f;
98  public override int BuffType => BuffPairs[_index].type;
99  public override int BuffTime => (int) (BuffPairs[_index].time * _timeScaleFactor);
100  public override float BuffInflictionChance => BuffPairs[_index].chance;
101  }
102 }
Defines a context in which a Modifier might be rolled Which fields are available (not null) depends o...
override void NetSend(Item item, BinaryWriter writer)
Definition: RandomDebuff.cs:39
override void NetReceive(Item item, BinaryReader reader)
Definition: RandomDebuff.cs:32
override void Roll(ModifierContext ctx, IEnumerable< Modifier > rolledModifiers)
Allows modders to do something when the modifier is rolled in the given context The passed rolledModi...
Definition: RandomDebuff.cs:74
override bool PostRoll(ModifierContext ctx, IEnumerable< Modifier > rolledModifiers)
Returns if the modifier will actually be added after it is rolled. This is called after Roll is calle...
Definition: RandomDebuff.cs:87
Defines a modifier that can inflict a debuff, applicable for weapons
override void Load(Item item, TagCompound tag)
Allows modder to do custom loading here Use the given TC to pull data you saved using Save(Item...
Definition: RandomDebuff.cs:53
override void Save(Item item, TagCompound tag)
Allows modder to do custom saving here Use the given TC to put data you want to save, which can be loaded using Load(Item,TagCompound)
Definition: RandomDebuff.cs:46