Skip to content

Commit

Permalink
[GH-621] Fix leap duration on auto aim (#622)
Browse files Browse the repository at this point in the history
* Fix bug

* Revive bots
  • Loading branch information
agustinesco authored May 14, 2024
1 parent df29d15 commit 776955a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
12 changes: 7 additions & 5 deletions apps/arena/lib/arena/game/player.ex
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ defmodule Arena.Game.Player do
skill_params.target
|> Skill.maybe_auto_aim(skill, player, targetable_players(game_state.players))

execution_duration = calculate_duration(skill, player.position, skill_direction)
execution_duration = calculate_duration(skill, player.position, skill_direction, auto_aim?)
Process.send_after(self(), {:block_actions, player.id}, execution_duration)

action =
Expand Down Expand Up @@ -402,18 +402,20 @@ defmodule Arena.Game.Player do
## so to simplify my life an executive decision was made to take thas as a fact
## When the time comes to have more than one mechanic per skill this function will need to be refactored, good thing
## is that it will crash here so not something we can ignore
defp calculate_duration(%{mechanics: [{:leap, leap}]}, position, direction) do
defp calculate_duration(%{mechanics: [{:leap, leap}]}, position, direction, auto_aim?) do
## TODO: Cap target_position to leap.range
direction = Skill.maybe_multiply_by_range(direction, auto_aim?, leap.range)

target_position = %{
x: position.x + direction.x * leap.range,
y: position.y + direction.y * leap.range
x: position.x + direction.x,
y: position.y + direction.y
}

## TODO: Magic number needs to be replaced with state.game_config.game.tick_rate_ms
Physics.calculate_duration(position, target_position, leap.speed) * 30
end

defp calculate_duration(%{mechanics: [_]} = skill, _, _) do
defp calculate_duration(%{mechanics: [_]} = skill, _, _, _) do
skill.execution_duration_ms
end

Expand Down
10 changes: 5 additions & 5 deletions apps/arena/lib/arena/game/skill.ex
Original file line number Diff line number Diff line change
Expand Up @@ -369,10 +369,10 @@ defmodule Arena.Game.Skill do
def maybe_auto_aim(%{x: x, y: y}, skill, player, entities) when x == 0.0 and y == 0.0 do
case skill.autoaim do
true ->
nearest_entity_direction_in_range =
Physics.nearest_entity_direction_in_range(player, entities, skill.max_autoaim_range)
nearest_entity_position_in_range =
Physics.nearest_entity_position_in_range(player, entities, skill.max_autoaim_range)

{nearest_entity_direction_in_range != player.direction, nearest_entity_direction_in_range}
{nearest_entity_position_in_range != player.direction, nearest_entity_position_in_range}

false ->
{false, player.direction |> maybe_normalize(not skill.can_pick_destination)}
Expand Down Expand Up @@ -413,11 +413,11 @@ defmodule Arena.Game.Skill do
game_state
end

defp maybe_multiply_by_range(%{x: x, y: y}, false = _auto_aim?, range) do
def maybe_multiply_by_range(%{x: x, y: y}, false = _auto_aim?, range) do
%{x: x * range, y: y * range}
end

defp maybe_multiply_by_range(direction, true = _auto_aim?, _range) do
def maybe_multiply_by_range(direction, true = _auto_aim?, _range) do
direction
end
end
2 changes: 1 addition & 1 deletion apps/arena/lib/physics.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ defmodule Physics do

def distance_between_entities(_entity, _entities), do: :erlang.nif_error(:nif_not_loaded)

def nearest_entity_direction_in_range(_entity, _entities, _range), do: :erlang.nif_error(:nif_not_loaded)
def nearest_entity_position_in_range(_entity, _entities, _range), do: :erlang.nif_error(:nif_not_loaded)
end
12 changes: 6 additions & 6 deletions apps/arena/native/physics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,13 @@ fn calculate_duration(position_a: Position, position_b: Position, speed: f32) ->
}

#[rustler::nif()]
fn nearest_entity_direction_in_range(
fn nearest_entity_position_in_range(
entity: Entity,
entities: HashMap<u64, Entity>,
range: i64,
) -> Direction {
) -> Position {
let mut max_distance = range as f32;
let mut direction = Direction {
let mut position = Position {
x: entity.direction.x,
y: entity.direction.y,
};
Expand All @@ -158,15 +158,15 @@ fn nearest_entity_direction_in_range(
max_distance = distance;
let difference_between_positions =
Position::sub(&other_entity.position, &entity.position);
direction = Direction {
position = Position {
x: difference_between_positions.x,
y: difference_between_positions.y,
};
}
}
}

direction
position
}
#[rustler::nif()]
fn distance_between_entities(entity_a: Entity, entity_b: Entity) -> f32 {
Expand Down Expand Up @@ -225,7 +225,7 @@ rustler::init!(
get_direction_from_positions,
calculate_duration,
distance_between_entities,
nearest_entity_direction_in_range,
nearest_entity_position_in_range,
get_closest_available_position
]
);

0 comments on commit 776955a

Please sign in to comment.