X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibmarklin%2Ftrain.cpp;h=f05799265ef9e186c3f553ffc28f6a045ec3d6c9;hb=e94014530a6e28a42bc5678e579ee07e0ee5329b;hp=f745313233d57ce7e2b513ee2628c815c69dade2;hpb=306cf0a8709a781b3031aa05a7b353a9b3a308e5;p=r2c2.git diff --git a/source/libmarklin/train.cpp b/source/libmarklin/train.cpp index f745313..f057992 100644 --- a/source/libmarklin/train.cpp +++ b/source/libmarklin/train.cpp @@ -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; @@ -37,9 +38,10 @@ Train::Train(Layout &l, const LocoType &t, unsigned a): 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); @@ -53,6 +55,8 @@ Train::Train(Layout &l, const LocoType &t, unsigned a): Train::~Train() { + for(vector::iterator i=vehicles.begin(); i!=vehicles.end(); ++i) + delete *i; layout.remove_train(*this); } @@ -63,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) @@ -101,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) @@ -200,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"); } @@ -245,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(); + Track *track = vehicles[0]->get_track(); - 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); - - bool ok = false; - for(list::const_iterator i=cur_blocks.begin(); (!ok && i!=cur_blocks.end()); ++i) - ok = i->block->get_tracks().count(next); + bool ok = false; + for(list::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; } } @@ -357,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; @@ -667,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 &blocks) { release_blocks(blocks, blocks.begin(), blocks.end()); @@ -752,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; }