From d3907f4b0e60f246a53201b93b06e86062f1b48a Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 23 Apr 2010 19:28:39 +0000 Subject: [PATCH] Implement train priority system --- source/engineer/trainproperties.cpp | 12 ++++++++- source/engineer/trainproperties.h | 1 + source/libmarklin/train.cpp | 38 +++++++++++++++++++++-------- source/libmarklin/train.h | 3 +++ 4 files changed, 43 insertions(+), 11 deletions(-) diff --git a/source/engineer/trainproperties.cpp b/source/engineer/trainproperties.cpp index 96c8313..748b0c9 100644 --- a/source/engineer/trainproperties.cpp +++ b/source/engineer/trainproperties.cpp @@ -23,7 +23,7 @@ TrainProperties::TrainProperties(Engineer &e, const GLtk::Resources &r, Train *t engineer(e), train(t) { - set_size(200, 120); + set_size(200, 145); GLtk::Label *label; add(*(label=new GLtk::Label(res, "Train properties"))); @@ -50,6 +50,15 @@ TrainProperties::TrainProperties(Engineer &e, const GLtk::Resources &r, Train *t add(*(ent_name=new GLtk::Entry(res))); ent_name->set_geometry(GLtk::Geometry(10, geom.h-75, geom.w-20, 20)); + add(*(drp_priority=new GLtk::Dropdown(res))); + drp_priority->set_geometry(GLtk::Geometry(10, geom.h-100, geom.w-20, 20)); + drp_priority->append("Standard freight"); + drp_priority->append("Express freight"); + drp_priority->append("Unspecified"); + drp_priority->append("Standard passenger"); + drp_priority->append("Express passenger"); + drp_priority->set_selected_index(train->get_priority()+2); + if(train) { ent_addr->set_text(lexical_cast(train->get_address())); @@ -80,4 +89,5 @@ void TrainProperties::on_ok_clicked() } train->set_name(ent_name->get_text()); + train->set_priority(drp_priority->get_selected_index()-2); } diff --git a/source/engineer/trainproperties.h b/source/engineer/trainproperties.h index 8048ee2..3c1a372 100644 --- a/source/engineer/trainproperties.h +++ b/source/engineer/trainproperties.h @@ -23,6 +23,7 @@ private: Msp::GLtk::Entry *ent_addr; Msp::GLtk::Dropdown *drp_type; Msp::GLtk::Entry *ent_name; + Msp::GLtk::Dropdown *drp_priority; public: TrainProperties(Engineer &, const Msp::GLtk::Resources &, Marklin::Train *); diff --git a/source/libmarklin/train.cpp b/source/libmarklin/train.cpp index 1abf535..634f45c 100644 --- a/source/libmarklin/train.cpp +++ b/source/libmarklin/train.cpp @@ -30,6 +30,7 @@ Train::Train(Layout &l, const LocoType &t, unsigned a): layout(l), loco_type(t), address(a), + priority(0), pending_block(0), control(new AIControl(*this, new SimplePhysics)), timetable(0), @@ -81,6 +82,11 @@ void Train::set_name(const string &n) signal_name_changed.emit(name); } +void Train::set_priority(int p) +{ + priority = p; +} + Vehicle &Train::get_vehicle(unsigned i) { if(i>=vehicles.size()) @@ -424,6 +430,8 @@ void Train::save(list &st) const { st.push_back((DataFile::Statement("name"), name)); + st.push_back((DataFile::Statement("priority"), priority)); + for(vector::const_iterator i=vehicles.begin(); i!=vehicles.end(); ++i) if(i!=vehicles.begin()) st.push_back((DataFile::Statement("vehicle"), (*i)->get_type().get_article_number())); @@ -694,19 +702,28 @@ unsigned Train::reserve_more() else if(route && route->get_tracks().count(entry_ep.track)) cur_route = route; - if(!link->reserve(this)) + bool reserved = link->reserve(this); + if(!reserved) { - // If we found another train and it's not headed straight for us, we can keep the blocks we got - int other_entry = link->get_train()->get_entry_to_block(*link); - if(other_entry<0) - throw LogicError("Block reservation inconsistency"); - if(static_cast(entry)!=link->traverse(other_entry)) + // Ask a lesser priority train to free the block for us + if(link->get_train()->get_priority()get_train()->free_block(*link)) + reserved = link->reserve(this); + + if(!reserved) { - good = last; - good_sens = nsens; + // If we found another train and it's not headed straight for us, we can keep the blocks we got + int other_entry = link->get_train()->get_entry_to_block(*link); + if(other_entry<0) + throw LogicError("Block reservation inconsistency"); + if(static_cast(entry)!=link->traverse(other_entry)) + { + good = last; + good_sens = nsens; + } + pending_block = link; + break; } - pending_block = link; - break; } if(link->get_turnout_id()) @@ -897,6 +914,7 @@ Train::Loader::Loader(Train &t): add("block", &Loader::block); add("block_hint", &Loader::block_hint); add("name", &Loader::name); + add("priority", &Train::priority); add("real_speed", &Loader::real_speed); add("route", &Loader::route); add("timetable", &Loader::timetable); diff --git a/source/libmarklin/train.h b/source/libmarklin/train.h index 18ad6a1..8087745 100644 --- a/source/libmarklin/train.h +++ b/source/libmarklin/train.h @@ -71,6 +71,7 @@ private: const LocoType &loco_type; unsigned address; std::string name; + int priority; std::vector vehicles; std::list cur_blocks; std::list rsv_blocks; @@ -104,6 +105,8 @@ public: unsigned get_address() const { return address; } void set_name(const std::string &); const std::string &get_name() const { return name; } + void set_priority(int); + int get_priority() const { return priority; } ControlModel &get_control() const { return *control; } Vehicle &get_vehicle(unsigned); -- 2.45.2