Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ============================ PATH FINDING ==========================
- int StepsFrom_To(int start_y, int start_x, int dest_y, int dest_x) {
- int count = 0;
- for (int y = start_y; y < dest_y; y++) { count++; }
- for (int x = start_x; x < dest_x; x++) { count++; }
- return count;
- }
- void FindPathFrom_To(int start_y, int start_x, int dest_y, int dest_x) {
- // create nodes for start and desticaion
- node start_node(start_y,start_x);
- node dest_node(dest_y,dest_x);
- node old_current(-1,-1);
- // open current id
- node current(start_y,start_x);
- current.state = OPEN;
- // node list
- node nodes[MAPY*MAPX];
- bool searching = true;
- // start searching
- int steps = 0;
- while (searching) {
- // Get the node off the open list with the lowest f and make it current node
- int lowest_f = 9999; // just so it works :)
- for (int i = 0; i < MAPY*MAPX; i++) {
- if ( (nodes[i].f < lowest_f) && (nodes[i].state == OPEN) ) {
- lowest_f = nodes[i].f;
- tiles[nodes[i].y][nodes[i].x].c[0] = 'x';
- old_current.set(current.y,current.x);
- current.set(nodes[i].y,nodes[i].x);
- } else {nodes[i].state = CLOSED; }
- }
- // check if reached destination
- if ( (current.x == dest_node.x) && (current.y == dest_node.y) ) { searching = false; }
- // check if node has not changed (meaning there is no where to go)
- if ( (current.x == old_current.x) && (current.x == old_current.x) ) { break; }
- // Generate each state node_successor that can come after node_current for each node_successor of node_current
- if (tiles[current.y+1][current.x].obj_solid == false) { // check below
- nodes[steps].set(current.y+1, current.x); // set node cords
- nodes[steps].state = OPEN; // add to open list
- nodes[steps].h = StepsFrom_To(current.y+1,current.x,dest_node.y,dest_node.x) * 10; // calc histerics
- nodes[steps].g = StepsFrom_To(current.y+1,current.x,start_node.y,start_node.x) + steps; // calc move cost
- nodes[steps].f = nodes[steps].h + nodes[steps].g;
- steps++;
- }
- if (tiles[current.y-1][current.x].obj_solid == false) { // check above
- nodes[steps].set(current.y-1, current.x); // set node cords
- nodes[steps].state = OPEN; // add to open list
- nodes[steps].h = StepsFrom_To(current.y-1,current.x,dest_node.y,dest_node.x) * 10; // calc histerics
- nodes[steps].g = StepsFrom_To(current.y-1,current.x,start_node.y,start_node.x) + steps; // calc move cost
- nodes[steps].f = nodes[steps].h + nodes[steps].g;
- steps++;
- }
- if (tiles[current.y][current.x+1].obj_solid == false) { // check right
- nodes[steps].set(current.y, current.x+1); // set node cords
- nodes[steps].state = OPEN; // add to open list
- nodes[steps].h = StepsFrom_To(current.y,current.x+1,dest_node.y,dest_node.x) * 10; // calc histerics
- nodes[steps].g = StepsFrom_To(current.y,current.x+1,start_node.y,start_node.x)+ steps; // calc move cost
- nodes[steps].f = nodes[steps].h + nodes[steps].g;
- steps++;
- }
- if (tiles[current.y][current.x-1].obj_solid == false) { // check left
- nodes[steps].set(current.y, current.x-1); // set node cords
- nodes[steps].state = OPEN; // add to open list
- nodes[steps].h = StepsFrom_To(current.y,current.x-1,dest_node.y,dest_node.x) * 10; // calc histerics
- nodes[steps].g = StepsFrom_To(current.y,current.x-1,start_node.y,start_node.x) + steps; // calc move cost
- nodes[steps].f = nodes[steps].h + nodes[steps].g;
- steps++;
- }
- current.state = CLOSED;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement