summaryrefslogtreecommitdiff
blob: 227c06b288b517b303d8883658caccecf6aeaa5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
-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_array_tuple:array_tuple(shr_tile_instance:type()),
      markers :: shr_map_marker:collection()
   }
).

-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_markers/1,
      get_marker/2,

      get_used_tile_ids/1
   ]
).

%%%% Fields
-export
(
   [
      get_width_field/0,
      get_height_field/0,
      get_markers_field/0,
      get_tile_instances_field/0
   ]
).

%%%% Utility
-export
(
   [
      update_from_list/5,
      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_array_tuple:array_tuple(shr_tile_instance:type()).
get_tile_instances (Map) -> Map#map.tile_instances.

-spec get_tile_instance
   (
      shr_location:type(),
      type()
   )
   -> shr_tile_instance:type().
get_tile_instance (Location, Map) ->
   TileIX = location_to_index(Map#map.width, Location),
   element((TileIX + 1), Map#map.tile_instances).

-spec get_markers (type()) -> shr_map_marker:collection().
get_markers (Map) -> Map#map.markers.

-spec get_marker
   (
      shr_map_marker:name(),
      type()
   )
   -> ('not_found' | {'ok', shr_map_marker:type()}).
get_marker (Name, Map) ->
   shr_map_marker:get(Name, Map#map.markers).

%%%% 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_markers_field () -> non_neg_integer().
get_markers_field () -> #map.markers.

-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:id()).
get_used_tile_ids (Map) ->
   UsedTileIDs =
      lists:foldl
      (
         fun (TileInstance, CurrentTileIDs) ->
            ordsets:add_element
            (
               shr_tile_instance:get_tile_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(),
      shr_map_marker:collection(),
      list(map())
   )
   -> type().
update_from_list (Map, Width, Height, Markers, List) ->
   TileInstances = lists:map(fun shr_tile_instance:decode/1, List),

   Map#map
   {
      width = Width,
      height = Height,
      markers = Markers,
      tile_instances = list_to_tuple(TileInstances)
   }.

-spec default (binary()) -> type().
default (Owner) ->
   DefaultTileInstance = shr_tile_instance:default(),

   #map
   {
      owner = Owner,
      width = 32,
      height = 32,
      markers = shr_map_marker:empty_collection(),
      tile_instances = list_to_tuple(lists:duplicate(1024, DefaultTileInstance))
   }.