Example #1
0
        public static bool TryFindRandomEdgeCellWith(Predicate <IntVec3> validator, Map map, Rot4 dir, float roadChance, out IntVec3 result)
        {
            if (Rand.Value < roadChance)
            {
                bool flag = (from c in map.roadInfo.roadEdgeTiles
                             where validator(c) && c.OnEdge(map, dir)
                             select c).TryRandomElement <IntVec3>(out result);
                if (flag)
                {
                    return(flag);
                }
            }
            for (int i = 0; i < 100; i++)
            {
                result = CellFinder.RandomEdgeCell(dir, map);
                if (validator(result))
                {
                    return(true);
                }
            }
            int asInt = dir.AsInt;

            if (CellFinder.mapSingleEdgeCells[asInt] == null || map.Size != CellFinder.mapSingleEdgeCellsSize)
            {
                CellFinder.mapSingleEdgeCellsSize    = map.Size;
                CellFinder.mapSingleEdgeCells[asInt] = new List <IntVec3>();
                foreach (IntVec3 edgeCell in CellRect.WholeMap(map).GetEdgeCells(dir))
                {
                    CellFinder.mapSingleEdgeCells[asInt].Add(edgeCell);
                }
            }
            List <IntVec3> list = CellFinder.mapSingleEdgeCells[asInt];

            list.Shuffle();
            int j     = 0;
            int count = list.Count;

            for (; j < count; j++)
            {
                try
                {
                    if (validator(list[j]))
                    {
                        result = list[j];
                        return(true);
                    }
                }
                catch (Exception ex)
                {
                    Log.Error("TryFindRandomEdgeCellWith exception validating " + list[j] + ": " + ex.ToString());
                }
            }
            result = IntVec3.Invalid;
            return(false);
        }
Example #2
0
 public static bool TryFindRandomEdgeCellWith(Predicate <IntVec3> validator, Map map, float roadChance, out IntVec3 result)
 {
     if (Rand.Chance(roadChance))
     {
         bool flag = (from c in map.roadInfo.roadEdgeTiles
                      where validator(c)
                      select c).TryRandomElement(out result);
         if (flag)
         {
             return(flag);
         }
     }
     for (int i = 0; i < 100; i++)
     {
         result = CellFinder.RandomEdgeCell(map);
         if (validator(result))
         {
             return(true);
         }
     }
     if (CellFinder.mapEdgeCells == null || map.Size != CellFinder.mapEdgeCellsSize)
     {
         CellFinder.mapEdgeCellsSize = map.Size;
         CellFinder.mapEdgeCells     = new List <IntVec3>();
         foreach (IntVec3 current in CellRect.WholeMap(map).EdgeCells)
         {
             CellFinder.mapEdgeCells.Add(current);
         }
     }
     CellFinder.mapEdgeCells.Shuffle <IntVec3>();
     for (int j = 0; j < CellFinder.mapEdgeCells.Count; j++)
     {
         try
         {
             if (validator(CellFinder.mapEdgeCells[j]))
             {
                 result = CellFinder.mapEdgeCells[j];
                 return(true);
             }
         }
         catch (Exception ex)
         {
             Log.Error(string.Concat(new object[]
             {
                 "TryFindRandomEdgeCellWith exception validating ",
                 CellFinder.mapEdgeCells[j],
                 ": ",
                 ex.ToString()
             }), false);
         }
     }
     result = IntVec3.Invalid;
     return(false);
 }