public int TryStartCarry(Thing item, int count, bool reserve = true)
        {
            if (this.pawn.Dead || this.pawn.Downed)
            {
                Log.Error(string.Concat(new object[]
                {
                    "Dead/downed pawn ",
                    this.pawn,
                    " tried to start carry ",
                    item.ToStringSafe <Thing>()
                }), false);
                return(0);
            }
            count = Mathf.Min(count, this.AvailableStackSpace(item.def));
            count = Mathf.Min(count, item.stackCount);
            int num = this.innerContainer.TryAdd(item.SplitOff(count), count, true);

            if (num > 0)
            {
                item.def.soundPickup.PlayOneShot(new TargetInfo(item.Position, this.pawn.Map, false));
                if (reserve)
                {
                    this.pawn.Reserve(this.CarriedThing, this.pawn.CurJob, 1, -1, null, true);
                }
            }
            return(num);
        }
        public override int TryAdd(Thing item, int count, bool canMergeWithExistingStacks = true)
        {
            if (count <= 0)
            {
                return(0);
            }
            if (item == null)
            {
                Log.Warning("Tried to add null item to ThingOwner.", false);
                return(0);
            }
            if (base.Contains(item))
            {
                Log.Warning("Tried to add " + item + " to ThingOwner but this item is already here.", false);
                return(0);
            }
            if (item.holdingOwner != null)
            {
                Log.Warning(string.Concat(new object[]
                {
                    "Tried to add ",
                    count,
                    " of ",
                    item.ToStringSafe <Thing>(),
                    " to ThingOwner but this thing is already in another container. owner=",
                    this.owner.ToStringSafe <IThingHolder>(),
                    ", current container owner=",
                    item.holdingOwner.Owner.ToStringSafe <IThingHolder>(),
                    ". Use TryAddOrTransfer, TryTransferToContainer, or remove the item before adding it."
                }), false);
                return(0);
            }
            if (!base.CanAcceptAnyOf(item, canMergeWithExistingStacks))
            {
                return(0);
            }
            int   stackCount = item.stackCount;
            int   num        = Mathf.Min(stackCount, count);
            Thing thing      = item.SplitOff(num);

            if (this.TryAdd((T)((object)thing), canMergeWithExistingStacks))
            {
                return(num);
            }
            if (thing != item)
            {
                int result = stackCount - item.stackCount - thing.stackCount;
                item.TryAbsorbStack(thing, false);
                return(result);
            }
            return(stackCount - item.stackCount);
        }
Exemple #3
0
        public override int TryAdd(Thing item, int count, bool canMergeWithExistingStacks = true)
        {
            if (count <= 0)
            {
                return(0);
            }
            if (item == null)
            {
                Log.Warning("Tried to add null item to ThingOwner.");
                return(0);
            }
            if (Contains(item))
            {
                Log.Warning("Tried to add " + item + " to ThingOwner but this item is already here.");
                return(0);
            }
            if (item.holdingOwner != null)
            {
                Log.Warning("Tried to add " + count + " of " + item.ToStringSafe() + " to ThingOwner but this thing is already in another container. owner=" + owner.ToStringSafe() + ", current container owner=" + item.holdingOwner.Owner.ToStringSafe() + ". Use TryAddOrTransfer, TryTransferToContainer, or remove the item before adding it.");
                return(0);
            }
            if (!CanAcceptAnyOf(item, canMergeWithExistingStacks))
            {
                return(0);
            }
            int   stackCount = item.stackCount;
            int   num        = Mathf.Min(stackCount, count);
            Thing thing      = item.SplitOff(num);

            if (!TryAdd((T)thing, canMergeWithExistingStacks))
            {
                if (thing != item)
                {
                    int result = stackCount - item.stackCount - thing.stackCount;
                    item.TryAbsorbStack(thing, respectStackLimit: false);
                    return(result);
                }
                return(stackCount - item.stackCount);
            }
            return(num);
        }
Exemple #4
0
        public int TryStartCarry(Thing item, int count, bool reserve = true)
        {
            if (pawn.Dead || pawn.Downed)
            {
                Log.Error("Dead/downed pawn " + pawn + " tried to start carry " + item.ToStringSafe());
                return(0);
            }
            count = Mathf.Min(count, AvailableStackSpace(item.def));
            count = Mathf.Min(count, item.stackCount);
            int num = innerContainer.TryAdd(item.SplitOff(count), count);

            if (num > 0)
            {
                item.def.soundPickup.PlayOneShot(new TargetInfo(item.Position, pawn.Map));
                if (reserve)
                {
                    pawn.Reserve(CarriedThing, pawn.CurJob);
                }
            }
            return(num);
        }
Exemple #5
0
        public static Thing Spawn(Thing newThing, IntVec3 loc, Map map, Rot4 rot, WipeMode wipeMode = WipeMode.Vanish, bool respawningAfterLoad = false)
        {
            if (map == null)
            {
                Log.Error("Tried to spawn " + newThing.ToStringSafe <Thing>() + " in a null map.", false);
                return(null);
            }
            if (!loc.InBounds(map))
            {
                Log.Error(string.Concat(new object[]
                {
                    "Tried to spawn ",
                    newThing.ToStringSafe <Thing>(),
                    " out of bounds at ",
                    loc,
                    "."
                }), false);
                return(null);
            }
            if (newThing.def.randomizeRotationOnSpawn)
            {
                rot = Rot4.Random;
            }
            CellRect occupiedRect = GenAdj.OccupiedRect(loc, rot, newThing.def.Size);

            if (!occupiedRect.InBounds(map))
            {
                Log.Error(string.Concat(new object[]
                {
                    "Tried to spawn ",
                    newThing.ToStringSafe <Thing>(),
                    " out of bounds at ",
                    loc,
                    " (out of bounds because size is ",
                    newThing.def.Size,
                    ")."
                }), false);
                return(null);
            }
            if (newThing.Spawned)
            {
                Log.Error("Tried to spawn " + newThing + " but it's already spawned.", false);
                return(newThing);
            }
            if (wipeMode == WipeMode.Vanish)
            {
                GenSpawn.WipeExistingThings(loc, rot, newThing.def, map, DestroyMode.Vanish);
            }
            else if (wipeMode == WipeMode.FullRefund)
            {
                GenSpawn.WipeAndRefundExistingThings(loc, rot, newThing.def, map);
            }
            if (newThing.def.category == ThingCategory.Item)
            {
                foreach (IntVec3 current in occupiedRect)
                {
                    foreach (Thing current2 in current.GetThingList(map).ToList <Thing>())
                    {
                        if (current2 != newThing)
                        {
                            if (current2.def.category == ThingCategory.Item)
                            {
                                current2.DeSpawn(DestroyMode.Vanish);
                                if (!GenPlace.TryPlaceThing(current2, current, map, ThingPlaceMode.Near, null, (IntVec3 x) => !occupiedRect.Contains(x)))
                                {
                                    current2.Destroy(DestroyMode.Vanish);
                                }
                            }
                        }
                    }
                }
            }
            newThing.Rotation = rot;
            newThing.Position = loc;
            if (newThing.holdingOwner != null)
            {
                newThing.holdingOwner.Remove(newThing);
            }
            newThing.SpawnSetup(map, respawningAfterLoad);
            if (newThing.Spawned && newThing.stackCount == 0)
            {
                Log.Error("Spawned thing with 0 stackCount: " + newThing, false);
                newThing.Destroy(DestroyMode.Vanish);
                return(null);
            }
            if (newThing.def.passability == Traversability.Impassable)
            {
                foreach (IntVec3 current3 in occupiedRect)
                {
                    foreach (Thing current4 in current3.GetThingList(map).ToList <Thing>())
                    {
                        if (current4 != newThing)
                        {
                            Pawn pawn = current4 as Pawn;
                            if (pawn != null)
                            {
                                pawn.pather.TryRecoverFromUnwalkablePosition(false);
                            }
                        }
                    }
                }
            }
            return(newThing);
        }
        public override bool TryAdd(Thing item, bool canMergeWithExistingStacks = true)
        {
            if (item == null)
            {
                Log.Warning("Tried to add null item to ThingOwner.", false);
                return(false);
            }
            T t = item as T;

            if (t == null)
            {
                return(false);
            }
            if (base.Contains(item))
            {
                Log.Warning("Tried to add " + item.ToStringSafe <Thing>() + " to ThingOwner but this item is already here.", false);
                return(false);
            }
            if (item.holdingOwner != null)
            {
                Log.Warning(string.Concat(new string[]
                {
                    "Tried to add ",
                    item.ToStringSafe <Thing>(),
                    " to ThingOwner but this thing is already in another container. owner=",
                    this.owner.ToStringSafe <IThingHolder>(),
                    ", current container owner=",
                    item.holdingOwner.Owner.ToStringSafe <IThingHolder>(),
                    ". Use TryAddOrTransfer, TryTransferToContainer, or remove the item before adding it."
                }), false);
                return(false);
            }
            if (!base.CanAcceptAnyOf(item, canMergeWithExistingStacks))
            {
                return(false);
            }
            if (canMergeWithExistingStacks)
            {
                for (int i = 0; i < this.innerList.Count; i++)
                {
                    T t2 = this.innerList[i];
                    if (t2.CanStackWith(item))
                    {
                        int num = Mathf.Min(item.stackCount, t2.def.stackLimit - t2.stackCount);
                        if (num > 0)
                        {
                            Thing other      = item.SplitOff(num);
                            int   stackCount = t2.stackCount;
                            t2.TryAbsorbStack(other, true);
                            if (t2.stackCount > stackCount)
                            {
                                base.NotifyAddedAndMergedWith(t2, t2.stackCount - stackCount);
                            }
                            if (item.Destroyed || item.stackCount == 0)
                            {
                                return(true);
                            }
                        }
                    }
                }
            }
            if (this.Count >= this.maxStacks)
            {
                return(false);
            }
            item.holdingOwner = this;
            this.innerList.Add(t);
            base.NotifyAdded(t);
            return(true);
        }
Exemple #7
0
        public static Thing Spawn(Thing newThing, IntVec3 loc, Map map, Rot4 rot, WipeMode wipeMode = WipeMode.Vanish, bool respawningAfterLoad = false)
        {
            if (map == null)
            {
                Log.Error("Tried to spawn " + newThing.ToStringSafe() + " in a null map.");
                return(null);
            }
            if (!loc.InBounds(map))
            {
                Log.Error("Tried to spawn " + newThing.ToStringSafe() + " out of bounds at " + loc + ".");
                return(null);
            }
            if (newThing.def.randomizeRotationOnSpawn)
            {
                rot = Rot4.Random;
            }
            CellRect occupiedRect = GenAdj.OccupiedRect(loc, rot, newThing.def.Size);

            if (!occupiedRect.InBounds(map))
            {
                Log.Error("Tried to spawn " + newThing.ToStringSafe() + " out of bounds at " + loc + " (out of bounds because size is " + newThing.def.Size + ").");
                return(null);
            }
            if (newThing.Spawned)
            {
                Log.Error("Tried to spawn " + newThing + " but it's already spawned.");
                return(newThing);
            }
            switch (wipeMode)
            {
            case WipeMode.Vanish:
                WipeExistingThings(loc, rot, newThing.def, map, DestroyMode.Vanish);
                break;

            case WipeMode.FullRefund:
                WipeAndRefundExistingThings(loc, rot, newThing.def, map);
                break;
            }
            if (newThing.def.category == ThingCategory.Item)
            {
                foreach (IntVec3 item in occupiedRect)
                {
                    foreach (Thing item2 in item.GetThingList(map).ToList())
                    {
                        if (item2 != newThing && item2.def.category == ThingCategory.Item)
                        {
                            item2.DeSpawn();
                            if (!GenPlace.TryPlaceThing(item2, item, map, ThingPlaceMode.Near, null, (IntVec3 x) => !occupiedRect.Contains(x)))
                            {
                                item2.Destroy();
                            }
                        }
                    }
                }
            }
            newThing.Rotation = rot;
            newThing.Position = loc;
            if (newThing.holdingOwner != null)
            {
                newThing.holdingOwner.Remove(newThing);
            }
            newThing.SpawnSetup(map, respawningAfterLoad);
            if (newThing.Spawned && newThing.stackCount == 0)
            {
                Log.Error("Spawned thing with 0 stackCount: " + newThing);
                newThing.Destroy();
                return(null);
            }
            if (newThing.def.passability == Traversability.Impassable)
            {
                foreach (IntVec3 item3 in occupiedRect)
                {
                    foreach (Thing item4 in item3.GetThingList(map).ToList())
                    {
                        if (item4 != newThing)
                        {
                            (item4 as Pawn)?.pather.TryRecoverFromUnwalkablePosition(error: false);
                        }
                    }
                }
                return(newThing);
            }
            return(newThing);
        }
Exemple #8
0
        public override bool TryAdd(Thing item, bool canMergeWithExistingStacks = true)
        {
            if (item == null)
            {
                Log.Warning("Tried to add null item to ThingOwner.");
                return(false);
            }
            T val = item as T;

            if (val == null)
            {
                return(false);
            }
            if (Contains(item))
            {
                Log.Warning("Tried to add " + item.ToStringSafe() + " to ThingOwner but this item is already here.");
                return(false);
            }
            if (item.holdingOwner != null)
            {
                Log.Warning("Tried to add " + item.ToStringSafe() + " to ThingOwner but this thing is already in another container. owner=" + owner.ToStringSafe() + ", current container owner=" + item.holdingOwner.Owner.ToStringSafe() + ". Use TryAddOrTransfer, TryTransferToContainer, or remove the item before adding it.");
                return(false);
            }
            if (!CanAcceptAnyOf(item, canMergeWithExistingStacks))
            {
                return(false);
            }
            if (canMergeWithExistingStacks)
            {
                for (int i = 0; i < innerList.Count; i++)
                {
                    T val2 = innerList[i];
                    if (val2.CanStackWith(item))
                    {
                        int num = Mathf.Min(item.stackCount, val2.def.stackLimit - val2.stackCount);
                        if (num > 0)
                        {
                            Thing other      = item.SplitOff(num);
                            int   stackCount = val2.stackCount;
                            val2.TryAbsorbStack(other, respectStackLimit: true);
                            if (val2.stackCount > stackCount)
                            {
                                NotifyAddedAndMergedWith(val2, val2.stackCount - stackCount);
                            }
                            if (item.Destroyed || item.stackCount == 0)
                            {
                                return(true);
                            }
                        }
                    }
                }
            }
            if (Count >= maxStacks)
            {
                return(false);
            }
            item.holdingOwner = this;
            innerList.Add(val);
            NotifyAdded(val);
            return(true);
        }