]> git.tdb.fi Git - r2c2.git/blobdiff - source/libmarklin/train.cpp
Handle vehicle positioning while going backwards
[r2c2.git] / source / libmarklin / train.cpp
index 69e7212a8919937d327f027c7a314296b057b361..f05799265ef9e186c3f553ffc28f6a045ec3d6c9 100644 (file)
@@ -15,6 +15,7 @@ Distributed under the GPL
 #include "route.h"
 #include "tracktype.h"
 #include "train.h"
+#include "vehicle.h"
 
 using namespace std;
 using namespace Msp;
@@ -31,14 +32,16 @@ Train::Train(Layout &l, const LocoType &t, unsigned a):
        reverse(false),
        functions(0),
        route(0),
+       next_route(0),
        end_of_route(false),
        status("Unplaced"),
        travel_dist(0),
        travel_speed(0),
        pure_speed(false),
-       real_speed(15),
-       cur_track(0)
+       real_speed(15)
 {
+       vehicles.push_back(new Vehicle(layout, loco_type));
+
        layout.add_train(*this);
 
        layout.get_driver().add_loco(address);
@@ -52,6 +55,8 @@ Train::Train(Layout &l, const LocoType &t, unsigned a):
 
 Train::~Train()
 {
+       for(vector<Vehicle *>::iterator i=vehicles.begin(); i!=vehicles.end(); ++i)
+               delete *i;
        layout.remove_train(*this);
 }
 
@@ -62,6 +67,20 @@ void Train::set_name(const string &n)
        signal_name_changed.emit(name);
 }
 
+Vehicle &Train::get_vehicle(unsigned i)
+{
+       if(i>=vehicles.size())
+               throw InvalidParameterValue("Vehicle index out of range");
+       return *vehicles[i];
+}
+
+const Vehicle &Train::get_vehicle(unsigned i) const
+{
+       if(i>=vehicles.size())
+               throw InvalidParameterValue("Vehicle index out of range");
+       return *vehicles[i];
+}
+
 void Train::set_speed(unsigned speed)
 {
        if(speed==target_speed)
@@ -100,13 +119,6 @@ void Train::set_reverse(bool rev)
 
        release_blocks(rsv_blocks);
        reverse_blocks(cur_blocks);
-
-       if(cur_track)
-       {
-               unsigned path = cur_track->get_active_path();
-               cur_track_ep = cur_track->traverse(cur_track_ep, path);
-               offset = cur_track->get_type().get_path_length(path)-offset;
-       }
 }
 
 void Train::set_function(unsigned func, bool state)
@@ -137,8 +149,21 @@ void Train::set_route(const Route *r)
        }
 
        route = r;
+       next_route = 0;
        end_of_route = false;
 
+       if(route)
+       {
+               BlockRef &last = (rsv_blocks.empty() ? cur_blocks.back() : rsv_blocks.back());
+               BlockRef next = last.next();
+               const Block::Endpoint &ep = next.block->get_endpoints()[next.entry];
+               if(!route->get_tracks().count(ep.track))
+               {
+                       next_route = route;
+                       route = Route::find(*ep.track, ep.track_ep, *next_route);
+               }
+       }
+
        if(target_speed && reserve_more()<2)
                update_speed();
 
@@ -147,6 +172,14 @@ void Train::set_route(const Route *r)
 
 void Train::go_to(const Track &to)
 {
+       for(list<BlockRef>::const_iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
+               if(i->block->get_tracks().count(const_cast<Track *>(&to)))
+               {
+                       set_speed(0);
+                       set_route(0);
+                       return;
+               }
+
        BlockRef *last = 0;
        if(rsv_blocks.empty())
                last = &cur_blocks.back();
@@ -157,15 +190,8 @@ void Train::go_to(const Track &to)
                                last = &*i;
        }
 
-       Block *next = last->block->get_endpoints()[last->block->traverse(last->entry)].link;
-       if(!next)
-               throw InvalidState("At end of line");
-
-       int entry = next->get_endpoint_by_link(*last->block);
-       if(entry<0)
-               throw LogicError("Block links are inconsistent");
-
-       const Block::Endpoint &ep = next->get_endpoints()[entry];
+       BlockRef next = last->next();
+       const Block::Endpoint &ep = next.block->get_endpoints()[next.entry];
 
        set_route(Route::find(*ep.track, ep.track_ep, to));
 }
@@ -185,7 +211,19 @@ void Train::place(Block &block, unsigned entry)
        }
 
        cur_blocks.push_back(BlockRef(&block, entry));
-       set_position(block.get_endpoints()[entry]);
+       if(reverse)
+       {
+               unsigned exit = block.traverse(entry);
+               const Block::Endpoint &bep = block.get_endpoints()[exit];
+               Track *track = bep.track->get_link(bep.track_ep);
+               unsigned ep = track->get_endpoint_by_link(*bep.track);
+               vehicles.front()->place(track, ep, 0, Vehicle::FRONT_BUFFER);
+       }
+       else
+       {
+               const Block::Endpoint &bep = block.get_endpoints()[entry];
+               vehicles.front()->place(bep.track, bep.track_ep, 0, Vehicle::BACK_BUFFER);
+       }
 
        set_status("Stopped");
 }
@@ -230,34 +268,19 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt)
                stop_timeout = Time::TimeStamp();
        }
 
-       if(cur_track)
+       if(current_speed)
        {
-               unsigned path = cur_track->get_active_path();
-
-               offset += get_real_speed(current_speed)*(dt/Time::sec);
-               float path_len = cur_track->get_type().get_path_length(path);
-               if(offset>path_len)
-               {
-                       unsigned out = cur_track->traverse(cur_track_ep, path);
-                       Track *next = cur_track->get_link(out);
+               Track *track = vehicles[0]->get_track();
 
-                       bool ok = false;
-                       for(list<BlockRef>::const_iterator i=cur_blocks.begin(); (!ok && i!=cur_blocks.end()); ++i)
-                               ok = i->block->get_tracks().count(next);
+               bool ok = false;
+               for(list<BlockRef>::const_iterator i=cur_blocks.begin(); (!ok && i!=cur_blocks.end()); ++i)
+                       ok = i->block->get_tracks().count(track);
 
-                       if(ok)
-                       {
-                               if(next)
-                                       cur_track_ep = next->get_endpoint_by_link(*cur_track);
-                               cur_track = next;
-                               offset = 0;
-                       }
-                       else
-                               offset = path_len-0.001;
+               if(ok)
+               {
+                       float d = get_real_speed(current_speed)*(dt/Time::sec);
+                       vehicles[0]->advance(reverse ? -d : d);
                }
-
-               if(cur_track)
-                       pos = cur_track->get_point(cur_track_ep, path, offset).pos;
        }
 }
 
@@ -267,8 +290,6 @@ void Train::save(list<DataFile::Statement> &st) const
        for(unsigned i=0; i<=14; ++i)
                if(real_speed[i].weight)
                        st.push_back((DataFile::Statement("real_speed"), i, real_speed[i].speed, real_speed[i].weight));
-       if(route)
-               st.push_back((DataFile::Statement("route"), route->get_name()));
 
        if(!cur_blocks.empty())
        {
@@ -282,6 +303,9 @@ void Train::save(list<DataFile::Statement> &st) const
                for(list<BlockRef>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
                        st.push_back((DataFile::Statement("block"), i->block->get_id()));
        }
+
+       if(route)
+               st.push_back((DataFile::Statement("route"), route->get_name()));
 }
 
 void Train::loco_speed_event(unsigned addr, unsigned speed, bool rev)
@@ -341,11 +365,36 @@ void Train::sensor_event(unsigned addr, bool state)
                                travel_dist += block_len;
 
                                if(j->block->get_sensor_id()==addr)
-                                       set_position(j->block->get_endpoints()[j->entry]);
+                               {
+                                       const Block::Endpoint &bep = j->block->get_endpoints()[j->entry];
+                                       if(reverse)
+                                       {
+                                               Track *track = bep.track->get_link(bep.track_ep);
+                                               unsigned ep = track->get_endpoint_by_link(*bep.track);
+                                               vehicles.back()->place(track, ep, 0, Vehicle::BACK_AXLE);
+                                       }
+                                       else
+                                               vehicles.front()->place(bep.track, bep.track_ep, 0, Vehicle::FRONT_AXLE);
+                               }
                        }
                        last_entry_time = Time::now();
                        pure_speed = true;
 
+                       // Check if we've reached the next route
+                       if(next_route)
+                       {
+                               const set<const Track *> &rtracks = next_route->get_tracks();
+                               for(list<BlockRef>::iterator j=rsv_blocks.begin(); j!=i; ++j)
+                                       if(rtracks.count(j->block->get_endpoints()[j->entry].track))
+                                       {
+                                               route = next_route;
+                                               next_route = 0;
+                                               // XXX Exceptions?
+                                               signal_route_changed.emit(route);
+                                               break;
+                                       }
+                       }
+
                        // Move blocks up to the next sensor to our current blocks
                        cur_blocks.splice(cur_blocks.end(), rsv_blocks, rsv_blocks.begin(), i);
 
@@ -419,8 +468,18 @@ unsigned Train::reserve_more()
        for(list<BlockRef>::const_iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
                if(i->block->get_sensor_id())
                        ++nsens;
+       
+       const Route *cur_route = 0;
+       if(route)
+       {
+               unsigned exit = last->block->traverse(last->entry);
+               Track *track = last->block->get_endpoints()[exit].track;
+               if(route->get_tracks().count(track))
+                       cur_route = route;
+               else if(next_route && next_route->get_tracks().count(track))
+                       cur_route = next_route;
+       }
 
-       bool on_route = (route && route->get_tracks().count(last->block->get_endpoints()[last->entry].track));
        bool got_more = false;
        BlockRef *good = last;
        unsigned good_sens = nsens;
@@ -436,14 +495,23 @@ unsigned Train::reserve_more()
                if(entry<0)
                        throw LogicError("Block links are inconsistent!");
 
-               if(on_route && !route->get_tracks().count(link->get_endpoints()[entry].track))
+               const Block::Endpoint &entry_ep = link->get_endpoints()[entry];
+
+               if(cur_route)
                {
-                       // Keep the blocks if we arrived at the end of the route
-                       good = last;
-                       good_sens = nsens;
-                       end_of_route = true;
-                       break;
+                       if(cur_route!=next_route && next_route && next_route->get_tracks().count(entry_ep.track))
+                               cur_route = next_route;
+                       else if(!cur_route->get_tracks().count(entry_ep.track))
+                       {
+                               // Keep the blocks if we arrived at the end of the route
+                               good = last;
+                               good_sens = nsens;
+                               end_of_route = true;
+                               break;
+                       }
                }
+               else if(route && route->get_tracks().count(entry_ep.track))
+                       cur_route = route;
 
                if(!link->reserve(this))
                {
@@ -462,8 +530,7 @@ unsigned Train::reserve_more()
 
                if(link->get_turnout_id())
                {
-                       const Block::Endpoint &ep = link->get_endpoints()[entry];
-                       const Endpoint &track_ep = ep.track->get_type().get_endpoints()[ep.track_ep];
+                       const Endpoint &track_ep = entry_ep.track->get_type().get_endpoints()[entry_ep.track_ep];
 
                        // Keep the blocks reserved so far, as either us or the other train can diverge
                        good = last;
@@ -471,10 +538,10 @@ unsigned Train::reserve_more()
 
                        // Figure out what path we'd like to take on the turnout
                        int path = -1;
-                       if(route)
-                               path = route->get_turnout(link->get_turnout_id());
+                       if(cur_route)
+                               path = cur_route->get_turnout(link->get_turnout_id());
                        if(path<0)
-                               path = ep.track->get_active_path();
+                               path = entry_ep.track->get_active_path();
                        if(!((track_ep.paths>>path)&1))
                        {
                                for(unsigned i=0; track_ep.paths>>i; ++i)
@@ -482,12 +549,12 @@ unsigned Train::reserve_more()
                                                path = i;
                        }
 
-                       if(path!=static_cast<int>(ep.track->get_active_path()))
+                       if(path!=static_cast<int>(entry_ep.track->get_active_path()))
                        {
                                // The turnout is set to wrong path - switch and wait for it
                                link->reserve(0);
                                pending_block = link;
-                               ep.track->set_active_path(path);
+                               entry_ep.track->set_active_path(path);
                                break;
                        }
                }
@@ -499,9 +566,6 @@ unsigned Train::reserve_more()
                        ++nsens;
                        got_more = true;
                }
-
-               if(route && !on_route)
-                       on_route = route->get_tracks().count(link->get_endpoints()[entry].track);
        }
 
        // Unreserve blocks that were not good
@@ -621,14 +685,6 @@ void Train::set_status(const string &s)
        signal_status_changed.emit(s);
 }
 
-void Train::set_position(const Block::Endpoint &bep)
-{
-       cur_track = bep.track;
-       cur_track_ep = bep.track_ep;
-       offset = 0;
-       pos = cur_track->get_endpoint_position(cur_track_ep);
-}
-
 void Train::release_blocks(list<BlockRef> &blocks)
 {
        release_blocks(blocks, blocks.begin(), blocks.end());
@@ -652,6 +708,25 @@ void Train::reverse_blocks(list<BlockRef> &blocks) const
 }
 
 
+Train::BlockRef::BlockRef(Block *b, unsigned e):
+       block(b),
+       entry(e)
+{ }
+
+Train::BlockRef Train::BlockRef::next() const
+{
+       Block *blk = block->get_endpoints()[block->traverse(entry)].link;
+       if(!blk)
+               throw InvalidState("At end of line");
+
+       int ep = blk->get_endpoint_by_link(*block);
+       if(ep<0)
+               throw LogicError("Block links are inconsistent");
+
+       return BlockRef(blk, ep);
+}
+
+
 Train::RealSpeed::RealSpeed():
        speed(0),
        weight(0)
@@ -687,7 +762,11 @@ void Train::Loader::block(unsigned id)
        blk.reserve(&obj);
        obj.cur_blocks.push_back(BlockRef(&blk, entry));
        obj.set_status("Stopped");
-       obj.set_position(blk.get_endpoints()[entry]);
+       const Block::Endpoint &bep = blk.get_endpoints()[entry];
+       obj.vehicles.back()->place(bep.track, bep.track_ep, 0, Vehicle::BACK_BUFFER);
+
+       if(blk.get_sensor_id())
+               obj.layout.get_driver().set_sensor(blk.get_sensor_id(), true);
 
        prev_block = &blk;
 }