-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDropableWrapper.cs
283 lines (242 loc) · 10.2 KB
/
DropableWrapper.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
using SideLoader;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OutwardGuaranteedDrop = GuaranteedDrop;
namespace Nm1fiOutward.Drops
{
public class DropableWrapper
{
public int Index { get; private set; } = -1;
public bool Simulated { get; private set; } = false;
public Dropable Real { get; private set; }
public string Name => Real?.name;
public string UID => Real?.UID;
public bool HasChanges { get; private set; } = false;
public List<GuaranteedDropWrapper> GuaranteedDroppers { get; private set; }
public List<DropTableWrapper> ChanceDroppers { get; private set; }
public DropableWrapper() : this(-1, null, true) { }
public DropableWrapper(Dropable real, bool simulate = false) : this(-1, real, simulate) { }
public DropableWrapper(int index, Dropable real, bool simulate = false)
{
Index = index;
Real = real;
Simulated = simulate;
if (real != null && real.GetComponentsInChildren<OutwardGuaranteedDrop>() is OutwardGuaranteedDrop[] guaranteed && guaranteed.Any())
GuaranteedDroppers = guaranteed.Select((dropper, i) => new GuaranteedDropWrapper(i, dropper, this, simulate)).ToList();
else
GuaranteedDroppers = new List<GuaranteedDropWrapper>();
if (real != null && real.GetComponentsInChildren<DropTable>() is DropTable[] chance && chance.Any())
ChanceDroppers = chance.Select((dropper, i) => new DropTableWrapper(i, dropper, this, simulate)).ToList();
else
ChanceDroppers = new List<DropTableWrapper>();
}
public static implicit operator Dropable(DropableWrapper wrapped)
=> wrapped.Real;
public void SetHasChanges() => HasChanges = true;
public GuaranteedDropWrapper NewGuaranteedDropper(string name = null)
{
var dropper = new GuaranteedDropWrapper(GuaranteedDroppers.Count, null, this, Simulated);
dropper.ItemGenatorName = name;
GuaranteedDroppers.Add(dropper);
return dropper;
}
public DropTableWrapper NewChanceDropper(string name = null)
{
var dropper = new DropTableWrapper(ChanceDroppers.Count, null, this, Simulated);
dropper.ItemGenatorName = name;
ChanceDroppers.Add(dropper);
return dropper;
}
public override string ToString()
{
var builder = new StringBuilder("Dropable");
if (Index >= 0)
builder.Append(" ").Append(Index.ToString());
builder.Append(": ").Append(Name ?? "");
if (HasChanges)
builder.Append(" (altered)");
return builder.ToString();
}
public StringBuilder ToInfoString(int indent = 0, StringBuilder builder = null)
{
if (builder == null)
builder = new StringBuilder();
builder.Append(' ', indent*2).Append(this).Append(":\n");
indent++;
if (GuaranteedDroppers is List<GuaranteedDropWrapper> guaranteedDroppers)
foreach (var dropper in guaranteedDroppers)
dropper.ToInfoString(indent, builder);
if (ChanceDroppers is List<DropTableWrapper> chanceDroppers)
foreach (var dropper in chanceDroppers)
dropper.ToInfoString(indent, builder);
return builder;
}
}
public abstract class ItemDropperWrapper {
private DropableWrapper parent;
protected abstract string TypeName { get; }
public int Index { get; private set; } = -1;
public bool Simulated { get; private set; } = false;
public ItemDropper Real { get; private set; }
public bool HasChanges { get; private set; } = false;
private string itemGeneratorName;
public string ItemGenatorName {
get {
return Real != null ? Real.ItemGenatorName : itemGeneratorName;
}
set {
itemGeneratorName = value;
}
}
public abstract HashSet<int> Items { get; }
public ItemDropperWrapper() { }
public ItemDropperWrapper(int index, ItemDropper real, DropableWrapper parent, bool simulate = false)
{
Index = index;
Simulated = simulate;
Real = real;
this.parent = parent;
}
public virtual void SetHasChanges() {
parent.SetHasChanges();
HasChanges = true;
}
public abstract bool Contains(int itemID);
public override string ToString()
{
var builder = new StringBuilder(TypeName);
if (Index >= 0)
builder.Append(" ").Append(Index.ToString());
builder.Append(": ").Append(ItemGenatorName ?? "?");
if (Real == null)
builder.Append(" (additional)");
else if (HasChanges)
builder.Append(" (altered)");
return builder.ToString();
}
public abstract StringBuilder ToInfoString(int indent = 0, StringBuilder builder = null);
}
public class GuaranteedDropWrapper : ItemDropperWrapper
{
protected override string TypeName => "GuaranteedDrop";
public List<BasicItemDrop> Drops;
public bool HasDrops => Drops != null && Drops.Any();
private HashSet<int> items;
public override HashSet<int> Items {
get {
if (items == null)
items = new HashSet<int>(Drops.Select(x => (int)x));
return items;
}
}
public GuaranteedDropWrapper(int index, OutwardGuaranteedDrop real, DropableWrapper parent, bool simulate = false)
: base(index, real, parent, simulate)
{
if (real != null && At.GetField(real, "m_itemDrops") is List<BasicItemDrop> itemDrops)
{
if (simulate)
{
Drops = itemDrops.Select(drop => new BasicItemDrop() {
ItemID = drop.ItemID,
ItemRef = drop.ItemRef,
MinDropCount = drop.MinDropCount,
MaxDropCount = drop.MaxDropCount,
}).ToList();
}
else
Drops = itemDrops;
}
else
Drops = new List<BasicItemDrop>();
}
public override void SetHasChanges()
{
base.SetHasChanges();
items = null;
}
public override bool Contains(int itemID)
=> Items.Contains(itemID);
public override StringBuilder ToInfoString(int indent = 0, StringBuilder builder = null)
{
if (builder == null)
builder = new StringBuilder();
builder.Append(' ', indent * 2).Append($"- {this}:\n");
indent++;
if (Drops is List<BasicItemDrop> itemDrops)
foreach (var drop in itemDrops)
{
var count = drop.MaxDropCount > drop.MinDropCount
? $"{drop.MinDropCount}-{drop.MaxDropCount}"
: drop.MinDropCount.ToString();
builder.Append(' ', indent * 2);
builder.Append($"- x{count,-6} {drop.DroppedItem?.Name ?? "<unknown>"} ({(int)drop})\n");
}
return builder;
}
}
public class DropTableWrapper : ItemDropperWrapper
{
protected override string TypeName => "DropTable";
public List<ItemDropChance> Drops;
public bool HasDrops => Drops != null && Drops.Any();
public float AverageDropChance { get; private set; } = 1;
private HashSet<int> items;
public override HashSet<int> Items {
get {
if (items == null)
items = new HashSet<int>(Drops.Select(x => (int)x));
return items;
}
}
public DropTableWrapper(int index, DropTable real, DropableWrapper parent, bool simulate = false)
: base(index, real, parent, simulate)
{
if (real != null && At.GetField(real, "m_itemDrops") is List<ItemDropChance> itemDrops)
{
if (simulate)
{
Drops = itemDrops.Select(drop => new ItemDropChance() {
ItemID = drop.ItemID,
ItemRef = drop.ItemRef,
MinDropCount = drop.MinDropCount,
MaxDropCount = drop.MaxDropCount,
DropChance = drop.DropChance,
ChanceRegenDelay = drop.ChanceRegenDelay,
}).ToList();
}
else
Drops = itemDrops;
AverageDropChance = (float)itemDrops.Sum(x => x.DropChance) / itemDrops.Count;
}
else
Drops = new List<ItemDropChance>();
}
public override void SetHasChanges()
{
base.SetHasChanges();
if (Real != null)
((DropTable)Real).Reset(false);
items = null;
}
public override bool Contains(int itemID)
=> Items.Contains(itemID);
public override StringBuilder ToInfoString(int indent = 0, StringBuilder builder = null)
{
if (builder == null)
builder = new StringBuilder();
builder.Append(' ', indent * 2).Append($"- {this}:\n");
indent++;
if (Drops is List<ItemDropChance> itemDrops)
foreach (var drop in itemDrops)
{
var count = drop.MaxDropCount > drop.MinDropCount
? $"{drop.MinDropCount}-{drop.MaxDropCount}"
: drop.MinDropCount.ToString();
builder.Append(' ', indent * 2);
builder.Append($"- x{count,-6} Chance={drop.DropChance,-2} {drop.DroppedItem?.Name ?? "<unknown>"} ({(int)drop})\n");
}
return builder;
}
}
}