y -= tpanel->get_geometry().h;
}
+ const list<Block *> &blocks = trfc_mgr->get_blocks();
+ for(list<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
+ reset_block_color(**i);
+
wnd->show();
Application::main();
const list<Train *> &trains = trfc_mgr->get_trains();
for(list<Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
{
+ if(!(*i)->is_placed())
+ continue;
+
GL::PushMatrix _push;
const Point &tp = (*i)->get_position();
tgl_forward->signal_toggled.connect(sigc::mem_fun(this, &TrainPanel::forward_toggled));
train.get_locomotive().signal_reverse_changed.connect(sigc::mem_fun(this, &TrainPanel::train_reverse_changed));
- add(*(lbl_route=new GLtk::Label(res, "Free run")));
+ const Route *route = train.get_route();
+ add(*(lbl_route=new GLtk::Label(res, (route ? route->get_name() : "Free run"))));
lbl_route->set_style("digital");
lbl_route->set_geometry(GLtk::Geometry(10, 58, geom.w-20, 24));
train.signal_route_changed.connect(sigc::mem_fun(this, &TrainPanel::train_route_changed));
Distributed under the GPL
*/
+#include <algorithm>
#include "control.h"
#include "block.h"
#include "tracktype.h"
namespace Marklin {
-unsigned Block::next_id = 1;
-
Block::Block(TrafficManager &tm, Track &start):
trfc_mgr(tm),
- id(next_id++),
+ id(0),
sensor_id(start.get_sensor_id()),
turnout_id(start.get_turnout_id()),
train(0)
}
}
+ if(sensor_id)
+ id = 0x1000|sensor_id;
+ else if(turnout_id)
+ id = 0x2000|turnout_id;
+
for(unsigned i=0; i<endpoints.size(); ++i)
{
unsigned path = 1<<i;
j->link = this;
}
}
+
+ if(!sensor_id && !turnout_id && endpoints.size()==2)
+ {
+ unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1;
+ unsigned id2 = endpoints[1].link ? endpoints[1].link->get_id() : 1;
+ if(id2<id1)
+ swap(id1, id2);
+ id = (id1<<16)|id2;
+ }
}
Block *Block::get_link(unsigned epi) const
public:
Block(TrafficManager &, Track &);
+ unsigned get_id() const { return id; }
unsigned get_sensor_id() const { return sensor_id; }
unsigned get_turnout_id() const { return turnout_id; }
const std::set<Track *> &get_tracks() const { return tracks; }
void print_debug();
private:
void find_paths(Track &, unsigned, unsigned, std::set<Track *> &);
-
- static unsigned next_id;
};
} // namespace Marklin
delete *i;
}
+Block &TrafficManager::get_block(unsigned id) const
+{
+ for(list<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 &TrafficManager::get_block_by_track(const Track &t) const
{
for(list<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
Control &get_control() const { return control; }
Layout &get_layout() const { return layout; }
const std::list<Block *> &get_blocks() const { return blocks; }
+ Block &get_block(unsigned) const;
Block &get_block_by_track(const Track &) const;
const std::list<Train *> &get_trains() const { return trains; }
Train &get_train_by_locomotive(const Locomotive &) const;
#include <msp/time/utils.h>
#include "control.h"
#include "except.h"
+#include "layout.h"
#include "route.h"
#include "tracktype.h"
#include "trafficmanager.h"
for(unsigned i=0; i<=14; ++i)
if(real_speed[i].weight)
st.push_back((DataFile::Statement("real_speed"), i, real_speed[i].speed, real_speed[i].weight));
+ if(route)
+ st.push_back((DataFile::Statement("route"), route->get_name()));
+
+ if(!cur_blocks.empty())
+ {
+ list<BlockRef> blocks = cur_blocks;
+ if(loco.get_reverse())
+ reverse_blocks(blocks);
+
+ Block *prev = blocks.front().block->get_endpoints()[blocks.front().entry].link;
+ st.push_back((DataFile::Statement("block_hint"), prev->get_id()));
+
+ for(list<BlockRef>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
+ st.push_back((DataFile::Statement("block"), i->block->get_id()));
+ }
}
void Train::locomotive_reverse_changed(bool)
for(list<BlockRef>::iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
i->block->reserve(0);
rsv_blocks.clear();
- cur_blocks.reverse();
- for(list<BlockRef>::iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
- i->entry = i->block->traverse(i->entry);
+ reverse_blocks(cur_blocks);
reserve_more();
if(cur_track)
blocks.clear();
}
+void Train::reverse_blocks(list<BlockRef> &blocks) const
+{
+ blocks.reverse();
+ for(list<BlockRef>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
+ i->entry = i->block->traverse(i->entry);
+}
+
Train::RealSpeed::RealSpeed():
speed(0),
Train::Loader::Loader(Train &t):
- DataFile::BasicLoader<Train>(t)
+ DataFile::BasicLoader<Train>(t),
+ prev_block(0)
{
+ add("block", &Loader::block);
+ add("block_hint", &Loader::block_hint);
add("name", &Train::name);
add("real_speed", &Loader::real_speed);
+ add("route", &Loader::route);
+}
+
+void Train::Loader::block(unsigned id)
+{
+ Block &blk = obj.trfc_mgr.get_block(id);
+ int entry = -1;
+ if(prev_block)
+ entry = blk.get_endpoint_by_link(*prev_block);
+ if(entry<0)
+ entry = 0;
+
+ blk.reserve(&obj);
+ obj.cur_blocks.push_back(BlockRef(&blk, entry));
+ obj.status = "Stopped";
+ obj.set_position(blk.get_endpoints()[entry]);
+
+ prev_block = &blk;
+}
+
+void Train::Loader::block_hint(unsigned id)
+{
+ prev_block = &obj.trfc_mgr.get_block(id);
}
void Train::Loader::real_speed(unsigned i, float speed, float weight)
obj.real_speed[i].weight = weight;
}
+void Train::Loader::route(const string &n)
+{
+ obj.route = &obj.trfc_mgr.get_layout().get_route(n);
+}
+
} // namespace Marklin
public:
class Loader: public Msp::DataFile::BasicLoader<Train>
{
+ private:
+ Block *prev_block;
+
public:
Loader(Train &);
private:
+ void block(unsigned);
+ void block_hint(unsigned);
void real_speed(unsigned, float, float);
+ void route(const std::string &);
};
sigc::signal<void, const std::string &> signal_name_changed;
const std::string &get_status() const { return status; }
const Point &get_position() const { return pos; }
void place(Block &, unsigned);
+ bool is_placed() const { return !cur_blocks.empty(); }
bool free_block(Block &);
int get_entry_to_block(Block &) const;
void tick(const Msp::Time::TimeStamp &, const Msp::Time::TimeDelta &);
void set_status(const std::string &);
void set_position(const Block::Endpoint &);
void release_blocks(std::list<BlockRef> &);
+ void reverse_blocks(std::list<BlockRef> &) const;
};
} // namespace Marklin