summaryrefslogtreecommitdiff
blob: 6ff09aa5657b91101f2e2ab06892f5b89d510abd (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
-module(btl_turn_actions).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-spec mod_current_health
   (
      non_neg_integer(),
      non_neg_integer(),
      btl_character_turn_update:type()
   )
   -> btl_character_turn_update:type().
mod_current_health (CurrentMaxHealth, PreviousMaxHealth, Update) ->
   Data = btl_character_turn_update:get_data(Update),
   Character = btl_character_turn_data:get_character(Data),
   CharacterIX = btl_character_turn_data:get_character_ix(Data),
   PreviousHealth = btl_character:get_current_health(Character),

   PreviousHealthRatio = (PreviousHealth / PreviousMaxHealth),
   NewHealth =
      min
      (
         CurrentMaxHealth,
         max(1, round(PreviousHealthRatio * CurrentMaxHealth))
      ),

   UpdatedCharacter = btl_character:set_current_health(NewHealth, Character),
   UpdatedData = btl_character_turn_data:set_character(UpdatedCharacter, Data),
   S0Update = btl_character_turn_update:set_data(UpdatedData, Update),

   DBQuery =
      shr_db_query:update_indexed
      (
         btl_battle:get_characters_field(),
         CharacterIX,
         [
            shr_db_query:set_field
            (
               btl_character:get_current_health_field(),
               NewHealth
            )
         ]
      ),

   S1Update =  btl_character_turn_update:add_to_db(DBQuery, S0Update),

   S1Update.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-spec handle
(
   btl_battle_action:type(),
   btl_character_turn_update:type()
)
-> btl_character_turn_update:type().
handle (BattleAction, Update) ->
   case btl_battle_action:get_category(BattleAction) of
      move -> btl_turn_actions_move:handle(BattleAction, Update);
      switch_weapon -> btl_turn_actions_switch_weapon:handle(Update);
      attack -> btl_turn_actions_attack:handle(BattleAction, Update)
   end.

-spec handle_max_health_changes
   (
      btl_character_current_data:type(),
      btl_character_turn_update:type()
   )
   -> btl_character_turn_update:type().
handle_max_health_changes (PreviousData, Update) ->
   Data = btl_character_turn_update:get_data(Update),
   CurrentData = btl_character_turn_data:get_character_current_data(Data),
   CurrentStats =  btl_character_current_data:get_statistics(CurrentData),
   PreviousStats = btl_character_current_data:get_statistics(PreviousData),

   CurrentMaxHealth = shr_statistics:get_health(CurrentStats),
   PreviousMaxHealth = shr_statistics:get_health(PreviousStats),

   case (CurrentMaxHealth == PreviousMaxHealth) of
      true -> Update;
      _ -> mod_current_health(CurrentMaxHealth, PreviousMaxHealth, Update)
   end.