From 4fb19151be503a7483022d8bd73c5cacc6be2703 Mon Sep 17 00:00:00 2001 From: Cj <161484149+CJ-SPT@users.noreply.github.com> Date: Tue, 24 Jun 2025 11:28:46 -0400 Subject: [PATCH] Merge pull request #424 from CJ-SPT/GetLowestHp-optimization Optimize GetLowestHp() --- .../Generators/BotGenerator.cs | 46 +++++++------------ 1 file changed, 16 insertions(+), 30 deletions(-) diff --git a/Libraries/SPTarkov.Server.Core/Generators/BotGenerator.cs b/Libraries/SPTarkov.Server.Core/Generators/BotGenerator.cs index 7c4ac327..53051d2d 100644 --- a/Libraries/SPTarkov.Server.Core/Generators/BotGenerator.cs +++ b/Libraries/SPTarkov.Server.Core/Generators/BotGenerator.cs @@ -668,43 +668,29 @@ public class BotGenerator( } /// - /// Sum up body parts max hp values, return the bodyPart collection with the lowest value + /// Get the bodyPart with the lowest hp /// - /// Body parts to sum up - /// Lowest hp collection - public BodyPart? GetLowestHpBody(List bodies) + /// Body parts + /// Part with the lowest hp + public BodyPart? GetLowestHpBody(List bodyParts) { - if (bodies.Count == 0) + if (bodyParts.Count == 0) { return null; } - BodyPart result = new(); - var props = result.GetType().GetProperties(); - double? currentHighest = double.MaxValue; - foreach (var bodyPart in bodies) - { - double? hpTotal = 0; - - foreach ( - var prop in props.Where(property => - !property.Name.Equals("extensiondata", StringComparison.OrdinalIgnoreCase) - ) - ) + return bodyParts.Select(bp => new { - var value = (MinMax)prop.GetValue(bodyPart); - hpTotal += value.Max; - } - - if (hpTotal < currentHighest) - { - // Found collection with lower value that previous, use it - currentHighest = hpTotal; - result = bodyPart; - } - } - - return result; + BodyPart = bp, + TotalMaxHp = bp.Head.Max + + bp.Chest.Max + + bp.LeftArm.Max + + bp.RightArm.Max + + bp.LeftLeg.Max + + bp.RightLeg.Max + }) + .OrderBy(x => x.TotalMaxHp) + .FirstOrDefault()?.BodyPart; } ///