summaryrefslogtreecommitdiff |
diff options
author | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2019-05-10 22:09:47 +0200 |
---|---|---|
committer | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2019-05-10 22:09:47 +0200 |
commit | 53768703626d71857b80dce2fa0ffe2a162979e4 (patch) | |
tree | 3fa0278b3ec62ee2d382c1092e6e390607f0e77e /src/shared/struct | |
parent | d8ae5b6c0bcf0701a3a8b5dec6c9daeec995e1b6 (diff) |
Adds neighborhood generation function.
Diffstat (limited to 'src/shared/struct')
-rw-r--r-- | src/shared/struct/map/shr_location.erl | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/shared/struct/map/shr_location.erl b/src/shared/struct/map/shr_location.erl index ec62ff7..e7d4432 100644 --- a/src/shared/struct/map/shr_location.erl +++ b/src/shared/struct/map/shr_location.erl @@ -22,6 +22,7 @@ -export ( [ + generate_neighborhood/4, apply_direction/2, dist/2 ] @@ -88,3 +89,44 @@ decode (Map) -> true = (is_integer(X) and is_integer(Y)), validate({X, Y}). + +-spec generate_neighborhood + ( + non_neg_integer(), + non_neg_integer(), + non_neg_integer(), + type() + ) + -> ordsets:ordset(type()). +generate_neighborhood(_, _, _, nowhere) -> ordsets:new(); +generate_neighborhood(MapWidth, MapHeight, Dist, {StartingX, StartingY}) -> + lists:foldl + ( + fun (CurrentYMod, CurrentYResult) -> + CurrentY = (StartingY + CurrentYMod), + case ((CurrentY < 0) or (CurrentY >= MapHeight)) of + true -> CurrentYResult; + false -> + XDistRange = Dist - abs(CurrentYMod), + lists:foldl + ( + fun (CurrentXMod, CurrentResult) -> + CurrentX = (StartingX + CurrentXMod), + case ((CurrentX < 0) or (CurrentX >= MapWidth)) of + true -> CurrentResult; + false -> + ordsets:add_element + ( + {CurrentX, CurrentY}, + CurrentResult + ) + end + end, + CurrentYResult, + lists:seq(-XDistRange, XDistRange) + ) + end + end, + ordsets:new(), + lists:seq(-Dist, Dist) + ). |