| 974 | } |
| 975 | |
| 976 | void FindNewTarget(ItemInfo& laraItem, const WeaponInfo& weaponInfo) |
| 977 | { |
| 978 | if (!g_Configuration.EnableAutoTargeting) |
| 979 | return; |
| 980 | |
| 981 | auto& player = *GetLaraInfo(&laraItem); |
| 982 | |
| 983 | if (player.Control.Look.OpticRange) |
| 984 | { |
| 985 | player.TargetEntity = nullptr; |
| 986 | return; |
| 987 | } |
| 988 | |
| 989 | float muzzleOffset = GetJointPosition(&laraItem, LM_RHAND).y - laraItem.Pose.Position.y; |
| 990 | |
| 991 | auto origin = GameVector( |
| 992 | laraItem.Pose.Position.x, |
| 993 | laraItem.Pose.Position.y + muzzleOffset, |
| 994 | laraItem.Pose.Position.z, |
| 995 | laraItem.RoomNumber); |
| 996 | |
| 997 | ItemInfo* closestEntityPtr = nullptr; |
| 998 | |
| 999 | float closestDistance = FLT_MAX; |
| 1000 | short closestHeadingAngle = SHRT_MAX; |
| 1001 | unsigned int targetCount = 0; |
| 1002 | float maxDistance = weaponInfo.TargetDist; |
| 1003 | |
| 1004 | for (auto creatureIndex : ActiveCreatures) |
| 1005 | { |
| 1006 | auto* creaturePtr = GetCreatureInfo(&g_Level.Items[creatureIndex]); |
| 1007 | |
| 1008 | // Continue loop if no item. |
| 1009 | if (creaturePtr->ItemNumber == NO_VALUE) |
| 1010 | continue; |
| 1011 | |
| 1012 | auto& item = g_Level.Items[creaturePtr->ItemNumber]; |
| 1013 | |
| 1014 | // Check if creature is alive. |
| 1015 | if (item.HitPoints <= 0) |
| 1016 | continue; |
| 1017 | |
| 1018 | // Offset target position to the same height as muzzle. |
| 1019 | Vector3 pos = item.Pose.Position.ToVector3(); |
| 1020 | pos.y += muzzleOffset; |
| 1021 | |
| 1022 | // Check distance. |
| 1023 | float distance = Vector3::Distance(origin.ToVector3(), pos); |
| 1024 | if (distance > maxDistance) |
| 1025 | continue; |
| 1026 | |
| 1027 | // Assess room line of sight. |
| 1028 | auto target = GetTargetPoint(item); |
| 1029 | if (!LOS(&origin, &target)) |
| 1030 | continue; |
| 1031 | |
| 1032 | // Assess occlusion by other moveables and static meshes. |
| 1033 | if (IsTargetOccludedByObjects(laraItem, origin.ToVector3(), target.ToVector3(), distance)) |
no test coverage detected