Elin Decompiled Documentation EA 23.326 Nightly
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 foreach (CardRow row in EClass.sources.cards.rows)
50 {
51 if (!row.isOrigin)
52 {
53 Create(row, "", row.isChara ? "-c" : "");
54 }
55 }
56 foreach (SourceBlock.Row row2 in EClass.sources.blocks.rows)
57 {
58 Create(row2, "Block");
59 if (row2.tileType == TileType.Pillar)
60 {
61 Create(row2, "BridgePillar", "-p");
62 }
63 }
64 foreach (SourceFloor.Row row3 in EClass.sources.floors.rows)
65 {
66 if (!row3.tag.Contains("noFloor"))
67 {
68 Create(row3, "Floor");
69 }
70 }
71 foreach (SourceFloor.Row row4 in EClass.sources.floors.rows)
72 {
73 if (!row4.tag.Contains("noBridge") && !row4.factory.IsEmpty() && row4.factory[0] != "x")
74 {
75 Create(row4, "Bridge", "-b");
76 }
77 }
78 foreach (SourceDeco.Row row5 in EClass.sources.decos.rows)
79 {
80 Create(row5, "Deco");
81 }
82 foreach (SourceObj.Row row6 in EClass.sources.objs.rows)
83 {
84 Create(row6, "Obj");
85 }
86 foreach (SourceCellEffect.Row row7 in EClass.sources.cellEffects.rows)
87 {
88 Create(row7, "Liquid");
89 }
90 rebuild = false;
91 }
92
93 public static void Create(RenderRow row, string type, string suffix = "")
94 {
95 RecipeSource recipeSource = new RecipeSource();
96 recipeSource.id = row.RecipeID + suffix;
97 recipeSource.isBridge = type == "Bridge";
98 recipeSource.isBridgePillar = type == "BridgePillar";
99 recipeSource.type = type;
100 recipeSource.row = row;
101 recipeSource.isChara = row is SourceChara.Row;
102 if (!row.factory.IsEmpty() && row.factory[0] == "x")
103 {
104 recipeSource.noListing = true;
105 }
106 list.Add(recipeSource);
107 dict[recipeSource.id] = recipeSource;
108 _ = row.components;
109 try
110 {
111 Recipe.Create(recipeSource).BuildIngredientList();
112 }
113 catch (Exception ex)
114 {
115 list.Remove(recipeSource);
116 dict.Remove(recipeSource.id);
117 Debug.LogWarning($"Failed to create recipe '{row.RecipeID}' '{type}{suffix}'\n{ex}");
118 }
119 }
120
121 public static RecipeSource Get(string id)
122 {
123 return dict.TryGetValue(id);
124 }
125
126 public RecipeSource GetSource(string id)
127 {
128 return dict.TryGetValue(id);
129 }
130
131 public void Add(string id, bool showEffect = true)
132 {
133 if (id.IsEmpty())
134 {
135 return;
136 }
137 RecipeSource recipeSource = Get(id);
138 if (recipeSource == null)
139 {
140 return;
141 }
142 if (!knownRecipes.ContainsKey(id))
143 {
144 newRecipes.Add(id);
145 knownRecipes[id] = 0;
146 }
147 knownRecipes[id]++;
148 int num = knownRecipes[id];
149 if (showEffect)
150 {
151 EClass.pc.PlaySound("idea");
152 EClass.pc.ShowEmo(Emo.idea, 0.5f, skipSame: false);
153 }
154 EClass.pc.Say("learnRecipe" + ((num == 1) ? "New" : ""), dict[id].Name.ToTitleCase(), num.ToString() ?? "");
155 if (num == 1 && !recipeSource.isBridgePillar)
156 {
157 if (recipeSource.NeedFactory)
158 {
159 EClass.pc.Say("recipe_factory", recipeSource.NameFactory);
160 }
161 else
162 {
163 EClass.pc.Say("recipe_nofactory");
164 }
165 }
166 recipeSource = Get(id + "-p");
167 if (recipeSource != null && !knownRecipes.ContainsKey(recipeSource.id))
168 {
169 Add(recipeSource.id, showEffect: false);
170 }
171 recipeSource = Get(id.Replace("-p", ""));
172 if (recipeSource != null && !knownRecipes.ContainsKey(recipeSource.id))
173 {
174 Add(recipeSource.id, showEffect: false);
175 }
176 if (recipeSource.row.category == "floor")
177 {
178 recipeSource = Get(id + "-b");
179 if (recipeSource != null && !knownRecipes.ContainsKey(recipeSource.id))
180 {
181 Add(recipeSource.id, showEffect: false);
182 }
183 recipeSource = Get(id.Replace("-b", ""));
184 if (recipeSource != null && !knownRecipes.ContainsKey(recipeSource.id))
185 {
186 Add(recipeSource.id, showEffect: false);
187 }
188 }
189 }
190
191 public bool IsKnown(string id)
192 {
194 {
195 return EClass.player.recipes.knownRecipes.ContainsKey(id);
196 }
197 return true;
198 }
199
200 public List<RecipeSource> ListSources(Thing factory, List<RecipeSource> newRecipes = null)
201 {
202 BuildList();
203 List<RecipeSource> list = new List<RecipeSource>();
205 {
206 if (item.isBridgePillar || (factory == null && item.idFactory != "self") || (factory != null && !factory.trait.Contains(item)) || item.isChara || item.noListing)
207 {
208 continue;
209 }
210 if (!EClass.debug.godCraft && !EClass.player.recipes.knownRecipes.ContainsKey(item.id))
211 {
212 bool flag = false;
213 if (item.row.recipeKey != null && item.row.recipeKey.Length != 0 && item.row.recipeKey[0][0] == '*')
214 {
215 flag = true;
216 }
217 string id = item.id;
218 if (!(id == "waystone"))
219 {
220 if (id == "container_shipping" && EClass.game.quests.GetPhase<QuestShippingChest>() >= 0)
221 {
222 flag = true;
223 }
224 }
225 else if (EClass.game.quests.GetPhase<QuestExploration>() >= 6)
226 {
227 flag = true;
228 }
229 if (!flag)
230 {
231 continue;
232 }
233 if (newRecipes != null)
234 {
236 if (EClass.player.recipes.newRecipes.Contains(item.id))
237 {
238 newRecipes.Add(item);
240 }
241 }
242 }
243 list.Add(item);
244 }
245 return list;
246 }
247
248 public void OnSleep(bool ehe = false)
249 {
250 int slept = EClass.player.stats.slept;
251 Rand.SetSeed(EClass.game.seed + slept);
252 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))))
253 {
254 Msg.Say("learnRecipeSleep");
255 Rand.SetSeed();
256 Add(GetRandomRecipe((ehe ? 5 : 0) + EClass.rnd(EClass.rnd(EClass.rnd(10)))));
257 }
258 }
259
260 public void ComeUpWithRandomRecipe(string idCat = null, int lvBonus = 0)
261 {
262 string randomRecipe = GetRandomRecipe(lvBonus, idCat, onlyUnlearned: true);
263 if (randomRecipe != null)
264 {
265 Msg.Say("learnRecipeIdea");
266 Add(randomRecipe);
267 }
268 }
269
270 public LearnState GetRecipeLearnState(string idRecipe)
271 {
272 if (idRecipe.IsEmpty())
273 {
274 return LearnState.Unavailable;
275 }
276 RecipeSource recipeSource = Get(idRecipe);
277 if (recipeSource == null || (!recipeSource.NeedFactory && !recipeSource.IsQuickCraft))
278 {
279 return LearnState.Unavailable;
280 }
281 if (EClass.player.recipes.knownRecipes.ContainsKey(idRecipe))
282 {
283 return LearnState.AlreadyLearned;
284 }
285 int id = recipeSource.GetReqSkill().id;
286 if (EClass.pc.Evalue(id) + 5 < recipeSource.row.LV)
287 {
288 return LearnState.InsufficientSkill;
289 }
290 return LearnState.Learnable;
291 }
292
293 public void ComeUpWithRecipe(string idRecipe, int chanceForRandomRecipe = 0)
294 {
295 if (idRecipe.IsEmpty())
296 {
297 return;
298 }
299 RecipeSource recipeSource = Get(idRecipe);
300 int num = EClass.pc.Evalue(1661);
301 if (EClass.rnd((num >= 2) ? 5 : ((num == 1) ? 8 : 10)) != 0 && !EClass.debug.enable)
302 {
303 return;
304 }
305 if (recipeSource == null || EClass.player.recipes.knownRecipes.ContainsKey(idRecipe) || (!recipeSource.NeedFactory && !recipeSource.IsQuickCraft))
306 {
307 if (recipeSource != null && chanceForRandomRecipe > 0 && EClass.rnd(EClass.debug.enable ? 1 : chanceForRandomRecipe) == 0)
308 {
309 ComeUpWithRandomRecipe(recipeSource.row.Category.id);
310 }
311 return;
312 }
313 int id = recipeSource.GetReqSkill().id;
314 if (EClass.pc.Evalue(id) + 5 < recipeSource.row.LV)
315 {
316 Msg.Say("recipeReqLv", EClass.sources.elements.map[id].GetName());
317 return;
318 }
319 Msg.Say("learnRecipeIdea");
320 Add(idRecipe);
321 }
322
323 public static RecipeSource GetUnlearnedRecipe(int lvBonus, string cat, bool onlyUnlearned)
324 {
325 BuildList();
326 List<RecipeSource> list = new List<RecipeSource>();
328 {
329 if (!item.alwaysKnown && !item.noRandomRecipe && (item.NeedFactory || item.IsQuickCraft) && (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"))
330 {
331 list.Add(item);
332 }
333 }
334 return list.RandomItemWeighted((RecipeSource r) => (r.row.chance == 0) ? 100 : r.row.chance);
335 }
336
337 public static RecipeSource GetLearnedRecipe(string cat = null)
338 {
339 List<RecipeSource> list = new List<RecipeSource>();
340 foreach (string key in EClass.player.recipes.knownRecipes.Keys)
341 {
342 RecipeSource recipeSource = Get(key);
343 if (recipeSource != null && recipeSource.row is SourceThing.Row && !recipeSource.noRandomRecipe && (recipeSource.NeedFactory || recipeSource.IsQuickCraft) && (cat == null || recipeSource.row.Category.IsChildOf(cat)))
344 {
345 list.Add(recipeSource);
346 }
347 }
348 return list.RandomItemWeighted((RecipeSource r) => (r.row.chance == 0) ? 100 : r.row.chance);
349 }
350
351 public static string GetRandomRecipe(int lvBonus, string cat = null, bool onlyUnlearned = false)
352 {
353 RecipeSource recipeSource = null;
354 recipeSource = GetUnlearnedRecipe(lvBonus, cat, onlyUnlearned);
355 if (recipeSource == null)
356 {
357 recipeSource = GetLearnedRecipe(cat);
358 }
359 return recipeSource?.id;
360 }
361
362 public void OnVersionUpdate()
363 {
364 EClass.pc.things.Foreach(delegate(Thing t)
365 {
366 knownIngredients.Add(t.id);
367 });
368 knownIngredients.Add("flower");
369 }
370}
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:6602
void ShowEmo(Emo _emo=Emo.none, float duration=0f, bool skipSame=true)
Definition: Card.cs:6570
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:7280
bool godCraft
Definition: CoreDebug.cs:196
bool enable
Definition: CoreDebug.cs:301
bool allRecipe
Definition: CoreDebug.cs:248
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
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:102
RecipeManager recipes
Definition: Player.cs:1164
Stats stats
Definition: Player.cs:1083
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 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:119
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
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