Skip to content

Commit 7bf13f5

Browse files
author
Maxime Buffa
committed
Merge branch 'formatting' into 'master'
Apply Mix Format changes See merge request maxime/move-your-cedric!2
2 parents 132f4e8 + 37d6ad8 commit 7bf13f5

File tree

8 files changed

+71
-89
lines changed

8 files changed

+71
-89
lines changed

lib/move_your_cedric/astar.ex

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -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
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
defmodule MoveYourCedric.Models.Entity do
2-
defstruct [
3-
name: nil,
4-
position: [0, 0],
5-
type: nil,
6-
status: nil,
7-
target: nil
8-
]
2+
defstruct name: nil,
3+
position: [0, 0],
4+
type: nil,
5+
status: nil,
6+
target: nil
97
end
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
defmodule MoveYourCedric.Models.Tile do
2-
defstruct [
3-
position: [0, 0],
4-
type: nil
5-
]
2+
defstruct position: [0, 0],
3+
type: nil
64
end
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
defmodule MoveYourCedric.Models.TileMap do
2-
defstruct [
3-
size: [0, 0],
4-
tiles: [],
5-
entities: []
6-
]
2+
defstruct size: [0, 0],
3+
tiles: [],
4+
entities: []
75
end

lib/move_your_cedric/workers/complex_map_generator.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ defmodule MoveYourCedric.Workers.ComplexMapGenerator do
2121
[6, 3],
2222
[6, 4],
2323
[6, 5],
24-
[6, 6],
24+
[6, 6]
2525
]
2626

2727
def tile_type_from_coords(x, y) do

lib/move_your_cedric/workers/pathfinder.ex

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ defmodule MoveYourCedric.Workers.Pathfinder do
1616
target: nil,
1717
path: nil
1818
}
19+
1920
{:ok, state}
2021
end
2122

@@ -51,11 +52,6 @@ defmodule MoveYourCedric.Workers.Pathfinder do
5152
GenServer.cast(__MODULE__, {:pick_target, position})
5253
end
5354

54-
55-
56-
57-
58-
5955
def handle_call(:get_path, _from, state) do
6056
{:reply, state.path, state}
6157
end
@@ -73,13 +69,13 @@ defmodule MoveYourCedric.Workers.Pathfinder do
7369
end
7470

7571
def handle_cast({:set_position, position}, state) do
76-
Logger.debug("[PLAYER] Setting position to #{ inspect position }")
72+
Logger.debug("[PLAYER] Setting position to #{inspect(position)}")
7773
{:noreply, %{state | position: position}}
7874
end
7975

8076
def handle_cast({:pick_target, position}, state) do
8177
new_state = pick_target(position, state)
82-
Logger.debug("[PLAYER] Setting target to #{ inspect new_state.target }")
78+
Logger.debug("[PLAYER] Setting target to #{inspect(new_state.target)}")
8379
{:noreply, new_state}
8480
end
8581

@@ -102,31 +98,21 @@ defmodule MoveYourCedric.Workers.Pathfinder do
10298
def handle_cast(:walk_path, %{path: %{final_path: []}} = state) do
10399
Logger.debug("[PLAYER] Reached destination.")
104100

105-
state =
106-
%{state | path: nil,
107-
position: state.target,
108-
status: :idle,
109-
target: nil}
101+
state = %{state | path: nil, position: state.target, status: :idle, target: nil}
110102

111103
{:noreply, state}
112104
end
113105

114106
def handle_cast(:walk_path, %{path: %{final_path: [head | tail]}} = state) do
115-
Logger.debug("[PLAYER] Walking the path, received #{inspect head} and #{inspect tail}.")
107+
Logger.debug("[PLAYER] Walking the path, received #{inspect(head)} and #{inspect(tail)}.")
116108

117-
new_path =
118-
%{state.path | final_path: tail}
109+
new_path = %{state.path | final_path: tail}
119110

120-
state =
121-
%{state | path: new_path,
122-
position: head.position,
123-
status: :moving}
111+
state = %{state | path: new_path, position: head.position, status: :moving}
124112

125113
{:noreply, state}
126114
end
127115

128-
129-
130116
defp pick_target([tx, ty] = target, %{position: [tx, ty]} = state) do
131117
%{state | status: :idle, target: target, path: nil}
132118
end

lib/move_your_cedric_web/live/complex_map_live.ex

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ defmodule MoveYourCedricWeb.ComplexMapLive do
1212
player = tile_map.entities |> Enum.find(fn entity -> entity.type == "player" end)
1313
Pathfinder.set_position(player.position)
1414

15-
socket = assign(socket,
16-
tile_map: tile_map,
17-
path: Pathfinder.get_path(),
18-
player_position: Pathfinder.get_position(),
19-
player_target: Pathfinder.get_target()
20-
)
15+
socket =
16+
assign(socket,
17+
tile_map: tile_map,
18+
path: Pathfinder.get_path(),
19+
player_position: Pathfinder.get_position(),
20+
player_target: Pathfinder.get_target()
21+
)
22+
2123
{:ok, socket}
2224
end
2325

@@ -148,9 +150,11 @@ defmodule MoveYourCedricWeb.ComplexMapLive do
148150
tile_map.entities
149151
|> Enum.map(fn entity ->
150152
if entity.type == "player" do
151-
%{entity | position: Pathfinder.get_position(),
152-
status: Pathfinder.get_status(),
153-
target: Pathfinder.get_target()
153+
%{
154+
entity
155+
| position: Pathfinder.get_position(),
156+
status: Pathfinder.get_status(),
157+
target: Pathfinder.get_target()
154158
}
155159
else
156160
entity
@@ -173,13 +177,10 @@ defmodule MoveYourCedricWeb.ComplexMapLive do
173177
"#{x}; #{y}"
174178
end
175179

176-
177-
178180
def get_tile_node(list, position) do
179181
list |> Enum.find(fn node -> position == node.position end)
180182
end
181183

182-
183184
def tile_has_player?([x, y], [x, y]), do: true
184185
def tile_has_player?(_, _), do: false
185186

@@ -190,13 +191,15 @@ defmodule MoveYourCedricWeb.ComplexMapLive do
190191
def tile_in_open_list?(nil, _), do: false
191192
def tile_in_open_list?(%{open_list: nil}, _), do: false
192193
def tile_in_open_list?(%{open_list: []}, _), do: false
194+
193195
def tile_in_open_list?(%{open_list: open_list}, position) do
194196
open_list |> Enum.any?(fn node -> node.position == position end)
195197
end
196198

197199
def tile_in_closed_list?(nil, _), do: false
198200
def tile_in_closed_list?(%{closed_list: nil}, _), do: false
199201
def tile_in_closed_list?(%{closed_list: []}, _), do: false
202+
200203
def tile_in_closed_list?(%{closed_list: closed_list}, position) do
201204
closed_list |> Enum.any?(fn node -> node.position == position end)
202205
end

lib/move_your_cedric_web/live/map_live.ex

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ defmodule MoveYourCedricWeb.MapLive do
1212
player = tile_map.entities |> Enum.find(fn entity -> entity.type == "player" end)
1313
Pathfinder.set_position(player.position)
1414

15-
socket = assign(socket,
16-
tile_map: tile_map,
17-
path: Pathfinder.get_path(),
18-
player_position: Pathfinder.get_position(),
19-
player_target: Pathfinder.get_target()
20-
)
15+
socket =
16+
assign(socket,
17+
tile_map: tile_map,
18+
path: Pathfinder.get_path(),
19+
player_position: Pathfinder.get_position(),
20+
player_target: Pathfinder.get_target()
21+
)
22+
2123
{:ok, socket}
2224
end
2325

@@ -148,9 +150,11 @@ defmodule MoveYourCedricWeb.MapLive do
148150
tile_map.entities
149151
|> Enum.map(fn entity ->
150152
if entity.type == "player" do
151-
%{entity | position: Pathfinder.get_position(),
152-
status: Pathfinder.get_status(),
153-
target: Pathfinder.get_target()
153+
%{
154+
entity
155+
| position: Pathfinder.get_position(),
156+
status: Pathfinder.get_status(),
157+
target: Pathfinder.get_target()
154158
}
155159
else
156160
entity
@@ -173,13 +177,10 @@ defmodule MoveYourCedricWeb.MapLive do
173177
"#{x}; #{y}"
174178
end
175179

176-
177-
178180
def get_tile_node(list, position) do
179181
list |> Enum.find(fn node -> position == node.position end)
180182
end
181183

182-
183184
def tile_has_player?([x, y], [x, y]), do: true
184185
def tile_has_player?(_, _), do: false
185186

@@ -190,13 +191,15 @@ defmodule MoveYourCedricWeb.MapLive do
190191
def tile_in_open_list?(nil, _), do: false
191192
def tile_in_open_list?(%{open_list: nil}, _), do: false
192193
def tile_in_open_list?(%{open_list: []}, _), do: false
194+
193195
def tile_in_open_list?(%{open_list: open_list}, position) do
194196
open_list |> Enum.any?(fn node -> node.position == position end)
195197
end
196198

197199
def tile_in_closed_list?(nil, _), do: false
198200
def tile_in_closed_list?(%{closed_list: nil}, _), do: false
199201
def tile_in_closed_list?(%{closed_list: []}, _), do: false
202+
200203
def tile_in_closed_list?(%{closed_list: closed_list}, position) do
201204
closed_list |> Enum.any?(fn node -> node.position == position end)
202205
end

0 commit comments

Comments
 (0)