SHOW:
|
|
- or go back to the newest paste.
1 | - | int x = _, y = _; // replace _ with the start x and y. I used the player's coordinate in my own code. |
1 | + | void Fill(int x, int y, int fillId, int replaceId = 0) |
2 | - | int replaceId = _, id = _; // replaceId is the only block Id it will replace, and it will be replaced with `id`. |
2 | + | |
3 | - | List<Point> points = new List<Point>(); // Point is from System.Drawing & lets you remember x's/y's. |
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 | - | Block[, ,] tempMap; // replace with whatever method you use to read the map. |
5 | + | // It needs to contain all the blocks in the room. |
6 | - | // It needs to contain all the blocks in the room. Or, at least the ones you are using. |
6 | + | // I use a three-dimensional array of Block objects. |
7 | - | // I use a three-dimensional array of Block objects. Blocks are found by typing Map[x,y,layer]. |
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. |
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. |
11 | + | // if you're on the replace id, replace it and remove it from the list. |
12 | - | if (tempMap[x, y, 0].Id == replaceId) |
12 | + | if (map[x, y, 0].Id == replaceId) |
13 | { | |
14 | - | Build(id, x, y); |
14 | + | Build(fillId, x, y); |
15 | - | tempMap[x, y, 0] = new Block(id, x, y);; |
15 | + | |
16 | map[x, y, 0] = new Block(id, x, y); | |
17 | - | for (int ind = 0; ind < points.Count; ind++) |
17 | + | |
18 | ||
19 | for (int ind = 0; ind < points.Count; ind++) | |
20 | - | { |
20 | + | |
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 | - | else // If you're not on the replace id, you're on the border. Select a random point and remove it from the list. |
25 | + | var RanPoint = points[MyRandom.Next(points.Count)]; // Do not use `new Random` here. Do it outside the loop. |
26 | ||
27 | - | var RanPoint = points[MyRandom.Next(points.Count)]; // Do not use `new Random` here. Do it outside the loop. |
27 | + | x = RanPoint.X; |
28 | - | x = RanPoint.X; |
28 | + | |
29 | - | y = RanPoint.Y; |
29 | + | y = RanPoint.Y; |
30 | ||
31 | - | for (int ind = 0; ind < points.Count; ind++) |
31 | + | for (int ind = 0; ind < points.Count; ind++) |
32 | if (points[ind].X == x && points[ind].Y == y) | |
33 | points.RemoveAt(ind); | |
34 | - | { |
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 | - | // Move to the right. |
40 | + | UpdatePoint(x, y + 1, fillId, replaceId, map, points); |
41 | - | x++; |
41 | + | UpdatePoint(x, y - 1, fillId, replaceId, map, points); |
42 | ||
43 | - | if (tempMap[x, y, 0].Id == replaceId) // If it's on the replace id, replace it and add it to the list. |
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 | - | b.Push.Build(id, x, y); |
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 | - | tempMap[x, y, 0] = new Block(id, x, y);; |
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 | - | // Moooove to the left. |
50 | + | |
51 | - | x -= 2; |
51 | + | if (map[x, y, 0].Id == replaceId) |
52 | { | |
53 | - | if (tempMap[x, y, 0].Id == replaceId) |
53 | + | Build(fillId, x, y); |
54 | ||
55 | - | b.Push.Build(id, x, y); |
55 | + | map[x, y, 0] = new Block(id, x, y); |
56 | - | tempMap[x, y, 0] = new Block(id, x, y);; |
56 | + | |
57 | points.Add(new Point(x, y)); | |
58 | } | |
59 | } | |
60 | - | // 2 hops this time (go one space above the original) |
60 | + | |
61 | - | x++; |
61 | + | void Build(int id, int x, int y) |
62 | - | y++; |
62 | + | |
63 | _ | |
64 | - | if (tempMap[x, y, 0].Id == replaceId) |
64 | + | // Insert whatever build method you use. |
65 | } |