Skip to content

Commit

Permalink
Separate movement from tick (#697)
Browse files Browse the repository at this point in the history
* separate movement from tick

* ignore vscode settings

* format an erase comment

* remove tick reference at leaping

* adjust configs

* disable bots

* add message field

---------

Co-authored-by: Manuel Camejo <[email protected]>
Co-authored-by: agustinesco <[email protected]>
  • Loading branch information
3 people authored Jul 3, 2024
1 parent 3ca50ab commit 09a8487
Show file tree
Hide file tree
Showing 15 changed files with 72 additions and 37 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ apps/*/_build/*

# Ignore google fetched certificates
apps/gateway/priv/google.oauth2.certificates.json
.vscode/settings.json
3 changes: 2 additions & 1 deletion apps/arena/lib/arena/entities.ex
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,8 @@ defmodule Arena.Entities do
inventory: entity.aditional_info.inventory,
cooldowns: entity.aditional_info.cooldowns,
visible_players: entity.aditional_info.visible_players,
on_bush: entity.aditional_info.on_bush
on_bush: entity.aditional_info.on_bush,
forced_movement: entity.aditional_info.forced_movement
}}
end

Expand Down
3 changes: 1 addition & 2 deletions apps/arena/lib/arena/game/player.ex
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,7 @@ defmodule Arena.Game.Player do
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
Physics.calculate_duration(position, target_position, leap.speed)
end

defp calculate_duration(%{mechanics: [_]} = skill, _, _, _) do
Expand Down
15 changes: 7 additions & 8 deletions apps/arena/lib/arena/game_updater.ex
Original file line number Diff line number Diff line change
Expand Up @@ -195,20 +195,19 @@ defmodule Arena.GameUpdater do
def handle_info(:update_game, %{game_state: game_state} = state) do
Process.send_after(self(), :update_game, state.game_config.game.tick_rate_ms)
now = DateTime.utc_now() |> DateTime.to_unix(:millisecond)
time_diff = now - game_state.server_timestamp
ticks_to_move = time_diff / state.game_config.game.tick_rate_ms
delta_time = now - game_state.server_timestamp

game_state =
game_state
|> Map.put(:ticks_to_move, ticks_to_move)
|> Map.put(:delta_time, delta_time / 1)
# Effects
|> remove_expired_effects()
|> remove_effects_on_action()
|> reset_players_effects()
|> Effect.apply_effect_mechanic_to_entities()
# Players
|> move_players()
|> reduce_players_cooldowns(time_diff)
|> reduce_players_cooldowns(delta_time)
|> resolve_players_collisions_with_power_ups()
|> resolve_players_collisions_with_items()
|> resolve_projectiles_effects_on_collisions(state.game_config)
Expand Down Expand Up @@ -894,7 +893,7 @@ defmodule Arena.GameUpdater do
defp move_players(
%{
players: players,
ticks_to_move: ticks_to_move,
delta_time: delta_time,
external_wall: external_wall,
obstacles: obstacles,
bushes: bushes,
Expand All @@ -911,7 +910,7 @@ defmodule Arena.GameUpdater do
moved_players =
players
|> Physics.move_entities(
ticks_to_move,
delta_time,
external_wall,
obstacles
)
Expand Down Expand Up @@ -947,7 +946,7 @@ defmodule Arena.GameUpdater do
obstacles: obstacles,
crates: crates,
external_wall: external_wall,
ticks_to_move: ticks_to_move,
delta_time: delta_time,
pools: pools
} = game_state
) do
Expand All @@ -967,7 +966,7 @@ defmodule Arena.GameUpdater do

moved_projectiles =
alive_projectiles
|> Physics.move_entities(ticks_to_move, external_wall, %{})
|> Physics.move_entities(delta_time, external_wall, %{})
|> update_collisions(
projectiles,
entities_to_collide_with
Expand Down
1 change: 1 addition & 0 deletions apps/arena/lib/arena/serialization/messages.pb.ex
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ defmodule Arena.Serialization.Player do
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")
field(:forced_movement, 15, type: :bool, json_name: "forcedMovement")
end

defmodule Arena.Serialization.Effect do
Expand Down
6 changes: 3 additions & 3 deletions apps/arena/lib/physics.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ defmodule Physics do
def add(_arg1, _arg2), do: :erlang.nif_error(:nif_not_loaded)
def check_collisions(_entity, _entities), do: :erlang.nif_error(:nif_not_loaded)

def move_entities(_entities, _ticks_to_move, _external_wall, _obstacles),
def move_entities(_entities, _delta_time, _external_wall, _obstacles),
do: :erlang.nif_error(:nif_not_loaded)

def move_entity(_entity, _ticks_to_move, _external_wall, _obstacles),
def move_entity(_entity, _delta_time, _external_wall, _obstacles),
do: :erlang.nif_error(:nif_not_loaded)

def move_entity(_entity, _ticks_to_move, _external_wall), do: :erlang.nif_error(:nif_not_loaded)
def move_entity(_entity, _delta_time, _external_wall), do: :erlang.nif_error(:nif_not_loaded)

def get_closest_available_position(_entity, _new_position, _external_wall, _obstacles),
do: :erlang.nif_error(:nif_not_loaded)
Expand Down
8 changes: 4 additions & 4 deletions apps/arena/native/physics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ fn add(a: i64, b: i64) -> i64 {
#[rustler::nif()]
fn move_entities(
entities: HashMap<u64, Entity>,
ticks_to_move: f32,
delta_time: f32,
external_wall: Entity,
obstacles: HashMap<u64, Entity>,
) -> HashMap<u64, Entity> {
let mut entities: HashMap<u64, Entity> = entities;

for entity in entities.values_mut() {
if entity.is_moving {
entity.move_entity(ticks_to_move);
entity.move_entity(delta_time);

move_entity_to_closest_available_position(entity, &external_wall, &obstacles);
}
Expand All @@ -35,13 +35,13 @@ fn move_entities(
#[rustler::nif()]
fn move_entity(
entity: Entity,
ticks_to_move: f32,
delta_time: f32,
external_wall: Entity,
obstacles: HashMap<u64, Entity>,
) -> Entity {
let mut entity: Entity = entity;
if entity.is_moving {
entity.move_entity(ticks_to_move);
entity.move_entity(delta_time);
move_entity_to_closest_available_position(&mut entity, &external_wall, &obstacles);
}

Expand Down
10 changes: 5 additions & 5 deletions apps/arena/native/physics/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,14 @@ impl Entity {
result
}

pub fn move_entity(&mut self, ticks_to_move: f32) {
self.position = self.next_position(ticks_to_move);
pub fn move_entity(&mut self, delta_time: f32) {
self.position = self.next_position(delta_time);
}

pub fn next_position(&mut self, ticks_to_move: f32) -> Position {
pub fn next_position(&mut self, delta_time: f32) -> Position {
Position {
x: self.position.x + self.direction.x * self.speed * ticks_to_move,
y: self.position.y + self.direction.y * self.speed * ticks_to_move,
x: self.position.x + self.direction.x * self.speed * delta_time,
y: self.position.y + self.direction.y * self.speed * delta_time,
}
}

Expand Down
16 changes: 8 additions & 8 deletions apps/arena/priv/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,7 @@
{
"leap": {
"range": 1300.0,
"speed": 50.0,
"speed": 1.7,
"radius": 600,
"on_arrival_mechanic": {
"circle_hit": {
Expand Down Expand Up @@ -976,7 +976,7 @@
"multi_shoot": {
"angle_between": 22.0,
"amount": 3,
"speed": 53.0,
"speed": 1.1,
"duration_ms": 1000,
"remove_on_collision": true,
"projectile_offset": 100,
Expand All @@ -1001,7 +1001,7 @@
"mechanics": [
{
"dash": {
"speed": 90.0,
"speed": 4.0,
"duration": 250
}
}
Expand All @@ -1021,7 +1021,7 @@
"mechanics": [
{
"dash": {
"speed": 80.0,
"speed": 3.3,
"duration": 330
}
}
Expand Down Expand Up @@ -1090,7 +1090,7 @@
"mechanics": [
{
"dash": {
"speed": 95.0,
"speed": 4.0,
"duration": 250
}
}
Expand Down Expand Up @@ -1134,7 +1134,7 @@
"mechanics": [
{
"simple_shoot": {
"speed": 45.0,
"speed": 1.8,
"duration_ms": 1100,
"remove_on_collision": true,
"projectile_offset": 100,
Expand Down Expand Up @@ -1291,7 +1291,7 @@
"one_time_application": true,
"effect_mechanics": {
"speed_boost": {
"modifier": 0.5,
"modifier": 0.02,
"effect_delay_ms": 0,
"execute_multiple_times": false
}
Expand Down Expand Up @@ -1338,7 +1338,7 @@
"execute_multiple_times": true
},
"speed_boost": {
"modifier": -0.25,
"modifier": -0.009,
"effect_delay_ms": 0,
"execute_multiple_times": false
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,7 @@ defmodule ArenaLoadTest.Serialization.Player do

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

defmodule ArenaLoadTest.Serialization.Effect do
Expand Down
1 change: 1 addition & 0 deletions apps/bot_manager/lib/protobuf/messages.pb.ex
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ defmodule BotManager.Protobuf.Player do
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")
field(:forced_movement, 15, type: :bool, json_name: "forcedMovement")
end

defmodule BotManager.Protobuf.Effect do
Expand Down
8 changes: 4 additions & 4 deletions apps/game_backend/priv/characters_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"name": "muflus",
"active": true,
"base_speed": 17.5,
"base_speed": 0.63,
"base_size": 110.0,
"base_health": 440,
"base_stamina": 3,
Expand All @@ -20,7 +20,7 @@
{
"name": "h4ck",
"active": true,
"base_speed": 22.5,
"base_speed": 0.7,
"base_size": 90.0,
"base_health": 400,
"base_stamina": 3,
Expand All @@ -37,7 +37,7 @@
{
"name": "uma",
"active": true,
"base_speed": 20.0,
"base_speed": 0.67,
"base_size": 95.0,
"base_health": 400,
"base_stamina": 3,
Expand All @@ -54,7 +54,7 @@
{
"name": "valtimer",
"active": false,
"base_speed": 20.0,
"base_speed": 0.68,
"base_size": 100.0,
"base_health": 400,
"base_stamina": 3,
Expand Down
32 changes: 31 additions & 1 deletion apps/game_client/assets/js/protobuf/messages_pb.js
Original file line number Diff line number Diff line change
Expand Up @@ -6368,7 +6368,8 @@ proto.Player.toObject = function(includeInstance, msg) {
inventory: (f = msg.getInventory()) && proto.Item.toObject(includeInstance, f),
cooldownsMap: (f = msg.getCooldownsMap()) ? f.toObject(includeInstance, undefined) : [],
visiblePlayersList: (f = jspb.Message.getRepeatedField(msg, 13)) == null ? undefined : f,
onBush: jspb.Message.getBooleanFieldWithDefault(msg, 14, false)
onBush: jspb.Message.getBooleanFieldWithDefault(msg, 14, false),
forcedMovement: jspb.Message.getBooleanFieldWithDefault(msg, 15, false)
};

if (includeInstance) {
Expand Down Expand Up @@ -6468,6 +6469,10 @@ proto.Player.deserializeBinaryFromReader = function(msg, reader) {
var value = /** @type {boolean} */ (reader.readBool());
msg.setOnBush(value);
break;
case 15:
var value = /** @type {boolean} */ (reader.readBool());
msg.setForcedMovement(value);
break;
default:
reader.skipField();
break;
Expand Down Expand Up @@ -6595,6 +6600,13 @@ proto.Player.serializeBinaryToWriter = function(message, writer) {
f
);
}
f = message.getForcedMovement();
if (f) {
writer.writeBool(
15,
f
);
}
};


Expand Down Expand Up @@ -6933,6 +6945,24 @@ proto.Player.prototype.setOnBush = function(value) {
};


/**
* optional bool forced_movement = 15;
* @return {boolean}
*/
proto.Player.prototype.getForcedMovement = function() {
return /** @type {boolean} */ (jspb.Message.getBooleanFieldWithDefault(this, 15, false));
};


/**
* @param {boolean} value
* @return {!proto.Player} returns this
*/
proto.Player.prototype.setForcedMovement = function(value) {
return jspb.Message.setProto3BooleanField(this, 15, value);
};





Expand Down
1 change: 1 addition & 0 deletions apps/game_client/lib/game_client/protobuf/messages.pb.ex
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ defmodule GameClient.Protobuf.Player do
field(:cooldowns, 12, repeated: true, type: GameClient.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")
field(:forced_movement, 15, type: :bool, json_name: "forcedMovement")
end

defmodule GameClient.Protobuf.Effect do
Expand Down
3 changes: 2 additions & 1 deletion apps/serialization/messages.proto
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ message Player {
Item inventory = 11;
map<string, uint64> cooldowns = 12;
repeated uint64 visible_players = 13;
bool on_bush = 14;
bool on_bush = 14;
bool forced_movement = 15;
}

message Effect {
Expand Down

0 comments on commit 09a8487

Please sign in to comment.