From: Mikko Rasa Date: Thu, 9 Apr 2015 07:07:52 +0000 (+0300) Subject: Export AI control parameters over the network X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=3b37ee041af581e47646c2487e7e302c55bd8cd7;p=r2c2.git Export AI control parameters over the network I'm going to revamp the whole network thing a bit later, but this at least allows it to function. --- diff --git a/source/libr2c2/aicontrol.cpp b/source/libr2c2/aicontrol.cpp index f1f16bf..e3ab1e4 100644 --- a/source/libr2c2/aicontrol.cpp +++ b/source/libr2c2/aicontrol.cpp @@ -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 diff --git a/source/network/client.cpp b/source/network/client.cpp index ec8e408..15d0b40 100644 --- a/source/network/client.cpp +++ b/source/network/client.cpp @@ -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); diff --git a/source/network/client.h b/source/network/client.h index 02931e5..9f142df 100644 --- a/source/network/client.h +++ b/source/network/client.h @@ -15,6 +15,7 @@ class Client: Msp::Net::PacketReceiver, Msp::Net::PacketReceiver, Msp::Net::PacketReceiver, Msp::Net::PacketReceiver, + Msp::Net::PacketReceiver, Msp::Net::PacketReceiver, Msp::Net::PacketReceiver, Msp::Net::PacketReceiver, @@ -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 &); diff --git a/source/network/packets.h b/source/network/packets.h index f720dcc..1982eea 100644 --- a/source/network/packets.h +++ b/source/network/packets.h @@ -36,6 +36,13 @@ struct TrainFunctionPacket unsigned functions; }; +struct TrainAIControlPacket +{ + unsigned address; + float target_speed; + char reverse; +}; + struct TrainStatusPacket { unsigned address; diff --git a/source/network/protocol.cpp b/source/network/protocol.cpp index 2cd893e..f7a6909 100644 --- a/source/network/protocol.cpp +++ b/source/network/protocol.cpp @@ -14,6 +14,8 @@ Protocol::Protocol() (&TrainControlPacket::control) (&TrainControlPacket::value); add() (&TrainFunctionPacket::address) (&TrainFunctionPacket::functions); + add() (&TrainAIControlPacket::address) + (&TrainAIControlPacket::target_speed) (&TrainAIControlPacket::reverse); add() (&TrainStatusPacket::address) (&TrainStatusPacket::status); add() (&RouteInfoPacket::name); diff --git a/source/network/server.cpp b/source/network/server.cpp index c60eb70..43dce66 100644 --- a/source/network/server.cpp +++ b/source/network/server.cpp @@ -1,6 +1,7 @@ #include #include #include +#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(); send(pkt); } + else if(ev.type=="target-speed-changed" || ev.type=="reverse-changed") + { + AIControl *control = dynamic_cast(&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 @@ -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()) + { + 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 diff --git a/source/network/server.h b/source/network/server.h index 50c0f72..402e55c 100644 --- a/source/network/server.h +++ b/source/network/server.h @@ -18,6 +18,7 @@ private: struct Connection: Msp::Net::PacketReceiver, Msp::Net::PacketReceiver, Msp::Net::PacketReceiver, + Msp::Net::PacketReceiver, Msp::Net::PacketReceiver { 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 &); diff --git a/source/network/train.cpp b/source/network/train.cpp index 4ba4df0..52eca84 100644 --- a/source/network/train.cpp +++ b/source/network/train.cpp @@ -10,6 +10,8 @@ NetTrain::NetTrain(Client &c, const TrainInfoPacket &pkt): loco_type(client.get_catalogue().get(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; diff --git a/source/network/train.h b/source/network/train.h index 948af2f..5c938dc 100644 --- a/source/network/train.h +++ b/source/network/train.h @@ -15,6 +15,8 @@ public: sigc::signal signal_name_changed; sigc::signal signal_control_changed; sigc::signal signal_function_changed; + sigc::signal signal_target_speed_changed; + sigc::signal signal_reverse_changed; sigc::signal signal_route_changed; sigc::signal signal_status_changed; @@ -24,6 +26,8 @@ private: unsigned address; std::string name; std::map 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 &); };