X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Flayout.cpp;h=df6989d3320cd3c7cbf4fb0f850915920f8ff431;hb=b14059de03324aecde3efc649293d98ce5b7aaf2;hp=cbe226a684c31ffcf6229fd5d1ead978530ae269;hpb=4e406ca29b1bb87a8e20324e5cf54f5b5de6afb8;p=r2c2.git diff --git a/source/libr2c2/layout.cpp b/source/libr2c2/layout.cpp index cbe226a..df6989d 100644 --- a/source/libr2c2/layout.cpp +++ b/source/libr2c2/layout.cpp @@ -1,7 +1,7 @@ /* $Id$ This file is part of R²C² -Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa +Copyright © 2006-2011 Mikkosoft Productions, Mikko Rasa Distributed under the GPL */ @@ -41,14 +41,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 +85,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 +112,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 +280,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); } @@ -312,11 +328,15 @@ void Layout::save(const string &fn) const } } -void Layout::save_trains(const string &fn) const +void Layout::save_dynamic(const string &fn) const { IO::BufferedFile out(fn, IO::M_WRITE); DataFile::Writer writer(out); + for(set::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) + if(unsigned tid = (*i)->get_turnout_id()) + writer.write((DataFile::Statement("turnout"), tid, (*i)->get_active_path())); + for(map::const_iterator i=trains.begin(); i!=trains.end(); ++i) { DataFile::Statement st("train"); @@ -328,18 +348,10 @@ void Layout::save_trains(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())); } @@ -351,6 +363,7 @@ Layout::Loader::Loader(Layout &l): add("route", static_cast(&Loader::route)); add("track", static_cast(&Loader::track)); add("train", static_cast(&Loader::train)); + add("turnout", &Loader::turnout); add("zone", &Loader::zone); // Deprecated aliases @@ -404,6 +417,12 @@ void Layout::Loader::train(ArticleNumber art_nr, unsigned addr, const std::strin load_sub(*trn); } +void Layout::Loader::turnout(unsigned addr, unsigned path) +{ + if(obj.driver) + obj.driver->set_turnout(addr, path); +} + void Layout::Loader::zone() { Zone *zne = new Zone(obj);