public void generateFresh(string seed)
        {
            float chanceAnimal;
            float chancePlant;
            float chanceResourceRock;

            chanceAnimal       = float.Parse(RimEconomy.SettingData["specialityChanceAnimal"].Value);
            chancePlant        = float.Parse(RimEconomy.SettingData["specialityChancePlant"].Value);
            chanceResourceRock = float.Parse(RimEconomy.SettingData["specialityChanceResourceRock"].Value);
            Rand.Seed          = GenText.StableStringHash(seed);
            List <Tile> tiles = Find.WorldGrid.tiles;
            Dictionary <BiomeDef, IEnumerable <ThingDef> > biomePlantCache = new Dictionary <BiomeDef, IEnumerable <ThingDef> >();
            IEnumerable <ThingDef> resourceRocks = from d in DefDatabase <ThingDef> .AllDefs
                                                   where d.category == ThingCategory.Building && d.building != null && d.building.isResourceRock
                                                   select d;

            for (int i = 0; i <= Find.WorldGrid.TilesCount - 1; i++)
            {
                Tile tile = tiles[i];
                if (!tile.WaterCovered)
                {
                    BiomeDef    biome         = tile.biome;
                    PawnKindDef animalKindDef = null;
                    ThingDef    plantDef      = null;
                    ThingDef    resourceRock  = null;
                    if (Rand.Chance(chanceAnimal * biome.animalDensity))
                    {
                        animalKindDef = biome.AllWildAnimals.RandomElementByWeight((PawnKindDef def) => biome.CommonalityOfAnimal(def) / def.wildSpawn_GroupSizeRange.Average);
                    }
                    if (Rand.Chance(chancePlant * biome.plantDensity))
                    {
                        IEnumerable <ThingDef> plants = null;
                        if (biomePlantCache.ContainsKey(biome))
                        {
                            plants = biomePlantCache[biome];
                        }
                        else
                        {
                            plants = from ThingDef def in biome.AllWildPlants
                                     where def.plant != null && (def.plant.harvestedThingDef != null || (def.plant.sowTags != null && def.plant.sowTags.Contains("Ground")))
                                     select def;
                            biomePlantCache[biome] = plants;
                        }
                        plantDef = plants.RandomElementByWeight((ThingDef def) => biome.CommonalityOfPlant(def));
                    }
                    if (Rand.Chance(chanceResourceRock))
                    {
                        resourceRock = resourceRocks.RandomElementByWeight((ThingDef def) => def.building.mineableScatterCommonality);
                    }
                    if (animalKindDef != null || plantDef != null || resourceRock != null)
                    {
                        specialities[i] = new Speciality(i, animalKindDef, plantDef, resourceRock);
                    }
                }
            }
        }
Example #2
0
        private static Dictionary <ThingDef, float> CalculateDesiredPlantProportions(BiomeDef biome)
        {
            Dictionary <ThingDef, float> dictionary = new Dictionary <ThingDef, float>();
            float num = 0f;

            foreach (ThingDef current in DefDatabase <ThingDef> .AllDefs)
            {
                if (current.plant != null)
                {
                    float num2 = biome.CommonalityOfPlant(current);
                    dictionary.Add(current, num2);
                    num += num2;
                }
            }
            foreach (ThingDef current2 in biome.AllWildPlants)
            {
                Dictionary <ThingDef, float> dictionary2;
                ThingDef key;
                (dictionary2 = dictionary)[key = current2] = dictionary2[key] / num;
            }
            return(dictionary);
        }
        private void generateSpecialities(string seed)
        {
            float chanceAnimal           = RimEconomy.SettingFloat["specialityChanceAnimal"].Value;
            float chancePlant            = RimEconomy.SettingFloat["specialityChancePlant"].Value;
            float chanceResourceRock     = RimEconomy.SettingFloat["specialityChanceResourceRock"].Value;
            bool  dontFilterSpeciality   = RimEconomy.SettingBool["dontFilterSpeciality"].Value;
            float maxCommonalityOfAnimal = RimEconomy.SettingFloat["maxCommonalityOfAnimal"].Value;
            float maxCommonalityOfPlant  = RimEconomy.SettingFloat["maxCommonalityOfPlant"].Value;

            if (seed != null)
            {
                Rand.Seed = GenText.StableStringHash(seed);
            }
            List <Tile> tiles = Find.WorldGrid.tiles;
            Dictionary <BiomeDef, IEnumerable <ThingDef> >    biomePlantCache  = new Dictionary <BiomeDef, IEnumerable <ThingDef> >();
            Dictionary <BiomeDef, IEnumerable <PawnKindDef> > biomeAnimalCache = new Dictionary <BiomeDef, IEnumerable <PawnKindDef> >();
            IEnumerable <ThingDef> resourceRocks = from d in DefDatabase <ThingDef> .AllDefs
                                                   where d.category == ThingCategory.Building && d.building != null && d.building.isResourceRock
                                                   select d;

            for (int i = 0; i <= Find.WorldGrid.TilesCount - 1; i++)
            {
                Tile tile = tiles[i];
                if (!tile.WaterCovered)
                {
                    BiomeDef    biome         = tile.biome;
                    PawnKindDef animalKindDef = null;
                    ThingDef    plantDef      = null;
                    ThingDef    resourceRock  = null;
                    if (Rand.Chance(chanceAnimal * biome.animalDensity))
                    {
                        IEnumerable <PawnKindDef> animals = null;
                        if (biomeAnimalCache.ContainsKey(biome))
                        {
                            animals = biomeAnimalCache[biome];
                        }
                        else
                        {
                            animals = biome.AllWildAnimals;
                            biomeAnimalCache[biome] = animals;
                            Log.Message("-----------deubg for animal----------------");
                            Log.Message("biome: " + biome);
                            foreach (PawnKindDef def in animals)
                            {
                                Log.Message("def: " + def.race + " commonality: " + biome.CommonalityOfAnimal(def) / def.wildSpawn_GroupSizeRange.Average);
                            }
                        }
                        animalKindDef = animals.RandomElementByWeight((PawnKindDef def) => {
                            if (def.race == DefDatabase <ThingDef> .GetNamed("Rat", true))
                            {
                                return(0);
                            }
                            if (def.wildSpawn_GroupSizeRange.Average > 0)
                            {
                                float commonality = biome.CommonalityOfAnimal(def) / def.wildSpawn_GroupSizeRange.Average;
                                if (!dontFilterSpeciality && commonality >= maxCommonalityOfAnimal)
                                {
                                    return(0);
                                }
                                return(commonality);
                            }
                            return(0);
                        });
                    }
                    if (Rand.Chance(chancePlant * biome.plantDensity))
                    {
                        IEnumerable <ThingDef> plants = null;
                        if (biomePlantCache.ContainsKey(biome))
                        {
                            plants = biomePlantCache[biome];
                        }
                        else
                        {
                            plants = from ThingDef def in biome.AllWildPlants
                                     where def.plant != null && (def.plant.harvestedThingDef != null || (def.plant.sowTags != null && def.plant.sowTags.Contains("Ground")))
                                     select def;
                            biomePlantCache[biome] = plants;
                            Log.Message("-----------deubg for plant----------------");
                            Log.Message("biome: " + biome);
                            foreach (ThingDef def in plants)
                            {
                                Log.Message("def: " + def + " commonality: " + biome.CommonalityOfPlant(def));
                            }
                        }
                        plantDef = plants.RandomElementByWeight((ThingDef def) => {
                            if (def == DefDatabase <ThingDef> .GetNamed("PlantGrass", true))
                            {
                                return(0);
                            }
                            float commonality = biome.CommonalityOfPlant(def);
                            if (!dontFilterSpeciality && commonality >= maxCommonalityOfPlant)
                            {
                                return(0);
                            }
                            return(commonality);
                        });
                    }
                    if (Rand.Chance(chanceResourceRock))
                    {
                        resourceRock = resourceRocks.RandomElementByWeight((ThingDef def) => def.building.mineableScatterCommonality);
                    }
                    if (animalKindDef != null || plantDef != null || resourceRock != null)
                    {
                        tileSpeciality[i] = new Speciality(i, animalKindDef, plantDef, resourceRock);
                    }
                }
            }
        }
 private static string <BiomePlantsExpectedCount> m__1A(ThingDef p, BiomeDef b)
 {
     return((b.CommonalityOfPlant(p) * b.plantDensity * 4000f).ToString("F0"));
 }
Example #5
0
 public static void TryFindReproductionDestination_PostFix(IntVec3 source, ThingDef plantDef, SeedTargFindMode mode, Map map, ref IntVec3 foundCell, ref bool __result)
 {
     if (plantDef.plant.cavePlant)
     {
         float radius = -1f;
         if (mode == SeedTargFindMode.Reproduce)
         {
             radius = plantDef.plant.reproduceRadius;
         }
         else if (mode == SeedTargFindMode.MapGenCluster)
         {
             radius = plantDef.plant.WildClusterRadiusActual;
         }
         else if (mode == SeedTargFindMode.MapEdge)
         {
             radius = 40f;
         }
         else if (mode == SeedTargFindMode.Cave)
         {
             radius = plantDef.plant.WildClusterRadiusActual;
         }
         int      num      = 0;
         int      num2     = 0;
         float    num3     = 0f;
         CellRect cellRect = CellRect.CenteredOn(source, Mathf.RoundToInt(radius));
         cellRect.ClipInsideMap(map);
         for (int i = cellRect.minZ; i <= cellRect.maxZ; i++)
         {
             for (int j = cellRect.minX; j <= cellRect.maxX; j++)
             {
                 IntVec3 c2    = new IntVec3(j, 0, i);
                 Plant   plant = c2.GetPlant(map);
                 if (plant != null && (mode != SeedTargFindMode.Cave || plant.def.plant.cavePlant))
                 {
                     num++;
                     if (plant.def == plantDef)
                     {
                         num2++;
                     }
                 }
                 num3 += c2.GetTerrain(map).fertility;
             }
         }
         float num4  = (mode != SeedTargFindMode.Cave) ? map.Biome.plantDensity : 0.5f;
         float num5  = num3 * num4;
         bool  flag  = (float)num > num5;
         bool  flag2 = (float)num > num5 * 1.25f;
         if (flag2 && map.Biome.defName != "RWBCavern")
         {
             foundCell = IntVec3.Invalid;
             return;
         }
         if (mode != SeedTargFindMode.MapGenCluster && mode != SeedTargFindMode.Cave)
         {
             BiomeDef curBiome = map.Biome;
             float    num6     = curBiome.AllWildPlants.Sum((ThingDef pd) => curBiome.CommonalityOfPlant(pd));
             float    num7     = curBiome.CommonalityOfPlant(plantDef) / num6;
             float    num8     = curBiome.CommonalityOfPlant(plantDef) * plantDef.plant.wildCommonalityMaxFraction / num6;
             float    num9     = num5 * num8;
             if ((float)num2 > num9)
             {
                 foundCell = IntVec3.Invalid;
                 return;
             }
             float num10 = num5 * num7;
             bool  flag3 = (float)num2 < num10 * 0.5f;
             if (flag && !flag3)
             {
                 foundCell = IntVec3.Invalid;
                 return;
             }
         }
         Predicate <IntVec3> validator = (IntVec3 c) => plantDef.CanEverPlantAt(c, map) && (!plantDef.plant.cavePlant || GenPlantReproduction.GoodRoofForCavePlantReproduction(c, map)) && GenPlant.SnowAllowsPlanting(c, map) && source.InHorDistOf(c, radius) && GenSight.LineOfSight(source, c, map, true, null, 0, 0);
         __result = CellFinder.TryFindRandomCellNear(source, map, Mathf.CeilToInt(radius), validator, out foundCell);
         return;
     }
 }