Example #1
0
 public static bool IsTravelingInTransportPodWorldObject(Pawn pawn)
 {
     return(ThingOwnerUtility.AnyParentIs <TravelingTransportPods>(pawn));
 }
Example #2
0
 public void GetChildHolders(List <IThingHolder> outChildren)
 {
     ThingOwnerUtility.AppendThingHoldersFromThings(outChildren, this.GetDirectlyHeldThings());
 }
        private static void CacheAccessibleThings(int nearTile)
        {
            if (nearTile == PlayerItemAccessibilityUtility.cachedAccessibleThingsForTile && RealTime.frameCount == PlayerItemAccessibilityUtility.cachedAccessibleThingsForFrame)
            {
                return;
            }
            PlayerItemAccessibilityUtility.cachedAccessibleThings.Clear();
            PlayerItemAccessibilityUtility.cachedPossiblyAccessibleThings.Clear();
            WorldGrid  worldGrid = Find.WorldGrid;
            List <Map> maps      = Find.Maps;

            for (int i = 0; i < maps.Count; i++)
            {
                float num = worldGrid.ApproxDistanceInTiles(nearTile, maps[i].Tile);
                if (!(num > 5.0))
                {
                    ThingOwnerUtility.GetAllThingsRecursively(maps[i], PlayerItemAccessibilityUtility.tmpThings, false);
                    PlayerItemAccessibilityUtility.cachedAccessibleThings.AddRange(PlayerItemAccessibilityUtility.tmpThings);
                }
            }
            List <Caravan> caravans = Find.WorldObjects.Caravans;

            for (int j = 0; j < caravans.Count; j++)
            {
                if (caravans[j].IsPlayerControlled)
                {
                    float num2 = worldGrid.ApproxDistanceInTiles(nearTile, caravans[j].Tile);
                    if (!(num2 > 5.0))
                    {
                        ThingOwnerUtility.GetAllThingsRecursively(caravans[j], PlayerItemAccessibilityUtility.tmpThings, false);
                        PlayerItemAccessibilityUtility.cachedAccessibleThings.AddRange(PlayerItemAccessibilityUtility.tmpThings);
                    }
                }
            }
            for (int k = 0; k < PlayerItemAccessibilityUtility.cachedAccessibleThings.Count; k++)
            {
                Thing thing = PlayerItemAccessibilityUtility.cachedAccessibleThings[k];
                PlayerItemAccessibilityUtility.cachedPossiblyAccessibleThings.Add(new ThingCount(thing.def, thing.stackCount));
                if (GenLeaving.CanBuildingLeaveResources(thing, DestroyMode.Deconstruct))
                {
                    List <ThingCountClass> list = thing.CostListAdjusted();
                    for (int l = 0; l < list.Count; l++)
                    {
                        int num3 = Mathf.RoundToInt((float)list[l].count * thing.def.resourcesFractionWhenDeconstructed);
                        if (num3 > 0)
                        {
                            PlayerItemAccessibilityUtility.cachedPossiblyAccessibleThings.Add(new ThingCount(list[l].thingDef, num3));
                        }
                    }
                }
                Plant plant = thing as Plant;
                if (plant != null && (plant.HarvestableNow || plant.HarvestableSoon))
                {
                    int num4 = Mathf.RoundToInt(plant.def.plant.harvestYield * Find.Storyteller.difficulty.cropYieldFactor);
                    if (num4 > 0)
                    {
                        PlayerItemAccessibilityUtility.cachedPossiblyAccessibleThings.Add(new ThingCount(plant.def.plant.harvestedThingDef, num4));
                    }
                }
                if (!thing.def.butcherProducts.NullOrEmpty())
                {
                    for (int m = 0; m < thing.def.butcherProducts.Count; m++)
                    {
                        PlayerItemAccessibilityUtility.cachedPossiblyAccessibleThings.Add(thing.def.butcherProducts[m]);
                    }
                }
                Pawn pawn = thing as Pawn;
                if (pawn != null)
                {
                    if (pawn.RaceProps.meatDef != null)
                    {
                        int num5 = Mathf.RoundToInt(pawn.GetStatValue(StatDefOf.MeatAmount, true));
                        if (num5 > 0)
                        {
                            PlayerItemAccessibilityUtility.cachedPossiblyAccessibleThings.Add(new ThingCount(pawn.RaceProps.meatDef, num5));
                        }
                    }
                    if (pawn.RaceProps.leatherDef != null)
                    {
                        int num6 = GenMath.RoundRandom(pawn.GetStatValue(StatDefOf.LeatherAmount, true));
                        if (num6 > 0)
                        {
                            PlayerItemAccessibilityUtility.cachedPossiblyAccessibleThings.Add(new ThingCount(pawn.RaceProps.leatherDef, num6));
                        }
                    }
                    if (!pawn.RaceProps.Humanlike)
                    {
                        PawnKindLifeStage curKindLifeStage = pawn.ageTracker.CurKindLifeStage;
                        if (curKindLifeStage.butcherBodyPart != null)
                        {
                            PlayerItemAccessibilityUtility.cachedPossiblyAccessibleThings.Add(new ThingCount(curKindLifeStage.butcherBodyPart.thing, 1));
                        }
                    }
                }
                if (thing.def.smeltable)
                {
                    List <ThingCountClass> list2 = thing.CostListAdjusted();
                    for (int n = 0; n < list2.Count; n++)
                    {
                        if (!list2[n].thingDef.intricate)
                        {
                            int num7 = Mathf.RoundToInt((float)((float)list2[n].count * 0.25));
                            if (num7 > 0)
                            {
                                PlayerItemAccessibilityUtility.cachedPossiblyAccessibleThings.Add(new ThingCount(list2[n].thingDef, num7));
                            }
                        }
                    }
                }
                if (thing.def.smeltable && !thing.def.smeltProducts.NullOrEmpty())
                {
                    for (int num8 = 0; num8 < thing.def.smeltProducts.Count; num8++)
                    {
                        PlayerItemAccessibilityUtility.cachedPossiblyAccessibleThings.Add(thing.def.smeltProducts[num8]);
                    }
                }
            }
            int num9 = 0;

            for (int num10 = 0; num10 < PlayerItemAccessibilityUtility.cachedAccessibleThings.Count; num10++)
            {
                Pawn pawn2 = PlayerItemAccessibilityUtility.cachedAccessibleThings[num10] as Pawn;
                if (pawn2 != null && pawn2.IsFreeColonist && !pawn2.Dead && !pawn2.Downed && pawn2.workSettings.WorkIsActive(WorkTypeDefOf.Crafting))
                {
                    num9++;
                }
            }
            if (num9 > 0)
            {
                PlayerItemAccessibilityUtility.tmpWorkTables.Clear();
                for (int num11 = 0; num11 < PlayerItemAccessibilityUtility.cachedAccessibleThings.Count; num11++)
                {
                    Building_WorkTable building_WorkTable = PlayerItemAccessibilityUtility.cachedAccessibleThings[num11] as Building_WorkTable;
                    if (building_WorkTable != null && building_WorkTable.Spawned && PlayerItemAccessibilityUtility.tmpWorkTables.Add(building_WorkTable.def))
                    {
                        List <RecipeDef> allRecipes = building_WorkTable.def.AllRecipes;
                        for (int num12 = 0; num12 < allRecipes.Count; num12++)
                        {
                            if (allRecipes[num12].AvailableNow && allRecipes[num12].products.Any() && !allRecipes[num12].PotentiallyMissingIngredients(null, building_WorkTable.Map).Any())
                            {
                                ThingDef stuffDef = (!allRecipes[num12].products[0].thingDef.MadeFromStuff) ? null : GenStuff.DefaultStuffFor(allRecipes[num12].products[0].thingDef);
                                float    num13    = allRecipes[num12].WorkAmountTotal(stuffDef);
                                if (!(num13 <= 0.0))
                                {
                                    int num14 = Mathf.FloorToInt((float)((float)(num9 * 60000 * 5) * 0.090000003576278687 / num13));
                                    if (num14 > 0)
                                    {
                                        for (int num15 = 0; num15 < allRecipes[num12].products.Count; num15++)
                                        {
                                            PlayerItemAccessibilityUtility.cachedPossiblyAccessibleThings.Add(new ThingCount(allRecipes[num12].products[num15].thingDef, allRecipes[num12].products[num15].count * num14));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            PlayerItemAccessibilityUtility.cachedAccessibleThingsForTile  = nearTile;
            PlayerItemAccessibilityUtility.cachedAccessibleThingsForFrame = RealTime.frameCount;
        }
Example #4
0
        public static bool get_AnyPawnBlockingMapRemoval(MapPawns __instance, ref bool __result)
        {
            Faction     ofPlayer     = Faction.OfPlayer;
            List <Pawn> pawnsSpawned = pawnsSpawnedFieldRef(__instance);

            for (int i = 0; i < pawnsSpawned.Count; i++)
            {
                if (!pawnsSpawned[i].Downed && pawnsSpawned[i].IsColonist)
                {
                    __result = true;
                    return(false);
                }

                if (pawnsSpawned[i].relations != null && pawnsSpawned[i].relations.relativeInvolvedInRescueQuest != null)
                {
                    __result = true;
                    return(false);
                }

                if (pawnsSpawned[i].Faction == ofPlayer || pawnsSpawned[i].HostFaction == ofPlayer)
                {
                    Job curJob = pawnsSpawned[i].CurJob;
                    if (curJob != null && curJob.exitMapOnArrival)
                    {
                        __result = true;
                        return(false);
                    }
                }

                if (CaravanExitMapUtility.FindCaravanToJoinFor(pawnsSpawned[i]) != null && !pawnsSpawned[i].Downed)
                {
                    __result = true;
                    return(false);
                }
            }

            List <Thing> list = mapFieldRef(__instance).listerThings.ThingsInGroup(ThingRequestGroup.ThingHolder);

            for (int j = 0; j < list.Count; j++)
            {
                if (!(list[j] is IActiveDropPod) && !(list[j] is PawnFlyer) && list[j].TryGetComp <CompTransporter>() == null)
                {
                    continue;
                }

                IThingHolder thingHolder = list[j].TryGetComp <CompTransporter>();
                IThingHolder holder      = thingHolder ?? ((IThingHolder)list[j]);
                if (tmpThings == null)
                {
                    tmpThings = new List <Thing>();
                }
                else
                {
                    tmpThings.Clear();
                }
                ThingOwnerUtility.GetAllThingsRecursively(holder, tmpThings);
                for (int k = 0; k < tmpThings.Count; k++)
                {
                    Pawn pawn = tmpThings[k] as Pawn;
                    if (pawn != null && !pawn.Dead && !pawn.Downed && pawn.IsColonist)
                    {
                        tmpThings.Clear();
                        __result = true;
                        return(false);
                    }
                }
            }

            //tmpThings.Clear();
            __result = false;
            return(false);
        }
Example #5
0
 public static bool IsTravelingInTransportPodWorldObject(Pawn pawn)
 {
     return(pawn.IsWorldPawn() && ThingOwnerUtility.AnyParentIs <ActiveDropPodInfo>(pawn));
 }
 public new void GetChildHolders(List <IThingHolder> outChildren)
 {
     //Not used, included just in case something external calls it
     ThingOwnerUtility.AppendThingHoldersFromThings(outChildren, this.GetDirectlyHeldThings());
 }
Example #7
0
 public void GetChildHolders(List <IThingHolder> outChildren)
 {
     ThingOwnerUtility.AppendThingHoldersFromThings(outChildren, container);
 }
 // RimWorld.PawnUtility
 public static void IsTravelingInTransportPodWorldObject_PostFix(Pawn pawn, ref bool __result)
 {
     __result = __result || ThingOwnerUtility.AnyParentIs <ActiveDropPodInfo>(pawn);
 }
        public static void TransferWeaponsToCorrectInventory(List <Pawn> pawns)
        {
            if (!(pawns?.Count > 0))
            {
                return;
            }

            var missingWeaponsForPawn   = new Dictionary <Pawn, List <ThingDefStuffDefPair> >();
            var availableThings         = new List <ThingWithComps>();
            var availableBiocodedThings = new Dictionary <Pawn, List <ThingWithComps> >();

            foreach (var pawn in pawns)
            {
                var pawnThings = new List <ThingWithComps>();

                // add equipped weapon to list
                if (pawn.equipment?.Primary != null)
                {
                    pawnThings.Add(pawn.equipment.Primary);
                }

                // add all weapons in inventory to list
                var inventory = pawn.inventory?.innerContainer;
                if (inventory != null)
                {
                    // filter out all weapons in inventory
                    foreach (var thing in inventory)
                    {
                        if ((thing.def.IsMeleeWeapon || thing.def.IsRangedWeapon) &&
                            thing is ThingWithComps thingWithComps)
                        {
                            pawnThings.Add(thingWithComps);
                        }
                    }
                }

                // check if we are looking at a colonist with an inventory
                if (pawn?.IsColonist == true)
                {
                    var pawnWeapons = new List <ThingDefStuffDefPair>(CompSidearmMemory.GetMemoryCompForPawn(pawn)?.RememberedWeapons);

                    // first remove things biocoded to this pawn; no-one else can use them anyway
                    for (int i = 0; i < pawnThings.Count;)
                    {
                        var thing = pawnThings[i];

                        // check if thing is biocoded to this pawn
                        var biocode = thing.TryGetComp <CompBiocodable>();
                        if (biocode?.Biocoded == true)
                        {
                            if (biocode.CodedPawn == pawn)
                            {
                                // remove from remembered weapon list
                                var weaponMemory = thing.toThingDefStuffDefPair();
                                if (weaponMemory != null)
                                {
                                    pawnWeapons.Remove(weaponMemory);
                                }
                            }
                            else
                            {
                                // add biocoded thing to their own list
                                var codedPawn = biocode.CodedPawn;
                                if (!availableBiocodedThings.ContainsKey(codedPawn))
                                {
                                    availableBiocodedThings.Add(codedPawn, new List <ThingWithComps>());
                                }
                                availableBiocodedThings[codedPawn].Add(thing);
                            }

                            // remove from thing list
                            pawnThings.Remove(thing);
                            continue;
                        }
                        i++;
                    }

                    // then remove things that fit the remembered weapons found on the pawn
                    if (pawnWeapons.Count > 0)
                    {
                        for (int i = 0; i < pawnThings.Count;)
                        {
                            var thing = pawnThings[i];

                            // check if thing is remembered
                            var weaponMemory = thing.toThingDefStuffDefPair();
                            if (weaponMemory != null && pawnWeapons.Contains(weaponMemory))
                            {
                                // remove remembered weapon and the thing that fits it
                                pawnWeapons.Remove(weaponMemory);
                                pawnThings.Remove(thing);
                                continue;
                            }
                            i++;
                        }
                    }

                    // finally all weapons still in the pawn's remembered weapons list are missing; we will look for them on other pawns
                    if (pawnWeapons.Count > 0 && !missingWeaponsForPawn.ContainsKey(pawn))
                    {
                        missingWeaponsForPawn.Add(pawn, new List <ThingDefStuffDefPair>());
                    }
                    foreach (var weapon in pawnWeapons)
                    {
                        missingWeaponsForPawn[pawn].Add(weapon);
                    }
                }

                // all things in the pawn's thing list are not remembered weapons for this pawn; they can be used by other pawns
                foreach (var thing in pawnThings)
                {
                    availableThings.Add(thing);
                }
            }

            // sort by quality; this way we should give every pawn the best weapon, unless they got a biocoded one
            availableThings.SortByDescending((thing) =>
            {
                QualityUtility.TryGetQuality(thing, out QualityCategory qc);
                return((int)qc);
            });

            // transfer unassigned weapons to pawns missing sidearms
            foreach (var entry in missingWeaponsForPawn)
            {
                var pawn           = entry.Key;
                var missingWeapons = entry.Value;

                List <ThingWithComps> biocodedToThisPawn = null;
                if (availableBiocodedThings.ContainsKey(pawn))
                {
                    biocodedToThisPawn = availableBiocodedThings[pawn];
                }

                // iterate over every missing weapon
                for (int i = 0; i < missingWeapons.Count;)
                {
                    var weaponMemory = missingWeapons[i];

                    // check available biocoded weapons first
                    if (biocodedToThisPawn != null)
                    {
                        // iterate over biocoded items
                        for (int j = 0; biocodedToThisPawn.Count > j;)
                        {
                            // get ThingDefStuffDefPair for thing
                            var thing       = biocodedToThisPawn[j];
                            var thingMemory = thing.toThingDefStuffDefPair();

                            // check if thing fits weapon memory
                            if (weaponMemory.Equals(thingMemory))
                            {
                                //Log.Message($"Transferring '{thing}' from '{ThingOwnerUtility.GetAnyParent<Pawn>(thing)}' ({thing.ParentHolder}) to '{pawn}' [biocoded]");
                                // transfer weapon
                                if (thing.holdingOwner.TryTransferToContainer(thing, pawn.inventory.innerContainer, 1) == 1)
                                {
                                    // remove weapon from missing weapons list and thing from unassigned things list
                                    missingWeapons.Remove(weaponMemory);
                                    biocodedToThisPawn.Remove(thing);
                                    goto CONTINUE;
                                }

                                // if it gets here, tranferring the weapon failed; this should obviously not happen
                                Log.Error($"Failed to transfer '{thing}' from '{ThingOwnerUtility.GetAnyParent<Pawn>(thing)}' ({thing.ParentHolder}) to '{pawn}'! [biocoded]");
                            }
                            j++;
                        }
                    }

                    // for each missing weapon, we check all things to find it
                    for (int j = 0; j < availableThings.Count;)
                    {
                        // get ThingDefStuffDefPair for thing
                        var thing       = availableThings[j];
                        var thingMemory = thing.toThingDefStuffDefPair();

                        // all things in this list should have a ThingDefStuffDefPair, remove it if it does not
                        if (thingMemory == null)
                        {
                            Log.Warning($"'{thing}' had null ThingDefStuffDefPair; removing from unassigned things");
                            availableThings.Remove(thing);
                            continue;
                        }

                        // check if thing fits weapon memory
                        if (weaponMemory.Equals(thingMemory))
                        {
                            //Log.Message($"Transferring '{thing}' from '{ThingOwnerUtility.GetAnyParent<Pawn>(thing)}' ({thing.ParentHolder}) to '{pawn}' [standard]");
                            // transfer weapon
                            if (thing.holdingOwner.TryTransferToContainer(thing, pawn.inventory.innerContainer, 1) == 1)
                            {
                                // remove weapon from missing weapons list and thing from unassigned things list
                                missingWeapons.Remove(weaponMemory);
                                availableThings.Remove(thing);
                                goto CONTINUE;
                            }

                            // if it gets here, tranferring the weapon failed; this should obviously not happen
                            Log.Error($"Failed to transfer '{thing}' from '{ThingOwnerUtility.GetAnyParent<Pawn>(thing)}' ({thing.ParentHolder}) to '{pawn}'! [standard]");
                        }
                        j++;
                    }
                    i++;

                    // if missing weapon was found go here without increasing the index
                    CONTINUE :;
                }
            }
        }
Example #10
0
 void IThingHolder.GetChildHolders(List <IThingHolder> outChildren)
 {
     ThingOwnerUtility.AppendThingHoldersFromThings(outChildren, ((IThingHolder)this).GetDirectlyHeldThings());
 }
Example #11
0
        public static void ConvertToZombie(ThingWithComps thing, bool force = false)
        {
            var pawn = thing is Corpse corpse ? corpse.InnerPawn : thing as Pawn;

            if (pawn == null || pawn.RaceProps.Humanlike == false)
            {
                return;
            }

            // clear zombie hediffs to avoid triggering this convert method again
            //
            var pawnName = pawn.Name;

            if (force == false && (pawn.health == null || pawnName == emptyName))
            {
                return;
            }
            pawn.Name = emptyName;

            var pos = thing is IThingHolder?ThingOwnerUtility.GetRootPosition(thing as IThingHolder) : thing.Position;

            var map = thing is IThingHolder?ThingOwnerUtility.GetRootMap(thing as IThingHolder) : thing.Map;

            var tickManager = map.GetComponent <TickManager>();
            var rot         = pawn.Rotation;
            var wasInGround = thing.Map == null;

            if (map == null && thing.Destroyed == false)
            {
                thing.Destroy();
                return;
            }

            var it = ZombieGenerator.SpawnZombieIterativ(pos, map, ZombieGenerator.ZombieType.Normal, (Zombie zombie) =>
            {
                zombie.Name   = pawnName;
                zombie.gender = pawn.gender;

                if (zombie.ageTracker != null && pawn.ageTracker != null)
                {
                    zombie.ageTracker.AgeBiologicalTicks    = pawn.ageTracker.AgeBiologicalTicks;
                    zombie.ageTracker.AgeChronologicalTicks = pawn.ageTracker.AgeChronologicalTicks;
                    zombie.ageTracker.BirthAbsTicks         = pawn.ageTracker.BirthAbsTicks;
                }

                if (zombie.story != null && pawn.story != null)
                {
                    zombie.story.childhood = pawn.story.childhood;
                    zombie.story.adulthood = pawn.story.adulthood;
                    zombie.story.melanin   = pawn.story.melanin;
                    zombie.story.crownType = pawn.story.crownType;
                    zombie.story.hairDef   = pawn.story.hairDef;
                    zombie.story.bodyType  = pawn.story.bodyType;
                }

                // redo because we changed stuff
                if (ZombieSettings.Values.useCustomTextures)
                {
                    var it2 = ZombieGenerator.AssignNewGraphics(zombie);
                    while (it2.MoveNext())
                    {
                        ;
                    }
                }

                var zTweener = Traverse.Create(zombie.Drawer.tweener);
                var pTweener = Traverse.Create(pawn.Drawer.tweener);
                new[] { "tweenedPos", "lastDrawFrame", "lastTickSpringPos" }
                .Do(field => zTweener.Field(field).SetValue(pTweener.Field(field).GetValue()));

                zombie.Rotation = rot;
                if (wasInGround == false)
                {
                    zombie.rubbleCounter = Constants.RUBBLE_AMOUNT;
                    zombie.state         = ZombieState.Wandering;
                }
                zombie.wasMapPawnBefore = true;

                if (zombie.apparel != null && pawn.apparel != null)
                {
                    zombie.apparel.DestroyAll();
                    pawn.apparel.WornApparel.ForEach(apparel =>
                    {
                        if (pawn.apparel.TryDrop(apparel, out var newApparel))
                        {
                            zombie.apparel.Wear(newApparel);
                            newApparel.SetForbidden(false, false);
                            newApparel.HitPoints = 1;
                            var compQuality      = newApparel.TryGetComp <CompQuality>();
                            if (compQuality != null)
                            {
                                compQuality.SetQuality(QualityCategory.Awful, ArtGenerationContext.Colony);
                            }

                            zombie.apparel.Notify_ApparelAdded(newApparel);
                        }
                    });
                }

                if (thing is Corpse)
                {
                    if (thing.Destroyed == false)
                    {
                        thing.Destroy();
                    }
                }
                else
                {
                    var previousProgramState = Current.ProgramState;
                    Current.ProgramState     = ProgramState.Entry;
                    pawn.Kill(null);
                    Current.ProgramState = previousProgramState;
                    Find.ColonistBar.MarkColonistsDirty();

                    if (pawn.Corpse != null && pawn.Corpse.Destroyed == false)
                    {
                        pawn.Corpse.Destroy();
                    }
                }

                tickManager.allZombiesCached.Add(zombie);

                var label = "BecameAZombieLabel".Translate();
                var text  = "BecameAZombieDesc".SafeTranslate(new object[] { pawnName.ToStringShort });
                Find.LetterStack.ReceiveLetter(label, text, LetterDefOf.ThreatBig, zombie);
            });

            while (it.MoveNext())
            {
                ;
            }
        }