Even More Modifiers  1.0.0.0
A mod for rolling various bonus stats on items
ModifierProperties.cs
Go to the documentation of this file.
1 using System;
2 using System.IO;
3 using Loot.Api.Builder;
4 using Terraria;
5 using Terraria.ModLoader.IO;
6 
7 namespace Loot.Api.Core
8 {
12  public class ModifierProperties
13  {
14  public static ModifierPropertiesBuilder Builder => new ModifierPropertiesBuilder();
15 
16  public float MinMagnitude { get; private set; } = 1f;
17  public float MaxMagnitude { get; private set; } = 1f;
18  public float MagnitudeStrength { get; private set; } = 1f;
19  public float BasePower { get; private set; } = 1f;
20  public float RarityLevel { get; private set; } = 1f;
21  public float RollChance { get; private set; } = 1f;
22  public int RoundPrecision { get; private set; } = 0;
23  public bool IsUnique { get; private set; } = false;
24  public float Magnitude { get; internal set; }
25  private float _power;
26 
27  public float Power
28  {
29  get => _power;
30  internal set
31  {
32  _power = value;
33  RoundedPower = (float)Math.Round(value, RoundPrecision);
34  }
35  }
36 
37  public float RoundedPower { get; private set; }
38 
39  public ModifierProperties RollMagnitudeAndPower(float magnitudePower = 1f, float lukStat = 0f)
40  {
41  Magnitude = RollMagnitude(magnitudePower, lukStat);
42 
43  int iterations = (int)Math.Ceiling(lukStat) / 2;
44 
45  // makes you more lucky rolling better magnitudes
46  for (int i = 0; i < iterations; i++)
47  {
48  float rolledMagnitude = RollMagnitude(magnitudePower, lukStat);
49  if (rolledMagnitude > Magnitude)
50  {
51  Magnitude = rolledMagnitude;
52  }
53  }
54 
55  Power = RollPower();
56 
57  return this;
58  }
59 
60  private float RollMagnitude(float magnitudePower, float lukStat)
61  {
62  float useMin = MinMagnitude * (1f + magnitudePower / 10f);
63  float useMax = MaxMagnitude * magnitudePower;
64  float randomMag = (useMin + Main.rand.NextFloat() * (useMax - useMin));
65  return randomMag * MagnitudeStrength;
66  }
67 
68  private float RollPower()
69  {
70  return BasePower * Magnitude;
71  }
72 
73  public virtual void NetReceive(Item item, BinaryReader reader)
74  {
75  }
76 
77  public virtual void NetSend(Item item, BinaryWriter writer)
78  {
79  }
80 
81  public virtual void Save(Item item, TagCompound tag)
82  {
83  }
84 
85  public virtual void Load(Item item, TagCompound tag)
86  {
87  }
88 
95  public class ModifierPropertiesBuilder : PropertyBuilder<ModifierProperties>
96  {
97  protected override ModifierProperties DefaultProperty
98  {
99  set
100  {
101  Property.MinMagnitude = value.MinMagnitude;
102  Property.MaxMagnitude = value.MaxMagnitude;
103  Property.MagnitudeStrength = value.MagnitudeStrength;
104  Property.BasePower = value.BasePower;
105  Property.RarityLevel = value.RarityLevel;
106  Property.RollChance = value.RollChance;
107  Property.RoundPrecision = value.RoundPrecision;
108  Property.IsUnique = value.IsUnique;
109  }
110  }
111 
113  {
114  Property.MinMagnitude = val;
115  return this;
116  }
117 
119  {
120  Property.MaxMagnitude = val;
121  return this;
122  }
123 
125  {
126  Property.MagnitudeStrength = val;
127  return this;
128  }
129 
131  {
132  Property.BasePower = val;
133  return this;
134  }
135 
137  {
138  Property.RarityLevel = val;
139  return this;
140  }
141 
143  {
144  Property.RollChance = val;
145  return this;
146  }
147 
149  {
150  Property.RoundPrecision = val;
151  return this;
152  }
153 
155  {
156  Property.IsUnique = val;
157  return this;
158  }
159  }
160  }
161 }
virtual void Save(Item item, TagCompound tag)
virtual void NetReceive(Item item, BinaryReader reader)
virtual void Load(Item item, TagCompound tag)
ModifierProperties RollMagnitudeAndPower(float magnitudePower=1f, float lukStat=0f)
The ModifierPropertiesBuilder implements the builder pattern for ModifierProperties It provides a str...
virtual void NetSend(Item item, BinaryWriter writer)
Defines the properties of a modifier
float RollMagnitude(float magnitudePower, float lukStat)
ModifierPropertiesBuilder WithMagnitudeStrength(float val)
Defines an abstract implementation of IPropertyBuilder<T>