-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlugin.cs
More file actions
306 lines (273 loc) · 10.9 KB
/
Plugin.cs
File metadata and controls
306 lines (273 loc) · 10.9 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
using BepInEx;
using HarmonyLib;
using BepInEx.Logging;
using System.Linq;
using System.IO;
using System.Collections.Generic;
using static Harvestable;
namespace DataLoaderPlugin
{
[BepInPlugin("bepinex.plugins.stacklands.dataloader", PluginInfo.PLUGIN_NAME, PluginInfo.PLUGIN_VERSION)]
[BepInProcess("Stacklands.exe")]
public class Plugin : BaseUnityPlugin
{
static ManualLogSource Log;
private void Awake()
{
Plugin.Log = base.Logger;
Logger.LogInfo($"Plugin {PluginInfo.PLUGIN_GUID} is loaded!");
Harmony.CreateAndPatchAll(typeof(Plugin));
}
[HarmonyPatch(typeof(WorldManager), "Play")]
[HarmonyPostfix]
private static void WorldManager__Load(ref WorldManager __instance)
{
load_boosters(__instance);
load_harvestable(__instance);
load_mobs(__instance);
blue_prints(__instance);
travelling_cart(__instance);
treasure_chest(__instance);
}
private static void treasure_chest(WorldManager worldManager)
{
Plugin.Log.LogInfo($"Simulate Treasure Chest");
List<CardData> cards = (from x in WorldManager.instance.CardDataPrefabs
where (x.MyCardType == CardType.Resources || x.MyCardType == CardType.Food) && !x.IsIslandCard
select x).ToList<CardData>();
cards.RemoveAll((CardData x) => x.Id == "goblet");
float chance = 1 / (float)cards.Count;
StreamWriter recipes = File.CreateText(@"treasure-chest-recipes.yaml");
foreach(var card in cards)
{
recipes.Write("-\n");
recipes.Write($" inp: {{treasure_chest: 1, key: 1}}\n");
recipes.Write($" out: {{{card.Id}: 1}}\n");
recipes.Write($" chance: {chance}\n");
}
recipes.Close();
}
private static void travelling_cart(WorldManager worldManager)
{
Plugin.Log.LogInfo($"Load Travelling cart");
var travelling_cart = (TravellingCart) worldManager.GetCardPrefab("travelling_cart");
StreamWriter recipes = File.CreateText(@"travelling-cart-recipes.yaml");
var new_chances = get_bag_chances(travelling_cart.MyCardBag);
foreach(var cardChance in new_chances)
{
recipes.Write("-\n");
recipes.Write($" inp: {{travelling_cart: 1, coin: 5}}\n");
recipes.Write($" out: {{{cardChance.Key}: 1}}\n");
recipes.Write($" chance: {cardChance.Value}\n");
}
recipes.Close();
}
private static void load_boosters(WorldManager worldManager)
{
Plugin.Log.LogInfo($"Load Boosters");
var boosterPackPrefabs = worldManager.GameDataLoader.BoosterPackPrefabs;
string path = @"boosters-recipes.yaml";
StreamWriter sw = File.CreateText(path);
foreach(var booster_pref in boosterPackPrefabs)
{
foreach(var card_chance in calc_booster_chances(booster_pref))
{
sw.Write($" {card_chance.Key}: {card_chance.Value}\n");
}
}
sw.Close();
}
private static void load_harvestable(WorldManager worldManager)
{
Plugin.Log.LogInfo($"Load Harvestable");
var cards = worldManager.GameDataLoader.CardDataPrefabs;
var harvestable_cards = (from x in cards
where typeof(Harvestable).IsInstanceOfType(x)
select (Harvestable)x).ToList<Harvestable>();
string path = @"harvestable.yaml";
StreamWriter sw = File.CreateText(path);
foreach(var card in harvestable_cards)
{
sw.Write($"{card.Id}:\n");
sw.Write($" cards:\n");
foreach(var card_chance in get_bag_chances(card.MyCardBag))
{
sw.Write($" {card_chance.Key}: {card_chance.Value}\n");
}
sw.Write($" time: {card.HarvestTime}\n");
}
var combatable_harvestable_cards = (from x in cards
where typeof(CombatableHarvestable).IsInstanceOfType(x)
select (CombatableHarvestable)x).ToList<CombatableHarvestable>();
foreach(var card in combatable_harvestable_cards)
{
sw.Write($"{card.Id}:\n");
sw.Write($" cards:\n");
foreach(var card_chance in get_bag_chances(card.MyCardBag))
{
sw.Write($" {card_chance.Key}: {card_chance.Value}\n");
}
sw.Write($" time: {card.HarvestTime}\n");
}
sw.Close();
}
private static void load_mobs(WorldManager worldManager)
{
Plugin.Log.LogInfo($"Load Mobs");
var cards = worldManager.GameDataLoader.CardDataPrefabs;
var mobs_cards = (from x in cards
where typeof(Mob).IsInstanceOfType(x)
select (Mob)x).ToList<Mob>();
string path = @"mobs.yaml";
StreamWriter sw = File.CreateText(path);
StreamWriter animals_recipes = File.CreateText(@"animals_recipes.yaml");
string csv_path = @"mobs.csv";
StreamWriter csv = File.CreateText(csv_path);
csv.Write($"Name,drop_count\n");
foreach(var card in mobs_cards)
{
if (typeof(Animal).IsInstanceOfType(card))
{
var animal = (Animal)card;
if (animal.CreateCard != "")
{
animals_recipes.Write($"-\n");
animals_recipes.Write($" inp: {{{card.Id}: 1}}\n");
animals_recipes.Write($" out: {{{animal.CreateCard}: 1}}\n");
animals_recipes.Write($" time: {animal.CreateTime}\n");
}
}
csv.Write($"{card.Id},");
csv.Write($"{card.Drops.CardsInPack}\n");
sw.Write($"{card.Id}:\n");
foreach(var card_chance in get_bag_chances(card.Drops))
{
sw.Write($" {card_chance.Key}: {card_chance.Value}\n");
}
}
animals_recipes.Close();
csv.Close();
sw.Close();
}
private static void blue_prints(WorldManager worldManager)
{
Plugin.Log.LogInfo($"Load Blueprints");
var blueprints = worldManager.GameDataLoader.BlueprintPrefabs;
string path = @"recipes.yaml";
StreamWriter sw = File.CreateText(path);
foreach(var blueprint in blueprints)
{
foreach(var subprint in blueprint.Subprints)
{
sw.Write($"-\n");
sw.Write(" inp: {");
print_cards(sw, subprint.RequiredCards);
sw.Write("}\n");
if (string.IsNullOrEmpty(subprint.ResultCard))
{
sw.Write(" out: {");
print_cards(sw, subprint.ExtraResultCards);
sw.Write("}\n");
}
else
{
sw.Write($" out: {{{subprint.ResultCard}: 1}}\n");
}
sw.Write($" time: {subprint.Time}\n");
}
}
sw.Close();
}
private static Dictionary<string, float> get_bag_chances(CardBag bag)
{
var result = new Dictionary<string, float>();
if (bag.CardBagType == CardBagType.SetPack)
{
var card_name = bag.SetPackCards[bag.SetPackCards.Count - bag.CardsInPack];
result.Add(card_name, 100.0f);
return result;
}
else
{
var chances = new List<CardChance>();
if (bag.CardBagType == CardBagType.Chances)
{
chances = bag.Chances;
}
else if (bag.CardBagType == CardBagType.SetCardBag)
{
if (bag.UseFallbackBag)
{
chances = CardBag.GetChancesForSetCardBag(WorldManager.instance.GameDataLoader, bag.SetCardBag, new SetCardBag?(bag.FallbackBag));
}
else
{
chances = CardBag.GetChancesForSetCardBag(WorldManager.instance.GameDataLoader, bag.SetCardBag, null);
}
}
var chance_sum = Enumerable.Sum(chances, (CardChance x) => x.Chance);
foreach(var cardChance in chances)
{
result[cardChance.Id]= (float)cardChance.Chance / (float)chance_sum;
}
return result;
}
}
private static Dictionary<string, float> calc_booster_chances(Boosterpack booster)
{
var result = new Dictionary<string, float>();
for(int i = 1; i <= booster.TotalCardsInPack; i++)
{
CardBag currentCardBag = get_bag(booster, i);
var new_chances = get_bag_chances(currentCardBag);
foreach(var cardChance in new_chances)
{
if(result.ContainsKey(cardChance.Key))
{
result[cardChance.Key] += ( 1- result[cardChance.Key]) * cardChance.Value;
}
else
{
result[cardChance.Key] = cardChance.Value;
}
}
}
return result;
}
private static CardBag get_bag(Boosterpack booster, int tap_number)
{
int tap = tap_number;
foreach(var bag in booster.CardBags)
{
for(int i = 0; i < bag.CardsInPack; i++)
{
tap--;
if(tap == 0)
{
return bag;
}
}
}
throw new System.Exception("bag overflow");
}
private static void print_cards(StreamWriter stream, string[] cards)
{
var inputs = new Dictionary<string, int>();
foreach(var card in cards)
{
if (inputs.ContainsKey(card))
{
inputs[card] += 1;
}
else
{
inputs[card] = 1;
}
}
foreach(var card in inputs)
{
stream.Write($"{card.Key}: {card.Value}, ");
}
}
}
}