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>
16 #include "tracktype.h"
17 #include "trafficmanager.h"
25 Train::Train(TrafficManager &tm, Locomotive &l):
38 trfc_mgr.add_train(this);
40 loco.signal_reverse_changed.connect(sigc::mem_fun(this, &Train::locomotive_reverse_changed));
42 const map<unsigned, Sensor *> &sensors = trfc_mgr.get_control().get_sensors();
43 for(map<unsigned, Sensor *>::const_iterator i=sensors.begin(); i!=sensors.end(); ++i)
44 i->second->signal_state_changed.connect(sigc::bind(sigc::mem_fun(this, &Train::sensor_event), i->second));
46 const map<unsigned, Turnout *> &turnouts = trfc_mgr.get_control().get_turnouts();
47 for(map<unsigned, Turnout *>::const_iterator i=turnouts.begin(); i!=turnouts.end(); ++i)
49 i->second->signal_path_changing.connect(sigc::bind(sigc::mem_fun(this, &Train::turnout_path_changing), i->second));
50 i->second->signal_path_changed.connect(sigc::bind(sigc::mem_fun(this, &Train::turnout_path_changed), i->second));
53 trfc_mgr.signal_block_reserved.connect(sigc::mem_fun(this, &Train::block_reserved));
56 void Train::set_name(const string &n)
60 signal_name_changed.emit(name);
63 void Train::set_speed(unsigned speed)
65 if(speed==target_speed)
67 travel_speed = static_cast<int>(round(get_real_speed(speed)*87*3.6/5))*5;
73 trfc_mgr.get_control().set_timer(3*Time::sec).signal_timeout.connect(
74 sigc::bind_return(sigc::bind(
75 sigc::mem_fun(this, static_cast<void (Train::*)(list<BlockRef> &)>(&Train::release_blocks)),
76 sigc::ref(rsv_blocks)), false));
81 signal_target_speed_changed.emit(target_speed);
87 void Train::set_reverse(bool rev)
89 loco.set_reverse(rev);
92 void Train::set_route(const Route *r)
95 signal_route_changed.emit(route);
98 void Train::place(Block &block, unsigned entry)
103 release_blocks(rsv_blocks);
104 release_blocks(cur_blocks);
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 release_blocks(rsv_blocks, i, rsv_blocks.end());
131 else if(i->block->get_sensor_id())
138 int Train::get_entry_to_block(Block &block) const
140 for(list<BlockRef>::const_iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
143 for(list<BlockRef>::const_iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
149 void Train::tick(const Time::TimeStamp &, const Time::TimeDelta &dt)
154 if(cur_track->get_turnout_id())
155 path = trfc_mgr.get_control().get_turnout(cur_track->get_turnout_id()).get_path();
157 offset += get_real_speed(loco.get_speed())*(dt/Time::sec);
158 float path_len = cur_track->get_type().get_path_length(path);
161 unsigned out = cur_track->traverse(cur_track_ep, path);
162 Track *next = cur_track->get_link(out);
165 for(list<BlockRef>::const_iterator i=cur_blocks.begin(); (!ok && i!=cur_blocks.end()); ++i)
166 ok = i->block->get_tracks().count(next);
171 cur_track_ep = next->get_endpoint_by_link(*cur_track);
176 offset = path_len-0.001;
180 pos = cur_track->get_point(cur_track_ep, path, offset);
184 void Train::save(list<DataFile::Statement> &st) const
186 st.push_back((DataFile::Statement("name"), name));
187 for(unsigned i=0; i<=14; ++i)
188 if(real_speed[i].weight)
189 st.push_back((DataFile::Statement("real_speed"), i, real_speed[i].speed, real_speed[i].weight));
191 st.push_back((DataFile::Statement("route"), route->get_name()));
193 if(!cur_blocks.empty())
195 list<BlockRef> blocks = cur_blocks;
196 if(loco.get_reverse())
197 reverse_blocks(blocks);
199 Block *prev = blocks.front().block->get_endpoints()[blocks.front().entry].link;
200 st.push_back((DataFile::Statement("block_hint"), prev->get_id()));
202 for(list<BlockRef>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
203 st.push_back((DataFile::Statement("block"), i->block->get_id()));
207 void Train::locomotive_reverse_changed(bool)
209 release_blocks(rsv_blocks);
210 reverse_blocks(cur_blocks);
216 if(unsigned turnout = cur_track->get_turnout_id())
217 path = trfc_mgr.get_control().get_turnout(turnout).get_path();
218 cur_track_ep = cur_track->traverse(cur_track_ep, path);
219 offset = cur_track->get_type().get_path_length(path)-offset;
223 void Train::sensor_event(bool state, Sensor *sensor)
225 unsigned addr = sensor->get_address();
229 // Find the first sensor block from our reserved blocks that isn't this sensor
230 list<BlockRef>::iterator i;
231 for(i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
232 if(i->block->get_sensor_id() && i->block->get_sensor_id()!=addr)
235 if(i!=rsv_blocks.begin())
237 // Compute speed and update related state
238 float travel_time_secs = (Time::now()-last_entry_time)/Time::sec;
239 travel_speed = static_cast<int>(round(travel_dist/travel_time_secs*87*3.6/5))*5;
243 RealSpeed &rs = real_speed[loco.get_speed()];
244 rs.add(travel_dist/travel_time_secs, travel_time_secs);
249 for(list<BlockRef>::iterator j=rsv_blocks.begin(); j!=i; ++j)
251 j->block->traverse(j->entry, &block_len);
252 travel_dist += block_len;
254 if(j->block->get_sensor_id()==addr)
255 set_position(j->block->get_endpoints()[j->entry]);
257 last_entry_time = Time::now();
260 // Move blocks up to the next sensor to our current blocks
261 cur_blocks.splice(cur_blocks.end(), rsv_blocks, rsv_blocks.begin(), i);
263 // Try to get more blocks if we're moving
264 if(target_speed && reserve_more()<2)
270 // Find the first sensor in our current blocks that's still active
271 list<BlockRef>::iterator end = cur_blocks.begin();
272 for(list<BlockRef>::iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
273 if(i->block->get_sensor_id())
275 if(trfc_mgr.get_control().get_sensor(i->block->get_sensor_id()).get_state())
281 if(end!=cur_blocks.begin())
283 // Free blocks up to the last inactive sensor
285 release_blocks(cur_blocks, cur_blocks.begin(), end);
290 void Train::turnout_path_changing(unsigned, Turnout *turnout)
292 unsigned tid = turnout->get_address();
293 for(list<BlockRef>::const_iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
294 if(i->block->get_turnout_id()==tid)
295 throw TurnoutBusy(this);
298 for(list<BlockRef>::const_iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
300 if(i->block->get_turnout_id()==tid)
303 throw TurnoutBusy(this);
306 else if(i->block->get_sensor_id())
311 void Train::turnout_path_changed(unsigned, Turnout *turnout)
313 unsigned tid = turnout->get_address();
314 for(list<BlockRef>::iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
315 if(i->block->get_turnout_id()==tid)
317 release_blocks(rsv_blocks, i, rsv_blocks.end());
322 if(pending_block && tid==pending_block->get_turnout_id())
326 void Train::block_reserved(const Block &block, const Train *train)
328 if(&block==pending_block && !train)
332 unsigned Train::reserve_more()
335 if(!rsv_blocks.empty())
336 last = &rsv_blocks.back();
337 else if(!cur_blocks.empty())
338 last = &cur_blocks.back();
344 // See how many blocks we already have
346 for(list<BlockRef>::const_iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
347 if(i->block->get_sensor_id())
350 bool got_more = false;
351 BlockRef *good = last;
352 unsigned good_sens = nsens;
355 // Traverse to the next block
356 unsigned exit = last->block->traverse(last->entry);
357 Block *link = last->block->get_link(exit);
361 int entry = link->get_endpoint_by_link(*last->block);
362 if(!link->reserve(this))
364 // If we found another train going in the same direction as us, we can keep the blocks we got
365 int other_entry = link->get_train()->get_entry_to_block(*link);
366 if(other_entry==entry || link->traverse(entry)==link->traverse(other_entry))
371 pending_block = link;
375 if(link->get_turnout_id())
377 const Block::Endpoint &ep = link->get_endpoints()[entry];
378 const Endpoint &track_ep = ep.track->get_type().get_endpoints()[ep.track_ep];
380 if(track_ep.paths&(track_ep.paths-1))
382 // We're facing the points - keep the blocks reserved so far
387 Turnout &turnout = trfc_mgr.get_control().get_turnout(link->get_turnout_id());
389 // Figure out what path we'd like to take on the turnout
392 path = route->get_turnout(link->get_turnout_id());
394 path = turnout.get_path();
395 if(!((track_ep.paths>>path)&1))
397 for(unsigned i=0; track_ep.paths>>i; ++i)
398 if((track_ep.paths>>i)&1)
402 if(path!=turnout.get_path())
404 // The turnout is set to wrong path - switch and wait for it
406 pending_block = link;
407 turnout.set_path(path);
412 rsv_blocks.push_back(BlockRef(link, entry));
413 last = &rsv_blocks.back();
414 if(last->block->get_sensor_id())
421 // Unreserve blocks that were not good
422 while(!rsv_blocks.empty() && last!=good)
424 last->block->reserve(0);
425 rsv_blocks.erase(--rsv_blocks.end());
426 if(!rsv_blocks.empty())
427 last = &rsv_blocks.back();
436 void Train::update_speed()
441 set_status("Stopped");
446 for(list<BlockRef>::const_iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
447 if(i->block->get_sensor_id())
450 unsigned slow_speed = find_speed(0.1); // 31.3 km/h
455 set_status("Blocked");
457 else if(nsens==1 && target_speed>slow_speed)
459 loco.set_speed(slow_speed);
465 loco.set_speed(target_speed);
466 set_status(format("Traveling %d kmh", travel_speed));
471 float Train::get_real_speed(unsigned i) const
473 if(real_speed[i].weight)
474 return real_speed[i].speed;
478 for(low=i; low>0; --low)
479 if(real_speed[low].weight)
481 for(high=i; high<14; ++high)
482 if(real_speed[high].weight)
485 if(real_speed[high].weight)
487 if(real_speed[low].weight)
489 float f = float(i-low)/(high-low);
490 return real_speed[low].speed*(1-f)+real_speed[high].speed*f;
493 return real_speed[high].speed*float(i)/high;
495 else if(real_speed[low].weight)
496 return real_speed[low].speed*float(i)/low;
501 unsigned Train::find_speed(float real) const
503 if(real<=real_speed[0].speed)
508 for(unsigned i=0; (!high && i<=14); ++i)
509 if(real_speed[i].weight)
511 if(real_speed[i].speed<real)
520 return min(static_cast<unsigned>(low*real/real_speed[low].speed), 14U);
523 float f = (real-real_speed[low].speed)/(real_speed[high].speed-real_speed[low].speed);
524 return static_cast<unsigned>(low*(1-f)+high*f+0.5);
527 void Train::set_status(const string &s)
530 signal_status_changed.emit(s);
533 void Train::set_position(const Block::Endpoint &bep)
535 cur_track = bep.track;
536 cur_track_ep = bep.track_ep;
538 pos = cur_track->get_endpoint_position(cur_track_ep);
541 void Train::release_blocks(list<BlockRef> &blocks)
543 release_blocks(blocks, blocks.begin(), blocks.end());
546 void Train::release_blocks(list<BlockRef> &blocks, list<BlockRef>::iterator begin, list<BlockRef>::iterator end)
550 Block *block = begin->block;
551 blocks.erase(begin++);
556 void Train::reverse_blocks(list<BlockRef> &blocks) const
559 for(list<BlockRef>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
560 i->entry = i->block->traverse(i->entry);
564 Train::RealSpeed::RealSpeed():
569 void Train::RealSpeed::add(float s, float w)
571 speed = (speed*weight+s*w)/(weight+w);
572 weight = min(weight+w, 300.0f);
576 Train::Loader::Loader(Train &t):
577 DataFile::BasicLoader<Train>(t),
580 add("block", &Loader::block);
581 add("block_hint", &Loader::block_hint);
582 add("name", &Train::name);
583 add("real_speed", &Loader::real_speed);
584 add("route", &Loader::route);
587 void Train::Loader::block(unsigned id)
589 Block &blk = obj.trfc_mgr.get_block(id);
592 entry = blk.get_endpoint_by_link(*prev_block);
597 obj.cur_blocks.push_back(BlockRef(&blk, entry));
598 obj.status = "Stopped";
599 obj.set_position(blk.get_endpoints()[entry]);
604 void Train::Loader::block_hint(unsigned id)
606 prev_block = &obj.trfc_mgr.get_block(id);
609 void Train::Loader::real_speed(unsigned i, float speed, float weight)
611 obj.real_speed[i].speed = speed;
612 obj.real_speed[i].weight = weight;
615 void Train::Loader::route(const string &n)
617 obj.route = &obj.trfc_mgr.get_layout().get_route(n);
620 } // namespace Marklin