craftingmanager.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //! Client only - manage set up crafting on client
  2. class CraftingManager
  3. {
  4. const int CM_MODE_NONE = 0;
  5. const int CM_MODE_WORLD = 1;
  6. const int CM_MODE_INVENTORY = 2;
  7. PlayerBase m_player;
  8. PluginRecipesManager m_recipesManager;
  9. ActionVariantManager m_actionVariantManager;
  10. int m_recipeID;
  11. int m_contextualRecipeID;
  12. int m_recipeCount;
  13. int m_craftingMode;
  14. ItemBase m_item1;
  15. ItemBase m_item2;
  16. ref array<int> m_recipes;
  17. void CraftingManager(PlayerBase player, PluginRecipesManager recipesManager)
  18. {
  19. m_recipesManager = recipesManager;
  20. m_player = player;
  21. m_craftingMode = CM_MODE_NONE;
  22. m_actionVariantManager = ActionManagerClient.GetVariantManager( ActionWorldCraft );
  23. m_actionVariantManager.GetOnUpdateInvoker().Clear();
  24. m_actionVariantManager.GetOnUpdateInvoker().Insert(OnUpdate);
  25. m_recipes = new array<int>;
  26. }
  27. void ~CraftingManager()
  28. {
  29. if (m_actionVariantManager)
  30. {
  31. m_actionVariantManager.GetOnUpdateInvoker().Remove(OnUpdate);
  32. }
  33. }
  34. void SetRecipeID(int recipeID)
  35. {
  36. m_recipeID = recipeID;
  37. }
  38. int GetRecipeID()
  39. {
  40. return m_recipeID;
  41. }
  42. bool IsInventoryCraft()
  43. {
  44. return m_craftingMode == CM_MODE_INVENTORY;
  45. }
  46. bool IsWorldCraft()
  47. {
  48. return m_craftingMode == CM_MODE_WORLD;
  49. }
  50. int GetRecipesCount()
  51. {
  52. return m_recipeCount;
  53. }
  54. // deprecated
  55. void SetNextRecipe()
  56. {
  57. }
  58. void OnUpdate( Object item, Object target, int component_index )
  59. {
  60. ItemBase item1 = ItemBase.Cast( item );
  61. ItemBase item2 = ItemBase.Cast( target );
  62. if ( m_player.GetActionManager().GetRunningAction() )
  63. return;
  64. ItemBase item_in_hands = m_player.GetItemInHands();
  65. if (!item1 || !item2)
  66. {
  67. m_recipeCount = 0;
  68. m_craftingMode = CM_MODE_NONE;
  69. m_actionVariantManager.Clear();
  70. return;
  71. }
  72. else
  73. {
  74. if ( item1 != item_in_hands && item2 != item_in_hands )
  75. {
  76. InventoryLocation il1 = new InventoryLocation;
  77. InventoryLocation il2 = new InventoryLocation;
  78. item1.GetInventory().GetCurrentInventoryLocation(il1);
  79. item2.GetInventory().GetCurrentInventoryLocation(il2);
  80. Error("Crafting manager - both of items are out of hands - item1: " + il1.DumpToString() + " item2: " + il2.DumpToString() + " / item in hands: - " + item_in_hands);
  81. }
  82. }
  83. int recipeCount = 0;
  84. if (m_craftingMode == CM_MODE_INVENTORY)
  85. {
  86. recipeCount = m_recipesManager.GetValidRecipes(m_item1, m_item2, m_recipes, m_player);
  87. m_recipeCount = recipeCount;
  88. m_recipeID = m_recipes.Get(m_contextualRecipeID);
  89. return;
  90. }
  91. recipeCount = m_recipesManager.GetValidRecipes(item1, item2, m_recipes, m_player);
  92. if (recipeCount == 0)
  93. {
  94. m_recipeCount = 0;
  95. m_craftingMode = CM_MODE_NONE;
  96. m_actionVariantManager.Clear();
  97. }
  98. else
  99. {
  100. if ( m_craftingMode == CM_MODE_NONE || m_recipeCount != recipeCount || m_item1 != item1 || m_item2 != item2 )
  101. {
  102. m_craftingMode = CM_MODE_WORLD;
  103. m_recipeCount = recipeCount;
  104. m_contextualRecipeID = 0;
  105. m_item1 = item1;
  106. m_item2 = item2;
  107. m_actionVariantManager.SetActionVariantCount(m_recipeCount);
  108. }
  109. m_recipeID = m_recipes.Get(m_contextualRecipeID);
  110. }
  111. }
  112. bool SetInventoryCraft(int recipeID, ItemBase item1, ItemBase item2)
  113. {
  114. int recipeCount = m_recipesManager.GetValidRecipes(item1,item2,m_recipes, m_player);
  115. for ( int i = 0; i < recipeCount; i++ )
  116. {
  117. if (recipeID == -1 || m_recipes.Get(i) == recipeID)
  118. {
  119. if ( m_recipesManager.GetIsInstaRecipe(m_recipes.Get(i)) || m_recipesManager.IsEnableDebugCrafting() )
  120. {
  121. Param craftParam = new Param3<int, ItemBase, ItemBase>(m_recipes.Get(i), item1, item2);
  122. m_player.RPCSingleParam(ERPCs.RPC_CRAFTING_INVENTORY_INSTANT, craftParam, true, m_player.GetIdentity());
  123. return true;
  124. }
  125. else
  126. {
  127. m_craftingMode = CM_MODE_INVENTORY;
  128. m_recipeCount = recipeCount;
  129. m_contextualRecipeID = i;
  130. m_item1 = item1;
  131. m_item2 = item2;
  132. m_recipeID = m_recipes.Get(i);
  133. ActionManagerClient am = ActionManagerClient.Cast(m_player.GetActionManager());
  134. if ( m_player.GetItemInHands() == item1) am.SetInventoryAction( am.GetAction(ActionWorldCraft), item2, item1);
  135. else am.SetInventoryAction( am.GetAction(ActionWorldCraft), item1, item2);
  136. return true;
  137. }
  138. }
  139. }
  140. return false;
  141. }
  142. void ResetInventoryCraft()
  143. {
  144. m_recipeCount = 0;
  145. m_craftingMode = CM_MODE_NONE;
  146. }
  147. bool IsEnableDebugCrafting()
  148. {
  149. return true;
  150. }
  151. int GetRecipeID( int action_index )
  152. {
  153. return m_recipes[action_index];
  154. }
  155. }