-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRecipeHelper.cs
435 lines (413 loc) · 18.2 KB
/
RecipeHelper.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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
using HarmonyLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VoxelTycoon;
using VoxelTycoon.Buildings;
using VoxelTycoon.Recipes;
using VoxelTycoon.Researches;
using static ScheduleStopwatch.VehicleScheduleCapacity;
using XMNUtils;
using UnityEngine;
namespace ScheduleStopwatch
{
class RecipeHelper : LazyManager<RecipeHelper>
{
private const float CONVEYOR_ITEMS_PER_SECOND = 1;
protected override void OnInitialize()
{
base.OnInitialize();
LazyManager<ResearchManager>.Current.ResearchCompleted += OnResearchCompleted;
}
private void OnResearchCompleted(Research research)
{
_minedItemsPerMonth = null;
}
private (List<RecipeItem> rawItems, Dictionary<Item, float> subItems, Dictionary<Recipe, float> recipes) FindIngredients(Item item, float? coeficient = null)
{
List<RecipeItem> cacheResultList;
Dictionary<Item, float> cacheResultSubItemList;
Dictionary<Recipe, float> cacheResultRecipes;
if (!this._itemToIngredients.TryGetValue(item, out cacheResultList))
{
Dictionary<Item, RecipeItem> dictItems = new Dictionary<Item, RecipeItem>();
cacheResultList = (this._itemToIngredients[item] = new List<RecipeItem>());
cacheResultSubItemList = this._itemToSubitems[item] = new Dictionary<Item, float>();
cacheResultRecipes = this._itemToSubRecipes[item] = new Dictionary<Recipe, float>();
(Recipe recipe, float outputCount) = GetRecipe(item);
if (recipe != null)
{
float localCoeficient = 1 / outputCount;
AddRecipeToDict(cacheResultRecipes, recipe, localCoeficient);
foreach (RecipeItem recipeItem in recipe.InputItems)
{
(List<RecipeItem> ingredients, Dictionary<Item, float> subItems, Dictionary<Recipe, float> subRecipes) = this.FindIngredients(recipeItem.Item, localCoeficient * recipeItem.Count);
if (ingredients.Count > 0)
{
AddRecipeItemToDict(cacheResultSubItemList, recipeItem.Item, localCoeficient * recipeItem.Count);
foreach (RecipeItem ingredient in ingredients)
{
AddRecipeItemToDict(dictItems, ingredient.Item, ingredient.Count);
}
foreach (KeyValuePair<Item, float> subItem in subItems)
{
AddRecipeItemToDict(cacheResultSubItemList, subItem.Key, subItem.Value);
}
foreach (KeyValuePair<Recipe, float> pair in subRecipes)
{
AddRecipeToDict(cacheResultRecipes, pair.Key, pair.Value);
}
} else
{
//add item as ingredient
AddRecipeItemToDict(dictItems, recipeItem.Item, localCoeficient * recipeItem.Count);
}
}
cacheResultList.AddRange(dictItems.Values);
}
} else
{
cacheResultSubItemList = _itemToSubitems[item];
cacheResultRecipes = _itemToSubRecipes[item];
}
if (coeficient != null && coeficient.Value != 1)
{
List<RecipeItem> resultList = new List<RecipeItem>();
Dictionary<Item, float> resultSubItemList = new Dictionary<Item, float>();
Dictionary<Recipe, float> resultRecipes = new Dictionary<Recipe, float>();
foreach (RecipeItem rawItem in cacheResultList)
{
RecipeItem newItem = new RecipeItem();
newItem.Count = rawItem.Count * coeficient.Value;
newItem.Item = rawItem.Item;
resultList.Add(newItem);
}
foreach (KeyValuePair<Item, float> subItem in cacheResultSubItemList) {
AddRecipeItemToDict(resultSubItemList, subItem.Key, subItem.Value * coeficient.Value);
}
foreach (KeyValuePair<Recipe, float> recipe in cacheResultRecipes)
{
AddRecipeToDict(resultRecipes, recipe.Key, recipe.Value * coeficient.Value);
}
return (resultList, resultSubItemList, resultRecipes);
}
return (cacheResultList, cacheResultSubItemList, cacheResultRecipes);
}
public static void AddItem(Dictionary<Item, int> items, Item item, int count)
{
if (items.TryGetValue(item, out int oldCount))
{
items[item] = oldCount + count;
}
else
{
items.Add(item, count);
}
}
public static void AddItems(Dictionary<Item, int> items, IReadOnlyDictionary<Item, TransferData> itemsToAdd, TransferDirection direction)
{
foreach (KeyValuePair<Item, TransferData> pair in itemsToAdd)
{
int count = pair.Value.Get(direction);
if (count != 0)
{
AddItem(items, pair.Key, count);
}
}
}
public static void AddItems(Dictionary<Item, int> items, IReadOnlyDictionary<Item, int> itemsToAdd)
{
foreach (KeyValuePair<Item, int> pair in itemsToAdd)
{
AddItem(items, pair.Key, pair.Value);
}
}
public ImmutableList<RecipeItem> GetIngredients(Item item)
{
(List<RecipeItem> ingredients, _, _) = FindIngredients(item);
return ingredients.ToImmutableList<RecipeItem>();
}
public List<RecipeItem> GetIngredients(Item item, List<Item> finalItems, float? multiplier = null, Dictionary<Recipe, float> recipesNeeded = null)
{
(List<RecipeItem> ingredients, Dictionary<Item, float> subItems, Dictionary<Recipe, float> recipes) = FindIngredients(item);
Dictionary<Item, float> tmpDict = new Dictionary<Item, float>();
foreach (RecipeItem ingrItem in ingredients)
{
tmpDict.Add(ingrItem.Item, ingrItem.Count);
}
Dictionary<Item, float> itemsToProcess = new Dictionary<Item, float>();
foreach (Item finalItem in finalItems)
{
if (subItems.TryGetValue(finalItem, out float count)) {
itemsToProcess.Add(finalItem, count);
}
}
Dictionary<Recipe, float> tmpRecipes = null;
if (recipesNeeded != null)
{
tmpRecipes = new Dictionary<Recipe, float>();
foreach (KeyValuePair<Recipe, float> recipe in recipes)
{
tmpRecipes[recipe.Key] = recipe.Value;
}
}
//subtract ingredients of finalItems
bool finished = itemsToProcess.Count == 0;
while (!finished)
{
finished = true;
foreach (KeyValuePair<Item, float> pair in itemsToProcess.ToArray<KeyValuePair<Item, float>>())
{
float value = itemsToProcess[pair.Key];
if (value != 0)
{
//subtract all ingredients of final item and add final item to result ingredients
tmpDict.AddFloatToDict(pair.Key, value);
itemsToProcess[pair.Key] = 0;
(List<RecipeItem> subIngredients, Dictionary<Item, float> subItems2, Dictionary<Recipe, float> subRecipes) = FindIngredients(pair.Key);
foreach (RecipeItem ingrItem in subIngredients)
{
tmpDict.AddFloatToDict(ingrItem.Item, -ingrItem.Count * value);
}
if (tmpRecipes != null)
{
foreach (KeyValuePair<Recipe, float> subRecipe in subRecipes)
{
tmpRecipes.AddFloatToDict(subRecipe.Key, -subRecipe.Value * value);
}
}
if (subItems2.Count>0)
{
//subtract subitems of final item from final items count
foreach (Item item2 in itemsToProcess.Keys.ToArray())
{
finished = false;
if (subItems2.TryGetValue(item2, out float subItemCount))
{
itemsToProcess[item2] -= subItemCount;
}
}
}
}
}
if (finished)
{
//look for negative items count (caused when there some item in finalItems is a subitem or ingredient of another in finalItems
foreach (KeyValuePair<Item, float> pair in tmpDict)
{
if (pair.Value < 0)
{
if (!itemsToProcess.TryGetValue(pair.Key, out float value))
{
value = 0;
}
itemsToProcess[pair.Key] = value - pair.Value;
finished = false;
}
}
}
}
List<RecipeItem> result = new List<RecipeItem>();
if (multiplier == null)
{
multiplier = 1;
}
foreach (KeyValuePair<Item, float> pair in tmpDict)
{
RecipeItem newItem = new RecipeItem();
newItem.Item = pair.Key;
newItem.Count = pair.Value * multiplier.Value;
result.Add(newItem);
// FileLog.Log(pair.Key.DisplayName + ": " + pair.Value.ToString("N2"));
}
if (recipesNeeded != null)
{
foreach(KeyValuePair<Recipe, float> recipe in tmpRecipes)
{
if (recipe.Value != 0)
{
AddRecipeToDict(recipesNeeded, recipe.Key, recipe.Value * multiplier.Value);
}
}
}
return result;
}
public (int? count, Mine mine) GetMinedItemsPerMineAndMonth(Item item)
{
if (_minedItemsPerMonth == null)
{
CalculateMinedItemsPerMonth();
}
if (_minedItemsPerMonth.TryGetValue(item, out (int count, Mine mine) data)) {
return data;
}
return (null, null);
}
public Device GetDevice(Recipe recipe)
{
if (_recipeToDevice == null)
{
CreateRecipeToDevice();
}
_recipeToDevice.TryGetValue(recipe, out Device result);
return result;
}
public Dictionary<(Device device, Recipe recipe), float> GetNeededDevicesPerMonth(Dictionary<Recipe, float> recipesAmounts)
{
Dictionary<(Device device, Recipe recipe), float> result = new();
foreach (KeyValuePair<Recipe, float> recipeAmount in recipesAmounts)
{
Device device = GetDevice(recipeAmount.Key);
if (device)
{
float loadingSeconds = Math.Max(0, _maxInputItemsPerRecipe[recipeAmount.Key] - 1) * CONVEYOR_ITEMS_PER_SECOND;
float devicesCount = ((recipeAmount.Key.Duration + loadingSeconds) * recipeAmount.Value * TimeManager.GameMonthsPerSecond);
result.AddFloatToDict((device, recipeAmount.Key), devicesCount);
}
}
return result;
}
public int GetRecipeInputItemMaxAmount(Recipe recipe)
{
return _maxInputItemsPerRecipe[recipe];
}
private static int CalculateRecipeInputItemMaxAmount(Recipe recipe)
{
float max = 0;
foreach (RecipeItem recipeItem in recipe.InputItems)
{
if (recipeItem.Count > max)
{
max = recipeItem.Count;
}
}
return Mathf.RoundToInt(max);
}
private void AddRecipeItemToDict(Dictionary<Item, RecipeItem> dictionary, Item item, float count)
{
if (!dictionary.TryGetValue(item, out RecipeItem recipeItem))
{
recipeItem = dictionary[item] = new RecipeItem();
recipeItem.Item = item;
recipeItem.Count = count;
} else
{
recipeItem.Count += count;
}
}
private void AddRecipeItemToDict(Dictionary<Item, float> dictionary, Item item, float count)
{
if (!dictionary.TryGetValue(item, out float dictCount))
{
dictionary.Add(item, count);
}
else
{
dictionary[item] = dictCount+count;
}
}
private void AddRecipeToDict(Dictionary<Recipe, float> dictionary, Recipe recipe, float count)
{
if (!dictionary.TryGetValue(recipe, out float dictCount))
{
dictionary.Add(recipe, count);
}
else
{
dictionary[recipe] = dictCount + count;
}
}
private (Recipe recipe, float outputCount) GetRecipe(Item item)
{
if (!_itemToRecipe.TryGetValue(item, out (Recipe recipe, float outputCount) result))
{
result = (null, 0);
ImmutableList<Recipe> recipes = Recipes;
for (int i = 0; i < recipes.Count; i++)
{
RecipeItem[] outputItems = recipes[i].OutputItems;
for (int j = 0; j < outputItems.Length; j++)
{
_itemToRecipe[item] = (recipes[i], outputItems[j].Count);
if (outputItems[j].Item == item)
{
result = _itemToRecipe[item];
}
}
if (result.recipe != null)
{
return result;
}
}
}
return result;
}
private void CalculateMinedItemsPerMonth()
{
List<Mine> mines = Manager<AssetLibrary>.Current.GetAll<Mine>();
BuildingRecipeManager buildMan = LazyManager<BuildingRecipeManager>.Current;
_minedItemsPerMonth = new Dictionary<Item, ValueTuple<int, Mine>>();
foreach (Mine mine in mines)
{
BuildingRecipe recipe = buildMan.Get(mine.AssetId);
if (recipe.Hidden == false && recipe.IsUnlocked == true)
{
int itemsPerMonth = (int)Math.Round(1 / mine.SharedData.OutputInterval / TimeManager.GameMonthsPerSecond);
var storages = LazyManager<StorageManager>.Current.GetStorages(mine.AssetId);
if (storages != null)
{
foreach (Storage storage in storages.ToList())
{
bool noChange;
if (noChange = _minedItemsPerMonth.TryGetValue(storage.Item, out (int count , Mine itemMine) itemData))
{
if (itemData.count < itemsPerMonth)
{
noChange = false;
}
}
if (!noChange)
{
_minedItemsPerMonth[storage.Item] = (itemsPerMonth, mine);
}
}
}
}
}
}
private ImmutableList<Recipe> Recipes
{
get
{
if (_recipes == null)
{
_recipes = Manager<RecipeManager>.Current.GetRecipes();
}
return _recipes.Value;
}
}
private void CreateRecipeToDevice()
{
_recipeToDevice = new Dictionary<Recipe, Device>();
List<Device> devices = Manager<AssetLibrary>.Current.GetAll<Device>();
RecipeManager recipeMan = Manager<RecipeManager>.Current;
foreach (Device device in devices)
{
ImmutableList<Recipe> recipes = recipeMan.GetRecipes(device.SharedData.RecipeTarget);
foreach (Recipe recipe in recipes.ToArray())
{
_recipeToDevice.Add(recipe, device);
}
}
}
private readonly Dictionary<Item, (Recipe recipe, float outputCount)> _itemToRecipe = new Dictionary<Item, (Recipe recipe, float outputCount)>();
private ImmutableList<Recipe>? _recipes;
private readonly Dictionary<Item, List<RecipeItem>> _itemToIngredients = new Dictionary<Item, List<RecipeItem>>();
private readonly Dictionary<Item, Dictionary<Item, float>> _itemToSubitems = new Dictionary<Item, Dictionary<Item, float>>();
private readonly Dictionary<Item, Dictionary<Recipe, float>> _itemToSubRecipes = new Dictionary<Item, Dictionary<Recipe, float>>();
private Dictionary<Item, ValueTuple<int, Mine>> _minedItemsPerMonth = null;
private Dictionary<Recipe, Device> _recipeToDevice = null;
private LazyDictionary<Recipe, int> _maxInputItemsPerRecipe = new LazyDictionary<Recipe, int>(CalculateRecipeInputItemMaxAmount);
}
}