Terraria ModLoader  0.11.4
A framework for Terraria mods
ModPrefix.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text.RegularExpressions;
5 using Terraria.ID;
6 using Terraria.Utilities;
7 
8 namespace Terraria.ModLoader
9 {
10  public abstract class ModPrefix
11  {
12  private static byte nextPrefix = PrefixID.Count;
13 
14  // TODO storing twice? could see a better implementation
15  internal static readonly IList<ModPrefix> prefixes = new List<ModPrefix>();
16  internal static readonly IDictionary<PrefixCategory, IList<ModPrefix>> categoryPrefixes;
17 
18  static ModPrefix() {
19  categoryPrefixes = new Dictionary<PrefixCategory, IList<ModPrefix>>();
20  foreach (PrefixCategory category in Enum.GetValues(typeof(PrefixCategory))) {
21  categoryPrefixes[category] = new List<ModPrefix>();
22  }
23  }
24 
25  internal static byte ReservePrefixID() {
26  if (ModNet.AllowVanillaClients) throw new Exception("Adding items breaks vanilla client compatibility");
27  if (nextPrefix == 0) throw new Exception("Prefix ID limit has been broken");
28 
29  byte reserveID = nextPrefix;
30  nextPrefix++;
31  return reserveID;
32  }
33 
40  public static ModPrefix GetPrefix(byte type) {
41  return type >= PrefixID.Count && type < PrefixCount ? prefixes[type - PrefixID.Count] : null;
42  }
43 
49  public static List<ModPrefix> GetPrefixesInCategory(PrefixCategory category) {
50  return new List<ModPrefix>(categoryPrefixes[category]);
51  }
52 
53  public static byte PrefixCount => nextPrefix;
54 
55  internal static void ResizeArrays() {
56  Array.Resize(ref Lang.prefix, nextPrefix);
57  }
58 
59  internal static void Unload() {
60  prefixes.Clear();
61  nextPrefix = PrefixID.Count;
62  foreach (PrefixCategory category in Enum.GetValues(typeof(PrefixCategory))) {
63  categoryPrefixes[category].Clear();
64  }
65  }
66 
70  internal static void Roll(Item item, ref int prefix, int vanillaWeight, params PrefixCategory[] categories) {
71  WeightedRandom<byte> wr = new WeightedRandom<byte>();
72  foreach (PrefixCategory category in categories)
73  foreach (ModPrefix modPrefix in categoryPrefixes[category].Where(x => x.CanRoll(item)))
74  wr.Add(modPrefix.Type, modPrefix.RollChance(item));
75 
76  if (vanillaWeight > 0)
77  wr.Add((byte)prefix, vanillaWeight);
78 
79  prefix = wr.Get();
80  }
81 
82  public Mod mod {
83  get;
84  internal set;
85  }
86 
87  public string Name {
88  get;
89  internal set;
90  }
91 
92  public byte Type {
93  get;
94  internal set;
95  }
96 
97  public ModTranslation DisplayName {
98  get;
99  internal set;
100  }
101 
107  public virtual float RollChance(Item item) {
108  return 1f;
109  }
110 
117  public virtual bool CanRoll(Item item) {
118  return RollChance(item) > 0;
119  }
120 
124  public virtual PrefixCategory Category => PrefixCategory.Custom;
125 
126  public virtual bool Autoload(ref string name) {
127  return mod.Properties.Autoload;
128  }
129 
130  public virtual void AutoDefaults() {
131  if (DisplayName.IsDefault())
132  DisplayName.SetDefault(Regex.Replace(Name, "([A-Z])", " $1").Trim());
133  }
134 
138  public virtual void SetDefaults() {
139  }
140 
144  public virtual void SetStats(ref float damageMult, ref float knockbackMult, ref float useTimeMult,
145  ref float scaleMult, ref float shootSpeedMult, ref float manaMult, ref int critBonus) {
146  }
147 
152  public virtual void ValidateItem(Item item, ref bool invalid) {
153  }
154 
159  public virtual void Apply(Item item) {
160  }
161 
165  public virtual void ModifyValue(ref float valueMult) {
166  }
167  }
168 
169  public enum PrefixCategory
170  {
174  Melee,
178  Ranged,
182  Magic,
183  AnyWeapon,
184  Accessory,
188  Custom
189  }
190 }
virtual void SetStats(ref float damageMult, ref float knockbackMult, ref float useTimeMult, ref float scaleMult, ref float shootSpeedMult, ref float manaMult, ref int critBonus)
Sets the stat changes for this prefix. If data is not already pre-stored, it is best to store custom ...
Definition: ModPrefix.cs:144
static bool AllowVanillaClients
Definition: ModNet.cs:52
virtual bool CanRoll(Item item)
Returns if your ModPrefix can roll on the given item By default returns RollChance(item) > 0 ...
Definition: ModPrefix.cs:117
virtual bool Autoload(ref string name)
Definition: ModPrefix.cs:126
virtual void SetDefaults()
Allows you to set the prefix&#39;s name/translations and to set its category.
Definition: ModPrefix.cs:138
virtual void Apply(Item item)
Applies the custom data stats set in SetStats to the given item.
Definition: ModPrefix.cs:159
static List< ModPrefix > GetPrefixesInCategory(PrefixCategory category)
Returns a list of all modded prefixes of a certain category.
Definition: ModPrefix.cs:49
static ModPrefix GetPrefix(byte type)
Returns the ModPrefix associated with specified type If not a ModPrefix, returns null.
Definition: ModPrefix.cs:40
virtual void ModifyValue(ref float valueMult)
Allows you to modify the sell price of the item based on the prefix or changes in custom data stats...
Definition: ModPrefix.cs:165
virtual float RollChance(Item item)
The roll chance of your prefix relative to a vanilla prefix, 1f by default.
Definition: ModPrefix.cs:107
Can modify the shoot speed of the weapon
Mod is an abstract class that you will override. It serves as a central place from which the mod&#39;s co...
Definition: Mod.cs:23
Will not appear by default. Useful as prefixes for your own damage type.
Can modify the mana usage of the weapon
virtual void AutoDefaults()
Definition: ModPrefix.cs:130
virtual void ValidateItem(Item item, ref bool invalid)
Validates whether this prefix with the custom data stats set from SetStats is allowed on the given it...
Definition: ModPrefix.cs:152
Can modify the size of the weapon