From fd4d031ab5b31763d376c663cc049f62ee389243 Mon Sep 17 00:00:00 2001 From: nsensfel Date: Mon, 26 Feb 2018 13:52:29 +0100 Subject: Got it to run, at last. --- src/battlemap/movement.erl | 68 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/battlemap/movement.erl (limited to 'src/battlemap/movement.erl') diff --git a/src/battlemap/movement.erl b/src/battlemap/movement.erl new file mode 100644 index 0000000..7fc5db1 --- /dev/null +++ b/src/battlemap/movement.erl @@ -0,0 +1,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)). -- cgit v1.2.3-70-g09d2