]> git.tdb.fi Git - libs/game.git/commitdiff
Streamline the send functions in Replicator a bit
authorMikko Rasa <tdb@tdb.fi>
Thu, 11 Jan 2024 20:55:17 +0000 (22:55 +0200)
committerMikko Rasa <tdb@tdb.fi>
Thu, 11 Jan 2024 20:56:43 +0000 (22:56 +0200)
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
source/game/replicator.h

index 9fd6eb52d76e6be8afad858d996f51ac9edd08de..5a90aefe05f1ffbaf4a55a8c103c3d8a389f7716 100644 (file)
@@ -60,7 +60,7 @@ void Replicator::tick(Time::TimeDelta)
                        {
                                event_source.emit<Events::PossessionChanged>(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)
index 23c5b6636d7adb848c8eceadea9cda8b9f1959b9..1efc30bf288495fe5ab3779a40da11360bd981f2 100644 (file)
@@ -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> entity, const P &packet)
 template<typename P>
 void Replicator::send(unsigned target, const P &packet)
 {
-       networking.send(target, packet);
+       if(target)
+               networking.send(target, packet);
+       else
+               send(packet);
 }
 
 } // namespace Msp::Game