3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
9 #include <msp/strings/formatter.h>
10 #include <msp/time/units.h>
11 #include <msp/time/utils.h>
15 #include "tracktype.h"
16 #include "trafficmanager.h"
24 Train::Train(TrafficManager &tm, Locomotive &l):
37 trfc_mgr.add_train(this);
39 loco.signal_reverse_changed.connect(sigc::mem_fun(this, &Train::locomotive_reverse_changed));
41 const map<unsigned, Sensor *> &sensors = trfc_mgr.get_control().get_sensors();
42 for(map<unsigned, Sensor *>::const_iterator i=sensors.begin(); i!=sensors.end(); ++i)
43 i->second->signal_state_changed.connect(sigc::bind(sigc::mem_fun(this, &Train::sensor_event), i->second));
45 const map<unsigned, Turnout *> &turnouts = trfc_mgr.get_control().get_turnouts();
46 for(map<unsigned, Turnout *>::const_iterator i=turnouts.begin(); i!=turnouts.end(); ++i)
48 i->second->signal_path_changing.connect(sigc::bind(sigc::mem_fun(this, &Train::turnout_path_changing), i->second));
49 i->second->signal_path_changed.connect(sigc::bind(sigc::mem_fun(this, &Train::turnout_path_changed), i->second));
53 void Train::set_name(const string &n)
57 signal_name_changed.emit(name);
60 void Train::set_speed(unsigned speed)
62 if(speed==target_speed)
64 travel_speed = static_cast<int>(round(get_real_speed(speed)*87*3.6/5))*5;
69 trfc_mgr.get_control().set_timer(3*Time::sec).signal_timeout.connect(
70 sigc::bind_return(sigc::mem_fun(this, &Train::release_reserved_blocks), false));
75 signal_target_speed_changed.emit(target_speed);
81 void Train::set_reverse(bool rev)
83 loco.set_reverse(rev);
86 void Train::set_route(const Route *r)
89 signal_route_changed.emit(route);
92 void Train::place(Block *block, unsigned entry)
94 for(list<BlockRef>::iterator i=rsv_blocks.begin(); i!=rsv_blocks.end();)
97 i = rsv_blocks.erase(i);
100 for(list<BlockRef>::iterator i=cur_blocks.begin(); i!=cur_blocks.end();)
102 i->block->reserve(0);
103 i = cur_blocks.erase(i);
106 if(!block->reserve(this))
108 set_status("Unplaced");
112 cur_blocks.push_back(BlockRef(block, entry));
113 set_position(block->get_endpoints()[entry]);
115 set_status("Stopped");
118 bool Train::free_block(Block *block)
121 for(list<BlockRef>::iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
127 while(i!=rsv_blocks.end())
129 i->block->reserve(0);
130 i = rsv_blocks.erase(i);
135 else if(i->block->get_sensor_id())
142 int Train::get_entry_to_block(Block *block) const
144 for(list<BlockRef>::const_iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
147 for(list<BlockRef>::const_iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
153 void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt)
155 if(try_reserve && t>try_reserve)
161 if(cur_track->get_turnout_id())
162 path = trfc_mgr.get_control().get_turnout(cur_track->get_turnout_id()).get_path();
164 offset += get_real_speed(loco.get_speed())*(dt/Time::sec);
165 if(offset>cur_track->get_type().get_path_length(path))
167 int out = cur_track->traverse(cur_track_ep, path);
170 Track *next = cur_track->get_link(out);
172 cur_track_ep = next->get_endpoint_by_link(*cur_track);
181 pos = cur_track->get_point(cur_track_ep, path, offset);
185 void Train::save(list<DataFile::Statement> &st) const
187 st.push_back((DataFile::Statement("name"), name));
188 for(unsigned i=0; i<=14; ++i)
189 if(real_speed[i].weight)
190 st.push_back((DataFile::Statement("real_speed"), i, real_speed[i].speed, real_speed[i].weight));
193 void Train::locomotive_reverse_changed(bool)
195 for(list<BlockRef>::iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
196 i->block->reserve(0);
198 cur_blocks.reverse();
199 for(list<BlockRef>::iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
200 i->entry = i->block->traverse(i->entry);
206 if(unsigned turnout = cur_track->get_turnout_id())
207 path = trfc_mgr.get_control().get_turnout(turnout).get_path();
208 cur_track_ep = cur_track->traverse(cur_track_ep, path);
209 offset = cur_track->get_type().get_path_length(path)-offset;
213 void Train::sensor_event(bool state, Sensor *sensor)
215 unsigned addr = sensor->get_address();
219 list<BlockRef>::iterator i;
220 for(i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
221 if(i->block->get_sensor_id() && i->block->get_sensor_id()!=addr)
224 if(i!=rsv_blocks.begin())
226 float travel_time_secs = (Time::now()-last_entry_time)/Time::sec;
227 travel_speed = static_cast<int>(round(travel_dist/travel_time_secs*87*3.6/5))*5;
231 RealSpeed &rs = real_speed[loco.get_speed()];
232 rs.add(travel_dist/travel_time_secs, travel_time_secs);
237 for(list<BlockRef>::iterator j=rsv_blocks.begin(); j!=i; ++j)
239 j->block->traverse(j->entry, &block_len);
240 travel_dist += block_len;
242 last_entry_time = Time::now();
245 cur_blocks.splice(cur_blocks.end(), rsv_blocks, rsv_blocks.begin(), i);
248 for(i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
249 if(i->block->get_sensor_id()==addr)
250 set_position(i->block->get_endpoints()[i->entry]);
252 if(target_speed && reserve_more()<2)
257 for(list<BlockRef>::iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
258 if(unsigned b_addr = i->block->get_sensor_id())
263 for(list<BlockRef>::iterator j=cur_blocks.begin(); j!=i; ++j)
264 j->block->reserve(0);
265 cur_blocks.erase(cur_blocks.begin(), i);
270 if(target_speed && pending_block && addr==pending_block->get_sensor_id())
275 void Train::turnout_path_changing(unsigned, Turnout *turnout)
277 unsigned tid = turnout->get_address();
278 for(list<BlockRef>::const_iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
279 if(i->block->get_turnout_id()==tid)
280 throw TurnoutBusy(this);
283 for(list<BlockRef>::const_iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
285 if(i->block->get_turnout_id()==tid)
288 throw TurnoutBusy(this);
291 else if(i->block->get_sensor_id())
296 void Train::turnout_path_changed(unsigned, Turnout *turnout)
298 unsigned tid = turnout->get_address();
299 for(list<BlockRef>::iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
300 if(i->block->get_turnout_id()==tid)
302 while(i!=rsv_blocks.end())
304 i->block->reserve(0);
305 i = rsv_blocks.erase(i);
311 if(pending_block && tid==pending_block->get_turnout_id())
315 unsigned Train::reserve_more()
318 if(!rsv_blocks.empty())
319 last = &rsv_blocks.back();
320 else if(!cur_blocks.empty())
321 last = &cur_blocks.back();
327 // See how many blocks we already have
329 for(list<BlockRef>::const_iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
330 if(i->block->get_sensor_id())
333 bool got_more = false;
334 BlockRef *good = last;
335 unsigned good_sens = nsens;
338 // Traverse to the next block
339 unsigned exit = last->block->traverse(last->entry);
340 Block *link = last->block->get_link(exit);
344 int entry = link->get_endpoint_by_link(*last->block);
345 if(!link->reserve(this))
347 // If we found another train going in the same direction as us, we can keep the blocks we got
348 int other_entry = link->get_train()->get_entry_to_block(link);
349 if(other_entry==entry || link->traverse(entry)==link->traverse(other_entry))
354 pending_block = link;
358 if(link->get_turnout_id())
360 const Block::Endpoint &ep = link->get_endpoints()[entry];
361 const Endpoint &track_ep = ep.track->get_type().get_endpoints()[ep.track_ep];
363 if(track_ep.paths&(track_ep.paths-1))
365 // We're facing the points - keep the blocks reserved so far
370 Turnout &turnout = trfc_mgr.get_control().get_turnout(link->get_turnout_id());
372 // Figure out what path we'd like to take on the turnout
375 path = route->get_turnout(link->get_turnout_id());
377 path = turnout.get_path();
378 if(!((track_ep.paths>>path)&1))
380 for(unsigned i=0; track_ep.paths>>i; ++i)
381 if((track_ep.paths>>i)&1)
385 if(path!=turnout.get_path())
387 // The turnout is set to wrong path - switch and wait for it
389 pending_block = link;
390 turnout.set_path(path);
395 rsv_blocks.push_back(BlockRef(link, entry));
396 last = &rsv_blocks.back();
397 if(last->block->get_sensor_id())
404 // Unreserve blocks that were not good
405 while(!rsv_blocks.empty() && last!=good)
407 last->block->reserve(0);
408 rsv_blocks.erase(--rsv_blocks.end());
409 if(!rsv_blocks.empty())
410 last = &rsv_blocks.back();
419 void Train::update_speed()
424 try_reserve = Time::TimeStamp();
425 set_status("Stopped");
430 for(list<BlockRef>::const_iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
431 if(i->block->get_sensor_id())
434 unsigned slow_speed = find_speed(0.1); // 31.3 km/h
439 try_reserve = Time::now()+2*Time::sec;
440 set_status("Blocked");
442 else if(nsens==1 && target_speed>slow_speed)
444 loco.set_speed(slow_speed);
446 try_reserve = Time::now()+2*Time::sec;
451 loco.set_speed(target_speed);
452 try_reserve = Time::TimeStamp();
453 set_status(format("Traveling %d kmh", travel_speed));
458 float Train::get_real_speed(unsigned i) const
460 if(real_speed[i].weight)
461 return real_speed[i].speed;
465 for(low=i; low>0; --low)
466 if(real_speed[low].weight)
468 for(high=i; high<14; ++high)
469 if(real_speed[high].weight)
472 if(real_speed[high].weight)
474 if(real_speed[low].weight)
476 float f = float(i-low)/(high-low);
477 return real_speed[low].speed*(1-f)+real_speed[high].speed*f;
480 return real_speed[high].speed*float(i)/high;
482 else if(real_speed[low].weight)
483 return real_speed[low].speed*float(i)/low;
488 unsigned Train::find_speed(float real) const
490 if(real<=real_speed[0].speed)
495 for(unsigned i=0; (!high && i<=14); ++i)
496 if(real_speed[i].weight)
498 if(real_speed[i].speed<real)
507 return min(static_cast<unsigned>(low*real/real_speed[low].speed), 14U);
510 float f = (real-real_speed[low].speed)/(real_speed[high].speed-real_speed[low].speed);
511 return static_cast<unsigned>(low*(1-f)+high*f+0.5);
514 void Train::set_status(const string &s)
517 signal_status_changed.emit(s);
520 void Train::set_position(const Block::Endpoint &bep)
522 cur_track = bep.track;
523 cur_track_ep = bep.track_ep;
525 pos = cur_track->get_endpoint_position(cur_track_ep);
528 void Train::release_reserved_blocks()
530 for(list<BlockRef>::iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
531 i->block->reserve(0);
536 Train::RealSpeed::RealSpeed():
541 void Train::RealSpeed::add(float s, float w)
543 speed = (speed*weight+s*w)/(weight+w);
544 weight = min(weight+w, 300.0f);
548 Train::Loader::Loader(Train &t):
549 DataFile::BasicLoader<Train>(t)
551 add("name", &Train::name);
552 add("real_speed", &Loader::real_speed);
555 void Train::Loader::real_speed(unsigned i, float speed, float weight)
557 obj.real_speed[i].speed = speed;
558 obj.real_speed[i].weight = weight;
561 } // namespace Marklin