202{
203 if (
Player ==
nullptr || Candidates.IsEmpty())
204 {
205 return nullptr;
206 }
207
209 int32 BestNeighborCount = INDEX_NONE;
210 float BestDistanceToClusterCenterSq = TNumericLimits<float>::Max();
211 float BestDistanceToPlayerSq = TNumericLimits<float>::Max();
213 const FVector PlayerLocation =
Player->GetActorLocation();
214
216 {
217 if (Candidate == nullptr)
218 {
219 continue;
220 }
221
222 const FVector CandidateLocation = Candidate->GetTargetedActorLocation();
223 FVector ClusterCenter = FVector::ZeroVector;
224 int32 NeighborCount = 0;
225
227 {
228 if (Other == nullptr)
229 {
230 continue;
231 }
232
233 const FVector OtherLocation = Other->GetTargetedActorLocation();
234
235 if (FVector::DistSquared2D(CandidateLocation, OtherLocation) > DensityRadiusSq)
236 {
237 continue;
238 }
239
240 ClusterCenter += OtherLocation;
241 NeighborCount++;
242 }
243
244 if (NeighborCount <= 0)
245 {
246 continue;
247 }
248
249 ClusterCenter /= static_cast<float>(NeighborCount);
250
251 const float DistanceToClusterCenterSq = FVector::DistSquared2D(CandidateLocation, ClusterCenter);
252 const float DistanceToPlayerSq = FVector::DistSquared2D(CandidateLocation, PlayerLocation);
253 const bool bHasMoreNeighbors = NeighborCount > BestNeighborCount;
254 const bool bCloserToClusterCenter = NeighborCount == BestNeighborCount && DistanceToClusterCenterSq < BestDistanceToClusterCenterSq;
255 const bool bCloserToPlayer = NeighborCount == BestNeighborCount && FMath::IsNearlyEqual(DistanceToClusterCenterSq, BestDistanceToClusterCenterSq) && DistanceToPlayerSq < BestDistanceToPlayerSq;
256
257 if (bHasMoreNeighbors || bCloserToClusterCenter || bCloserToPlayer)
258 {
259 BestTarget = Candidate;
260 BestNeighborCount = NeighborCount;
261 BestDistanceToClusterCenterSq = DistanceToClusterCenterSq;
262 BestDistanceToPlayerSq = DistanceToPlayerSq;
263 }
264 }
265
266 return BestTarget;
267}
float DensityRadius
Definition TargetSelector.h:77