From 87c2c7e832035f76eb2635af93b5ec0fed4b1254 Mon Sep 17 00:00:00 2001 From: agustinesco Date: Tue, 14 May 2024 19:20:08 -0300 Subject: [PATCH 1/2] Fix bug --- apps/arena/lib/arena/game/player.ex | 12 +++++++----- apps/arena/lib/arena/game/skill.ex | 10 +++++----- apps/arena/lib/physics.ex | 2 +- apps/arena/native/physics/src/lib.rs | 12 ++++++------ apps/bot_manager/lib/game_socket_handler.ex | 4 ++-- 5 files changed, 21 insertions(+), 19 deletions(-) diff --git a/apps/arena/lib/arena/game/player.ex b/apps/arena/lib/arena/game/player.ex index 5feca6f0c..5515ddca4 100644 --- a/apps/arena/lib/arena/game/player.ex +++ b/apps/arena/lib/arena/game/player.ex @@ -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 = @@ -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 diff --git a/apps/arena/lib/arena/game/skill.ex b/apps/arena/lib/arena/game/skill.ex index 29b076641..284bdc7c8 100644 --- a/apps/arena/lib/arena/game/skill.ex +++ b/apps/arena/lib/arena/game/skill.ex @@ -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)} @@ -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 diff --git a/apps/arena/lib/physics.ex b/apps/arena/lib/physics.ex index ec0c3d44b..1ec91caeb 100644 --- a/apps/arena/lib/physics.ex +++ b/apps/arena/lib/physics.ex @@ -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 diff --git a/apps/arena/native/physics/src/lib.rs b/apps/arena/native/physics/src/lib.rs index 149dff4dd..59a5c92b9 100644 --- a/apps/arena/native/physics/src/lib.rs +++ b/apps/arena/native/physics/src/lib.rs @@ -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, 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, }; @@ -158,7 +158,7 @@ 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, }; @@ -166,7 +166,7 @@ fn nearest_entity_direction_in_range( } } - direction + position } #[rustler::nif()] fn distance_between_entities(entity_a: Entity, entity_b: Entity) -> f32 { @@ -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 ] ); diff --git a/apps/bot_manager/lib/game_socket_handler.ex b/apps/bot_manager/lib/game_socket_handler.ex index 1d98eb555..22f651f9f 100644 --- a/apps/bot_manager/lib/game_socket_handler.ex +++ b/apps/bot_manager/lib/game_socket_handler.ex @@ -26,8 +26,8 @@ defmodule BotManager.GameSocketHandler do ####################### def handle_connect(_conn, state) do - send(self(), :decide_action) - send(self(), :perform_action) + # send(self(), :decide_action) + # send(self(), :perform_action) {:ok, state} end From 3f500c5928800ec0c4e8bb025b52ac3eb0147070 Mon Sep 17 00:00:00 2001 From: agustinesco Date: Tue, 14 May 2024 19:21:57 -0300 Subject: [PATCH 2/2] Revive bots --- apps/bot_manager/lib/game_socket_handler.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/bot_manager/lib/game_socket_handler.ex b/apps/bot_manager/lib/game_socket_handler.ex index 22f651f9f..1d98eb555 100644 --- a/apps/bot_manager/lib/game_socket_handler.ex +++ b/apps/bot_manager/lib/game_socket_handler.ex @@ -26,8 +26,8 @@ defmodule BotManager.GameSocketHandler do ####################### def handle_connect(_conn, state) do - # send(self(), :decide_action) - # send(self(), :perform_action) + send(self(), :decide_action) + send(self(), :perform_action) {:ok, state} end