summaryrefslogtreecommitdiff
blob: 9194380b303972c25757a1ec61be320627f2b87a (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
-module(btl_character).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-type rank() :: ('optional' | 'target' | 'commander').

-record
(
   btl_char_ref,
   {
      player_ix :: non_neg_integer(),
      rank :: rank(),
      location :: {non_neg_integer(), non_neg_integer()},
      current_health :: integer(), %% Negative integers let us reverse attacks.
      is_active :: boolean(),
      is_defeated :: boolean(),
      base :: shr_character:unresolved()
   }
).

-record
(
   btl_char,
   {
      player_ix :: non_neg_integer(),
      rank :: rank(),
      location :: {non_neg_integer(), non_neg_integer()},
      current_health :: integer(), %% Negative integers let us reverse attacks.
      is_active :: boolean(),
      is_defeated :: boolean(),
      base :: shr_character:type()
   }
).

-opaque type() :: #btl_char{}.
-opaque unresolved() :: #btl_char_ref{}.
-type either() :: (type() | unresolved()).
-export_type([type/0, unresolved/0, either/0, rank/0]).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Accessors
-export
(
   [
      get_player_index/1,
      get_rank/1,
      get_location/1,
      get_current_health/1,
      get_is_alive/1,
      get_is_active/1,
      get_is_defeated/1,
      get_base_character/1,

      set_rank/2,
      set_location/2,
      set_current_health/2,
      set_is_active/2,
      set_is_defeated/2,
      set_base_character/2,

      get_rank_field/0,
      get_current_health_field/0,
      get_is_active_field/0,
      get_is_defeated_field/0,
      get_location_field/0,
      get_base_character_field/0
   ]
).

-export
(
   [
      new/4,
      resolve/2,
      to_unresolved/1
   ]
).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Accessors
-spec get_player_index (either()) -> non_neg_integer().
get_player_index (#btl_char{ player_ix = R}) -> R;
get_player_index (#btl_char_ref{ player_ix = R}) -> R.

-spec get_rank (either()) -> rank().
get_rank (#btl_char{ rank = R }) -> R;
get_rank (#btl_char_ref{ rank = R }) -> R.

-spec get_location (either()) -> {non_neg_integer(), non_neg_integer()}.
get_location (#btl_char{ location = R }) -> R;
get_location (#btl_char_ref{ location = R }) -> R.

-spec get_current_health (either()) -> integer().
get_current_health (#btl_char{ current_health = R }) -> R;
get_current_health (#btl_char_ref{ current_health = R }) -> R.

-spec get_is_alive (type()) -> boolean().
get_is_alive (#btl_char{ current_health = H, is_defeated = D }) ->
   ((not D) and (H > 0));
get_is_alive (#btl_char_ref{ current_health = H, is_defeated = D }) ->
   ((not D) and (H > 0)).


-spec get_is_active (either()) -> boolean().
get_is_active
(
   #btl_char{ current_health = H, is_defeated = D, is_active = A }
) ->
   ((not D) and (H > 0) and A);
get_is_active
(
   #btl_char_ref{ current_health = H, is_defeated = D, is_active = A }
) ->
   ((not D) and (H > 0) and A).

-spec get_is_defeated (either()) -> boolean().
get_is_defeated (#btl_char{ is_defeated = R }) -> R;
get_is_defeated (#btl_char_ref{ is_defeated = R }) -> R.

-spec get_base_character
   (type()) -> shr_character:type();
   (unresolved()) -> shr_character:unresolved().
get_base_character (#btl_char{ base = R }) -> R;
get_base_character (#btl_char_ref{ base = R }) -> R.

-spec set_rank
   (rank(), type()) -> type();
   (rank(), unresolved()) -> unresolved().
set_rank (Rank, Char) when is_record(Char, btl_char) ->
   Char#btl_char{ rank = Rank };
set_rank (Rank, Char) when is_record(Char, btl_char_ref) ->
   Char#btl_char_ref{ rank = Rank }.

-spec set_location
   ({non_neg_integer(), non_neg_integer()}, type()) -> type();
   ({non_neg_integer(), non_neg_integer()}, unresolved()) -> unresolved().
set_location (Location, Char) when is_record(Char, btl_char) ->
   Char#btl_char{ location = Location };
set_location (Location, Char) when is_record(Char, btl_char_ref) ->
   Char#btl_char_ref{ location = Location }.

-spec set_current_health
   (integer(), type()) -> type();
   (integer(), unresolved()) -> unresolved().
set_current_health (Health, Char) when is_record(Char, btl_char) ->
   Char#btl_char{ current_health = Health };
set_current_health (Health, Char) when is_record(Char, btl_char_ref) ->
   Char#btl_char_ref{ current_health = Health }.

-spec set_is_active
   (boolean(), type()) -> type();
   (boolean(), unresolved()) -> unresolved().
set_is_active (Active, Char) when is_record(Char, btl_char) ->
   Char#btl_char{ is_active = Active };
set_is_active (Active, Char) when is_record(Char, btl_char_ref) ->
   Char#btl_char_ref{ is_active = Active }.

-spec set_is_defeated
   (boolean(), type()) -> type();
   (boolean(), unresolved()) -> unresolved().
set_is_defeated (Defeated, Char) when is_record(Char, btl_char) ->
   Char#btl_char{ is_defeated = Defeated };
set_is_defeated (Defeated, Char) when is_record(Char, btl_char_ref) ->
   Char#btl_char_ref{ is_defeated = Defeated }.

-spec set_base_character
   (shr_character:type(), type()) -> type();
   (shr_character:unresolved(), unresolved()) -> unresolved().
set_base_character (Base, Char) when is_record(Char, btl_char) ->
   Char#btl_char{ base = Base};
set_base_character (Base, Char) when is_record(Char, btl_char_ref) ->
   Char#btl_char_ref{ base = Base}.

%%%% Utils
-spec new
   (
      non_neg_integer(),
      rank(),
      shr_location:type(),
      shr_character:type()
   )
   -> type().
new
(
   PlayerIX,
   Rank,
   Location,
   Base
) ->
   Statistics = shr_character:get_statistics(Base),

   #btl_char
   {
      player_ix = PlayerIX,
      rank = Rank,
      location = Location,
      current_health = shr_statistics:get_health(Statistics),
      is_active = (PlayerIX == 0),
      is_defeated = false,
      base = Base
   }.

-spec resolve (shr_omnimods:type(), unresolved()) -> type().
resolve (LocalOmnimods, CharRef) ->
   #btl_char
   {
      player_ix = CharRef#btl_char_ref.player_ix,
      rank = CharRef#btl_char_ref.rank,
      location = CharRef#btl_char_ref.location,
      current_health = CharRef#btl_char_ref.current_health,
      is_active = CharRef#btl_char_ref.is_active,
      is_defeated = CharRef#btl_char_ref.is_defeated,
      base = shr_character:resolve(LocalOmnimods, CharRef#btl_char_ref.base)
   }.

-spec to_unresolved (type()) -> unresolved().
to_unresolved (Char) ->
   #btl_char_ref
   {
      player_ix = Char#btl_char.player_ix,
      rank = Char#btl_char.rank,
      location = Char#btl_char.location,
      current_health = Char#btl_char.current_health,
      is_active = Char#btl_char.is_active,
      is_defeated = Char#btl_char.is_defeated,
      base = shr_character:to_unresolved(Char#btl_char.base)
   }.

-spec get_rank_field() -> non_neg_integer().
get_rank_field () -> #btl_char_ref.rank.
-spec get_location_field() -> non_neg_integer().
get_location_field () -> #btl_char_ref.location.
-spec get_current_health_field() -> non_neg_integer().
get_current_health_field () -> #btl_char_ref.current_health.
-spec get_is_active_field() -> non_neg_integer().
get_is_active_field () -> #btl_char_ref.is_active.
-spec get_is_defeated_field() -> non_neg_integer().
get_is_defeated_field () -> #btl_char_ref.is_defeated.
-spec get_base_character_field() -> non_neg_integer().
get_base_character_field () -> #btl_char_ref.base.