Exemple #1
0
        public override void Generate()
        {
            if (Find.Map.Biome != Util_CaveBiome.CaveBiomeDef)
            {
                // Use standard base function.
                base.Generate();
                return;
            }

            RegionAndRoomUpdater.Enabled = false;
            List <ThingDef_ClusterPlant> wildCavePlants = new List <ThingDef_ClusterPlant>();
            Dictionary <ThingDef_ClusterPlant, float> wildCavePlantsWeighted = new Dictionary <ThingDef_ClusterPlant, float>();

            foreach (ThingDef def in Find.Map.Biome.AllWildPlants)
            {
                ThingDef_ClusterPlant cavePlantDef = def as ThingDef_ClusterPlant;
                if (cavePlantDef != null)
                {
                    wildCavePlants.Add(cavePlantDef);
                    wildCavePlantsWeighted.Add(cavePlantDef, Find.Map.Biome.CommonalityOfPlant(cavePlantDef) / cavePlantDef.clusterSizeRange.Average);
                    Log.Message("Found caveplant def/commonality " + cavePlantDef + "/" + wildCavePlantsWeighted[cavePlantDef]);
                }
            }

            int spawnTriesNumber      = 10000;
            int totalSuccessfulSpawns = 0;
            int failedSpawns          = 0;
            int totalFailedSpawns     = 0;

            for (int tryIndex = 0; tryIndex < spawnTriesNumber; tryIndex++)
            {
                ThingDef_ClusterPlant cavePlantDef = wildCavePlants.RandomElementByWeight((ThingDef_ClusterPlant def) => wildCavePlantsWeighted[def]);

                int     newDesiredClusterSize = cavePlantDef.clusterSizeRange.RandomInRange;
                IntVec3 spawnCell             = IntVec3.Invalid;
                GenClusterPlantReproduction.TryGetRandomClusterSpawnCell(cavePlantDef, newDesiredClusterSize, false, out spawnCell);
                if (spawnCell.IsValid)
                {
                    totalSuccessfulSpawns++;
                    failedSpawns = 0;
                    ClusterPlant newPlant = Cluster.SpawnNewClusterAt(spawnCell, cavePlantDef, newDesiredClusterSize);
                    newPlant.Growth = Rand.Range(ClusterPlant.minGrowthToReproduce, plantMaxGrowth);

                    bool clusterIsMature = (Rand.Value < 0.7f);
                    GrowCluster(newPlant, clusterIsMature);
                }
                else
                {
                    failedSpawns++;
                    totalFailedSpawns++;
                    if (failedSpawns >= 50)
                    {
                        Log.Message("Stopping plant generation.");
                        break;
                    }
                }
            }
            Log.Message("Total successful/failed spawns = " + totalSuccessfulSpawns + "/" + totalFailedSpawns);
            RegionAndRoomUpdater.Enabled = true;
        }
        public override void Generate(Map map)
        {
            if (map.Biome != Util_CaveBiome.CaveBiomeDef)
            {
                // Use standard base function.
                base.Generate(map);
                return;
            }

            // Enabled it to avoid error while checking new plant can be spawned nearby in same room.
            map.regionAndRoomUpdater.Enabled = true;
            List <ThingDef_ClusterPlant> wildCavePlants = new List <ThingDef_ClusterPlant>();
            Dictionary <ThingDef_ClusterPlant, float> wildCavePlantsWeighted = new Dictionary <ThingDef_ClusterPlant, float>();

            foreach (ThingDef def in map.Biome.AllWildPlants)
            {
                ThingDef_ClusterPlant cavePlantDef = def as ThingDef_ClusterPlant;
                if (cavePlantDef != null)
                {
                    wildCavePlants.Add(cavePlantDef);
                    wildCavePlantsWeighted.Add(cavePlantDef, map.Biome.CommonalityOfPlant(cavePlantDef) / cavePlantDef.clusterSizeRange.Average);
                }
            }

            int spawnTriesNumber      = 10000;
            int totalSuccessfulSpawns = 0;
            int failedSpawns          = 0;
            int totalFailedSpawns     = 0;

            for (int tryIndex = 0; tryIndex < spawnTriesNumber; tryIndex++)
            {
                ThingDef_ClusterPlant cavePlantDef = wildCavePlants.RandomElementByWeight((ThingDef_ClusterPlant def) => wildCavePlantsWeighted[def]);

                int     newDesiredClusterSize = cavePlantDef.clusterSizeRange.RandomInRange;
                IntVec3 spawnCell             = IntVec3.Invalid;
                GenClusterPlantReproduction.TryGetRandomClusterSpawnCell(cavePlantDef, newDesiredClusterSize, false, map, out spawnCell); // Ignore temperature condition.
                if (spawnCell.IsValid)
                {
                    totalSuccessfulSpawns++;
                    failedSpawns = 0;
                    ClusterPlant newPlant = Cluster.SpawnNewClusterAt(map, spawnCell, cavePlantDef, newDesiredClusterSize);
                    newPlant.Growth = Rand.Range(ClusterPlant.minGrowthToReproduce, plantMaxGrowth);

                    bool clusterIsMature = (Rand.Value < 0.7f);
                    GrowCluster(newPlant, clusterIsMature);
                }
                else
                {
                    failedSpawns++;
                    totalFailedSpawns++;
                    if (failedSpawns >= 50)
                    {
                        break;
                    }
                }
            }
        }