From a132188ccc244a6d802bd1c32fbf196d4cb53cbd Mon Sep 17 00:00:00 2001 From: nsensfel Date: Thu, 12 Jul 2018 17:48:41 +0200 Subject: Got it to load the map (full of "error" tiles). --- src/map/map_shim.erl | 2 +- src/map/query/map_load.erl | 2 +- src/map/reply/map_add_tile.erl | 14 ++--- src/map/reply/map_set_map.erl | 12 ++--- src/map/struct/map_battlemap.erl | 104 ------------------------------------- src/map/struct/map_direction.erl | 2 +- src/map/struct/map_location.erl | 4 +- src/map/struct/map_map.erl | 107 +++++++++++++++++++++++++++++++++++++++ src/map/struct/map_tile.erl | 2 +- 9 files changed, 126 insertions(+), 123 deletions(-) delete mode 100644 src/map/struct/map_battlemap.erl create mode 100644 src/map/struct/map_map.erl (limited to 'src/map') diff --git a/src/map/map_shim.erl b/src/map/map_shim.erl index 08034e4..ef23027 100644 --- a/src/map/map_shim.erl +++ b/src/map/map_shim.erl @@ -54,6 +54,6 @@ demo_map () -> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -spec generate_random_map () -> map_map:type(). generate_random_map () -> - Map = map_map:from_list(<<"0">>, <<"0">>, 32, 32, demo_map()), + Map = map_map:from_list(0, <<"0">>, 32, 32, demo_map()), Map. diff --git a/src/map/query/map_load.erl b/src/map/query/map_load.erl index 0f9c685..8365521 100644 --- a/src/map/query/map_load.erl +++ b/src/map/query/map_load.erl @@ -65,7 +65,7 @@ generate_reply (QueryState) -> Map = QueryState#query_state.map, SetMap = map_set_map:generate(Map), - Output = jiffy:encode(SetMap), + Output = jiffy:encode([SetMap]), Output. diff --git a/src/map/reply/map_add_tile.erl b/src/map/reply/map_add_tile.erl index 04c4ec2..ab0d80b 100644 --- a/src/map/reply/map_add_tile.erl +++ b/src/map/reply/map_add_tile.erl @@ -1,4 +1,4 @@ --module(btl_add_tile). +-module(map_add_tile). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -16,15 +16,15 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% --spec generate (btl_tile:type()) -> {list(any())}. +-spec generate (map_tile:type()) -> {list(any())}. generate (Tile) -> { [ {<<"msg">>, <<"add_tile">>}, - {<<"id">>, btl_tile:get_id(Tile)}, - {<<"nam">>, btl_tile:get_name(Tile)}, - {<<"ct">>, btl_tile:get_cost(Tile)}, - {<<"rmi">>, btl_tile:get_range_minimum(Tile)}, - {<<"rma">>, btl_tile:get_range_maximum(Tile)} + {<<"id">>, map_tile:get_id(Tile)}, + {<<"nam">>, map_tile:get_name(Tile)}, + {<<"ct">>, map_tile:get_cost(Tile)}, + {<<"rmi">>, map_tile:get_range_minimum(Tile)}, + {<<"rma">>, map_tile:get_range_maximum(Tile)} ] }. diff --git a/src/map/reply/map_set_map.erl b/src/map/reply/map_set_map.erl index 37c6331..336c9b2 100644 --- a/src/map/reply/map_set_map.erl +++ b/src/map/reply/map_set_map.erl @@ -1,4 +1,4 @@ --module(btl_set_map). +-module(map_set_map). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -16,16 +16,16 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% --spec generate (btl_battlemap:type()) -> {list(any())}. -generate (Battlemap) -> +-spec generate (map_map:type()) -> {list(any())}. +generate (Map) -> { [ {<<"msg">>, <<"set_map">>}, - {<<"w">>, btl_battlemap:get_width(Battlemap)}, - {<<"h">>, btl_battlemap:get_height(Battlemap)}, + {<<"w">>, map_map:get_width(Map)}, + {<<"h">>, map_map:get_height(Map)}, { <<"t">>, - array:sparse_to_list(btl_battlemap:get_tile_class_ids(Battlemap)) + array:sparse_to_list(map_map:get_tile_class_ids(Map)) } ] }. diff --git a/src/map/struct/map_battlemap.erl b/src/map/struct/map_battlemap.erl deleted file mode 100644 index 595bcb3..0000000 --- a/src/map/struct/map_battlemap.erl +++ /dev/null @@ -1,104 +0,0 @@ --module(map_map). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% --type id() :: binary(). - --record -( - map, - { - id :: id(), - owner :: binary(), - width :: integer(), - height :: integer(), - tile_class_ids :: array:array(btl_tile:class_id()) - } -). - --opaque type() :: #map{}. - --export_type([type/0, id/0]). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%% Accessors --export -( - [ - get_id/1, - get_owner/1, - get_width/1, - get_height/1, - get_tile_class_ids/1, - get_tile_class_id/2 - ] -). - --export -( - [ - from_list/5 - ] -). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% --spec location_to_array_index - ( - non_neg_integer(), - btl_location:type() - ) - -> ('error' | non_neg_integer()). -location_to_array_index (ArrayWidth, {X, Y}) -> - if - (X < 0) -> error; - (Y < 0) -> error; - (X >= ArrayWidth) -> error; - true -> ((Y * ArrayWidth) + X) - end. - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%% Accessors --spec get_id (type()) -> id(). -get_id (Map) -> Map#map.id. - --spec get_width (type()) -> integer(). -get_width (Map) -> Map#map.width. - --spec get_height (type()) -> integer(). -get_height (Map) -> Map#map.height. - --spec get_tile_class_ids (type()) -> array:array(btl_tile:class_id()). -get_tile_class_ids (Map) -> Map#map.tile_class_ids. - --spec get_tile_class_id (btl_location:type(), type()) -> btl_tile:class_id(). -get_tile_class_id (Location, Map) -> - TileIX = location_to_array_index(Map#map.width, Location), - array:get(TileIX, Map#map.tile_class_ids). - --spec from_list - ( - non_neg_integer(), - binary(), - non_neg_integer(), - non_neg_integer(), - list(non_neg_integer()) - ) - -> type(). -from_list (ID, Owner, Width, Height, List) -> - TileClassIDs = lists:map(fun btl_tile:class_id_from_int/1, List), - - #map - { - id = list_to_binary(integer_to_list(ID)), - owner = Owner, - width = Width, - height = Height, - tile_class_ids = array:from_list(TileClassIDs) - }. diff --git a/src/map/struct/map_direction.erl b/src/map/struct/map_direction.erl index 9fb5a01..bd61b7d 100644 --- a/src/map/struct/map_direction.erl +++ b/src/map/struct/map_direction.erl @@ -1,4 +1,4 @@ --module(btl_direction). +-module(map_direction). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% diff --git a/src/map/struct/map_location.erl b/src/map/struct/map_location.erl index 9670cb0..edfb85a 100644 --- a/src/map/struct/map_location.erl +++ b/src/map/struct/map_location.erl @@ -1,4 +1,4 @@ --module(btl_location). +-module(map_location). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -44,7 +44,7 @@ validate ({X, Y}) -> -spec get_nowhere () -> type(). get_nowhere () -> nowhere. --spec apply_direction (btl_direction:enum(), type()) -> type(). +-spec apply_direction (map_direction:enum(), type()) -> type(). apply_direction (left, {X, Y}) -> validate({(X - 1), Y}); apply_direction (right, {X, Y}) -> diff --git a/src/map/struct/map_map.erl b/src/map/struct/map_map.erl new file mode 100644 index 0000000..d5a2a7c --- /dev/null +++ b/src/map/struct/map_map.erl @@ -0,0 +1,107 @@ +-module(map_map). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-type id() :: binary(). + +-record +( + map, + { + id :: id(), + owner :: binary(), + width :: integer(), + height :: integer(), + tile_class_ids :: array:array(map_tile:class_id()) + } +). + +-opaque type() :: #map{}. + +-export_type([type/0, id/0]). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%% Accessors +-export +( + [ + get_id/1, + get_owner/1, + get_width/1, + get_height/1, + get_tile_class_ids/1, + get_tile_class_id/2 + ] +). + +-export +( + [ + from_list/5 + ] +). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-spec location_to_array_index + ( + non_neg_integer(), + map_location:type() + ) + -> ('error' | non_neg_integer()). +location_to_array_index (ArrayWidth, {X, Y}) -> + if + (X < 0) -> error; + (Y < 0) -> error; + (X >= ArrayWidth) -> error; + true -> ((Y * ArrayWidth) + X) + end. + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%% Accessors +-spec get_id (type()) -> id(). +get_id (Map) -> Map#map.id. + +-spec get_owner (type()) -> binary(). +get_owner (Map) -> Map#map.owner. + +-spec get_width (type()) -> integer(). +get_width (Map) -> Map#map.width. + +-spec get_height (type()) -> integer(). +get_height (Map) -> Map#map.height. + +-spec get_tile_class_ids (type()) -> array:array(map_tile:class_id()). +get_tile_class_ids (Map) -> Map#map.tile_class_ids. + +-spec get_tile_class_id (map_location:type(), type()) -> map_tile:class_id(). +get_tile_class_id (Location, Map) -> + TileIX = location_to_array_index(Map#map.width, Location), + array:get(TileIX, Map#map.tile_class_ids). + +-spec from_list + ( + non_neg_integer(), + binary(), + non_neg_integer(), + non_neg_integer(), + list(non_neg_integer()) + ) + -> type(). +from_list (ID, Owner, Width, Height, List) -> + TileClassIDs = lists:map(fun map_tile:class_id_from_int/1, List), + + #map + { + id = list_to_binary(integer_to_list(ID)), + owner = Owner, + width = Width, + height = Height, + tile_class_ids = array:from_list(TileClassIDs) + }. diff --git a/src/map/struct/map_tile.erl b/src/map/struct/map_tile.erl index 16e671b..58ef658 100644 --- a/src/map/struct/map_tile.erl +++ b/src/map/struct/map_tile.erl @@ -1,4 +1,4 @@ --module(btl_tile). +-module(map_tile). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -- cgit v1.2.3-70-g09d2