/* $Id$
This file is part of the MSP Märklin suite
-Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa
+Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
Distributed under the GPL
*/
+#include <algorithm>
#include <msp/core/refptr.h>
#include <msp/datafile/parser.h>
#include <msp/datafile/writer.h>
+#include <msp/time/utils.h>
+#include "block.h"
#include "catalogue.h"
+#include "driver.h"
#include "layout.h"
+#include "locotype.h"
+#include "route.h"
+#include "track.h"
#include "tracktype.h"
+#include "train.h"
using namespace std;
using namespace Msp;
namespace Marklin {
-Layout::Layout(const Catalogue &c):
- catalogue(c)
+Layout::Layout(Catalogue &c, Driver *d):
+ catalogue(c),
+ driver(d)
{ }
Layout::~Layout()
{
- for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
- delete *i;
+ delete driver;
+ while(!trains.empty())
+ delete trains.begin()->second;
+ while(!routes.empty())
+ delete routes.begin()->second;
+ while(!tracks.empty())
+ delete *tracks.begin();
+ while(!blocks.empty())
+ delete *blocks.begin();
+}
+
+Driver &Layout::get_driver() const
+{
+ if(!driver)
+ throw InvalidState("No driver");
+ return *driver;
}
void Layout::add_track(Track &t)
{
if(tracks.insert(&t).second)
+ {
+ create_blocks();
signal_track_added.emit(t);
+ }
}
void Layout::remove_track(Track &t)
{
if(tracks.erase(&t))
+ {
+ create_blocks(t);
signal_track_removed.emit(t);
+ }
+}
+
+void Layout::add_block(Block &b)
+{
+ blocks.insert(&b);
+}
+
+Block &Layout::get_block(unsigned id) const
+{
+ for(set<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
+ if((*i)->get_id()==id)
+ return **i;
+
+ throw KeyError("Unknown block", lexical_cast(id));
+}
+
+Block &Layout::get_block_by_track(const Track &t) const
+{
+ for(set<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
+ if((*i)->get_tracks().count(const_cast<Track *>(&t)))
+ return **i;
+
+ throw InvalidParameterValue("No block found for track");
+}
+
+void Layout::create_blocks()
+{
+ set<Track *> used_tracks;
+ for(set<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
+ {
+ const set<Track *> &btracks = (*i)->get_tracks();
+ used_tracks.insert(btracks.begin(), btracks.end());
+ }
+
+ for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
+ if(used_tracks.count(*i)==0)
+ {
+ Block *block = new Block(*this, **i);
+ used_tracks.insert(block->get_tracks().begin(), block->get_tracks().end());
+ }
+
+ for(set<Block *>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
+ for(set<Block *>::iterator j=i; j!=blocks.end(); ++j)
+ if(j!=i)
+ (*i)->check_link(**j);
+}
+
+void Layout::create_blocks(const Track &track)
+{
+ const vector<Track *> &links = track.get_links();
+ for(set<Block *>::iterator i=blocks.begin(); i!=blocks.end();)
+ {
+ bool del = (*i)->get_tracks().count(const_cast<Track *>(&track));
+ for(vector<Track *>::const_iterator j=links.begin(); (!del && j!=links.end()); ++j)
+ del = (*i)->get_tracks().count(*j);
+
+ if(del)
+ delete *i++;
+ else
+ ++i;
+ }
+
+ create_blocks();
+}
+
+void Layout::remove_block(Block &b)
+{
+ blocks.erase(&b);
}
void Layout::add_route(Route &r)
{
- if(routes.insert(&r).second)
- signal_route_added.emit(r);
+ if(routes.count(r.get_name()))
+ throw KeyError("Duplicate route name", r.get_name());
+
+ routes[r.get_name()] = &r;
+ signal_route_added.emit(r);
+}
+
+Route &Layout::get_route(const string &name) const
+{
+ map<string, Route *>::const_iterator i = routes.find(name);
+ if(i==routes.end())
+ throw KeyError("Unknown route", name);
+ return *i->second;
}
void Layout::remove_route(Route &r)
{
- if(routes.erase(&r))
+ if(routes.erase(r.get_name()))
signal_route_removed.emit(r);
}
+void Layout::add_train(Train &t)
+{
+ if(trains.count(t.get_address()))
+ throw KeyError("Duplicate train address", lexical_cast(t.get_address()));
+
+ trains[t.get_address()] = &t;
+ signal_train_added.emit(t);
+}
+
+Train &Layout::get_train(unsigned addr) const
+{
+ map<unsigned, Train *>::const_iterator i = trains.find(addr);
+ if(i==trains.end())
+ throw KeyError("Unknown train", lexical_cast(addr));
+ return *i->second;
+}
+
+void Layout::remove_train(Train &t)
+{
+ if(trains.erase(t.get_address()))
+ signal_train_removed.emit(t);
+}
+
+void Layout::tick()
+{
+ if(driver)
+ driver->tick();
+
+ Time::TimeStamp t = Time::now();
+ Time::TimeDelta dt;
+ if(last_tick)
+ dt = t-last_tick;
+ last_tick = t;
+
+ for(map<unsigned, Train *>::iterator i=trains.begin(); i!=trains.end(); ++i)
+ i->second->tick(t, dt);
+}
+
void Layout::save(const string &fn)
{
IO::BufferedFile out(fn, IO::M_WRITE);
writer.write(st);
}
- for(set<Route *>::iterator i=routes.begin(); i!=routes.end(); ++i)
+ for(map<string, Route *>::iterator i=routes.begin(); i!=routes.end(); ++i)
{
DataFile::Statement st("route");
- st.append((*i)->get_name());
- (*i)->save(st.sub);
+ st.append(i->first);
+ i->second->save(st.sub);
+ writer.write(st);
+ }
+}
+
+void Layout::save_trains(const string &fn)
+{
+ IO::BufferedFile out(fn, IO::M_WRITE);
+ DataFile::Writer writer(out);
+
+ for(map<unsigned, Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
+ {
+ DataFile::Statement st("train");
+ st.append(i->second->get_locomotive_type().get_article_number());
+ st.append(i->second->get_address());
+ i->second->save(st.sub);
writer.write(st);
}
}
void Layout::check_routes()
{
- for(set<Route *>::iterator i=routes.begin(); i!=routes.end(); ++i)
+ for(map<string, Route *>::iterator i=routes.begin(); i!=routes.end(); ++i)
{
- // We must copy the turnout map, since adding tracks to the route will (temporarily) mess it up
- const map<unsigned, int> turnouts = (*i)->get_turnouts();
+ /* We must copy the turnout map, since adding tracks to the route will
+ (temporarily) mess it up */
+ const map<unsigned, int> turnouts = i->second->get_turnouts();
+ // Find any turnout in the route
Track *track = 0;
unsigned trk_path = 0;
for(set<Track *>::const_iterator j=tracks.begin(); j!=tracks.end(); ++j)
}
}
- (*i)->add_track(*track);
+ if(!track)
+ continue;
+ // Find an applicable endpoint
const vector<Endpoint> &eps = track->get_type().get_endpoints();
unsigned ep = 0;
- for(unsigned j=0; j<eps.size(); ++i)
+ for(unsigned j=0; j<eps.size(); ++j)
if(eps[j].paths&(1<<trk_path))
{
ep = j;
break;
}
- Track *start = track;
+ Track *start = 0;
while(1)
{
- int out_ep = track->traverse(ep, trk_path);
- if(out_ep<0)
- break;
+ // Traverse the track and get the next one
+ unsigned out_ep = track->traverse(ep, trk_path);
Track *next = track->get_links()[out_ep];
if(!next || next == start)
break;
+
ep = next->get_endpoint_by_link(*track);
- if(next->get_type().get_n_paths()>1)
+ if(next->get_type().is_turnout())
{
+ // Select correct path across the turnout, or break if we hit an unknown turnout
map<unsigned, int>::const_iterator j = turnouts.find(next->get_turnout_id());
if(j==turnouts.end())
break;
trk_path = j->second;
}
else
+ {
trk_path = 0;
- (*i)->add_track(*next);
+
+ /* Start adding tracks when we find the first non-turnout. This
+ prevents the occurrence of ambiguities while adding the tracks */
+ if(!start)
+ start = next;
+ }
+
+ if(start)
+ i->second->add_track(*next);
+
track = next;
}
}
Layout::Loader::Loader(Layout &l):
- DataFile::BasicLoader<Layout>(l)
+ DataFile::BasicLoader<Layout>(l),
+ new_tracks(false)
{
add("base", &Layout::base);
add("route", &Loader::route);
add("track", &Loader::track);
+ add("train", &Loader::train);
}
void Layout::Loader::finish()
{
- obj.check_links();
+ if(new_tracks)
+ obj.check_links();
obj.check_routes();
for(set<Track *>::iterator i=obj.tracks.begin(); i!=obj.tracks.end(); ++i)
void Layout::Loader::route(const string &n)
{
- RefPtr<Route> rte = new Route(obj, n);
+ Route *rte = new Route(obj, n);
load_sub(*rte);
- obj.add_route(*rte.release());
}
void Layout::Loader::track(unsigned art_nr)
{
- TrackType &type = obj.catalogue.get_track(art_nr);
-
- RefPtr<Track> trk = new Track(type);
+ Track *trk = new Track(obj, obj.catalogue.get_track(art_nr));
load_sub(*trk);
- obj.add_track(*trk.release());
+ new_tracks = true;
+}
+
+void Layout::Loader::train(unsigned art_nr, unsigned addr)
+{
+ Train *trn = new Train(obj, obj.catalogue.get_locomotive(art_nr), addr);
+ load_sub(*trn);
}
} // namespace Marklin