chiark / gitweb /
realtime: break out new function movpos.c:ouposn_feat
[trains.git] / hostside / movpos.c
index 632fbdee94e63d0feaaa89413bc794fadc8cbf10..b13326761792cb50868b7ebb44cdab775948ed25 100644 (file)
@@ -12,6 +12,35 @@ typedef struct {
 } Motion;
 
 typedef struct Method Method;
+typedef struct Change Change;
+
+typedef struct MovPosChange {
+  Segment *move;
+  /* everything beyond here is private for indep */
+  MovPosComb actual, target;
+  int refcount;
+  int n_changes;
+  Change *changes[];
+} Indep;
+
+static void method_update_feature(Method*, MovPosChange*, const Motion *mo);
+  /* Called from methods' execution logic when an individual feature
+   * has been moved.  This is used by the method-independent code to
+   * compute the correct delta set of movements from the current
+   * actual position, when thinking about new plans.  It is also sent
+   * to clients and ultimately used by the UI.
+   */
+
+static void method_change_done(Method *m, Change *chg);
+  /* Called from methods' execution logic when a whole change
+   * has been done.  The method-independent code will take care of
+   * updating move->movposcomb. etc.
+   *
+   * REENTRANCY: May be called from within a call to the method's
+   * execute().  Of course cannot legally be called from within
+   * prepare, consider, check or dispose.
+   */
+
 
 /* Kind-independent code is responsible for determining
  * the method, doing a bit of cleanup, and adjusting the flow
@@ -20,7 +49,7 @@ typedef struct Method Method;
  */
 /*
  * The following states exist for each Method
- *   I  Initialising corresponds to global states other than Sta_Run
+ *   S  Starting     corresponds to global states other than Sta_Run
  *   T  Tentative    changes have been made but may yet be undone
  *   Y  Yes          proposed changes have been checked and are OK
  *   E  Executing    the method is executing
@@ -29,7 +58,7 @@ typedef struct Method Method;
  * at points when control flow  passes between kind and indep:
  *   U  Unallocated  no memory allocated (Change does not exist)
  *   P  Prepared     memory allocation done, basics filled in
- *   Q  Queued       reservation or confirmation successful
+ *   I  Installed    reservation or confirmation successful
  *   D  Done         motion is complete and callback is being entered
  *   G  Garbage      motion is complete and callback is being exited
  *
@@ -40,55 +69,58 @@ typedef struct Method Method;
  * No Changes remain Prepared while the Method is Executing.
  */
 
-typedef struct Change Change;
-
 struct Change {                    /* valid in:  filled in by and when:      */
-  Method *meth;                    /*  PQ         indep after allocate()     */
-  MovPosChange *indep;             /*  PQ         indep after allocate()     */
+  Method *meth;                    /*  PIDG       indep after prepare()      */
+  MovPosChange *indep;             /*  PID        indep after prepare()      */
+  unsigned installed:1; /* private for indep */
   /* kind-specific data follows */ /*  varies     kind-specific code, varies */
-} Change;
-  /* `actual' contains the kind's public opinion about the physical
-   * state.  It is initialised by indep (just before confirm) from
-   * move->motion->actual or move->movposcomb as the case may be.  It
-   * should be updated by the kind, since it is used by indep for
-   * calculating the number and identities of the features which may
-   * need to change when a new move request is intended to replace an
-   * existing one - ie, the contents of the motions[] provided to a
-   * subsequent confirm().  So while a change is Confirmed, the
-   * physical state is recorded only in the relevant MovPosChange, and not
-   * in the segment's movposcomb.  Once a change goes to Confirmed,
-   * the indep code never untangles it so the kind can manage the
-   * proper transition.  */
+};
 
 struct Method {
   const char *pname;
-  MovFeatKind kind;
+
   ErrorCode (*prepare)(Method *m,                                 /* TYE->T */
-                      Segment *move,
+                      const Segment *move,
                       int n_motions, const Motion *motions,
-                      int ms, int confirming,
-                      Change *chg_r,           /* U->P; err: U->U; may be 0 */
+                      int ms, int confirmation,
+                      Change **chg_r,          /* 0->P; err: 0->0; may be 0 */
                       int *cost_r /* may be 0 */);
-  ErrorCode (*consider)(Method *m,                                 /* TYE->T */
-                       Change *remove,                   /* Q->P; err: Q->Q */
-                       Change *install);                 /* P->Q; err: P->P */
-      /* error: ET->T, no change to Changes */
   void (*dispose)(Method *m,                                       /* TY->TY */
                  Change*);                                          /* P->U */
 
+  ErrorCode (*install)(Method *m,                   /* TYE->T; err: TYE->TYY */
+                      Change *inst);                     /* P->I; err: P->P */
+      /* error: ET->T, no change to Changes */
+  void (*remove)(Method *m,                                      /* TYE->TYY */
+                Change *remove);                                    /* I->P */
+      /* error: ET->T, no change to Changes */
+
   ErrorCode (*check)(Method *m);                    /* TYE->Y; err: TYE->TYE */
   void (*execute)(Method *m);                                       /* EY->E */
-  void (*all_abandon)(Method *m);
+  void (*all_abandon)(Method *m);                                  /* TYE->S */
+
+  unsigned needcheck, needexec; /* used by indep to track T/Y/E */
 };
 
-const char *movpos_pname(Segment *move, MovPosComb poscomb) {
-  return poscomb<0 ? "?" : move->i->poscombs[poscomb].pname;
+/*========== general utility functions ==========*/
+
+const char *movpos_pname(const Segment *move, MovPosComb poscomb) {
+  return !SOMEP(poscomb) ? "?" : move->i->poscombs[poscomb].pname;
 }
 
-static void ouposn_moving(Change *chg) {
-  Segment *move= chg->move;
+static void ouposn_moving(const MovPosChange *indep) {
+  Segment *move= indep->move;
   ouprintf("movpos %s position %s moving\n",
-          move->i->pname, movpos_pname(move, chg->actual));
+          move->i->pname, movpos_pname(move, indep->actual));
+}
+static void ouposn_stable(const Segment *move) {
+  ouprintf("movpos %s position %s stable\n",
+          move->i->pname, movpos_pname(move, move->movposcomb));
+}
+static void ouposn_feat(const Segment *move, const MovFeatInfo *feati,
+                       MovPosComb posn, const Method *m) {
+  ouprintf("movpos %s feat %s %d %s\n", move->i->pname,
+          feati->pname, posn, m->pname);
 }
 
 MovPosComb movposcomb_update_feature(MovPosComb startpoint,
@@ -100,6 +132,10 @@ MovPosComb movposcomb_update_feature(MovPosComb startpoint,
   return above*above_weight + featpos*mfi->weight + below;
 }
 
+MovPosComb movpos_poscomb_actual(const Segment *seg) {
+  return seg->moving ? seg->motion->actual : seg->movposcomb;
+}
+
 static void ignore_all_abandon(Method *m) { }
 
 /*========== points and other fixed timeslot movfeats ==========*/
@@ -124,57 +160,15 @@ static void ignore_all_abandon(Method *m) { }
 typedef unsigned FsqSlot;
 typedef int FsqSlotSigned;
 
-/* We think there are three states: Allocated, Reserved and Confirmed.
- * (plus of course Unallocated where we don't have a request at all).
- * These correspond to the indep code as follows:
- *
- *  indep state   pt state    queues checked and plan viable
- *   Unallocated   n/a         yes
- *   Allocated     Allocated   yes
- *   Reserved      Reserved    yes
- *   Confirmed     Confirmed   yes
- *   Erroneous     A/R/C       no
- *
- * Erroneous exists only after a failed reserve() or confirm() so it's
- * not that confusing to have this slightly malleable terminology.
- */
-
 #define FSQDN (~(FsqSlot)0)
 
-typedef struct {     /* PR         QR            PC         QC       */
-        /* in queue?    absent     reserved      absent     confirmed */
-  Change h; 
-  FsqSlot deadline;  /* relative  relative       absolute   absolute     */
-  MovPosComb actual; /*            undef          undef     see below      */
-  int n_motions;     /*  1           1           alloc'd    num undone     */
-  Motion motions[];                                                        
-           /*  [0].i:     0          0            non-0     non-0 */
-           /*  .posn:     undef      undef        defined   defined        */
+typedef struct {   /* state      Prep Resv  Inst Resv  Prep Conf  Inst Conf   */
+  Change h;        /* in queue?  absent     reserved   absent     confirmed   */
+  FsqSlot deadline; /*           relative   relative   absolute   absolute    */
+  int n_motions;    /*            1          1         num undone num undone  */
+  Motion motions[]; /*  [0].i:    0          0         non-0      non-0       */
+                    /*  .posn:    undef      undef     defined    defined     */
 } FsqReq;
-  /* We can determine the the state by looking at the two
-   * `statedet' fields, marked <- above.
-   * There are also intermediate states where the req's
-   *  statedet fields do not agree with the queue it's on.
-   *  We write these as, for example,
-   *      AR   to mean  statedet says Allocated, but queued on fsq_reserved
-   *      A?   to mean  statedet says Allocated, but may be queued
-   *  etc.  They are only allowed while we are in a fsq_... method function.
-   */
-  /* FsqReq.actual is subtly differnet to MovPosChange.actual,
-   * as follows:
-   *                              in MovPosChange     in FsqReq
-   *  Position unknown              -1                  0
-   *  Position partly known         -1                  unknown feats are 0
-   *  Position completely known     exact               exact
-   *
-   * The partial knowledge positions can only occur in requests that
-   * are confirmed with as many motions as features, so we know that
-   * if we complete a request we know that we can copy actual out
-   * to MovPosChange.
-   *
-   * If we abandon a half-done change to a multi-feat segment
-   * we lose the partial knowledge.
-   */
 
 #define FSQ_MAX_QUEUE  15
 
@@ -241,8 +235,8 @@ static void fsq_queue_remove_item(FsqQueue *q, FsqReq *r) {
   fsq_queue_remove_index(q, i);
 }
 
-static FsqQueue *fsq_item_queue(FsqMethod *m, FsqReq *r) {
-  return r->motions[0].i ? &m->f.confirmed : &m->f.reserved;
+static FsqQueue *fsq_item_queue(FsqMethod *m, FsqReq *r)
+  { return r->motions[0].i ? &m->f.confirmed : &m->f.reserved; }
 
 #define WHICH(wh)                              \
   (whichr= wh##r,                              \
@@ -281,8 +275,8 @@ static ErrorCode fsq_check_plan(FsqMethod *m) {
       future++;     DPRINTF2("-");
       continue;
     }
-    DPRINTF2("%s/%s[%d@t+%d]", whichr->h.move->i->pname,
-           movpos_pname(whichr->h.move, whichr->h.intent),
+    DPRINTF2("%s/%s[%d@t+%d]", whichr->h.indep->move->i->pname,
+           movpos_pname(whichr->h.indep->move, whichr->h.indep->target),
            whichr->n_motions, whichwhen);
     if (future > whichwhen) {
       DPRINTF2("!...bad\n");
@@ -308,42 +302,48 @@ static ErrorCode fsq_queue_insert_item(FsqMethod *m, FsqQueue *q, FsqReq *r) {
     q->l[insat]= q->l[insat-1];
   q->l[insat]= r;
   q->n++;
+
+  return 0;
 }
 
-static void fsq_item_debug(FsqMethod *m, FsqReq *r,
-                          const char *pfx, Segment *move) {
-  DPRINTF1(movpos,fsq, "%s%s", pfx, move.pname);
+static void fsq_item_debug(FsqMethod *m, FsqReq *r, const char *opwhat) {
+  int DP;
+  Segment *move= r->h.indep->move;
+  DPRINTF1(movpos,fsq, "%s %s %s", m->m.pname, opwhat, move->i->pname);
   if (r->motions[0].i) {
-    for (int i=0, Motion *mo=r->motions; i<r->n_motions; i++, mo++)
-      DPRINTF1(movpos,fsq, "/%s%d", mo->i->pname, (int)mo->posn);
+    int i;
+    Motion *mo;
+    for (i=0, mo=r->motions; i<r->n_motions; i++, mo++)
+      DPRINTF2("/%s%d", mo->i->pname, (int)mo->posn);
+    DPRINTF2("\n");
   } else {
-    DPRINTF1(movpos,fsq, "(%d)", r->n_motions);
+    DPRINTF2("(%d)\n", r->n_motions);
   }
 }
  
-static ErrorCode fsq_enqueue(FsqMethod *m, FsqReq *r) { /* P->Q; err: P->P */
+static ErrorCode fsq_enqueue(FsqMethod *m, FsqReq *r) { /* P->I; err: P->P */
   if (!r) return 0;
-  fsq_item_debug(m,r,"enqueue:",r->h.indep.move);
-  return fsq_queue_insert_item(fsq_item_queue(m,r), r);
+  fsq_item_debug(m,r,"enqueue");
+  return fsq_queue_insert_item(m, fsq_item_queue(m,r), r);
 }
 
-static void fsq_dequeue(FsqMethod *m, FsqReq *r) { /* Q->P */
-  if (!r) return 0;
-  fsq_item_debug(m,r,"dequeue:",r->h.indep.move);
-  fsq_remove_item(fsq_item_queue(m,r), r);
+static void fsq_dequeue(FsqMethod *m, FsqReq *r) { /* I->P */
+  if (!r) return;
+  fsq_item_debug(m,r,"dequeue");
+  fsq_queue_remove_item(fsq_item_queue(m,r), r);
 }
 
 /*---------- method entrypoints ----------*/
 
-static ErrorCode fsq_prepare(Method *mm, Segment *move,
+static ErrorCode fsq_prepare(Method *mm, const Segment *move,
                             int n_motions, const Motion *motions,
                             int ms, int confirmation,
-                            Change *chg_r, int *cost_r) {
+                            Change **chg_r, int *cost_r) {
   FsqMethod *m= (void*)mm;
 
   assert(n_motions > 0);
 
-  FsqSlotSigned reldeadline= fsq_maxdelay_reldeadline(m, ms, r->n_motions);
+  FsqSlotSigned reldeadline= fsq_maxdelay_reldeadline(m, ms, n_motions);
   if (reldeadline <= 0) return EC_MovFeatTooLate;
 
   if (chg_r) {
@@ -354,7 +354,7 @@ static ErrorCode fsq_prepare(Method *mm, Segment *move,
     r->n_motions= n_motions;
     if (confirmation) {
       r->deadline= reldeadline + m->f.cslot;
-      memcpy(r->motions, motions, sizeof(*r->motions)*motions);
+      memcpy(r->motions, motions, sizeof(*r->motions)*n_motions);
     } else {
       r->deadline= reldeadline;
       r->motions[0].i= 0;
@@ -362,48 +362,42 @@ static ErrorCode fsq_prepare(Method *mm, Segment *move,
     *chg_r= &r->h;
   }
   if (cost_r) {
-    *cost_r= n_motions * m->slot_ms;
+    *cost_r= n_motions * m->f.slot_ms;
   }
 
   return 0;
 }
 
-static ErrorCode fsq_consider(Method *mm, Change *remove, Change *install) {
-  FsqMethod *m= (void*)mm;
-  FsqReq *remv= (FsqReq*)remove;
-  FsqReq *inst= (FsqReq*)install;
+static void fsq_dispose(Method *mm, Change *chg) {
+  FsqReq *r= (FsqReq*)chg;
+  free(r);
+}
 
-  DPRINTF1(movpos,fsq, "%s consider ", m->m.pname);
+static void fsq_remove(Method *mm, Change *remvchg) {
+  FsqMethod *m= (void*)mm;
+  FsqReq *remv= (FsqReq*)remvchg;
 
   fsq_dequeue(m, remv);
-  ErrorCode ec= fsq_enqueue(m, inst);
-  if (ec) {
-    ErrorCode ec2= fsq_enqueue(m, remv);
-    assert(!ec2);
-  }
-  DPRINTF1(movpos,fsq, " %s\n", ec2str(ec));
-  return ec;
+}
+
+static ErrorCode fsq_install(Method *mm, Change *instchg) {
+  FsqMethod *m= (void*)mm;
+  FsqReq *inst= (FsqReq*)instchg;
+
+  return fsq_enqueue(m, inst);
 }
 
 static ErrorCode fsq_check(Method *mm) { 
   FsqMethod *m= (void*)mm;
   ErrorCode ec= fsq_check_plan(m);
-  DPRINTF(movpos,fsq, "%s check=%s\n", m->m.pname, ec2str(ec));
   return ec;
 }
 
 static void fsq_execute(Method *mm) {
   FsqMethod *m= (void*)mm;
-  DPRINTF(movpos,fsq, "%s execute\n", m->m.pname);
   fsq_check_action(m);
 }
 
-static void fsq_dispose(Method *mm, Change *chg) {
-  FsqMethod *m= (void*)mm;
-  FsqReq *r= (FsqReq*)chg;
-  free(r);
-}
-
 /*---------- something to do ? ----------*/
 
 static void fsq_check_action(FsqMethod *m) {
@@ -425,13 +419,13 @@ static void fsq_check_action(FsqMethod *m) {
     m->f.move(m, mo->i, mo->posn);
     m->f.cslot++;
 
-    indep_update_feature(&m->m, r->h.indep, mo);
+    method_update_feature(&m->m, r->h.indep, mo);
   }
 
   if (!r->n_motions) {
     fsq_queue_remove_index(&m->f.confirmed, 0);
-    indep_change_done(&m->m, &r->h);
-    m->h.dispose(&m->m, &r->h);
+    method_change_done(&m->m, &r->h);
+    m->m.dispose(&m->m, &r->h);
 
     ec= fsq_check_plan(m);  assert(!ec);
     fsq_check_action(m);
@@ -473,10 +467,10 @@ static void fsq_check_action(FsqMethod *m) {
 #define CDU_RECHARGE   350 /*ms*/
 #define POINT_MOVEMENT  50 /*ms*/
 
-static ErrorCode point_prepare(Method *mm, Segment *move,
+static ErrorCode point_prepare(Method *mm, const Segment *move,
                               int n_motions, const Motion *motions,
                               int ms, int confirmation,
-                              Change *chg_r, int *cost_r) {
+                              Change **chg_r, int *cost_r) {
   FsqMethod *m= (void*)mm;
   assert(m->f.ready>=0);
   return fsq_prepare(mm,move, n_motions,motions,
@@ -509,8 +503,9 @@ void on_pic_charged(const PicInsnInfo *pii, const PicInsn *pi, int objnum) {
 }
 
 static FsqMethod points_method= {
-  { "point", mfk_point,
-    point_prepare, fsq_consider, fsq_dispose,
+  { "point",
+    point_prepare, fsq_dispose,
+    fsq_install, fsq_remove,
     fsq_check, fsq_execute, points_all_abandon },
   { .lag_ms= POINT_MOVEMENT, .slot_ms= CDU_RECHARGE, .move= point_move }
 };
@@ -588,7 +583,7 @@ void waggle_settle(void) {
   waggle.f.ready= 1;
   waggle_settle_check();
 }
-  
+
 void on_pic_waggled(const PicInsnInfo *pii, const PicInsn *pi, int objnum) {
   if (sta_state == Sta_Settling) {
     waggle.f.ready= 1;
@@ -601,74 +596,127 @@ void on_pic_waggled(const PicInsnInfo *pii, const PicInsn *pi, int objnum) {
 }
 
 static FsqMethod waggle= {
-  { "relay", mfk_relay,
-    fsq_prepare, fsq_consider, fsq_dispose,
+  { "relay",
+    fsq_prepare, fsq_dispose,
+    fsq_install, fsq_remove,
     fsq_check, fsq_execute, ignore_all_abandon },
   { .lag_ms= 5, .slot_ms= 50, .move= waggle_do }
 };
 
 /*========== dummy `nomove' kind ==========*/
 
-static ErrorCode nomove_prepare(Method *m, Segment *move,
+typedef struct NomoveChange {
+  Change h;
+  DLIST_NODE(struct NomoveChange) inqueue;
+  int n_motions; /* 0 for reservations */
+  Motion motions[];
+} NomoveChange;
+
+typedef struct {
+  Method m;
+  NomoveChange *queuehead; /* contains confirmations only */
+} NomoveMethod;
+
+static ErrorCode nomove_prepare(Method *meth_in, const Segment *move,
                                int n_motions, const Motion *motions,
                                int ms, int confirming,
-                               Change *chg_r, int *cost_r) {
+                               Change **chg_r, int *cost_r) {
   if (chg_r) {
-    *chg= mmalloc(sizeof(Change));
-    fixme
+    NomoveChange *chg;
+    assert(n_motions>0);
+    if (!confirming) n_motions= 0;
+    chg= mmalloc(sizeof(*chg) + sizeof(Motion)*n_motions);
+    chg->n_motions= n_motions;
+    memcpy(chg->motions, motions, sizeof(Motion)*n_motions);
   }
   if (cost_r) {
     *cost_r= 0;
   }
   return 0;
 }
+static void nomove_dispose(Method *mm, Change *remvchg) {
+  NomoveChange *remv= (void*)remvchg;
+  free(remv);
+}
+
+static ErrorCode nomove_install(Method *mm, Change *instchg) {
+  NomoveMethod *meth= (void*)mm;
+  NomoveChange *inst= (void*)instchg;
+  if (inst->n_motions)
+    DLIST1_PREPEND(meth->queuehead, inst, inqueue);
+  return 0;
+}
+static void nomove_remove(Method *mm, Change *remvchg) {
+  NomoveMethod *meth= (void*)mm;
+  NomoveChange *remv= (void*)remvchg;
+  if (remv->n_motions)
+    DLIST1_REMOVE(meth->queuehead, remv, inqueue);
+}
+
+static ErrorCode nomove_check(Method *mm) { return 0; }
 
-static ErrorCode nomove_consider(Method *m, Change *remove, Change *install)
-  { return 0; }
+static void nomove_execute(Method *mm) {
+  NomoveMethod *meth= (void*)mm;
+  NomoveChange *done;
 
-static void nomove_dispose(Method *m, Change *chg) { }
-static ErrorCode nomove_check(Method *m) { return 0; }
-static ErrorCode nomove_execute(Method *m) {
-  fixme
+  while ((done= meth->queuehead)) {
+    assert(done->n_motions);
+    int i;
+    for (i=0; i<done->n_motions; i++)
+      method_update_feature(&meth->m, done->h.indep, &done->motions[i]);
+    method_change_done(&meth->m, &done->h);
+    DLIST1_REMOVE(meth->queuehead, done, inqueue);
+    nomove_dispose(&meth->m, &done->h);
+  }
 }
+
 static Method nomove_method= {
-  "nomove", mfk_none,
-  nomove_prepare, nomove_consider, nomove_dispose,
+  "nomove",
+  nomove_prepare, nomove_dispose,
+  nomove_install, nomove_remove,
   nomove_check, nomove_execute, ignore_all_abandon
 };
 
 /*========== method-independent machinery ==========*/
 
-struct MovPosChange {
-  Segment *move;
-  MovPosComb start;
-  MovPosComb actual;
-  int n_changes;
-  Change *changes[];
-};
+#define INDEP_DBG_FMT "<%p:%s/%s[%d]>"
+#define INDEP_DBG_ARGS(in) (in),                                       \
+    ((in)->move->i->pname), (movpos_pname((in)->move, (in)->target)),  \
+    ((in)->n_changes)
+
+#define INDEP_DPFX_FMT "movpos " INDEP_DBG_FMT " "
+#define INDEP_DPFX_ARGS(in) INDEP_DBG_ARGS((in))
+
+#define METH_DPFX_FMT "%s " INDEP_DBG_FMT " "
+#define METH_DPFX_ARGS(indep, meth) ((meth).pname), INDEP_DBG_ARGS((indep))
+
+static void indep_indep_done(Indep *indep);
 
 static Method *methods[]= {
-  &nomove_method,
-  (Method*)&points_method,
-  (Method*)&waggle,
+  [mfk_none] = (Method*)&nomove_method,
+  [mfk_point] = (Method*)&points_method,
+  [mfk_relay] = (Method*)&waggle,
   0
 };
 
-static void indep_update_feature(Method *m, MovPosChange *indep,
-                                const Motion *mo) {
-  if (SOMEP(indep->start))
-    ouprintf("movpos %s feat %s %d %s\n", indep->move->i->pname,
-            mo->i->pname, mo->posn, m.pname);
-  indep->actual=
-    movposcomb_update_feature(indep->actual, mo->i, mo->posn);
+/*---------- entrypoints from methods ----------*/
+
+static void method_update_feature(Method *m, MovPosChange *indep,
+                                 const Motion *mo) {
+  ouposn_feat(indep->move, mo->i, mo->posn, m);
+  if (SOMEP(indep->actual))
+    indep->actual=
+      movposcomb_update_feature(indep->actual, mo->i, mo->posn);
+  ouposn_moving(indep);
 }
 
-static void indep_change_done(Method *m, Change *chg) {
-  MovPosComb *indep= chg->indep;
+static void method_change_done(Method *m, Change *chg) {
+  Indep *indep= chg->indep;
   Change **search;
 
+  DPRINTF(movpos,meth, METH_DPFX_FMT "method_change_done...\n",
+         METH_DPFX_ARGS(indep,*m));
+
   for (search=indep->changes; *search; search++)
     if ((*search) == chg) goto found;
   assert(!"change in indep");
@@ -676,404 +724,444 @@ static void indep_change_done(Method *m, Change *chg) {
  found:
   indep->n_changes--;
   if (indep->n_changes) {
-    *search= indep[n_changes];
+    *search= indep->changes[indep->n_changes];
     return;
   }
 
+  if (!indep->refcount)
+    indep_indep_done(indep);
+}
+
+static void indep_indep_done(Indep *indep) {
   /* all done */
+  Segment *move= indep->move;
   move->moving= 0;
   move->motion= 0;
-  move->movposcomb= indep->actual;
-  ouprintf("movpos %s position %s stable\n",
-          move->i->pname, movpos_pname(move, move->movposcomb));
+  move->movposcomb= indep->target;
+  ouposn_stable(move);
   free(indep);
 }
 
-static ErrorCode evaluate_target(Segment *move, MovPosComb target,
-                                MovPosComb startpoint,
-                                int *n_changes_r,
-                                Change *changes_r[N_METHODS],
-                                int *cost_r) {
+/*---------- internal core machinery ----------*/
+
+static Method *feature_method(const MovFeatInfo *feati) {
+  assert(feati->kind >= 0);
+  assert(feati->kind < ARRAY_SIZE(methods));
+  Method *meth= methods[feati->kind];
+  assert(meth);
+  return meth;
+}
+
+static int change_needed(const MovFeatInfo *feati,
+                        MovPosComb startpoint, MovPosComb target) {
+  int r;
+  r= !SOMEP(startpoint) ||
+    (target / feati->weight) % feati->posns -
+    (startpoint / feati->weight) % feati->posns;
+  if (DEBUGP(movpos,eval))
+    DPRINTFA(" { %s:%s(%d*%d) %d..%d => %d }",
+            feature_method(feati)->pname, feati->pname,
+            feati->posns, feati->weight,
+            startpoint, target, r);
+  return r;
+}
+
+static void indep_dispose(MovPosChange *indep) {
+  if (!indep) return;
+
+  DPRINTF(movpos,intern, INDEP_DPFX_FMT "dispose...\n",
+         INDEP_DPFX_ARGS(indep));
+
+  int changei;
+  for (changei=0; changei<indep->n_changes; changei++) {
+      Change *chg= indep->changes[changei];
+      if (!chg) continue;
+
+      Method *meth= chg->meth;
+      DPRINTF(movpos,meth, METH_DPFX_FMT "dispose...\n",
+             METH_DPFX_ARGS(indep,*meth));
+      meth->dispose(meth, chg);
+  }
+  free(indep);
+}
+
+#define EVAL_MAX_METHODS 2
+#define EVAL_MAX_MOTIONS 2
+
+static ErrorCode indep_prepare(Segment *move, MovPosComb target,
+                              MovPosComb startpoint,
+                              int ms, int confirming,
+                              MovPosChange **indep_r /* 0 ok */,
+                              int *cost_r /* 0 ok */) {
+  static int n_meths;
+  static Method *meths[EVAL_MAX_METHODS];
+  static int n_motions[EVAL_MAX_METHODS];
+  static Motion motions[EVAL_MAX_METHODS][EVAL_MAX_MOTIONS];
+
   const SegmentInfo *movei= move->i;
-  int feat;
-  const MovFeatInfo *feati;
-  MovFeatKind kind;
-  int n_changes;
-  Method *change_methods[N_METHODS];
-  Motion *change_motions[N_METHODS];
-  int i;
+  int feat, DP;
+
+  MovPosChange *indep=0;
 
-  DPRINTF(movpos,eval, "%s/%s <-%s\n", move->i->pname,
-         movpos_pname(move,target), movpos_pname(move,startpoint));
+  DPRINTF1(movpos,eval, "movpos prepare %s/%s <-%s", move->i->pname,
+          movpos_pname(move,target), movpos_pname(move,startpoint));
 
   if (!SOMEP(startpoint)) {
     startpoint= movpos_poscomb_actual(move);
-    DPRINTF(movpos,eval, "  actual <-%s\n",
-           movpos_pname(move,startpoint));
+    DPRINTF2(" actual <-%s", movpos_pname(move,startpoint));
   }
 
-  n_changes= 0;
+  n_meths= 0;
 
-  for (feat=0, feati=movei->movfeats, tchanges=0, kind= mfk_none;
-       feat<movei->n_movfeats;
-       feat++, feati++) {
+  for (feat=0; feat<movei->n_movfeats; feat++) {
+    const MovFeatInfo *feati= &movei->movfeats[feat];
     if (!change_needed(feati,startpoint,target)) continue;
-    for (i=0; i<n_changes; i++)
-      
-
-    tchanges++;
-    if (kind && feati->kind != kind) return -1;
-    kind= feati->kind;
+    MovPosComb posn= target / feati->weight % feati->posns;
+    Method *meth= feature_method(feati);
+
+    int methi;
+    for (methi=0; methi<n_meths; methi++)
+      if (meths[methi] == meth) goto found_method;
+    /* need a new method */
+    methi= n_meths++;
+    if (methi >= EVAL_MAX_METHODS) {
+      DPRINTF2(" MovFeatTooManyMethods methi=%d\n",methi);
+      return EC_MovFeatTooManyMethods;
+    }
+    meths[methi]= meth;
+    n_motions[methi]= 0;
+    DPRINTF2(" meths[%d]=%s", methi,meth->pname);
+
+  found_method:;
+    int motioni= n_motions[methi]++;
+    if (motioni >= EVAL_MAX_MOTIONS) {
+      DPRINTF2(" MovFeatTooManyMotions motioni=%d\n",motioni);
+      return EC_MovFeatTooManyMotions;
+    }
+    DPRINTF2(" motion[%d][%d]=%s%d", methi, motioni, feati->pname,posn);
+    motions[methi][motioni].i= feati;
+    motions[methi][motioni].posn= posn;
   }
 
-  if (kind_r) *kind_r= kind;
-  DPRINTF(movpos,eval, "changes=%d kind=%s\n",
-         tchanges, methods[kind]->pname);
-  return tchanges;
-}
+  if (indep_r) {
+    DPRINTF2(" alloc");
+    indep= mmalloc(sizeof(*indep) + sizeof(Change*) * n_meths);
+    indep->move= move;
+    indep->actual= startpoint;
+    indep->target= target;
+    indep->n_changes= n_meths;
+    indep->refcount= 0;
+    memset(indep->changes, 0, sizeof(Change*) * n_meths);
+  }
+  DPRINTF2("\n");
 
+  int totalcost= 0;
+  int changei;
+  ErrorCode ec;
 
+  for (changei=0; changei<n_meths; changei++) {
+    Method *meth= meths[changei];
+    int thiscost= 0;
+    meth->needcheck= 1;
+    meth->needexec= 1;
+
+    if (indep_r)
+      DPRINTF(movpos,meth, METH_DPFX_FMT "prepare n_motions=%d...\n",
+             METH_DPFX_ARGS(indep,*meth), n_motions[changei]);
+    else
+      DPRINTF(movpos,meth, "%s prepare (costing) n_motions=%d...\n",
+             meth->pname, n_motions[changei]);
+
+    ec= meth->prepare(meth,move,
+                     n_motions[changei],motions[changei],
+                     ms, confirming,
+                     indep ? &indep->changes[changei] : 0,
+                     &thiscost);
+    if (ec) goto x;
+    if (indep) {
+      Change *chg= indep->changes[changei];
+      chg->meth= meth;
+      chg->indep= indep;
+      chg->installed= 0;
+    }
+    totalcost += thiscost;
+  }
 
-ErrorCode
-movpos_reserve(Segment *move, int maxdelay_ms, MovPosChange **res_r,
-              MovPosComb target, MovPosComb startpoint /*as for findcomb*/) {
-  MovFeatKind kind= mfk_none;
-  ErrorCode ec;
-  int nchanges;
+  if (indep_r) *indep_r= indep;
+  if (cost_r) *cost_r= totalcost;
 
-  int n_changes;
-  Change *changes[N_METHODS];
+  if (indep_r)
+    DPRINTF(movpos,eval, INDEP_DPFX_FMT "prepare cost=%d ok\n",
+           INDEP_DPFX_ARGS(indep), totalcost);
+  else
+    DPRINTF(movpos,eval, "movpos prepare %s/%s cost=%d ok\n",
+           move->i->pname, movpos_pname(move,target), totalcost);
+  return 0;
 
-  DPRINTF(movpos,reserve, "%s/%s maxdelay=%dms startpoint=%s\n",
-         move->i->pname, movpos_pname(move,target),
-         maxdelay_ms, movpos_pname(move,startpoint));
+ x:
+  indep_dispose(indep);
+  DPRINTF(movpos,entry, "movpos prepare %s/%s err=%s\n", move->i->pname,
+         movpos_pname(move,target), ec2str(ec));
+  return ec;
+}
 
-  evaluate_target(move,target,startpoint, &n_changes,&changes);
+static void indep_remove(MovPosChange *remv) {
+  if (!remv) return;
   
+  DPRINTF(movpos,intern, INDEP_DPFX_FMT "remove...\n",
+         INDEP_DPFX_ARGS(remv));
 
-  if (nchanges==-1) return EC_MovFeatKindsCombination;
+  int i;
+  for (i=0; i<remv->n_changes; i++) {
+    Change *chg= remv->changes[i];
+    if (!chg->installed) continue;
+    Method *meth= chg->meth;
+    meth->needexec= 1;
+
+    DPRINTF(movpos,meth, METH_DPFX_FMT "remove...\n",
+           METH_DPFX_ARGS(remv,*meth));
+    meth->remove(meth, chg);
+    chg->installed= 0;
+  }
+}
 
-  Method *meth= methods[kind];
-  DPRINTF(movpos,reserve, "allocate %s:%d...\n",
-         meth->pname, nchanges);
-  Change *chg= mp_allocate(meth, move, nchanges, target);
-  ec= meth->reserve(meth, chg, move, maxdelay_ms);
-  DPRINTF(movpos,reserve, "reserve => %s\n",errorcodelist[ec]);
-  if (ec) goto x;
+static ErrorCode
+indep_install(MovPosChange *inst, int checknow) {
+  /* if this fails, inst may be left partially installed */
+  if (!inst) return 0;
+  
+  DPRINTF(movpos,intern, INDEP_DPFX_FMT "install checknow=%d...\n",
+         INDEP_DPFX_ARGS(inst), checknow);
 
-  *res_r= chg;
+  int i;
+  ErrorCode ec;
+  for (i=0; i<inst->n_changes; i++) {
+    Change *chg= inst->changes[i];
+    assert(!chg->installed);
+    Method *meth= chg->meth;
+
+    meth->needexec= 1;
+    ec= meth->install(meth, chg);
+    DPRINTF(movpos,meth, METH_DPFX_FMT "install=%s\n",
+           METH_DPFX_ARGS(inst,*meth), ec2str(ec));
+    if (ec) goto x;
+    chg->installed= 1;
+    meth->needcheck= 1;
+
+    if (checknow) {
+      ec= meth->check(meth);
+      DPRINTF(movpos,meth, METH_DPFX_FMT "check=%s\n",
+             METH_DPFX_ARGS(inst,*meth), ec2str(ec));
+      if (ec) goto x;
+      meth->needcheck= 0;
+    }
+  }
   return 0;
 
  x:
-  movpos_unreserve(chg);
   return ec;
 }
 
+static void indep_check_execute(void) {
+  DPRINTF(movpos,intern, "movpos indep_check_execute\n");
 
+  Method **methwalk, *meth;
+  for (methwalk= methods; (meth= *methwalk); methwalk++) {
+    if (meth->needcheck) {
+      ErrorCode ec= meth->check(meth);
+      DPRINTF(movpos,meth, "%s check=%s\n", meth->pname, ec2str(ec));
+      assert(!ec);
+      meth->needcheck= 0;
+    }
+    if (meth->needexec) {
+      meth->needexec= 0;
+      DPRINTF(movpos,meth, "%s execute...\n", meth->pname);
+      meth->execute(meth);
+    }
+  }
+}
 
+/*---------- entrypoints from rest of program ----------*/
 
+ErrorCode
+movpos_reserve(Segment *move, int maxdelay_ms, MovPosChange **res_r,
+              MovPosComb target, MovPosComb startpoint /*as for findcomb*/) {
+  ErrorCode ec;
+  MovPosChange *indep= 0;
 
-static Change *mp_allocate(Method *meth, Segment *move,
-                          int alloc_motions, MovPosComb target) {
-  assert(sta_state >= Sta_Resolving || sta_state == Sta_Manual);
-  Change *chg= meth->allocate(meth, alloc_motions);
-  chg->meth=      meth;
-  chg->move=      move;
-  chg->intent=    target;
-  return chg;
-}
+  DPRINTF(movpos,entry, "movpos reserve %s/%s maxdelay=%dms startpoint=%s\n",
+         move->i->pname, movpos_pname(move,target),
+         maxdelay_ms, movpos_pname(move,startpoint));
 
-static int change_needed(const MovFeatInfo *feati,
-                        MovPosComb startpoint, MovPosComb target) {
-  int r;
-  r= startpoint<0 ||
-    (target - startpoint) / feati->weight % feati->posns;
-  DPRINTF(movpos,changeneeded, "%s:%s(%d*%d) %d..%d => %d\n",
-         methods[feati->kind]->pname, feati->pname,
-         feati->posns, feati->weight,
-         startpoint, target, r);
-  return r;
-}  
+  ec= indep_prepare(move,target, startpoint,
+                   maxdelay_ms,0,
+                   &indep, 0);
+  if (ec) return ec;
+
+  ec= indep_install(indep, 1);
+  if (ec) goto x;
+
+  indep_check_execute();
+
+  DPRINTF(movpos,entry, "movpos reserve %s/%s ok\n",
+         move->i->pname, movpos_pname(move,target));
+  *res_r= indep;
+  return 0;
+
+ x:
+  indep_remove(indep);
+  indep_dispose(indep);
+  indep_check_execute();
+
+  DPRINTF(movpos,entry, "movpos reserve %s/%s err=%s\n",
+         move->i->pname, movpos_pname(move,target), ec2str(ec));
+  return ec;
+}
 
 ErrorCode movpos_findcomb_bysegs(Segment *back, Segment *move, Segment *fwd,
                                 MovPosComb startpoint, MovPosComb *chosen_r) {
   const SegmentInfo *movei= move->i;
   MovPosComb tcomb, bestcomb=-1;
-  int tchanges, bestchanges=INT_MAX;
+  int tcost, bestcost=INT_MAX;
   const SegPosCombInfo *pci;
 
+  DPRINTF(movpos,eval, "movpos_findcomb_bysegs %s-%s-%s <-%s\n",
+         back ? back->i->pname : "*", move->i->pname,
+         fwd  ? fwd ->i->pname : "*", movpos_pname(move, startpoint));
+
   for (tcomb=0, pci=movei->poscombs;
        tcomb<movei->n_poscombs;
        tcomb++, pci++) {
+    /* these next assignments may generate segments[-1] but we don't
+     * care because that won't compare equal to back or fwd */
     Segment *tback= &segments[pci->link[1].next];
     Segment *tfwd=  &segments[pci->link[0].next];
+
+    DPRINTF(movpos,intern, "movpos_findcomb_bysegs  ... %s : %s-%s-%s\n",
+           movpos_pname(move, tcomb),
+           SOMEP(pci->link[1].next) ? tback->i->pname : "#",
+           move->i->pname,
+           SOMEP(pci->link[0].next) ? tfwd->i->pname : "#");       
+
     if (back && !(back==tback || back==tfwd)) continue;
     if (fwd  && !(fwd ==tback || fwd ==tfwd)) continue;
 
     /* we have to search for the one which is least effort */
-    tchanges= evaluate_target(move,tcomb,startpoint,0);
-
-    if (tchanges==-1)
-      /* mixed kinds */
-      tchanges= INT_MAX-1;
+    ErrorCode ec= indep_prepare(move,tcomb,startpoint, -1,0, 0,&tcost);
+    if (ec) return ec;
 
-    if (tchanges >= bestchanges) /* prefer low-numbered movposcombs */
+    if (tcost >= bestcost) /* prefer low-numbered movposcombs */
       continue;
 
     bestcomb= tcomb;
-    bestchanges= tchanges;
+    bestcost= tcost;
   }
+  DPRINTF(movpos,entry, "movpos_findcomb_bysegs %s..%s..%s <-%s => %s/%s\n",
+         back ? back->i->pname : "-", move->i->pname,
+         fwd  ? fwd ->i->pname : "-", movpos_pname(move, startpoint),
+         move->i->pname, movpos_pname(move,bestcomb));
+
   if (chosen_r) *chosen_r= bestcomb;
   return
-    bestchanges==INT_MAX ? EC_MovFeatRouteNotFound :
-    bestchanges==INT_MAX-1 ? EC_MovFeatKindsCombination :
+    bestcost==INT_MAX ? EC_MovFeatRouteNotFound :
     0;
 }
 
 ErrorCode movpos_change(Segment *move, MovPosComb target,
-                       int maxdelay_ms, MovPosChange *chg) {
-  const SegmentInfo *movei= move->i;
-  const MovFeatInfo *feati;
-  int feat;
+                       int maxdelay_ms, MovPosChange *resv) {
+  int DP;
   MovPosComb actual;
   ErrorCode ec;
-  MovFeatKind kind= mfk_none;
 
   if (!move->moving) {
     actual= move->movposcomb;
     assert(!move->motion);
   } else {
-    kind= move->motion->meth->kind;
     actual= move->motion->actual;
   }
 
-  DPRINTF(movpos,change, "%s/%s maxdelay=%dms actual=%s\n",
-         move->i->pname, movpos_pname(move,target),
-         maxdelay_ms, movpos_pname(move, actual));
-  if (chg) DPRINTF(movpos,change, " chg=%s:%s/%s\n",
-                  chg->meth->pname, chg->move->i->pname,
-                  movpos_pname(chg->move, chg->intent));
-
-  { /* provide horizon for visibility of motions[] */
-    int n_motions=0;
-    Motion motions[movei->n_movfeats];
-
-    for (feat=0, feati=movei->movfeats;
-        feat<movei->n_movfeats;
-        feat++, feati++) {
-      if (!change_needed(feati,actual,target))
-       continue;
-      MovPosComb posn= target / feati->weight % feati->posns;
-      if (kind) {
-       if (feati->kind != kind) { ec= EC_MovFeatKindsCombination; goto x; }
-      } else {
-       kind= feati->kind;
-      }
-      motions[n_motions].i= feati;
-      motions[n_motions].posn= posn;
-      n_motions++;
-    }
+  DPRINTF1(movpos,entry, "movpos change %s/%s maxdelay=%dms actual=%s",
+          move->i->pname, movpos_pname(move, target),
+          maxdelay_ms, movpos_pname(move, actual));
+  if (resv) DPRINTF2(" resv=%s/%s",
+                    resv->move->i->pname,
+                    movpos_pname(resv->move, resv->target));
+  if (move->motion) DPRINTF2(" oldmotion=/%s",
+                            movpos_pname(move, move->motion->target));
+  DPRINTF2("\n");
+
+  MovPosChange *inst= 0;
+
+  ec= indep_prepare(move,target, actual,
+                   maxdelay_ms,1,
+                   &inst, 0);
+  if (ec) goto x;
 
-    Method *meth= methods[kind];
+  indep_remove(resv);
+  indep_remove(move->motion);;
 
-    if (chg) {
-      if (chg->meth != meth ||
-         chg->move != move ||
-         chg->intent != target)
-       return EC_MovFeatReservationInapplicable;
-    } else {
-      chg= mp_allocate(meth,move,n_motions,target);
-    }
-    chg->actual= actual;
+  ec= indep_install(inst, 1);
+  if (ec) goto x;
 
-    DPRINTF(movpos,change, "confirm %s:%d...\n",
-           meth->pname, n_motions);
-    ec= meth->confirm(meth, chg, move, n_motions, motions, maxdelay_ms);
-    DPRINTF(movpos,change, "confirm => %s\n",errorcodelist[ec]);
-    if (ec) goto x;
-  }
+  indep_dispose(resv);
+  indep_dispose(move->motion);
+
+  move->motion= inst;
+  move->moving= 1;
+
+  inst->refcount++; /* prevents method_change_done from destroying it */
+
+  ouposn_moving(inst);
+  indep_check_execute();
+
+  inst->refcount--;
+  if (!inst->n_changes)
+    /* oh! */
+    indep_indep_done(inst);
+
+  DPRINTF(movpos,entry, "movpos change %s/%s ok\n",
+         move->i->pname, movpos_pname(move, target));
   return 0;
 
  x:
-  movpos_unreserve(chg);
+  indep_remove(inst);
+  indep_dispose(inst);
+  ec= indep_install(move->motion, 0);  assert(!ec);
+  ec= indep_install(resv, 0);          assert(!ec);
+  indep_check_execute();
+
+  DPRINTF(movpos,entry, "movpos change %s/%s err=%s\n",
+         move->i->pname, movpos_pname(move, target), ec2str(ec));
   return ec;
 }
 
-void movpos_unreserve(MovPosChange *res) {
-  if (!res) return;
-  DPRINTF(movpos,unreserve, "%s:%s/%s\n",
-         res->meth->pname, res->move->i->pname,
-         movpos_pname(res->move, res->intent));
-  res->meth->destroy(res->meth, res);
-}
-
-MovPosComb movpos_poscomb_actual(Segment *seg) {
- return seg->moving ? seg->motion->actual : seg->movposcomb;
+void movpos_unreserve(MovPosChange *resv) {
+  if (!resv) return;
+  DPRINTF(movpos,entry, "movpos unreserve %s/%s...\n",
+         resv->move->i->pname, movpos_pname(resv->move, resv->target));
+  indep_remove(resv);
+  indep_dispose(resv);
+  indep_check_execute();
 }
 
-MovPosComb movpos_change_intent(MovPosChange *chg) {
-  return chg->intent;
+MovPosComb movpos_change_intent(MovPosChange *indep) {
+  return indep->target;
 }
 
 void motions_all_abandon(void) {
   Method **meth;
+  SEG_IV;
+  DPRINTF(movpos,entry, "movpos motions_all_abandon...\n");
+  FOR_SEG {
+    if (!seg->moving) continue;
+
+    MovPosChange *abandon= seg->motion;
+    indep_remove(abandon);
+    seg->movposcomb= abandon->actual;
+    seg->moving= 0;
+    seg->motion= 0;
+    indep_dispose(abandon);
+  }
   for (meth=methods; *meth; meth++)
     (*meth)->all_abandon(*meth);
 }
-
-
-
-
-
-
-
- STUFF BEYOND HERE IS FUNCTIONALITY WHICH NEEDS MOVING TO INDEP
-   AND/OR CHECKING THAT WE STILL HAVE IT
-
-
-static ErrorCode fsq_confirm(Method *mm, Change *chg, Segment *move,
-                            int n_motions, const Motion *motions,
-                            int maxdelay_ms) {
-  FsqMethod *m= (void*)mm;
-  FsqReq *r= (FsqReq*)chg;
-  FsqSlotSigned reldeadline;
-  int allow_failure, DP;
-  ErrorCode ec;
-
-  DPRINTF1(movpos,fsq, "%s confirm %s n=%d maxdelay=%dms",
-          m->m.pname, move->i->pname, n_motions, maxdelay_ms);
-
-  assert(!r->motions[0].i); /* no confirming things already confirmed */
-  if (r->deadline==FSQDN)
-    DPRINTF2(" (alloc'd: %d)\n", r->n_motions);
-  else
-    DPRINTF2(" (res: %s/%s[%d@t+%d])\n",
-            r->h.move->i->pname, movpos_pname(r->h.move, r->h.intent),
-            r->n_motions, r->deadline);
-
-  /* If the segment is moving, these motions are already based on the
-   * actual physical position which is stored in the existing request.
-   * So we try removing the existing request from the queue and put
-   * it back if it doesn't work.
-   */
-
-  if (n_motions > r->n_motions)
-    return EC_MovFeatReservationInapplicable;
-  assert(n_motions <= r->n_motions);
-  if (maxdelay_ms == -1) {
-    reldeadline= r->deadline;
-    if (reldeadline==FSQDN)
-      reldeadline= fsq_maxdelay_reldeadline(m, -1, n_motions);
-  } else {
-    reldeadline= fsq_maxdelay_reldeadline(m, maxdelay_ms, n_motions);
-  }
-  allow_failure= reldeadline < (FsqSlotSigned)r->deadline;
-  DPRINTF(movpos,fsq, "%s  reldeadline=[%d@t+%d] allow_failure=%d\n",
-         m->m.pname, n_motions, reldeadline, allow_failure);
-
-  /* state A or R */
-  fsq_dequeue(m, r);
-                                           /* states of existing: */
-  FsqReq *existing=
-    move->moving ? (FsqReq*)move->motion : 0;   /* U or C */
-  if (existing) {
-    DPRINTF(movpos,fsq,
-           "%s  existing %s n=%d deadline=t+%d\n",
-           m->m.pname,
-           existing->h.move->i->pname,
-           existing->n_motions,
-           existing->deadline - m->f.cslot);
-    fsq_dequeue(m, existing);                   /* U or CA */
-  }
-
-  /* state A or RA */
-  memcpy(r->motions, motions, sizeof(r->motions[0])*n_motions);
-  if (!n_motions) r->motions[0].i= move->i->movfeats;
-  assert(r->motions[0].i);
-  r->n_motions= n_motions;
-  r->deadline= reldeadline + m->f.cslot;
-
-  if (n_motions == move->i->n_movfeats)
-    r->actual= 0;
-  else
-    r->actual= chg->actual;
-  assert(r->actual >= 0);
-
-  /* state CA */
-  ec= fsq_enqueue(m, &m->f.confirmed, r);
-  DPRINTF(movpos,fsq, "%s  fsq_enqueue=%s\n", m->m.pname, ec2str(ec));
-  assert(allow_failure || !ec);
-
-  if (existing) {                                  /* CA */
-    if (ec) { /* state C but bad */
-      fsq_dequeue(m,r); /* state CA */
-      fsq_mark_as_allocated(r); /* state A */
-      ErrorCode ec_putback= fsq_enqueue(m,&m->f.confirmed, existing);
-      assert(!ec_putback);                         /* C */
-    } else { /* state C and good */
-      free(existing);                              /* U */
-    }
-  }
-  /* either  ec=0   state C                            U
-   *     or  ec!=0  state A                            C
-   *     or  ec!=0  state C but bad                    C
-   */
-
-  if (ec) return ec;
-
-  move->moving= 1;
-  move->motion= chg;
-  move->movposcomb= -1;
-  ouposn_moving(chg);
-  fsq_check_action(m);
-  return 0;
-}
-
-
-
-    r->actual= movposcomb_update_feature(r->actual,mo->i,mo->posn);
-    if (r->h.actual >= 0 || !r->n_motions)
-      r->h.actual= r->actual;
-    ouposn_moving(&r->h);
-
-
-    
-
-    /* look for something to report
-     * we can get things here other than from the above
-     * eg if we are asked to move the 
-     */
-    Segment *move= r->h.move;
-    assert(move->moving && move->motion == (Change*)r);
-    fsq_mark_as_allocated(r); /* now state A aka Done */
-    motion_done(move,r->h.actual);
-    free(r);
-
-/*---------- entrypoints from rest of program ----------*/
-
-static void fsq_all_abandon(Method *mm) {
-  FsqMethod *m= (void*)mm;
-  int i;
-
-  assert(!m->f.reserved.n);
-
-  for (i=0; i<m->f.confirmed.n; i++) {
-    indep_
-    FsqReq *r= m->f.confirmed.l[i];
-    Segment *move= r->h.move;
-    assert(move->motion == (Change*)r);
-    motion_done(move,r->h.actual);
-    free(r);
-  }
-  m->f.confirmed.n= 0;
-}
-
-in points_all_abandon
-  fsq_all_abandon(mm);
-
-
- *
-
- * seg->moving and ->motion is in one of the states UC