private void TrySpawnCavePlant() { WildSpawner.undergroundCells.Clear(); CellRect.CellRectIterator iterator = CellRect.WholeMap(this.map).GetIterator(); while (!iterator.Done()) { IntVec3 current = iterator.Current; if (GenPlantReproduction.GoodRoofForCavePlantReproduction(current, this.map)) { if (current.GetFirstItem(this.map) == null && current.GetFirstPawn(this.map) == null && current.GetFirstBuilding(this.map) == null) { bool flag = false; for (int i = 0; i < WildSpawner.cavePlants.Count; i++) { if (WildSpawner.cavePlants[i].CanEverPlantAt(current, this.map)) { flag = true; break; } } if (flag) { WildSpawner.undergroundCells.Add(current); } } } iterator.MoveNext(); } if (WildSpawner.undergroundCells.Any <IntVec3>()) { IntVec3 cell = WildSpawner.undergroundCells.RandomElement <IntVec3>(); ThingDef plantDef = (from x in WildSpawner.cavePlants where x.CanEverPlantAt(cell, this.map) select x).RandomElement <ThingDef>(); GenPlantReproduction.TryReproduceFrom(cell, plantDef, SeedTargFindMode.Cave, this.map); } }
public static bool TryFindReproductionDestination(IntVec3 source, ThingDef plantDef, SeedTargFindMode mode, Map map, out IntVec3 foundCell) { 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) { foundCell = IntVec3.Invalid; return(false); } 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(false); } float num10 = num5 * num7; bool flag3 = (float)num2 < num10 * 0.5f; if (flag && !flag3) { foundCell = IntVec3.Invalid; return(false); } } 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); return(CellFinder.TryFindRandomCellNear(source, map, Mathf.CeilToInt(radius), validator, out foundCell)); }