RecipeInfo.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. using Server.MirEnvir;
  2. using Server.MirObjects;
  3. namespace Server.MirDatabase
  4. {
  5. public class RecipeInfo
  6. {
  7. protected static Envir Envir
  8. {
  9. get { return Envir.Main; }
  10. }
  11. protected static MessageQueue MessageQueue
  12. {
  13. get { return MessageQueue.Instance; }
  14. }
  15. public UserItem Item;
  16. public List<UserItem> Ingredients;
  17. public List<UserItem> Tools;
  18. public List<int> RequiredFlag = new List<int>();
  19. public ushort? RequiredLevel = null;
  20. public List<int> RequiredQuest = new List<int>();
  21. public List<MirClass> RequiredClass = new List<MirClass>();
  22. public MirGender? RequiredGender = null;
  23. public byte Chance = 100;
  24. public uint Gold = 0;
  25. public RecipeInfo(string name)
  26. {
  27. ItemInfo itemInfo = Envir.GetItemInfo(name);
  28. if (itemInfo == null)
  29. {
  30. MessageQueue.Enqueue(GameLanguage.ServerTextMap.GetLocalization((ServerTextKeys.CouldNotFindItem), name));
  31. return;
  32. }
  33. Item = Envir.CreateShopItem(itemInfo, ++Envir.NextRecipeID);
  34. LoadIngredients(name);
  35. }
  36. private void LoadIngredients(string recipe)
  37. {
  38. List<string> lines = File.ReadAllLines(Path.Combine(Settings.RecipePath, recipe + ".txt")).ToList();
  39. Tools = new List<UserItem>();
  40. Ingredients = new List<UserItem>();
  41. var mode = "ingredients";
  42. for (int i = 0; i < lines.Count; i++)
  43. {
  44. if (String.IsNullOrEmpty(lines[i])) continue;
  45. if (lines[i].StartsWith("["))
  46. {
  47. mode = lines[i].Substring(1, lines[i].Length - 2).ToLower();
  48. continue;
  49. }
  50. switch (mode)
  51. {
  52. case "recipe":
  53. {
  54. var data = lines[i].Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  55. if (data.Length < 2) continue;
  56. switch (data[0].ToLower())
  57. {
  58. case "amount":
  59. Item.Count = ushort.Parse(data[1]);
  60. break;
  61. case "chance":
  62. Chance = byte.Parse(data[1]);
  63. if (Chance > 100)
  64. {
  65. Chance = 100;
  66. }
  67. break;
  68. case "gold":
  69. Gold = uint.Parse(data[1]);
  70. break;
  71. default:
  72. break;
  73. }
  74. }
  75. break;
  76. case "tools":
  77. {
  78. var data = lines[i].Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  79. ItemInfo info = Envir.GetItemInfo(data[0]);
  80. if (info == null)
  81. {
  82. MessageQueue.Enqueue(GameLanguage.ServerTextMap.GetLocalization((ServerTextKeys.CouldNotFindToolRecipe), lines[i], recipe));
  83. continue;
  84. }
  85. UserItem tool = Envir.CreateShopItem(info, 0);
  86. Tools.Add(tool);
  87. }
  88. break;
  89. case "ingredients":
  90. {
  91. var data = lines[i].Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  92. ItemInfo info = Envir.GetItemInfo(data[0]);
  93. if (info == null)
  94. {
  95. MessageQueue.Enqueue(GameLanguage.ServerTextMap.GetLocalization((ServerTextKeys.CouldNotFindIngredientRecipe), lines[i], recipe));
  96. continue;
  97. }
  98. UserItem ingredient = Envir.CreateShopItem(info, 0);
  99. ushort count = 1;
  100. if (data.Length >= 2)
  101. ushort.TryParse(data[1], out count);
  102. if (data.Length >= 3)
  103. ushort.TryParse(data[2], out ingredient.CurrentDura);
  104. ingredient.Count = count > info.StackSize ? info.StackSize : count;
  105. Ingredients.Add(ingredient);
  106. }
  107. break;
  108. case "criteria":
  109. {
  110. var data = lines[i].Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  111. if (data.Length < 2) continue;
  112. try
  113. {
  114. switch (data[0].ToLower())
  115. {
  116. case "level":
  117. RequiredLevel = ushort.Parse(data[1]);
  118. break;
  119. case "class":
  120. if (Enum.TryParse<MirClass>(data[1], true, out MirClass cls))
  121. {
  122. RequiredClass.Add(cls);
  123. }
  124. else
  125. {
  126. RequiredClass.Add((MirClass)byte.Parse(data[1]));
  127. }
  128. break;
  129. case "gender":
  130. if (Enum.TryParse<MirGender>(data[1], true, out MirGender gender))
  131. {
  132. RequiredGender = gender;
  133. }
  134. else
  135. {
  136. RequiredGender = (MirGender)byte.Parse(data[1]);
  137. }
  138. break;
  139. case "flag":
  140. RequiredFlag.Add(int.Parse(data[1]));
  141. break;
  142. case "quest":
  143. RequiredQuest.Add(int.Parse(data[1]));
  144. break;
  145. }
  146. }
  147. catch
  148. {
  149. MessageQueue.Enqueue(GameLanguage.ServerTextMap.GetLocalization((ServerTextKeys.CouldNotParseOption), data[0], data[1]));
  150. continue;
  151. }
  152. }
  153. break;
  154. }
  155. }
  156. }
  157. public bool MatchItem(int index)
  158. {
  159. return Item != null && Item.ItemIndex == index;
  160. }
  161. public bool CanCraft(PlayerObject player)
  162. {
  163. if (RequiredLevel != null && RequiredLevel.Value > player.Level)
  164. return false;
  165. if (RequiredGender != null && RequiredGender.Value != player.Gender)
  166. return false;
  167. if (RequiredClass.Count > 0 && !RequiredClass.Contains(player.Class))
  168. return false;
  169. if (RequiredFlag.Count > 0)
  170. {
  171. foreach (var flag in RequiredFlag)
  172. {
  173. if(!player.Info.Flags[flag])
  174. return false;
  175. }
  176. }
  177. if (RequiredQuest.Count > 0)
  178. {
  179. foreach (var quest in RequiredQuest)
  180. {
  181. if (!player.Info.CompletedQuests.Contains(quest))
  182. return false;
  183. }
  184. }
  185. return true;
  186. }
  187. public ClientRecipeInfo CreateClientRecipeInfo()
  188. {
  189. ClientRecipeInfo clientInfo = new ClientRecipeInfo
  190. {
  191. Gold = Gold,
  192. Chance = Chance,
  193. Item = Item.Clone(),
  194. Tools = Tools.Select(x => x).ToList(),
  195. Ingredients = Ingredients.Select(x => x).ToList()
  196. };
  197. return clientInfo;
  198. }
  199. }
  200. }