X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibmarklin%2Fblock.cpp;h=9a9164ea292770380548aea3a2c517ac4fbe327d;hb=6dc18b0e518407bd2a86602bae1e9bbae05da7c8;hp=3ca744ca0c287796396263c825130977be3e4e7f;hpb=25b79ab38c2be2e5c89b8240504f641fd9c4899c;p=r2c2.git diff --git a/source/libmarklin/block.cpp b/source/libmarklin/block.cpp index 3ca744c..9a9164e 100644 --- a/source/libmarklin/block.cpp +++ b/source/libmarklin/block.cpp @@ -1,20 +1,29 @@ +/* $Id$ + +This file is part of the MSP Märklin suite +Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa +Distributed under the GPL +*/ + +#include #include "control.h" #include "block.h" #include "tracktype.h" #include "trafficmanager.h" #include "turnout.h" -using namespace Msp; - -#include using namespace std; +using namespace Msp; namespace Marklin { +unsigned Block::next_id = 1; + Block::Block(TrafficManager &tm, Track &start): trfc_mgr(tm), id(next_id++), sensor_id(start.get_sensor_id()), + turnout_id(start.get_turnout_id()), train(0) { tracks.insert(&start); @@ -24,14 +33,14 @@ Block::Block(TrafficManager &tm, Track &start): while(!queue.empty()) { - Track *track=queue.front(); + Track *track = queue.front(); queue.erase(queue.begin()); - const vector &links=track->get_links(); + const vector &links = track->get_links(); for(unsigned i=0; iget_sensor_id()==sensor_id) + if(links[i]->get_sensor_id()==sensor_id && links[i]->get_turnout_id()==turnout_id) { queue.push_back(links[i]); tracks.insert(links[i]); @@ -43,10 +52,10 @@ Block::Block(TrafficManager &tm, Track &start): for(unsigned i=0; i visited; - find_routes(*endpoints[i].track, endpoints[i].track_ep, route, visited); + find_paths(*endpoints[i].track, endpoints[i].track_ep, path, visited); } } @@ -64,27 +73,27 @@ int Block::traverse(unsigned epi, float *len) const if(epi>=endpoints.size()) throw InvalidParameterValue("Endpoint index out of range"); - const Endpoint &ep=endpoints[epi]; - Track *track=ep.track; - unsigned track_ep=ep.track_ep; + const Endpoint &ep = endpoints[epi]; + Track *track = ep.track; + unsigned track_ep = ep.track_ep; if(len) - *len=0; + *len = 0; while(1) { - unsigned cur_route=0; - unsigned tid=track->get_turnout_id(); + unsigned cur_path = 0; + unsigned tid = track->get_turnout_id(); if(tid) { - Turnout &turnout=trfc_mgr.get_control().get_turnout(tid); - cur_route=turnout.get_route(); + Turnout &turnout = trfc_mgr.get_control().get_turnout(tid); + cur_path = turnout.get_path(); } if(len) - *len+=track->get_type().get_route_length(cur_route); + *len += track->get_type().get_path_length(cur_path); - int other_ep=track->traverse(track_ep, cur_route); + int other_ep = track->traverse(track_ep, cur_path); if(other_ep<0) return -1; @@ -92,12 +101,11 @@ int Block::traverse(unsigned epi, float *len) const if(endpoints[i].track==track && endpoints[i].track_ep==static_cast(other_ep)) return i; - Track *next=track->get_link(other_ep); - track_ep=next->get_endpoint_by_link(*track); - track=next; - - if(tracks.count(track)==0) + Track *next = track->get_link(other_ep); + if(tracks.count(next)==0) return -1; + track_ep = next->get_endpoint_by_link(*track); + track = next; } } @@ -111,8 +119,8 @@ void Block::check_link(Block &other) for(vector::iterator j=other.endpoints.begin(); j!=other.endpoints.end(); ++j) if(j->track==i->track->get_link(i->track_ep) && j->track->get_link(j->track_ep)==i->track && !j->link) { - i->link=&other; - j->link=this; + i->link = &other; + j->link = this; } } } @@ -128,11 +136,7 @@ bool Block::reserve(const Train *t) { if(!t || !train) { - train=t; - if(train) - cout<<"Block "<get_name()<<'\n'; - else - cout<<"Block "<link->id; else cout<<"none"; - cout<<", routes="<routes<<'\n'; + cout<<", paths="<paths<<'\n'; } } -void Block::find_routes(Track &track, unsigned track_ep, unsigned route, set &visited) +void Block::find_paths(Track &track, unsigned track_ep, unsigned path, set &visited) { visited.insert(&track); - const vector &eps=track.get_type().get_endpoints(); + const vector &eps = track.get_type().get_endpoints(); for(unsigned i=0; iget_endpoint_by_link(track), route, visited); + find_paths(*link, link->get_endpoint_by_link(track), path, visited); else { for(vector::iterator j=endpoints.begin(); j!=endpoints.end(); ++j) if(j->track==&track && j->track_ep==i) - j->routes|=route; + j->paths |= path; } } } -unsigned Block::next_id=1; - Block::Endpoint::Endpoint(Track *t, unsigned e): track(t), track_ep(e), link(0), - routes(0) + paths(0) { } } // namespace Marklin