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
|
-module(battle_action).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-record
(
move,
{
path :: list(direction:enum())
}
).
-record
(
switch_weapon,
{
}
).
-record
(
attack,
{
target :: non_neg_integer()
}
).
-type category() :: ('move' | 'switch_weapon' | 'attack' | 'nothing').
-opaque struct() :: (#move{} | #switch_weapon{} | #attack{}).
-export_type([category/0, struct/0]).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-export
(
[
decode/1,
handle/4,
can_follow/2
]
).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-spec decode_mov_action (map()) -> struct().
decode_mov_action (JSONMap) ->
PathInBinary = maps:get(<<"p">>, JSONMap),
Path = lists:map(fun direction:from_binary/1, PathInBinary),
#move { path = Path }.
-spec decode_atk_action (map()) -> struct().
decode_atk_action (JSONMap) ->
TargetIX = maps:get(<<"tix">>, JSONMap),
#attack { target = TargetIX }.
-spec decode_swp_action (map()) -> struct().
decode_swp_action (_JSONMap) ->
#switch_weapon{}.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-spec decode (binary()) -> struct().
decode (EncodedAction) ->
JSONActionMap = jiffy:decode(EncodedAction, [return_maps]),
ActionType = maps:get(<<"t">>, JSONActionMap),
case ActionType of
<<"mov">> -> decode_mov_action(JSONActionMap);
<<"atk">> -> decode_atk_action(JSONActionMap);
<<"swp">> -> decode_swp_action(JSONActionMap)
end.
-spec can_follow (category(), category()) -> boolean().
can_follow (nothing, attack) -> true;
can_follow (nothing, switch_weapon) -> true;
can_follow (nothing, move) -> true;
can_follow (switch_weapon, attack) -> true;
can_follow (move, attack) -> true;
can_follow (_, _) -> false.
-spec handle
(
battle:struct(),
character_instance:struct(),
non_neg_integer(),
struct()
)
->
{
list(database_diff:struct()),
list(turn_result:struct()),
battle:struct(),
character_instance:struct()
}.
handle (Battle, CharacterInstance, CharacterInstanceIX, BattleAction)
when is_record(BattleAction, switch_weapon) ->
Character = character_instance:get_character(CharacterInstance),
CharacterAttributes = character:get_attributes(Character),
{PrimaryWeaponID, SecondaryWeaponID} = character:get_weapon_ids(Character),
UpdatedWeaponIDs = {SecondaryWeaponID, PrimaryWeaponID},
UpdatedCharacterStatistics =
statistics:new(CharacterAttributes, UpdatedWeaponIDs),
UpdatedCharacter =
character:set_statistics
(
UpdatedCharacterStatistics,
character:set_weapon_ids(UpdatedWeaponIDs, Character)
),
UpdatedCharacterInstance =
character_instance:set_character(UpdatedCharacter, CharacterInstance),
{
% TODO: hide that into database_diff structs.
[
{character_instance, CharacterInstanceIX, wp0, SecondaryWeaponID},
{character_instance, CharacterInstanceIX, wp1, PrimaryWeaponID}
% ... statistics as well.
],
% TODO: hide that into turn_result structs.
[
{switched_weapons, CharacterInstanceIX}
],
UpdatedCharacterInstance,
Battle
};
handle (Battle, CharacterInstance, CharacterInstanceIX, BattleAction)
when is_record(BattleAction, move) ->
Character = character_instance:get_character(CharacterInstance),
CharacterStatistics = character:get_statistics(Character),
Battlemap = battle:get_battlemap(Battle),
Path = BattleAction#move.path,
CharacterMovementPoints =
statistics:get_movement_points(CharacterStatistics),
ForbiddenLocations =
array:foldl
(
fun (IX, CharInst, Prev) ->
case IX of
CharacterInstanceIX -> Prev;
_ -> [character_instance:get_location(CharInst)|Prev]
end
end,
[],
battle:get_character_instances(Battle)
),
{NewLocation, Cost} =
movement:cross
(
Battlemap,
ForbiddenLocations,
Path,
character_instance:get_location(CharacterInstance)
),
true = (Cost =< CharacterMovementPoints),
UpdatedCharacterInstance =
character_instance:set_location(NewLocation, CharacterInstance),
{
% TODO: hide that into database_diff structs.
[{character_instance, CharacterInstanceIX, loc, NewLocation}],
% TODO: hide that into turn_result structs.
[
{moved, Path, NewLocation}
],
UpdatedCharacterInstance,
Battle
}.
|