예제 #1
0
 public static void Postfix(ThingOwner __instance, bool __result)
 {
     if (__result)
     {
         CE_Utility.TryUpdateInventory(__instance);
     }
 }
        internal static bool TryTransferEquipmentToContainer(this Pawn_EquipmentTracker _this, ThingWithComps eq, ThingContainer container, out ThingWithComps resultingEq)
        {
            // Fetch private fields
            Pawn pawn = (Pawn)pawnFieldInfo.GetValue(_this);

            if (!_this.AllEquipment.Contains(eq))
            {
                Log.Warning(pawn.LabelCap + " tried to transfer equipment he didn't have: " + eq);
                resultingEq = null;
                return(false);
            }
            if (container.TryAdd(eq, true))
            {
                resultingEq = null;
            }
            else
            {
                resultingEq = eq;
            }
            _this.Remove(eq);
            CE_Utility.TryUpdateInventory(pawn);   // Equipment was stored away, update inventory

            // Cancel current job (use verb, etc.)
            if (pawn.Spawned)
            {
                pawn.stances.CancelBusyStanceSoft();
            }

            return(resultingEq == null);
        }
예제 #3
0
        internal static Thing Get(this ThingContainer _this, Thing thing, int count)
        {
            if (count > thing.stackCount)
            {
                Log.Error(string.Concat(new object[]
                {
                    "Tried to get ",
                    count,
                    " of ",
                    thing,
                    " while only having ",
                    thing.stackCount
                }));
                count = thing.stackCount;
            }
            if (count == thing.stackCount)
            {
                _this.Remove(thing);
                return(thing);
            }
            Thing thing2 = thing.SplitOff(count);

            thing2.holdingContainer = null;
            CE_Utility.TryUpdateInventory(_this.owner as Pawn_InventoryTracker);   // Item was taken from inventory, update
            return(thing2);
        }
        internal static void AddEquipment(this Pawn_EquipmentTracker _this, ThingWithComps newEq)
        {
            SlotGroupUtility.Notify_TakingThing(newEq);

            // Fetch private fields
            Pawn           pawn       = (Pawn)pawnFieldInfo.GetValue(_this);
            ThingWithComps primaryInt = (ThingWithComps)primaryIntFieldInfo.GetValue(_this);

            if ((from eq in _this.AllEquipment
                 where eq.def == newEq.def
                 select eq).Any <ThingWithComps>())
            {
                Log.Error(string.Concat(new object[]
                {
                    "Pawn ",
                    pawn.LabelCap,
                    " got equipment ",
                    newEq,
                    " while already having it."
                }));
                return;
            }
            if (newEq.def.equipmentType == EquipmentType.Primary && _this.Primary != null)
            {
                Log.Error(string.Concat(new object[]
                {
                    "Pawn ",
                    pawn.LabelCap,
                    " got primaryInt equipment ",
                    newEq,
                    " while already having primaryInt equipment ",
                    _this.Primary
                }));
                return;
            }
            if (newEq.def.equipmentType == EquipmentType.Primary)
            {
                primaryPropertyInfo.SetValue(_this, newEq, null); // Changed assignment to SetValue() since we're fetching a private variable through reflection
            }
            else
            {
                Log.Error("Tried to equip " + newEq + " but it's not Primary. Secondary weapons are not supported.");
            }
            foreach (Verb current in newEq.GetComp <CompEquippable>().AllVerbs)
            {
                current.caster = pawn;
                current.Notify_PickedUp();
            }
            CE_Utility.TryUpdateInventory(pawn);   // Added equipment, update inventory
        }
        internal static void Notify_PrimaryDestroyed(this Pawn_EquipmentTracker _this)
        {
            // Fetch private fields
            Pawn pawn = (Pawn)pawnFieldInfo.GetValue(_this);

            _this.Remove(_this.Primary);
            if (pawn.Spawned)
            {
                pawn.stances.CancelBusyStanceSoft();
            }

            CE_Utility.TryUpdateInventory(pawn);   // Equipment was destroyed, update inventory

            // Try switching to the next available weapon
            CompInventory inventory = pawn.TryGetComp <CompInventory>();

            if (inventory != null)
            {
                inventory.SwitchToNextViableWeapon(false);
            }
        }
예제 #6
0
        internal static void Remove(this ThingContainer _this, Thing item)
        {
            if (!_this.Contains(item))
            {
                return;
            }
            if (item.holdingContainer == _this)
            {
                item.holdingContainer = null;
            }
            List <Thing> innerList = (List <Thing>)innerListFieldInfo.GetValue(_this);    // Fetch innerList through reflection

            innerList.Remove(item);
            Pawn_InventoryTracker pawn_InventoryTracker = _this.owner as Pawn_InventoryTracker;

            if (pawn_InventoryTracker != null)
            {
                pawn_InventoryTracker.Notify_ItemRemoved(item);
            }
            CE_Utility.TryUpdateInventory(_this.owner as Pawn_InventoryTracker);           // Item was removed, update inventory
        }
        internal static bool TryDropEquipment(this Pawn_EquipmentTracker _this, ThingWithComps eq, out ThingWithComps resultingEq, IntVec3 pos, bool forbid = true)
        {
            // Fetch private fields
            Pawn pawn = (Pawn)pawnFieldInfo.GetValue(_this);

            if (!_this.AllEquipment.Contains(eq))
            {
                Log.Warning(pawn.LabelCap + " tried to drop equipment they didn't have: " + eq);
                resultingEq = null;
                return(false);
            }
            if (!pos.IsValid)
            {
                Log.Error(string.Concat(new object[]
                {
                    pawn,
                    " tried to drop ",
                    eq,
                    " at invalid cell."
                }));
                resultingEq = null;
                return(false);
            }
            _this.Remove(eq);
            Thing thing  = null;
            bool  result = GenThing.TryDropAndSetForbidden(eq, pos, pawn.MapHeld, ThingPlaceMode.Near, out thing, forbid);

            resultingEq = (thing as ThingWithComps);
            CE_Utility.TryUpdateInventory(pawn);       // Dropped equipment, update inventory

            // Cancel current job (use verb, etc.)
            if (pawn.Spawned)
            {
                pawn.stances.CancelBusyStanceSoft();
            }

            return(result);
        }
예제 #8
0
 internal static bool TryDrop(this ThingContainer _this, Thing thing, IntVec3 dropLoc, Map map, ThingPlaceMode mode, int count, out Thing resultingThing, Action <Thing, int> placedAction = null)
 {
     if (thing.stackCount < count)
     {
         Log.Error(string.Concat(new object[]
         {
             "Tried to drop ",
             count,
             " of ",
             thing,
             " while only having ",
             thing.stackCount
         }));
         count = thing.stackCount;
     }
     if (count == thing.stackCount)
     {
         if (GenDrop.TryDropSpawn(thing, dropLoc, map, mode, out resultingThing, placedAction))
         {
             _this.Remove(thing);
             CE_Utility.TryUpdateInventory(_this.owner as Pawn_InventoryTracker);   // Thing dropped, update inventory
             return(true);
         }
         return(false);
     }
     else
     {
         Thing thing2 = thing.SplitOff(count);
         if (GenDrop.TryDropSpawn(thing2, dropLoc, map, mode, out resultingThing, placedAction))
         {
             CE_Utility.TryUpdateInventory(_this.owner as Pawn_InventoryTracker);   // Thing dropped, update inventory
             return(true);
         }
         thing.stackCount += thing2.stackCount;
         return(false);
     }
 }
        internal static bool TryDrop(this Pawn_ApparelTracker _this, Apparel ap, out Apparel resultingAp, IntVec3 pos, bool forbid = true)
        {
            if (!_this.WornApparel.Contains(ap))
            {
                Log.Warning(_this.pawn.LabelCap + " tried to drop apparel he didn't have: " + ap.LabelCap);
                resultingAp = null;
                return(false);
            }
            if (_this.pawn.MapHeld == null)
            {
                Log.Warning(_this.pawn.LabelCap + " tried to drop apparel but his MapHeld is null.");
                resultingAp = null;
                return(false);
            }
            ap.Notify_Stripped(_this.pawn);
            _this.Remove(ap);
            Thing thing = null;

            bool result = GenThing.TryDropAndSetForbidden(ap, pos, _this.pawn.MapHeld, ThingPlaceMode.Near, out thing, forbid);

            resultingAp = (thing as Apparel);
            CE_Utility.TryUpdateInventory(_this.pawn);     // Apparel was dropped, update inventory
            return(result);
        }
예제 #10
0
        internal static bool TryAdd(this ThingContainer _this, Thing item, bool canMergeWithExistingStacks = true)
        {
            if (item == null)
            {
                Log.Warning("Tried to add null item to ThingContainer.");
                return(false);
            }
            if (_this.Contains(item))
            {
                Log.Warning("Tried to add " + item + " to ThingContainer but this item is already here.");
                return(false);
            }

            if (item.stackCount > _this.AvailableStackSpace)
            {
                return(_this.TryAdd(item, _this.AvailableStackSpace) > 0);
            }

            List <Thing> innerList = (List <Thing>)innerListFieldInfo.GetValue(_this);    // Fetch innerList through reflection

            SlotGroupUtility.Notify_TakingThing(item);
            if (canMergeWithExistingStacks && item.def.stackLimit > 1)
            {
                for (int i = 0; i < innerList.Count; i++)
                {
                    if (innerList[i].def == item.def)
                    {
                        int num = item.stackCount;
                        if (num > _this.AvailableStackSpace)
                        {
                            num = _this.AvailableStackSpace;
                        }
                        Thing other = item.SplitOff(num);
                        if (!innerList[i].TryAbsorbStack(other, false))
                        {
                            Log.Error("ThingContainer did TryAbsorbStack " + item + " but could not absorb stack.");
                        }
                    }
                    if (item.Destroyed)
                    {
                        //CE PART!!!
                        CE_Utility.TryUpdateInventory(_this.owner as Pawn_InventoryTracker);   // Item has been added, notify CompInventory
                        return(true);
                    }
                }
            }

            int maxStacks = (int)maxStacksFieldInfo.GetValue(_this);    // Fetch maxStacks through reflection

            if (innerList.Count >= maxStacks)
            {
                return(false);
            }
            if (item.Spawned)
            {
                item.DeSpawn();
            }
            if (item.HasAttachment(ThingDefOf.Fire))
            {
                item.GetAttachment(ThingDefOf.Fire).Destroy(DestroyMode.Vanish);
            }
            item.holdingContainer = _this;
            innerList.Add(item);

            CE_Utility.TryUpdateInventory(_this.owner as Pawn_InventoryTracker);   // Item has been added, notify CompInventory

            return(true);
        }
        internal static void Wear(this Pawn_ApparelTracker _this, Apparel newApparel, bool dropReplacedApparel = true)
        {
            SlotGroupUtility.Notify_TakingThing(newApparel);
            if (newApparel.Spawned)
            {
                newApparel.DeSpawn();
            }
            if (!ApparelUtility.HasPartsToWear(_this.pawn, newApparel.def))
            {
                Log.Warning(string.Concat(new object[]
                {
                    _this.pawn,
                    " tried to wear ",
                    newApparel,
                    " but he has no body parts required to wear it."
                }));
                return;
            }
            for (int i = _this.WornApparel.Count - 1; i >= 0; i--)
            {
                Apparel apparel = _this.WornApparel[i];
                if (!ApparelUtility.CanWearTogether(newApparel.def, apparel.def))
                {
                    bool forbid = _this.pawn.Faction.HostileTo(Faction.OfPlayer);
                    if (dropReplacedApparel)
                    {
                        Apparel apparel2;
                        if (!_this.TryDrop(apparel, out apparel2, _this.pawn.Position, forbid))
                        {
                            Log.Error(_this.pawn + " could not drop " + apparel);
                            return;
                        }
                    }
                    else
                    {
                        _this.Remove(apparel);
                    }
                }
            }
            if (newApparel.wearer != null)
            {
                Log.Warning(string.Concat(new object[]
                {
                    _this.pawn,
                    " is trying to wear ",
                    newApparel,
                    " but this apparel already has a wearer (",
                    newApparel.wearer,
                    "). This may or may not cause bugs."
                }));
            }
            _this.WornApparel.Add(newApparel);
            newApparel.wearer = _this.pawn;
            _this.SortWornApparelIntoDrawOrder();
            _this.ApparelChanged();

            //CE PART
            CE_Utility.TryUpdateInventory(_this.pawn);     // Apparel was added, update inventory
            MethodInfo methodInfo = typeof(Pawn_ApparelTracker).GetMethod("SortWornApparelIntoDrawOrder", BindingFlags.Instance | BindingFlags.NonPublic);

            methodInfo.Invoke(_this, new object[] { });

            LongEventHandler.ExecuteWhenFinished(new Action(_this.ApparelChanged));
        }
 internal static void Notify_WornApparelDestroyed(this Pawn_ApparelTracker _this, Apparel apparel)
 {
     _this.Remove(apparel);
     CE_Utility.TryUpdateInventory(_this.pawn);     // Apparel was destroyed, update inventory
 }