attachmentsoutofreach.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. class AttachmentsOutOfReach
  2. {
  3. protected static ref map<string, ref map<int, vector>> m_AttData;
  4. static bool IsAttachmentReachable(EntityAI e, string att_slot_name = "", int slot_id = -1, float range = 1.5)
  5. {
  6. if( !e.IgnoreOutOfReachCondition() )
  7. {
  8. PlayerBase player = PlayerBase.Cast( GetGame().GetPlayer() );
  9. if( player.IsInVehicle() )
  10. {
  11. return false;
  12. }
  13. else
  14. {
  15. vector pos_att;
  16. if ( slot_id != -1 )
  17. {
  18. att_slot_name = InventorySlots.GetSlotName(slot_id);
  19. }
  20. if( att_slot_name != "" )
  21. {
  22. if( e.MemoryPointExists(att_slot_name) )
  23. {
  24. vector mem_point = e.GetMemoryPointPos(att_slot_name);
  25. pos_att = e.ModelToWorld(mem_point);
  26. }
  27. else
  28. {
  29. pos_att = e.ModelToWorld(GetAttachmentPosition(e, InventorySlots.GetSlotIdFromString( att_slot_name ) ));
  30. }
  31. }
  32. vector pos_player = player.GetPosition();
  33. float height_diff = Math.AbsFloat( pos_player[1] - pos_att[1] );
  34. if( height_diff < range )
  35. {
  36. pos_player[1] = 0;
  37. pos_att[1] = 0;
  38. if ( vector.Distance(pos_player, pos_att) <= range )
  39. {
  40. return true;
  41. }
  42. return false;
  43. }
  44. else
  45. {
  46. return false;
  47. }
  48. }
  49. }
  50. else
  51. {
  52. return true;
  53. }
  54. }
  55. static vector GetAttachmentPosition(EntityAI e, int slot_id)
  56. {
  57. if ( m_AttData == NULL )
  58. {
  59. m_AttData = new map<string, ref map<int, vector>>();
  60. }
  61. string type_name = e.GetType();
  62. if ( !m_AttData.Contains( type_name ) )
  63. {
  64. map<int, vector> att = CreateAttachmentPosition( e );
  65. m_AttData.Insert( type_name, att );
  66. }
  67. return m_AttData.Get( type_name ).Get( slot_id );
  68. }
  69. static protected map<int, vector> CreateAttachmentPosition( EntityAI entity )
  70. {
  71. map<int, vector> ret_val = new map<int, vector>();
  72. string type_name = entity.GetType();
  73. TStringArray cfg_attachments = new TStringArray;
  74. string cfg_path;
  75. if ( GetGame().ConfigIsExisting(CFG_VEHICLESPATH+" "+type_name) )
  76. {
  77. cfg_path = CFG_VEHICLESPATH+" "+type_name+" attachments";
  78. }
  79. else if ( GetGame().ConfigIsExisting(CFG_WEAPONSPATH+" "+type_name) )
  80. {
  81. cfg_path = CFG_WEAPONSPATH+" "+type_name+" attachments";
  82. }
  83. else if ( GetGame().ConfigIsExisting(CFG_MAGAZINESPATH+" "+type_name) )
  84. {
  85. cfg_path = CFG_MAGAZINESPATH+" "+type_name+" attachments";
  86. }
  87. GetGame().ConfigGetTextArray(cfg_path, cfg_attachments);
  88. int child_count = GetGame().ConfigGetChildrenCount("CfgNonAIVehicles");
  89. for ( int x = 0; x < child_count; ++x )
  90. {
  91. string child_name;
  92. GetGame().ConfigGetChildName("CfgNonAIVehicles",x , child_name);
  93. string inventory_slot_name;
  94. GetGame().ConfigGetText("CfgNonAIVehicles "+ child_name +" inventorySlot", inventory_slot_name);
  95. if ( cfg_attachments.Find( inventory_slot_name ) > 0 )
  96. {
  97. string model_path;
  98. GetGame().ConfigGetText("CfgNonAIVehicles "+ child_name +" model", model_path);
  99. if ( model_path.Length() > 5 )
  100. {
  101. LOD lod = entity.GetLODByName(LOD.NAME_VIEW);
  102. if ( lod )
  103. {
  104. model_path = model_path.Substring(0, model_path.Length() - 4);
  105. model_path.ToLower();
  106. array<Selection> selections = new array<Selection>();
  107. lod.GetSelections(selections);
  108. for ( int i = 0; i < selections.Count(); ++i )
  109. {
  110. string selection = selections[i].GetName();
  111. selection.ToLower();
  112. if ( selection.Contains(model_path) )
  113. {
  114. ret_val.Set(InventorySlots.GetSlotIdFromString( inventory_slot_name ), selections[i].GetVertexPosition(lod, 0));
  115. }
  116. }
  117. }
  118. }
  119. }
  120. }
  121. return ret_val;
  122. }
  123. }