public static void Prefix(Thing t, Pawn carrier, Map map, ref StoragePriority currentPriority,
                           ref IntVec3 foundCell)
 {
     if (t.stackCount > Limits.CalculateStackLimit(t))
     {
         currentPriority = StoragePriority.Unstored;
     }
 }
 public static void RemoveUnusedSlotGroups(Map map, List <SlotGroup> activeSlotGroups)
 {
     if (heldItems.ContainsKey(map))
     {
         var dk = heldItems[map];
         dk.RemoveAll(x => !activeSlotGroups.Contains(x.Key) || !Limits.HasLimit(x.Key.Settings));
     }
 }
Beispiel #3
0
        /*
         #if DEBUG
         *      static bool Prepare() => HarmonyLibInstance.DEBUG = true;
         *      static bool Cleanup() => HarmonyLibInstance.DEBUG = false;
         #endif
         *      static IEnumerable<CodeInstruction> Transpiler(ILGenerator generator, IEnumerable<CodeInstruction> instructions)
         *      => new TranspilerFactory().Search("ret", 2).Insert("localvar int;ldarg.1;ldarg.2;call Limits::CalculateStackLimit(Verse.Map,Verse.IntVec3);stloc.s 0")
         *          .Replace("ldarg.2;ldfld Verse.Thing::def;ldfld Verse.ThingDef::stackLimit", "ldloc.s 0;ldloc.s 0;brtrue 0;pop;ldarg.2;ldfld Verse.Thing::def;ldfld Verse.ThingDef::stackLimit;label 0")
         *          .Replace("ldarg.2;ldfld Verse.Thing::def;ldfld Verse.ThingDef::stackLimit", "ldloc.s 0;ldloc.s 0;brtrue 1;pop;ldarg.2;ldfld Verse.Thing::def;ldfld Verse.ThingDef::stackLimit;label 1")
         *          .Transpiler(generator, instructions);
         */
        public static bool Prefix(IntVec3 c, Map map, Thing thing, IntVec3 center, ref byte __result)
        {
            Mod.Debug("GenPlace.PlaceSpotQualityAt begin");
            if (thing.stackCount < Limits.CalculateStackLimit(map, c))
            {
                return(true);
            }

            __result = 0;
            return(false);
        }
Beispiel #4
0
 public static void Postfix(StorageSettings __instance)
 {
     if (Scribe.mode == LoadSaveMode.Saving)
     {
         Scribe.saver.WriteElement("stacklimit", Limits.GetLimit(__instance).ToString());
     }
     else if (Scribe.mode == LoadSaveMode.LoadingVars)
     {
         Limits.SetLimit(__instance,
                         ScribeExtractor.ValueFromNode(Scribe.loader.curXmlParent["stacklimit"], -1));
     }
 }
        public static void Prefix(Thing t, Pawn carrier, Map map, ref StoragePriority currentPriority, Faction faction)
        {
            var slotGroup0 = t.GetSlotGroup();

            if (slotGroup0 != null && Limits.HasLimit(slotGroup0.Settings))
            {
                int stockpileMax = Limits.GetLimit(slotGroup0.Settings);

                if (slotGroup0.TotalPrecalculatedItemsStack(false) > stockpileMax)
                {
                    currentPriority = StoragePriority.Unstored;
                }
            }
        }
Beispiel #6
0
        public static void Postfix(ref List <IHaulDestination> __result)
        {
            int HeldItems(IHaulDestination x) => StoreUtility.GetSlotGroup(x.Position, x.Map).TotalPrecalculatedItemsStack();

            __result = __result.Where(x =>
            {
                if (!Limits.HasLimit(x.GetStoreSettings()))
                {
                    return(true);
                }

                int limit = Limits.GetLimit(x.GetStoreSettings());
                if (limit < 0)
                {
                    limit = int.MaxValue;
                }

                return(HeldItems(x) < limit);
            })
                       .OrderByDescending(x => x.GetStoreSettings().Priority)
                       .ThenByDescending(x =>
            {
                if (!Limits.HasLimit(x.GetStoreSettings()))
                {
                    return(0);
                }

                int limit = Limits.GetLimit(x.GetStoreSettings());
                if (limit < 0)
                {
                    limit = int.MaxValue;
                }

                return(limit - HeldItems(x));
            })

                       /*.ThenByDescending(x =>
                        * {
                        *      int limit = Limits.GetLimit(x.GetStoreSettings());
                        *      if (limit < 0)
                        *              return int.MaxValue;
                        *      else
                        *      {
                        *              int heldItems = HeldItems(x);
                        *              int spaceLeft = limit - heldItems;
                        *              return spaceLeft;
                        *      }
                        * })*/
                       .ToList();
        }
Beispiel #7
0
        public static void Postfix(ref List <SlotGroup> __result)
        {
            __result = __result.Where(x =>
            {
                if (!Limits.HasLimit(x.Settings))
                {
                    return(true);
                }

                int limit = Limits.GetLimit(x.Settings);
                if (limit < 0)
                {
                    limit = int.MaxValue;
                }

                return(x.TotalPrecalculatedItemsStack() < limit);
            })
                       .OrderByDescending(x => x.Settings.Priority)
                       .ThenByDescending(x =>
            {
                if (!Limits.HasLimit(x.Settings))
                {
                    return(0);
                }

                int limit = Limits.GetLimit(x.Settings);
                if (limit < 0)
                {
                    limit = int.MaxValue;
                }

                return(limit - x.TotalPrecalculatedItemsStack());
            })

                       /*.ThenByDescending(x =>
                        * {
                        *      int limit = Limits.GetLimit(x.Settings);
                        *      if (limit < 0)
                        *              return int.MaxValue;
                        *      else
                        *      {
                        *              int heldItems = x.TotalItemsStack();
                        *              int spaceLeft = limit - heldItems;
                        *              return spaceLeft;
                        *      }
                        * })*/
                       .ToList();
        }
Beispiel #8
0
        private static void Postfix(Thing thing, ref int __result)
        {
            if (!Limits.HasStackLimit(thing))
            {
                return;
            }

            var t = thing.stackCount - Limits.CalculateStackLimit(thing);

            if (t < 0)
            {
                t = 0;
            }

            __result = Mathf.Min(t, __result);
        }
        public static bool Prefix(Pawn pawn, Thing thing, ref Job __result)
        {
            StoragePriority currentPriority = StoreUtility.CurrentStoragePriorityOf(thing);

            if (StoreUtility.TryFindBestBetterStoreCellFor(thing, pawn, pawn.Map, currentPriority, pawn.Faction, out IntVec3 storeCell, true))
            {
                SlotGroup slotGroup = StoreUtility.GetSlotGroup(storeCell, thing.Map);
                if (Limits.HasLimit(slotGroup.Settings))
                {
                    __result = HaulAIUtility.HaulToStorageJob(pawn, thing);
                    return(false);
                }
            }

            return(true);
        }
Beispiel #10
0
        public static bool Prefix(ref int __result, Thing thing, Thing other, bool respectStackLimit)
        {
            if (respectStackLimit)
            {
                var t = Limits.CalculateStackLimit(thing) - thing.stackCount;
                if (t < 0)
                {
                    t = 0;
                }

                __result = Mathf.Min(other.stackCount, t);
            }
            else
            {
                __result = other.stackCount;
            }

            return(false);
        }
 private static void Postfix(Thing t, ref bool __result)
 {
     if (__result)
     {
         __result = !Limits.HasStackLimit(t) && t.stackCount != Limits.CalculateStackLimit(t);
     }
     //if (t.IsForbidden(Faction.OfPlayer))
     //{
     //    __result = false;
     //}
     //else if (!Limits.HasStackLimit(t))
     //{
     //    __result = false;
     //}
     //else if (t.stackCount == Limits.CalculateStackLimit(t))
     //{
     //    __result = false;
     //}
     //else
     //    __result = true;
     //return false;
 }
        public static bool Prefix(Pawn p, Thing t, IntVec3 storeCell, bool fitInStoreCell, ref Job __result)
        {
            var limit = Limits.CalculateStackLimit(t);

            if (t.stackCount > limit)
            {
                var job = new Job(JobDefOf.HaulToCell, t, storeCell)
                {
                    count = t.stackCount - limit,
                    haulOpportunisticDuplicates = true,
                    haulMode = HaulMode.ToCellStorage
                };
                __result = job;
                Mod.Debug($"dispatch job1, thing={t},cell={storeCell}");
                return(false);
            }

            limit = Limits.CalculateStackLimit(p.Map.haulDestinationManager.SlotGroupAt(storeCell));
            if (limit >= 99999)
            {
                Mod.Debug($"dispatch job3, thing={t},cell={storeCell}");
                return(true);
            }

            {
                var job   = new Job(JobDefOf.HaulToCell, t, storeCell);
                var thing = p.Map.thingGrid.ThingAt(storeCell, t.def);
                job.count = limit - thing?.stackCount ?? limit;

                job.haulOpportunisticDuplicates = false;
                job.haulMode = HaulMode.ToCellStorage;
                __result     = job;
                Mod.Debug($"dispatch job2, thing={t},cell={storeCell}");
                return(false);
            }
        }
Beispiel #13
0
        public static void Postfix(ref Rect rect)
        {
            var tab = ITab_StorageFillTabsPatcher.currentTab;

            if (tab == null)
            {
                return;
            }

            var storeSettingsParent = (IStoreSettingsParent)typeof(ITab_Storage)
                                      .GetProperty("SelStoreSettingsParent", BindingFlags.NonPublic | BindingFlags.Instance)
                                      ?.GetGetMethod(true).Invoke(tab, new object[0]);
            var settings = storeSettingsParent?.GetStoreSettings();

            var limit    = Limits.GetLimit(settings);
            var hasLimit = limit != -1;

            Widgets.CheckboxLabeled(new Rect(rect.xMin, rect.yMin - 48f - 3f - 32f, rect.width / 2, 24f),
                                    "Total stack limit", ref hasLimit);

            if (hasLimit)
            {
                if (oldSettings != settings)
                {
                    buffer = limit.ToString();
                }

                Widgets.TextFieldNumeric(
                    new Rect(rect.xMin + (rect.width / 2) + 60f, rect.yMin - 48f - 3f - 32f, (rect.width / 2) - 60f,
                             24f), ref limit, ref buffer, 0, max);
            }

            Limits.SetLimit(settings, hasLimit ? limit : -1);

            oldSettings = settings;
        }
Beispiel #14
0
 public static void Postfix(StorageSettings s)
 {
     Limits.SetLimit(s, Clipboard1.clipboardLimit);
 }
Beispiel #15
0
        public static void SetCountLimit(Pawn p, Thing t, ref Job __result, IntVec3 storeCell)
        {
            SlotGroup toSlotGroup = p.Map.haulDestinationManager.SlotGroupAt(storeCell);            // ?? __result.targetB.Thing?.GetSlotGroup();

            if (toSlotGroup == null)
            {
                //It is a haul destination without a SlotGroup, e.g. a grave
                return;
            }

            int toLimit = Limits.GetLimit(toSlotGroup.Settings);

            //int currentStack = __result.count < 0 ? t.stackCount : __result.count;
            int stackCount = __result.targetA.Thing.stackCount;

            if (stackCount < 1)
            {
                stackCount = int.MaxValue;
            }
            int currentStack = Math.Min(__result.count, stackCount);

            currentStack = Math.Min(currentStack, p.carryTracker.AvailableStackSpace(__result.targetA.Thing.def));


            bool hasSetFirstLimit = false;

            SlotGroup fromSlotGroup = StoreUtility.GetSlotGroup(t.Position, t.Map);

            if (fromSlotGroup != null)
            {
                int fromLimit = Limits.GetLimit(fromSlotGroup.Settings);

                if (fromLimit > 0 && StoreUtility.CurrentStoragePriorityOf(t) > StoreUtility.StoragePriorityAtFor(storeCell, t))
                {
                    //Hauling from limited high priority to low priority. Only haul the minimum necessary to go to exact limit.
                    int stockpileStack1 = fromSlotGroup.TotalPrecalculatedItemsStack();
                    __result.count   = stockpileStack1 - fromLimit;
                    hasSetFirstLimit = true;
                }
            }

            if (toLimit < 0)
            {
                return;
            }

            int stockpileStack = toSlotGroup.TotalPrecalculatedItemsStack();

            if (stockpileStack >= toLimit)
            {
                //Say no spot availible
                JobFailReason.Is(GetNoEmptyPlaceLowerTransString(), null);
                __result = null;
            }
            else if (stockpileStack + currentStack > toLimit)            //It will go over the limit
            {
                int newLimit = toLimit - stockpileStack;

                __result.count = hasSetFirstLimit ? Math.Min(newLimit, __result.count < 0 ? int.MaxValue : __result.count) : newLimit;
            }

            if (__result != null && __result.count <= 0)
            {
                JobFailReason.Is(GetNoEmptyPlaceLowerTransString(), null);
                __result = null;
            }
        }
        public static bool Prefix(Pawn pawn, Thing thing, WorkGiver_HaulToInventory __instance, bool forced, ref bool __result)
        {
            #region PickUpAndHaul code
            //bulky gear (power armor + minigun) so don't bother.
            if (MassUtility.GearMass(pawn) / MassUtility.Capacity(pawn) >= 0.8f)
            {
                return(false);
            }

            if (!WorkGiver_HaulToInventory.GoodThingToHaul(thing, pawn) || !HaulAIUtility.PawnCanAutomaticallyHaulFast(pawn, thing, forced))
            {
                return(false);
            }

            StoragePriority currentPriority = StoreUtility.CurrentStoragePriorityOf(thing);
            bool            foundCell       = StoreUtility.TryFindBestBetterStoreCellFor(thing, pawn, pawn.Map, currentPriority, pawn.Faction, out IntVec3 storeCell, true);
            #endregion

            if (!foundCell)
            {
                __result = false;
            }
            else
            {
                SlotGroup slotGroup = pawn.Map.haulDestinationManager.SlotGroupAt(storeCell);
                __result = !(slotGroup != null && Limits.HasLimit(slotGroup.Settings) && Limits.GetLimit(slotGroup.Settings) >= slotGroup.TotalPrecalculatedItemsStack());
            }

            return(false);
        }
Beispiel #17
0
 public static void Postfix(StorageSettings s)
 {
     clipboardLimit = Limits.GetLimit(s);
 }
        public static void Postfix(Map __instance)
        {
            var hdm = __instance.haulDestinationManager;

            if (hdm != null)
            {
                var slotGroups = (List <SlotGroup>) typeof(HaulDestinationManager).GetField("allGroupsInOrder", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(hdm);

                if (slotGroups != null)
                {
                    foreach (SlotGroup slotGroup in slotGroups.Where(x => x != null && Limits.HasLimit(x.Settings)))
                    {
                        HeldItemsCounter.UpdateSlotGroup(__instance, slotGroup);
                    }

                    HeldItemsCounter.RemoveUnusedSlotGroups(__instance, slotGroups);
                }
            }

            //HeldItemsCounter.TestOutput();
        }
Beispiel #19
0
        public static bool Prefix(Thing __instance, Thing other, bool respectStackLimit, ref bool __result)
        {
            Mod.Debug("Thing.TryAbsorbStack begin");
            if (!Limits.HasStackLimit(__instance))
            {
                return(true);
            }

            if (!__instance.CanStackWith(other))
            {
                __result = false;
                return(false);
            }

            int num;

            if (respectStackLimit)
            {
                var t = Limits.CalculateStackLimit(__instance) - __instance.stackCount;
                if (t < 0)
                {
                    t = 0;
                }

                num = Mathf.Min(other.stackCount, t);
            }
            else
            {
                num = other.stackCount;
            }

            if (num <= 0)
            {
                __result = false;
                return(false);
            }

            if (__instance.def.useHitPoints)
            {
                __instance.HitPoints =
                    Mathf.CeilToInt(((__instance.HitPoints * __instance.stackCount) + (other.HitPoints * num)) /
                                    (float)(__instance.stackCount + num));
            }

            __instance.stackCount += num;
            other.stackCount      -= num;
            StealAIDebugDrawer.Notify_ThingChanged(__instance);
            if (__instance.Spawned)
            {
                __instance.Map.listerMergeables.Notify_ThingStackChanged(__instance);
            }

            if (other.stackCount <= 0)
            {
                other.Destroy();
                __result = true;
            }
            else
            {
                __result = false;
            }

            return(false);
        }
Beispiel #20
0
 public static void Postfix(StorageSettings __instance)
 {
     Scribe_Values.Look <int>(ref Limits.GetLimit(__instance), "stacklimit", -1, false);
 }