summaryrefslogtreecommitdiff
blob: da62e8c30d95caf625c0e7f6f9364e621a0b9b27 (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
-module(btl_turn_actions_move).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-export
(
   [
      handle/2
   ]
).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-spec cross
   (
      shr_map:type(),
      list(shr_location:type()),
      list(shr_direction:enum()),
      non_neg_integer(),
      shr_location:type()
   )
   -> {shr_location:type(), non_neg_integer()}.
cross (_Map, _ForbiddenLocations, [], Cost, Location) ->
   {Location, Cost};
cross (Map, ForbiddenLocations, [Step|NextSteps], Cost, Location) ->
   NextLocation = shr_location:apply_direction(Step, Location),
   NextTileInstance = shr_map:get_tile_instance(NextLocation, Map),
   NextTileClassID = shr_tile_instance:get_tile_id(NextTileInstance),
   NextTile = shr_tile:from_id(NextTileClassID),
   NextCost = (Cost + shr_tile:get_cost(NextTile)),
   IsForbidden =
      lists:foldl
      (
         fun (ForbiddenLocation, Prev) ->
            (Prev or (NextLocation == ForbiddenLocation))
         end,
         false,
         ForbiddenLocations
      ),

   IsForbidden = false,

   cross(Map, ForbiddenLocations, NextSteps, NextCost, NextLocation).

-spec cross
   (
      shr_map:type(),
      list(shr_location:type()),
      list(shr_direction:enum()),
      shr_location:type()
   )
   -> {shr_location:type(), non_neg_integer()}.
cross (Map, ForbiddenLocations, Path, Location) ->
   cross(Map, ForbiddenLocations, Path, 0, Location).

-spec get_path_cost_and_destination
   (
      btl_character_turn_update:type(),
      list(shr_direction:type())
   )
   ->
   {
      non_neg_integer(),
      shr_location:type(),
      btl_character_turn_update:type()
   }.
get_path_cost_and_destination (Update, Path) ->
   {S0Update, Character} = btl_character_turn_update:get_character(Update),
   {S1Update, Battle} = btl_character_turn_update:get_battle(S0Update),
   CharacterIX = btl_character_turn_update:get_character_ix(S1Update),
   Map = btl_battle:get_map(Battle),

   ForbiddenLocations =
      orddict:fold
      (
         fun (IX, Char, Prev) ->
            IsAlive = btl_character:get_is_alive(Char),
            if
               (IX == CharacterIX) -> Prev;
               (not IsAlive) -> Prev;
               true ->
                  ordsets:add_element(btl_character:get_location(Char), Prev)
            end
         end,
         ordsets:new(),
         btl_battle:get_characters(Battle)
      ),

   {NewLocation, Cost} =
      cross
      (
         Map,
         ForbiddenLocations,
         Path,
         btl_character:get_location(Character)
      ),

   {Cost, NewLocation, S1Update}.

-spec assert_character_can_move
   (
      btl_character:type(),
      non_neg_integer()
   )
   -> 'ok'.
assert_character_can_move (Char, Cost) ->
   CharacterMovementPoints =
      shr_statistics:get_movement_points
      (
         shr_character:get_statistics
         (
            btl_character:get_base_character(Char)
         )
      ),

   true = (Cost =< CharacterMovementPoints),

   ok.

-spec commit_move
   (
      btl_character:type(),
      btl_character_turn_update:type(),
      list(shr_direction:type()),
      shr_location:type()
   )
   -> btl_character_turn_update:type().
commit_move (Character, Update, Path, NewLocation) ->
   {S0Update, Battle} = btl_character_turn_update:get_battle(Update),
   Map = btl_battle:get_map(Battle),
   TileOmnimods =
      shr_tile:get_omnimods
      (
         shr_tile:from_id
         (
            shr_tile_instance:get_tile_id
            (
               shr_map:get_tile_instance(NewLocation, Map)
            )
         )
      ),

   {UpdatedCharacter, CharacterAtaxiaUpdate} =
      btl_character:ataxia_set_location(NewLocation, TileOmnimods, Character),

   S1Update =
      btl_character_turn_update:ataxia_set_character
      (
         UpdatedCharacter,
         CharacterAtaxiaUpdate,
         S0Update
      ),

   TimelineItem =
      btl_turn_result:new_character_moved
      (
         btl_character_turn_update:get_character_ix(S1Update),
         Path,
         NewLocation
      ),

   S2Update = btl_character_turn_update:add_to_timeline(TimelineItem, S1Update),

   S2Update.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-spec handle
   (
      btl_action:type(),
      btl_character_turn_update:type()
   )
   -> btl_character_turn_update:type().
handle (BattleAction, Update) ->
   {S0Update, Character} = btl_character_turn_update:get_character(Update),
   Path = btl_action:get_path(BattleAction),

   {PathCost, NewLocation, S1Update} =
      get_path_cost_and_destination(S0Update, Path),

   assert_character_can_move(Character, PathCost),

   commit_move(Character, S1Update, Path, NewLocation).