internal static Group TryCreateGroup(MapCompGrowthSync mapComp, Plant crop, bool flashcells = false) { if (!flashcells && GroupsUtils.HasGroup(crop)) { return(null); } var list = new HashSet <Plant>(); try { if (!GroupsUtils.HasGroup(crop)) { Iterate(mapComp, crop, list, crop.Growth, crop.Growth, flashcells, IntVec3.Invalid); } } catch (Exception ex) { Log.Warning("failed to create group from crop " + crop + " at " + crop.Position + " : " + ex.Message + ". " + ex.StackTrace); return(null); } if (flashcells || list.Count <= 0) { return(null); } return(new Group(list)); }
static void Iterate(MapCompGrowthSync mapComp, Plant crop, ICollection <Plant> list, float minGrowth, float maxGrowth, bool flashcells, IntVec3 previous) { if (!CanHaveGroup(crop, flashcells) || list.Contains(crop)) { return; } #if DEBUG if (flashcells) { var count = crop.Map.GetComponent <MapCompGrowthSync>().Count; crop.Map.debugDrawer.FlashCell(crop.Position, list.Count * 10000000, "#" + list.Count, 300); } #endif list.Add(crop); mapComp.allPlantsInGroup.Add(crop); foreach (IntVec3 cell in CellsAdjacent4Way(crop.Position)) { if (cell == previous || !cell.InBounds(crop.Map)) { continue; } Plant plantAtCell = cell.GetPlant(crop.Map); if (plantAtCell != null && CanGroupTogether(crop, plantAtCell)) { if (Math.Max(plantAtCell.Growth, maxGrowth) - Math.Min(plantAtCell.Growth, minGrowth) > MAX_GROWTH_GAP) { #if DEBUG if (flashcells) { crop.Map.debugDrawer.FlashCell(crop.Position, float.MinValue, "max gap", 300); } #endif continue; } maxGrowth = Math.Max(plantAtCell.Growth, maxGrowth); minGrowth = Math.Min(plantAtCell.Growth, minGrowth); Iterate(mapComp, plantAtCell, list, minGrowth, maxGrowth, flashcells, crop.Position); } } }