2 #include "trafficmanager.h"
7 Train::Train(TrafficManager &tm, Locomotive &l):
12 trfc_mgr.add_train(this);
13 trfc_mgr.get_control().signal_sensor_event.connect(sigc::mem_fun(this, &Train::sensor_event));
16 void Train::set_speed(unsigned speed)
19 if(rsv_blocks.empty() && !reserve_more())
21 loco.set_speed(speed);
24 void Train::place(Block *block, const Block::Endpoint *entry)
26 for(BlockRefSeq::iterator i=rsv_blocks.begin(); i!=rsv_blocks.end();)
29 i=rsv_blocks.erase(i);
32 for(BlockRefSeq::iterator i=cur_blocks.begin(); i!=cur_blocks.end();)
35 i=cur_blocks.erase(i);
38 if(!block->reserve(this))
41 cur_blocks.push_back(BlockRef(block, entry));
44 bool Train::free_block(Block *block)
46 for(BlockRefSeq::iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
49 while(i!=rsv_blocks.end())
52 i=rsv_blocks.erase(i);
60 void Train::sensor_event(unsigned addr, bool state)
64 BlockRefSeq::iterator i;
65 for(i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
66 if(i->block->get_sensor_id() && i->block->get_sensor_id()!=addr)
68 cur_blocks.splice(cur_blocks.begin(), rsv_blocks, rsv_blocks.begin(), i);
71 if(rsv_blocks.empty())
76 BlockRefSeq::iterator i;
77 for(i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
78 if(i->block->get_sensor_id()==addr)
80 if(i!=cur_blocks.end())
83 for(BlockRefSeq::iterator j=cur_blocks.begin(); j!=i; ++j)
85 cur_blocks.erase(cur_blocks.begin(), i);
92 bool Train::reserve_more()
95 if(!rsv_blocks.empty())
96 last=&rsv_blocks.back();
97 else if(!cur_blocks.empty())
98 last=&cur_blocks.back();
103 unsigned size=rsv_blocks.size();
106 const Block::Endpoint *exit=last->block->traverse(last->entry);
107 if(exit->link->reserve(this))
109 rsv_blocks.push_back(BlockRef(exit->link, exit->link->get_endpoint_by_link(last->block)));
110 last=&rsv_blocks.back();
121 } // namespace Marklin