Elin Decompiled Documentation EA 23.355 Stable
Loading...
Searching...
No Matches
RecipeManager.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using Newtonsoft.Json;
4using UnityEngine;
5
6public class RecipeManager : EClass
7{
8 public enum LearnState
9 {
14 }
15
16 public static bool rebuild;
17
18 public static List<RecipeSource> list = new List<RecipeSource>();
19
20 public static Dictionary<string, RecipeSource> dict = new Dictionary<string, RecipeSource>();
21
22 [JsonProperty]
23 public HashSet<string> knownIngredients = new HashSet<string>();
24
25 [JsonProperty]
26 public HashSet<string> craftedRecipes = new HashSet<string>();
27
28 [JsonProperty]
29 public Dictionary<string, int> knownRecipes = new Dictionary<string, int>();
30
31 [JsonProperty]
32 public HashSet<string> hoveredRecipes = new HashSet<string>();
33
34 [JsonProperty]
35 public HashSet<string> newRecipes = new HashSet<string>();
36
37 [JsonProperty]
38 public Dictionary<string, List<int>> lastIngredients = new Dictionary<string, List<int>>();
39
40 public static void BuildList()
41 {
42 if (!rebuild && list.Count > 0)
43 {
44 return;
45 }
46 Debug.Log("Rebuilding recipe list");
47 list.Clear();
48 dict.Clear();
49 Recipe.recipeCache.Clear();
50 foreach (CardRow row in EClass.sources.cards.rows)
51 {
52 if (!row.isOrigin)
53 {
54 Create(row, "", row.isChara ? "-c" : "");
55 }
56 }
57 foreach (SourceBlock.Row row2 in EClass.sources.blocks.rows)
58 {
59 Create(row2, "Block");
60 if (row2.tileType == TileType.Pillar)
61 {
62 Create(row2, "BridgePillar", "-p");
63 }
64 }
65 foreach (SourceFloor.Row row3 in EClass.sources.floors.rows)
66 {
67 if (!row3.tag.Contains("noFloor"))
68 {
69 Create(row3, "Floor");
70 }
71 }
72 foreach (SourceFloor.Row row4 in EClass.sources.floors.rows)
73 {
74 if (!row4.tag.Contains("noBridge") && !row4.factory.IsEmpty() && row4.factory[0] != "x")
75 {
76 Create(row4, "Bridge", "-b");
77 }
78 }
79 foreach (SourceDeco.Row row5 in EClass.sources.decos.rows)
80 {
81 Create(row5, "Deco");
82 }
83 foreach (SourceObj.Row row6 in EClass.sources.objs.rows)
84 {
85 Create(row6, "Obj");
86 }
87 foreach (SourceCellEffect.Row row7 in EClass.sources.cellEffects.rows)
88 {
89 Create(row7, "Liquid");
90 }
91 rebuild = false;
92 }
93
94 public static void Create(RenderRow row, string type, string suffix = "")
95 {
96 RecipeSource recipeSource = new RecipeSource();
97 recipeSource.id = row.RecipeID + suffix;
98 recipeSource.isBridge = type == "Bridge";
99 recipeSource.isBridgePillar = type == "BridgePillar";
100 recipeSource.type = type;
101 recipeSource.row = row;
102 recipeSource.isChara = row is SourceChara.Row;
103 if (!row.factory.IsEmpty() && !recipeSource.isBridge && !recipeSource.isBridgePillar)
104 {
105 string text = row.factory[0];
106 if (text != "x" && text != "none" && text != "None" && text != "self" && !EClass.sources.cards.map.ContainsKey(text))
107 {
108 Debug.LogWarning("Recipe '" + recipeSource.id + "' (" + row.GetAlias + ") has unknown factory '" + text + "'");
109 recipeSource.noListing = true;
110 }
111 }
112 if (!row.factory.IsEmpty() && row.factory[0] == "x")
113 {
114 recipeSource.noListing = true;
115 }
116 list.Add(recipeSource);
117 dict[recipeSource.id] = recipeSource;
118 _ = row.components;
119 try
120 {
121 Recipe.Create(recipeSource).BuildIngredientList();
122 }
123 catch (Exception ex)
124 {
125 list.Remove(recipeSource);
126 dict.Remove(recipeSource.id);
127 Debug.LogWarning($"Failed to create recipe '{row.RecipeID}' '{type}{suffix}'\n{ex}");
128 }
129 }
130
131 public static RecipeSource Get(string id)
132 {
133 return dict.TryGetValue(id);
134 }
135
136 public RecipeSource GetSource(string id)
137 {
138 return dict.TryGetValue(id);
139 }
140
141 public void Add(string id, bool showEffect = true)
142 {
143 if (id.IsEmpty())
144 {
145 return;
146 }
147 RecipeSource recipeSource = Get(id);
148 if (recipeSource == null)
149 {
150 return;
151 }
152 if (!knownRecipes.ContainsKey(id))
153 {
154 newRecipes.Add(id);
155 knownRecipes[id] = 0;
156 }
157 knownRecipes[id]++;
158 int num = knownRecipes[id];
159 if (showEffect)
160 {
161 EClass.pc.PlaySound("idea");
162 EClass.pc.ShowEmo(Emo.idea, 0.5f, skipSame: false);
163 }
164 EClass.pc.Say("learnRecipe" + ((num == 1) ? "New" : ""), dict[id].Name.ToTitleCase(), num.ToString() ?? "");
165 if (num == 1 && !recipeSource.isBridgePillar)
166 {
167 if (recipeSource.NeedFactory)
168 {
169 EClass.pc.Say("recipe_factory", recipeSource.NameFactory);
170 }
171 else
172 {
173 EClass.pc.Say("recipe_nofactory");
174 }
175 }
176 recipeSource = Get(id + "-p");
177 if (recipeSource != null && !knownRecipes.ContainsKey(recipeSource.id))
178 {
179 Add(recipeSource.id, showEffect: false);
180 }
181 recipeSource = Get(id.Replace("-p", ""));
182 if (recipeSource != null && !knownRecipes.ContainsKey(recipeSource.id))
183 {
184 Add(recipeSource.id, showEffect: false);
185 }
186 if (recipeSource.row.category == "floor")
187 {
188 recipeSource = Get(id + "-b");
189 if (recipeSource != null && !knownRecipes.ContainsKey(recipeSource.id))
190 {
191 Add(recipeSource.id, showEffect: false);
192 }
193 recipeSource = Get(id.Replace("-b", ""));
194 if (recipeSource != null && !knownRecipes.ContainsKey(recipeSource.id))
195 {
196 Add(recipeSource.id, showEffect: false);
197 }
198 }
199 }
200
201 public bool IsKnown(string id)
202 {
204 {
205 return EClass.player.recipes.knownRecipes.ContainsKey(id);
206 }
207 return true;
208 }
209
210 public List<RecipeSource> ListSources(Thing factory, List<RecipeSource> newRecipes = null)
211 {
212 BuildList();
213 List<RecipeSource> list = new List<RecipeSource>();
215 {
216 if (item.isBridgePillar || (factory == null && item.idFactory != "self") || (factory != null && !factory.trait.Contains(item)) || item.isChara || !item.ShouldList())
217 {
218 continue;
219 }
220 if (!EClass.debug.godCraft && !EClass.player.recipes.knownRecipes.ContainsKey(item.id))
221 {
222 bool flag = false;
223 if (item.row.recipeKey != null && item.row.recipeKey.Length != 0 && item.row.recipeKey[0].Length > 0 && item.row.recipeKey[0][0] == '*')
224 {
225 flag = true;
226 }
227 string id = item.id;
228 if (!(id == "waystone"))
229 {
230 if (id == "container_shipping" && EClass.game.quests.GetPhase<QuestShippingChest>() >= 0)
231 {
232 flag = true;
233 }
234 }
235 else if (EClass.game.quests.GetPhase<QuestExploration>() >= 6)
236 {
237 flag = true;
238 }
239 if (!flag)
240 {
241 continue;
242 }
243 if (newRecipes != null)
244 {
246 if (EClass.player.recipes.newRecipes.Contains(item.id))
247 {
248 newRecipes.Add(item);
250 }
251 }
252 }
253 list.Add(item);
254 }
255 return list;
256 }
257
258 public void OnSleep(bool ehe = false)
259 {
260 int slept = EClass.player.stats.slept;
261 Rand.SetSeed(EClass.game.seed + slept);
262 if ((slept <= 3 || EClass.rnd(ehe ? 777 : 3) != 0) && (EClass.rnd(EClass.pc.Evalue(1642) + 1) > 0 || ((slept <= 15 || EClass.rnd(3) != 0) && (slept <= 30 || EClass.rnd(3) != 0) && (slept <= 60 || EClass.rnd(3) != 0))))
263 {
264 Msg.Say("learnRecipeSleep");
265 Rand.SetSeed();
266 Add(GetRandomRecipe((ehe ? 5 : 0) + EClass.rnd(EClass.rnd(EClass.rnd(10)))));
267 }
268 }
269
270 public void ComeUpWithRandomRecipe(string idCat = null, int lvBonus = 0)
271 {
272 string randomRecipe = GetRandomRecipe(lvBonus, idCat, onlyUnlearned: true);
273 if (randomRecipe != null)
274 {
275 Msg.Say("learnRecipeIdea");
276 Add(randomRecipe);
277 }
278 }
279
280 public LearnState GetRecipeLearnState(string idRecipe)
281 {
282 if (idRecipe.IsEmpty())
283 {
284 return LearnState.Unavailable;
285 }
286 RecipeSource recipeSource = Get(idRecipe);
287 if (recipeSource == null || (!recipeSource.NeedFactory && !recipeSource.IsQuickCraft))
288 {
289 return LearnState.Unavailable;
290 }
291 if (EClass.player.recipes.knownRecipes.ContainsKey(idRecipe))
292 {
293 return LearnState.AlreadyLearned;
294 }
295 int id = recipeSource.GetReqSkill().id;
296 if (EClass.pc.Evalue(id) + 5 < recipeSource.row.LV)
297 {
298 return LearnState.InsufficientSkill;
299 }
300 return LearnState.Learnable;
301 }
302
303 public void ComeUpWithRecipe(string idRecipe, int chanceForRandomRecipe = 0)
304 {
305 if (idRecipe.IsEmpty())
306 {
307 return;
308 }
309 RecipeSource recipeSource = Get(idRecipe);
310 int num = EClass.pc.Evalue(1661);
311 if (EClass.rnd((num >= 2) ? 5 : ((num == 1) ? 8 : 10)) != 0 && !EClass.debug.enable)
312 {
313 return;
314 }
315 if (recipeSource == null || EClass.player.recipes.knownRecipes.ContainsKey(idRecipe) || (!recipeSource.NeedFactory && !recipeSource.IsQuickCraft))
316 {
317 if (recipeSource != null && chanceForRandomRecipe > 0 && EClass.rnd(EClass.debug.enable ? 1 : chanceForRandomRecipe) == 0)
318 {
319 ComeUpWithRandomRecipe(recipeSource.row.Category.id);
320 }
321 return;
322 }
323 int id = recipeSource.GetReqSkill().id;
324 if (EClass.pc.Evalue(id) + 5 < recipeSource.row.LV)
325 {
326 Msg.Say("recipeReqLv", EClass.sources.elements.map[id].GetName());
327 return;
328 }
329 Msg.Say("learnRecipeIdea");
330 Add(idRecipe);
331 }
332
333 public static RecipeSource GetUnlearnedRecipe(int lvBonus, string cat, bool onlyUnlearned)
334 {
335 BuildList();
336 List<RecipeSource> list = new List<RecipeSource>();
338 {
339 if (!item.alwaysKnown && !item.noRandomRecipe && (item.NeedFactory || item.IsQuickCraft) && (EClass.game.IsSurvival || !item.row.tag.Contains("oneblock_only")) && (cat == null || item.row.Category.IsChildOf(cat)) && (!onlyUnlearned || !EClass.player.recipes.knownRecipes.ContainsKey(item.id)) && EClass.pc.Evalue(item.GetReqSkill().id) + 5 + lvBonus >= item.row.LV && !item.row.ContainsTag("hiddenRecipe"))
340 {
341 list.Add(item);
342 }
343 }
344 return list.RandomItemWeighted((RecipeSource r) => (r.row.chance == 0) ? 100 : r.row.chance);
345 }
346
347 public static RecipeSource GetLearnedRecipe(string cat = null)
348 {
349 List<RecipeSource> list = new List<RecipeSource>();
350 foreach (string key in EClass.player.recipes.knownRecipes.Keys)
351 {
352 RecipeSource recipeSource = Get(key);
353 if (recipeSource != null && recipeSource.row is SourceThing.Row && !recipeSource.noRandomRecipe && (recipeSource.NeedFactory || recipeSource.IsQuickCraft) && (cat == null || recipeSource.row.Category.IsChildOf(cat)))
354 {
355 list.Add(recipeSource);
356 }
357 }
358 return list.RandomItemWeighted((RecipeSource r) => (r.row.chance == 0) ? 100 : r.row.chance);
359 }
360
361 public static string GetRandomRecipe(int lvBonus, string cat = null, bool onlyUnlearned = false)
362 {
363 RecipeSource recipeSource = null;
364 recipeSource = GetUnlearnedRecipe(lvBonus, cat, onlyUnlearned);
365 if (recipeSource == null)
366 {
367 recipeSource = GetLearnedRecipe(cat);
368 }
369 return recipeSource?.id;
370 }
371
372 public void OnVersionUpdate()
373 {
374 EClass.pc.things.Foreach(delegate(Thing t)
375 {
376 knownIngredients.Add(t.id);
377 });
378 knownIngredients.Add("flower");
379 }
380}
Emo
Definition: Emo.cs:2
bool isChara
Definition: CardRow.cs:55
bool isOrigin
Definition: CardRow.cs:52
string id
Definition: Card.cs:36
SoundSource PlaySound(string id, float v=1f, bool spatial=true)
Definition: Card.cs:6645
void ShowEmo(Emo _emo=Emo.none, float duration=0f, bool skipSame=true)
Definition: Card.cs:6613
ThingContainer things
Definition: Card.cs:39
int Evalue(int ele)
Definition: Card.cs:2704
void Say(string lang, string ref1=null, string ref2=null)
Definition: Card.cs:7324
bool godCraft
Definition: CoreDebug.cs:197
bool enable
Definition: CoreDebug.cs:302
bool allRecipe
Definition: CoreDebug.cs:249
Definition: EClass.cs:6
static Game game
Definition: EClass.cs:9
static int rnd(long a)
Definition: EClass.cs:59
static SourceManager sources
Definition: EClass.cs:43
static Player player
Definition: EClass.cs:13
static Chara pc
Definition: EClass.cs:15
static CoreDebug debug
Definition: EClass.cs:49
int id
Definition: ELEMENT.cs:259
bool IsSurvival
Definition: Game.cs:276
QuestManager quests
Definition: Game.cs:183
int seed
Definition: Game.cs:201
Definition: Msg.cs:5
static string Say(string idLang, string ref1, string ref2=null, string ref3=null, string ref4=null)
Definition: Msg.cs:58
int slept
Definition: Player.cs:99
RecipeManager recipes
Definition: Player.cs:1167
Stats stats
Definition: Player.cs:1086
int GetPhase(string id)
Definition: Rand.cs:4
static void SetSeed(int a=-1)
Definition: Rand.cs:44
static bool rebuild
Dictionary< string, List< int > > lastIngredients
static void BuildList()
HashSet< string > craftedRecipes
static List< RecipeSource > list
void ComeUpWithRecipe(string idRecipe, int chanceForRandomRecipe=0)
bool IsKnown(string id)
static RecipeSource Get(string id)
static RecipeSource GetLearnedRecipe(string cat=null)
static Dictionary< string, RecipeSource > dict
HashSet< string > knownIngredients
static void Create(RenderRow row, string type, string suffix="")
static RecipeSource GetUnlearnedRecipe(int lvBonus, string cat, bool onlyUnlearned)
LearnState GetRecipeLearnState(string idRecipe)
void OnSleep(bool ehe=false)
Dictionary< string, int > knownRecipes
List< RecipeSource > ListSources(Thing factory, List< RecipeSource > newRecipes=null)
void Add(string id, bool showEffect=true)
void OnVersionUpdate()
HashSet< string > hoveredRecipes
static string GetRandomRecipe(int lvBonus, string cat=null, bool onlyUnlearned=false)
void ComeUpWithRandomRecipe(string idCat=null, int lvBonus=0)
RecipeSource GetSource(string id)
HashSet< string > newRecipes
Element GetReqSkill()
RenderRow row
Definition: RecipeSource.cs:5
bool IsQuickCraft
Definition: RecipeSource.cs:51
string NameFactory
Definition: RecipeSource.cs:75
bool isBridgePillar
Definition: RecipeSource.cs:15
bool noRandomRecipe
Definition: RecipeSource.cs:25
bool NeedFactory
Definition: RecipeSource.cs:63
Definition: Recipe.cs:7
static Dictionary< string, Recipe > recipeCache
Definition: Recipe.cs:175
static Recipe Create(RecipeSource _source, int idMat=-1, Thing ing=null)
Definition: Recipe.cs:325
virtual void BuildIngredientList()
Definition: Recipe.cs:539
string[] factory
Definition: RenderRow.cs:54
SourceCategory.Row Category
Definition: RenderRow.cs:123
string category
Definition: RenderRow.cs:46
string[] components
Definition: RenderRow.cs:52
int chance
Definition: RenderRow.cs:24
int LV
Definition: RenderRow.cs:22
List< CardRow > rows
Definition: SourceCard.cs:7
Dictionary< string, CardRow > map
Definition: SourceCard.cs:9
virtual string GetAlias
Definition: SourceData.cs:655
SourceCard cards
SourceObj objs
SourceDeco decos
SourceCellEffect cellEffects
SourceBlock blocks
SourceElement elements
SourceFloor floors
void Foreach(Action< Thing > action, bool onlyAccessible=true)
Definition: Thing.cs:8
static TileTypePillar Pillar
Definition: TileType.cs:41