X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibmarklin%2Ftrain.cpp;h=634f45c11a692a6ecbbd94f06b77051b6e5de7d2;hb=d3907f4b0e60f246a53201b93b06e86062f1b48a;hp=44f9298d142daf72682594148476699a0ce89554;hpb=3b3099eb9e6d14e2a0495ea78144a734a102112f;p=r2c2.git diff --git a/source/libmarklin/train.cpp b/source/libmarklin/train.cpp index 44f9298..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()) @@ -322,10 +328,10 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt) float speed = control->get_speed(); unsigned speed_notch = find_speed(abs(speed)); - if(speed && (speed<0)!=reverse) + if(control->get_reverse()!=reverse) { - driver.set_loco_reverse(address, speed<0); - reverse = speed<0; + reverse = control->get_reverse(); + driver.set_loco_reverse(address, reverse); release_blocks(rsv_blocks); reverse_blocks(cur_blocks); @@ -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);