internal static bool tryMeleeWeaponSwap(Pawn pawn, bool dropCurrent, bool considerUnarmed, bool skipDangerous)
        {
            if (pawn.Dead)
                return false;
            Thing best = null;

            bool unarmedIsBest;
            
            if (pawn.inventory.innerContainer.Any((Thing x) => x.def.IsMeleeWeapon))
            {
                best = GettersFilters.findBestMeleeWeapon(pawn, skipDangerous, out unarmedIsBest/*, MeleeSelectionMode*/);
            }
            else
            {
                unarmedIsBest = true;
            }

            if (best == null | unarmedIsBest)
            {
                if (considerUnarmed)
                {
                    SetPrimary(pawn, null, false, true, dropCurrent, false);
                }
            }
            else
            {
                if (best is ThingWithComps)
                {
                    SetPrimary(pawn, best, false, true, dropCurrent, false);
                    return true;
                }
            }
            
            return false;
        }
Exemple #2
0
        internal static bool tryMeleeWeaponSwap(Pawn pawn, bool dropCurrent, bool considerUnarmed, bool skipDangerous, Pawn target = null)
        {
            if (pawn.Dead)
            {
                return(false);
            }
            ThingWithComps best = GettersFilters.findBestMeleeWeapon(pawn, skipDangerous /*, MeleeSelectionMode*/, target);

            if (best != null || considerUnarmed)
            {
                return(SetPrimary(pawn, best, false, true, dropCurrent, false));
            }

            return(false);
        }
        public static void equipBestWeaponFromInventoryByPreference(Pawn pawn, DroppingModeEnum drop, GoldfishModule.PrimaryWeaponMode?modeOverride = null, Pawn target = null)
        {
            GoldfishModule pawnMemory = GoldfishModule.GetGoldfishForPawn(pawn);

            if (pawn == null || pawn.Dead || pawnMemory == null || pawn.equipment == null || pawn.inventory == null)
            {
                return;
            }

            GoldfishModule.PrimaryWeaponMode mode = modeOverride == null ? pawnMemory.primaryWeaponMode : modeOverride.Value;

            if (pawn.Drafted &&
                (pawnMemory.ForcedUnarmedWhileDrafted || pawnMemory.ForcedUnarmed && pawnMemory.ForcedWeaponWhileDrafted == null))
            {
                if (pawn.equipment.Primary != null)
                {
                    bool success = equipSpecificWeapon(pawn, null, MiscUtils.shouldDrop(drop), false);
                    if (success)
                    {
                        return;
                    }
                }
                else
                {
                    return;
                }
            }
            if (pawn.Drafted && pawnMemory.ForcedWeaponWhileDrafted != null)
            {
                if (pawn.equipment.Primary == null || pawn.equipment.Primary.toThingStuffPair() != pawnMemory.ForcedWeaponWhileDrafted.Value)
                {
                    var  requiredWeapon = pawnMemory.ForcedWeaponWhileDrafted.Value;
                    bool success        = equipSpecificWeaponTypeFromInventory(pawn, requiredWeapon, MiscUtils.shouldDrop(drop), false);
                    if (success)
                    {
                        return;
                    }
                }
                else
                {
                    return;
                }
            }
            if (pawnMemory.ForcedUnarmed)
            {
                if (pawn.equipment.Primary != null)
                {
                    bool success = equipSpecificWeapon(pawn, null, MiscUtils.shouldDrop(drop), false);
                    if (success)
                    {
                        return;
                    }
                }
                else
                {
                    return;
                }
            }
            if (pawnMemory.ForcedWeapon != null)
            {
                if (pawn.equipment.Primary == null || pawn.equipment.Primary.toThingStuffPair() != pawnMemory.ForcedWeapon.Value)
                {
                    var  requiredWeapon = pawnMemory.ForcedWeapon.Value;
                    bool success        = equipSpecificWeaponTypeFromInventory(pawn, requiredWeapon, MiscUtils.shouldDrop(drop), false);
                    if (success)
                    {
                        return;
                    }
                }
                else
                {
                    return;
                }
            }

            if (mode == GoldfishModule.PrimaryWeaponMode.Ranged ||
                ((mode == GoldfishModule.PrimaryWeaponMode.BySkill) && (pawn.getSkillWeaponPreference() == GoldfishModule.PrimaryWeaponMode.Ranged)))
            {
                if (pawnMemory.DefaultRangedWeapon != null && pawn.hasWeaponSomewhere(pawnMemory.DefaultRangedWeapon.Value))
                {
                    if (pawn.equipment.Primary == null || pawn.equipment.Primary.toThingStuffPair() != pawnMemory.DefaultRangedWeapon.Value)
                    {
                        var  requiredWeapon = pawnMemory.DefaultRangedWeapon.Value;
                        bool success        = equipSpecificWeaponTypeFromInventory(pawn, requiredWeapon, MiscUtils.shouldDrop(drop), false);
                        if (success)
                        {
                            return;
                        }
                    }
                    else
                    {
                        return;
                    }
                }

                else
                {
                    ThingWithComps result;
                    (ThingWithComps weapon, float dps, float averageSpeed)bestWeapon = GettersFilters.findBestRangedWeapon(pawn, null, pawn.IsColonistPlayerControlled);
                    if (bestWeapon.weapon != null)
                    {
                        if (pawn.equipment.Primary != bestWeapon.weapon)
                        {
                            bool success = equipSpecificWeaponFromInventory(pawn, bestWeapon.weapon, MiscUtils.shouldDrop(drop), false);
                            if (success)
                            {
                                return;
                            }
                        }
                        else
                        {
                            return;
                        }
                    }
                }
            }

            //all that's left is either melee preference or no ranged weapon found - so in either case, we want to equip a melee weapon.

            /*if (mode == GoldfishModule.PrimaryWeaponMode.Melee ||
             *  ((mode == GoldfishModule.PrimaryWeaponMode.BySkill) && (pawn.getSkillWeaponPreference() == GoldfishModule.PrimaryWeaponMode.Melee)))*/
            {
                //Log.Message("melee mode");
                //prefers melee
                if (pawnMemory.PreferredUnarmed)
                {
                    if (pawn.equipment.Primary != null)
                    {
                        bool success = equipSpecificWeapon(pawn, null, MiscUtils.shouldDrop(drop), false);
                        if (success)
                        {
                            return;
                        }
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    if (pawnMemory.PreferredMeleeWeapon != null && pawn.hasWeaponSomewhere(pawnMemory.PreferredMeleeWeapon.Value))
                    {
                        if (pawn.equipment.Primary == null || pawn.equipment.Primary.toThingStuffPair() != pawnMemory.PreferredMeleeWeapon.Value)
                        {
                            var  requiredWeapon = pawnMemory.PreferredMeleeWeapon.Value;
                            bool success        = equipSpecificWeaponTypeFromInventory(pawn, requiredWeapon, MiscUtils.shouldDrop(drop), false);
                            if (success)
                            {
                                return;
                            }
                        }
                        else
                        {
                            return;
                        }
                    }
                    else
                    {
                        ThingWithComps result;
                        bool           foundAlternative = GettersFilters.findBestMeleeWeapon(pawn, out result, includeRangedWithBash: false);
                        if (foundAlternative)
                        {
                            if (pawn.equipment.Primary != result)
                            {
                                bool success = equipSpecificWeaponFromInventory(pawn, result, MiscUtils.shouldDrop(drop), false);
                                if (success)
                                {
                                    return;
                                }
                            }
                            else
                            {
                                return;
                            }
                        }
                    }
                }
            }
            return;
        }