]> git.tdb.fi Git - r2c2.git/commitdiff
Get rid of the TrainAI tagging system
authorMikko Rasa <tdb@tdb.fi>
Sun, 24 Feb 2013 11:53:46 +0000 (13:53 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sun, 24 Feb 2013 11:53:46 +0000 (13:53 +0200)
Instead add a function to get an AI of a specific type.

The tags were too clumsy and brittle.  Most AIs don't work well with
multiple instances per train, so the type is an adequate identifier.

13 files changed:
source/engineer/routeselect.cpp
source/engineer/trainpanel.cpp
source/engineer/trainproperties.cpp
source/libr2c2/timetable.cpp
source/libr2c2/timetable.h
source/libr2c2/train.cpp
source/libr2c2/train.h
source/libr2c2/trainai.cpp
source/libr2c2/trainai.h
source/libr2c2/trainrouter.cpp
source/libr2c2/trainrouter.h
source/network/server.cpp
source/network/server.h

index e5c090c9bcb26fbe7a9535cf80697264f0a8e667..770a455a4130b7406e22a6f348c38b680b757626 100644 (file)
@@ -24,7 +24,7 @@ RouteSelect::RouteSelect(Engineer &e, Train &t):
        drp_route->set_selected_index(0);
 
        const Route *current_route = 0;
-       if(TrainRouter *router = dynamic_cast<TrainRouter *>(train.get_tagged_ai("router")))
+       if(TrainRouter *router = train.get_ai_of_type<TrainRouter>())
                current_route = router->get_route();
 
        const set<Route *> &routes = engineer.get_layout().get_routes();
index 97592be1bed92dc2547c5fb79ca3eb2399ca38f2..d6066cb1c5a962e489032b4d9d8eda0ef6bbc7d5 100644 (file)
@@ -66,12 +66,9 @@ TrainPanel::TrainPanel(Engineer &e, Train &t):
        pnl_extra->set_geometry(GLtk::Geometry(0, 10, geom.w, 135));
        pnl_extra->set_visible(false);
 
-       TrainRouter *router = dynamic_cast<TrainRouter *>(train.get_tagged_ai("router"));
+       TrainRouter *router = train.get_ai_of_type<TrainRouter>();
        if(!router)
-       {
                router = new TrainRouter(train);
-               router->set_tag("router");
-       }
 
        const Route *route = router->get_route();
        pnl_extra->add(*(lbl_route = new GLtk::Label((route ? route->get_name() : "Free run"))));
@@ -215,12 +212,9 @@ void TrainPanel::goto_clicked()
 
 void TrainPanel::timetable_clicked()
 {
-       Timetable *timetable = dynamic_cast<Timetable *>(train.get_tagged_ai("timetable"));
+       Timetable *timetable = train.get_ai_of_type<Timetable>();
        if(!timetable)
-       {
                timetable = new Timetable(train);
-               timetable->set_tag("timetable");
-       }
 
        TimetableDialog *dialog = new TimetableDialog(*timetable);
        engineer.get_root().add(*dialog);
@@ -284,7 +278,7 @@ void TrainPanel::go_to(Track *track, unsigned)
 {
        pick_conn.disconnect();
 
-       TrainRouter *router = dynamic_cast<TrainRouter *>(train.get_tagged_ai("router"));
+       TrainRouter *router = train.get_ai_of_type<TrainRouter>();
        if(!router || !router->go_to(*track))
                engineer.set_status("Could not set route");
 }
index ecc9b2b73e555c718927ea626d540f00744ba6af..d92750e41725fd7d65e7071fbcfefc7e785c5cb1 100644 (file)
@@ -90,7 +90,7 @@ TrainProperties::TrainProperties(Engineer &e, Train *t):
        {
                ent_addr->set_text(lexical_cast<string>(train->get_address()));
                ent_name->set_text(train->get_name());
-               if(TrainRouter *router = dynamic_cast<TrainRouter *>(train->get_tagged_ai("router")))
+               if(TrainRouter *router = train->get_ai_of_type<TrainRouter>())
                        drp_priority->set_selected_index(router->get_priority()+2);
 
                unsigned n_vehicles = train->get_n_vehicles();
@@ -117,7 +117,7 @@ void TrainProperties::on_ok_clicked()
        }
 
        train->set_name(ent_name->get_text());
-       if(TrainRouter *router = dynamic_cast<TrainRouter *>(train->get_tagged_ai("router")))
+       if(TrainRouter *router = train->get_ai_of_type<TrainRouter>())
                router->set_priority(drp_priority->get_selected_index()-2);
 
        // The locomotive is vehicle 0 so we need to add 1
index d5a09517ef045c830a0903cdb96efa4fa2be6a23..753aae83f719a2f453605a78f6dac417a7bcec30 100644 (file)
@@ -162,8 +162,6 @@ void Timetable::tick(const Time::TimeStamp &t, const Time::TimeDelta &)
 
 void Timetable::save(list<DataFile::Statement> &st) const
 {
-       if(!tag.empty())
-               st.push_back((DataFile::Statement("tag"), tag));
        for(vector<Row>::const_iterator i=rows.begin(); i!=rows.end(); ++i)
                st.push_back(i->save());
 }
@@ -415,7 +413,6 @@ Timetable::Loader::Loader(Timetable &tt):
        add("route",       &Loader::route);
        add("speed",       &Loader::speed);
        add("reverse",     &Loader::reverse);
-       add("tag",         &Loader::tag);
        add("travel_to",   &Loader::travel_to);
        add("travel_past", &Loader::travel_past);
        add("wait",        &Loader::wait);
@@ -463,11 +460,6 @@ void Timetable::Loader::speed(unsigned s)
        obj.rows.push_back(Row(SPEED, s));
 }
 
-void Timetable::Loader::tag(const string &t)
-{
-       obj.tag = t;
-}
-
 void Timetable::Loader::travel_to(unsigned s)
 {
        obj.rows.push_back(Row(TRAVEL_TO, s));
index 67565a239af34d802a69f20be906090da0fb7fec..92a4d65fa92b5b15f2674a2df22e67853d008366 100644 (file)
@@ -29,7 +29,6 @@ public:
                void route(const std::string &);
                void reverse();
                void speed(unsigned);
-               void tag(const std::string &);
                void travel_to(unsigned);
                void travel_past(unsigned);
                void wait(unsigned);
index eb1133a565e2d94ff4c1f2ea91eae290e8f2fae1..10227222daac98b38956c14975d0a334ad2cc3ab 100644 (file)
@@ -206,15 +206,6 @@ void Train::remove_ai(TrainAI &ai)
                ais.erase(i);
 }
 
-TrainAI *Train::get_tagged_ai(const string &tag) const
-{
-       for(list<TrainAI *>::const_iterator i=ais.begin(); i!=ais.end(); ++i)
-               if((*i)->get_tag()==tag)
-                       return *i;
-
-       return 0;
-}
-
 void Train::ai_message(const TrainAI::Message &msg)
 {
        for(list<TrainAI *>::iterator i=ais.begin(); i!=ais.end(); ++i)
index fa638f8d5034390b25388a1fa06b7e125538ef80..461a27e583146bde1080e64acbb6384b0c33a3e6 100644 (file)
@@ -109,9 +109,17 @@ public:
 
        void add_ai(TrainAI &);
        void remove_ai(TrainAI &);
-       TrainAI *get_tagged_ai(const std::string &) const;
        void ai_message(const TrainAI::Message &);
 
+       template<typename T>
+       T *get_ai_of_type() const
+       {
+               for(std::list<TrainAI *>::const_iterator i=ais.begin(); i!=ais.end(); ++i)
+                       if(T *ai = dynamic_cast<T *>(*i))
+                               return ai;
+               return 0;
+       }
+
        void place(Block &, unsigned);
        void unplace();
        bool is_placed() const { return !blocks.empty(); }
index ed3766ca32b96e474402fbc627ca9f0264999efd..df80240d83284156613da0817e51dc2e41b16cfc 100644 (file)
@@ -16,9 +16,4 @@ TrainAI::~TrainAI()
        train.remove_ai(*this);
 }
 
-void TrainAI::set_tag(const string &t)
-{
-       tag = t;
-}
-
 } // namespace R2C2
index c3e16556e773fc4802abd7aed6b941bec68fc711..b040493ce1ea5d1c903745ddd60cbbda528dc83a 100644 (file)
@@ -37,15 +37,11 @@ public:
 
 protected:
        Train &train;
-       std::string tag;
 
        TrainAI(Train &);
 public:
        virtual ~TrainAI();
 
-       void set_tag(const std::string &);
-       const std::string &get_tag() const { return tag; }
-
        virtual void message(const Message &) { }
        virtual void tick(const Msp::Time::TimeStamp &, const Msp::Time::TimeDelta &) { }
 };
index c39af92c246e9ed30ce78ded29d40832dc8e8925..6e588e7a7ac4302c97db28f968fa1c8d04a01b27 100644 (file)
@@ -163,9 +163,6 @@ void TrainRouter::tick(const Time::TimeStamp &, const Time::TimeDelta &)
 
 void TrainRouter::save(list<DataFile::Statement> &st) const
 {
-       if(!tag.empty())
-               st.push_back((DataFile::Statement("tag"), tag));
-
        st.push_back((DataFile::Statement("priority"), priority));
 
        if(!routes.empty())
@@ -231,8 +228,7 @@ void TrainRouter::block_reserved(Block &block, Train *t)
                        bool exit_conflict = (exit==static_cast<unsigned>(other_entry));
                        // TODO: speed matching with preceding train
 
-                       // XXX Should invent a better way to get our counterpart from the other train
-                       TrainRouter *other_router = dynamic_cast<TrainRouter *>(other_train->get_tagged_ai("router"));
+                       TrainRouter *other_router = other_train->get_ai_of_type<TrainRouter>();
                        int other_prio = (other_router ? other_router->get_priority() : 0);
 
                        if(!entry_conflict && !exit_conflict && other_prio<priority)
@@ -349,7 +345,6 @@ TrainRouter::Loader::Loader(TrainRouter &r):
 {
        add("priority", &TrainRouter::priority);
        add("route",    &Loader::route);
-       add("tag",      &Loader::tag);
 }
 
 void TrainRouter::Loader::route(const string &n)
@@ -357,9 +352,4 @@ void TrainRouter::Loader::route(const string &n)
        obj.set_route(&obj.train.get_layout().get_route(n));
 }
 
-void TrainRouter::Loader::tag(const string &t)
-{
-       obj.set_tag(t);
-}
-
 } // namespace R2C2
index 6aae439c3c8a48a21ad4324b98aae02fa852a417..28fc80246f522cb48d6f1db88db7e66c917b37b9 100644 (file)
@@ -21,7 +21,6 @@ public:
                Loader(TrainRouter &);
        private:
                void route(const std::string &);
-               void tag(const std::string &);
        };
 
        sigc::signal<void, const Route *> signal_route_changed;
index 73cee24a4f0c2f5d7676b98a507be2f25dcce6fd..bf404e9b5ea90eae6116d7ce5651f7e5b7acf429 100644 (file)
@@ -75,9 +75,8 @@ void Server::train_added(Train &train)
        pkt.name = train.get_name();
        send(pkt);
 
-       TrainStatus *status = new TrainStatus(train);
-       status->set_tag("server:status");
-       status->signal_changed.connect(sigc::bind<0>(sigc::mem_fun(this, &Server::train_status_changed), sigc::ref(train)));
+       if(!train.get_ai_of_type<TrainStatus>())
+               new TrainStatus(train);
 }
 
 void Server::train_control_changed(const Train &train, const string &control, float value)
@@ -107,14 +106,13 @@ void Server::train_ai_event(const Train &train, TrainAI &, const TrainAI::Messag
                        pkt.route = route->get_name();
                send(pkt);
        }
-}
-
-void Server::train_status_changed(const Train &train, const string &status)
-{
-       TrainStatusPacket pkt;
-       pkt.address = train.get_address();
-       pkt.status = status;
-       send(pkt);
+       else if(ev.type=="status-changed")
+       {
+               TrainStatusPacket pkt;
+               pkt.address = train.get_address();
+               pkt.status = ev.value.value<std::string>();
+               send(pkt);
+       }
 }
 
 template<typename P>
@@ -198,14 +196,14 @@ void Server::Connection::handshake_done()
                        pkt.functions = train.get_functions();
                        comm.send(pkt);
                }
-               if(TrainStatus *status = dynamic_cast<TrainStatus *>(train.get_tagged_ai("server:status")))
+               if(TrainStatus *status = train.get_ai_of_type<TrainStatus>())
                {
                        TrainStatusPacket pkt;
                        pkt.address = train.get_address();
                        pkt.status = status->get_status();
                        comm.send(pkt);
                }
-               if(TrainRouter *router = dynamic_cast<TrainRouter *>(train.get_tagged_ai("router")))
+               if(TrainRouter *router = train.get_ai_of_type<TrainRouter>())
                {
                        TrainRoutePacket pkt;
                        pkt.address = train.get_address();
index cfc8d7642c9e929e49a4624ff22547410f88fef7..83be2e044da57b1a05849a74ee0ff38b9fe9829f 100644 (file)
@@ -55,7 +55,6 @@ private:
        void train_control_changed(const Train &, const std::string &, float);
        void train_function_changed(const Train &, unsigned, bool);
        void train_ai_event(const Train &, TrainAI &, const TrainAI::Message &);
-       void train_status_changed(const Train &, const std::string &);
 
        template<typename P>
        void send(const P &);