| summaryrefslogtreecommitdiff | 
diff options
| author | nsensfel <SpamShield0@noot-noot.org> | 2019-02-15 15:53:58 +0100 | 
|---|---|---|
| committer | nsensfel <SpamShield0@noot-noot.org> | 2019-02-15 15:53:58 +0100 | 
| commit | 9b91ff37a1e39f48631b5bee338c31318d1e2336 (patch) | |
| tree | ccaa397a8268bcc8c2679ecbfa5fe75bf11b242a /src/shared | |
| parent | 103d1672665b64c85808836778fde21b7622abd1 (diff) | |
/{btl,map}_{map,location,direction}/shr_\2/
Diffstat (limited to 'src/shared')
| -rw-r--r-- | src/shared/reply/shr_set_map.erl | 35 | ||||
| -rw-r--r-- | src/shared/struct/shr_direction.erl | 38 | ||||
| -rw-r--r-- | src/shared/struct/shr_location.erl | 90 | ||||
| -rw-r--r-- | src/shared/struct/shr_map.erl | 154 | ||||
| -rw-r--r-- | src/shared/struct/shr_map_summary.erl | 8 | 
5 files changed, 321 insertions, 4 deletions
| diff --git a/src/shared/reply/shr_set_map.erl b/src/shared/reply/shr_set_map.erl new file mode 100644 index 0000000..b335302 --- /dev/null +++ b/src/shared/reply/shr_set_map.erl @@ -0,0 +1,35 @@ +-module(shr_set_map). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-export([generate/1]). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-spec generate (shr_map:type()) -> {list(any())}. +generate (Map) -> +   { +      [ +         {<<"msg">>, <<"set_map">>}, +         {<<"w">>, shr_map:get_width(Map)}, +         {<<"h">>, shr_map:get_height(Map)}, +         { +            <<"t">>, +            lists:map +            ( +               fun shr_tile:instance_to_binary_list/1, +               tuple_to_list(shr_map:get_tile_instances(Map)) +            ) +         } +      ] +   }. diff --git a/src/shared/struct/shr_direction.erl b/src/shared/struct/shr_direction.erl new file mode 100644 index 0000000..f0793b3 --- /dev/null +++ b/src/shared/struct/shr_direction.erl @@ -0,0 +1,38 @@ +-module(shr_direction). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-type enum() :: ('up' | 'down' | 'left' | 'right'). +-type type() :: enum(). + +-export_type([enum/0, type/0]). +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-export +( +   [ +      decode/1, +      encode/1 +   ] +). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-spec decode (binary()) -> enum(). +decode (<<"U">>) -> up; +decode (<<"D">>) -> down; +decode (<<"L">>) -> left; +decode (<<"R">>) -> right. + +-spec encode (enum()) -> binary(). +encode (up) -> <<"U">>; +encode (down) -> <<"D">>; +encode (left) -> <<"L">>; +encode (right) -> <<"R">>. diff --git a/src/shared/struct/shr_location.erl b/src/shared/struct/shr_location.erl new file mode 100644 index 0000000..ec62ff7 --- /dev/null +++ b/src/shared/struct/shr_location.erl @@ -0,0 +1,90 @@ +-module(shr_location). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-type type() :: ({non_neg_integer(), non_neg_integer()} | 'nowhere'). + +-export_type([type/0]). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-export +( +   [ +      decode/1, +      encode/1, +      get_nowhere/0 +   ] +). + +-export +( +   [ +      apply_direction/2, +      dist/2 +   ] +). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-spec validate ({integer(), integer()}) -> type(). +validate ({X, Y}) -> +   if +      (X < 0) -> nowhere; +      (Y < 0) -> nowhere; +      true -> {X, Y} +   end. + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-spec get_nowhere () -> type(). +get_nowhere () -> nowhere. + +-spec apply_direction (shr_direction:enum(), type()) -> type(). +apply_direction (left, {X, Y}) -> +   validate({(X - 1), Y}); +apply_direction (right, {X, Y}) -> +   validate({(X + 1), Y}); +apply_direction (up, {X, Y}) -> +   validate({X, (Y - 1)}); +apply_direction (down, {X, Y}) -> +   validate({X, (Y + 1)}); +apply_direction (_, nowhere) -> +   error("Trying to move from 'nowhere'."), +   nowhere. + +-spec dist(type(), type()) -> non_neg_integer(). +dist ({OX, OY}, {DX, DY}) -> +   (abs(DY - OY) + abs(DX - OX)); +dist (_, _) -> +   error("Trying to measure distance to 'nowhere'"), +   999. + +-spec encode (type()) -> {list(any())}. +encode ({X, Y}) -> +   { +      [ +         {<<"x">>, X}, +         {<<"y">>, Y} +      ] +   }; +encode (nowhere) -> +   { +      [ +         {<<"x">>, -1}, +         {<<"y">>, -1} +      ] +   }. + +-spec decode (map()) -> type(). +decode (Map) -> +   X = maps:get(<<"x">>, Map), +   Y = maps:get(<<"y">>, Map), + +   true = (is_integer(X) and is_integer(Y)), + +   validate({X, Y}). diff --git a/src/shared/struct/shr_map.erl b/src/shared/struct/shr_map.erl new file mode 100644 index 0000000..a57ed32 --- /dev/null +++ b/src/shared/struct/shr_map.erl @@ -0,0 +1,154 @@ +-module(shr_map). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-type id() :: ataxia_id:type(). + +-record +( +   map, +   { +      owner :: shr_player:id(), +      width :: non_neg_integer(), +      height :: non_neg_integer(), +      tile_instances :: shr_tile:instances_tuple() +   } +). + +-opaque type() :: #map{}. + +-export_type([type/0, id/0]). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%% Accessors +-export +( +   [ +      get_owner/1, +      get_width/1, +      get_height/1, +      get_tile_instances/1, +      get_tile_instance/2, + +      get_used_tile_ids/1 +   ] +). + +%%%% Fields +-export +( +   [ +      get_width_field/0, +      get_height_field/0, +      get_tile_instances_field/0 +   ] +). + +%%%% Utility +-export +( +   [ +      update_from_list/4, +      default/1 +   ] +). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-spec location_to_index +   ( +      non_neg_integer(), +      shr_location:type() +   ) +   -> ('error' | non_neg_integer()). +location_to_index (ArrayWidth, {X, Y}) -> +   if +      (X < 0) -> error; +      (Y < 0) -> error; +      (X >= ArrayWidth) -> error; +      true -> ((Y * ArrayWidth) + X) +   end. + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%% Accessors +-spec get_owner (type()) -> shr_player:id(). +get_owner (Map) -> Map#map.owner. + +-spec get_width (type()) -> non_neg_integer(). +get_width (Map) -> Map#map.width. + +-spec get_height (type()) -> non_neg_integer(). +get_height (Map) -> Map#map.height. + +-spec get_tile_instances (type()) -> shr_tile:instances_tuple(). +get_tile_instances (Map) -> Map#map.tile_instances. + +-spec get_tile_instance (shr_location:type(), type()) -> shr_tile:instance(). +get_tile_instance (Location, Map) -> +   TileIX = location_to_index(Map#map.width, Location), +   element((TileIX + 1), Map#map.tile_instances). + +%%%% Fields +-spec get_width_field () -> non_neg_integer(). +get_width_field () -> #map.width. + +-spec get_height_field () -> non_neg_integer(). +get_height_field () -> #map.height. + +-spec get_tile_instances_field () -> non_neg_integer(). +get_tile_instances_field () -> #map.tile_instances. + +%%%% Utility +-spec get_used_tile_ids (type()) -> ordsets:ordset(shr_tile:class_id()). +get_used_tile_ids (Map) -> +   UsedTileIDs = +      lists:foldl +      ( +         fun (TileInstance, CurrentTileIDs) -> +            ordsets:add_element +            ( +               shr_tile:extract_main_class_id(TileInstance), +               CurrentTileIDs +            ) +         end, +         ordsets:new(), +         tuple_to_list(Map#map.tile_instances) +      ), + +   UsedTileIDs. + +-spec update_from_list +   ( +      type(), +      non_neg_integer(), +      non_neg_integer(), +      list(list(binary())) +   ) +   -> type(). +update_from_list (Map, Width, Height, List) -> +   TileInstances = lists:map(fun shr_tile:instance_from_binary_list/1, List), + +   Map#map +   { +      width = Width, +      height = Height, +      tile_instances = list_to_tuple(TileInstances) +   }. + +-spec default (binary()) -> type(). +default (Owner) -> +   DefaultTileInstance = shr_tile:default_tile_instance(), + +   #map +   { +      owner = Owner, +      width = 32, +      height = 32, +      tile_instances = list_to_tuple(lists:duplicate(1024, DefaultTileInstance)) +   }. diff --git a/src/shared/struct/shr_map_summary.erl b/src/shared/struct/shr_map_summary.erl index 554b988..745193c 100644 --- a/src/shared/struct/shr_map_summary.erl +++ b/src/shared/struct/shr_map_summary.erl @@ -7,7 +7,7 @@  (     map_summary,     { -      id :: binary(), +      id :: shr_map:id(),        name :: binary()     }  ). @@ -65,7 +65,7 @@  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  %% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% --spec new (binary(), binary()) -> type(). +-spec new (shr_map:id(), binary()) -> type().  new (ID, Name) ->     #map_summary     { @@ -74,13 +74,13 @@ new (ID, Name) ->     }.  %%%% Accessors --spec get_id (type()) -> binary(). +-spec get_id (type()) -> shr_map:id().  get_id (MapSummary) -> MapSummary#map_summary.id.  -spec get_name (type()) -> binary().  get_name (MapSummary) -> MapSummary#map_summary.name. --spec set_id (binary(), type()) -> type(). +-spec set_id (shr_map:id(), type()) -> type().  set_id (Val, MapSummary) -> MapSummary#map_summary{ id = Val }.  -spec set_name (binary(), type()) -> type(). | 


