Even More Modifiers  1.0.0.0
A mod for rolling various bonus stats on items
ModifierPool.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Linq;
5 using System.Reflection;
6 using Loot.Api.Attributes;
7 using Loot.Api.Content;
8 using Loot.Api.Ext;
9 using Loot.Api.Loaders;
10 using Terraria;
11 using Terraria.ModLoader;
12 using Terraria.ModLoader.IO;
13 
14 namespace Loot.Api.Core
15 {
16  [DoNotLoad]
18  {
19  public readonly List<Modifier> Modifiers;
20 
21  public FiniteModifierPool(List<Modifier> modifiers)
22  {
23  Modifiers = modifiers ?? new List<Modifier>();
24  }
25 
26  public void Apply(Item item)
27  {
28  Modifiers.ForEach(mod => mod.Apply(item));
29  }
30  }
31 
37  public abstract class ModifierPool : ILoadableContent, ILoadableContentSetter, ICloneable
38  {
39  public Mod Mod { get; internal set; }
40 
41  Mod ILoadableContentSetter.Mod
42  {
43  set => Mod = value;
44  }
45 
46  public uint Type { get; internal set; }
47 
48  uint ILoadableContentSetter.Type
49  {
50  set => Type = value;
51  }
52 
53  public string Name => GetType().Name;
54 
56 
57  internal void CacheAttributes()
58  {
59  _populatePoolFrom = GetType().GetCustomAttribute<PopulatePoolFromAttribute>(true);
60  }
61 
62  private IEnumerable<Modifier> _GetModifiers()
63  {
64  if (_populatePoolFrom != null)
65  {
66  var classes = _populatePoolFrom.GetClasses().SelectMany(x => x.Value);
67 
68  foreach (var @class in classes)
69  {
70  var m = ContentLoader.Modifier.GetContent(@class);
71  if (m != null)
72  {
73  yield return m;
74  }
75  }
76  }
77 
78  foreach (Modifier m in GetModifiers())
79  {
80  if (m != null)
81  {
82  yield return m;
83  }
84  }
85  }
86 
87  public virtual IEnumerable<Modifier> GetModifiers()
88  => Enumerable.Empty<Modifier>();
89 
95  protected internal IEnumerable<Modifier> GetRollableModifiers(ModifierContext ctx)
96  => _GetModifiers().Where(x => x._CanRoll(ctx));
97 
101  //public float TotalRarityLevel
102  // => ActiveModifiers.Select(m => m.Properties.RarityLevel).DefaultIfEmpty(0).Sum();
103 
107  //public IEnumerable<ModifierTooltipLine[]> Description
108  // => ActiveModifiers.Select(m => m.GetTooltip().Build().ToArray());
109 
110  public virtual float RollChance => 1f;
111 
112  //internal float ModifierRollChance(int len) => 0.5f / (float)Math.Pow(2, len);
113 
118  protected internal bool _CanRoll(ModifierContext ctx)
119  => CanRoll(ctx);
120 
125  public virtual bool CanRoll(ModifierContext ctx)
126  => ctx.Item?.IsModifierRollableItem() ?? true;
127 
132  internal void ApplyModifiers(Item item)
133  {
134  //foreach (Modifier m in ActiveModifiers)
135  //{
136  // m.Apply(item);
137  //}
138  }
139 
144  public virtual void Clone(ref ModifierPool clone)
145  {
146  }
147 
148  public object Clone()
149  {
150  ModifierPool clone = (ModifierPool)MemberwiseClone();
151  clone.Type = Type;
152  clone.Mod = Mod;
153  //clone.ActiveModifiers =
154  // ActiveModifiers?
155  // .Select(x => (Modifier)x?.Clone() ?? Loot.Instance.GetNullModifier())
156  // .ToArray();
157  Clone(ref clone);
158  return clone;
159  }
160 
164  public virtual void NetReceive(Item item, BinaryReader reader)
165  {
166  }
167 
171  public virtual void NetSend(Item item, BinaryWriter writer)
172  {
173  }
174 
179  public virtual void Load(Item item, TagCompound tag)
180  {
181  }
182 
188  public virtual void Save(Item item, TagCompound tag)
189  {
190  }
191  }
192 }
virtual void Clone(ref ModifierPool clone)
Allows modders to do custom cloning Happens after default cloning
Defines a modifier, which is an unloaded GlobalItem Making it a GlobalItem gives easy access to all h...
Definition: Modifier.cs:21
Defines a context in which a Modifier might be rolled Which fields are available (not null) depends o...
static ModifierContent Modifier
Will populate a ModifierPool based on a namespace All Modifiers in that namespace will become part of...
virtual void NetSend(Item item, BinaryWriter writer)
Allows modder to do custom NetSend here
virtual 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)
virtual 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...
Defines a modifier pool. A modifier pool holds a certain amount of effects in an array It allows to r...
Definition: ModifierPool.cs:37
readonly List< Modifier > Modifiers
Definition: ModifierPool.cs:19
Dictionary< string, List< Type > > GetClasses()
Defines a piece of loadable content The interface is used in classes where the deriving class already...
FiniteModifierPool(List< Modifier > modifiers)
Definition: ModifierPool.cs:21
PopulatePoolFromAttribute _populatePoolFrom
Definition: ModifierPool.cs:55
This class holds all Content holders of this mod You can use this to access content loaded into the m...
IEnumerable< Modifier > _GetModifiers()
Definition: ModifierPool.cs:62
virtual void NetReceive(Item item, BinaryReader reader)
Allows the modder to do custom NetReceive