chiark / gitweb /
Reuse old plan for direction setting; report train location on prediction entry
authorian <ian>
Sun, 27 Jul 2008 14:27:12 +0000 (14:27 +0000)
committerian <ian>
Sun, 27 Jul 2008 14:27:12 +0000 (14:27 +0000)
hostside/safety.c

index 56f9f548730f15eec5f29234c7dbce9ea6a43753..14f4704399341cec9b72fbb3b3ba75bd73a80ff0 100644 (file)
@@ -305,6 +305,12 @@ static int calc_advanced(TrackAdvanceContext *c) {
   PredictUserContext *u= c->u;
   return u->was_distance - c->distance;
 }
+static void qprintf_position(Train *tra,
+   void (*qp)(const char *fmt,...) __attribute__((format(printf,1,2)))) {
+  qp("%s%s:%d+-%d",
+     tra->foredetect->tr_backwards?"-":"",
+     tra->foredetect->i->pname, tra->maxinto, tra->uncertainty);
+}
 
 #define SKELETON_PREDICT_USER_CONTEXT(u,tra)   \
   (memset(&(u),0,sizeof((u))),                 \
@@ -722,21 +728,24 @@ ErrorCode predict(Train *tra, struct timeval tnow, unsigned flags,
   u.desire_move= desire_move;
   u.desire_movposcomb= desire_movposcomb;
 
-  DPRINTF(safety,core," ***predicting*** %s%s %smaxspeed=%f%s stopdist=%d"
-         " (speed %f, step %d%s) flags=%c%c%s"
-         " desire=%s/%s\n",
-         tra->backwards?"-":"",tra->pname,
-         u.speed_info_specified ? "specified " : "",
-         u.maxspeed, u.stopping ? "(stopping)" : "",
-         u.stopping_distance,
-         tra->speed.speed, tra->speed.step,
-         tra->speed.decel.running ? " decel" : "",
-         "-j"[ !!(flags & PREDF_JUSTDET) ],
-         "no"[ !!(flags & PREDF_OLDPLAN) ],
-         u.stopping ? " stopping" : "",
-         u.desire_move ? u.desire_move->i->pname : "",
-         u.desire_move ? u.desire_move->i->poscombs[u.desire_movposcomb]
+  if (DEBUGP(safety,core)) {
+    DPRINTF(safety,core," ***predicting*** %s%s ",
+           tra->backwards?"-":"",tra->pname);
+    qprintf_position(tra,DPRINTFA);
+    DPRINTFA(" %smaxspeed=%f%s stopdist=%d (speed %f, step %d%s)"
+            " flags=%c%c%s desire=%s/%s\n",
+            u.speed_info_specified ? "specified " : "",
+            u.maxspeed, u.stopping ? "(stopping)" : "",
+            u.stopping_distance,
+            tra->speed.speed, tra->speed.step,
+            tra->speed.decel.running ? " decel" : "",
+            "-j"[ !!(flags & PREDF_JUSTDET) ],
+            "no"[ !!(flags & PREDF_OLDPLAN) ],
+            u.stopping ? " stopping" : "",
+            u.desire_move ? u.desire_move->i->pname : "",
+            u.desire_move ? u.desire_move->i->poscombs[u.desire_movposcomb]
                            .pname : "");
+  }
 
   FOR_SEG {
     if (!seg->owner || seg->owner == u.train)
@@ -989,11 +998,9 @@ ErrorCode predict(Train *tra, struct timeval tnow, unsigned flags,
 /*========== reporting position and ownership ==========*/
 
 void report_train_position(Train *tra) {
-  ouprintf("train %s at %s%s:%d+-%d %s\n",
-          tra->pname,
-          tra->foredetect->tr_backwards?"-":"",
-          tra->foredetect->i->pname, tra->maxinto, tra->uncertainty,
-          tra->backwards ? "backwards" : "forwards");
+  ouprintf("train %s at ",tra->pname);
+  qprintf_position(tra,ouprintf);
+  ouprintf(" %s\n", tra->backwards ? "backwards" : "forwards");
 }  
   
 static int report_getmovpos(TrackLocation *t, TrackAdvanceContext *c,
@@ -1112,7 +1119,7 @@ ErrorCode safety_setdirection(Train *tra, int backwards,
   PredictUserContext u;
   const SegPosCombInfo *pci;
   Segment *oldfdet;
-  int oldmaxinto, oldfdet_trbackwards;
+  int oldmaxinto, oldfdet_trbackwards, old_planlookaheadnsegs;
   ErrorCode ec, ec2;
   int r;
   struct timeval tnow;
@@ -1145,6 +1152,7 @@ ErrorCode safety_setdirection(Train *tra, int backwards,
   oldfdet= tra->foredetect;
   oldmaxinto= tra->maxinto;
   oldfdet_trbackwards= tra->foredetect->tr_backwards;
+  old_planlookaheadnsegs= tra->plan_lookahead_nsegs;
 
   r= trackloc_getlink(&newfdet,&c,&pci,0,-1);  assert(!r);
 
@@ -1152,8 +1160,9 @@ ErrorCode safety_setdirection(Train *tra, int backwards,
   tra->maxinto= pci->dist - newfdet.remain;
   tra->backwards ^= 1;
   newfdet.seg->tr_backwards ^= 1;
+  tra->plan_lookahead_nsegs= INT_MAX;
   
-  ec= predict(tra,tnow, 0, 0,0, 0,ppc,ppcu);
+  ec= predict(tra,tnow, PREDF_OLDPLAN, 0,0, 0,ppc,ppcu);
   if (!ec) {
     /* yay! */
     report_train_position(tra);
@@ -1168,6 +1177,7 @@ ErrorCode safety_setdirection(Train *tra, int backwards,
   tra->maxinto= oldmaxinto;
   tra->backwards ^= 1;
   tra->foredetect->tr_backwards= oldfdet_trbackwards;
+  tra->plan_lookahead_nsegs= old_planlookaheadnsegs;
 
   ec2= predict(tra,tnow, PREDF_OLDPLAN,0,0, 0,0,(char*)"abandon reverse");
   assert(!ec2);