From dec294d40194a640e7b4bccf20dd1baa4a87038c Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 24 Feb 2013 13:53:46 +0200 Subject: [PATCH] Get rid of the TrainAI tagging system 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. --- source/engineer/routeselect.cpp | 2 +- source/engineer/trainpanel.cpp | 12 +++--------- source/engineer/trainproperties.cpp | 4 ++-- source/libr2c2/timetable.cpp | 8 -------- source/libr2c2/timetable.h | 1 - source/libr2c2/train.cpp | 9 --------- source/libr2c2/train.h | 10 +++++++++- source/libr2c2/trainai.cpp | 5 ----- source/libr2c2/trainai.h | 4 ---- source/libr2c2/trainrouter.cpp | 12 +----------- source/libr2c2/trainrouter.h | 1 - source/network/server.cpp | 24 +++++++++++------------- source/network/server.h | 1 - 13 files changed, 27 insertions(+), 66 deletions(-) diff --git a/source/engineer/routeselect.cpp b/source/engineer/routeselect.cpp index e5c090c..770a455 100644 --- a/source/engineer/routeselect.cpp +++ b/source/engineer/routeselect.cpp @@ -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(train.get_tagged_ai("router"))) + if(TrainRouter *router = train.get_ai_of_type()) current_route = router->get_route(); const set &routes = engineer.get_layout().get_routes(); diff --git a/source/engineer/trainpanel.cpp b/source/engineer/trainpanel.cpp index 97592be..d6066cb 100644 --- a/source/engineer/trainpanel.cpp +++ b/source/engineer/trainpanel.cpp @@ -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(train.get_tagged_ai("router")); + TrainRouter *router = train.get_ai_of_type(); 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(train.get_tagged_ai("timetable")); + Timetable *timetable = train.get_ai_of_type(); 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(train.get_tagged_ai("router")); + TrainRouter *router = train.get_ai_of_type(); if(!router || !router->go_to(*track)) engineer.set_status("Could not set route"); } diff --git a/source/engineer/trainproperties.cpp b/source/engineer/trainproperties.cpp index ecc9b2b..d92750e 100644 --- a/source/engineer/trainproperties.cpp +++ b/source/engineer/trainproperties.cpp @@ -90,7 +90,7 @@ TrainProperties::TrainProperties(Engineer &e, Train *t): { ent_addr->set_text(lexical_cast(train->get_address())); ent_name->set_text(train->get_name()); - if(TrainRouter *router = dynamic_cast(train->get_tagged_ai("router"))) + if(TrainRouter *router = train->get_ai_of_type()) 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(train->get_tagged_ai("router"))) + if(TrainRouter *router = train->get_ai_of_type()) router->set_priority(drp_priority->get_selected_index()-2); // The locomotive is vehicle 0 so we need to add 1 diff --git a/source/libr2c2/timetable.cpp b/source/libr2c2/timetable.cpp index d5a0951..753aae8 100644 --- a/source/libr2c2/timetable.cpp +++ b/source/libr2c2/timetable.cpp @@ -162,8 +162,6 @@ void Timetable::tick(const Time::TimeStamp &t, const Time::TimeDelta &) void Timetable::save(list &st) const { - if(!tag.empty()) - st.push_back((DataFile::Statement("tag"), tag)); for(vector::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)); diff --git a/source/libr2c2/timetable.h b/source/libr2c2/timetable.h index 67565a2..92a4d65 100644 --- a/source/libr2c2/timetable.h +++ b/source/libr2c2/timetable.h @@ -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); diff --git a/source/libr2c2/train.cpp b/source/libr2c2/train.cpp index eb1133a..1022722 100644 --- a/source/libr2c2/train.cpp +++ b/source/libr2c2/train.cpp @@ -206,15 +206,6 @@ void Train::remove_ai(TrainAI &ai) ais.erase(i); } -TrainAI *Train::get_tagged_ai(const string &tag) const -{ - for(list::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::iterator i=ais.begin(); i!=ais.end(); ++i) diff --git a/source/libr2c2/train.h b/source/libr2c2/train.h index fa638f8..461a27e 100644 --- a/source/libr2c2/train.h +++ b/source/libr2c2/train.h @@ -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 + T *get_ai_of_type() const + { + for(std::list::const_iterator i=ais.begin(); i!=ais.end(); ++i) + if(T *ai = dynamic_cast(*i)) + return ai; + return 0; + } + void place(Block &, unsigned); void unplace(); bool is_placed() const { return !blocks.empty(); } diff --git a/source/libr2c2/trainai.cpp b/source/libr2c2/trainai.cpp index ed3766c..df80240 100644 --- a/source/libr2c2/trainai.cpp +++ b/source/libr2c2/trainai.cpp @@ -16,9 +16,4 @@ TrainAI::~TrainAI() train.remove_ai(*this); } -void TrainAI::set_tag(const string &t) -{ - tag = t; -} - } // namespace R2C2 diff --git a/source/libr2c2/trainai.h b/source/libr2c2/trainai.h index c3e1655..b040493 100644 --- a/source/libr2c2/trainai.h +++ b/source/libr2c2/trainai.h @@ -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 &) { } }; diff --git a/source/libr2c2/trainrouter.cpp b/source/libr2c2/trainrouter.cpp index c39af92..6e588e7 100644 --- a/source/libr2c2/trainrouter.cpp +++ b/source/libr2c2/trainrouter.cpp @@ -163,9 +163,6 @@ void TrainRouter::tick(const Time::TimeStamp &, const Time::TimeDelta &) void TrainRouter::save(list &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(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(other_train->get_tagged_ai("router")); + TrainRouter *other_router = other_train->get_ai_of_type(); int other_prio = (other_router ? other_router->get_priority() : 0); if(!entry_conflict && !exit_conflict && other_prio signal_route_changed; diff --git a/source/network/server.cpp b/source/network/server.cpp index 73cee24..bf404e9 100644 --- a/source/network/server.cpp +++ b/source/network/server.cpp @@ -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()) + 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(); + send(pkt); + } } template @@ -198,14 +196,14 @@ void Server::Connection::handshake_done() pkt.functions = train.get_functions(); comm.send(pkt); } - if(TrainStatus *status = dynamic_cast(train.get_tagged_ai("server:status"))) + if(TrainStatus *status = train.get_ai_of_type()) { TrainStatusPacket pkt; pkt.address = train.get_address(); pkt.status = status->get_status(); comm.send(pkt); } - if(TrainRouter *router = dynamic_cast(train.get_tagged_ai("router"))) + if(TrainRouter *router = train.get_ai_of_type()) { TrainRoutePacket pkt; pkt.address = train.get_address(); diff --git a/source/network/server.h b/source/network/server.h index cfc8d76..83be2e0 100644 --- a/source/network/server.h +++ b/source/network/server.h @@ -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 void send(const P &); -- 2.45.2