vicinityitemmanager.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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] = playerPosition[1] + 1.6;
  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. //Test distance to closest component first
  252. rayInput = new RaycastRVParams(playerHeadPositionFixed, objectInCone.GetPosition());
  253. rayInput.flags = CollisionFlags.NEARESTCONTACT;
  254. rayInput.type = ObjIntersectView;
  255. rayInput.radius = 0.1;
  256. DayZPhysics.RaycastRVProxy(rayInput, raycastResults, {player});
  257. foreach (RaycastRVResult result : raycastResults)
  258. {
  259. if (vector.DistanceSq(result.pos, playerHeadPositionFixed) > VICINITY_CONE_REACH_DISTANCE * VICINITY_CONE_REACH_DISTANCE)
  260. continue;
  261. if (result.hierLevel > 0 && result.parent == objectInCone)
  262. filteredObjects.Insert(objectInCone);
  263. else if (result.hierLevel == 0 && result.obj == objectInCone)
  264. filteredObjects.Insert(objectInCone);
  265. }
  266. }
  267. }
  268. //4. Add large objects - particularly buildings and BaseBuildingBase
  269. BoxCollidingParams params = new BoxCollidingParams();
  270. vector boxEdgeLength = {
  271. VICINITY_LARGE_ACTOR_DISTANCE,
  272. VICINITY_LARGE_ACTOR_DISTANCE,
  273. VICINITY_LARGE_ACTOR_DISTANCE
  274. };
  275. params.SetParams(playerPosition, headingDirection.VectorToAngles(), boxEdgeLength * 2, ObjIntersect.View, ObjIntersect.Fire, true);
  276. array<ref BoxCollidingResult> results = new array<ref BoxCollidingResult>();
  277. if (GetGame().IsBoxCollidingGeometryProxy(params, {player}, results))
  278. {
  279. foreach (BoxCollidingResult bResult : results)
  280. {
  281. if (bResult.obj && (bResult.obj.CanObstruct() || bResult.obj.CanProxyObstruct()))
  282. {
  283. if (allFoundObjects.Find(bResult.obj) == INDEX_NOT_FOUND)
  284. {
  285. allFoundObjects.Insert(bResult.obj);
  286. }
  287. }
  288. if (bResult.parent && (bResult.parent.CanObstruct() || bResult.parent.CanProxyObstruct()))
  289. {
  290. if (allFoundObjects.Find(bResult.parent) == INDEX_NOT_FOUND)
  291. {
  292. allFoundObjects.Insert(bResult.parent);
  293. }
  294. }
  295. }
  296. }
  297. //5. Filter filtered objects with RayCast from the player ( head bone )
  298. array<Object> obstructingObjects = new array<Object>();
  299. MiscGameplayFunctions.FilterObstructingObjects(allFoundObjects, obstructingObjects);
  300. //! If in free camera, don't worry about obstructed objects if there are any
  301. if (obstructingObjects.Count() > 0 && !cameraActive)
  302. {
  303. if (filteredObjects.Count() > 10)
  304. {
  305. vector rayStart;
  306. MiscGameplayFunctions.GetHeadBonePos(player, rayStart);
  307. array<Object> filteredObjectsGrouped = new array<Object>();
  308. MiscGameplayFunctions.FilterObstructedObjectsByGrouping(rayStart, VICINITY_CONE_DISTANCE, 0.3, filteredObjects, obstructingObjects, filteredObjectsGrouped, true, true, VICINITY_CONE_REACH_DISTANCE);
  309. foreach (Object object: filteredObjectsGrouped)
  310. AddVicinityItems(object);
  311. }
  312. else
  313. {
  314. foreach (Object filteredObjectClose: filteredObjects)
  315. {
  316. EntityAI entity;
  317. Class.CastTo(entity, filteredObjectClose);
  318. //distance check
  319. if (vector.DistanceSq(playerPosition, entity.GetPosition()) > VICINITY_CONE_REACH_DISTANCE * VICINITY_CONE_REACH_DISTANCE)
  320. {
  321. if (!CanIgnoreDistanceCheck(entity))
  322. {
  323. continue;
  324. }
  325. }
  326. if (!IsObstructed(filteredObjectClose))
  327. {
  328. AddVicinityItems(filteredObjectClose);
  329. }
  330. }
  331. }
  332. }
  333. else
  334. {
  335. foreach (Object filteredObject: filteredObjects)
  336. AddVicinityItems(filteredObject);
  337. }
  338. }
  339. bool IsObstructed(Object filtered_object)
  340. {
  341. return MiscGameplayFunctions.IsObjectObstructed(filtered_object);
  342. }
  343. #ifdef DIAG_DEVELOPER
  344. //Debug functions
  345. ref array<Shape> rayShapes = new array<Shape>();
  346. private void DebugActorsSphereDraw(float radius)
  347. {
  348. CleanupDebugShapes(rayShapes);
  349. rayShapes.Insert(Debug.DrawSphere( GetGame().GetPlayer().GetPosition(), radius, COLOR_GREEN, ShapeFlags.TRANSP|ShapeFlags.WIREFRAME));
  350. }
  351. private void DebugObjectsSphereDraw(float radius)
  352. {
  353. rayShapes.Insert(Debug.DrawSphere(GetGame().GetPlayer().GetPosition(), radius, COLOR_GREEN, ShapeFlags.TRANSP|ShapeFlags.WIREFRAME));
  354. }
  355. private void DebugRaycastDraw(vector start, vector end)
  356. {
  357. rayShapes.Insert(Debug.DrawArrow(start, end, 0.5, COLOR_YELLOW));
  358. }
  359. private void DebugConeDraw(vector start, float cone_angle)
  360. {
  361. vector endL, endR;
  362. float playerAngle;
  363. float xL,xR,zL,zR;
  364. playerAngle = MiscGameplayFunctions.GetHeadingAngle(PlayerBase.Cast(GetGame().GetPlayer()));
  365. endL = start;
  366. endR = start;
  367. xL = VICINITY_CONE_REACH_DISTANCE * Math.Cos( playerAngle + Math.PI_HALF + cone_angle * Math.DEG2RAD ); // x
  368. zL = VICINITY_CONE_REACH_DISTANCE * Math.Sin( playerAngle + Math.PI_HALF + cone_angle * Math.DEG2RAD ); // z
  369. xR = VICINITY_CONE_REACH_DISTANCE * Math.Cos( playerAngle + Math.PI_HALF - cone_angle * Math.DEG2RAD ); // x
  370. zR = VICINITY_CONE_REACH_DISTANCE * Math.Sin( playerAngle + Math.PI_HALF - cone_angle * Math.DEG2RAD ); // z
  371. endL[0] = endL[0] + xL;
  372. endL[2] = endL[2] + zL;
  373. endR[0] = endR[0] + xR;
  374. endR[2] = endR[2] + zR;
  375. rayShapes.Insert(Debug.DrawLine(start, endL, COLOR_GREEN));
  376. rayShapes.Insert(Debug.DrawLine(start, endR, COLOR_GREEN)) ;
  377. rayShapes.Insert(Debug.DrawLine(endL, endR, COLOR_GREEN));
  378. }
  379. private void CleanupDebugShapes(array<Shape> shapesArr)
  380. {
  381. foreach (Shape shape : shapesArr)
  382. Debug.RemoveShape(shape);
  383. shapesArr.Clear();
  384. }
  385. #endif
  386. }