Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// @description Returns an array of positions
- /// @param target_x
- /// @param target_y
- global._astar_end_x = argument0;
- global._astar_end_y = argument1;
- var _time = get_timer();
- //Create intial cell
- var _first_cell = array_create(astar_struct_size);
- _first_cell[@ astar_x] = global._astar_start_x;
- _first_cell[@ astar_y] = global._astar_start_y;
- _first_cell[@ astar_h] = 9999;
- _first_cell[@ astar_open] = true;
- //Add initial cell to open queue
- global._astar_value_grid[# global._astar_start_x, global._astar_start_y] = _first_cell;
- ds_priority_add(global._astar_open,_first_cell,_first_cell[@ astar_f]);
- while(ds_priority_size(global._astar_open)){
- //Get first celland remove from open list
- var _cell = ds_priority_delete_min(global._astar_open);
- _cell[@ astar_closed] = true;
- _cell[@ astar_open] = false;
- //Mark cell as closed
- global._astar_value_grid[# _cell[astar_x],_cell[astar_y]] = _cell;
- if(_cell[astar_x] == global._astar_end_x && _cell[astar_y] == global._astar_end_y){
- //Path has been found - return path
- var _path = ds_create(ds_type_list);
- var _origin = vector2(global._astar_start_x,global._astar_start_y);
- var _child_position = vector2(_cell[astar_x],_cell[astar_y]);
- while(!vector2_equals(_child_position,_origin)){
- var _child = global._astar_value_grid[# _child_position[v2x], _child_position[v2y]];
- ds_list_add(_path,_child_position);
- _child_position = vector2(_child[astar_parent_x],_child[astar_parent_y]);
- }
- if(!ds_list_size(_path)){
- return _path;
- }
- ds_list_add(_path,_origin);
- ds_list_flip(_path);
- return _path;
- }
- //Check adjacent
- var _i=-1;
- repeat(array_length_1d(global._astar_adjacent)){
- ++_i;
- var _adjacent_data = global._astar_adjacent[_i],
- _position = vector2_add(vector2(_cell[astar_x],_cell[astar_y]),_adjacent_data);
- //Supress out of bound errors
- if(!ds_grid_within_bounds(global._astar_grid, _position[v2x], _position[v2y])){
- continue;
- }
- //Look out for non passable cells
- var _passable_value = global._astar_grid[# _position[v2x], _position[v2y]],
- _can_pass = (global._astar_custom_passable_script) ?
- script_execute(global._astar_passable_script,_passable_value) :
- astar_is_passable(_passable_value);
- //Ignore if cannot pass
- if(!_can_pass){
- continue;
- }
- var _adjacent_cell = global._astar_value_grid[# _position[v2x],_position[v2y]],
- _update = false,
- _create = !is_array(_adjacent_cell),
- _g = _cell[astar_g]+_adjacent_data[2];
- if(_create){
- _adjacent_cell = array_create(astar_struct_size);
- _adjacent_cell[astar_x] = _position[v2x];
- _adjacent_cell[astar_y] = _position[v2y];
- _update = true;
- }else{
- //Don't check closed cells
- if(_adjacent_cell[astar_closed]){
- continue;
- }
- _update = (_g < _adjacent_cell[astar_g]);
- }
- if(_update){
- //Update cell values
- _adjacent_cell[@ astar_h] = abs(_position[v2x] - global._astar_end_x) + abs(_position[v2y] - abs(global._astar_end_y));
- _adjacent_cell[@ astar_g] = _g;
- _adjacent_cell[@ astar_f] = _adjacent_cell[astar_h] + _adjacent_cell[astar_g];
- _adjacent_cell[@ astar_parent_x] = _cell[astar_x];
- _adjacent_cell[@ astar_parent_y] = _cell[astar_y];
- if(_create){
- //Add to open queue
- _adjacent_cell[@ astar_open] = true;
- ds_priority_add(global._astar_open,_adjacent_cell,_adjacent_cell[astar_f]);
- global._astar_value_grid[# _position[v2x],_position[v2y]] = _adjacent_cell;
- }
- }
- }
- }
- return undefined;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement