forked from CyrusTheLesser/Citruslib
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCustomLootTables.cs
More file actions
376 lines (279 loc) · 10.4 KB
/
Copy pathCustomLootTables.cs
File metadata and controls
376 lines (279 loc) · 10.4 KB
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;
namespace CitrusLib
{
class CustomLootTables
{
internal static string LOOTTABLEKEY = "loot table location";
static string FilePath = null;
internal static MatchModifier[] vanillaMods;
public static List<LootTable> LootTables;
static string path
{
get
{
if (FilePath != null)
{
return FilePath;
}
else
{
GameSetting g;
if (!Citrus.ExtraSettings.TryGetSetting(LOOTTABLEKEY, out g))
{
Citrus.log.LogError("filepath setting for loottables is missing!");
}
string res = g.value;
bool absolute = res != "";
//path = path + ".txt";
string text = res;
if (!absolute)
{
DirectoryInfo directoryInfo = new DirectoryInfo(Application.dataPath);
text = directoryInfo.Parent.FullName;
}
text = Path.Combine(text, "LootTables");
FilePath = text;
return FilePath;
}
}
}
//plan: designate folder location in ExtraSettings. EVERY file inside MUST be a valid json loot table, all of which will override the static loot table that loads on... load.
//honestly, this doesnt need to be in a seperate file but it may as well be for better feature organization.
//...in fact, i should probably break main up into smaller files. all those chat commands...
//register settings
internal static void Register()
{
Citrus.ExtraSettings.AddSetting(new GameSetting()
{
name = LOOTTABLEKEY,
description = "The FOLDER that will hold a loottables FOLDER inside of it, which, in turn, will hold the customizable loot tables. LEAVE BLANK for a local folder",
value = ""
});
}
//loads all the loottables from the folder. if the folder is empty or doesnt exist, it populates the folder with "example" tables: exact copies of the vanilla loot tables!
public static void ReadLoot()
{
Citrus.log.Log("Reading Loot Table Files at "+ path);
if (!Directory.Exists(path))
{
Citrus.log.Log("creating loot folder");
Directory.CreateDirectory(path);
}
List<string> names = Directory.GetFiles(path,"*.json").ToList();
//filter out any non-json files (BUT THERE SHOULD BE NONE!)
//List<string> files = names.Where(n => n.EndsWith(".json")).ToList();
if (names.Count == 0)
{
//darn...
Citrus.log.Log("no json files in the filepath folder! adding defaults");
CreateDefaultLoot();
}
LootTables = new List<LootTable>();
foreach (string data in names.ConvertAll((s)=>
{
Citrus.log.Log( Path.GetFileName(s));
return File.ReadAllText(s); //so cool
}))
{
LootTables.Add(JsonConvert.DeserializeObject<LootTable>(data));
}
}
//rolls a random Loot Table (identical to tabg's MatchModifier) using customizeable weights
public static MatchModifier RandomMatchModifier()
{
if(LootTables == null)
{
Citrus.log.Log("huh. randommatchmod is called before gamestart? loading loottables early then!");
ReadLoot();
}
float total = 0;
if (LootTables.Count() == 0)
{
Citrus.log.LogError("no loot table, still???");
return null;
}
foreach(LootTable lt in LootTables)
{
total += lt.weight;
}
//maaan i forgot i need to make a matchmodifier somehow
float num= UnityEngine.Random.Range(0f, total);
foreach (LootTable lt in LootTables)
{
num -= lt.weight;
if (num <= 0)
{
return ToModifier(lt);
}
}
return null; //:)
}
static MatchModifier ToModifier(LootTable lt)
{
//guh
MatchModifier mm = new MatchModifier
{
ModifierTitle = lt.name,
Index = lt.index
};
//OH AND THE LOOT PRESET TOO
LootPreset lp = (LootPreset)ScriptableObject.CreateInstance(typeof(LootPreset));
lp.m_forceSpawn = false;
lp.loot = new List<LootDropWrapper>();
//oh, and the loot wrappers...
foreach(LootTableEntry le in lt.entries)
{
List<Loot> li = new List<Loot>();
foreach(LootTableItem lti in le.entries) //the naming conventions arent confusing yet
{
GameObject lGo = PickupObject(lti.id);
Loot loot = new Loot()
{
quanitity = lti.amount,
loot = lGo
};
li.Add(loot);
}
LootDropWrapper ldw = new LootDropWrapper(null) //probably works
{
lootName = le.name,
spawnRate = le.weight,
m_loot = li.ToArray()
};
lp.loot.Add(ldw);
}
mm.Preset = lp;
return mm; //hooray!
}
//converts the vanilla rarity to improved float rarity
static float vRarityCalc(Curse.Rarity rarity)
{
float num3 = 0.1f;
float num4 = 10f;
float num5 = 0.5f;
float num6 = 0.1f;
float num7 = 0.01f;
num5 *= num3;
num6 *= num3;
num7 *= num3;
//dont ask.
if (rarity == Curse.Rarity.Common)
{
return num4;
}
if (rarity == Curse.Rarity.Rare)
{
return num5;
}
if (rarity == Curse.Rarity.Epic)
{
return num6;
}
if (rarity == Curse.Rarity.Legendary)
{
return num7;
}
return 1;
}
static int PickupId(GameObject g)
{
Pickup p = g.GetComponent<Pickup>();
if (p == null) return -1; //hell on earth
return p.m_itemIndex;
}
static GameObject PickupObject(int id)
{
return LootDatabase.Instance.GetDataEntry(id).prefab;
/*
Pickup ent = LootDatabase.Instance.GetAllLoot().Find((p) => p.m_itemIndex == id);
if(ent == null)
{
Citrus.log.LogError(string.Format("invalid pickup id {0} when generating loot table!",id));
}
return ent.gameObject;*/ //i think this is okay. i hope this is okay.
}
static void CreateDefaultLoot()
{
List<LootTable> loots = new List<LootTable>();
// Citrus.log.Log("1");
//im not sure if this list ALSO needs to be altered/ovverwritten
TABGLootPresetDatabase.Instance.GetAllLootPresets();
foreach(MatchModifier m in vanillaMods)
{
LootTable nloot = new LootTable
{
name = m.ModifierTitle,
weight = vRarityCalc(m.Rarity),
index = m.Index
};
nloot.entries = new List<LootTableEntry>();
foreach (LootDropWrapper l in m.Preset.loot)
{
LootTableEntry ent = new LootTableEntry
{
name = l.lootName,
weight = l.spawnRate
};
ent.entries = new List<LootTableItem>();
foreach (Loot lo in l.m_loot)
{
LootTableItem lti = new LootTableItem
{
id = PickupId(lo.loot),
amount = lo.quanitity
};
ent.entries.Add(lti); //adds items to entries
}
nloot.entries.Add(ent); //adds entries to tables
}
loots.Add(nloot);//adds loot tables to loot tables list! whew!
}
LootTables = loots;
WriteAllLootTables();
}
static void WriteAllLootTables()
{
Citrus.log.Log("Writing all loot tables!");
foreach (LootTable t in LootTables)
{
File.WriteAllText(path+"/"+t.name+".json", JsonConvert.SerializeObject(t, Formatting.Indented));
}
Citrus.log.Log("hooray!");
}
//secretly, its a mixup of the Matchmodifer and
[System.Serializable]
public class LootTable
{
[JsonProperty(Order = 0)]
public string name;
[JsonProperty(Order = 1)]
public float weight;
[JsonProperty(Order = 2)]
public int index;
[JsonProperty(Order = 3)]
public List<LootTableEntry> entries;
}
[System.Serializable]
public class LootTableEntry
{
[JsonProperty(Order = 0)]
public string name;
[JsonProperty(Order = 1)]
public float weight;
[JsonProperty(Order = 2)]
public List<LootTableItem> entries;
}
[System.Serializable]
public class LootTableItem
{
[JsonProperty(Order = 0)]
public int id;
[JsonProperty(Order = 1)]
public int amount;
}
}
}