vicinityitemmanager.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. class VicinityItemManager
  2. {
  3. private const float UPDATE_FREQUENCY = 0.25;
  4. private const float VICINITY_DISTANCE = 0.5;
  5. private const float VICINITY_ACTOR_DISTANCE = 2.0;
  6. private const float VICINITY_LARGE_ACTOR_DISTANCE = 3.0;
  7. private const float VICINITY_CONE_DISTANCE = 2.0;
  8. private const float VICINITY_CONE_REACH_DISTANCE = 2.0;
  9. private const float VICINITY_CONE_ANGLE = 30;
  10. private const float VICINITY_CONE_RADIANS = 0.5;
  11. private const string CE_CENTER = "ce_center";
  12. private const float HEIGHT_OFFSET = 0.2;
  13. private const int OBJECT_OBSTRUCTION_WEIGHT = 10000; //in grams
  14. private const float CONE_HEIGHT_MIN = -0.5;
  15. private const float CONE_HEIGHT_MAX = 3.0;
  16. private ref array<EntityAI> m_VicinityItems = new array<EntityAI>();
  17. private ref array<CargoBase> m_VicinityCargos = new array<CargoBase>();
  18. private float m_RefreshCounter;
  19. private static ref VicinityItemManager s_Instance;
  20. static VicinityItemManager GetInstance ()
  21. {
  22. if (!s_Instance)
  23. s_Instance = new VicinityItemManager();
  24. return s_Instance;
  25. }
  26. void Init()
  27. {
  28. }
  29. array<EntityAI> GetVicinityItems()
  30. {
  31. return m_VicinityItems;
  32. }
  33. void AddVicinityItems(Object object)
  34. {
  35. EntityAI entity = EntityAI.Cast(object);
  36. if (!entity)
  37. {
  38. return;
  39. }
  40. if (m_VicinityItems.Find(entity) != INDEX_NOT_FOUND)
  41. {
  42. return;
  43. }
  44. if (GameInventory.CheckManipulatedObjectsDistances(entity, GetGame().GetPlayer(), VICINITY_CONE_REACH_DISTANCE + 1.0) == false)
  45. {
  46. if (!FreeDebugCamera.GetInstance() || FreeDebugCamera.GetInstance().IsActive() == false)
  47. {
  48. return;
  49. }
  50. }
  51. m_VicinityItems.Insert(entity);
  52. }
  53. array<CargoBase> GetVicinityCargos()
  54. {
  55. return m_VicinityCargos;
  56. }
  57. void AddVicinityCargos(CargoBase object)
  58. {
  59. if (m_VicinityCargos.Find(object) == INDEX_NOT_FOUND)
  60. {
  61. m_VicinityCargos.Insert(object);
  62. }
  63. }
  64. void ResetRefreshCounter()
  65. {
  66. m_RefreshCounter = 0;
  67. }
  68. void Update(float delta_time)
  69. {
  70. m_RefreshCounter += delta_time;
  71. if (m_RefreshCounter >= UPDATE_FREQUENCY)
  72. {
  73. RefreshVicinityItems();
  74. m_RefreshCounter = 0;
  75. }
  76. }
  77. bool ExcludeFromContainer_Phase1(Object actor_in_radius)
  78. {
  79. EntityAI entity;
  80. if (!Class.CastTo(entity, actor_in_radius))
  81. return true;
  82. PlayerBase player = PlayerBase.Cast(GetGame().GetPlayer());
  83. if (entity == player)
  84. return true;
  85. if (entity.IsParticle())
  86. return true;
  87. if (entity.IsScriptedLight())
  88. return true;
  89. if (entity.IsBeingPlaced())
  90. return true;
  91. if (entity.IsHologram())
  92. return true;
  93. if (entity.IsMan() || entity.IsZombie() || entity.IsZombieMilitary())
  94. {
  95. //visibility cone check
  96. vector entityPosition = entity.GetPosition();
  97. if (entity && entity.IsMan())
  98. {
  99. PlayerBase vicinityPlayer = PlayerBase.Cast(entity);
  100. if (vicinityPlayer)
  101. {
  102. entityPosition = vicinityPlayer.GetBonePositionWS(vicinityPlayer.GetBoneIndexByName("spine3"));
  103. }
  104. }
  105. else if (entity && (entity.IsZombie() || entity.IsZombieMilitary()))
  106. {
  107. ZombieBase zombie = ZombieBase.Cast(entity);
  108. if (zombie)
  109. {
  110. entityPosition = zombie.GetBonePositionWS(zombie.GetBoneIndexByName("spine3"));
  111. }
  112. }
  113. //! If in free camera, allow viewing of inventory
  114. if (FreeDebugCamera.GetInstance() && FreeDebugCamera.GetInstance().IsActive())
  115. {
  116. return false;
  117. }
  118. vector entityDirection = player.GetPosition() - entityPosition;
  119. entityDirection.Normalize();
  120. entityDirection[1] = 0; //ignore height
  121. vector playerDirection = MiscGameplayFunctions.GetHeadingVector(player);
  122. playerDirection.Normalize();
  123. playerDirection[1] = 0; //ignore height
  124. float dotRadians = vector.Dot(playerDirection, entityDirection);
  125. if (dotRadians > -0.5)
  126. return true;
  127. }
  128. return false;
  129. }
  130. bool ExcludeFromContainer_Phase2(Object object_in_radius)
  131. {
  132. EntityAI entity;
  133. if (!Class.CastTo(entity, object_in_radius))
  134. return true;
  135. if (entity == PlayerBase.Cast(GetGame().GetPlayer()))
  136. return true;
  137. if (entity.IsParticle())
  138. return true;
  139. if (entity.IsScriptedLight())
  140. return true;
  141. if (entity.IsBeingPlaced())
  142. return true;
  143. if (entity.IsHologram())
  144. return true;
  145. ItemBase item;
  146. if (!Class.CastTo(item, object_in_radius))
  147. return true;
  148. return false;
  149. }
  150. bool ExcludeFromContainer_Phase3(Object object_in_cone)
  151. {
  152. EntityAI entity;
  153. PlayerBase player = PlayerBase.Cast(GetGame().GetPlayer());
  154. //Print("---object in cone: " + object_in_cone);
  155. if (!Class.CastTo(entity, object_in_cone))
  156. return true;
  157. if (entity == player)
  158. return true;
  159. if (entity.IsParticle())
  160. return true;
  161. if (entity.IsScriptedLight())
  162. return true;
  163. if (entity.IsBeingPlaced())
  164. return true;
  165. if (entity.IsHologram())
  166. return true;
  167. ItemBase item;
  168. if (!Class.CastTo(item, object_in_cone) && !object_in_cone.IsTransport() && !PASBroadcaster.Cast(object_in_cone))
  169. return true;
  170. return false;
  171. }
  172. bool CanIgnoreDistanceCheck(EntityAI entity_ai)
  173. {
  174. return MiscGameplayFunctions.CanIgnoreDistanceCheck(entity_ai);
  175. }
  176. //per frame call
  177. void RefreshVicinityItems()
  178. {
  179. PlayerBase player = PlayerBase.Cast(GetGame().GetPlayer());
  180. array<Object> objectsInVicinity = new array<Object>();
  181. array<CargoBase> proxyCargos = new array<CargoBase>();
  182. array<Object> filteredObjects = new array<Object>();
  183. array<Object> allFoundObjects = new array<Object>();
  184. vector playerPosition = player.GetPosition();
  185. vector playerHeadPositionFixed = playerPosition;
  186. playerHeadPositionFixed[1] = playerHeadPositionFixed[1] + GetFixedHeadHeightAdjustment(player);
  187. vector headingDirection = MiscGameplayFunctions.GetHeadingVector(player);
  188. //! If in free camera, override the position that the inventory gets generated from
  189. bool cameraActive = FreeDebugCamera.GetInstance() && FreeDebugCamera.GetInstance().IsActive();
  190. if (cameraActive)
  191. {
  192. playerPosition = FreeDebugCamera.GetInstance().GetPosition();
  193. playerHeadPositionFixed = playerPosition;
  194. float headingAngle = FreeDebugCamera.GetInstance().GetOrientation()[0] * Math.DEG2RAD;
  195. headingDirection[0] = Math.Cos(headingAngle);
  196. headingDirection[1] = 0;
  197. headingDirection[2] = Math.Sin(headingAngle);
  198. headingDirection.Normalize();
  199. }
  200. if (m_VicinityItems)
  201. m_VicinityItems.Clear();
  202. if (m_VicinityCargos)
  203. m_VicinityCargos.Clear();
  204. //1. GetAll actors in VICINITY_ACTOR_DISTANCE
  205. //DebugActorsSphereDraw(VICINITY_ACTOR_DISTANCE);
  206. GetGame().GetObjectsAtPosition3D(playerPosition, VICINITY_ACTOR_DISTANCE, objectsInVicinity, proxyCargos);
  207. // no filtering for cargo (initial implementation)
  208. foreach (CargoBase cargoObject : proxyCargos)
  209. AddVicinityCargos(cargoObject);
  210. //filter unnecessary and duplicate objects beforehand
  211. foreach (Object actorInRadius : objectsInVicinity)
  212. {
  213. if (allFoundObjects.Find(actorInRadius) == INDEX_NOT_FOUND)
  214. allFoundObjects.Insert(actorInRadius);
  215. if (ExcludeFromContainer_Phase1(actorInRadius))
  216. continue;
  217. if (filteredObjects.Find(actorInRadius) == INDEX_NOT_FOUND)
  218. filteredObjects.Insert(actorInRadius);
  219. }
  220. if (objectsInVicinity)
  221. objectsInVicinity.Clear();
  222. //2. GetAll Objects in VICINITY_DISTANCE
  223. GetGame().GetObjectsAtPosition3D(playerPosition, VICINITY_DISTANCE, objectsInVicinity, proxyCargos);
  224. //DebugObjectsSphereDraw(VICINITY_DISTANCE);
  225. //filter unnecessary and duplicate objects beforehand
  226. foreach (Object objectInRadius : objectsInVicinity)
  227. {
  228. if (allFoundObjects.Find(objectInRadius) == INDEX_NOT_FOUND)
  229. allFoundObjects.Insert(objectInRadius);
  230. if (ExcludeFromContainer_Phase2(objectInRadius))
  231. continue;
  232. if (filteredObjects.Find(objectInRadius) == INDEX_NOT_FOUND)
  233. filteredObjects.Insert(objectInRadius);
  234. }
  235. if (objectsInVicinity)
  236. objectsInVicinity.Clear();
  237. //3. Add objects from GetEntitiesInCone
  238. DayZPlayerUtils.GetEntitiesInCone(playerPosition, headingDirection, VICINITY_CONE_ANGLE, VICINITY_CONE_REACH_DISTANCE, CONE_HEIGHT_MIN, CONE_HEIGHT_MAX, objectsInVicinity);
  239. //DebugConeDraw(playerPosition, VICINITY_CONE_ANGLE);
  240. RaycastRVParams rayInput;
  241. array<ref RaycastRVResult> raycastResults = new array<ref RaycastRVResult>();
  242. //filter unnecessary and duplicate objects beforehand
  243. foreach (Object objectInCone : objectsInVicinity)
  244. {
  245. if (allFoundObjects.Find(objectInCone) == INDEX_NOT_FOUND)
  246. allFoundObjects.Insert(objectInCone);
  247. if (ExcludeFromContainer_Phase3(objectInCone))
  248. continue;
  249. if (filteredObjects.Find(objectInCone) == INDEX_NOT_FOUND)
  250. {
  251. rayInput = new RaycastRVParams(playerHeadPositionFixed, objectInCone.GetPosition());
  252. rayInput.flags = CollisionFlags.ALLOBJECTS;
  253. rayInput.type = ObjIntersectView;
  254. rayInput.radius = 0.1;
  255. DayZPhysics.RaycastRVProxy(rayInput, raycastResults, {player});
  256. //Debug.DrawLine(playerHeadPositionFixed,objectInCone.GetPosition(),COLOR_WHITE,ShapeFlags.ONCE);
  257. foreach (RaycastRVResult result : raycastResults)
  258. {
  259. if (vector.DistanceSq(result.pos, playerPosition) > VICINITY_CONE_REACH_DISTANCE * VICINITY_CONE_REACH_DISTANCE)
  260. continue;
  261. if (result.hierLevel > 0)
  262. {
  263. if (result.parent.CanProxyObstruct())
  264. break;
  265. if (result.parent == objectInCone)
  266. {
  267. filteredObjects.Insert(objectInCone);
  268. break;
  269. }
  270. }
  271. else
  272. {
  273. if (result.obj == objectInCone)
  274. {
  275. filteredObjects.Insert(objectInCone);
  276. break;
  277. }
  278. }
  279. }
  280. }
  281. }
  282. //4. Add large objects - particularly buildings and BaseBuildingBase
  283. BoxCollidingParams params = new BoxCollidingParams();
  284. vector boxEdgeLength = {
  285. VICINITY_LARGE_ACTOR_DISTANCE,
  286. VICINITY_LARGE_ACTOR_DISTANCE,
  287. VICINITY_LARGE_ACTOR_DISTANCE
  288. };
  289. params.SetParams(playerPosition, headingDirection.VectorToAngles(), boxEdgeLength * 2, ObjIntersect.View, ObjIntersect.Fire, true);
  290. array<ref BoxCollidingResult> results = new array<ref BoxCollidingResult>();
  291. if (GetGame().IsBoxCollidingGeometryProxy(params, {player}, results))
  292. {
  293. foreach (BoxCollidingResult bResult : results)
  294. {
  295. if (bResult.obj && (bResult.obj.CanObstruct() || bResult.obj.CanProxyObstruct()))
  296. {
  297. if (allFoundObjects.Find(bResult.obj) == INDEX_NOT_FOUND)
  298. allFoundObjects.Insert(bResult.obj);
  299. }
  300. if (bResult.parent && (bResult.parent.CanObstruct() || bResult.parent.CanProxyObstruct()))
  301. {
  302. if (allFoundObjects.Find(bResult.parent) == INDEX_NOT_FOUND)
  303. allFoundObjects.Insert(bResult.parent);
  304. }
  305. }
  306. }
  307. //5. Filter filtered objects with RayCast from the player ( head bone )
  308. array<Object> obstructingObjects = new array<Object>();
  309. MiscGameplayFunctions.FilterObstructingObjects(allFoundObjects, obstructingObjects);
  310. //! If in free camera, don't worry about obstructed objects if there are any
  311. if (obstructingObjects.Count() > 0 && !cameraActive)
  312. {
  313. //obstructingObjects.Debug();
  314. if (filteredObjects.Count() > 10)
  315. {
  316. array<Object> filteredObjectsGrouped = new array<Object>();
  317. MiscGameplayFunctions.FilterObstructedObjectsByGrouping(playerHeadPositionFixed, VICINITY_CONE_DISTANCE, 0.3, filteredObjects, obstructingObjects, filteredObjectsGrouped, true, true, VICINITY_CONE_REACH_DISTANCE);
  318. foreach (Object object: filteredObjectsGrouped)
  319. AddVicinityItems(object);
  320. }
  321. else
  322. {
  323. foreach (Object filteredObjectClose: filteredObjects)
  324. {
  325. EntityAI entity;
  326. Class.CastTo(entity, filteredObjectClose);
  327. //distance check
  328. if (vector.DistanceSq(playerPosition, entity.GetPosition()) > VICINITY_CONE_REACH_DISTANCE * VICINITY_CONE_REACH_DISTANCE)
  329. {
  330. if (!CanIgnoreDistanceCheck(entity))
  331. {
  332. continue;
  333. }
  334. }
  335. if (!IsObstructed(filteredObjectClose))
  336. {
  337. AddVicinityItems(filteredObjectClose);
  338. }
  339. }
  340. }
  341. }
  342. else
  343. {
  344. foreach (Object filteredObject: filteredObjects)
  345. AddVicinityItems(filteredObject);
  346. }
  347. }
  348. protected float GetFixedHeadHeightAdjustment(PlayerBase player)
  349. {
  350. if (player.IsPlayerInStance(DayZPlayerConstants.STANCEMASK_ERECT|DayZPlayerConstants.STANCEMASK_RAISEDERECT))
  351. return PlayerConstants.HEAD_HEIGHT_ERECT;
  352. if (player.IsPlayerInStance(DayZPlayerConstants.STANCEMASK_CROUCH|DayZPlayerConstants.STANCEMASK_RAISEDCROUCH))
  353. return PlayerConstants.HEAD_HEIGHT_CROUCH;
  354. if (player.IsPlayerInStance(DayZPlayerConstants.STANCEMASK_PRONE|DayZPlayerConstants.STANCEMASK_RAISEDPRONE))
  355. return PlayerConstants.HEAD_HEIGHT_PRONE;
  356. return PlayerConstants.HEAD_HEIGHT_ERECT;
  357. }
  358. bool IsObstructed(Object filtered_object)
  359. {
  360. return MiscGameplayFunctions.IsObjectObstructed(filtered_object);
  361. }
  362. #ifdef DIAG_DEVELOPER
  363. //Debug functions
  364. ref array<Shape> rayShapes = new array<Shape>();
  365. private void DebugActorsSphereDraw(float radius)
  366. {
  367. CleanupDebugShapes(rayShapes);
  368. rayShapes.Insert(Debug.DrawSphere( GetGame().GetPlayer().GetPosition(), radius, COLOR_GREEN, ShapeFlags.TRANSP|ShapeFlags.WIREFRAME));
  369. }
  370. private void DebugObjectsSphereDraw(float radius)
  371. {
  372. rayShapes.Insert(Debug.DrawSphere(GetGame().GetPlayer().GetPosition(), radius, COLOR_GREEN, ShapeFlags.TRANSP|ShapeFlags.WIREFRAME));
  373. }
  374. private void DebugRaycastDraw(vector start, vector end)
  375. {
  376. rayShapes.Insert(Debug.DrawArrow(start, end, 0.5, COLOR_YELLOW));
  377. }
  378. private void DebugConeDraw(vector start, float cone_angle)
  379. {
  380. vector endL, endR;
  381. float playerAngle;
  382. float xL,xR,zL,zR;
  383. playerAngle = MiscGameplayFunctions.GetHeadingAngle(PlayerBase.Cast(GetGame().GetPlayer()));
  384. endL = start;
  385. endR = start;
  386. xL = VICINITY_CONE_REACH_DISTANCE * Math.Cos( playerAngle + Math.PI_HALF + cone_angle * Math.DEG2RAD ); // x
  387. zL = VICINITY_CONE_REACH_DISTANCE * Math.Sin( playerAngle + Math.PI_HALF + cone_angle * Math.DEG2RAD ); // z
  388. xR = VICINITY_CONE_REACH_DISTANCE * Math.Cos( playerAngle + Math.PI_HALF - cone_angle * Math.DEG2RAD ); // x
  389. zR = VICINITY_CONE_REACH_DISTANCE * Math.Sin( playerAngle + Math.PI_HALF - cone_angle * Math.DEG2RAD ); // z
  390. endL[0] = endL[0] + xL;
  391. endL[2] = endL[2] + zL;
  392. endR[0] = endR[0] + xR;
  393. endR[2] = endR[2] + zR;
  394. rayShapes.Insert(Debug.DrawLine(start, endL, COLOR_GREEN));
  395. rayShapes.Insert(Debug.DrawLine(start, endR, COLOR_GREEN)) ;
  396. rayShapes.Insert(Debug.DrawLine(endL, endR, COLOR_GREEN));
  397. }
  398. private void CleanupDebugShapes(array<Shape> shapesArr)
  399. {
  400. foreach (Shape shape : shapesArr)
  401. Debug.RemoveShape(shape);
  402. shapesArr.Clear();
  403. }
  404. #endif
  405. }