summaryrefslogtreecommitdiff
blob: 667d010fee90682c73ec1103c72bff1f4c4956f3 (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
-module(btl_victory_progression).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-spec mark_characters_of_player_as_defeated
   (
      non_neg_integer(),
      btl_battle:type()
   )
   ->
   {
      btl_battle:type(),
      ataxic:basic()
   }.
mark_characters_of_player_as_defeated (PlayerIX, Battle) ->
   AllCharacters = btl_battle:get_characters(Battle),

   {ResultingBattle, BattleAtaxiaUpdates} =
      orddict:fold
      (
         fun (IX, Character, {CurrentBattle, CurrentBattleAtaxiaUpdates}) ->
            case (btl_character:get_player_index(Character) == PlayerIX) of
               false -> {CurrentBattle, CurrentBattleAtaxiaUpdates};
               true ->
                  {UpdatedCharacter, CharacterAtaxiaUpdate} =
                     btl_character:ataxia_set_is_defeated(true, Character),

                  {UpdatedBattle, NewBattleAtaxiaUpdate} =
                     btl_battle:ataxia_set_character
                     (
                        IX,
                        UpdatedCharacter,
                        CharacterAtaxiaUpdate,
                        Battle
                     ),

                  {
                     UpdatedBattle,
                     [NewBattleAtaxiaUpdate|CurrentBattleAtaxiaUpdates]
                  }
            end
         end,
         {Battle, []},
         AllCharacters
      ),

   {ResultingBattle, ataxic:optimize(ataxic:sequence(BattleAtaxiaUpdates))}.

-spec mark_player_as_inactive
   (
      non_neg_integer(),
      btl_battle:type()
   )
   -> {btl_battle:type(), ataxic:basic()}.
mark_player_as_inactive (PlayerIX, Battle) ->
   Player = btl_battle:get_player(PlayerIX, Battle),

   {UpdatedPlayer, PlayerAtaxicUpdate} =
      btl_player:ataxia_set_is_active(false, Player),

   {UpdateBattle, BattleAtaxicUpdate} =
      btl_battle:ataxia_set_player
      (
         PlayerIX,
         UpdatedPlayer,
         PlayerAtaxicUpdate,
         Battle
      ),

   {UpdateBattle, BattleAtaxicUpdate}.

-spec handle_player_defeat
   (
      non_neg_integer(),
      btl_character_turn_update:type()
   )
   -> btl_character_turn_update:type().
handle_player_defeat (PlayerIX, S0Update) ->
   Battle = btl_character_turn_update:get_battle(S0Update),

   {S0Battle, BattleAtaxicUpdate0} =
      mark_characters_of_player_as_defeated(PlayerIX, Battle),
   {S1Battle, BattleAtaxicUpdate1} =
      mark_player_as_inactive(PlayerIX, S0Battle),

   S1Update =
      btl_character_turn_update:ataxia_set_battle
      (
         S1Battle,
         ataxic:sequence([BattleAtaxicUpdate0, BattleAtaxicUpdate1]),
         S0Update
      ),

   S2Update =
      btl_character_turn_update:add_to_timeline
      (
         btl_turn_result:new_player_lost(PlayerIX),
         S1Update
      ),

   S2Update.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-spec handle_character_loss
   (
      btl_character:either(),
      btl_character_turn_update:type()
   )
   -> btl_character_turn_update:type().
handle_character_loss (Character, Update) ->
   Battle = btl_character_turn_update:get_battle(Update),
   Characters = btl_battle:get_characters(Battle),
   CharacterPlayerIX = btl_character:get_player_index(Character),

   case btl_character:get_rank(Character) of
      optional ->
         %% Let's not assume there is a commander, meaning that we still have
         %% to check if at least one character is alive, despite the fact that
         %% if there is a commander, it being killed would have triggered
         %% the defeat.
         StillHasAliveChar =
            lists:any
            (
               fun ({_IX, Char}) ->
                  (
                     (CharacterPlayerIX == btl_character:get_player_index(Char))
                     and btl_character:get_is_alive(Char)
                  )
               end,
               orddict:to_list(Characters)
            ),

         case StillHasAliveChar of
            true -> Update;
            _ -> handle_player_defeat(CharacterPlayerIX, Update)
         end;

      commander -> handle_player_defeat(CharacterPlayerIX, Update);

      target ->
         StillHasAliveTargetChar =
            lists:any
            (
               fun ({_IX, Char}) ->
                  (
                     (CharacterPlayerIX == btl_character:get_player_index(Char))
                     and btl_character:get_is_alive(Char)
                     and (btl_character:get_rank(Char) == target)
                  )
               end,
               orddict:to_list(Characters)
            ),

         case StillHasAliveTargetChar of
            true -> Update;
            _ -> handle_player_defeat(CharacterPlayerIX, Update)
         end
   end.