From 45434f444962ef9c852e8122fe86ae65c9c49436 Mon Sep 17 00:00:00 2001 From: nsensfel Date: Thu, 1 Mar 2018 18:18:48 +0100 Subject: ... --- src/struct/battle_action.erl | 100 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/src/struct/battle_action.erl b/src/struct/battle_action.erl index 5189384..7287a95 100644 --- a/src/struct/battle_action.erl +++ b/src/struct/battle_action.erl @@ -176,3 +176,103 @@ when is_record(BattleAction, move) -> UpdatedCharacterInstance, Battle }. +handle (Battle, CharacterInstance, CharacterInstanceIX, BattleAction) +when is_record(BattleAction, attack) -> + Character = character_instance:get_character(CharacterInstance), + CharacterStatistics = character:get_statistics(Character), + Battlemap = battle:get_battlemap(Battle), + TargetIX = BattleAction#attack.target_ix, + TargetCharacterInstance = battle:get_character_instance(TargetIX, Battle), + TargetCharacter = character_instance:get_character(TargetCharacterInstance), + TargetCharacterStatistics = character:get_statistics(TargetCharacter), + + Range = + location:dist + ( + character_instance:get_location(CharacterInstance), + character_instance:get_location(TargetCharacterInstance) + ), + + {AttackingWeaponID, _} = character:get_weapon_ids(Character), + {DefendingWeaponID, _} = character:get_weapon_ids(TargetCharacter), + + AttackingWeapon = weapon:from_id(AttackingWeaponID), + DefendingWeapon = weapon:from_id(DefendingWeaponID), + + AttackSequence = + attack:get_sequence(Range, AttackingWeapon, DefendingWeapon), + + AttackPlannedEffects = + lists:map + ( + fun (AttackStep) -> + attack:get_description_of + ( + AttackStep, + CharacterStatistics, + TargetCharacterStatistics + ) + end, + AttackSequence + ), + + % FIXME: may warrant a separate function + {AttackEffects, RemainingAttakerHealth, RemainingDefenderHealth} = + lists:foldl + ( + fun + ( + AttackEffectCandidate, + {AttackValidEffects, AttackerHealth, DefenderHealth } + ) -> + {AttackResult, NewAttackerHealth, NewDefenderHealth} = + attack:apply_to_healths + ( + AttackPlannedEffect, + AttackerHealth, + DefenderHealth + ), + case AttackResult of + nothing -> {AttackValidEffects, AttackerHealth, DefenderHealth}; + _ -> + { + [AttackResult|AttackValidEffects], + NewAttackerHealth, + NewDefenderHealth + } + end, + end, + { + [], + character_instance:get_current_health(CharacterInstance), + character_instance:get_current_health(TargetCharacterInstance) + }, + AttackPlannedEffects + ), + + UpdatedCharacterInstance = + character_instance:set_current_health + ( + RemainingAttackerHealth, + CharacterInstance + ), + + UpdatedBattle = + battle:set_character_instance + ( + TargetIX, + character_instance:set_current_health + ( + RemainingDefenderHealth, + TargetCharacterInstance + ), + Battle + ) + { + % TODO: hide that into database_diff structs. + [], % TODO + % TODO: hide that into turn_result structs. + AttackEffets, + UpdatedCharacterInstance, + UpdatedBattle + }. -- cgit v1.2.3-70-g09d2