Пример #1
0
        public static bool IsBestSidearm(Pawn pawn, Thing weapon)
        {
            if (!weapon.def.IsWeapon)
            {
                return(false);
            }
            ThingDef weaponDef = weapon.def;

            //Go ahead and drop if same type is already equipped
            if (pawn.equipment?.Primary?.def == weaponDef)
            {
                return(false);
            }

            GoldfishModule pawnMemory = GoldfishModule.GetGoldfishForPawn(pawn);

            if (pawnMemory != null)
            {
                foreach (string wepName in pawnMemory.weapons)
                {
                    if (weaponDef.defName == wepName)
                    {
                        List <Thing> matchingWeapons = pawn.inventory?.innerContainer.Where(t => t.def == weaponDef).ToList() ?? new List <Thing> ();

                        Thing bestWeapon = matchingWeapons.MaxBy(t => t.GetStatValue(StatDefOf.MeleeWeapon_AverageDPS, false));
                        //Ranged damage scales with melee damage so this works for all weapons

                        return(bestWeapon == weapon);
                    }
                }
            }
            return(false);
        }
Пример #2
0
        public static bool tryCQCWeaponSwapToMelee(Pawn pawn, Pawn target, DroppingModeEnum drop)
        {
            GoldfishModule pawnMemory = GoldfishModule.GetGoldfishForPawn(pawn);

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

            if (!pawn.RaceProps.Humanlike)
            {
                return(false);
            }

            if (pawn.equipment.Primary != null)
            {
                if (pawn.equipment.Primary.def.destroyOnDrop)
                {
                    return(false);
                }
            }

            if (pawnMemory.IsCurrentWeaponForced(false))
            {
                return(false);
            }

            var current = pawn.equipment.Primary;

            equipBestWeaponFromInventoryByPreference(pawn, drop, GoldfishModule.PrimaryWeaponMode.Melee, target: target);
            return(current != pawn.equipment.Primary);
        }
 internal static void forgetSidearmMemory(Pawn pawn, ThingDef interactedWeaponMemory)
 { 
     GoldfishModule pawnMemory = GoldfishModule.GetGoldfishForPawn(pawn);
     if (pawnMemory == null)
         return;
     pawnMemory.ForgetSidearm(interactedWeaponMemory);
 }
Пример #4
0
        public static bool equipBestWeaponFromInventoryByStatModifiers(Pawn pawn, List <StatDef> stats)
        {
            //Log.Message("looking for a stat booster for stats " + String.Join(",", stats.Select(s => s.label))); ;
            GoldfishModule pawnMemory = GoldfishModule.GetGoldfishForPawn(pawn);

            if (pawn == null || pawn.Dead || pawnMemory == null || pawn.equipment == null || pawn.inventory == null || stats == null || stats.Count == 0 || pawn.Drafted)
            {
                return(false);
            }

            ThingWithComps bestBooster = pawn.getCarriedWeapons(includeTools: true).Where(t =>
            {
                _ = t.toThingDefStuffDefPair().getBestStatBoost(stats, out bool found); return(found);
            }).OrderBy(t =>
            {
                return(t.toThingDefStuffDefPair().getBestStatBoost(stats, out _));
            }).FirstOrDefault();

            if (bestBooster == default(ThingWithComps))
            {
                return(false);
            }

            if (bestBooster == pawn.equipment.Primary)
            {
                return(true);
            }

            bool success = equipSpecificWeaponFromInventory(pawn, bestBooster, false, false);

            return(success);
        }
Пример #5
0
        public static void _ctor(Toil __instance)
        {
            if (SimpleSidearms.ToolAutoSwitch == true)
            {
                Toil toil = __instance;
                toil.AddPreInitAction(delegate
                {
                    if (toil.activeSkill != null && toil.activeSkill() != null && toil.GetActor() != null)
                    {
                        //Log.Message("Pawn " + toil.GetActor().Label + " initializing toil that uses skill " + toil.activeSkill().label);
                        bool usingAppropriateTool = WeaponAssingment.equipBestWeaponFromInventoryByStatModifiers(toil.GetActor(), SkillStatMap.Map[toil.activeSkill()]);
                        if (usingAppropriateTool)
                        {
                            GoldfishModule pawnMemory = GoldfishModule.GetGoldfishForPawn(toil.GetActor());
                            if (pawnMemory != null)
                            {
                                pawnMemory.autotoolToil = toil;
                            }
                        }
                    }
                });
                toil.AddFinishAction(delegate
                {
                    GoldfishModule pawnMemory = GoldfishModule.GetGoldfishForPawn(toil.GetActor());
                    if (pawnMemory != null && pawnMemory.autotoolToil == toil)
                    {
                        pawnMemory.delayIdleSwitchTimestamp = Find.TickManager.TicksGame;
                    }

                    pawnMemory.autotoolToil = null;
                });
            }
        }
Пример #6
0
 private static void InterfaceDrop(ITab_Pawn_Gear __instance, Thing t)
 {
     if (t.def.IsMeleeWeapon || t.def.IsRangedWeapon)
     {
         ThingWithComps thingWithComps = t as ThingWithComps;
         ThingOwner     thingOwner     = thingWithComps.holdingOwner;
         IThingHolder   actualOwner    = thingOwner.Owner;
         if (actualOwner is Pawn_InventoryTracker)
         {
             GoldfishModule pawnMemory = GoldfishModule.GetGoldfishForPawn((actualOwner as Pawn_InventoryTracker).pawn);
             if (pawnMemory == null)
             {
                 return;
             }
             pawnMemory.DropSidearm(thingWithComps.def, true);
         }
         else if (actualOwner is Pawn_EquipmentTracker)
         {
             GoldfishModule pawnMemory = GoldfishModule.GetGoldfishForPawn((actualOwner as Pawn_EquipmentTracker).ParentHolder as Pawn);
             if (pawnMemory == null)
             {
                 return;
             }
             pawnMemory.DropPrimary(true);
         }
     }
 }
Пример #7
0
        public static bool trySwapToMoreAccurateRangedWeapon(Pawn pawn, LocalTargetInfo target, bool dropCurrent, bool skipDangerous = true)
        {
            GoldfishModule pawnMemory = GoldfishModule.GetGoldfishForPawn(pawn);

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

            if (pawnMemory.IsCurrentWeaponForced(false))
            {
                return(false);
            }

            (ThingWithComps weapon, float dps, float averageSpeed)bestWeapon = GettersFilters.findBestRangedWeapon(pawn, target, skipDangerous, true);

            if (bestWeapon.weapon == null)
            {
                return(false);
            }

            CellRect cellRect   = (!target.HasThing) ? CellRect.SingleCell(target.Cell) : target.Thing.OccupiedRect();
            float    range      = cellRect.ClosestDistSquaredTo(pawn.Position);
            float    currentDPS = StatCalculator.RangedDPS(pawn.equipment.Primary, SpeedSelectionBiasRanged.Value, bestWeapon.averageSpeed, range);

            if (bestWeapon.dps < currentDPS + ANTI_OSCILLATION_FACTOR)
            {
                return(false);
            }

            equipSpecificWeaponFromInventory(pawn, bestWeapon.weapon, dropCurrent, false);
            return(true);
        }
Пример #8
0
        public static void dropSidearm(Pawn pawn, Thing weapon, bool intentional)
        {
            if (weapon == null)
            {
                return;
            }
            if (pawn.IsQuestLodger() && intentional)
            {
                return;
            }

            ThingWithComps discarded1;
            Thing          discarded2;

            if (pawn.equipment.Primary == weapon)
            {
                pawn.equipment.TryDropEquipment(pawn.equipment.Primary, out discarded1, pawn.Position, false);
            }
            else
            {
                pawn.inventory.innerContainer.TryDrop(weapon, pawn.Position, pawn.Map, ThingPlaceMode.Near, out discarded2, null);
            }

            GoldfishModule pawnMemory = GoldfishModule.GetGoldfishForPawn(pawn);

            if (pawnMemory == null)
            {
                return;
            }
            pawnMemory.InformOfDroppedSidearm(weapon, intentional);
        }
        public static void SetPrimary(Pawn pawn, Thing toSwapTo, bool intentionalEquip, bool fromInventory, bool dropCurrent, bool intentionalDrop)
        {
            GoldfishModule pawnMemory = GoldfishModule.GetGoldfishForPawn(pawn);

            if (toSwapTo != null)
            {
                if (toSwapTo.stackCount > 1)
                {
                    toSwapTo = toSwapTo.SplitOff(1);
                }

                if (fromInventory)
                {
                    if (pawn.inventory.Contains(toSwapTo))
                        pawn.inventory.innerContainer.Remove(toSwapTo);
                }
            }

            if (dropCurrent)
            {
                if(pawnMemory != null)
                    pawnMemory.DropPrimary(intentionalDrop);

                if(pawn.equipment.Primary != null)
                {
                    ThingWithComps whocares;
                    pawn.equipment.TryDropEquipment(pawn.equipment.Primary, out whocares, pawn.Position, false);
                }
            }
            else
            {
                if (pawn.equipment.Primary != null)
                {
                    ThingWithComps oldPrimary = pawn.equipment.Primary;
                    pawn.equipment.Remove(pawn.equipment.Primary);
                    pawn.inventory.innerContainer.TryAdd(oldPrimary, true);
                }
            }

            if(toSwapTo != null)
            {
                Pawn_EquipmentTracker_AddEquipment_Postfix.sourcedBySimpleSidearms = true;
                pawn.equipment.AddEquipment(toSwapTo as ThingWithComps);
                Pawn_EquipmentTracker_AddEquipment_Postfix.sourcedBySimpleSidearms = false;

                if (toSwapTo.def.soundInteract != null)
                {
                    toSwapTo.def.soundInteract.PlayOneShot(new TargetInfo(pawn.Position, pawn.Map, false));
                }
                if (pawnMemory != null)
                    pawnMemory.SetPrimary(toSwapTo.def, intentionalEquip);
            }
            else
            {
                if (pawnMemory != null)
                    pawnMemory.SetPrimaryEmpty(intentionalEquip);
            }

        }
Пример #10
0
        public static bool equipSpecificWeapon(Pawn pawn, ThingWithComps weapon, bool dropCurrent, bool intentionalDrop)
        {
            GoldfishModule pawnMemory = GoldfishModule.GetGoldfishForPawn(pawn);

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

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

            //drop current on the ground
            if (dropCurrent && pawn.equipment.Primary != null)
            {
                pawnMemory.InformOfDroppedSidearm(weapon, intentionalDrop);
                ThingWithComps discarded;
                pawn.equipment.TryDropEquipment(pawn.equipment.Primary, out discarded, pawn.Position, false);
            }
            //or put it in inventory
            else if (pawn.equipment.Primary != null)
            {
                ThingWithComps oldPrimary = pawn.equipment.Primary;
                pawn.equipment.Remove(oldPrimary);
                pawn.inventory.innerContainer.TryAdd(oldPrimary, true);
            }

            if (weapon == null)
            {
            }
            else
            {
                if (weapon.stackCount > 1)
                {
                    weapon = weapon.SplitOff(1) as ThingWithComps; //if this cast doesnt work the world has gone mad
                }
                Pawn_EquipmentTracker_AddEquipment_Postfix.sourcedBySimpleSidearms = true;
                pawn.equipment.AddEquipment(weapon as ThingWithComps);
                Pawn_EquipmentTracker_AddEquipment_Postfix.sourcedBySimpleSidearms = 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);
        }
Пример #11
0
        private static void GetGizmos(Pawn __instance, ref IEnumerable <Gizmo> __result)
        {
            if (__instance.IsColonistPlayerControlled)
            {
                if (__instance.inventory != null)
                {
                    List <Thing> rangedWeapons;
                    List <Thing> meleeWeapons;
                    GettersFilters.getWeaponLists(out rangedWeapons, out meleeWeapons, __instance.inventory);

                    GoldfishModule pawnMemory = GoldfishModule.GetGoldfishForPawn(__instance);
                    //if (pawnMemory == null)
                    //    return;

                    if (rangedWeapons.Count > 0 || meleeWeapons.Count > 0 || (pawnMemory != null && pawnMemory.weapons.Count > 0))
                    {
                        List <ThingDef> rangedWeaponMemories = new List <ThingDef>();
                        List <ThingDef> meleeWeaponMemories  = new List <ThingDef>();

                        if (pawnMemory != null)
                        {
                            foreach (string weapon in pawnMemory.weapons)
                            {
                                ThingDef wepDef = DefDatabase <ThingDef> .GetNamedSilentFail(weapon);

                                if (wepDef == null)
                                {
                                    continue;
                                }

                                if (wepDef.IsMeleeWeapon)
                                {
                                    meleeWeaponMemories.Add(wepDef);
                                }
                                else if (wepDef.IsRangedWeapon)
                                {
                                    rangedWeaponMemories.Add(wepDef);
                                }
                            }
                        }

                        Gizmo_SidearmsList advanced = new Gizmo_SidearmsList(__instance, rangedWeapons, meleeWeapons, rangedWeaponMemories, meleeWeaponMemories);
                        advanced.defaultLabel = "DrawSidearm_gizmoTitle".Translate();
                        //draft.hotKey = KeyBindingDefOf.CommandColonistDraft;
                        advanced.defaultDesc = "DrawSidearm_gizmoTooltip".Translate();

                        List <Gizmo> results = new List <Gizmo>();
                        foreach (Gizmo gizmo in __result)
                        {
                            results.Add(gizmo);
                        }
                        results.Add(advanced);
                        __result = results;
                    }
                }
            }
        }
Пример #12
0
        private IEnumerable <Pawn> AffectedPawns()
        {
            HashSet <Pawn> pawns = new HashSet <Pawn>();

            if (PawnsFinder.AllMaps_FreeColonistsSpawned != null)
            {
                foreach (Pawn pawn in PawnsFinder.AllMaps_FreeColonistsSpawned)
                {
                    if (pawn.health != null && pawn.Dead)
                    {
                        Log.Error("Dead pawn in PawnsFinder.AllMaps_FreeColonists:" + pawn);
                    }
                    else
                    {
                        if (pawn.health != null && pawn.Downed)
                        {
                            continue;
                        }
                        if (pawn.drafter != null && pawn.Drafted)
                        {
                            continue;
                        }
                        if (pawn.CurJob != null && pawn.CurJob.def != null && (pawn.CurJob.def == SidearmsDefOf.EquipSecondary || pawn.CurJob.def == SidearmsDefOf.EquipSecondaryCombat))
                        {
                            continue;
                        }

                        GoldfishModule pawnMemory = GoldfishModule.GetGoldfishForPawn(pawn);
                        if (pawnMemory != null)
                        {
                            foreach (string wepName in pawnMemory.weapons)
                            {
                                if (pawnMemory.primary != null && wepName.Equals(pawnMemory.primary))
                                {
                                    continue;
                                }
                                ThingDef def = DefDatabase <ThingDef> .GetNamedSilentFail(wepName);

                                if (def == null)
                                {
                                    continue;
                                }

                                if (!pawn.hasWeaponSomewhere(wepName))
                                {
                                    if (pawns.Add(pawn))
                                    {
                                        yield return(pawn);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        internal static void dropSidearm(Pawn pawn, Thing interactedWeapon)
        {
            Thing whoCares;
            pawn.inventory.innerContainer.TryDrop(interactedWeapon, pawn.Position, pawn.Map, ThingPlaceMode.Near, out whoCares, null);

            GoldfishModule pawnMemory = GoldfishModule.GetGoldfishForPawn(pawn);
            if (pawnMemory == null)
                return;
            pawnMemory.ForgetSidearm(interactedWeapon.def);
        }
Пример #14
0
        public static void GetGizmos(Pawn __instance, ref IEnumerable <Gizmo> __result)
        {
            if ((__instance.IsColonistPlayerControlled && !__instance.story.DisabledWorkTagsBackstoryAndTraits.OverlapsWithOnAnyWorkType(WorkTags.Violent)) ||
                DebugSettings.godMode)
            {
                if (__instance.equipment != null && __instance.inventory != null)
                {
                    IEnumerable <ThingWithComps> carriedWeapons = __instance.getCarriedWeapons();

                    GoldfishModule pawnMemory = GoldfishModule.GetGoldfishForPawn(__instance);

                    //if (carriedWeapons.Count() > 0 || (pawnMemory != null && pawnMemory.RememberedWeapons.Count > 0))
                    {
                        List <ThingStuffPair> rangedWeaponMemories = new List <ThingStuffPair>();
                        List <ThingStuffPair> meleeWeaponMemories  = new List <ThingStuffPair>();

                        if (pawnMemory != null)
                        {
                            foreach (ThingStuffPair weapon in pawnMemory.RememberedWeapons)
                            {
                                if (weapon.thing.IsMeleeWeapon)
                                {
                                    meleeWeaponMemories.Add(weapon);
                                }
                                else if (weapon.thing.IsRangedWeapon)
                                {
                                    rangedWeaponMemories.Add(weapon);
                                }
                            }
                        }

                        Gizmo_SidearmsList advanced = new Gizmo_SidearmsList(__instance, carriedWeapons, pawnMemory.RememberedWeapons);

                        List <Gizmo> results = new List <Gizmo>();
                        foreach (Gizmo gizmo in __result)
                        {
                            results.Add(gizmo);
                        }
                        results.Add(advanced);
                        __result = results;
                    }
                }
            }
            if (DebugSettings.godMode)
            {
                Gizmo_Brainscope brainscope = new Gizmo_Brainscope(__instance);
                List <Gizmo>     results    = new List <Gizmo>();
                foreach (Gizmo gizmo in __result)
                {
                    results.Add(gizmo);
                }
                results.Add(brainscope);
                __result = results;
            }
        }
Пример #15
0
 public static void DraftedSetter(Pawn_DraftController __instance)
 {
     //Log.Message("undraft intercept: " + __instance.Drafted);
     if (!__instance.Drafted)
     {
         Pawn pawn = __instance.pawn;
         if (pawn != null && !pawn.Dead && pawn.IsColonist)
         {
             GoldfishModule.GetGoldfishForPawn(pawn).InformOfUndraft();
         }
     }
 }
Пример #16
0
        public static void Postfix(Pawn_InventoryTracker __instance, ref ThingCount __result)
        {
            if (__result == default(ThingCount) ||
                !__result.Thing.def.IsWeapon)
            {
                return;
            }
            else
            {
                Pawn           pawn       = __instance.pawn;
                GoldfishModule pawnMemory = GoldfishModule.GetGoldfishForPawn(pawn);
                if (
                    pawnMemory == null ||
                    !pawn.IsColonist ||
                    pawn.equipment == null ||
                    __instance.innerContainer == null
                    )
                {
                    return;
                }
                List <ThingDefStuffDefPair> desiredSidearms = pawnMemory.RememberedWeapons.ListFullCopy();

                if (pawn.equipment.Primary != null)
                {
                    if (desiredSidearms.Contains(pawn.equipment.Primary.toThingDefStuffDefPair()))
                    {
                        desiredSidearms.Remove(pawn.equipment.Primary.toThingDefStuffDefPair());
                    }
                }


                int inventoryOffset = 0;

                //TODO: this does not preserve best possible weapon, just whichever one is first in inventory. Maybe fix?
                while (inventoryOffset < __instance.innerContainer.Count)
                {
                    Thing candidate = __instance.innerContainer[inventoryOffset];
                    if (candidate.def.IsWeapon & desiredSidearms.Contains(candidate.toThingDefStuffDefPair()))
                    {
                        desiredSidearms.Remove(candidate.toThingDefStuffDefPair());
                    }
                    else
                    {
                        __result = new ThingCount(candidate, candidate.stackCount);
                        return;
                    }
                    inventoryOffset++;
                }
                __result = default(ThingCount);
                return;
            }
        }
Пример #17
0
        private static void AutoUndraftTick(AutoUndrafter __instance)
        {
            Pawn pawn = Traverse.Create(__instance).Field("pawn").GetValue <Pawn>();

            if (Find.TickManager.TicksGame % 100 == 0)
            {
                if (pawn.jobs.curJob != null && pawn.jobs.curJob.def == JobDefOf.WaitCombat && pawn.stances != null && pawn.stances.curStance is Stance_Mobile)
                {
                    //pawn.jobs.EndCurrentJob(JobCondition.Succeeded);

                    WeaponAssingment.reequipPrimaryIfNeededAndAvailable(pawn, GoldfishModule.GetGoldfishForPawn(pawn));
                }
            }
        }
Пример #18
0
 public static void CarryWeaponOpenly(ref PawnRenderer __instance, ref Pawn ___pawn, ref bool __result)
 {
     if (__result == true)
     {
         return;
     }
     else
     {
         if (GoldfishModule.GetGoldfishForPawn(___pawn).autotoolToil != null)
         {
             __result = true;
         }
     }
 }
Пример #19
0
 private static void AddEquipment(Pawn_EquipmentTracker __instance, ThingWithComps newEq)
 {
     if (!sourcedBySimpleSidearms)
     {
         Pawn pawn = Traverse.Create(__instance).Field("pawn").GetValue <Pawn>();
         if (pawn == null)
         {
             return;
         }
         GoldfishModule pawnMemory = GoldfishModule.GetGoldfishForPawn(pawn);
         if (pawnMemory == null)
         {
             return;
         }
         pawnMemory.PickupPrimary(newEq.def, true);
     }
 }
Пример #20
0
 public static void AddEquipment_Postfix(Pawn_EquipmentTracker __instance, ThingWithComps newEq)
 {
     if (!addEquipmentSourcedBySimpleSidearms)
     {
         Pawn pawn = __instance.pawn;
         if (pawn == null)
         {
             return;
         }
         GoldfishModule pawnMemory = GoldfishModule.GetGoldfishForPawn(pawn);
         if (pawnMemory == null)
         {
             return;
         }
         pawnMemory.InformOfAddedPrimary(newEq);
     }
 }
        public IEnumerable <Pawn> AffectedPawns()
        {
            HashSet <Pawn> pawns = new HashSet <Pawn>();

            if (PawnsFinder.AllMaps_FreeColonistsSpawned != null)
            {
                foreach (Pawn pawn in PawnsFinder.AllMaps_FreeColonistsSpawned)
                {
                    if (pawn.health != null && pawn.Dead)
                    {
                        Log.Error("Dead pawn in PawnsFinder.AllMaps_FreeColonists:" + pawn);
                    }
                    else
                    {
                        if (pawn.health != null && pawn.Downed)
                        {
                            continue;
                        }
                        if (pawn.drafter != null && pawn.Drafted)
                        {
                            continue;
                        }
                        if (pawn.CurJob != null && pawn.CurJob.def != null && (pawn.CurJob.def == SidearmsDefOf.EquipSecondary))
                        {
                            continue;
                        }

                        GoldfishModule pawnMemory = GoldfishModule.GetGoldfishForPawn(pawn);
                        if (pawnMemory != null)
                        {
                            foreach (ThingDefStuffDefPair weaponMemory in pawnMemory.RememberedWeapons)
                            {
                                if (!pawn.hasWeaponSomewhere(weaponMemory))
                                {
                                    if (pawns.Add(pawn))
                                    {
                                        yield return(pawn);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Пример #22
0
 internal static void reequipPrimaryIfNeededAndAvailable(Pawn pawn, GoldfishModule pawnMemory)
 {
     if (pawn == null || pawnMemory == null)
     {
         return;
     }
     if (pawn.equipment.Primary == null)
     {
         if (pawnMemory.NoPrimary)
         {
             return;
         }
         else
         {
             foreach (Thing thing in pawn.inventory.innerContainer)
             {
                 if (thing.def.defName.Equals(pawnMemory.primary))
                 {
                     SetPrimary(pawn, thing, true, true, false, false);
                     return;
                 }
             }
         }
     }
     else
     {
         if (pawnMemory.primary.Equals(pawn.equipment.Primary.def.defName))
         {
             return;
         }
         else
         {
             foreach (Thing thing in pawn.inventory.innerContainer)
             {
                 if (thing.def.defName.Equals(pawnMemory.primary))
                 {
                     SetPrimary(pawn, thing, true, true, false, false);
                     return;
                 }
             }
         }
     }
 }
Пример #23
0
        public static bool SetPrimary(Pawn pawn, Thing toSwapTo, bool intentionalEquip, bool fromInventory, bool dropCurrent, bool intentionalDrop)
        {
            GoldfishModule pawnMemory = GoldfishModule.GetGoldfishForPawn(pawn);

            if (toSwapTo != null)
            {
                if (pawn.equipment != null && pawn.equipment.Primary != null &&
                    toSwapTo.thingIDNumber == pawn.equipment.Primary.thingIDNumber)
                {
                    return(false);
                }

                if (toSwapTo.stackCount > 1)
                {
                    toSwapTo = toSwapTo.SplitOff(1);
                }

                if (fromInventory)
                {
                    if (pawn.inventory.Contains(toSwapTo))
                    {
                        pawn.inventory.innerContainer.Remove(toSwapTo);
                    }
                }
            }
            else if (pawn.equipment != null && pawn.equipment.Primary == null)
            {
                return(false);
            }

            if (dropCurrent)
            {
                if (pawnMemory != null)
                {
                    pawnMemory.DropPrimary(intentionalDrop);
                }

                if (pawn.equipment.Primary != null)
                {
                    ThingWithComps whocares;
                    pawn.equipment.TryDropEquipment(pawn.equipment.Primary, out whocares, pawn.Position, false);
                }
            }
            else
            {
                if (pawn.equipment.Primary != null)
                {
                    ThingWithComps oldPrimary = pawn.equipment.Primary;
                    pawn.equipment.Remove(pawn.equipment.Primary);
                    pawn.inventory.innerContainer.TryAdd(oldPrimary, true);
                }
            }

            if (toSwapTo != null)
            {
                Pawn_EquipmentTracker_AddEquipment_Postfix.sourcedBySimpleSidearms = true;
                pawn.equipment.AddEquipment(toSwapTo as ThingWithComps);
                Pawn_EquipmentTracker_AddEquipment_Postfix.sourcedBySimpleSidearms = false;

                if (toSwapTo.def.soundInteract != null)
                {
                    toSwapTo.def.soundInteract.PlayOneShot(new TargetInfo(pawn.Position, pawn.Map, false));
                }
                if (pawnMemory != null)
                {
                    pawnMemory.SetPrimary(toSwapTo.def, intentionalEquip);
                }
            }
            else
            {
                if (pawnMemory != null)
                {
                    pawnMemory.SetPrimaryEmpty(intentionalEquip);
                }
            }

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

            return(true);
        }
Пример #24
0
        public static void GetGizmos(Pawn __instance, ref IEnumerable <Gizmo> __result)
        {
            try
            {
                if ((__instance.IsColonistPlayerControlled) ||
                    DebugSettings.godMode)
                {
                    if (__instance.equipment != null && __instance.inventory != null)
                    {
                        IEnumerable <ThingWithComps> carriedWeapons = __instance.getCarriedWeapons(includeTools: true);

                        GoldfishModule pawnMemory = GoldfishModule.GetGoldfishForPawn(__instance);

                        //if (carriedWeapons.Count() > 0 || (pawnMemory != null && pawnMemory.RememberedWeapons.Count > 0))
                        {
                            List <ThingDefStuffDefPair> rangedWeaponMemories = new List <ThingDefStuffDefPair>();
                            List <ThingDefStuffDefPair> meleeWeaponMemories  = new List <ThingDefStuffDefPair>();

                            if (pawnMemory != null)
                            {
                                foreach (ThingDefStuffDefPair weapon in pawnMemory.RememberedWeapons)
                                {
                                    if (weapon.thing.IsMeleeWeapon)
                                    {
                                        meleeWeaponMemories.Add(weapon);
                                    }
                                    else if (weapon.thing.IsRangedWeapon)
                                    {
                                        rangedWeaponMemories.Add(weapon);
                                    }
                                }
                            }

                            Gizmo_SidearmsList advanced = new Gizmo_SidearmsList(__instance, carriedWeapons, pawnMemory.RememberedWeapons);

                            List <Gizmo> results = new List <Gizmo>();
                            foreach (Gizmo gizmo in __result)
                            {
                                results.Add(gizmo);
                            }
                            results.Add(advanced);
                            __result = results;
                        }
                    }
                }
                if (DebugSettings.godMode)
                {
                    Gizmo_Brainscope brainscope = new Gizmo_Brainscope(__instance);
                    List <Gizmo>     results    = new List <Gizmo>();
                    foreach (Gizmo gizmo in __result)
                    {
                        results.Add(gizmo);
                    }
                    results.Add(brainscope);
                    __result = results;
                }
            }
            catch (Exception e)
            {
                Log.Error("Exception during SimpleSidearms gizmo intercept. Cancelling intercept. Exception: " + e.ToString());
            }
        }
Пример #25
0
        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;
        }