/// <summary> /// Try to get a valid cell to spawn a new cluster away from plant. /// </summary> public static void TryGetRandomSpawnCellAwayFromCluster(Cluster cluster, int newDesiredClusterSize, out IntVec3 spawnCell) { spawnCell = IntVec3.Invalid; float newClusterExclusivityRadius = Cluster.GetExclusivityRadius(cluster.plantDef, newDesiredClusterSize); //Log.Message("newCluster size/ExclusivityRadius = " + newClusterSize + "/" + newClusterExclusivityRadius); // Current cluster and new cluster zones are exclusive and should not overlap. float newClusterMinDistance = cluster.exclusivityRadius + newClusterExclusivityRadius; float newClusterMaxDistance = 2f * newClusterMinDistance; //Log.Message("newClusterMinDistance/newClusterMaxDistance = " + newClusterMinDistance + "/" + newClusterMaxDistance); Predicate <IntVec3> validator = delegate(IntVec3 cell) { // Check cell is not too close from current cluster. if (cell.InHorDistOf(cluster.Position, newClusterMinDistance)) { return(false); } //Log.Message("not too close"); // Check cell is not too distant from current cluster. if (cell.InHorDistOf(cluster.Position, newClusterMaxDistance) == false) { return(false); } //Log.Message("not too distant"); // Check cell is in the same room. if (cell.GetRoom() != cluster.GetRoom()) { return(false); } // Check a plant can be spawned here. if (IsValidPositionToGrowPlant(cluster.plantDef, cell) == false) { return(false); } //Log.Message("IsValidPositionToSpawnPlant OK"); // Check there is no third cluster nearby. if (IsClusterAreaClear(cluster.plantDef, newDesiredClusterSize, cell) == false) { return(false); } return(true); }; bool validCellIsFound = CellFinder.TryFindRandomCellNear(cluster.Position, (int)newClusterMaxDistance, validator, out spawnCell); if (validCellIsFound == false) { // Note that TryFindRandomCellNear set result to root if no valid cell is found! spawnCell = IntVec3.Invalid; } }
/// <summary> /// Check if there is another cluster too close. /// </summary> public static bool IsClusterAreaClear(ThingDef_ClusterPlant plantDef, int newDesiredClusterSize, Map map, IntVec3 position) { float newClusterExclusivityRadius = Cluster.GetExclusivityRadius(plantDef, newDesiredClusterSize); foreach (Thing thing in map.listerThings.ThingsOfDef(Util_CaveworldFlora.ClusterDef)) { Cluster cluster = thing as Cluster; if (cluster.plantDef != plantDef) { continue; } if (cluster.Position.InHorDistOf(position, cluster.exclusivityRadius + newClusterExclusivityRadius)) { return(false); } } return(true); }