actiontargets.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. //! objects in vicinity - extended with secondary object which is parent of that Object
  2. class VicinityObjects
  3. {
  4. private ref map<Object, Object> m_VicinityObjects;
  5. void VicinityObjects()
  6. {
  7. m_VicinityObjects = new map<Object, Object>;
  8. }
  9. //! stores VicinityObject to Hashmap - for storing of parent/child relationship
  10. void StoreVicinityObject(Object object, Object parent = null)
  11. {
  12. //! completely remove items that are being placed or are holograms
  13. ItemBase ib = ItemBase.Cast(object);
  14. if (ib && (ib.IsBeingPlaced() || ib.IsHologram()))
  15. return;
  16. //! ignores plain objects
  17. /*if(object && object.IsPlainObject())
  18. {
  19. Print("ERROR: VicinityObjects | StoreVicinityObject | IsPlainObject check fail");
  20. return;
  21. }*/
  22. if ( !m_VicinityObjects.Contains(object) )
  23. {
  24. //! init of VicinityObjects - object, parent(if exists)
  25. m_VicinityObjects.Set(object, parent);
  26. }
  27. }
  28. //! transform simple array of Objects to VicinityObjects hashmap
  29. void TransformToVicinityObjects(array<Object> objects)
  30. {
  31. for (int i = 0; i < objects.Count(); i++)
  32. {
  33. if (objects[i].GetType() != "" && objects[i].CanBeActionTarget())
  34. {
  35. StoreVicinityObject(objects[i]);
  36. //Print("storing, 2nd pass: " + objects[i]);
  37. }
  38. }
  39. }
  40. void ClearVicinityObjects()
  41. {
  42. m_VicinityObjects.Clear();
  43. }
  44. //! return simple array of Objects in Vicinity
  45. array< Object > GetVicinityObjects()
  46. {
  47. ref array<Object> vicinityObjects = new array<Object>;
  48. for (int i = 0; i < m_VicinityObjects.Count(); i++)
  49. {
  50. //! filters out non-takeable items (won't be shown in vicinity)
  51. ItemBase ib = ItemBase.Cast(GetObject(i));
  52. if (ib && !ib.IsTakeable())
  53. continue;
  54. vicinityObjects.Insert(GetObject(i));
  55. }
  56. return vicinityObjects;
  57. }
  58. //! return simple array of Objects in Vicinity
  59. array< Object > GetRawVicinityObjects()
  60. {
  61. ref array<Object> vicinityObjects = new array<Object>;
  62. for (int i = 0; i < m_VicinityObjects.Count(); i++)
  63. {
  64. vicinityObjects.Insert(GetObject(i));
  65. }
  66. return vicinityObjects;
  67. }
  68. //! returns VicinityObjects Key
  69. Object GetObject(int i)
  70. {
  71. return m_VicinityObjects.GetKey(i);
  72. }
  73. //! returns VicinityObjects Element
  74. Object GetParent(int i)
  75. {
  76. return m_VicinityObjects.GetElement(i);
  77. }
  78. int Count()
  79. {
  80. return m_VicinityObjects.Count();
  81. }
  82. void Remove(Object object)
  83. {
  84. m_VicinityObjects.Remove(object);
  85. }
  86. void Remove(array<Object> objects)
  87. {
  88. for (int i = 0; i < objects.Count(); i++)
  89. {
  90. m_VicinityObjects.Remove(objects[i]);
  91. }
  92. }
  93. }
  94. class ActionTarget
  95. {
  96. void ActionTarget(Object object, Object parent, int componentIndex, vector cursorHitPos, float utility)
  97. {
  98. m_Object = object;
  99. m_Parent = parent;
  100. m_ComponentIndex = componentIndex;
  101. m_CursorHitPos = cursorHitPos;
  102. m_Utility = utility;
  103. }
  104. Object GetObject()
  105. { return m_Object; }
  106. Object GetParent()
  107. { return m_Parent; }
  108. bool IsProxy()
  109. {
  110. if (m_Parent)
  111. return true;
  112. return false;
  113. }
  114. int GetComponentIndex()
  115. { return m_ComponentIndex; }
  116. float GetUtility()
  117. { return m_Utility; }
  118. vector GetCursorHitPos()
  119. { return m_CursorHitPos; }
  120. void SetCursorHitPos(vector cursor_position)
  121. {
  122. m_CursorHitPos = cursor_position;
  123. }
  124. void DbgPrintTargetDump()
  125. {
  126. Print(DumpToString());
  127. }
  128. string DumpToString()
  129. {
  130. string res = "ActionTarget dump = {";
  131. res = res + "m_Object: " + Object.GetDebugName(m_Object);
  132. res = res + "; m_Parent: " + Object.GetDebugName(m_Parent);
  133. res = res + "; m_ComponentIndex: " + m_ComponentIndex.ToString();
  134. res = res + "; m_CursorHitPos: " + m_CursorHitPos.ToString();
  135. res = res + "; m_Utility: " + m_Utility.ToString();
  136. res = res + "}";
  137. return res;
  138. }
  139. private Object m_Object; // object itself
  140. private Object m_Parent; // null or parent of m_Object
  141. private int m_ComponentIndex; // p3d Component ID or -1
  142. private vector m_CursorHitPos;
  143. private float m_Utility;
  144. };
  145. class ActionTargets
  146. {
  147. void ActionTargets(PlayerBase player)
  148. {
  149. m_Player = player;
  150. m_VicinityObjects = new VicinityObjects;
  151. m_Targets = new array<ref ActionTarget>;
  152. m_Debug = false;
  153. }
  154. static array<Object> GetVicinityObjects()
  155. {
  156. return m_VicinityObjects.GetVicinityObjects();
  157. }
  158. void Clear()
  159. {
  160. m_Targets.Clear();
  161. }
  162. void Update()
  163. {
  164. int i;
  165. //! clear state
  166. m_VicinityObjects.ClearVicinityObjects();
  167. Clear();
  168. Object cursorTarget = null;
  169. EntityAI cursorTargetEntity = null;
  170. array<Object> vicinityObjects = new array<Object>;
  171. //! camera & ray properties
  172. int hitComponentIndex;
  173. vector playerPos = m_Player.GetPosition();
  174. vector headingDirection = MiscGameplayFunctions.GetHeadingVector(m_Player);
  175. m_RayStart = GetGame().GetCurrentCameraPosition();
  176. m_RayEnd = m_RayStart + GetGame().GetCurrentCameraDirection() * c_RayDistance;
  177. RaycastRVParams rayInput = new RaycastRVParams(m_RayStart, m_RayEnd, m_Player);
  178. rayInput.flags = CollisionFlags.ALLOBJECTS;
  179. //rayInput.sorted = true;
  180. array<ref RaycastRVResult> results = new array<ref RaycastRVResult>;
  181. if ( DayZPhysics.RaycastRVProxy(rayInput, results) )
  182. {
  183. if ( results.Count() > 0 )
  184. {
  185. array<float> distance_helper = new array<float>;
  186. array<float> distance_helper_unsorted = new array<float>;
  187. float distance;
  188. for (i = 0; i < results.Count(); i++)
  189. {
  190. distance = vector.DistanceSq(results[i].pos, m_RayStart);
  191. distance_helper.Insert(distance);
  192. //Print("#" + i + " " + results.Get(i).obj);
  193. }
  194. //Print("--------------");
  195. distance_helper_unsorted.Copy(distance_helper);
  196. distance_helper.Sort();
  197. RaycastRVResult res;
  198. for ( i = 0; i < results.Count(); i++)
  199. {
  200. res = results.Get(distance_helper_unsorted.Find(distance_helper[i])); //closest object
  201. cursorTarget = res.obj;
  202. Class.CastTo(cursorTargetEntity,cursorTarget);
  203. if (cursorTarget && !cursorTarget.CanBeActionTarget())
  204. continue;
  205. //! if the cursor target is a proxy
  206. if ( res.hierLevel > 0 )
  207. {
  208. //! ignores attachments on player
  209. if ( !res.parent.IsMan() )
  210. {
  211. m_VicinityObjects.StoreVicinityObject(res.obj, res.parent);
  212. //Print("storing, 1st pass (hier > 0): " + res.obj);
  213. }
  214. else
  215. continue;
  216. }
  217. else
  218. {
  219. m_VicinityObjects.StoreVicinityObject(res.obj, null);
  220. //Print("storing, 1st pass: " + res.obj);
  221. }
  222. m_HitPos = res.pos;
  223. hitComponentIndex = res.component;
  224. break;
  225. }
  226. }
  227. //else
  228. //Print("NO RESULTS FOUND!");
  229. }
  230. else
  231. {
  232. //Print("CAST UNSUCCESFUL");
  233. cursorTarget = null;
  234. m_HitPos = vector.Zero;
  235. hitComponentIndex = -1;
  236. }
  237. //Print(cursorTarget);
  238. //! spacial search
  239. DayZPlayerCamera camera = m_Player.GetCurrentCamera();
  240. if (camera && camera.GetCurrentPitch() <= -45) // Spatial search is a contributor to very heavy searching, limit it to when we are at least looking down
  241. DayZPlayerUtils.GetEntitiesInCone(playerPos, headingDirection, c_ConeAngle, c_MaxTargetDistance, c_ConeHeightMin, c_ConeHeightMax, vicinityObjects);
  242. //! removes player from the vicinity
  243. vicinityObjects.RemoveItem(m_Player);
  244. //! transformation of array of Objects to hashmap (VicinityObjects)
  245. //Print("m_VicinityObjects before" + m_VicinityObjects.Count());
  246. m_VicinityObjects.TransformToVicinityObjects(vicinityObjects);
  247. //Print("m_VicinityObjects after" + m_VicinityObjects.Count());
  248. //! removes Vicinity objects that are not directly visible from player position
  249. FilterObstructedObjectsEx(cursorTarget, vicinityObjects);
  250. //! select & sort targets based on utility function
  251. for ( i = 0; i < m_VicinityObjects.Count(); i++ )
  252. {
  253. Object object = m_VicinityObjects.GetObject(i);
  254. Object parent = m_VicinityObjects.GetParent(i);
  255. float utility = ComputeUtility(object, m_RayStart, m_RayEnd, cursorTarget, m_HitPos);
  256. if ( utility > 0 )
  257. {
  258. int targetComponent = -1;
  259. targetComponent = hitComponentIndex;
  260. ActionTarget at = new ActionTarget(object, parent, targetComponent, m_HitPos, utility);
  261. StoreTarget(at);
  262. }
  263. /*else
  264. Print("utility < 0; object: " + object + " | parent: " + parent);*/
  265. }
  266. //! action target for surface actions (lowest utility)
  267. if (m_HitPos == vector.Zero)
  268. {
  269. vector contact_pos, contact_dir, hitNormal;
  270. int contactComponent;
  271. float hitFraction;
  272. Object hitObject;
  273. m_RayEnd = m_RayStart + GetGame().GetCurrentCameraDirection() * c_RayDistance * 3;
  274. PhxInteractionLayers collisionLayerMask = PhxInteractionLayers.ROADWAY|PhxInteractionLayers.TERRAIN|PhxInteractionLayers.WATERLAYER;
  275. DayZPhysics.RayCastBullet(m_RayStart,m_RayEnd,collisionLayerMask,null,hitObject,contact_pos,hitNormal,hitFraction);
  276. m_HitPos = contact_pos;
  277. }
  278. m_Targets.Insert(new ActionTarget(null, null, -1, m_HitPos, 0));
  279. #ifdef DIAG_DEVELOPER
  280. if (DiagMenu.GetBool(DiagMenuIDs.MISC_ACTION_TARGETS_DEBUG))
  281. {
  282. ShowDebugActionTargets(true);
  283. DrawDebugActionTargets(true);
  284. DrawDebugCone(true);
  285. DrawDebugRay(true);
  286. DrawSelectionPos(DiagMenu.GetBool(DiagMenuIDs.MISC_ACTION_TARGETS_SELPOS_DEBUG));
  287. }
  288. else
  289. {
  290. ShowDebugActionTargets(false);
  291. DrawDebugActionTargets(false);
  292. DrawDebugCone(false);
  293. DrawDebugRay(false);
  294. DrawSelectionPos(false);
  295. }
  296. #endif
  297. //Print("--------------");
  298. }
  299. private bool IsObstructed(Object object)
  300. {
  301. IsObjectObstructedCache cache = new IsObjectObstructedCache(m_RayStart, 1);
  302. return IsObstructedEx(object, cache);
  303. }
  304. private bool IsObstructedEx(Object object, IsObjectObstructedCache cache)
  305. {
  306. return MiscGameplayFunctions.IsObjectObstructedEx(object, cache);
  307. }
  308. //! returns count of founded targets
  309. int GetTargetsCount()
  310. { return m_Targets.Count(); }
  311. //! returns action target at index
  312. ActionTarget GetTarget(int index)
  313. { return m_Targets.Get(index); }
  314. //! inserts action into sorted array based on utility
  315. private void StoreTarget(ActionTarget pActionTarget)
  316. {
  317. int index = FindIndexForStoring(pActionTarget.GetUtility());
  318. m_Targets.InsertAt(pActionTarget, index);
  319. //Print("StoreTarget; object: " + pActionTarget.GetObject() + " | parent: " + pActionTarget.GetParent() + " | idx: " + index);
  320. }
  321. //! binary search algorithm
  322. private int FindIndexForStoring(float value)
  323. {
  324. int left = 0;
  325. int right = m_Targets.Count() - 1;
  326. while ( left <= right )
  327. {
  328. int middle = (left + right) / 2;
  329. float middleValue = m_Targets.Get(middle).GetUtility();
  330. if ( middleValue == value )
  331. return middle;
  332. else if ( middleValue < value )
  333. right = middle - 1;
  334. else
  335. left = middle + 1;
  336. }
  337. return left;
  338. }
  339. //! computes utility of target
  340. private float ComputeUtility(Object pTarget, vector pRayStart, vector pRayEnd, Object cursorTarget, vector hitPos)
  341. {
  342. //! out of reach
  343. if (vector.DistanceSq(hitPos, m_Player.GetPosition()) > c_MaxTargetDistance * c_MaxTargetDistance)
  344. return -1;
  345. if (pTarget)
  346. {
  347. if ( pTarget == cursorTarget )
  348. {
  349. //! ground and static objects
  350. if ( pTarget.GetType() == string.Empty )
  351. return 0.01;
  352. if ( pTarget.IsBuilding() )
  353. return 0.25;
  354. if ( pTarget.IsTransport() )
  355. return 0.25;
  356. //!basebuilding objects
  357. if (pTarget.CanUseConstruction())
  358. return 0.85;
  359. if ( pTarget.IsWell() )
  360. return 0.9;
  361. vector playerPosXZ = m_Player.GetPosition();
  362. vector hitPosXZ = hitPos;
  363. playerPosXZ[1] = 0;
  364. hitPosXZ[1] = 0;
  365. if ( vector.DistanceSq(playerPosXZ, hitPosXZ) <= c_MaxTargetDistance * c_MaxTargetDistance )
  366. return c_UtilityMaxValue;
  367. }
  368. if ( PlayerBase.Cast(pTarget) && PlayerBase.Cast(pTarget).IsInVehicle() ) // utility in vehicle should be below base vehicle val
  369. return 0.20;
  370. float distSqr = DistSqrPoint2Line(pTarget.GetPosition(), pRayStart, pRayEnd);
  371. return (c_UtilityMaxDistFromRaySqr - distSqr) / c_UtilityMaxDistFromRaySqr;
  372. }
  373. return -1;
  374. }
  375. //! distance between point and line
  376. private float DistSqrPoint2Line(vector pPoint, vector pL1, vector pL2)
  377. {
  378. vector v = pL2 - pL1;
  379. vector w = pPoint - pL1;
  380. float c1 = vector.Dot(w,v);
  381. float c2 = vector.Dot(v,v);
  382. if ( c1 <= 0 || c2 == 0 )
  383. return vector.DistanceSq(pPoint, pL1);
  384. float b = c1 / c2;
  385. vector nearestPoint = pL1 + (v * b);
  386. return vector.DistanceSq(pPoint, nearestPoint);
  387. }
  388. private void FilterObstructedObjectsEx(Object cursor_target, array<Object> vicinityObjects)
  389. {
  390. #ifdef DIAG_DEVELOPER
  391. if (DiagMenu.GetBool(DiagMenuIDs.MISC_ACTION_TARGETS_DEBUG))
  392. CleanupDebugShapes(obstruction);
  393. #endif
  394. array<Object> obstructingObjects = new array<Object>;
  395. MiscGameplayFunctions.FilterObstructingObjects(vicinityObjects, obstructingObjects);
  396. if ( obstructingObjects.Count() > 0 )
  397. {
  398. PlayerBase player = PlayerBase.Cast(g_Game.GetPlayer());
  399. int numObstructed = 0;
  400. int mCount = m_VicinityObjects.Count();
  401. if (mCount > GROUPING_COUNT_THRESHOLD)
  402. {
  403. array<Object> filteredObjects = new array<Object>;
  404. MiscGameplayFunctions.FilterObstructedObjectsByGrouping(m_RayStart, c_MaxTargetDistance, c_DistanceDelta, m_VicinityObjects.GetRawVicinityObjects(), vicinityObjects, filteredObjects);
  405. m_VicinityObjects.ClearVicinityObjects();
  406. m_VicinityObjects.TransformToVicinityObjects(filteredObjects);
  407. }
  408. else
  409. {
  410. FilterObstructedObjects(cursor_target);
  411. }
  412. }
  413. }
  414. private void FilterObstructedObjects(Object cursor_target)
  415. {
  416. int numObstructed = 0;
  417. int mCount = m_VicinityObjects.Count();
  418. IsObjectObstructedCache cache = new IsObjectObstructedCache(m_RayStart, mCount);
  419. mCount--;
  420. //! check if targets are not obstructed (eg.: wall)
  421. for ( int i = mCount; i >= 0; --i )
  422. {
  423. Object object = m_VicinityObjects.GetObject(i);
  424. Object parent = m_VicinityObjects.GetParent(i);
  425. //! check for object obstruction(if the object is not a proxy - has no parent)
  426. if (object && !parent)
  427. {
  428. //! when the number of obstructed items is higher than OBSTRUCTED_COUNT_THRESHOLD
  429. //! remove do no run obstruction check and skip these items
  430. if (numObstructed > OBSTRUCTED_COUNT_THRESHOLD && object != cursor_target)
  431. {
  432. m_VicinityObjects.Remove(object);
  433. continue;
  434. }
  435. //! obstruction check
  436. if (object != cursor_target && IsObstructedEx(object, cache))
  437. {
  438. m_VicinityObjects.Remove(object);
  439. numObstructed++;
  440. }
  441. cache.ClearCache();
  442. }
  443. }
  444. }
  445. #ifdef DIAG_DEVELOPER
  446. ref array<Shape> shapes = new array<Shape>();
  447. ref array<Shape> dbgConeShapes = new array<Shape>();
  448. ref array<Shape> rayShapes = new array<Shape>();
  449. ref array<Shape> obstruction = new array<Shape>();
  450. ref array<Shape> dbgPosShapes = new array<Shape>();
  451. void ShowDebugActionTargets(bool enabled)
  452. {
  453. int windowPosX = 0;
  454. int windowPosY = 50;
  455. Object obj;
  456. DbgUI.BeginCleanupScope();
  457. DbgUI.Begin("Action Targets", windowPosX, windowPosY);
  458. if ( enabled )
  459. {
  460. for ( int i = 0; i < GetTargetsCount(); i++ )
  461. {
  462. obj = m_Targets.Get(i).GetObject();
  463. if ( obj )
  464. {
  465. float util = m_Targets.Get(i).GetUtility();
  466. int compIdx = m_Targets.Get(i).GetComponentIndex();
  467. string compName;
  468. array<string> compNames = new array<string>;
  469. compName = obj.GetActionComponentName(compIdx);
  470. obj.GetActionComponentNameList(compIdx, compNames);
  471. if ( compNames.Count() > 0 )
  472. {
  473. for ( int c = 0; c < compNames.Count(); c++ )
  474. {
  475. DbgUI.Text(obj.GetDisplayName() + " :: " + obj + " | util: " + util + " | compIdx: " + compIdx + " | compName: " + compNames[c] + "| wPos: " + obj.GetWorldPosition() );
  476. }
  477. }
  478. else
  479. {
  480. DbgUI.Text(obj.GetDisplayName() + " :: " + obj + " | util: " + util + " | compIdx: " + compIdx + " | compName: " + compName + "| wPos: " + obj.GetWorldPosition() );
  481. }
  482. }
  483. else
  484. continue;
  485. }
  486. }
  487. DbgUI.End();
  488. DbgUI.EndCleanupScope();
  489. }
  490. void DrawDebugActionTargets(bool enabled)
  491. {
  492. int s_id;
  493. vector w_pos;
  494. vector w_pos_sphr;
  495. vector w_pos_lend;
  496. Object obj;
  497. if ( enabled )
  498. {
  499. CleanupDebugShapes(shapes);
  500. for ( int i = 0; i < GetTargetsCount(); i++ )
  501. {
  502. obj = m_Targets.Get(i).GetObject();
  503. if ( obj )
  504. {
  505. w_pos = obj.GetPosition();
  506. // sphere pos tweaks
  507. w_pos_sphr = w_pos;
  508. w_pos_sphr[1] = w_pos_sphr[1] + 0.5;
  509. // line pos tweaks
  510. w_pos_lend = w_pos;
  511. w_pos_lend[1] = w_pos_lend[1] + 0.5;
  512. if ( i == 0 )
  513. {
  514. shapes.Insert( Debug.DrawSphere(w_pos_sphr, 0.03, COLOR_RED) );
  515. shapes.Insert( Debug.DrawLine(w_pos, w_pos_lend, COLOR_RED) );
  516. }
  517. else
  518. {
  519. shapes.Insert( Debug.DrawSphere(w_pos_sphr, 0.03, COLOR_YELLOW) );
  520. shapes.Insert( Debug.DrawLine(w_pos, w_pos_lend, COLOR_YELLOW) );
  521. }
  522. }
  523. }
  524. }
  525. else
  526. CleanupDebugShapes(shapes);
  527. }
  528. private void DrawDebugCone(bool enabled)
  529. {
  530. // "cone" settings
  531. vector start, end, endL, endR;
  532. float playerAngle;
  533. float xL,xR,zL,zR;
  534. if (enabled)
  535. {
  536. CleanupDebugShapes(dbgConeShapes);
  537. start = m_Player.GetPosition();
  538. playerAngle = MiscGameplayFunctions.GetHeadingAngle(m_Player);
  539. // offset position of the shape in height
  540. start[1] = start[1] + 0.2;
  541. endL = start;
  542. endR = start;
  543. xL = c_MaxTargetDistance * Math.Cos(playerAngle + Math.PI_HALF + c_ConeAngle * Math.DEG2RAD); // x
  544. zL = c_MaxTargetDistance * Math.Sin(playerAngle + Math.PI_HALF + c_ConeAngle * Math.DEG2RAD); // z
  545. xR = c_MaxTargetDistance * Math.Cos(playerAngle + Math.PI_HALF - c_ConeAngle * Math.DEG2RAD); // x
  546. zR = c_MaxTargetDistance * Math.Sin(playerAngle + Math.PI_HALF - c_ConeAngle * Math.DEG2RAD); // z
  547. endL[0] = endL[0] + xL;
  548. endL[2] = endL[2] + zL;
  549. endR[0] = endR[0] + xR;
  550. endR[2] = endR[2] + zR;
  551. dbgConeShapes.Insert( Debug.DrawLine(start, endL, COLOR_BLUE) );
  552. dbgConeShapes.Insert( Debug.DrawLine(start, endR, COLOR_BLUE) ) ;
  553. dbgConeShapes.Insert( Debug.DrawLine(endL, endR, COLOR_BLUE) );
  554. }
  555. else
  556. CleanupDebugShapes(dbgConeShapes);
  557. }
  558. private void DrawSelectionPos(bool enabled)
  559. {
  560. if (enabled)
  561. {
  562. CleanupDebugShapes(dbgPosShapes);
  563. if (GetTargetsCount() > 0 && GetTarget(0).GetUtility() > -1 )
  564. {
  565. ActionTarget at = GetTarget(0);
  566. if (at.GetObject())
  567. {
  568. string compName = at.GetObject().GetActionComponentName(at.GetComponentIndex());
  569. vector modelPos = at.GetObject().GetSelectionPositionMS(compName);
  570. vector worldPos = at.GetObject().ModelToWorld(modelPos);
  571. dbgPosShapes.Insert( Debug.DrawSphere(worldPos, 0.25, Colors.PURPLE, ShapeFlags.NOZBUFFER) );
  572. }
  573. }
  574. }
  575. else
  576. CleanupDebugShapes(dbgPosShapes);
  577. }
  578. private void DrawDebugRay(bool enabled)
  579. {
  580. if (enabled)
  581. {
  582. CleanupDebugShapes(rayShapes);
  583. rayShapes.Insert( Debug.DrawSphere(m_HitPos, Math.Sqrt(c_UtilityMaxDistFromRaySqr), COLOR_BLUE_A, ShapeFlags.TRANSP) );
  584. rayShapes.Insert( Debug.DrawLine(m_RayStart, m_RayEnd, COLOR_BLUE) );
  585. }
  586. else
  587. CleanupDebugShapes(rayShapes);
  588. }
  589. private void CleanupDebugShapes(array<Shape> shapesArr)
  590. {
  591. for ( int it = 0; it < shapesArr.Count(); ++it )
  592. {
  593. Debug.RemoveShape( shapesArr[it] );
  594. }
  595. shapesArr.Clear();
  596. }
  597. #endif
  598. //--------------------------------------------------------
  599. // Members
  600. //--------------------------------------------------------
  601. //! player owner
  602. private PlayerBase m_Player;
  603. //! selected & sorted targets by utility function
  604. private ref array<ref ActionTarget> m_Targets;
  605. //! objects in vicinity
  606. static private ref VicinityObjects m_VicinityObjects
  607. private bool m_Debug
  608. private vector m_RayStart;
  609. private vector m_RayEnd;
  610. private vector m_HitPos;
  611. //--------------------------------------------------------
  612. // Constants
  613. //--------------------------------------------------------
  614. //! searching properties
  615. private const float c_RayDistance = 5.0;
  616. private const float c_MaxTargetDistance = 3.0;
  617. private const float c_MaxActionDistance = UAMaxDistances.DEFAULT;
  618. private const float c_ConeAngle = 30.0;
  619. private const float c_ConeHeightMin = -0.5;
  620. private const float c_ConeHeightMax = 2.0;
  621. private const float c_DistanceDelta = 0.3;
  622. //! utility constants
  623. private const float c_UtilityMaxValue = 10000;
  624. private const float c_UtilityMaxDistFromRaySqr = 0.8 * 0.8;
  625. //! p3d
  626. private const string CE_CENTER = "ce_center";
  627. private const float HEIGHT_OFFSET = 0.2;
  628. //! misc
  629. private const int OBSTRUCTED_COUNT_THRESHOLD = 3;
  630. private const int GROUPING_COUNT_THRESHOLD = 10;
  631. //! DEPRECATED
  632. vector CalculateRayStart();
  633. };
  634. class ObjectGroup
  635. {
  636. ref array<Object> Objects = new array<Object>;
  637. }