craftingmanager.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 SetRecipeID(int recipeID)
  28. {
  29. m_recipeID = recipeID;
  30. }
  31. int GetRecipeID()
  32. {
  33. return m_recipeID;
  34. }
  35. bool IsInventoryCraft()
  36. {
  37. return m_craftingMode == CM_MODE_INVENTORY;
  38. }
  39. bool IsWorldCraft()
  40. {
  41. return m_craftingMode == CM_MODE_WORLD;
  42. }
  43. int GetRecipesCount()
  44. {
  45. return m_recipeCount;
  46. }
  47. // deprecated
  48. void SetNextRecipe()
  49. {
  50. }
  51. void OnUpdate( Object item, Object target, int component_index )
  52. {
  53. ItemBase item1 = ItemBase.Cast( item );
  54. ItemBase item2 = ItemBase.Cast( target );
  55. if ( m_player.GetActionManager().GetRunningAction() )
  56. return;
  57. ItemBase item_in_hands = m_player.GetItemInHands();
  58. if (!item1 || !item2)
  59. {
  60. m_recipeCount = 0;
  61. m_craftingMode = CM_MODE_NONE;
  62. m_actionVariantManager.Clear();
  63. return;
  64. }
  65. else
  66. {
  67. if ( item1 != item_in_hands && item2 != item_in_hands )
  68. {
  69. InventoryLocation il1 = new InventoryLocation;
  70. InventoryLocation il2 = new InventoryLocation;
  71. item1.GetInventory().GetCurrentInventoryLocation(il1);
  72. item2.GetInventory().GetCurrentInventoryLocation(il2);
  73. Error("Crafting manager - both of items are out of hands - item1: " + il1.DumpToString() + " item2: " + il2.DumpToString() + " / item in hands: - " + item_in_hands);
  74. }
  75. }
  76. int recipeCount = 0;
  77. if (m_craftingMode == CM_MODE_INVENTORY)
  78. {
  79. recipeCount = m_recipesManager.GetValidRecipes(m_item1, m_item2, m_recipes, m_player);
  80. m_recipeCount = recipeCount;
  81. m_recipeID = m_recipes.Get(m_contextualRecipeID);
  82. return;
  83. }
  84. recipeCount = m_recipesManager.GetValidRecipes(item1, item2, m_recipes, m_player);
  85. if (recipeCount == 0)
  86. {
  87. m_recipeCount = 0;
  88. m_craftingMode = CM_MODE_NONE;
  89. m_actionVariantManager.Clear();
  90. }
  91. else
  92. {
  93. if ( m_craftingMode == CM_MODE_NONE || m_recipeCount != recipeCount || m_item1 != item1 || m_item2 != item2 )
  94. {
  95. m_craftingMode = CM_MODE_WORLD;
  96. m_recipeCount = recipeCount;
  97. m_contextualRecipeID = 0;
  98. m_item1 = item1;
  99. m_item2 = item2;
  100. m_actionVariantManager.SetActionVariantCount(m_recipeCount);
  101. }
  102. m_recipeID = m_recipes.Get(m_contextualRecipeID);
  103. }
  104. }
  105. bool SetInventoryCraft(int recipeID, ItemBase item1, ItemBase item2)
  106. {
  107. int recipeCount = m_recipesManager.GetValidRecipes(item1,item2,m_recipes, m_player);
  108. for ( int i = 0; i < recipeCount; i++ )
  109. {
  110. if (recipeID == -1 || m_recipes.Get(i) == recipeID)
  111. {
  112. if ( m_recipesManager.GetIsInstaRecipe(m_recipes.Get(i)) || m_recipesManager.IsEnableDebugCrafting() )
  113. {
  114. Param craftParam = new Param3<int, ItemBase, ItemBase>(m_recipes.Get(i), item1, item2);
  115. m_player.RPCSingleParam(ERPCs.RPC_CRAFTING_INVENTORY_INSTANT, craftParam, true, m_player.GetIdentity());
  116. return true;
  117. }
  118. else
  119. {
  120. m_craftingMode = CM_MODE_INVENTORY;
  121. m_recipeCount = recipeCount;
  122. m_contextualRecipeID = i;
  123. m_item1 = item1;
  124. m_item2 = item2;
  125. m_recipeID = m_recipes.Get(i);
  126. ActionManagerClient am = ActionManagerClient.Cast(m_player.GetActionManager());
  127. if ( m_player.GetItemInHands() == item1) am.SetInventoryAction( am.GetAction(ActionWorldCraft), item2, item1);
  128. else am.SetInventoryAction( am.GetAction(ActionWorldCraft), item1, item2);
  129. return true;
  130. }
  131. }
  132. }
  133. return false;
  134. }
  135. void ResetInventoryCraft()
  136. {
  137. m_recipeCount = 0;
  138. m_craftingMode = CM_MODE_NONE;
  139. }
  140. bool IsEnableDebugCrafting()
  141. {
  142. return true;
  143. }
  144. int GetRecipeID( int action_index )
  145. {
  146. return m_recipes[action_index];
  147. }
  148. }