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
|
-module(movement).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-export
(
[
cross/4,
steps_between/2
]
).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
location_after_step (Step, X, Y) ->
case Step of
<<"L">> -> {(X - 1), Y};
<<"R">> -> {(X + 1), Y};
<<"U">> -> {X, (Y - 1)};
<<"D">> -> {X, (Y + 1)}
end.
location_to_array_index (ArrayWidth, X, Y) ->
if
(X < 0) -> -1;
(Y < 0) -> -1;
(X >= ArrayWidth) -> error;
true -> ((Y * ArrayWidth) + X)
end.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
cross (_Battlemap, _ForbiddenLocations, [], Cost, X, Y) ->
{{X, Y}, Cost};
cross (Battlemap, ForbiddenLocations, [Step|NextSteps], Cost, X, Y) ->
BattlemapTiles = battlemap:get_tiles(Battlemap),
{NextX, NextY} = location_after_step(Step, X, Y),
NextTileIX =
location_to_array_index(array:size(BattlemapTiles), NextX, NextY),
NextTile = array:get(array:get(NextTileIX, BattlemapTiles)),
NextCost = (Cost + tile:get_cost(NextTile)),
IsForbidden =
array:foldl
(
fun (_IX, Location, Prev) ->
(Prev or ({NextX, NextY} == Location))
end,
ForbiddenLocations
),
IsForbidden = false,
cross(Battlemap, ForbiddenLocations, NextSteps, NextCost, NextX, NextY).
cross (Battlemap, ForbiddenLocations, Path, {X, Y}) ->
cross(Battlemap, ForbiddenLocations, Path, 0, X, Y).
steps_between ({OX, OY}, {DX, DY}) ->
(abs(DY - OY) + abs(DX - OX)).
|