summaryrefslogtreecommitdiff
path: root/src/pathfinding.adb
blob: 1435ecf16214f06721907d91953bf0f756e3d759 (plain)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193


with

    Things,
    Ada.Containers.Ordered_Sets,
    Ada.Containers.Ordered_Maps;


package body Pathfinding is


    type Node is record
        X, Y : Integer;
    end record;

    function "<" (A, B : in Node) return Boolean is
    begin
        return A.X < B.X or (A.X = B.X and A.Y < B.Y);
    end "<";

    package Node_Sets is new Ada.Containers.Ordered_Sets
        (Element_Type => Node);
    package Node_To_Node_Maps is new Ada.Containers.Ordered_Maps
        (Key_Type => Node, Element_Type => Node);




    subtype G_Score is Integer;
    subtype F_Score is Integer;

    package G_Score_Maps is new Ada.Containers.Ordered_Maps
        (Key_Type => Node, Element_Type => G_Score);
    package F_Score_Maps is new Ada.Containers.Ordered_Maps
        (Key_Type => Node, Element_Type => F_Score);

    G_Scores : G_Score_Maps.Map := G_Score_Maps.Empty_Map;
    F_Scores : F_Score_Maps.Map := F_Score_Maps.Empty_Map;




    function F_Min
           (N_Set : in Node_Sets.Set)
        return Node
    is
        Result : Node := N_Set.First_Element;
    begin
        for N of N_Set loop
            if F_Scores.Contains (N) then
                if  not F_Scores.Contains (Result) or else
                    F_Scores.Element (N) < F_Scores.Element (Result)
                then
                    Result := N;
                end if;
            end if;
        end loop;
        return Result;
    end F_Min;




    function Reconstruct_Path
           (Came_From : in Node_To_Node_Maps.Map;
            Current   : in Node)
        return Moves.Path
    is
        Result : Moves.Path := Moves.Empty_Path;
        Working : Node := Current;
        Next : Node;
    begin
        while Came_From.Contains (Working) loop
            Next := Came_From.Element (Working);
            Result.Prefix
                ((Delta_X => Working.X - Next.X, Delta_Y => Working.Y - Next.Y, Push => False));
            Working := Next;
        end loop;
        return Result;
    end Reconstruct_Path;




    function Heuristic
           (A, B : in Node)
        return Integer is
    begin
        return abs (A.X - B.X) + abs (A.Y - B.Y);
    end Heuristic;




    function Neighbours
           (My_Grid : in Grids.Grid;
            Current : in Node)
        return Node_Sets.Set
    is
        use type Things.Thing;

        function Valid (X, Y : in Integer) return Boolean is
        begin
            return My_Grid.In_Bounds (X, Y) and then
                My_Grid.Get_Square (X, Y).Is_Walkable and then
                My_Grid.Get_Square (X, Y).Get_Contents = Things.Nothing;
        end Valid;

        Result : Node_Sets.Set := Node_Sets.Empty_Set;
    begin
        if Valid (Current.X - 1, Current.Y) then
            Result.Insert ((Current.X - 1, Current.Y));
        end if;
        if Valid (Current.X + 1, Current.Y) then
            Result.Insert ((Current.X + 1, Current.Y));
        end if;
        if Valid (Current.X, Current.Y - 1) then
            Result.Insert ((Current.X, Current.Y - 1));
        end if;
        if Valid (Current.X, Current.Y + 1) then
            Result.Insert ((Current.X, Current.Y + 1));
        end if;
        return Result;
    end Neighbours;




    function A_Star
           (My_Grid : in Grids.Grid;
            SX, SY  : in Integer;
            FX, FY  : in Integer)
        return Moves.Path
    is
        use type Node_Sets.Set;

        Start   : Node := (X => SX, Y => SY);
        Goal    : Node := (X => FX, Y => FY);
        Current : Node;

        Closed_Set : Node_Sets.Set := Node_Sets.Empty_Set;
        Open_Set   : Node_Sets.Set := Node_Sets.To_Set (Start);

        Came_From : Node_To_Node_Maps.Map := Node_To_Node_Maps.Empty_Map;
    begin
        G_Scores := G_Score_Maps.Empty_Map;
        G_Scores.Insert (Start, 0);
        F_Scores := F_Score_Maps.Empty_Map;
        F_Scores.Insert (Start, Heuristic (Start, Goal));

        --  This is a textbook implementation of A* search.
        while Open_Set /= Node_Sets.Empty_Set loop
            Current := F_Min (Open_Set);
            if Current = Goal then
                return Reconstruct_Path (Came_From, Current);
            end if;
            Open_Set.Delete (Current);
            Closed_Set.Insert (Current);
            for N of Neighbours (My_Grid, Current) loop
                if not Closed_Set.Contains (N) then
                    if not Open_Set.Contains (N) then
                        Open_Set.Insert (N);
                    end if;
                    declare
                        New_G_Score : G_Score := G_Scores.Element (Current) + 1;
                        New_F_Score : F_Score := New_G_Score + Heuristic (N, Goal);
                    begin
                        if  not G_Scores.Contains (N) or else
                            New_G_Score < G_Scores.Element (N)
                        then
                            Came_From.Insert (N, Current);
                            G_Scores.Insert (N, New_G_Score);
                            F_Scores.Insert (N, New_F_Score);
                        end if;
                    end;
                end if;
            end loop;
        end loop;

        --  And this is a modification to get as close as possible
        --  if the goal is out of reach.
        for N of Closed_Set loop
            if Heuristic (N, Goal) < Heuristic (Current, Goal) then
                Current := N;
            end if;
        end loop;
        return Reconstruct_Path (Came_From, Current);
    end A_Star;


end Pathfinding;