コード例 #1
0
        public static bool HasEnoughFeedstockInStorage(ref bool __result, Building_NutrientPasteDispenser __instance)
        {
            if (!(__instance.def.GetModExtension <NPDModExtension>() is null))
            {
                bool empty = __instance.def.GetModExtension <NPDModExtension>().ingredientList is null ||
                             !__instance.def.GetModExtension <NPDModExtension>().ingredientList.Any();
                if (!empty && !(__instance.def.GetCompProperties <CompProperties_Refillable>() is null))
                {
                    __result = false;
                    List <IngredientAndCostClass> ingredientList = __instance.def.GetModExtension <NPDModExtension>().ingredientList;

                    for (int i = 0; i < ingredientList.Count; i++)
                    {
                        IngredientAndCostClass ingredient = ingredientList[i];
                        if (__instance.GetComp <CompRefillable>().items[i] < ingredient.nutritionCost)
                        {
                            __result = false;
                            return(false);
                        }
                    }
                    __result = true;
                    return(false);
                }
                Log.Error("Must include ingredient list if using CompRefillable comp class");
            }
            return(true);
        }
コード例 #2
0
        public static bool TryDispenseCustomFood(ref Thing __result, Building_NutrientPasteDispenser __instance, ref List <IntVec3> ___cachedAdjCellsCardinal)
        {
            if (__instance.def.HasModExtension <NPDModExtension>())
            {
                if (!__instance.CanDispenseNow)
                {
                    __result = null;
                    return(false);
                }
                List <ThingDef> list = new List <ThingDef>();
                if (!(__instance.def.GetCompProperties <CompProperties_Refillable>() is null))
                {
                    List <IngredientAndCostClass> ingredients = __instance.def.GetModExtension <NPDModExtension>().ingredientList;
                    for (int i = 0; i < ingredients.Count; i++)
                    {
                        __instance.GetComp <CompRefillable>().ConsumeAmount(i, (int)(ingredients[i].nutritionCost * 10));
                        list.Add(ingredients[i].thingDef);
                    }
                    goto Block_3;
                }
                List <IngredientAndCostClass> ingredientList = __instance.def.GetModExtension <NPDModExtension>().ingredientList;
                bool empty = !__instance.def.GetModExtension <NPDModExtension>().ingredientList.Any();
                if (!empty)
                {
                    float[] nutritionLeft = new float[ingredientList.Count];
                    for (int i = 0; i < nutritionLeft.Length; i++)
                    {
                        nutritionLeft[i] = ingredientList[i].nutritionCost;
                    }
                    for (; ;)
                    {
                        Thing thing = __instance.def.GetModExtension <NPDModExtension>().FindNextIngredientInHopper(___cachedAdjCellsCardinal, __instance, nutritionLeft);
                        if (thing is null)
                        {
                            break;
                        }
                        int index = ingredientList.FindIndex(x => x.thingDef == thing.def);
                        int num2  = Mathf.Min(thing.stackCount, Mathf.CeilToInt(nutritionLeft[index] / thing.GetStatValue(StatDefOf.Nutrition, true)));
                        nutritionLeft[index] -= (float)num2 * thing.GetStatValue(StatDefOf.Nutrition, true);
                        list.Add(thing.def);
                        thing.SplitOff(num2);
                        if (!nutritionLeft.Any(x => x > 0f))
                        {
                            goto Block_3;
                        }
                    }
                }
                else
                {
                    float num = __instance.def.building.nutritionCostPerDispense - 0.0001f;
                    for (; ;)
                    {
                        Thing thing = __instance.FindFeedInAnyHopper();
                        if (thing is null)
                        {
                            break;
                        }
                        int num2 = Mathf.Min(thing.stackCount, Mathf.CeilToInt(num / thing.GetStatValue(StatDefOf.Nutrition, true)));
                        num -= (float)num2 * thing.GetStatValue(StatDefOf.Nutrition, true);
                        list.Add(thing.def);
                        thing.SplitOff(num2);
                        if (num <= 0f)
                        {
                            goto Block_3;
                        }
                    }
                }

                Log.Error("Did not find enough food in hoppers while trying to dispense.", false);
                __result = null;
                return(false);

Block_3:
                __instance.def.building.soundDispense.PlayOneShot(new TargetInfo(__instance.Position, __instance.Map, false));
                Thing           thing2          = ThingMaker.MakeThing(__instance.def.GetModExtension <NPDModExtension>().customMeal[rand.Next(__instance.def.GetModExtension <NPDModExtension>().customMeal.Count)], null);
                CompIngredients compIngredients = thing2.TryGetComp <CompIngredients>();
                if (!(compIngredients is null))
                {
                    foreach (ThingDef ingredient in list)
                    {
                        if (!__instance.def.GetModExtension <NPDModExtension>().mysteryIngredients)
                        {
                            compIngredients.RegisterIngredient(ingredient);
                        }
                    }
                }
                __result = thing2;
                return(false);
            }
            return(true);
        }