@@ -2,13 +2,11 @@ defmodule MoveYourCedric.Astar do
22 require Logger
33
44 defmodule Node do
5- defstruct [
6- position: nil ,
7- f: nil ,
8- g: nil ,
9- h: nil ,
10- parent: nil
11- ]
5+ defstruct position: nil ,
6+ f: nil ,
7+ g: nil ,
8+ h: nil ,
9+ parent: nil
1210 end
1311
1412 # G
@@ -54,7 +52,8 @@ defmodule MoveYourCedric.Astar do
5452 # Core algorithm
5553
5654 # Final handler: We got nothing to do, we got our path.
57- def build_path ( _tiles , % { path: % { final_path: final_path } } = state ) when is_nil ( final_path ) == false do
55+ def build_path ( _tiles , % { path: % { final_path: final_path } } = state )
56+ when is_nil ( final_path ) == false do
5857 Logger . debug ( "[ASTAR] Nothing to do, final path is already known." )
5958 state
6059 end
@@ -79,16 +78,14 @@ defmodule MoveYourCedric.Astar do
7978 final_path: nil
8079 }
8180
82- % { state | path: path }
81+ % { state | path: path }
8382 end
8483
8584 # Second handler: We just got the initial node in our open list.
8685 def build_path ( tiles , % { path: % { open_list: [ current ] , closed_list: closed_list } } = state ) do
87- path =
88- % { state . path | closed_list: closed_list ++ [ current ] ,
89- open_list: [ ] }
86+ path = % { state . path | closed_list: closed_list ++ [ current ] , open_list: [ ] }
9087
91- state = % { state | path: path }
88+ state = % { state | path: path }
9289
9390 if state . target == current . position do
9491 Logger . info ( "[ASTAR] Reached destination" )
@@ -105,7 +102,7 @@ defmodule MoveYourCedric.Astar do
105102 else
106103 state = % { state | path: compute_path ( current , tiles , state ) }
107104
108- Logger . info ( "[ASTAR] Added nodes to the open list: #{ inspect state . path . open_list } " )
105+ Logger . info ( "[ASTAR] Added nodes to the open list: #{ inspect ( state . path . open_list ) } " )
109106
110107 state
111108 end
@@ -116,20 +113,17 @@ defmodule MoveYourCedric.Astar do
116113 current =
117114 open_list
118115 |> Enum . sort_by ( fn node -> { node . f , node . h } end , & <= / 2 )
119- |> List . first
116+ |> List . first ( )
120117
121118 new_open_list =
122119 open_list
123120 |> Enum . reject ( fn node -> node . position == current . position end )
124121
125122 new_closed_list = closed_list ++ [ current ]
126123
127- new_path =
128- % { state . path | open_list: new_open_list ,
129- closed_list: new_closed_list }
124+ new_path = % { state . path | open_list: new_open_list , closed_list: new_closed_list }
130125
131- state =
132- % { state | path: new_path }
126+ state = % { state | path: new_path }
133127
134128 if state . target == current . position do
135129 final_path =
@@ -149,9 +143,11 @@ defmodule MoveYourCedric.Astar do
149143 end
150144 end
151145
152-
153-
154- defp compute_path ( current , tiles , % { path: % { closed_list: closed_list , open_list: open_list } } = state ) do
146+ defp compute_path (
147+ current ,
148+ tiles ,
149+ % { path: % { closed_list: closed_list , open_list: open_list } } = state
150+ ) do
155151 # We filter our neighbors: we don't want obstacles or nodes in closed_list.
156152 neighbors =
157153 neighbors_of ( tiles , current . position )
@@ -170,7 +166,7 @@ defmodule MoveYourCedric.Astar do
170166 neighbors
171167 |> Enum . map ( fn neighbor ->
172168 cost_to_enter ( current . position , neighbor . position ) +
173- manhattan_distance ( neighbor . position , state . target )
169+ manhattan_distance ( neighbor . position , state . target )
174170 end )
175171
176172 Logger . info ( "[ASTAR] Calculated shortest path available" )
@@ -215,12 +211,12 @@ defmodule MoveYourCedric.Astar do
215211 end
216212
217213 defp build_final_path ( closed_list , origin , current , path ) do
218- Logger . debug ( "[PLAYER] Building: #{ inspect current } " )
214+ Logger . debug ( "[PLAYER] Building: #{ inspect ( current ) } " )
219215
220216 parent =
221217 closed_list
222- |> Enum . filter ( fn node -> node . position == current . parent end )
223- |> List . first ( )
218+ |> Enum . filter ( fn node -> node . position == current . parent end )
219+ |> List . first ( )
224220
225221 build_final_path ( closed_list , origin , parent , [ parent , current ] ++ path )
226222 end
0 commit comments