Пример #1
0
        public static Thing TryMakeForStockSingle(ThingDef thingDef, int stackCount)
        {
            Thing result;

            if (stackCount <= 0)
            {
                result = null;
            }
            else if (!thingDef.tradeability.TraderCanSell())
            {
                Log.Error("Tried to make non-trader-sellable thing for trader stock: " + thingDef, false);
                result = null;
            }
            else
            {
                ThingDef stuff = null;
                if (thingDef.MadeFromStuff)
                {
                    if (!(from x in GenStuff.AllowedStuffsFor(thingDef, TechLevel.Undefined)
                          where !PawnWeaponGenerator.IsDerpWeapon(thingDef, x)
                          select x).TryRandomElementByWeight((ThingDef x) => x.stuffProps.commonality, out stuff))
                    {
                        stuff = GenStuff.RandomStuffByCommonalityFor(thingDef, TechLevel.Undefined);
                    }
                }
                Thing thing = ThingMaker.MakeThing(thingDef, stuff);
                thing.stackCount = stackCount;
                result           = thing;
            }
            return(result);
        }
        public static bool TryGetRandomThingWhichCanWeighNoMoreThan(IEnumerable <ThingDef> candidates, TechLevel stuffTechLevel, float maxMass, QualityGenerator?qualityGenerator, out ThingStuffPair thingStuffPair)
        {
            if (!candidates.Where((ThingDef x) => PossibleToWeighNoMoreThan(x, maxMass, GenStuff.AllowedStuffsFor(x, stuffTechLevel))).TryRandomElement(out var thingDef))
            {
                thingStuffPair = default(ThingStuffPair);
                return(false);
            }
            ThingDef result;

            if (thingDef.MadeFromStuff)
            {
                if (!(from x in GenStuff.AllowedStuffsFor(thingDef, stuffTechLevel)
                      where thingDef.GetStatValueAbstract(StatDefOf.Mass, x) <= maxMass && !IsDerpAndDisallowed(thingDef, x, qualityGenerator)
                      select x).TryRandomElementByWeight((ThingDef x) => x.stuffProps.commonality, out result))
                {
                    thingStuffPair = default(ThingStuffPair);
                    return(false);
                }
            }
            else
            {
                result = null;
            }
            thingStuffPair = new ThingStuffPair(thingDef, result);
            return(true);
        }
Пример #3
0
        public static ThingDef RandomStuffInexpensiveFor(ThingDef thingDef, TechLevel maxTechLevel)
        {
            ThingDef result;

            if (!thingDef.MadeFromStuff)
            {
                result = null;
            }
            else
            {
                IEnumerable <ThingDef> enumerable = GenStuff.AllowedStuffsFor(thingDef, maxTechLevel);
                float cheapestPrice = -1f;
                foreach (ThingDef thingDef2 in enumerable)
                {
                    float num = thingDef2.BaseMarketValue / thingDef2.VolumePerUnit;
                    if (cheapestPrice == -1f || num < cheapestPrice)
                    {
                        cheapestPrice = num;
                    }
                }
                enumerable = from x in enumerable
                             where x.BaseMarketValue / x.VolumePerUnit <= cheapestPrice * 4f
                             select x;
                ThingDef thingDef3;
                if (enumerable.TryRandomElementByWeight((ThingDef x) => x.stuffProps.commonality, out thingDef3))
                {
                    result = thingDef3;
                }
                else
                {
                    result = null;
                }
            }
            return(result);
        }
Пример #4
0
 public static ThingDef RandomStuffFor(ThingDef td)
 {
     if (!td.MadeFromStuff)
     {
         return(null);
     }
     return(GenStuff.AllowedStuffsFor(td, TechLevel.Undefined).RandomElement <ThingDef>());
 }
Пример #5
0
 public static ThingDef RandomStuffFor(ThingDef td)
 {
     if (!td.MadeFromStuff)
     {
         return(null);
     }
     return(GenStuff.AllowedStuffsFor(td).RandomElement());
 }
Пример #6
0
        public static bool TryRandomStuffFor(ThingDef td, out ThingDef stuff, TechLevel maxTechLevel = TechLevel.Undefined)
        {
            if (!td.MadeFromStuff)
            {
                stuff = null;
                return(true);
            }
            IEnumerable <ThingDef> source = GenStuff.AllowedStuffsFor(td, maxTechLevel);

            return(source.TryRandomElement(out stuff));
        }
Пример #7
0
        public static bool TryRandomStuffByCommonalityFor(ThingDef td, out ThingDef stuff, TechLevel maxTechLevel = TechLevel.Undefined)
        {
            if (!td.MadeFromStuff)
            {
                stuff = null;
                return(true);
            }
            IEnumerable <ThingDef> source = GenStuff.AllowedStuffsFor(td, maxTechLevel);

            return(source.TryRandomElementByWeight((ThingDef x) => x.stuffProps.commonality, out stuff));
        }
Пример #8
0
        public static ThingDef RandomStuffFor(ThingDef td)
        {
            ThingDef result;

            if (!td.MadeFromStuff)
            {
                result = null;
            }
            else
            {
                result = GenStuff.AllowedStuffsFor(td, TechLevel.Undefined).RandomElement <ThingDef>();
            }
            return(result);
        }
Пример #9
0
        public static bool TryRandomStuffFor(ThingDef td, out ThingDef stuff, TechLevel maxTechLevel = TechLevel.Undefined)
        {
            bool result;

            if (!td.MadeFromStuff)
            {
                stuff  = null;
                result = true;
            }
            else
            {
                IEnumerable <ThingDef> source = GenStuff.AllowedStuffsFor(td, maxTechLevel);
                result = source.TryRandomElement(out stuff);
            }
            return(result);
        }
Пример #10
0
        public static bool TryRandomStuffByCommonalityFor(ThingDef td, out ThingDef stuff, TechLevel maxTechLevel = TechLevel.Undefined)
        {
            if (!td.MadeFromStuff)
            {
                stuff = null;
                return(true);
            }
            IEnumerable <ThingDef> source = GenStuff.AllowedStuffsFor(td);

            if (maxTechLevel != 0)
            {
                source = from x in source
                         where (int)x.techLevel <= (int)maxTechLevel
                         select x;
            }
            return(source.TryRandomElementByWeight <ThingDef>((Func <ThingDef, float>)((ThingDef x) => x.stuffProps.commonality), out stuff));
        }
        public static float GetMinMarketValue(ThingDef thingDef, TechLevel stuffTechLevel)
        {
            float num = float.MaxValue;

            if (thingDef.MadeFromStuff)
            {
                foreach (ThingDef item in GenStuff.AllowedStuffsFor(thingDef, stuffTechLevel))
                {
                    if (item.stuffProps.commonality > 0f)
                    {
                        num = Mathf.Min(num, StatDefOf.MarketValue.Worker.GetValue(StatRequest.For(thingDef, item, QualityCategory.Awful)));
                    }
                }
                return(num);
            }
            return(Mathf.Min(num, StatDefOf.MarketValue.Worker.GetValue(StatRequest.For(thingDef, null, QualityCategory.Awful))));
        }
        public static float GetMinMass(ThingDef thingDef, TechLevel stuffTechLevel)
        {
            float num = float.MaxValue;

            if (thingDef.MadeFromStuff)
            {
                foreach (ThingDef item in GenStuff.AllowedStuffsFor(thingDef, stuffTechLevel))
                {
                    if (item.stuffProps.commonality > 0f)
                    {
                        num = Mathf.Min(num, thingDef.GetStatValueAbstract(StatDefOf.Mass, item));
                    }
                }
                return(num);
            }
            return(Mathf.Min(num, thingDef.GetStatValueAbstract(StatDefOf.Mass)));
        }
Пример #13
0
        public override void DoEditInterface(Listing_ScenEdit listing)
        {
            Rect scenPartRect = listing.GetScenPartRect(this, ScenPart.RowHeight * 3f);
            Rect rect         = new Rect(scenPartRect.x, scenPartRect.y, scenPartRect.width, scenPartRect.height / 3f);
            Rect rect2        = new Rect(scenPartRect.x, scenPartRect.y + scenPartRect.height / 3f, scenPartRect.width, scenPartRect.height / 3f);
            Rect rect3        = new Rect(scenPartRect.x, scenPartRect.y + scenPartRect.height * 2f / 3f, scenPartRect.width, scenPartRect.height / 3f);

            if (Widgets.ButtonText(rect, this.thingDef.LabelCap, true, false, true))
            {
                List <FloatMenuOption> list = new List <FloatMenuOption>();
                foreach (ThingDef localTd2 in from t in this.PossibleThingDefs()
                         orderby t.label
                         select t)
                {
                    ThingDef localTd = localTd2;
                    list.Add(new FloatMenuOption(localTd.LabelCap, delegate()
                    {
                        this.thingDef = localTd;
                        this.stuff    = GenStuff.DefaultStuffFor(localTd);
                    }, MenuOptionPriority.Default, null, null, 0f, null, null));
                }
                Find.WindowStack.Add(new FloatMenu(list));
            }
            if (this.thingDef.MadeFromStuff)
            {
                if (Widgets.ButtonText(rect2, this.stuff.LabelCap, true, false, true))
                {
                    List <FloatMenuOption> list2 = new List <FloatMenuOption>();
                    foreach (ThingDef localSd2 in from t in GenStuff.AllowedStuffsFor(this.thingDef, TechLevel.Undefined)
                             orderby t.label
                             select t)
                    {
                        ThingDef localSd = localSd2;
                        list2.Add(new FloatMenuOption(localSd.LabelCap, delegate()
                        {
                            this.stuff = localSd;
                        }, MenuOptionPriority.Default, null, null, 0f, null, null));
                    }
                    Find.WindowStack.Add(new FloatMenu(list2));
                }
            }
            Widgets.TextFieldNumeric <int>(rect3, ref this.count, ref this.countBuf, 1f, 1E+09f);
        }
Пример #14
0
        public override void DoEditInterface(Listing_ScenEdit listing)
        {
            Rect scenPartRect = listing.GetScenPartRect(this, ScenPart.RowHeight * 3f);
            Rect rect         = new Rect(scenPartRect.x, scenPartRect.y, scenPartRect.width, scenPartRect.height / 3f);
            Rect rect2        = new Rect(scenPartRect.x, scenPartRect.y + scenPartRect.height / 3f, scenPartRect.width, scenPartRect.height / 3f);
            Rect rect3        = new Rect(scenPartRect.x, scenPartRect.y + scenPartRect.height * 2f / 3f, scenPartRect.width, scenPartRect.height / 3f);

            if (Widgets.ButtonText(rect, thingDef.LabelCap))
            {
                List <FloatMenuOption> list = new List <FloatMenuOption>();
                foreach (ThingDef item in from t in PossibleThingDefs()
                         orderby t.label
                         select t)
                {
                    ThingDef localTd = item;
                    list.Add(new FloatMenuOption(localTd.LabelCap, delegate
                    {
                        thingDef = localTd;
                        stuff    = GenStuff.DefaultStuffFor(localTd);
                    }));
                }
                Find.WindowStack.Add(new FloatMenu(list));
            }
            if (thingDef.MadeFromStuff && Widgets.ButtonText(rect2, stuff.LabelCap))
            {
                List <FloatMenuOption> list2 = new List <FloatMenuOption>();
                foreach (ThingDef item2 in from t in GenStuff.AllowedStuffsFor(thingDef)
                         orderby t.label
                         select t)
                {
                    ThingDef localSd = item2;
                    list2.Add(new FloatMenuOption(localSd.LabelCap, delegate
                    {
                        stuff = localSd;
                    }));
                }
                Find.WindowStack.Add(new FloatMenu(list2));
            }
            Widgets.TextFieldNumeric(rect3, ref count, ref countBuf, 1f);
        }
Пример #15
0
        public static Thing TryMakeForStockSingle(ThingDef thingDef, int stackCount)
        {
            if (stackCount <= 0)
            {
                return(null);
            }
            if (!thingDef.tradeability.TraderCanSell())
            {
                Log.Error("Tried to make non-trader-sellable thing for trader stock: " + thingDef);
                return(null);
            }
            ThingDef result = null;

            if (thingDef.MadeFromStuff && !(from x in GenStuff.AllowedStuffsFor(thingDef)
                                            where !PawnWeaponGenerator.IsDerpWeapon(thingDef, x)
                                            select x).TryRandomElementByWeight((ThingDef x) => x.stuffProps.commonality, out result))
            {
                result = GenStuff.RandomStuffByCommonalityFor(thingDef);
            }
            Thing thing = ThingMaker.MakeThing(thingDef, result);

            thing.stackCount = stackCount;
            return(thing);
        }
Пример #16
0
        public static ThingDef DefaultStuffFor(BuildableDef bd)
        {
            ThingDef result;

            if (!bd.MadeFromStuff)
            {
                result = null;
            }
            else
            {
                ThingDef thingDef = bd as ThingDef;
                if (thingDef != null)
                {
                    if (thingDef.IsMeleeWeapon)
                    {
                        if (ThingDefOf.Steel.stuffProps.CanMake(bd))
                        {
                            return(ThingDefOf.Steel);
                        }
                        if (ThingDefOf.Plasteel.stuffProps.CanMake(bd))
                        {
                            return(ThingDefOf.Plasteel);
                        }
                    }
                    if (thingDef.IsApparel)
                    {
                        if (ThingDefOf.Cloth.stuffProps.CanMake(bd))
                        {
                            return(ThingDefOf.Cloth);
                        }
                        if (ThingDefOf.Leather_Plain.stuffProps.CanMake(bd))
                        {
                            return(ThingDefOf.Leather_Plain);
                        }
                        if (ThingDefOf.Steel.stuffProps.CanMake(bd))
                        {
                            return(ThingDefOf.Steel);
                        }
                    }
                }
                if (ThingDefOf.WoodLog.stuffProps.CanMake(bd))
                {
                    result = ThingDefOf.WoodLog;
                }
                else if (ThingDefOf.Steel.stuffProps.CanMake(bd))
                {
                    result = ThingDefOf.Steel;
                }
                else if (ThingDefOf.Plasteel.stuffProps.CanMake(bd))
                {
                    result = ThingDefOf.Plasteel;
                }
                else if (ThingDefOf.BlocksGranite.stuffProps.CanMake(bd))
                {
                    result = ThingDefOf.BlocksGranite;
                }
                else if (ThingDefOf.Cloth.stuffProps.CanMake(bd))
                {
                    result = ThingDefOf.Cloth;
                }
                else if (ThingDefOf.Leather_Plain.stuffProps.CanMake(bd))
                {
                    result = ThingDefOf.Leather_Plain;
                }
                else
                {
                    result = GenStuff.AllowedStuffsFor(bd, TechLevel.Undefined).First <ThingDef>();
                }
            }
            return(result);
        }
        public static List <ThingStuffPair> GenerateDefsWithPossibleTotalValue(int count, float totalValue, IEnumerable <ThingDef> allowed, TechLevel techLevel, Func <Thing, float> getValue, Func <ThingStuffPair, float> getMinValue, Func <ThingStuffPair, float> getMaxValue, Func <ThingDef, float> weightSelector = null, int tries = 100)
        {
            List <ThingStuffPair> list = new List <ThingStuffPair>();

            ItemCollectionGeneratorByTotalValueUtility.allowedFiltered.Clear();
            ItemCollectionGeneratorByTotalValueUtility.allowedFiltered.AddRange(allowed);
            ItemCollectionGeneratorByTotalValueUtility.allowedFiltered.RemoveAll((ThingDef x) => x.techLevel > techLevel || (!x.MadeFromStuff && getMinValue(new ThingStuffPair(x, null, 1f)) > totalValue));
            ItemCollectionGeneratorByTotalValueUtility.allowedStuff.Clear();
            for (int i = 0; i < ItemCollectionGeneratorByTotalValueUtility.allowedFiltered.Count; i++)
            {
                ThingDef thingDef = ItemCollectionGeneratorByTotalValueUtility.allowedFiltered[i];
                if (thingDef.MadeFromStuff)
                {
                    List <ThingDef> list2;
                    if (!ItemCollectionGeneratorByTotalValueUtility.allowedStuff.TryGetValue(thingDef, out list2))
                    {
                        list2 = new List <ThingDef>();
                        ItemCollectionGeneratorByTotalValueUtility.allowedStuff.Add(thingDef, list2);
                    }
                    foreach (ThingDef current in GenStuff.AllowedStuffsFor(thingDef))
                    {
                        if (current.stuffProps.commonality > 0f)
                        {
                            if (current.techLevel <= techLevel)
                            {
                                if (getMinValue(new ThingStuffPair(thingDef, current, 1f)) <= totalValue)
                                {
                                    list2.Add(current);
                                }
                            }
                        }
                    }
                }
            }
            ItemCollectionGeneratorByTotalValueUtility.allowedFiltered.RemoveAll((ThingDef x) => x.MadeFromStuff && !ItemCollectionGeneratorByTotalValueUtility.allowedStuff[x].Any <ThingDef>());
            if (!ItemCollectionGeneratorByTotalValueUtility.allowedFiltered.Any <ThingDef>())
            {
                return(list);
            }
            float num = 0f;

            for (int j = 0; j < tries; j++)
            {
                float num2 = 0f;
                float num3 = 0f;
                ItemCollectionGeneratorByTotalValueUtility.newCandidates.Clear();
                for (int k = 0; k < count; k++)
                {
                    ThingDef thingDef2;
                    if (weightSelector != null)
                    {
                        thingDef2 = ItemCollectionGeneratorByTotalValueUtility.allowedFiltered.RandomElementByWeight(weightSelector);
                    }
                    else
                    {
                        thingDef2 = ItemCollectionGeneratorByTotalValueUtility.allowedFiltered.RandomElement <ThingDef>();
                    }
                    ThingDef arg_221_0;
                    if (thingDef2.MadeFromStuff)
                    {
                        arg_221_0 = ItemCollectionGeneratorByTotalValueUtility.allowedStuff[thingDef2].RandomElementByWeight((ThingDef x) => x.stuffProps.commonality);
                    }
                    else
                    {
                        arg_221_0 = null;
                    }
                    ThingDef       stuff          = arg_221_0;
                    ThingStuffPair thingStuffPair = new ThingStuffPair(thingDef2, stuff, 1f);
                    ItemCollectionGeneratorByTotalValueUtility.newCandidates.Add(thingStuffPair);
                    num2 += getMinValue(thingStuffPair);
                    num3 += getMaxValue(thingStuffPair);
                }
                float num4 = (num2 > totalValue) ? (num2 - totalValue) : 0f;
                float num5 = (num3 < totalValue) ? (totalValue - num3) : 0f;
                if (!list.Any <ThingStuffPair>() || num > num4 + num5)
                {
                    list.Clear();
                    list.AddRange(ItemCollectionGeneratorByTotalValueUtility.newCandidates);
                    num = num4 + num5;
                }
                if (num <= 0f)
                {
                    break;
                }
            }
            return(list);
        }
        public static List <ThingStuffPair> GenerateDefsWithPossibleTotalValue(int count, float totalValue, IEnumerable <ThingDef> allowed, TechLevel techLevel, Func <Thing, float> getValue, Func <ThingStuffPair, float> getMinValue, Func <ThingStuffPair, float> getMaxValue, Func <ThingDef, float> weightSelector = null, int tries = 100)
        {
            List <ThingStuffPair> list = new List <ThingStuffPair>();

            ItemCollectionGeneratorByTotalValueUtility.allowedFiltered.Clear();
            ItemCollectionGeneratorByTotalValueUtility.allowedFiltered.AddRange(allowed);
            ItemCollectionGeneratorByTotalValueUtility.allowedFiltered.RemoveAll(delegate(ThingDef x)
            {
                if ((int)x.techLevel > (int)techLevel)
                {
                    return(true);
                }
                if (!x.MadeFromStuff && getMinValue(new ThingStuffPair(x, null, 1f)) > totalValue)
                {
                    return(true);
                }
                return(false);
            });
            ItemCollectionGeneratorByTotalValueUtility.allowedStuff.Clear();
            for (int i = 0; i < ItemCollectionGeneratorByTotalValueUtility.allowedFiltered.Count; i++)
            {
                ThingDef thingDef = ItemCollectionGeneratorByTotalValueUtility.allowedFiltered[i];
                if (thingDef.MadeFromStuff)
                {
                    List <ThingDef> list2 = default(List <ThingDef>);
                    if (!ItemCollectionGeneratorByTotalValueUtility.allowedStuff.TryGetValue(thingDef, out list2))
                    {
                        list2 = new List <ThingDef>();
                        ItemCollectionGeneratorByTotalValueUtility.allowedStuff.Add(thingDef, list2);
                    }
                    foreach (ThingDef item in GenStuff.AllowedStuffsFor(thingDef))
                    {
                        if (!(item.stuffProps.commonality <= 0.0) && (int)item.techLevel <= (int)techLevel && !(getMinValue(new ThingStuffPair(thingDef, item, 1f)) > totalValue))
                        {
                            list2.Add(item);
                        }
                    }
                }
            }
            ItemCollectionGeneratorByTotalValueUtility.allowedFiltered.RemoveAll((ThingDef x) => x.MadeFromStuff && !ItemCollectionGeneratorByTotalValueUtility.allowedStuff[x].Any());
            if (!ItemCollectionGeneratorByTotalValueUtility.allowedFiltered.Any())
            {
                return(list);
            }
            float num  = 0f;
            int   num2 = 0;

            while (num2 < tries)
            {
                float num3 = 0f;
                float num4 = 0f;
                ItemCollectionGeneratorByTotalValueUtility.newCandidates.Clear();
                for (int j = 0; j < count; j++)
                {
                    ThingDef       thingDef2      = (weightSelector == null) ? ItemCollectionGeneratorByTotalValueUtility.allowedFiltered.RandomElement() : ItemCollectionGeneratorByTotalValueUtility.allowedFiltered.RandomElementByWeight(weightSelector);
                    ThingDef       stuff          = (!thingDef2.MadeFromStuff) ? null : ItemCollectionGeneratorByTotalValueUtility.allowedStuff[thingDef2].RandomElementByWeight((ThingDef x) => x.stuffProps.commonality);
                    ThingStuffPair thingStuffPair = new ThingStuffPair(thingDef2, stuff, 1f);
                    ItemCollectionGeneratorByTotalValueUtility.newCandidates.Add(thingStuffPair);
                    num3 += getMinValue(thingStuffPair);
                    num4 += getMaxValue(thingStuffPair);
                }
                float num5 = (float)((!(num3 <= totalValue)) ? (num3 - totalValue) : 0.0);
                float num6 = (float)((!(num4 >= totalValue)) ? (totalValue - num4) : 0.0);
                if (!list.Any() || num > num5 + num6)
                {
                    list.Clear();
                    list.AddRange(ItemCollectionGeneratorByTotalValueUtility.newCandidates);
                    num = num5 + num6;
                }
                if (!(num <= 0.0))
                {
                    num2++;
                    continue;
                }
                break;
            }
            return(list);
        }