Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'Self' targeting strategy #585

Merged
merged 10 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion apps/champions/lib/champions/battle/simulator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,10 @@ defmodule Champions.Battle.Simulator do
take_unit_ids_by_slots(target_team, [1, 2])
end

defp choose_targets(caster, %{type: "self"}, _state) do
[caster.id]
end

defp find_by_proximity(units, slots_priorities, amount) do
sorted_units =
Enum.sort_by(units, fn unit ->
Expand Down Expand Up @@ -953,7 +957,8 @@ defmodule Champions.Battle.Simulator do
"furthest",
"all",
"frontline",
"backline"
"backline",
"self"
]

defp create_mechanics_map(%Mechanic{} = mechanic, skill_id, caster_id) do
Expand Down
148 changes: 148 additions & 0 deletions apps/champions/test/battle_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,154 @@ defmodule Champions.Test.BattleTest do
maximum_steps: maximum_steps
).result
end

test "Self Heal" do
tkz00 marked this conversation as resolved.
Show resolved Hide resolved
tkz00 marked this conversation as resolved.
Show resolved Hide resolved
# This test pairs a self healing unit with 10 health, against a damage dealing unit that makes 5 damage for each attack,
# the second one attacks 3 times, but since the healer unit will heal itself for the same amount the same number of times,
# the battle will end on a timeout.
skills_cooldown = 5
maximum_steps = (skills_cooldown + 1) * 3

# Create a character with a basic skill that will heal 5 damage to itself
heal_basic_skill_params =
TestUtils.build_skill(%{
name: "Heal Self skill",
mechanics: [
%{
trigger_delay: 0,
tkz00 marked this conversation as resolved.
Show resolved Hide resolved
apply_effects_to:
TestUtils.build_apply_effects_to_mechanic(%{
effects: [
TestUtils.build_effect(%{
executions: [
%{
type: "Heal",
attack_ratio: 1,
energy_recharge: 0
}
]
})
],
targeting_strategy: %{
type: "self"
}
})
}
],
cooldown: skills_cooldown * @miliseconds_per_step
})

{:ok, heal_character} =
TestUtils.build_character(%{
name: "Self Heal Character",
basic_skill: heal_basic_skill_params,
ultimate_skill: TestUtils.build_skill(%{name: "Self Heal Empty Skill"}),
base_attack: 5,
base_health: 10
})
|> Characters.insert_character()

{:ok, heal_unit} = TestUtils.build_unit(%{character_id: heal_character.id}) |> Units.insert_unit()
{:ok, heal_unit} = Units.get_unit(heal_unit.id)

deal_damage_basic_skill_params =
TestUtils.build_skill(%{
name: "DealDamage Skill for Self Test",
mechanics: [
%{
trigger_delay: 0,
tkz00 marked this conversation as resolved.
Show resolved Hide resolved
apply_effects_to:
TestUtils.build_apply_effects_to_mechanic(%{
effects: [
TestUtils.build_effect(%{
executions: [
%{
type: "DealDamage",
attack_ratio: 1,
energy_recharge: 50,
delay: 0
}
]
})
]
})
}
],
cooldown: skills_cooldown * @miliseconds_per_step
})

# Create enemy unit
{:ok, heal_damage_character} =
TestUtils.build_character(%{
name: "DealDamage Character for Self Test",
basic_skill: deal_damage_basic_skill_params,
ultimate_skill: TestUtils.build_skill(%{name: "Self Heal Empty Skill 2"}),
base_attack: 5
})
|> Characters.insert_character()

{:ok, damage_unit_for_heal} =
TestUtils.build_unit(%{character_id: heal_damage_character.id}) |> Units.insert_unit()

{:ok, damage_unit_for_heal} = Units.get_unit(damage_unit_for_heal.id)

# Battle is timeout, since every time ally unit takes damage, it heals itself
assert "timeout" ==
Champions.Battle.Simulator.run_battle([heal_unit], [damage_unit_for_heal], maximum_steps: maximum_steps).result
end

test "Self Damage", %{target_dummy: target_dummy} do
# This test pairs a self damaging unit agains a target dummy, the second team has no way of dealing damage to the first, but since the first team unit's
# skill makes it damage itself for 5 damage, after 4 turns it will die, since it has 20 health.
self_damage_cooldown = 2
maximum_steps = (self_damage_cooldown + 1) * 4

# Create a character with a basic skill that will self deal 5 damage
self_damage_skill_params =
TestUtils.build_skill(%{
name: "Self Damage skill",
mechanics: [
%{
trigger_delay: 0,
apply_effects_to:
TestUtils.build_apply_effects_to_mechanic(%{
effects: [
TestUtils.build_effect(%{
executions: [
%{
type: "DealDamage",
attack_ratio: 1,
energy_recharge: 0
}
]
})
],
targeting_strategy: %{
type: "self"
}
})
}
],
cooldown: self_damage_cooldown * @miliseconds_per_step
})
tkz00 marked this conversation as resolved.
Show resolved Hide resolved

{:ok, self_damage_character} =
TestUtils.build_character(%{
name: "Self Damage Character",
basic_skill: self_damage_skill_params,
ultimate_skill: TestUtils.build_skill(%{name: "Self Damage Empty Skill"}),
base_attack: 5,
base_health: 20
})
|> Characters.insert_character()

{:ok, self_damage_unit} = TestUtils.build_unit(%{character_id: self_damage_character.id}) |> Units.insert_unit()
{:ok, self_damage_unit} = Units.get_unit(self_damage_unit.id)

# Battle is won by team_2, the dummy, since the team_1 unit damages itself
assert "team_2" ==
Champions.Battle.Simulator.run_battle([self_damage_unit], [target_dummy], maximum_steps: maximum_steps).result
end
end

describe "Items" do
Expand Down
Loading