Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Fill(int x, int y, int fillId, int replaceId = 0)
- {
- List<Point> points = new List<Point>(); // Point is from System.Drawing & lets you remember x's/y's.
- Block[, ,] map; // replace with whatever method you use to read the map.
- // It needs to contain all the blocks in the room.
- // I use a three-dimensional array of Block objects.
- // Blocks are found by typing Map[x,y,layer].
- do // using a do-while instead of a while because the list starts at 0 and it wouldn't execute.
- {
- // if you're on the replace id, replace it and remove it from the list.
- if (map[x, y, 0].Id == replaceId)
- {
- Build(fillId, x, y);
- map[x, y, 0] = new Block(id, x, y);
- for (int ind = 0; ind < points.Count; ind++)
- if (points[ind].X == x && points[ind].Y == y)
- points.RemoveAt(ind);
- }
- else // If you're not on the replace id, you're on the border. Select a random point and remove it from the list.
- {
- var RanPoint = points[MyRandom.Next(points.Count)]; // Do not use `new Random` here. Do it outside the loop.
- x = RanPoint.X;
- y = RanPoint.Y;
- for (int ind = 0; ind < points.Count; ind++)
- if (points[ind].X == x && points[ind].Y == y)
- points.RemoveAt(ind);
- }
- }
- // Where there is replaceId, draw and add a point. Update the map and it becomes exponentially faster.
- UpdatePoint(x + 1, y, fillId, replaceId, map, points);
- UpdatePoint(x - 1, y, fillId, replaceId, map, points);
- UpdatePoint(x, y + 1, fillId, replaceId, map, points);
- UpdatePoint(x, y - 1, fillId, replaceId, map, points);
- // You just added a few points to the list, generally 2-3.
- // Return, select a random one, delete it, and repeat until there are none left.
- // 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.
- } while (points.Count > 0); // Once the list is empty, it is done.
- } // end Fill function
- void UpdatePoint(int x, int y, int fillId, int replaceId, out Block[, ,] map, out List<Point> points)
- {
- if (map[x, y, 0].Id == replaceId)
- {
- Build(fillId, x, y);
- map[x, y, 0] = new Block(id, x, y);
- points.Add(new Point(x, y));
- }
- }
- void Build(int id, int x, int y)
- {
- _
- // Insert whatever build method you use.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement