/// @function scr_breadth_first(startx, starty, grid)
/// @function Does a breadth-first search across a grid and returns an array of coordinates connected to start point.
/// @param startx Starting x-position on the grid.
/// @param starty Starting y-position on the grid.
/// @param grid The ds grid to check. Script assumes clear grid cells are marked with 0 and blocked grid cells with 1.
var Start = [argument[0], argument[1]];
var Grid = argument[2];
var Width = ds_grid_width(Grid);
var Height = ds_grid_height(Grid);
var Frontier = ds_queue_create();
var Visited = ds_grid_create(Width, Height);
ds_queue_enqueue(Frontier, Start);
var Result = 0;
var Counter = 0;
while(!ds_queue_empty(Frontier)){
var Current = ds_queue_dequeue(Frontier);
var xx = Current[0];
var yy = Current[1];
// Check above.
if(yy > 0 && Visited[# xx, yy] == 0 && Grid[# xx, yy - 1] == 0){
var New = [xx, yy - 1];
ds_queue_enqueue(Frontier, New);
Visited[# xx, yy - 1] = 1;
Result[Counter++] = New;
}
// Check right.
if(xx < Width && Visited[# xx + 1, yy] == 0 && Grid[# xx + 1, yy] == 0){
var New = [xx + 1, yy];
ds_queue_enqueue(Frontier, New);
Visited[# xx + 1, yy] = 1;
Result[Counter++] = New;
}
// Check below.
if(yy < Height && Visited[# xx, yy + 1] == 0 && Grid[# xx, yy + 1] == 0){
var New = [xx, yy + 1];
ds_queue_enqueue(Frontier, New);
Visited[# xx, yy + 1] = 1;
Result[Counter++] = New;
}
// Check left.
if(xx > 0 && Visited[# xx - 1, yy] == 0 && Grid[# xx - 1, yy] == 0){
var New = [xx - 1, yy];
ds_queue_enqueue(Frontier, New);
Visited[# xx - 1, yy] = 1;
Result[Counter++] = New;
}
}
ds_grid_destroy(Visited);
return Result;