Example #1
0
        public static bool findBestMeleeWeapon(Pawn pawn, out ThingWithComps result, bool includeEquipped = true, bool includeRangedWithBash = true, Pawn target = null)
        {
            result = null;
            if (pawn == null || pawn.Dead || pawn.equipment == null || pawn.inventory == null)
            {
                return(false);
            }

            IEnumerable <ThingWithComps> options = pawn.getCarriedWeapons(includeEquipped).Where(t =>
            {
                return
                (t.def.IsMeleeWeapon ||
                 (includeRangedWithBash && t.def.IsWeapon && !t.def.tools.NullOrEmpty()));
            });

            if (!Settings.AllowBlockedWeaponUse)
            {
                options = options.Where(t => StatCalculator.canUseSidearmInstance(t, pawn, out _));
            }

            if (options.Count() < 1)
            {
                return(false);
            }

            float averageSpeed = AverageSpeedMelee(options, pawn);

            /*if (target != null)
             * {
             *  //handle DPS vs. armor?
             * }
             * else*/
            {
                float resultDPS = options.Max(t => StatCalculator.getMeleeDPSBiased(t, pawn, Settings.SpeedSelectionBiasMelee, averageSpeed));
                result = options.MaxBy(t => StatCalculator.getMeleeDPSBiased(t, pawn, Settings.SpeedSelectionBiasMelee, averageSpeed));

                //check if pawn is better when punching
                //if (pawn.GetStatValue(StatDefOf.MeleeDPS) > resultDPS)
                //    result = null;

                return(true);
            }
        }
Example #2
0
        public static (ThingWithComps weapon, float dps, float averageSpeed) findBestRangedWeapon(Pawn pawn, LocalTargetInfo?target = null, bool skipManualUse = true, bool skipDangerous = true, bool skipEMP = true, bool includeEquipped = true)
        {
            if (pawn == null || pawn.Dead || pawn.equipment == null || pawn.inventory == null)
            {
                return(null, -1, 0);
            }

            IEnumerable <ThingWithComps> options = pawn.getCarriedWeapons(includeEquipped).Where(t => t.def.IsRangedWeapon);

            if (!Settings.AllowBlockedWeaponUse)
            {
                options = options.Where(t => StatCalculator.canUseSidearmInstance(t, pawn, out _));
            }

            options = options.Where(t => !pawn.IsColonistPlayerControlled || !isManualUse(t));

            if (skipManualUse)
            {
                options = options.Where(t => (!isManualUse(t)));
            }
            if (skipDangerous)
            {
                options = options.Where(t => (!isDangerousWeapon(t)));
            }
            if (skipEMP)
            {
                options = options.Where(t => !isEMPWeapon(t));
            }

            if (options.Count() == 0)
            {
                return(null, -1, 0);
            }

            float averageSpeed = AverageSpeedRanged(options);

            if (target.HasValue)
            {
                CellRect cellRect       = (!target.Value.HasThing) ? CellRect.SingleCell(target.Value.Cell) : target.Value.Thing.OccupiedRect();
                float    targetDistance = cellRect.ClosestDistSquaredTo(pawn.Position);

                options = options.Where(t =>
                {
                    VerbProperties atkProps = (t.GetComp <CompEquippable>())?.PrimaryVerb?.verbProps;
                    if (atkProps == null)
                    {
                        return(false);
                    }
                    return(atkProps.range >= targetDistance);
                });

                if (options.Count() == 0)
                {
                    return(null, -1, 0);
                }

                //TODO: handle DPS vs. armor?
                (ThingWithComps thing, float dps, float averageSpeed)best = (null, -1, averageSpeed);
                foreach (ThingWithComps candidate in options)
                {
                    float dps = StatCalculator.RangedDPS(candidate, Settings.SpeedSelectionBiasRanged, averageSpeed, targetDistance);
                    if (dps > best.dps)
                    {
                        best = (candidate, dps, averageSpeed);
                    }
                }
                return(best);
            }
            else
            {
                (ThingWithComps thing, float dps, float averageSpeed)best = (null, -1, averageSpeed);
                foreach (ThingWithComps candidate in options)
                {
                    float dps = StatCalculator.RangedDPSAverage(candidate, Settings.SpeedSelectionBiasRanged, averageSpeed);
                    if (dps > best.dps)
                    {
                        best = (candidate, dps, averageSpeed);
                    }
                }
                return(best);
            }
        }
Example #3
0
        public static bool equipSpecificWeapon(Pawn pawn, ThingWithComps weapon, bool dropCurrent, bool intentionalDrop)
        {
            if (!pawn.IsValidSidearmsCarrier())
            {
                return(false);
            }

            CompSidearmMemory pawnMemory = CompSidearmMemory.GetMemoryCompForPawn(pawn);

            if (pawnMemory == null)
            {
                return(false);
            }

            if (weapon == pawn.equipment.Primary) //attepmpting to equip already-equipped weapon
            {
                Log.Warning("attepmpting to equip already-equipped weapon");
                return(false);
            }

            if (!Settings.AllowBlockedWeaponUse && !StatCalculator.canUseSidearmInstance(weapon, pawn, out string reason))
            {
                Log.Warning($"blocked equip of {weapon.Label} at equip-time because of: {reason}");
                return(false);
            }

            var currentPrimary = pawn.equipment.Primary;

            //drop current on the ground
            if (dropCurrent && pawn.equipment.Primary != null)
            {
                if (!intentionalDrop)
                {
                    DoFumbleMote(pawn);
                }
                pawnMemory.InformOfDroppedSidearm(weapon, intentionalDrop);
                Pawn_EquipmentTracker_TryDropEquipment.dropEquipmentSourcedBySimpleSidearms = true;
                pawn.equipment.TryDropEquipment(pawn.equipment.Primary, out ThingWithComps droppedItem, pawn.Position, false);
                Pawn_EquipmentTracker_TryDropEquipment.dropEquipmentSourcedBySimpleSidearms = false;
            }
            //or put it in inventory
            else if (pawn.equipment.Primary != null)
            {
                ThingWithComps oldPrimary       = pawn.equipment.Primary;
                bool           addedToInventory = pawn.inventory.innerContainer.TryAddOrTransfer(oldPrimary, true);
                //pawn.equipment.Remove(oldPrimary);
                //bool addedToInventory = pawn.inventory.innerContainer.TryAdd(oldPrimary, true);
                if (!addedToInventory)
                {
                    Log.Warning(String.Format("Failed to place primary equipment {0} (initially was {1}) into inventory when swapping to {2} on pawn {3} (colonist: {4}) (dropping: {5}, current drop mode: {6}). Aborting swap. Please report this!",
                                              pawn.equipment.Primary != null ? pawn.equipment.Primary.LabelCap : "NULL",
                                              currentPrimary != null ? currentPrimary.LabelCap : "NULL",
                                              weapon != null ? weapon.LabelCap : "NULL",
                                              pawn?.LabelCap,
                                              pawn?.IsColonist,
                                              dropCurrent,
                                              Settings.FumbleMode
                                              ));
                }
            }

            if (pawn.equipment.Primary != null)
            {
                Log.Warning(String.Format("Failed to remove current primary equipment {0} (initially was {1}) when swapping to {2} on pawn {3} (colonist: {4}) (dropping: {5}, current drop mode: {6}). Aborting swap. Please report this!",
                                          pawn.equipment.Primary != null ? pawn.equipment.Primary.LabelCap : "NULL",
                                          currentPrimary != null ? currentPrimary.LabelCap : "NULL",
                                          weapon != null ? weapon.LabelCap : "NULL",
                                          pawn?.LabelCap,
                                          pawn?.IsColonist,
                                          dropCurrent,
                                          Settings.FumbleMode
                                          ));
                return(false);
            }

            if (weapon == null)
            {
            }
            else
            {
                if (weapon.stackCount > 1)
                {
                    weapon = weapon.SplitOff(1) as ThingWithComps; //if this cast doesnt work the world has gone mad
                }
                if (weapon.holdingOwner != null)
                {
                    weapon.holdingOwner.Remove(weapon);
                }
                Pawn_EquipmentTracker_AddEquipment.addEquipmentSourcedBySimpleSidearms = true;
                pawn.equipment.AddEquipment(weapon as ThingWithComps);
                Pawn_EquipmentTracker_AddEquipment.addEquipmentSourcedBySimpleSidearms = false;

                if (weapon.def.soundInteract != null)
                {
                    weapon.def.soundInteract.PlayOneShot(new TargetInfo(pawn.Position, pawn.Map, false));
                }
            }

            //avoid hunting stackoverflowexception
            if (pawn.jobs != null && pawn.jobs.curJob != null && pawn.jobs.curJob.def == JobDefOf.Hunt)
            {
                pawn.jobs.EndCurrentJob(JobCondition.InterruptForced, true);
            }

            return(true);
        }