#include <msp/time/units.h>
#include <msp/time/utils.h>
#include "aicontrol.h"
+#include "block.h"
#include "catalogue.h"
#include "driver.h"
#include "layout.h"
#include "route.h"
#include "simplecontroller.h"
#include "speedquantizer.h"
-#include "timetable.h"
+#include "trackcircuit.h"
#include "trackiter.h"
#include "tracktype.h"
#include "train.h"
layout.get_driver().signal_loco_speed.connect(sigc::mem_fun(this, &Train::loco_speed_event));
layout.get_driver().signal_loco_function.connect(sigc::mem_fun(this, &Train::loco_func_event));
- layout.signal_block_state_changed.connect(sigc::mem_fun(this, &Train::block_state_changed));
+ layout.signal_sensor_state_changed.connect(sigc::mem_fun(this, &Train::sensor_state_changed));
layout.get_driver().signal_halt.connect(sigc::mem_fun(this, &Train::halt_event));
active = a;
if(active)
{
- stop_timeout = Time::TimeStamp();
+ stop_timeout = Time::zero;
allocator.reserve_more();
}
else
- stop_timeout = Time::now()+2*Time::sec;
+ stop_timeout = 2*Time::sec;
}
void Train::set_function(unsigned func, bool state)
bool Train::free_block(Block &block)
{
- if(get_reserved_distance_until(&block, false)<controller->get_braking_distance()*1.3)
+ if(get_reserved_distance_until(&block)<controller->get_braking_distance()*1.3)
return false;
return allocator.release_from(block);
float margin = 10*layout.get_catalogue().get_scale();
float min_dist = controller->get_braking_distance()*1.3+margin;
- Vehicle &veh = *(reverse ? vehicles.back() : vehicles.front());
-
- TrackIter track = veh.get_track_iter();
- BlockIter block = allocator.first();
- const BlockIter &last_cur = allocator.last_current();
- const BlockIter &last = allocator.last();
- bool in_rsv = false;
- while(!block->has_track(*track))
+ BlockIter i = allocator.last_current().next();
+ float dist = 0;
+ bool sensor_seen = false;
+ for(; i->get_train()==this; i=i.next())
{
- if(&*block==&*last_cur)
- in_rsv = true;
- if(&*block==&*last)
- break;
- block = block.next();
- }
-
- float dist = veh.get_offset();
- if(reverse)
- track.reverse();
- else
- dist = track->get_type().get_path_length(track->get_active_path())-dist;
- dist -= veh.get_type().get_length()/2;
-
- bool nsens = 0;
- while(1)
- {
- track = track.next();
-
- if(!block->has_track(*track))
+ if(dist>min_dist && sensor_seen)
{
- if(&*block==&*last_cur)
- in_rsv = true;
- if(&*block==&*last)
- return;
- block = block.next();
-
- if(dist>min_dist && nsens>0)
- {
- allocator.release_from(*block);
- return;
- }
-
- if(in_rsv && block->get_sensor_id())
- ++nsens;
+ allocator.release_from(*i);
+ return;
}
- dist += track->get_type().get_path_length(track->get_active_path());
+ dist += i->get_path_length(i.entry());
+
+ if(i->get_sensor_id())
+ sensor_seen = true;
}
}
if(next && next->get_type().is_turnout())
margin = 15*layout.get_catalogue().get_scale();
- return max(get_reserved_distance_until(0, false)-margin, 0.0f);
+ return max(get_reserved_distance_until(0)-margin, 0.0f);
}
void Train::reserve_more()
allocator.reserve_more();
}
-void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt)
+void Train::tick(const Time::TimeDelta &dt)
{
- if(!active && stop_timeout && t>=stop_timeout)
+ if(!active && stop_timeout)
{
- allocator.release_noncurrent();
- stop_timeout = Time::TimeStamp();
+ stop_timeout -= dt;
+ if(stop_timeout<=Time::zero)
+ {
+ allocator.release_noncurrent();
+ stop_timeout = Time::TimeDelta();
+ }
}
+ travel_time += dt;
+
Driver &driver = layout.get_driver();
for(list<TrainAI *>::iterator i=ais.begin(); i!=ais.end(); ++i)
- (*i)->tick(t, dt);
+ (*i)->tick(dt);
controller->tick(dt);
float speed = controller->get_speed();
bool moving = speed>0;
}
}
}
-
- if(!allocator.empty() && !allocator.first()->get_sensor_id())
- {
- float dist = get_reserved_distance_until(&*allocator.first(), true);
-
- if(dist>10*layout.get_catalogue().get_scale())
- allocator.release_until(*allocator.first());
- }
}
void Train::save(list<DataFile::Statement> &st) const
router->save(ss.sub);
st.push_back(ss);
}
- else if(Timetable *timetable = dynamic_cast<Timetable *>(*i))
- {
- DataFile::Statement ss("timetable");
- timetable->save(ss.sub);
- st.push_back(ss);
- }
}
}
}
}
-void Train::block_state_changed(Block &block, Block::State state)
+void Train::sensor_state_changed(Sensor &sensor, Sensor::State state)
{
- if(block.get_train()==this && state==Block::MAYBE_ACTIVE)
+ Block *block = 0;
+ if(TrackCircuit *tc = dynamic_cast<TrackCircuit *>(&sensor))
+ block = &tc->get_block();
+ else
+ return;
+
+ if(block->get_train()==this && state==Sensor::MAYBE_ACTIVE)
{
if(last_entry_block)
{
if(pure_speed && speed_quantizer && current_speed_step>0)
travel_distance = 0;
- for(BlockIter i=last_entry_block; &*i!=█ i=i.next())
+ for(BlockIter i=last_entry_block; &*i!=block; i=i.next())
{
if(i->get_sensor_id())
return;
if(travel_distance>0)
{
- float travel_time_secs = (Time::now()-last_entry_time)/Time::sec;
+ float travel_time_secs = travel_time/Time::sec;
if(travel_time_secs>=2)
speed_quantizer->learn(current_speed_step, travel_distance/travel_time_secs, travel_time_secs);
}
}
- last_entry_block = allocator.iter_for(block);
- last_entry_time = Time::now();
+ last_entry_block = allocator.iter_for(*block);
+ travel_time = Time::zero;
pure_speed = true;
accurate_position = true;
overshoot_dist = 0;
accurate_position = false;
}
-float Train::get_reserved_distance_until(const Block *until_block, bool back) const
+float Train::get_reserved_distance_until(const Block *until_block) const
{
if(allocator.empty())
return 0;
- Vehicle &veh = *(reverse!=back ? vehicles.back() : vehicles.front());
- const VehicleType &vtype = veh.get_type();
+ Vehicle &veh = *(reverse ? vehicles.back() : vehicles.front());
TrackIter track = veh.get_track_iter();
if(!track) // XXX Probably unnecessary
return 0;
- const BlockIter &first = allocator.first();
- const BlockIter &last = allocator.last();
- BlockIter block = first;
- while(!block->has_track(*track))
- {
- if(&*block==&*last)
- return 0;
- block = block.next();
- }
+ BlockIter block = track.block_iter();
if(&*block==until_block)
return 0;
- if(back)
- block = block.reverse();
-
+ // Account for the vehicle's offset on its current track
float result = veh.get_offset();
- if(reverse!=back)
+ if(reverse)
track = track.reverse();
else
result = track->get_type().get_path_length(track->get_active_path())-result;
- result -= vtype.get_length()/2;
+ result -= veh.get_type().get_length()/2;
- while(1)
- {
- track = track.next();
- if(!track)
- break;
+ // Count remaining distance in the vehicle's current block
+ for(track=track.next(); &track->get_block()==&*block; track=track.next())
+ result += track->get_type().get_path_length(track->get_active_path());
- if(!block->has_track(*track))
- {
- if(back)
- {
- if(&*block==&*first)
- break;
- }
- else
- {
- if(&*block==&*last)
- break;
- }
- block = block.next();
+ const BlockIter &last = allocator.last();
+ if(&*block==&*last)
+ return result;
- if(&*block==until_block)
- break;
- }
+ // Count any remaining blocks
+ for(block=block.next(); (&*block!=until_block && block->get_train()==this); block=block.next())
+ {
+ result += block->get_path_length(block.entry());
- result += track->get_type().get_path_length(track->get_active_path());
+ if(&*block==&*last)
+ break;
}
return result;
add("name", &Loader::name);
add("quantized_speed", &Loader::quantized_speed);
add("router", &Loader::router);
- add("timetable", &Loader::timetable);
add("vehicle", &Loader::vehicle);
}
load_sub(*rtr);
}
-void Train::Loader::timetable()
-{
- Timetable *ttbl = new Timetable(obj);
- load_sub(*ttbl);
-}
-
void Train::Loader::vehicle(ArticleNumber art_nr)
{
const VehicleType &vtype = obj.layout.get_catalogue().get_vehicle(art_nr);