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