loading
open in
main.cpp
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
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#include <iomanip>
#include "pathfinding.hpp"

int main() {
    /**
     * DEFINE ENVIRONMENT SETTINGS
     * directions: Vectors for West, South, East, North
     * blocked_nodes: Indices of nodes that cannot be passed through
     * start_node: Starting node indice
     * end_node: Destination node indice
     * init_grid(x,y): Grid dimensions (max X and Y indices)
     */
    directions = {
        {1, 0},
        {0, 1},
        {-1, 0},
        {0, -1}
    };

   

    blocked_nodes = {4, 14, 19, 21, 24, 28, 31, 34, 36, 41, 44, 47, 52, 58, 59, 61, 62, 68, 64, 71, 72, 74, 78, 83, 84, 85, 86, 87, 88 };
    
    //blocked_nodes = {4, 14, 21, 24, 28, 31, 34, 36, 41, 44, 47, 52, 58, 61, 62, 68, 64, 71, 72, 74, 78, 82, 83, 84, 85, 86, 87, 88 };

    start_node = 81;
    end_node = 8;
    init_grid(10, 10);

    /**
     * MAIN LOGIC: Loop through open_list until empty or end_node found
     */
    Node* current;
    NodeList::iterator current_it;

    open_list.insert(grid.at(start_node)); // Begin with start node
    while(!open_list.empty()) { // Loop through open_list until exhausted
        current_it = open_list.begin();
        current = *current_it; // current_node

        /*
        for(NodeList::iterator cit = open_list.begin(); cit != open_list.end(); ++cit) {
            if((*cit)->getWeight() < current->getWeight()) {
                current = (*cit);
            }
        }
        */

        if(end_node == current->index) {
            break;
        }

        // Add current node to closed list so no other paths use this node
        closed_list.push_back(current->index);
        
        // Remove current node from open_list so it isn't processed again
        open_list.erase(current_it);

        
        for(std::vector<Vec2i>::iterator dit = directions.begin(); dit != directions.end(); ++dit) {
            if(current->x + (*dit).x >= MAP_WIDTH || current->x + (*dit).x < 0) {
                continue;
            }

            if(current->y + (*dit).y >= MAP_HEIGHT || current->y + (*dit).y < 0) {
                continue;
            }

            int index = coords_to_index(current->x + (*dit).x, current->y + (*dit).y);
          
            // Skip if node is in closed list
            std::vector<int>::iterator cit = std::find(closed_list.begin(), closed_list.end(), index);
            if(cit != closed_list.end()) {
                continue;
            }

            // Update if on openset or new node
            Node *node = grid.at(index);
            
            if(node->blocked) {
                closed_list.push_back(node->index);
                continue;
            }

            NodeList::iterator it = open_list.find(node);

            int total_weight = current->weight + 10; // Used to weed out most efficient path

            if(it == open_list.end()) {
                node->parent = current;
                node->weight = total_weight;
                open_list.insert(node);
            } else if(total_weight < node->weight) { // Haven't found a reason conditional makes this more efficient yet
                node->parent = current;
                node->weight = total_weight;
            }
        }
    }

    std::vector<int> path_indices;
    while(current != NULL) {
        path_indices.push_back(current->index);
        current = current->parent;
    }
    

    // Draw grid
    int last_y = 0;
    for(std::vector<Node*>::iterator it  = grid.begin(); it != grid.end(); it++) {
        if(last_y != (*it)->y) {
            std::cout << "\n";
            last_y = (*it)->y;
        }

        std::cout << "[";
        if((*it)->blocked) {
            std::cout << " X ";
        } else if(start_node == (*it)->index) {
            std::cout << " S ";
        } else if(end_node == (*it)->index) {
            std::cout << " E ";
        } else if(std::find(path_indices.begin(), path_indices.end(), (*it)->index) != path_indices.end()) {
            std::cout << " # ";
        } else {
            std::cout << std::setw(3) << (*it)->index;
        }
        
        std::cout << "]";
    }
    std::cout << "\n";

    return 0;

}