]> git.tdb.fi Git - r2c2.git/commitdiff
Export AI control parameters over the network
authorMikko Rasa <tdb@tdb.fi>
Thu, 9 Apr 2015 07:07:52 +0000 (10:07 +0300)
committerMikko Rasa <tdb@tdb.fi>
Thu, 9 Apr 2015 07:46:57 +0000 (10:46 +0300)
I'm going to revamp the whole network thing a bit later, but this at
least allows it to function.

source/libr2c2/aicontrol.cpp
source/network/client.cpp
source/network/client.h
source/network/packets.h
source/network/protocol.cpp
source/network/server.cpp
source/network/server.h
source/network/train.cpp
source/network/train.h

index f1f16bf1850a511c25059aa247356ec4445fbcee..e3ab1e47bf2886615c9cf7051be4b4e81a24395c 100644 (file)
@@ -29,6 +29,9 @@ void AIControl::set_target_speed(float s)
 void AIControl::set_reverse(bool r)
 {
        pending_reverse = r;
+       if(r==reverse)
+               return;
+
        if(train.get_controller().get_speed())
                set_target_speed(0);
        else
index ec8e408582a657f2f1a3b07576c56735c5fb0c12..15d0b407691fbb3e526f365aa877b55d105aa053 100644 (file)
@@ -96,6 +96,11 @@ void Client::receive(const TrainFunctionPacket &pkt)
        get_train(pkt.address).process_packet(pkt);
 }
 
+void Client::receive(const TrainAIControlPacket &pkt)
+{
+       get_train(pkt.address).process_packet(pkt);
+}
+
 void Client::receive(const TrainStatusPacket &pkt)
 {
        get_train(pkt.address).process_packet(pkt);
index 02931e553d69a9efc1a8afed0d4714da924654f2..9f142df89e15492a824fd46fbebc726bc39a9f62 100644 (file)
@@ -15,6 +15,7 @@ class Client: Msp::Net::PacketReceiver<DriverStatePacket>,
        Msp::Net::PacketReceiver<TrainInfoPacket>,
        Msp::Net::PacketReceiver<TrainFunctionPacket>,
        Msp::Net::PacketReceiver<TrainControlPacket>,
+       Msp::Net::PacketReceiver<TrainAIControlPacket>,
        Msp::Net::PacketReceiver<TrainStatusPacket>,
        Msp::Net::PacketReceiver<RouteInfoPacket>,
        Msp::Net::PacketReceiver<TrainRoutePacket>,
@@ -62,6 +63,7 @@ private:
        virtual void receive(const TrainInfoPacket &);
        virtual void receive(const TrainControlPacket &);
        virtual void receive(const TrainFunctionPacket &);
+       virtual void receive(const TrainAIControlPacket &);
        virtual void receive(const TrainStatusPacket &);
        virtual void receive(const RouteInfoPacket &);
        virtual void receive(const TrainRoutePacket &);
index f720dcc63029b933024270415a5f13d17cf0caf9..1982eea5966525e07d4978b45e182daf0ab41af8 100644 (file)
@@ -36,6 +36,13 @@ struct TrainFunctionPacket
        unsigned functions;
 };
 
+struct TrainAIControlPacket
+{
+       unsigned address;
+       float target_speed;
+       char reverse;
+};
+
 struct TrainStatusPacket
 {
        unsigned address;
index 2cd893e7a55689ed8f4b52649cfa9c1ed41255d9..f7a690997088b12d07fa831250a8265dfdc4d630 100644 (file)
@@ -14,6 +14,8 @@ Protocol::Protocol()
                (&TrainControlPacket::control) (&TrainControlPacket::value);
        add<TrainFunctionPacket>() (&TrainFunctionPacket::address)
                (&TrainFunctionPacket::functions);
+       add<TrainAIControlPacket>() (&TrainAIControlPacket::address)
+               (&TrainAIControlPacket::target_speed) (&TrainAIControlPacket::reverse);
        add<TrainStatusPacket>() (&TrainStatusPacket::address)
                (&TrainStatusPacket::status);
        add<RouteInfoPacket>() (&RouteInfoPacket::name);
index c60eb70e109f296ccad3a820f10df62c958a6798..43dce66cfc7353022df95b4893fba47710df31d6 100644 (file)
@@ -1,6 +1,7 @@
 #include <msp/core/refptr.h>
 #include <msp/net/inet.h>
 #include <msp/net/resolve.h>
+#include "libr2c2/aicontrol.h"
 #include "libr2c2/catalogue.h"
 #include "libr2c2/driver.h"
 #include "libr2c2/route.h"
@@ -100,7 +101,7 @@ void Server::train_function_changed(const Train &train, unsigned, bool)
        send(pkt);
 }
 
-void Server::train_ai_event(const Train &train, TrainAI &, const TrainAI::Message &ev)
+void Server::train_ai_event(const Train &train, TrainAI &ai, const TrainAI::Message &ev)
 {
        if(ev.type=="route-changed")
        {
@@ -117,6 +118,18 @@ void Server::train_ai_event(const Train &train, TrainAI &, const TrainAI::Messag
                pkt.status = ev.value.value<std::string>();
                send(pkt);
        }
+       else if(ev.type=="target-speed-changed" || ev.type=="reverse-changed")
+       {
+               AIControl *control = dynamic_cast<AIControl *>(&ai);
+               if(control)
+               {
+                       TrainAIControlPacket pkt;
+                       pkt.address = train.get_address();
+                       pkt.target_speed = control->get_target_speed();
+                       pkt.reverse = control->get_reverse();
+                       send(pkt);
+               }
+       }
 }
 
 template<typename P>
@@ -175,6 +188,14 @@ void Server::Connection::handshake_done()
                        pkt.name = train.get_name();
                        comm.send(pkt);
                }
+               if(AIControl *control = train.get_ai_of_type<AIControl>())
+               {
+                       TrainAIControlPacket pkt;
+                       pkt.address = train.get_address();
+                       pkt.target_speed = control->get_target_speed();
+                       pkt.reverse = control->get_reverse();
+                       comm.send(pkt);
+               }
                for(unsigned j=0;; ++j)
                {
                        const char *name = train.get_controller().enumerate_controls(j);
@@ -252,6 +273,20 @@ void Server::Connection::receive(const TrainFunctionPacket &pkt)
        }
 }
 
+void Server::Connection::receive(const TrainAIControlPacket &pkt)
+{
+       try
+       {
+               Train &train = server.layout.get_train(pkt.address);
+               train.ai_message(TrainAI::Message("set-target-speed", pkt.target_speed));
+               train.ai_message(TrainAI::Message("set-reverse", pkt.reverse));
+       }
+       catch(const exception &e)
+       {
+               error(e.what());
+       }
+}
+
 void Server::Connection::receive(const TrainRoutePacket &pkt)
 {
        try
index 50c0f72fae98d6ac2a0f0ce00e7d49a9b194ad02..402e55cd6ec1bf62507218f025fb61fbccbc45f7 100644 (file)
@@ -18,6 +18,7 @@ private:
        struct Connection: Msp::Net::PacketReceiver<DriverStatePacket>,
                Msp::Net::PacketReceiver<TrainControlPacket>,
                Msp::Net::PacketReceiver<TrainFunctionPacket>,
+               Msp::Net::PacketReceiver<TrainAIControlPacket>,
                Msp::Net::PacketReceiver<TrainRoutePacket>
        {
                Server &server;
@@ -33,6 +34,7 @@ private:
                virtual void receive(const DriverStatePacket &);
                virtual void receive(const TrainControlPacket &);
                virtual void receive(const TrainFunctionPacket &);
+               virtual void receive(const TrainAIControlPacket &);
                virtual void receive(const TrainRoutePacket &);
                void comm_error(const std::exception &);
                void error(const std::string &);
index 4ba4df08870891e354c10c86489bc3c82a0290ac..52eca849b30421c79591f6296ef6a266f125b6a0 100644 (file)
@@ -10,6 +10,8 @@ NetTrain::NetTrain(Client &c, const TrainInfoPacket &pkt):
        loco_type(client.get_catalogue().get<VehicleType>(pkt.loco_type)),
        address(pkt.address),
        name(pkt.name),
+       target_speed(0),
+       reverse(false),
        functions(0)
 { }
 
@@ -31,6 +33,24 @@ float NetTrain::get_control(const string &c) const
        return i!=controls.end() ? i->second : 0;
 }
 
+void NetTrain::set_target_speed(float s)
+{
+       TrainAIControlPacket pkt;
+       pkt.address = address;
+       pkt.target_speed = s;
+       pkt.reverse = reverse;
+       client.send(pkt);
+}
+
+void NetTrain::set_reverse(bool r)
+{
+       TrainAIControlPacket pkt;
+       pkt.address = address;
+       pkt.target_speed = target_speed;
+       pkt.reverse = r;
+       client.send(pkt);
+}
+
 void NetTrain::set_function(unsigned i, bool set)
 {
        TrainFunctionPacket pkt;
@@ -69,6 +89,21 @@ void NetTrain::process_packet(const TrainFunctionPacket &pkt)
                        signal_function_changed.emit(i, (functions>>i)&1);
 }
 
+void NetTrain::process_packet(const TrainAIControlPacket &pkt)
+{
+       if(pkt.target_speed!=target_speed)
+       {
+               target_speed = pkt.target_speed;
+               signal_target_speed_changed.emit(target_speed);
+       }
+
+       if(pkt.reverse!=reverse)
+       {
+               reverse = pkt.reverse;
+               signal_reverse_changed.emit(reverse);
+       }
+}
+
 void NetTrain::process_packet(const TrainStatusPacket &pkt)
 {
        status = pkt.status;
index 948af2f82b5dfc45d94da55e8bcfcdcc139559ac..5c938dcbe0afc9517cb565e654912cfb973d4b58 100644 (file)
@@ -15,6 +15,8 @@ public:
        sigc::signal<void, const std::string &> signal_name_changed;
        sigc::signal<void, const std::string &, float> signal_control_changed;
        sigc::signal<void, unsigned, bool> signal_function_changed;
+       sigc::signal<void, float> signal_target_speed_changed;
+       sigc::signal<void, bool> signal_reverse_changed;
        sigc::signal<void, const std::string &> signal_route_changed;
        sigc::signal<void, const std::string &> signal_status_changed;
 
@@ -24,6 +26,8 @@ private:
        unsigned address;
        std::string name;
        std::map<std::string, float> controls;
+       float target_speed;
+       bool reverse;
        unsigned functions;
        std::string route;
        std::string status;
@@ -36,6 +40,10 @@ public:
        const std::string &get_name() const { return name; }
        void set_control(const std::string &, float);
        float get_control(const std::string &) const;
+       void set_target_speed(float);
+       void set_reverse(bool);
+       float get_target_speed() const { return target_speed; }
+       bool get_reverse() const { return reverse; }
        void set_function(unsigned, bool);
        bool get_function(unsigned i) const { return (functions>>i)&1; }
        void set_route(const std::string &);
@@ -43,6 +51,7 @@ public:
 
        void process_packet(const TrainControlPacket &);
        void process_packet(const TrainFunctionPacket &);
+       void process_packet(const TrainAIControlPacket &);
        void process_packet(const TrainRoutePacket &);
        void process_packet(const TrainStatusPacket &);
 };