forked from SunnyValleyStudio/SimpleCityBuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grid.cs
211 lines (192 loc) · 5.09 KB
/
Grid.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using System;
using System.Collections.Generic;
/// <summary>
/// Source https://github.com/lordjesus/Packt-Introduction-to-graph-algorithms-for-game-developers
/// </summary>
public class Point
{
public int X { get; set; }
public int Y { get; set; }
public Point(int x, int y)
{
this.X = x;
this.Y = y;
}
public override bool Equals(object obj)
{
if (obj == null)
{
return false;
}
if (obj is Point)
{
Point p = obj as Point;
return this.X == p.X && this.Y == p.Y;
}
return false;
}
public override int GetHashCode()
{
unchecked
{
int hash = 6949;
hash = hash * 7907 + X.GetHashCode();
hash = hash * 7907 + Y.GetHashCode();
return hash;
}
}
public override string ToString()
{
return "P(" + this.X + ", " + this.Y + ")";
}
}
public enum CellType
{
Empty,
Road,
Structure,
SpecialStructure,
None
}
public class Grid
{
private CellType[,] _grid;
private int _width;
public int Width { get { return _width; } }
private int _height;
public int Height { get { return _height; } }
private List<Point> _roadList = new List<Point>();
private List<Point> _specialStructure = new List<Point>();
public Grid(int width, int height)
{
_width = width;
_height = height;
_grid = new CellType[width, height];
}
// Adding index operator to our Grid class so that we can use grid[][] to access specific cell from our grid.
public CellType this[int i, int j]
{
get
{
return _grid[i, j];
}
set
{
if (value == CellType.Road)
{
_roadList.Add(new Point(i, j));
}
else
{
_roadList.Remove(new Point(i, j));
}
if (value == CellType.SpecialStructure)
{
_specialStructure.Add(new Point(i, j));
}
else
{
_specialStructure.Remove(new Point(i, j));
}
_grid[i, j] = value;
}
}
public static bool IsCellWakable(CellType cellType, bool aiAgent = false)
{
if (aiAgent)
{
return cellType == CellType.Road;
}
return cellType == CellType.Empty || cellType == CellType.Road;
}
public Point GetRandomRoadPoint()
{
Random rand = new Random();
return _roadList[rand.Next(0, _roadList.Count - 1)];
}
public Point GetRandomSpecialStructurePoint()
{
Random rand = new Random();
return _roadList[rand.Next(0, _roadList.Count - 1)];
}
public List<Point> GetAdjacentCells(Point cell, bool isAgent)
{
return GetWakableAdjacentCells((int)cell.X, (int)cell.Y, isAgent);
}
public float GetCostOfEnteringCell(Point cell)
{
return 1;
}
public List<Point> GetAllAdjacentCells(int x, int y)
{
List<Point> adjacentCells = new List<Point>();
if (x > 0)
{
adjacentCells.Add(new Point(x - 1, y));
}
if (x < _width - 1)
{
adjacentCells.Add(new Point(x + 1, y));
}
if (y > 0)
{
adjacentCells.Add(new Point(x, y - 1));
}
if (y < _height - 1)
{
adjacentCells.Add(new Point(x, y + 1));
}
return adjacentCells;
}
public List<Point> GetWakableAdjacentCells(int x, int y, bool isAgent)
{
List<Point> adjacentCells = GetAllAdjacentCells(x, y);
for (int i = adjacentCells.Count - 1; i >= 0; i--)
{
if(IsCellWakable(_grid[adjacentCells[i].X, adjacentCells[i].Y], isAgent)==false)
{
adjacentCells.RemoveAt(i);
}
}
return adjacentCells;
}
public List<Point> GetAdjacentCellsOfType(int x, int y, CellType type)
{
List<Point> adjacentCells = GetAllAdjacentCells(x, y);
for (int i = adjacentCells.Count - 1; i >= 0; i--)
{
if (_grid[adjacentCells[i].X, adjacentCells[i].Y] != type)
{
adjacentCells.RemoveAt(i);
}
}
return adjacentCells;
}
/// <summary>
/// Returns array [Left neighbour, Top neighbour, Right neighbour, Down neighbour]
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public CellType[] GetAllAdjacentCellTypes(int x, int y)
{
CellType[] neighbours = { CellType.None, CellType.None, CellType.None, CellType.None };
if (x > 0)
{
neighbours[0] = _grid[x - 1, y];
}
if (x < _width - 1)
{
neighbours[2] = _grid[x + 1, y];
}
if (y > 0)
{
neighbours[3] = _grid[x, y - 1];
}
if (y < _height - 1)
{
neighbours[1] = _grid[x, y + 1];
}
return neighbours;
}
}