X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibmarklin%2Faicontrol.cpp;h=553159c6a1c320d9e71d5abb1c0fbd9e042fb7b0;hb=9b05c573a38639827697fe393d55b7c76f5bde45;hp=b97b0374add45037a986ca047d7e9354c0d4a528;hpb=d88bc162df8b34f2ca966524222880033132da0f;p=r2c2.git diff --git a/source/libmarklin/aicontrol.cpp b/source/libmarklin/aicontrol.cpp index b97b037..553159c 100644 --- a/source/libmarklin/aicontrol.cpp +++ b/source/libmarklin/aicontrol.cpp @@ -15,20 +15,22 @@ using namespace Msp; namespace Marklin { -AIControl::AIControl(Train &t, ControlModel *n): +AIControl::AIControl(Train &t, Controller *n): train(t), - next_model(n), - target_speed(TrainControl::continuous("speed", -1000, 1000)), - blocked(false) + next_ctrl(n), + target_speed(Control::continuous("speed", 0, 1000)), + blocked(false), + approach(false) { target_speed.set(0); train.signal_arrived.connect(sigc::mem_fun(this, &AIControl::arrived)); + next_ctrl->signal_control_changed.connect(sigc::mem_fun(this, &AIControl::control_changed)); } AIControl::~AIControl() { - delete next_model; + delete next_ctrl; } void AIControl::set_control(const string &n, float v) @@ -40,58 +42,83 @@ void AIControl::set_control(const string &n, float v) target_speed.set(v); if(!blocked) - next_model->set_control("speed", target_speed.value); - - signal_control_changed.emit(n, target_speed.value); + { + float approach_speed = 5*train.get_layout().get_catalogue().get_scale(); + if(approach && target_speed.value>approach_speed) + next_ctrl->set_control("speed", approach_speed); + else + next_ctrl->set_control("speed", target_speed.value); + } + + signal_control_changed.emit(target_speed); } else - next_model->set_control(n, v); + next_ctrl->set_control(n, v); } -const TrainControl &AIControl::get_control(const string &n) const +const Controller::Control &AIControl::get_control(const string &n) const { if(n=="speed") return target_speed; else - return next_model->get_control(n); + return next_ctrl->get_control(n); } float AIControl::get_speed() const { - return next_model->get_speed(); + return next_ctrl->get_speed(); +} + +bool AIControl::get_reverse() const +{ + return next_ctrl->get_reverse(); } float AIControl::get_braking_distance() const { - return next_model->get_braking_distance(); + return next_ctrl->get_braking_distance(); } void AIControl::tick(const Time::TimeDelta &dt) { + float scale = train.get_layout().get_catalogue().get_scale(); float rsv_dist = train.get_reserved_distance(); - float brake_dist = next_model->get_braking_distance()*1.15; - float margin = 25*train.get_layout().get_catalogue().get_scale(); + float brake_dist = next_ctrl->get_braking_distance(); + float approach_margin = 50*scale; + float approach_speed = 5*scale; + float margin = 10*scale; + if(!blocked && rsv_distset_control("speed", 0); + next_ctrl->set_control("speed", 0); + } + else if((!approach && rsv_distbrake_dist+margin*2)) + { + blocked = false; + approach = true; + if(target_speed.value>approach_speed) + next_ctrl->set_control("speed", approach_speed); + else + next_ctrl->set_control("speed", target_speed.value); } - else if(blocked && rsv_dist>brake_dist+margin*3) + else if((blocked || approach) && rsv_dist>brake_dist*1.3+approach_margin*2) { blocked = false; - next_model->set_control("speed", target_speed.value); + approach = false; + next_ctrl->set_control("speed", target_speed.value); } - next_model->tick(dt); + next_ctrl->tick(dt); - if(!target_speed.value && !next_model->get_speed() && train.is_active()) + if(!target_speed.value && !next_ctrl->get_speed() && train.is_active()) train.set_active(false); } -void AIControl::control_changed(const string &n, float v) +void AIControl::control_changed(const Control &ctrl) { - if(n!="speed") - signal_control_changed.emit(n, v); + if(ctrl.name!="speed") + signal_control_changed.emit(ctrl); } void AIControl::arrived()