actiontargets.c 22 KB

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