X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Flayout.cpp;h=df8ce74e62b01ba1d52a178469903992afb78a83;hb=d15ac13f2e170f155b4bbd124df48400c339b644;hp=d6bd9ee909dee6f11c7016559ccdd24acbf7386a;hpb=6010ad6857ce5690f4053bf8795f2c476daa4694;p=r2c2.git diff --git a/source/libr2c2/layout.cpp b/source/libr2c2/layout.cpp index d6bd9ee..df8ce74 100644 --- a/source/libr2c2/layout.cpp +++ b/source/libr2c2/layout.cpp @@ -1,10 +1,3 @@ -/* $Id$ - -This file is part of R²C² -Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa -Distributed under the GPL -*/ - #include #include #include @@ -41,14 +34,13 @@ Layout::Layout(Catalogue &c, Driver *d): catalogue(c), driver(d), next_turnout_id(0x800) -{ - if(driver) - driver->signal_sensor.connect(sigc::mem_fun(this, &Layout::sensor_event)); -} +{ } Layout::~Layout() { delete driver; + driver = 0; + while(!trains.empty()) delete trains.begin()->second; while(!routes.empty()) @@ -86,6 +78,15 @@ void Layout::remove_track(Track &t) } } +Track *Layout::pick_track(const Vector &start, const Vector &ray) +{ + for(set::iterator i=tracks.begin(); i!=tracks.end(); ++i) + if((*i)->collide_ray(start, ray)) + return *i; + + return 0; +} + unsigned Layout::allocate_turnout_id() { set used_ids; @@ -104,6 +105,12 @@ unsigned Layout::allocate_turnout_id() void Layout::add_block(Block &b) { blocks.insert(&b); + b.signal_reserved.connect(sigc::bind<0>(signal_block_reserved, sigc::ref(b))); + if(b.get_sensor_id()) + { + b.signal_state_changed.connect(sigc::bind<0>(sigc::mem_fun(this, &Layout::block_state_changed), sigc::ref(b))); + b.signal_state_changed.connect(sigc::bind<0>(signal_block_state_changed, sigc::ref(b))); + } } Block &Layout::get_block(unsigned id) const @@ -266,6 +273,8 @@ void Layout::tick() dt = t-last_tick; last_tick = t; + for(set::iterator i=blocks.begin(); i!=blocks.end(); ++i) + (*i)->tick(dt); for(map::iterator i=trains.begin(); i!=trains.end(); ++i) i->second->tick(t, dt); } @@ -332,18 +341,10 @@ void Layout::save_dynamic(const string &fn) const } } -void Layout::sensor_event(unsigned addr, bool state) +void Layout::block_state_changed(Block &block, Block::State state) { - if(state) - { - for(set::iterator i=blocks.begin(); i!=blocks.end(); ++i) - if((*i)->get_sensor_id()==addr) - { - if(!(*i)->get_train()) - emergency(format("Unreserved sensor %d triggered", addr)); - break; - } - } + if(state==Block::ACTIVE && !block.get_train()) + emergency(format("Unreserved sensor %d triggered", block.get_sensor_id())); }