-
Notifications
You must be signed in to change notification settings - Fork 0
/
CellGrid.pde
207 lines (167 loc) · 5.73 KB
/
CellGrid.pde
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
import java.util.Map;
class CellGrid {
/// How many pixels each cell should fill on the screen
private int m_CellPxSize = 50;
// Stores how many colums and rows there are in the cell m_Grid
private int m_Cols, m_Rows;
// Store 2D array representing the cell m_Grid
private Cell[][] m_Grid;
// Millis it takes for a generation to be generated
private float m_GenerationTime = 0f;
private int m_GenerationCount = 0;
// Millis it takes for a generation to be generated
private float m_DrawTime = 0f;
CellGrid() {
m_Cols = width / m_CellPxSize;
m_Rows = height / m_CellPxSize;
m_Grid = new Cell[m_Rows][m_Cols];
// Initialize cell m_Grid
init();
}
/// Translates the pixel coordinates to array indices and return the cell. If its out of bounds return null
public Cell getCellAt(int xPos, int yPos)
{
// colums is y cords and rows is x cords
int row = yPos / m_CellPxSize;
int col = xPos / m_CellPxSize;
if (row >= m_Rows || col >= m_Cols)
return null;
return m_Grid[row][col];
}
public void setCellAt(Cell cell, int xPos, int yPos)
{
// colums is y cords and rows is x cords
int row = yPos / m_CellPxSize;
int col = xPos / m_CellPxSize;
if (row >= m_Rows || col >= m_Cols)
return;
m_Grid[row][col] = cell;
}
// Translates world/pixel position to index into the cell grid
public ZVector translateWS2Idx(ZVector WP)
{
return new ZVector((int) WP.y / m_CellPxSize, (int) WP.x / m_CellPxSize);
}
// Translates idx postiion to world/pixel position. inverse of translateWS2Idx.
public ZVector translateIdx2WS(ZVector Idx)
{
return new ZVector(Idx.y * m_CellPxSize, Idx.x * m_CellPxSize);
}
public ZVector getWSCenterOfCell(ZVector cellWPSosition)
{
return cellWPSosition.add(new ZVector(m_CellPxSize / 2, m_CellPxSize / 2));
}
float getGenTime() { return m_GenerationTime; }
float getDrawTime() { return m_DrawTime; }
int getGenCount() { return m_GenerationCount; }
int getCellSize() { return m_CellPxSize; }
void init()
{
for (int i = 0; i < m_Rows; i++)
{
for (int j = 0; j < m_Cols; j++)
{
// 1 out of 50 chance of grass cell at start
int rand = int(random(0, 50));
if (rand == 0)
m_Grid[i][j] = new GrassCell();
else if (rand == 1)
m_Grid[i][j] = new WaterCell();
else
m_Grid[i][j] = new DirtCell();
}
}
}
/// Generates a new generation of cells based on the current
void generate()
{
float startTime = millis();
Cell[][] nextGrid = new Cell[m_Rows][m_Cols];
for (int row = 0; row < m_Rows; row++)
{
for (int col = 0; col < m_Cols; col++)
{
Cell currentCell = m_Grid[row][col];
int depth = currentCell.getNeightbourDepth();
HashMap<CellType, Integer> neighbours = getNeighbours(row, col, depth);
// Subtract cell's own value from neighbours
int quantity = neighbours.get(m_Grid[row][col].getCellType());
neighbours.put(m_Grid[row][col].getCellType(), --quantity);
m_Grid[row][col].increaseLifeTime();
// Rules of Life
Cell newCell = m_Grid[row][col].updateState(neighbours);
if (newCell == null) // Same cell survived: Stasis
nextGrid[row][col] = m_Grid[row][col];
else
nextGrid[row][col] = newCell;
}
}
m_Grid = nextGrid;
m_GenerationTime = millis() - startTime;
m_GenerationCount++;
}
/// Display the cell m_Grid
void display()
{
float startTime = millis();
for (int i = 0; i < m_Rows; i++)
{
for (int j = 0; j < m_Cols; j++)
{
// x-cord is column (j) and y-cord is row (i)
m_Grid[i][j].display(j, i, m_CellPxSize);
}
}
m_DrawTime = millis() - startTime;
}
void singleStep()
{
generate();
}
// Gets a hashmap which maps a quantity of cells to a specific cell type from a center position and a depth from that center
HashMap<CellType, Integer> getNeighbours(int row, int col, int depth)
{
HashMap<CellType, Integer> neighbours = new HashMap<CellType, Integer>();
// Generate hashmap that maps cell types to the amount of those spotted as neighbours
for (int i = -depth; i <= depth; i++)
{
for (int j = -depth; j <= depth; j++)
{
int neighbourRow = i + row;
int neighbourCol = j + col;
if (neighbourRow < 0 || neighbourRow >= m_Rows)
continue;
if (neighbourCol < 0 || neighbourCol >= m_Cols)
continue;
Cell cell = m_Grid[neighbourRow][neighbourCol];
Integer qauntity = neighbours.get(cell.getCellType());
if (qauntity != null)
neighbours.put(cell.getCellType(), ++qauntity);
else
neighbours.put(cell.getCellType(), 1);
}
}
return neighbours;
}
// Gets a list tuples which map cell types to the idx position of that celltype in a search area.
ArrayList<Tuple<CellType, ZVector>> getNeighbourLocations(int row, int col, int depth)
{
ArrayList<Tuple<CellType, ZVector>> neighbours = new ArrayList<Tuple<CellType, ZVector>>();
// Generate hashmap that maps cell types to the amount of those spotted as neighbours
for (int i = -depth; i <= depth; i++)
{
for (int j = -depth; j <= depth; j++)
{
int neighbourRow = i + row;
int neighbourCol = j + col;
if (neighbourRow < 0 || neighbourRow >= m_Rows)
continue;
if (neighbourCol < 0 || neighbourCol >= m_Cols)
continue;
Cell cell = m_Grid[neighbourRow][neighbourCol];
neighbours.add(new Tuple<CellType, ZVector>(cell.getCellType(), new ZVector(neighbourRow, neighbourCol)));
}
}
return neighbours;
}
}