Exemplo n.º 1
0
 public AvoidGrid(Map map)
 {
     mapSize   = map.Size.x * map.Size.z;
     costGrids = new int[][] { new int[mapSize], new int[mapSize] };
     idx       = 0;
     filler    = new FloodFiller(map);
 }
Exemplo n.º 2
0
    bool TestMapCandidate(int[,] grid, float requiredFillPercent)
    {
        Vector2     randomOpenGridCoord = GetRandomOpenGridCoord();
        FloodFiller floodFillTester     = new FloodFiller(grid);

        bool[,] filled = floodFillTester.Fill((int)randomOpenGridCoord.x, (int)randomOpenGridCoord.y);

        int total       = 0;
        int totalFilled = 0;

        for (int y = 0; y < filled.GetLength(0); y++)
        {
            for (int x = 0; x < filled.GetLength(1); x++)
            {
                total++;
                if (filled[y, x])
                {
                    totalFilled++;
                }
            }
        }

        float filledPercent = (float)totalFilled / (float)total;

        return(filledPercent >= requiredFillPercent);
    }
Exemplo n.º 3
0
        private bool TrySpawnPillar(Faction faction, ThingDef wallStuff)
        {
            if (!roofsAboutToCollapse.Any())
            {
                return(false);
            }
            Map                 map         = BaseGen.globalSettings.map;
            IntVec3             bestCell    = IntVec3.Invalid;
            float               bestScore   = 0f;
            FloodFiller         floodFiller = map.floodFiller;
            IntVec3             invalid     = IntVec3.Invalid;
            Predicate <IntVec3> passCheck   = (IntVec3 x) => roofsAboutToCollapse.Contains(x);
            Action <IntVec3>    processor   = delegate(IntVec3 x)
            {
                float pillarSpawnScore = GetPillarSpawnScore(x);
                if (pillarSpawnScore > 0f && (!bestCell.IsValid || pillarSpawnScore >= bestScore))
                {
                    bestCell  = x;
                    bestScore = pillarSpawnScore;
                }
            };
            List <IntVec3> extraRoots = edgeRoofs;

            floodFiller.FloodFill(invalid, passCheck, processor, 2147483647, rememberParents: false, extraRoots);
            if (bestCell.IsValid)
            {
                Thing thing = ThingMaker.MakeThing(ThingDefOf.Wall, wallStuff);
                thing.SetFaction(faction);
                GenSpawn.Spawn(thing, bestCell, map);
                return(true);
            }
            return(false);
        }
        private bool TrySpawnPillar(Faction faction, ThingDef wallStuff)
        {
            if (!SymbolResolver_EnsureCanHoldRoof.roofsAboutToCollapse.Any <IntVec3>())
            {
                return(false);
            }
            Map                 map       = BaseGen.globalSettings.map;
            IntVec3             bestCell  = IntVec3.Invalid;
            float               bestScore = 0f;
            FloodFiller         arg_8A_0  = map.floodFiller;
            IntVec3             invalid   = IntVec3.Invalid;
            Predicate <IntVec3> passCheck = (IntVec3 x) => SymbolResolver_EnsureCanHoldRoof.roofsAboutToCollapse.Contains(x);
            Action <IntVec3>    processor = delegate(IntVec3 x)
            {
                float pillarSpawnScore = this.GetPillarSpawnScore(x);
                if (pillarSpawnScore > 0f && (!bestCell.IsValid || pillarSpawnScore >= bestScore))
                {
                    bestCell  = x;
                    bestScore = pillarSpawnScore;
                }
            };
            List <IntVec3> extraRoots = SymbolResolver_EnsureCanHoldRoof.edgeRoofs;

            arg_8A_0.FloodFill(invalid, passCheck, processor, 2147483647, false, extraRoots);
            if (bestCell.IsValid)
            {
                Thing thing = ThingMaker.MakeThing(ThingDefOf.Wall, wallStuff);
                thing.SetFaction(faction, null);
                GenSpawn.Spawn(thing, bestCell, map, WipeMode.Vanish);
                return(true);
            }
            return(false);
        }
Exemplo n.º 5
0
    public virtual void SetupScene()
    {
        Random.InitState(seed);

        BoardSetup();

        floodFiller = new FloodFiller(grid);
    }
Exemplo n.º 6
0
    void BoardSetup()
    {
        bool passed = false;
        int  count  = 0;

        bool[,] randomContinuousRegion = new bool[mapHeight, mapWidth];
        while (!passed && count < 30)
        {
            count++;
            grid = GenerateMapCandidate();
            Vector2     randomOpenGridCoord = GetRandomOpenGridCoord();
            FloodFiller floodFillTester     = new FloodFiller(grid);
            randomContinuousRegion = floodFillTester.Fill((int)randomOpenGridCoord.x, (int)randomOpenGridCoord.y);

            float total       = 0f;
            float totalFilled = 0f;
            for (int y = 0; y < randomContinuousRegion.GetLength(0); y++)
            {
                for (int x = 0; x < randomContinuousRegion.GetLength(1); x++)
                {
                    total++;
                    if (randomContinuousRegion[y, x])
                    {
                        totalFilled++;
                    }
                }
            }

            passed = totalFilled / total >= 0.3f;
        }

        terrainArray = new GameObject[mapHeight, mapWidth];
        for (int x = 0; x < mapWidth; x++)
        {
            for (int y = 0; y < mapHeight; y++)
            {
                terrainArray[y, x] = randomContinuousRegion[y, x] ? floorTile : wallTile;
            }
        }

        board      = new GameObject("Board").transform;
        boardTiles = new GameObject[mapHeight, mapWidth];

        for (int x = 0; x < mapWidth; x++)
        {
            for (int y = 0; y < mapHeight; y++)
            {
                GameObject toInstantiate = terrainArray[y, x];
                boardTiles[y, x] = Instantiate(toInstantiate, new Vector3(x, y, 0f), Quaternion.identity, board);
            }
        }
    }
Exemplo n.º 7
0
        private static void ClearVisited(FloodFiller __instance)
        {
            int index1 = 0;

            for (int count = visited(__instance).Count; index1 < count; ++index1)
            {
                int index2 = visited(__instance)[index1];
                traversalDistance(__instance)[index2] = -1;
                if (parentGrid(__instance) != null)
                {
                    parentGrid(__instance)[index2] = IntVec3.Invalid;
                }
            }
            visited(__instance).Clear();
            openSet(__instance).Clear();
        }
Exemplo n.º 8
0
        public static Bitmap FillColor(Bitmap bmp, Point location, Color fillColor)
        {
            if (bmp == null || fillColor == null)
            {
                log.Error("Failed to fill color, image or color is null.");
                return(null);
            }

            try {
                FloodFiller filler = new FloodFiller();
                filler.FillColor = fillColor;
                return(filler.FloodFill(bmp, location));
            }
            catch (Exception ex) {
                log.ErrorFormat("Failed to fill color, image size {0}, location {1}, color {2}, error info {3}.",
                                bmp.Size.ToString(), location.ToString(), fillColor.ToString(), ex.Message);
                return(null);
            }
        }
Exemplo n.º 9
0
        void GenerateCells(Map map, List <ZombieCostSpecs> specs, int[] costCells, FloodFiller filler)
        {
            var mapSizeX  = map.Size.x;
            var pathGrid  = map.pathGrid;
            var cardinals = GenAdj.CardinalDirections;

            foreach (var spec in specs)
            {
                var loc           = spec.position;
                var costBase      = spec.maxCosts;
                var radiusSquared = spec.radius * spec.radius;

                var floodedCells = new Dictionary <IntVec3, int>();
                filler.FloodFill(loc,
                                 cell =>
                                 (loc - cell).LengthHorizontalSquared <= radiusSquared &&
                                 pathGrid.Walkable(cell) &&
                                 (cell.GetEdifice(map) is Building_Door) == false,
                                 cell =>
                {
                    var f              = 1f - (loc - cell).LengthHorizontalSquared / radiusSquared;
                    var cost           = (int)(costBase * f);
                    var idx            = cell.x + cell.z * mapSizeX;
                    costCells[idx]     = Math.Max(costCells[idx], cost);
                    floodedCells[cell] = costCells[idx];
                });

                foreach (var cell in floodedCells.Keys)
                {
                    for (var i = 0; i < 3; i++)
                    {
                        var pos = cell + cardinals[i];
                        if (floodedCells.ContainsKey(pos) == false &&
                            (loc - cell).LengthHorizontalSquared <= radiusSquared &&
                            pos.InBounds(map) && pos.GetEdifice(map) is Building_Door)
                        {
                            costCells[pos.x + pos.z * mapSizeX] = floodedCells[cell];
                        }
                    }
                }
            }
        }
Exemplo n.º 10
0
        public static Bitmap MagicCutOut(Bitmap sourceImage, Color color, Point[] points)
        {
            byte toll = 40;

            FloodFiller filter = new FloodFiller
            {
                FillStyle = FloodFillStyle.Queue,
                Tolerance =
                {
                    [0] = toll,
                    [1] = toll,
                    [2] = toll
                },
                FillColor = color,
                Bmp       = sourceImage
            };

            points.ToList().ForEach(point => filter.FloodFill(filter.Bmp, point));

            return(filter.Bmp);
        }
    public bool RemoveIslands(bool refValue)
    {
        FloodFiller floodFiller = new FloodFiller(grid, refValue);

        List <FloodFiller.Pos> prevRegion = null;
        bool filled = false;

        for (var row = 0; row < rows; row++)
        {
            for (var col = 0; col < cols; col++)
            {
                if (grid[row, col] == refValue && !floodFiller.IsFilled(row, col))
                {
                    var newRegion = floodFiller.FloodFill(row, col);
                    if (prevRegion == null)
                    {
                        prevRegion = newRegion;
                        continue;
                    }

                    var fillRegion = newRegion;
                    if (prevRegion.Count < newRegion.Count)
                    {
                        fillRegion = prevRegion;
                        prevRegion = newRegion;
                    }

                    foreach (var pos in fillRegion)
                    {
                        grid[pos.row, pos.col] = !refValue;
                        filled = true;
                    }
                }
            }
        }

        return(filled);
    }
Exemplo n.º 12
0
 public static bool FloodFill(FloodFiller __instance,
                              IntVec3 root,
                              Predicate <IntVec3> passCheck,
                              Func <IntVec3, int, bool> processor,
                              int maxCellsToProcess            = 2147483647,
                              bool rememberParents             = false,
                              IEnumerable <IntVec3> extraRoots = null)
 {
     lock (__instance)
     {
         if (working(__instance))
         {
             Log.Error("Nested FloodFill calls are not allowed. This will cause bugs.", false);
         }
         working(__instance) = true;
         ClearVisited(__instance);
         if (rememberParents && parentGrid(__instance) == null)
         {
             parentGrid(__instance) = new CellGrid(map(__instance));
         }
         if (root.IsValid && extraRoots == null && !passCheck(root))
         {
             if (rememberParents)
             {
                 parentGrid(__instance)[root] = IntVec3.Invalid;
             }
             working(__instance) = false;
         }
         else
         {
             int         area             = map(__instance).Area;
             IntVec3[]   directionsAround = GenAdj.CardinalDirectionsAround;
             int         length           = directionsAround.Length;
             CellIndices cellIndices      = map(__instance).cellIndices;
             int         num1             = 0;
             openSet(__instance).Clear();
             if (root.IsValid)
             {
                 int index = cellIndices.CellToIndex(root);
                 visited(__instance).Add(index);
                 traversalDistance(__instance)[index] = 0;
                 openSet(__instance).Enqueue(root);
             }
             if (extraRoots != null)
             {
                 if (extraRoots is IList <IntVec3> intVec3List)
                 {
                     for (int index1 = 0; index1 < intVec3List.Count; ++index1)
                     {
                         int index2 = cellIndices.CellToIndex(intVec3List[index1]);
                         visited(__instance).Add(index2);
                         traversalDistance(__instance)[index2] = 0;
                         openSet(__instance).Enqueue(intVec3List[index1]);
                     }
                 }
                 else
                 {
                     foreach (IntVec3 extraRoot in extraRoots)
                     {
                         int index = cellIndices.CellToIndex(extraRoot);
                         visited(__instance).Add(index);
                         traversalDistance(__instance)[index] = 0;
                         openSet(__instance).Enqueue(extraRoot);
                     }
                 }
             }
             if (rememberParents)
             {
                 for (int index = 0; index < visited(__instance).Count; ++index)
                 {
                     IntVec3 cell = cellIndices.IndexToCell(visited(__instance)[index]);
                     parentGrid(__instance)[visited(__instance)[index]] = passCheck(cell) ? cell : IntVec3.Invalid;
                 }
             }
             while (openSet(__instance).Count > 0)
             {
                 IntVec3 c1   = openSet(__instance).Dequeue();
                 int     num2 = traversalDistance(__instance)[cellIndices.CellToIndex(c1)];
                 if (!processor(c1, num2))
                 {
                     ++num1;
                     if (num1 != maxCellsToProcess)
                     {
                         for (int index1 = 0; index1 < length; ++index1)
                         {
                             IntVec3 c2     = c1 + directionsAround[index1];
                             int     index2 = cellIndices.CellToIndex(c2);
                             if (c2.InBounds(map(__instance)) && traversalDistance(__instance)[index2] == -1 && passCheck(c2))
                             {
                                 visited(__instance).Add(index2);
                                 openSet(__instance).Enqueue(c2);
                                 traversalDistance(__instance)[index2] = num2 + 1;
                                 if (rememberParents)
                                 {
                                     parentGrid(__instance)[index2] = c1;
                                 }
                             }
                         }
                         if (openSet(__instance).Count > area)
                         {
                             Log.Error("Overflow on flood fill (>" + (object)area + " cells). Make sure we're not flooding over the same area after we check it.", false);
                             working(__instance) = false;
                             return(false);
                         }
                     }
                     else
                     {
                         break;
                     }
                 }
                 else
                 {
                     break;
                 }
             }
             working(__instance) = false;
         }
     }
     return(false);
 }
Exemplo n.º 13
0
 public void SetUp()
 {
     colors      = new Color[Width, Height];
     floodFiller = new FloodFiller(colors);
 }
Exemplo n.º 14
0
		public void SetUp()
		{
			colors = new Color[Width,Height];
			floodFiller = new FloodFiller(colors);
		}
        /// <summary>
        /// Mod_FloodFillSkin
        /// Fill background pixels so mipmapping doesn't have haloes - Ed
        /// </summary>
        private void FloodFillSkin(UInt32[] table8To24, ByteArraySegment skin, Int32 skinwidth, Int32 skinheight)
        {
            var filler = new FloodFiller(skin, skinwidth, skinheight);

            filler.Perform(table8To24);
        }
Exemplo n.º 16
0
      public static bool FloodFill(FloodFiller __instance,
                                   IntVec3 root,
                                   Predicate <IntVec3> passCheck,
                                   Func <IntVec3, int, bool> processor,
                                   int maxCellsToProcess            = 2147483647,
                                   bool rememberParents             = false,
                                   IEnumerable <IntVec3> extraRoots = null)
      {
          lock (__instance)
          {
              if (__instance.working)
              {
                  Log.Error("Nested FloodFill calls are not allowed. This will cause bugs.");
              }
              __instance.working = true;
              __instance.ClearVisited();
              if (rememberParents && __instance.parentGrid == null)
              {
                  __instance.parentGrid = new CellGrid(__instance.map);
              }
              if (root.IsValid && extraRoots == null && !passCheck(root))
              {
                  if (rememberParents)
                  {
                      __instance.parentGrid[root] = IntVec3.Invalid;
                  }
                  __instance.working = false;
              }
              else
              {
                  int         area             = __instance.map.Area;
                  IntVec3[]   directionsAround = GenAdj.CardinalDirectionsAround;
                  int         length           = directionsAround.Length;
                  CellIndices cellIndices      = __instance.map.cellIndices;
                  int         num1             = 0;
                  __instance.openSet.Clear();
                  if (root.IsValid)
                  {
                      int index = cellIndices.CellToIndex(root);
                      __instance.visited.Add(index);
                      __instance.traversalDistance[index] = 0;
                      __instance.openSet.Enqueue(root);
                  }
                  if (extraRoots != null)
                  {
                      if (extraRoots is IList <IntVec3> intVec3List)
                      {
                          for (int index1 = 0; index1 < intVec3List.Count; ++index1)
                          {
                              int index2 = cellIndices.CellToIndex(intVec3List[index1]);
                              __instance.visited.Add(index2);
                              __instance.traversalDistance[index2] = 0;
                              __instance.openSet.Enqueue(intVec3List[index1]);
                          }
                      }
                      else
                      {
                          foreach (IntVec3 extraRoot in extraRoots)
                          {
                              int index = cellIndices.CellToIndex(extraRoot);
                              __instance.visited.Add(index);
                              __instance.traversalDistance[index] = 0;
                              __instance.openSet.Enqueue(extraRoot);
                          }
                      }
                  }
                  if (rememberParents)
                  {
                      for (int index = 0; index < __instance.visited.Count; ++index)
                      {
                          IntVec3 cell = cellIndices.IndexToCell(__instance.visited[index]);
                          __instance.parentGrid[__instance.visited[index]] = passCheck(cell) ? cell : IntVec3.Invalid;
                      }
                  }
                  while (__instance.openSet.Count > 0)
                  {
                      IntVec3 c1   = __instance.openSet.Dequeue();
                      int     num2 = __instance.traversalDistance[cellIndices.CellToIndex(c1)];
                      if (!processor(c1, num2))
                      {
                          ++num1;
                          if (num1 != maxCellsToProcess)
                          {
                              for (int index1 = 0; index1 < length; ++index1)
                              {
                                  IntVec3 c2     = c1 + directionsAround[index1];
                                  int     index2 = cellIndices.CellToIndex(c2);
                                  if (!c2.InBounds(__instance.map) || __instance.traversalDistance[index2] != -1 ||
                                      !passCheck(c2))
                                  {
                                      continue;
                                  }
                                  __instance.visited.Add(index2);
                                  __instance.openSet.Enqueue(c2);
                                  __instance.traversalDistance[index2] = num2 + 1;
                                  if (rememberParents)
                                  {
                                      __instance.parentGrid[index2] = c1;
                                  }
                              }

                              if (__instance.openSet.Count <= area)
                              {
                                  continue;
                              }
                              Log.Error("Overflow on flood fill (>" + area + " cells). Make sure we're not flooding over the same area after we check it.");
                              __instance.working = false;
                              return(false);
                          }
                      }
                      break;
                  }
                  __instance.working = false;
              }
          }
          return(false);
      }
Exemplo n.º 17
0
        private void ExpandSnow()
        {
            if (this.snowNoise == null)
            {
                this.snowNoise = new Perlin(0.054999999701976776, 2.0, 0.5, 5, Rand.Range(0, 651431), QualityMode.Medium);
            }
            if (this.snowRadius < 8f)
            {
                this.snowRadius += 1.3f;
            }
            else
            {
                if (this.snowRadius < 17f)
                {
                    this.snowRadius += 0.7f;
                }
                else
                {
                    if (this.snowRadius < 30f)
                    {
                        this.snowRadius += 0.4f;
                    }
                    else
                    {
                        this.snowRadius += 0.1f;
                    }
                }
            }
            if (this.snowRadius > 55f)
            {
                this.snowRadius = 55f;
            }
            CellRect occupiedRect = this.OccupiedRect();

            Building_CrashedShipPartCopy.reachableCells.Clear();
            FloodFiller.FloodFill(base.Position, (IntVec3 x) => x.DistanceToSquared(this.Position) <= this.snowRadius * this.snowRadius && (occupiedRect.Contains(x) || !x.Filled()), delegate(IntVec3 x)
            {
                Building_CrashedShipPartCopy.reachableCells.Add(x);
            });
            int num = GenRadial.NumCellsInRadius(this.snowRadius);

            for (int i = 0; i < num; i++)
            {
                IntVec3 intVec = base.Position + GenRadial.RadialPattern[i];
                if (intVec.InBounds())
                {
                    if (Building_CrashedShipPartCopy.reachableCells.Contains(intVec))
                    {
                        float num2 = this.snowNoise.GetValue(intVec);
                        num2 += 1f;
                        num2 *= 0.5f;
                        if (num2 < 0.1f)
                        {
                            num2 = 0.1f;
                        }
                        if (Find.SnowGrid.GetDepth(intVec) <= num2)
                        {
                            float lengthHorizontal = (intVec - base.Position).LengthHorizontal;
                            float num3             = 1f - lengthHorizontal / this.snowRadius;
                            Find.SnowGrid.AddDepth(intVec, num3 * 0.12f * num2);
                        }
                    }
                }
            }
        }