constructionactiondata.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. class ConstructionActionData
  2. {
  3. Object m_Target;
  4. //base building
  5. ref array<ConstructionPart> m_BuildParts;
  6. ref array<ConstructionPart> m_BuildPartsNoTool;
  7. int m_PartIndex; //used on client only, action synchronizes it to server to avoid mismatch
  8. string m_MainPartName;
  9. string m_MainPartNameNoTool;
  10. ref ConstructionPart m_TargetPart;
  11. //combination lock
  12. CombinationLock m_CombinationLock;
  13. //attaching
  14. int m_SlotId;
  15. PlayerBase m_ActionInitiator;
  16. //detaching
  17. ref array<EntityAI> m_Attachments;
  18. protected ActionVariantManager m_ActionVariantManager;
  19. protected ActionVariantManager m_ActionNoToolVariantManager;
  20. int m_AttachmentsIndex;
  21. void ConstructionActionData()
  22. {
  23. m_BuildParts = new ref array<ConstructionPart>;
  24. m_BuildPartsNoTool = new ref array<ConstructionPart>;
  25. m_PartIndex = 0;
  26. m_Attachments = new ref array<EntityAI>;
  27. m_AttachmentsIndex = 0;
  28. if ( GetGame().IsClient() || !GetGame().IsMultiplayer() )
  29. {
  30. m_ActionVariantManager = ActionManagerClient.GetVariantManager( ActionBuildPart );
  31. m_ActionVariantManager.GetOnUpdateInvoker().Clear();
  32. m_ActionVariantManager.GetOnUpdateInvoker().Insert(OnUpdateActions);
  33. m_ActionNoToolVariantManager = ActionManagerClient.GetVariantManager( ActionBuildShelter );
  34. m_ActionNoToolVariantManager.GetOnUpdateInvoker().Clear();
  35. m_ActionNoToolVariantManager.GetOnUpdateInvoker().Insert(OnUpdateActionsNoTool);
  36. }
  37. }
  38. void ~ConstructionActionData()
  39. {
  40. if (GetGame() && (GetGame().IsClient() || !GetGame().IsMultiplayer()))
  41. {
  42. if (m_ActionVariantManager)
  43. {
  44. m_ActionVariantManager.GetOnUpdateInvoker().Remove(OnUpdateActions);
  45. }
  46. if (m_ActionNoToolVariantManager)
  47. {
  48. m_ActionNoToolVariantManager.GetOnUpdateInvoker().Remove(OnUpdateActionsNoTool);
  49. }
  50. }
  51. }
  52. //************************************************/
  53. // Base building
  54. //************************************************/
  55. string GetMainPartName()
  56. {
  57. return m_MainPartName;
  58. }
  59. string GetMainPartNameNoTool()
  60. {
  61. return m_MainPartNameNoTool;
  62. }
  63. void SetTarget( Object target )
  64. {
  65. m_Target = target;
  66. }
  67. Object GetTarget()
  68. {
  69. return m_Target;
  70. }
  71. void SetTargetPart( ConstructionPart target_part )
  72. {
  73. m_TargetPart = target_part;
  74. }
  75. ConstructionPart GetTargetPart()
  76. {
  77. return m_TargetPart;
  78. }
  79. void SetSlotId( int slot_id )
  80. {
  81. m_SlotId = slot_id;
  82. }
  83. int GetSlotId()
  84. {
  85. return m_SlotId;
  86. }
  87. void SetActionInitiator( PlayerBase action_initiator )
  88. {
  89. m_ActionInitiator = action_initiator;
  90. }
  91. PlayerBase GetActionInitiator()
  92. {
  93. return m_ActionInitiator;
  94. }
  95. // deprecated
  96. void SetNextIndex()
  97. {
  98. }
  99. // deprecated
  100. void RefreshPartsToBuild( string main_part_name, ItemBase tool, bool use_tool = true )
  101. {
  102. }
  103. void OnUpdateActions( Object item, Object target, int component_index )
  104. {
  105. ItemBase tool = ItemBase.Cast( item );
  106. if ( tool )
  107. {
  108. BaseBuildingBase base_building_object = BaseBuildingBase.Cast( target );
  109. if ( base_building_object )
  110. {
  111. string main_part_name = target.GetActionComponentName( component_index );
  112. base_building_object.GetConstruction().GetConstructionPartsToBuild( main_part_name, m_BuildParts, tool, m_MainPartName, true );
  113. m_ActionVariantManager.SetActionVariantCount(m_BuildParts.Count());
  114. }
  115. else
  116. {
  117. m_BuildParts.Clear();
  118. m_ActionVariantManager.Clear();
  119. }
  120. }
  121. else
  122. {
  123. m_BuildParts.Clear();
  124. m_ActionVariantManager.Clear();
  125. }
  126. //not needed
  127. //m_Target = target;
  128. }
  129. void OnUpdateActionsNoTool( Object item, Object target, int component_index )
  130. {
  131. BaseBuildingBase base_building_object = BaseBuildingBase.Cast( target );
  132. if ( base_building_object )
  133. {
  134. string main_part_name = target.GetActionComponentName( component_index );
  135. base_building_object.GetConstruction().GetConstructionPartsToBuild( main_part_name, m_BuildPartsNoTool, null, m_MainPartNameNoTool, false );
  136. m_ActionNoToolVariantManager.SetActionVariantCount(m_BuildPartsNoTool.Count());
  137. }
  138. else
  139. {
  140. m_BuildPartsNoTool.Clear();
  141. m_ActionNoToolVariantManager.Clear();
  142. }
  143. }
  144. int GetConstructionPartsCount()
  145. {
  146. return m_BuildParts.Count();
  147. }
  148. // deprecated
  149. ConstructionPart GetCurrentBuildPart()
  150. {
  151. return null;
  152. }
  153. ConstructionPart GetBuildPartAtIndex(int idx)
  154. {
  155. if( m_BuildParts.Count() > idx )
  156. {
  157. return m_BuildParts.Get( idx );
  158. }
  159. return null;
  160. }
  161. ConstructionPart GetBuildPartNoToolAtIndex(int idx)
  162. {
  163. if( m_BuildPartsNoTool.Count() > idx )
  164. {
  165. return m_BuildPartsNoTool.Get( idx );
  166. }
  167. return null;
  168. }
  169. //************************************************/
  170. // Combination lock
  171. //************************************************/
  172. CombinationLock GetCombinationLock()
  173. {
  174. return m_CombinationLock;
  175. }
  176. void SetCombinationLock( CombinationLock combination_lock )
  177. {
  178. m_CombinationLock = CombinationLock.Cast( combination_lock );
  179. }
  180. string GetDialNumberText()
  181. {
  182. string dial_text;
  183. if ( m_CombinationLock )
  184. {
  185. string combination_text = m_CombinationLock.GetCombination().ToString();
  186. //insert zeros to dials with 0 value
  187. int length_diff = m_CombinationLock.GetLockDigits() - combination_text.Length();
  188. for ( int i = 0; i < length_diff; ++i )
  189. {
  190. combination_text = "0" + combination_text;
  191. }
  192. //assemble the whole combination with selected part
  193. for ( int j = 0; j < m_CombinationLock.GetLockDigits(); ++j )
  194. {
  195. if ( j == m_CombinationLock.GetDialIndex() )
  196. {
  197. dial_text += string.Format( "[%1]", combination_text.Get( j ) );
  198. }
  199. else
  200. {
  201. dial_text += string.Format( " %1 ", combination_text.Get( j ) );
  202. }
  203. }
  204. }
  205. return dial_text;
  206. }
  207. //************************************************/
  208. // Attach/Detach actions
  209. //************************************************/
  210. int GetAttachmentSlotFromSelection( PlayerBase player, EntityAI target, ItemBase item_to_attach, string selection )
  211. {
  212. string cfg_path = "cfgVehicles" + " " + target.GetType() + " "+ "GUIInventoryAttachmentsProps";
  213. if ( GetGame().ConfigIsExisting( cfg_path ) )
  214. {
  215. int child_count = GetGame().ConfigGetChildrenCount( cfg_path );
  216. for ( int i = 0; i < child_count; i++ )
  217. {
  218. string child_name;
  219. GetGame().ConfigGetChildName( cfg_path, i, child_name );
  220. string child_selection;
  221. GetGame().ConfigGetText( cfg_path + " " + child_name + " " + "selection", child_selection );
  222. if ( selection == child_selection )
  223. {
  224. ref array<string> attachment_slots = new array<string>;
  225. GetGame().ConfigGetTextArray( cfg_path + " " + child_name + " " + "attachmentSlots", attachment_slots );
  226. for ( int j = 0; j < attachment_slots.Count(); ++j )
  227. {
  228. int target_slot_id = InventorySlots.GetSlotIdFromString( attachment_slots.Get( j ) );
  229. int item_slot_count = item_to_attach.GetInventory().GetSlotIdCount();
  230. for ( int k = 0; k < item_slot_count; ++k )
  231. {
  232. int item_slot_id = item_to_attach.GetInventory().GetSlotId( k );
  233. ItemBase attachment_item = ItemBase.Cast( target.GetInventory().FindAttachment( item_slot_id ) );
  234. if ( target_slot_id == item_slot_id )
  235. {
  236. if ( target.GetInventory().CanAddAttachmentEx( item_to_attach, item_slot_id ) && target.CanReceiveAttachment( item_to_attach, item_slot_id ) || attachment_item && attachment_item.CanBeCombined( item_to_attach ) )
  237. {
  238. if(target.CanDisplayAttachmentSlot(target_slot_id))
  239. return item_slot_id;
  240. else
  241. return -1;
  242. }
  243. }
  244. }
  245. }
  246. }
  247. }
  248. }
  249. return -1;
  250. }
  251. void GetAttachmentsFromSelection( EntityAI target, string selection, out array<EntityAI> attachments )
  252. {
  253. attachments.Clear(); //clear output
  254. string cfg_path = "cfgVehicles" + " " + target.GetType() + " "+ "GUIInventoryAttachmentsProps";
  255. if ( GetGame().ConfigIsExisting( cfg_path ) )
  256. {
  257. int child_count = GetGame().ConfigGetChildrenCount( cfg_path );
  258. for ( int i = 0; i < child_count; i++ )
  259. {
  260. string child_name;
  261. GetGame().ConfigGetChildName( cfg_path, i, child_name );
  262. string child_selection;
  263. GetGame().ConfigGetText( cfg_path + " " + child_name + " " + "selection", child_selection );
  264. if ( selection == child_selection )
  265. {
  266. ref array<string> attachment_slots = new array<string>;
  267. GetGame().ConfigGetTextArray( cfg_path + " " + child_name + " " + "attachmentSlots", attachment_slots );
  268. for ( int j = 0; j < attachment_slots.Count(); ++j )
  269. {
  270. int target_slot_id = InventorySlots.GetSlotIdFromString( attachment_slots.Get( j ) );
  271. //is attached and can be detached
  272. EntityAI attachment = target.GetInventory().FindAttachment( target_slot_id );
  273. if ( attachment && target.GetInventory().CanRemoveAttachmentEx( attachment, target_slot_id ) && !target.GetInventory().GetSlotLock( target_slot_id ) )
  274. {
  275. attachments.Insert( attachment );
  276. }
  277. }
  278. }
  279. }
  280. }
  281. }
  282. void CombineItems( ItemBase target, ItemBase item )
  283. {
  284. if ( target.ConfigGetBool( "canBeSplit" ) && item && !target.IsFullQuantity() )
  285. {
  286. int quantity_used = target.ComputeQuantityUsed( item, true );
  287. if( quantity_used != 0 )
  288. {
  289. target.AddQuantity( quantity_used );
  290. item.AddQuantity( -quantity_used );
  291. }
  292. }
  293. }
  294. void RefreshAttachmentsToDetach( EntityAI target, string main_part_name )
  295. {
  296. GetAttachmentsFromSelection( target, main_part_name, m_Attachments );
  297. }
  298. void SetNextAttachmentIndex()
  299. {
  300. if ( GetAttachmentsToDetachCount() > 1 )
  301. {
  302. if ( m_AttachmentsIndex <= GetAttachmentsToDetachCount() - 2 )
  303. {
  304. m_AttachmentsIndex++;
  305. }
  306. else if ( m_AttachmentsIndex >= GetAttachmentsToDetachCount() > - 1 )
  307. {
  308. m_AttachmentsIndex = 0;
  309. }
  310. }
  311. else
  312. {
  313. m_AttachmentsIndex = 0;
  314. }
  315. }
  316. int GetAttachmentsToDetachCount()
  317. {
  318. return m_Attachments.Count();
  319. }
  320. EntityAI GetActualAttachmentToDetach()
  321. {
  322. if ( GetAttachmentsToDetachCount() > 0 )
  323. {
  324. m_AttachmentsIndex = Math.Clamp( m_AttachmentsIndex, 0, GetAttachmentsToDetachCount() - 1 );
  325. if ( m_Attachments && GetAttachmentsToDetachCount() > ( m_AttachmentsIndex ) )
  326. {
  327. return m_Attachments.Get( m_AttachmentsIndex );
  328. }
  329. }
  330. return NULL;
  331. }
  332. //************************************************/
  333. // Common
  334. //************************************************/
  335. void ResetActionIndexes()
  336. {
  337. m_PartIndex = 0;
  338. m_AttachmentsIndex = 0;
  339. }
  340. }