Advertisement
Guest User

Untitled

a guest
Jul 29th, 2014
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void Fill(int x, int y, int fillId, int replaceId = 0)
  2. {
  3.     List<Point> points = new List<Point>(); // Point is from System.Drawing & lets you remember x's/y's.
  4.     Block[, ,] map;                         // replace with whatever method you use to read the map.
  5.                                             //   It needs to contain all the blocks in the room.
  6.                                             //   I use a three-dimensional array of Block objects.
  7.                                             //   Blocks are found by typing Map[x,y,layer].
  8.  
  9.     do // using a do-while instead of a while because the list starts at 0 and it wouldn't execute.
  10.     {
  11.         // if you're on the replace id, replace it and remove it from the list.
  12.         if (map[x, y, 0].Id == replaceId)
  13.         {
  14.             Build(fillId, x, y);
  15.    
  16.             map[x, y, 0] = new Block(id, x, y);
  17.  
  18.                
  19.             for (int ind = 0; ind < points.Count; ind++)
  20.             if (points[ind].X == x && points[ind].Y == y)
  21.                 points.RemoveAt(ind);
  22.         }
  23.         else // If you're not on the replace id, you're on the border. Select a random point and remove it from the list.
  24.         {
  25.             var RanPoint = points[MyRandom.Next(points.Count)]; // Do not use `new Random` here. Do it outside the loop.
  26.    
  27.             x = RanPoint.X;
  28.  
  29.             y = RanPoint.Y;
  30.  
  31.             for (int ind = 0; ind < points.Count; ind++)
  32.                 if (points[ind].X == x && points[ind].Y == y)
  33.                     points.RemoveAt(ind);
  34.             }
  35.         }
  36.  
  37.         // Where there is replaceId, draw and add a point. Update the map and it becomes exponentially faster.
  38.         UpdatePoint(x + 1, y, fillId, replaceId, map, points);
  39.         UpdatePoint(x - 1, y, fillId, replaceId, map, points);
  40.         UpdatePoint(x, y + 1, fillId, replaceId, map, points);
  41.         UpdatePoint(x, y - 1, fillId, replaceId, map, points);
  42.  
  43.         // You just added a few points to the list, generally 2-3.
  44.         // Return, select a random one, delete it, and repeat until there are none left.
  45.         // What you end up with in the List<Point> points is all the points that have not been checked, aka the ones on the front line.
  46.     } while (points.Count > 0); // Once the list is empty, it is done.
  47. } // end Fill function
  48.  
  49. void UpdatePoint(int x, int y, int fillId, int replaceId, out Block[, ,] map, out List<Point> points)
  50. {
  51.     if (map[x, y, 0].Id == replaceId)
  52.     {
  53.         Build(fillId, x, y);
  54.  
  55.         map[x, y, 0] = new Block(id, x, y);
  56.  
  57.         points.Add(new Point(x, y));
  58.     }
  59. }
  60.  
  61. void Build(int id, int x, int y)
  62. {
  63.     _
  64.     // Insert whatever build method you use.
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement