]> git.tdb.fi Git - r2c2.git/commitdiff
Handle vehicle positioning while going backwards
authorMikko Rasa <tdb@tdb.fi>
Mon, 12 Apr 2010 09:56:02 +0000 (09:56 +0000)
committerMikko Rasa <tdb@tdb.fi>
Mon, 12 Apr 2010 09:56:02 +0000 (09:56 +0000)
More sensible initial vehicle placement
Activate occupied sensors while loading trains

source/libmarklin/train.cpp
source/libmarklin/train.h
source/libmarklin/vehicle.cpp
source/libmarklin/vehicle.h

index a87717480bf7806d77b0e014bb2baf2b54e94d0b..f05799265ef9e186c3f553ffc28f6a045ec3d6c9 100644 (file)
@@ -119,8 +119,6 @@ void Train::set_reverse(bool rev)
 
        release_blocks(rsv_blocks);
        reverse_blocks(cur_blocks);
-
-       // XXX Do something about the vehicles
 }
 
 void Train::set_function(unsigned func, bool state)
@@ -213,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");
 }
@@ -267,7 +277,10 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt)
                        ok = i->block->get_tracks().count(track);
 
                if(ok)
-                       vehicles[0]->advance(get_real_speed(current_speed)*(dt/Time::sec));
+               {
+                       float d = get_real_speed(current_speed)*(dt/Time::sec);
+                       vehicles[0]->advance(reverse ? -d : d);
+               }
        }
 }
 
@@ -352,7 +365,17 @@ 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;
@@ -662,11 +685,6 @@ void Train::set_status(const string &s)
        signal_status_changed.emit(s);
 }
 
-void Train::set_position(const Block::Endpoint &bep)
-{
-       vehicles[0]->place(bep.track, bep.track_ep, 0, Vehicle::FRONT_AXLE);
-}
-
 void Train::release_blocks(list<BlockRef> &blocks)
 {
        release_blocks(blocks, blocks.begin(), blocks.end());
@@ -744,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;
 }
index f9dba5dfbabb68339fd71ddc3fd4a0fd353ff816..5a20bd01a4855fce7a330606838817b5caba6ba8 100644 (file)
@@ -133,7 +133,6 @@ private:
        float get_real_speed(unsigned) const;
        unsigned find_speed(float) const;
        void set_status(const std::string &);
-       void set_position(const Block::Endpoint &);
        void release_blocks(std::list<BlockRef> &);
        void release_blocks(std::list<BlockRef> &, std::list<BlockRef>::iterator, std::list<BlockRef>::iterator);
        void reverse_blocks(std::list<BlockRef> &) const;
index 7c299c78bd4311f6bb3b41506529a75fb99f4cce..6011537d02b3205a4996a89a80edad272cfe2a2e 100644 (file)
@@ -35,6 +35,7 @@ Vehicle::~Vehicle()
 void Vehicle::place(Track *t, unsigned e, float o, PlaceMode m)
 {
        track_pos = TrackPosition(t, e, o);
+
        if(m==FRONT_AXLE)
        {
                float front = type.get_length()/2;
@@ -47,6 +48,23 @@ void Vehicle::place(Track *t, unsigned e, float o, PlaceMode m)
                }
                track_pos.advance(-front);
        }
+       else if(m==FRONT_BUFFER)
+               track_pos.advance(-type.get_length()/2);
+       else if(m==BACK_AXLE)
+       {
+               float back = type.get_length()/2;
+               if(!type.get_axles().empty())
+                       back = type.get_axles().back().position;
+               if(!type.get_bogies().empty())
+               {
+                       const VehicleType::Bogie &bogie = type.get_bogies().back();
+                       back = min(back, bogie.position+bogie.axles.back().position);
+               }
+               track_pos.advance(-back);
+       }
+       else if(m==BACK_BUFFER)
+               track_pos.advance(type.get_length()/2);
+
        update_position();
 }
 
index dcf773736bac3d08b13131a02901279fda00db0b..d24bebea60f491568302fead5aff1c3545b58142 100644 (file)
@@ -22,7 +22,10 @@ public:
        enum PlaceMode
        {
                CENTER,
-               FRONT_AXLE
+               FRONT_AXLE,
+               FRONT_BUFFER,
+               BACK_AXLE,
+               BACK_BUFFER
        };
 
 private: