From 8493faa67f71b3bc538c0405ce9682ab76fd2cb9 Mon Sep 17 00:00:00 2001 From: nsensfel Date: Tue, 28 Nov 2017 18:58:09 +0100 Subject: Improves readability. --- src/type/battlemap.erl | 27 ++++++++++++++++-- src/type/battlemap/cross.erl | 60 ---------------------------------------- src/type/battlemap/cross_5.erl | 63 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 63 deletions(-) delete mode 100644 src/type/battlemap/cross.erl create mode 100644 src/type/battlemap/cross_5.erl diff --git a/src/type/battlemap.erl b/src/type/battlemap.erl index cce1a76..d9d7b8c 100644 --- a/src/type/battlemap.erl +++ b/src/type/battlemap.erl @@ -1,4 +1,8 @@ -module(battlemap). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -record ( battlemap, @@ -10,6 +14,11 @@ instances } ). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%% Accessors -export ( [ @@ -20,19 +29,31 @@ get_instances/1 ] ). +%%%% Utils -export ( - [dist/2] + [ + cross/5, + dist/2 + ] ). --export([cross/5]). +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%% Accessors get_id (Battlemap) -> Battlemap#battlemap.id. get_width (Battlemap) -> Battlemap#battlemap.width. get_height (Battlemap) -> Battlemap#battlemap.height. list_tiles (Battlemap) -> array:sparse_to_list(Battlemap#battlemap.content). get_instances (Battlemap) -> Battlemap#battlemap.instances. --include("battlemap/cross.erl"). +%%%% Utils +-include("battlemap/cross_5.erl"). dist ({OX, OY}, {DX, DY}) -> (abs(OX - DX) + abs(OY + DY)). diff --git a/src/type/battlemap/cross.erl b/src/type/battlemap/cross.erl deleted file mode 100644 index aa1645e..0000000 --- a/src/type/battlemap/cross.erl +++ /dev/null @@ -1,60 +0,0 @@ -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% LOCAL %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -next_loc (X, Y, <<"L">>) -> {(X - 1), Y}; -next_loc (X, Y, <<"R">>) -> {(X + 1), Y}; -next_loc (X, Y, <<"U">>) -> {X, (Y - 1)}; -next_loc (X, Y, <<"D">>) -> {X, (Y + 1)}. - -loc_to_index(X, Y, Map) -> - if - (X < 0) -> error; - (Y < 0) -> error; - (X >= Map#battlemap.width) -> error; - true -> ((Y * Map#battlemap.width) + X) - end. - -calc_new_loc (X, Y, [], Points, _Map, _CharInstsLocs) -> - io:format("~nPoints remaining: ~p ~n", [Points]), - true = (Points >= 0), - {X, Y}; -calc_new_loc (X, Y, [Step|Path], Points, Map, CharInstsLocs) -> - io:format("~nStep - Points remaining: ~p ~n", [Points]), - {NX, NY} = next_loc(X, Y, Step), - TileCost = - tile:get_cost - ( - array:get - ( - loc_to_index(NX, NY, Map), - Map#battlemap.content - ) - ), - io:format("~nStep cost: ~p ~n", [TileCost]), - NPoints = - ( - Points - - - TileCost - ), - false = lists:member({NX, NY}, CharInstsLocs), - calc_new_loc(NX, NY, Path, NPoints, Map, CharInstsLocs). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% EXPORTED %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -cross (Battlemap, {X, Y}, Points, Path, CharInsts) -> - calc_new_loc - ( - X, - Y, - Path, - Points, - Battlemap, - lists:map - ( - fun character_instance:get_location/1, - CharInsts - ) - ). diff --git a/src/type/battlemap/cross_5.erl b/src/type/battlemap/cross_5.erl new file mode 100644 index 0000000..dccc4b9 --- /dev/null +++ b/src/type/battlemap/cross_5.erl @@ -0,0 +1,63 @@ +%% +%% battlemap:cross/5 +%% + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +next_loc (X, Y, <<"L">>) -> {(X - 1), Y}; +next_loc (X, Y, <<"R">>) -> {(X + 1), Y}; +next_loc (X, Y, <<"U">>) -> {X, (Y - 1)}; +next_loc (X, Y, <<"D">>) -> {X, (Y + 1)}. + +loc_to_index(X, Y, Map) -> + if + (X < 0) -> error; + (Y < 0) -> error; + (X >= Map#battlemap.width) -> error; + true -> ((Y * Map#battlemap.width) + X) + end. + +calc_new_loc (X, Y, [], Points, _Map, _CharInstsLocs) -> + io:format("~nPoints remaining: ~p ~n", [Points]), + true = (Points >= 0), + {X, Y}; +calc_new_loc (X, Y, [Step|Path], Points, Map, CharInstsLocs) -> + io:format("~nStep - Points remaining: ~p ~n", [Points]), + {NX, NY} = next_loc(X, Y, Step), + TileCost = + tile:get_cost + ( + array:get + ( + loc_to_index(NX, NY, Map), + Map#battlemap.content + ) + ), + io:format("~nStep cost: ~p ~n", [TileCost]), + NPoints = + ( + Points + - + TileCost + ), + false = lists:member({NX, NY}, CharInstsLocs), + calc_new_loc(NX, NY, Path, NPoints, Map, CharInstsLocs). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +cross (Battlemap, {X, Y}, Points, Path, CharInsts) -> + calc_new_loc + ( + X, + Y, + Path, + Points, + Battlemap, + lists:map + ( + fun character_instance:get_location/1, + CharInsts + ) + ). -- cgit v1.2.3-70-g09d2