constructionactiondata.c 9.9 KB

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