Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///movement_range(origin_gridspacer, movement_range, unit);
- wipe_nodes();
- //declare relevant variables from arguments
- start = argument0;
- range = argument1;
- unit = argument2;
- //create data structures
- open = ds_priority_create();
- closed = ds_list_create();
- neighbors = ds_list_create();
- //add starting node to the open list
- ds_priority_add(open,start,0);
- //while open queue is NOT empty...
- //repeat the following until ALL nodes have been looked at
- while!(ds_priority_empty(open))
- {
- //remove node with lowest G score from open
- current_gridspacer = ds_priority_delete_min(open);
- //add current_gridspacer to closed list
- ds_list_add(closed, current_gridspacer);
- //add neighbors to ds_list neighbors
- if unit.walkable[current_gridspacer.n+1,current_gridspacer.m]
- {
- ds_list_add(neighbors,instance_nearest(global.handler.gridspace_x[current_gridspacer.n + 1],global.handler.gridspace_y[current_gridspacer.m],obj_gridspacer));
- }
- if unit.walkable[current_gridspacer.n-1,current_gridspacer.m]
- {
- ds_list_add(neighbors,instance_nearest(global.handler.gridspace_x[current_gridspacer.n - 1],global.handler.gridspace_y[current_gridspacer.m],obj_gridspacer));
- }
- if unit.walkable[current_gridspacer.n,current_gridspacer.m-1]
- {
- ds_list_add(neighbors,instance_nearest(global.handler.gridspace_x[current_gridspacer.n],global.handler.gridspace_y[current_gridspacer.m-1],obj_gridspacer));
- }
- if unit.walkable[current_gridspacer.n,current_gridspacer.m+1]
- {
- ds_list_add(neighbors,instance_nearest(global.handler.gridspace_x[current_gridspacer.n],global.handler.gridspace_y[current_gridspacer.m+1],obj_gridspacer));
- }
- //step through all of current_gridspacer's neighbors
- for (ii = 0; ii < ds_list_size(neighbors); ii ++)
- {
- //store current neighbor in neighbor variable
- neighbor = ds_list_find_value(neighbors, ii);
- //add neighbor to open list
- //note: must not be ALREADY on closed list
- if (ds_list_find_index(closed,neighbor) < 0)
- {
- costMod = 1;
- //only calculate a new g score if it hasnt already been calculated
- if (ds_priority_find_priority(open, neighbor) == 0 or ds_priority_find_priority(open, neighbor) == undefined)
- {
- //give neighbor the appropriate parent
- neighbor.parent = current_gridspacer;
- //calculate G store of nieghbor using costmod
- neighbor.G = current_gridspacer.G + (neighbor.cost * costMod);
- //add neighbor to the open list so it can be checked out too!
- ds_priority_add(open, neighbor, neighbor.G);
- }
- //ELSE!
- //If neighbor's score has ALREADY been calculated for the open list!
- else
- {
- //figure outif the neighbor's score would be LOWER if found from the current node
- costMod = 1;
- tempG = current_gridspacer.G + (nieghbor.cost * costMod);
- //check if G score would be lower
- if (tempG < neighbor.G)
- {
- neighbor.parent = current_gridspacer;
- neighbor.G = tempG;
- ds_priority_change_priority(open, neighbor, neighbor.G);
- }
- }
- }
- }
- //empty the neighbors list
- ds_list_clear(neighbors);
- }
- //round down all G scores!
- with(obj_gridspacer)
- {
- G = floor(G);
- }
- //destroy the neighbors list
- ds_list_destroy(neighbors);
- //destroy the open list SUPER IMPORTANT!!! No leaks here!
- ds_priority_destroy(open);
- //destroy the closed list.
- ds_list_destroy(closed);
Advertisement
Add Comment
Please, Sign In to add comment