(a8f2f39bd) The ai now sorts the weapons by the combat priority value, which can be set in xml. Ranged weapons are favored. The required items are also taken into account.

This commit is contained in:
Joonas Rikkonen
2019-05-16 05:19:14 +03:00
parent 9facded24c
commit 363eb0540a
11 changed files with 183 additions and 224 deletions
@@ -111,21 +111,38 @@ namespace Barotrauma
}
}
private HashSet<RangedWeapon> rangedWeapons = new HashSet<RangedWeapon>();
private HashSet<MeleeWeapon> meleeWeapons = new HashSet<MeleeWeapon>();
private readonly HashSet<Item> adHocWeapons = new HashSet<Item>();
private Item GetWeapon()
{
rangedWeapons.Clear();
meleeWeapons.Clear();
adHocWeapons.Clear();
Item weapon = null;
_weaponComponent = null;
var weapon = character.Inventory.FindItemByTag("weapon");
if (weapon == null)
foreach (var item in character.Inventory.Items)
{
foreach (var item in character.Inventory.Items)
if (item == null) { continue; }
foreach (var component in item.Components)
{
if (item == null) { continue; }
foreach (var component in item.Components)
if (component is RangedWeapon rw)
{
if (component is MeleeWeapon || component is RangedWeapon)
if (rw.HasRequiredContainedItems(false))
{
return item;
rangedWeapons.Add(rw);
}
}
else if (component is MeleeWeapon mw)
{
if (mw.HasRequiredContainedItems(false))
{
meleeWeapons.Add(mw);
}
}
else
{
var effects = component.statusEffectLists;
if (effects != null)
{
@@ -135,7 +152,10 @@ namespace Barotrauma
{
if (statusEffect.Afflictions.Any())
{
return item;
if (component.HasRequiredContainedItems(false))
{
adHocWeapons.Add(item);
}
}
}
}
@@ -143,6 +163,20 @@ namespace Barotrauma
}
}
}
var rangedWeapon = rangedWeapons.OrderByDescending(w => w.CombatPriority).FirstOrDefault();
var meleeWeapon = meleeWeapons.OrderByDescending(w => w.CombatPriority).FirstOrDefault();
if (rangedWeapon != null)
{
weapon = rangedWeapon.Item;
}
else if (meleeWeapon != null)
{
weapon = meleeWeapon.Item;
}
if (weapon == null)
{
weapon = adHocWeapons.GetRandom(Rand.RandSync.Server);
}
return weapon;
}