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

[GH-414] add bush mechanic #417

Merged
merged 18 commits into from
May 7, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 24 additions & 2 deletions apps/arena/lib/arena/entities.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ defmodule Arena.Entities do
damage_immunity: false,
effects: [],
cooldowns: %{},
bonus_damage: 0
bonus_damage: 0,
visible_players: [],
on_bush: false
}
}
end
Expand Down Expand Up @@ -186,6 +188,24 @@ defmodule Arena.Entities do
}
end

def new_bush(id, position, radius, shape, vertices \\ []) do
%{
id: id,
category: :bush,
shape: get_shape(shape),
name: "Bush" <> Integer.to_string(id),
position: position,
radius: radius,
vertices: vertices,
speed: 0.0,
direction: %{
x: 0.0,
y: 0.0
},
is_moving: false
}
end

def new_crate(id, %{
position: position,
radius: radius,
Expand Down Expand Up @@ -263,7 +283,9 @@ defmodule Arena.Entities do
effects: entity.aditional_info.effects,
power_ups: entity.aditional_info.power_ups,
inventory: entity.aditional_info.inventory,
cooldowns: entity.aditional_info.cooldowns
cooldowns: entity.aditional_info.cooldowns,
visible_players: entity.aditional_info.visible_players,
on_bush: entity.aditional_info.on_bush
}}
end

Expand Down
58 changes: 57 additions & 1 deletion apps/arena/lib/arena/game_updater.ex
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ defmodule Arena.GameUpdater do
|> resolve_players_collisions_with_items()
|> resolve_projectiles_effects_on_collisions(state.game_config)
|> apply_zone_damage_to_players(state.game_config.game)
|> update_visible_players(state.game_config)
# Projectiles
|> update_projectiles_status()
|> move_projectiles()
Expand Down Expand Up @@ -443,6 +444,7 @@ defmodule Arena.GameUpdater do
projectiles: complete_entities(state.projectiles),
power_ups: complete_entities(state.power_ups),
pools: complete_entities(state.pools),
bushes: complete_entities(state.bushes),
items: complete_entities(state.items),
server_timestamp: state.server_timestamp,
player_timestamps: state.player_timestamps,
Expand Down Expand Up @@ -552,10 +554,12 @@ defmodule Arena.GameUpdater do

{obstacles, last_id} = initialize_obstacles(config.map.obstacles, game.last_id)
{crates, last_id} = initialize_crates(config.crates, last_id)
{bushes, last_id} = initialize_bushes(config.map.bushes, last_id)

game
|> Map.put(:last_id, last_id)
|> Map.put(:obstacles, obstacles)
|> Map.put(:bushes, bushes)
|> Map.put(:crates, crates)
end

Expand All @@ -578,6 +582,21 @@ defmodule Arena.GameUpdater do
end)
end

defp initialize_bushes(bushes, last_id) do
Enum.reduce(bushes, {Map.new(), last_id}, fn bush, {bush_acc, last_id} ->
last_id = last_id + 1

bush_acc =
Map.put(
bush_acc,
last_id,
Entities.new_bush(last_id, bush.position, bush.radius, bush.shape, bush.vertices)
)

{bush_acc, last_id}
end)
end

# Initialize crates
defp initialize_crates(crates, last_id) do
Enum.reduce(crates, {Map.new(), last_id}, fn crate, {crates_acc, last_id} ->
Expand Down Expand Up @@ -657,12 +676,13 @@ defmodule Arena.GameUpdater do
ticks_to_move: ticks_to_move,
external_wall: external_wall,
obstacles: obstacles,
bushes: bushes,
power_ups: power_ups,
pools: pools,
items: items
} = game_state
) do
entities_to_collide = Map.merge(power_ups, pools) |> Map.merge(items)
entities_to_collide = Map.merge(power_ups, pools) |> Map.merge(items) |> Map.merge(bushes)

moved_players =
players
Expand Down Expand Up @@ -1218,6 +1238,42 @@ defmodule Arena.GameUpdater do
end
end

defp update_visible_players(%{players: players, bushes: bushes} = game_state, game_config) do
Enum.reduce(players, game_state, fn {player_id, player}, game_state ->
bush_collisions =
Enum.filter(player.collides_with, fn collided_id ->
Map.has_key?(bushes, collided_id)
end)

visible_players =
Map.delete(players, player_id)
|> Enum.reduce([], fn {candicandidate_player_id, candidate_player}, acc ->
candidate_bush_collisions =
Enum.filter(candidate_player.collides_with, fn collided_id ->
Map.has_key?(bushes, collided_id)
end)

players_in_same_bush? =
Enum.any?(bush_collisions, fn collided_id -> collided_id in candidate_bush_collisions end)

players_close_enough? =
Physics.distance_between_entities(player, candidate_player) <=
game_config.bushes.field_of_view_inside_bush

if Enum.empty?(candidate_bush_collisions) or (players_in_same_bush? and players_close_enough?) do
[candicandidate_player_id | acc]
else
acc
end
end)

update_in(game_state, [:players, player_id, :aditional_info], fn aditional_info ->
Map.put(aditional_info, :visible_players, visible_players)
|> Map.put(:on_bush, not Enum.empty?(bush_collisions))
end)
end)
end

# You'll only apply effect to owned entities or
# entities without an owner, implement behavior if needed
defp get_entities_to_apply(collided_entities, projectile) do
Expand Down
19 changes: 19 additions & 0 deletions apps/arena/lib/arena/serialization/messages.pb.ex
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,15 @@ defmodule Arena.Serialization.GameState.CratesEntry do
field(:value, 2, type: Arena.Serialization.Entity)
end

defmodule Arena.Serialization.GameState.BushesEntry do
@moduledoc false

use Protobuf, map: true, syntax: :proto3, protoc_gen_elixir_version: "0.12.0"

field(:key, 1, type: :uint64)
field(:value, 2, type: Arena.Serialization.Entity)
end

defmodule Arena.Serialization.GameState do
@moduledoc false

Expand Down Expand Up @@ -336,6 +345,7 @@ defmodule Arena.Serialization.GameState do

field(:pools, 15, repeated: true, type: Arena.Serialization.GameState.PoolsEntry, map: true)
field(:crates, 16, repeated: true, type: Arena.Serialization.GameState.CratesEntry, map: true)
field(:bushes, 17, repeated: true, type: Arena.Serialization.GameState.BushesEntry, map: true)
end

defmodule Arena.Serialization.Entity do
Expand Down Expand Up @@ -363,6 +373,7 @@ defmodule Arena.Serialization.Entity do
field(:item, 16, type: Arena.Serialization.Item, oneof: 0)
field(:pool, 17, type: Arena.Serialization.Pool, oneof: 0)
field(:crate, 18, type: Arena.Serialization.Crate, oneof: 0)
field(:bush, 19, type: Arena.Serialization.Bush, oneof: 0)
end

defmodule Arena.Serialization.Player.CooldownsEntry do
Expand Down Expand Up @@ -397,6 +408,8 @@ defmodule Arena.Serialization.Player do
field(:effects, 10, repeated: true, type: Arena.Serialization.Effect)
field(:inventory, 11, type: Arena.Serialization.Item)
field(:cooldowns, 12, repeated: true, type: Arena.Serialization.Player.CooldownsEntry, map: true)
field(:visible_players, 13, repeated: true, type: :uint64, json_name: "visiblePlayers")
field(:on_bush, 14, type: :bool, json_name: "onBush")
end

defmodule Arena.Serialization.Effect do
Expand Down Expand Up @@ -462,6 +475,12 @@ defmodule Arena.Serialization.Pool do
field(:owner_id, 1, type: :uint64, json_name: "ownerId")
end

defmodule Arena.Serialization.Bush do
@moduledoc false

use Protobuf, syntax: :proto3, protoc_gen_elixir_version: "0.12.0"
end

defmodule Arena.Serialization.PlayerAction do
@moduledoc false

Expand Down
2 changes: 2 additions & 0 deletions apps/arena/lib/physics.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,7 @@ defmodule Physics do
def calculate_speed(_position_a, _position_b, _duration_ms),
do: :erlang.nif_error(:nif_not_loaded)

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)
end
7 changes: 7 additions & 0 deletions apps/arena/native/physics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ fn nearest_entity_direction_in_range(

direction
}
#[rustler::nif()]
fn distance_between_entities(entity_a: Entity, entity_b: Entity) -> f32 {
distance_between_positions(entity_a.position, entity_b.position)
- entity_a.radius
- entity_b.radius
}

fn move_entity_to_closest_available_position(
entity: &mut Entity,
Expand Down Expand Up @@ -218,6 +224,7 @@ rustler::init!(
calculate_triangle_vertices,
get_direction_from_positions,
calculate_speed,
distance_between_entities,
nearest_entity_direction_in_range,
get_closest_available_position
]
Expand Down
1 change: 1 addition & 0 deletions apps/arena/native/physics/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub enum Category {
PowerUp,
Pool,
Item,
Bush,
Crate,
}

Expand Down
6 changes: 5 additions & 1 deletion apps/arena/priv/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,8 @@
}
]
}
]
],
"bushes": []
},
"game": {
"tick_rate_ms": 30,
Expand Down Expand Up @@ -1405,5 +1406,8 @@
}
}
],
"bushes": {
"field_of_view_inside_bush": 500
},
"crates": []
}
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,15 @@ defmodule ArenaLoadTest.Serialization.GameState.CratesEntry do
field(:value, 2, type: ArenaLoadTest.Serialization.Entity)
end

defmodule ArenaLoadTest.Serialization.GameState.BushesEntry do
@moduledoc false

use Protobuf, map: true, syntax: :proto3, protoc_gen_elixir_version: "0.12.0"

field(:key, 1, type: :uint64)
field(:value, 2, type: ArenaLoadTest.Serialization.Entity)
end

defmodule ArenaLoadTest.Serialization.GameState do
@moduledoc false

Expand Down Expand Up @@ -355,6 +364,12 @@ defmodule ArenaLoadTest.Serialization.GameState do
type: ArenaLoadTest.Serialization.GameState.CratesEntry,
map: true
)

field(:bushes, 17,
repeated: true,
type: ArenaLoadTest.Serialization.GameState.BushesEntry,
map: true
)
end

defmodule ArenaLoadTest.Serialization.Entity do
Expand Down Expand Up @@ -382,6 +397,7 @@ defmodule ArenaLoadTest.Serialization.Entity do
field(:item, 16, type: ArenaLoadTest.Serialization.Item, oneof: 0)
field(:pool, 17, type: ArenaLoadTest.Serialization.Pool, oneof: 0)
field(:crate, 18, type: ArenaLoadTest.Serialization.Crate, oneof: 0)
field(:bush, 19, type: ArenaLoadTest.Serialization.Bush, oneof: 0)
end

defmodule ArenaLoadTest.Serialization.Player.CooldownsEntry do
Expand Down Expand Up @@ -421,6 +437,9 @@ defmodule ArenaLoadTest.Serialization.Player do
type: ArenaLoadTest.Serialization.Player.CooldownsEntry,
map: true
)

field(:visible_players, 13, repeated: true, type: :uint64, json_name: "visiblePlayers")
field(:on_bush, 14, type: :bool, json_name: "onBush")
end

defmodule ArenaLoadTest.Serialization.Effect do
Expand Down Expand Up @@ -486,6 +505,12 @@ defmodule ArenaLoadTest.Serialization.Pool do
field(:owner_id, 1, type: :uint64, json_name: "ownerId")
end

defmodule ArenaLoadTest.Serialization.Bush do
@moduledoc false

use Protobuf, syntax: :proto3, protoc_gen_elixir_version: "0.12.0"
end

defmodule ArenaLoadTest.Serialization.PlayerAction do
@moduledoc false

Expand Down
19 changes: 19 additions & 0 deletions apps/bot_manager/lib/protobuf/messages.pb.ex
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,15 @@ defmodule BotManager.Protobuf.GameState.CratesEntry do
field(:value, 2, type: BotManager.Protobuf.Entity)
end

defmodule BotManager.Protobuf.GameState.BushesEntry do
@moduledoc false

use Protobuf, map: true, syntax: :proto3, protoc_gen_elixir_version: "0.12.0"

field(:key, 1, type: :uint64)
field(:value, 2, type: BotManager.Protobuf.Entity)
end

defmodule BotManager.Protobuf.GameState do
@moduledoc false

Expand Down Expand Up @@ -336,6 +345,7 @@ defmodule BotManager.Protobuf.GameState do

field(:pools, 15, repeated: true, type: BotManager.Protobuf.GameState.PoolsEntry, map: true)
field(:crates, 16, repeated: true, type: BotManager.Protobuf.GameState.CratesEntry, map: true)
field(:bushes, 17, repeated: true, type: BotManager.Protobuf.GameState.BushesEntry, map: true)
end

defmodule BotManager.Protobuf.Entity do
Expand Down Expand Up @@ -363,6 +373,7 @@ defmodule BotManager.Protobuf.Entity do
field(:item, 16, type: BotManager.Protobuf.Item, oneof: 0)
field(:pool, 17, type: BotManager.Protobuf.Pool, oneof: 0)
field(:crate, 18, type: BotManager.Protobuf.Crate, oneof: 0)
field(:bush, 19, type: BotManager.Protobuf.Bush, oneof: 0)
end

defmodule BotManager.Protobuf.Player.CooldownsEntry do
Expand Down Expand Up @@ -397,6 +408,8 @@ defmodule BotManager.Protobuf.Player do
field(:effects, 10, repeated: true, type: BotManager.Protobuf.Effect)
field(:inventory, 11, type: BotManager.Protobuf.Item)
field(:cooldowns, 12, repeated: true, type: BotManager.Protobuf.Player.CooldownsEntry, map: true)
field(:visible_players, 13, repeated: true, type: :uint64, json_name: "visiblePlayers")
field(:on_bush, 14, type: :bool, json_name: "onBush")
end

defmodule BotManager.Protobuf.Effect do
Expand Down Expand Up @@ -462,6 +475,12 @@ defmodule BotManager.Protobuf.Pool do
field(:owner_id, 1, type: :uint64, json_name: "ownerId")
end

defmodule BotManager.Protobuf.Bush do
@moduledoc false

use Protobuf, syntax: :proto3, protoc_gen_elixir_version: "0.12.0"
end

defmodule BotManager.Protobuf.PlayerAction do
@moduledoc false

Expand Down