actiondetachfromtarget.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. class ActionDetachFromTarget: ActionInteractBase
  2. {
  3. override void CreateConditionComponents()
  4. {
  5. m_ConditionItem = new CCINone;
  6. m_ConditionTarget = new CCTCursor;
  7. }
  8. void ActionDetachFromTarget()
  9. {
  10. m_Text = "#take_to_hands";
  11. }
  12. override typename GetInputType()
  13. {
  14. return ContinuousInteractActionInput;
  15. }
  16. override ActionData CreateActionData()
  17. {
  18. DetachActionData action_data = new DetachActionData;
  19. return action_data;
  20. }
  21. int FindSlotIdToDetach(PlayerBase player, ActionTarget target, ItemBase item)
  22. {
  23. EntityAI target_entity = EntityAI.Cast(target.GetObject());
  24. if ( player && target_entity )
  25. {
  26. array<string> selections = new array<string>();
  27. target_entity.GetActionComponentNameList( target.GetComponentIndex(),selections );
  28. if ( target_entity && target_entity.GetInventory() && target_entity.GetInventory().AttachmentCount() > 0 )
  29. {
  30. for(int i = 0; i < selections.Count(); i++ )
  31. {
  32. int target_slot_id = InventorySlots.GetSlotIdFromString( selections[i] );
  33. EntityAI att = target_entity.GetInventory().FindAttachment(target_slot_id);
  34. if ( att && player.GetInventory().CanAddEntityIntoHands(att) )
  35. {
  36. if ( att.CanDetachAttachment( target_entity ) && target_entity.CanReleaseAttachment( att ) )
  37. return target_slot_id;
  38. }
  39. }
  40. }
  41. }
  42. return InventorySlots.INVALID;
  43. }
  44. override bool SetupAction(PlayerBase player, ActionTarget target, ItemBase item, out ActionData action_data, Param extra_data = NULL)
  45. {
  46. int attSlotId = InventorySlots.INVALID;
  47. if (!GetGame().IsDedicatedServer() )
  48. {
  49. attSlotId = FindSlotIdToDetach(player, target, item);
  50. }
  51. if ( super.SetupAction( player, target, item, action_data, extra_data))
  52. {
  53. if (!GetGame().IsDedicatedServer())
  54. {
  55. if(attSlotId != InventorySlots.INVALID)
  56. {
  57. DetachActionData action_data_a = DetachActionData.Cast(action_data);
  58. action_data_a.m_AttSlot = attSlotId;
  59. return true;
  60. }
  61. return false;
  62. }
  63. return true;
  64. }
  65. return false;
  66. }
  67. override bool ActionCondition( PlayerBase player, ActionTarget target, ItemBase item )
  68. {
  69. if ( GetGame().IsMultiplayer() && GetGame().IsServer() ) return true;
  70. return FindSlotIdToDetach(player, target, item) != InventorySlots.INVALID);
  71. }
  72. override Object GetDisplayInteractObject(PlayerBase player, ActionTarget target)
  73. {
  74. int target_slot_id = FindSlotIdToDetach(player, target, null);
  75. EntityAI target_entity = EntityAI.Cast( target.GetObject() );
  76. if(target_slot_id != InventorySlots.INVALID)
  77. {
  78. return target_entity.GetInventory().FindAttachment(target_slot_id);
  79. }
  80. return null;
  81. }
  82. override void OnExecute(ActionData action_data)
  83. {
  84. if (GetGame().IsDedicatedServer())
  85. {
  86. ClearActionJuncture(action_data);
  87. return;
  88. }
  89. Process(action_data);
  90. }
  91. void Process( ActionData action_data )
  92. {
  93. ClearInventoryReservationEx(action_data);
  94. DetachActionData action_data_a = DetachActionData.Cast(action_data);
  95. EntityAI target_entity = EntityAI.Cast( action_data_a.m_Target.GetObject() );
  96. ItemBase attachment = ItemBase.Cast(target_entity.GetInventory().FindAttachment(action_data_a.m_AttSlot));
  97. if(attachment)
  98. {
  99. float stackable = attachment.GetTargetQuantityMax();
  100. if( stackable == 0 || stackable >= attachment.GetQuantity() )
  101. {
  102. //take to hands
  103. action_data.m_Player.PredictiveTakeEntityToHands( attachment );
  104. }
  105. else if( stackable != 0 && stackable < attachment.GetQuantity() )
  106. {
  107. //split and take to hands
  108. attachment.SplitIntoStackMaxHandsClient( action_data.m_Player );
  109. }
  110. }
  111. }
  112. }
  113. class ActionDetachFromTarget_SpecificSlot: ActionDetachFromTarget
  114. {
  115. string m_slotToDetach;
  116. override int FindSlotIdToDetach(PlayerBase player, ActionTarget target, ItemBase item)
  117. {
  118. EntityAI target_entity = EntityAI.Cast(target.GetObject());
  119. if ( player && target_entity )
  120. {
  121. array<string> selections = new array<string>();
  122. target_entity.GetActionComponentNameList( target.GetComponentIndex(),selections );
  123. if ( target_entity && target_entity.GetInventory() && target_entity.GetInventory().AttachmentCount() > 0 )
  124. {
  125. for(int i = 0; i < selections.Count(); i++ )
  126. {
  127. if( selections[i] == m_slotToDetach )
  128. {
  129. int target_slot_id = InventorySlots.GetSlotIdFromString( selections[i] );
  130. EntityAI att = target_entity.GetInventory().FindAttachment(target_slot_id);
  131. if ( att && player.GetInventory().CanAddEntityIntoHands(att) )
  132. {
  133. if ( att.CanDetachAttachment( target_entity ) && target_entity.CanReleaseAttachment( att ) )
  134. return target_slot_id;
  135. }
  136. }
  137. }
  138. }
  139. }
  140. return InventorySlots.INVALID;
  141. }
  142. }
  143. class ActionDetachFromTarget_SpecificSlotsCategory: ActionDetachFromTarget
  144. {
  145. string m_slotsToDetach;
  146. override int FindSlotIdToDetach(PlayerBase player, ActionTarget target, ItemBase item)
  147. {
  148. EntityAI target_entity = EntityAI.Cast(target.GetObject());
  149. if ( player && target_entity )
  150. {
  151. array<string> selections = new array<string>();
  152. target_entity.GetActionComponentNameList( target.GetComponentIndex(),selections );
  153. if ( target_entity && target_entity.GetInventory() && target_entity.GetInventory().AttachmentCount() > 0 )
  154. {
  155. for(int i = 0; i < selections.Count(); i++ )
  156. {
  157. if (selections[i].Contains(m_slotsToDetach))
  158. {
  159. int target_slot_id = InventorySlots.GetSlotIdFromString( selections[i] );
  160. EntityAI att = target_entity.GetInventory().FindAttachment(target_slot_id);
  161. if ( att && player.GetInventory().CanAddEntityIntoHands(att) )
  162. {
  163. if ( att.CanDetachAttachment( target_entity ) && target_entity.CanReleaseAttachment( att ) )
  164. return target_slot_id;
  165. }
  166. }
  167. }
  168. }
  169. }
  170. return InventorySlots.INVALID;
  171. }
  172. }
  173. class ActionDetachFromTarget_SpecificSlot_WoodenLogs: ActionDetachFromTarget_SpecificSlot
  174. {
  175. void ActionDetachFromTarget_SpecificSlot_WoodenLogs ()
  176. {
  177. m_slotToDetach = "truck_01_woodenlogs";
  178. }
  179. }
  180. class ActionDetachFromTarget_SpecificSlot_WoodenPlanks: ActionDetachFromTarget_SpecificSlot
  181. {
  182. void ActionDetachFromTarget_SpecificSlot_WoodenPlanks ()
  183. {
  184. m_slotToDetach = "truck_01_woodenplanks";
  185. }
  186. }
  187. class ActionDetachFromTarget_SpecificSlot_MetalSheets: ActionDetachFromTarget_SpecificSlot
  188. {
  189. void ActionDetachFromTarget_SpecificSlot_MetalSheets ()
  190. {
  191. m_slotToDetach = "truck_01_metalsheets";
  192. }
  193. }
  194. class ActionDetachFromTarget_SpecificSlotsCategory_Barrel: ActionDetachFromTarget_SpecificSlotsCategory
  195. {
  196. void ActionDetachFromTarget_SpecificSlotsCategory_Barrel ()
  197. {
  198. m_slotsToDetach = "truck_01_barrel";
  199. }
  200. }
  201. class ActionDetachFromTarget_SpecificSlotsCategory_WoodenCrate: ActionDetachFromTarget_SpecificSlotsCategory
  202. {
  203. void ActionDetachFromTarget_SpecificSlotsCategory_WoodenCrate ()
  204. {
  205. m_slotsToDetach = "truck_01_woodencrate";
  206. }
  207. }