-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPathfinding.cs
126 lines (107 loc) · 3.51 KB
/
Pathfinding.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using System.Collections.Generic;
namespace ProcGenTiles
{
public class Pathfinding
{
HashSet<(int x, int y)> visited = new HashSet<(int x, int y)>();
Queue<(int x, int y)> queue = new Queue<(int x, int y)>();
Map Map;
public Pathfinding(Map map)
{
Map = map;
}
public void LandWaterFloodfill((int x, int y) start)
{
queue.Clear();
queue.Enqueue(start);
visited.Clear();
visited.Add(start);
while (queue.Count > 0)
{
(int x, int y) coords = queue.Dequeue();
Tile tile = Map.GetTile(coords); //Can always assume value is not null due to AddNeighborsToQueue
//Check the elevation layer, if it doesn't exist exit with an error
if (!tile.ValuesHere.ContainsKey("Elevation"))
{
throw new InvalidOperationException("Cannot floodfill without elevation data");
}
if (tile.ValuesHere["Elevation"] >= 0)
tile.ValuesHere.Add("Land", 1); //Heck this only takes floats so we'll use positive 1 for true and 0 for false
else
tile.ValuesHere.Add("Land", 0);
AddFourNeighbors(coords.x, coords.y, queue);
}
}
public void MarkAllRegions()
{
//Get a list of all tiles
List<(int x, int y)> values = new List<(int x, int y)>();
for (int x = 0; x < Map.Width; x++)
{
for (int y = 0; y < Map.Height; y++)
{
values.Add((x, y));
}
}
//Move first tile from list to frontier
Queue<(int x, int y)> frontier = new Queue<(int x, int y)>();
visited.Clear();
int region = 0; //Track which region we're marking
//Add neighbors to frontier if Land values match
while (values.Count > 0)
{ //Loop for all tiles
frontier.Enqueue(values[0]);
visited.Add(values[0]);
Tile compare = Map.GetTile(values[0]); //For checking the Land value
while (frontier.Count > 0)
{
(int x, int y) coords = frontier.Dequeue();
Tile found = Map.GetTile(coords);
if (found.ValuesHere["Land"] == compare.ValuesHere["Land"])
{ //The neighbor matches the start value so assign them the same region
found.ValuesHere.TryAdd("Region", region);
values.Remove(coords); //Delete from values if region is marked
AddFourNeighbors(coords.x, coords.y, frontier);
}
}
region++; //On to the next one if the frontier ran out
visited.Clear();
}
//Mark until frontier is empty, removing values from the list
//increment region and pop first item from list until list is empty
}
public void BFS((int x, int y) start)
{
visited.Add(start);
queue.Enqueue(start);
while (queue.Count > 0)
{
var current = queue.Dequeue();
// Add neighboring tiles to the queue if not visited for 4 dir pathfinding: diamonds
AddFourNeighbors(current.x, current.y, queue);
//Thinking about running a Func<> through the params to determine what to do with the found tiles
}
}
private void AddFourNeighbors(int x, int y, Queue<(int x, int y)> q)
{
AddNeighborToQueue(x - 1, y, q);
AddNeighborToQueue(x + 1, y, q);
AddNeighborToQueue(x, y - 1, q);
AddNeighborToQueue(x, y + 1, q);
}
private void AddEightNeighbors(int x, int y, Queue<(int x, int y)> q)
{ //Stubbed just in case
AddFourNeighbors(x, y, q);
AddNeighborToQueue(x - 1, y - 1, q);
AddNeighborToQueue(x + 1, y - 1, q);
AddNeighborToQueue(x - 1, y + 1, q);
AddNeighborToQueue(x + 1, y + 1, q);
}
private void AddNeighborToQueue(int x, int y, Queue<(int x, int y)> q)
{
if (Map.IsValidTilePosition(x, y) && !visited.Contains((x, y)))
q.Enqueue((x, y));
visited.Add((x, y));
}
}
}