public static void Postfix(WildPlantSpawner __instance, IntVec3 c, float plantDensity, float wholeMapNumDesiredPlants, bool setRandomGrowth, ref bool __result)
        {
            if (__result == false)
            {
                Map map = Traverse.Create(__instance).Field("map").GetValue <Map>();

                if (!(plantDensity <= 0.0) && c.GetPlant(map) == null && c.GetCover(map) == null && c.GetEdifice(map) == null && PlantUtility.SnowAllowsPlanting(c, map))
                {
                    //check if we're on water or salt.
                    TerrainDef            terrain  = c.GetTerrain(map);
                    BiomeSeasonalSettings biomeSet = map.Biome.GetModExtension <BiomeSeasonalSettings>();
                    if (biomeSet.specialPlants != null)
                    {
                        //spawn any special plants
                        WildPlantSpawner.tmpPossiblePlants

                        this.CalculatePlantsWhichCanGrowAt(c, WildPlantSpawner.tmpPossiblePlants, cavePlants, plantDensity);

                        if (!WildPlantSpawner.tmpPossiblePlants.Any())
                        {
                            return(false);
                        }
                        this.CalculateDistancesToNearbyClusters(c);
                        WildPlantSpawner.tmpPossiblePlantsWithWeight.Clear();
                        for (int i = 0; i < WildPlantSpawner.tmpPossiblePlants.Count; i++)
                        {
                            float value = this.PlantChoiceWeight(WildPlantSpawner.tmpPossiblePlants[i], c, WildPlantSpawner.distanceSqToNearbyClusters, wholeMapNumDesiredPlants, plantDensity);
                            WildPlantSpawner.tmpPossiblePlantsWithWeight.Add(new KeyValuePair <ThingDef, float>(WildPlantSpawner.tmpPossiblePlants[i], value));
                        }
                        KeyValuePair <ThingDef, float> keyValuePair = default(KeyValuePair <ThingDef, float>);
                        if (!((IEnumerable <KeyValuePair <ThingDef, float> >)WildPlantSpawner.tmpPossiblePlantsWithWeight).TryRandomElementByWeight <KeyValuePair <ThingDef, float> >((Func <KeyValuePair <ThingDef, float>, float>)((KeyValuePair <ThingDef, float> x) => x.Value), out keyValuePair))
                        {
                            return(false);
                        }
                        Plant plant = (Plant)ThingMaker.MakeThing(keyValuePair.Key, null);
                        if (setRandomGrowth)
                        {
                            plant.Growth = Rand.Range(0.07f, 1f);
                            if (plant.def.plant.LimitedLifespan)
                            {
                                plant.Age = Rand.Range(0, Mathf.Max(plant.def.plant.LifespanTicks - 50, 0));
                            }
                        }
                        GenSpawn.Spawn(plant, c, map, WipeMode.Vanish);
                        return(true);
                    }

                    PlantDef = c.GetPlant(map);
                    if (__result == true && plantDef != null && c != null && map != null)
                    {
                        BiomeDef biome = map.Biome;
                        BiomeSeasonalSettings biomeSettings = biome.GetModExtension <BiomeSeasonalSettings>();
                        if (biomeSettings != null)
                        {
                            if (!biomeSettings.canPutOnTerrain(c, plantDef, map))
                            {
                                __result = false;
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        public static bool CheckSpawnWildPlantAt(WildPlantSpawner __instance, ref bool __result, IntVec3 c, float plantDensity, float wholeMapNumDesiredPlants, bool setRandomGrowth = false)
        {
            Map map2 = map(__instance);

            if (plantDensity <= 0f || c.GetPlant(map2) != null || c.GetCover(map2) != null || c.GetEdifice(map2) != null || map2.fertilityGrid.FertilityAt(c) <= 0f || !PlantUtility.SnowAllowsPlanting(c, map2))
            {
                __result = false;
                return(false);
            }

            bool cavePlants = GoodRoofForCavePlant2(map2, c);

            if (SaturatedAt2(map2, c, plantDensity, cavePlants, wholeMapNumDesiredPlants))
            {
                __result = false;
                return(false);
            }
            List <ThingDef> tmpPossiblePlants = new List <ThingDef>();

            CalculatePlantsWhichCanGrowAt2(__instance, c, tmpPossiblePlants, cavePlants, plantDensity);
            if (!tmpPossiblePlants.Any())
            {
                __result = false;
                return(false);
            }

            Dictionary <ThingDef, float>           distanceSqToNearbyClusters  = CalculateDistancesToNearbyClusters2(__instance, c);
            List <KeyValuePair <ThingDef, float> > tmpPossiblePlantsWithWeight = new List <KeyValuePair <ThingDef, float> >();

            tmpPossiblePlantsWithWeight.Clear();
            for (int i = 0; i < tmpPossiblePlants.Count; i++)
            {
                float value = PlantChoiceWeight2(__instance, tmpPossiblePlants[i], c, distanceSqToNearbyClusters, wholeMapNumDesiredPlants, plantDensity);
                tmpPossiblePlantsWithWeight.Add(new KeyValuePair <ThingDef, float>(tmpPossiblePlants[i], value));
            }

            if (!tmpPossiblePlantsWithWeight.TryRandomElementByWeight((KeyValuePair <ThingDef, float> x) => x.Value, out KeyValuePair <ThingDef, float> result))
            {
                __result = false;
                return(false);
            }

            Plant plant = (Plant)ThingMaker.MakeThing(result.Key);

            if (setRandomGrowth)
            {
                plant.Growth = Rand.Range(0.07f, 1f);
                if (plant.def.plant.LimitedLifespan)
                {
                    plant.Age = Rand.Range(0, Mathf.Max(plant.def.plant.LifespanTicks - 50, 0));
                }
            }

            GenSpawn.Spawn(plant, c, map2);
            __result = true;
            return(false);
        }
 static bool CheckSpawnWildPlantAt(IntVec3 c, float plantDensity, float wholeMapNumDesiredPlants, bool setRandomGrowth = false)
 {
     if (!(plantDensity <= 0.0) && c.GetPlant(this.map) == null && c.GetCover(this.map) == null && c.GetEdifice(this.map) == null && !(this.map.fertilityGrid.FertilityAt(c) <= 0.0) && PlantUtility.SnowAllowsPlanting(c, this.map))
     {
     }
 }