From ae8e23f3dba26692b8fce84e52b4e1c37437e290 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Thu, 11 Jan 2024 22:55:17 +0200 Subject: [PATCH] Streamline the send functions in Replicator a bit Calling the targeted send function with a target of zero will now forward to the broadcast function, avoiding the need to branch at call sites. --- source/game/replicator.cpp | 30 ++++++++---------------------- source/game/replicator.h | 9 ++++++--- 2 files changed, 14 insertions(+), 25 deletions(-) diff --git a/source/game/replicator.cpp b/source/game/replicator.cpp index 9fd6eb5..5a90aef 100644 --- a/source/game/replicator.cpp +++ b/source/game/replicator.cpp @@ -60,7 +60,7 @@ void Replicator::tick(Time::TimeDelta) { event_source.emit(i->entity, p.get_player_id()); if(i->visible_to_players && is_server()) - send_possession(*i, -1); + send_possession(*i, 0); } p.clear_changed(); } @@ -72,7 +72,7 @@ void Replicator::deferred_tick() for(ReplicatedEntity &e: entities) if(!e.visible_to_players) { - send_spawn(e, -1); + send_spawn(e, 0); e.visible_to_players = true; } } @@ -101,9 +101,9 @@ void Replicator::component_created(const Events::ComponentCreated &event) } } -void Replicator::send_spawn(const ReplicatedEntity &re, int target) +void Replicator::send_spawn(const ReplicatedEntity &re, unsigned target) { - if(target<0 && players.empty()) + if(!target && players.empty()) return; const SpawnInfo &info = re.zygote->get_spawn_info(); @@ -115,14 +115,7 @@ void Replicator::send_spawn(const ReplicatedEntity &re, int target) message.position = tf.position; message.rotation = tf.rotation; message.scale = tf.scale; - - if(target>=0) - networking.send(target, message); - else - { - for(unsigned p: players) - networking.send(p, message); - } + send(target, message); info.spawner->notify_spawn_sent(re.entity, target); @@ -130,22 +123,15 @@ void Replicator::send_spawn(const ReplicatedEntity &re, int target) send_possession(re, target); } -void Replicator::send_possession(const ReplicatedEntity &re, int target) +void Replicator::send_possession(const ReplicatedEntity &re, unsigned target) { - if(target<0 && players.empty()) + if(!target && players.empty()) return; Messages::GrantPossession grant; grant.entity_id = re.zygote->get_entity_id(); grant.player_id = re.possessed->get_player_id(); - - if(target>=0) - networking.send(target, grant); - else - { - for(unsigned p: players) - networking.send(p, grant); - } + send(target, grant); } void Replicator::receive(const Messages::SpawnEntity &message) diff --git a/source/game/replicator.h b/source/game/replicator.h index 23c5b66..1efc30b 100644 --- a/source/game/replicator.h +++ b/source/game/replicator.h @@ -69,8 +69,8 @@ public: private: void component_created(const Events::ComponentCreated &); - void send_spawn(const ReplicatedEntity &, int); - void send_possession(const ReplicatedEntity &, int); + void send_spawn(const ReplicatedEntity &, unsigned); + void send_possession(const ReplicatedEntity &, unsigned); void receive(const Messages::SpawnEntity &) override; void receive(const Messages::GrantPossession &) override; @@ -104,7 +104,10 @@ void Replicator::send(Handle entity, const P &packet) template void Replicator::send(unsigned target, const P &packet) { - networking.send(target, packet); + if(target) + networking.send(target, packet); + else + send(packet); } } // namespace Msp::Game -- 2.45.2