]> git.tdb.fi Git - r2c2.git/commitdiff
Implement train priority system
authorMikko Rasa <tdb@tdb.fi>
Fri, 23 Apr 2010 19:28:39 +0000 (19:28 +0000)
committerMikko Rasa <tdb@tdb.fi>
Fri, 23 Apr 2010 19:28:39 +0000 (19:28 +0000)
source/engineer/trainproperties.cpp
source/engineer/trainproperties.h
source/libmarklin/train.cpp
source/libmarklin/train.h

index 96c8313d4800d2809cf5da62021d5f1d9a4ab5a8..748b0c9560ac5ec0a963d9ebd83893a9e0409855 100644 (file)
@@ -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);
 }
index 8048ee20aef71ecc964d6e357a381c57b0c19d26..3c1a372fe606fed9801ce1ea069fbebe38903f10 100644 (file)
@@ -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 *);
index 1abf535c06b6d03a0649c95162b7264da1802c73..634f45c11a692a6ecbbd94f06b77051b6e5de7d2 100644 (file)
@@ -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<DataFile::Statement> &st) const
 {
        st.push_back((DataFile::Statement("name"), name));
 
+       st.push_back((DataFile::Statement("priority"), priority));
+
        for(vector<Vehicle *>::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<unsigned>(entry)!=link->traverse(other_entry))
+                       // Ask a lesser priority train to free the block for us
+                       if(link->get_train()->get_priority()<priority)
+                               if(link->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<unsigned>(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);
index 18ad6a1e1407d807330aedd359e686eb30d2e70a..8087745f3dd29f03b9d3b3bfb4eec75859c246e8 100644 (file)
@@ -71,6 +71,7 @@ private:
        const LocoType &loco_type;
        unsigned address;
        std::string name;
+       int priority;
        std::vector<Vehicle *> vehicles;
        std::list<BlockRef> cur_blocks;
        std::list<BlockRef> 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);