; This macro is called twice by each RemoveLink block.

  (define removelinkend
    mark
    $1                  ; go to one end of the link
    (if (piece? $2)
      capture
     else (if (piece? $3)               ; There should be just four possible conditions:
                                        ; this is the only link end in this region,
            (change-type $4)            ; another link end of the same color joins to the same peg here,
                                        ; or another link end of either color runs parallel to this link end.
            else (if (piece? $5)
                                        ; this routine places a new end piece which preserves the
                  (change-type $6)      ; display of any other link end in this region.
                  
                 else (if (or (and (piece? $7) friend?) (and (piece? $8) not-friend?))
                        (change-type $6)
                        (if friend? flip)
                       else               ; this branch should never occur
                        (change-type ErrorLinkEnd)
                      )
                 )
          )
    )
    back
  )

; Here are macros that perform 'autolinking' to a peg.
; If a friendly peg is at the proper linking distance, and no other link crosses its path, add a link there.



  (define placelinkend  ; This macro is called by trylink when it decides to place a link.
    $1                  ; go to one end of the link
    (if (and empty? (or (not $9) (not-flag? CornerLinked)))
      cascade                                                   ; If another link end is being added to this corner of the peg being
      (change-type $2)                                          ; autolinked, the space will appear empty. The flags FirstInCorner
      to                                                        ; and CornerLinked are used to make sure the proper end piece is used.
     else (if (or (piece? $3) (and $9 (flag? CornerLinked)))
                                        ; There should be just four possible conditions:
            cascade                     ; There are no other link ends in this region,
            (change-type $4)            ; another link end of the same color joins to the same peg here,
            to                          ; or another link end of either color runs parallel to this link end.
           else (if (piece? $5)         ; this routine places a new end piece which preserves the
                  (if friend?           ; display of any other link end in this region.
                    cascade
                    (change-type $6)
                    to
                   else
                    cascade 
                    (change-type $7)
                    to
                  )
                 else               ; this branch should never occur
                  cascade
                  (change-type ErrorLinkEnd)
                  to
                )
          )
    )
    $8   ; return to the middle of the link (Can't use mark & back because the calling routine (trylink) uses it)
  )


  (define trylink
    (if (friend? $1)               ; is there a friendly peg on the other hole for this link?
      mark
      $2                           ; tunnel over to the other link grid, to check for blocking links.
      (if (empty? $3)
        (if (not-piece? $4 $5)
          (if (empty? $6)
            (if (not-piece? $4 $7)
              back
              $8                   ; now tunnel to the same link grid as the potential link.
              (if (empty? $9)
                (if (not-piece? $10 $11)
                  (if (not-piece? $10 $12)
                    $9                        ; ready to place a link
                    cascade
                    (change-type $13)         ; put the middle part down
                    to
                    (placelinkend $14 $15 $16 $17 $18 $19 $20 $21 true)    ; place the end that touches the peg that is being autolinked
                    (placelinkend $21 $18 $22 $23 $15 $19 $24 $14 false)   ; place the other end
                    (if (flag? FirstInCorner)
                      (set-flag CornerLinked true)
                    )               
                  )
                )
              )
            )
          )
        )
      )
      back
    )
  )
  
  
(define tryall
  to
  (set-flag FirstInCorner true)
  (set-flag CornerLinked false)
  (trylink nne Lese Ln Link4 Lnn Lenne Lene Lsse Lnne Link2 Lnnne Le Link1 Dn L1b L3L L13bL L1t L11ff L11ef Up L3R L13tR L11fe)
  (set-flag FirstInCorner false)
  (trylink ene Lsse Le Link2 Lee Lnene Lnne Lese Lene Link4 Leene Ln Link3 Lf L3L L1b L13bL L3R L33ff L33ef Rt L1t L13tR L33fe)
  (set-flag FirstInCorner true)
  (set-flag CornerLinked false)
  (trylink ese Lsse Le Link1 Lee Lsese Lsse Lese Lese Link3 Leese Ls Link4 Lf L4L L2t L24tL L4R L44ff L44ef Rt L2b L24bR L44fe)
  (set-flag FirstInCorner false)
  (trylink sse Lese Ls Link3 Lss Lesse Lese Lsse Lsse Link1 Lssse Le Link2 Up L2t L4L L24tL L2b L22ff L22fe Dn L4R L24bR L22ef)
  (set-flag FirstInCorner true)
  (set-flag CornerLinked false)
  (trylink ssw Lese Ls Link4 Lss Lwssw Lwsw Lsse Lssw Link2 Lsssw Lw Link1 Up L1t L3R L13tR L1b L11ff L11fe Dn L3L L13bL L11ef)
  (set-flag FirstInCorner false)
  (trylink wsw Lsse Lw Link2 Lww Lswsw Lssw Lese Lwsw Link4 Lwwsw Ls Link3 Rt L3R L1t L13tR L3L L33ff L33fe Lf L1b L13bL L33ef)
  (set-flag FirstInCorner true)
  (set-flag CornerLinked false)
  (trylink wnw Lsse Lw Link1 Lww Lnwnw Lnnw Lese Lwnw Link3 Lwwnw Ln Link4 Rt L4R L2b L24bR L4L L44ff L44fe Lf L2t L24tL L44ef)
  (set-flag FirstInCorner false)
  (trylink nnw Lese Ln Link3 Lnn Lwnnw Lwnw Lsse Lnnw Link1 Lnnnw Lw Link2 Dn L2b L4R L24bR L2t L22ff L22ef Up L4L L24tL L22fe)
  (storeinfo)
  add
  (getinfo)
)
 
; This macro is called at the beginning of all moves and drops.
; It retrieves the attributes of the info piece and stores them in flags.

(define getinfo
    mark
    info
    (set-flag WhiteManualMode iWhiteManualMode)
    (set-flag BlackManualMode iBlackManualMode)
    (set-flag FirstMove  iFirstMove)
    (set-flag Swapmove iSwapMove)
    back
)

; This macro is called at the end of most moves and drops. (just before the add)
; It stores flag info in the attributes of the info piece.

(define storeinfo
    mark
    info
    (set-attribute iWhiteManualMode (flag? WhiteManualMode))
    (set-attribute iBlackManualMode (flag? BlackManualMode))
    (set-attribute iFirstMove (flag? FirstMove))
    (set-attribute iSwapMove (flag? SwapMove))
    back
)

