;shared code for both photonworld zrfs.
;======================= Self moves ================================
(define Shiftall (
  zarrow-$1 to (change-type arrow-$1)

  a1a
  (while (on-board? x)
     (if (on-board? $2)
        (if (empty? $2) 
           (if not-empty? capture) (if (not-empty? y) (capture y))
        )
        (if (piece? M-up  $2)(create M-up) (create M-up y))
        (if (piece? M-dn  $2)(create M-dn) (create M-dn y))
        (if (piece? M-uf  $2)(create M-uf) (create M-uf y))
        (if (piece? M-df  $2)(create M-df) (create M-df y))
        (if (piece? Block $2)(create Block)(create Block y))
        (if (piece? Delay $2)(create Delay)(create Delay y))
        (if (piece? ph-n  $2)(create ph-n) (if (not-empty? y) (capture y)))
        (if (piece? ph-s  $2)(create ph-s) (if (not-empty? y) (capture y)))
        (if (piece? ph-e  $2)(create ph-e) (if (not-empty? y) (capture y)))
        (if (piece? ph-w  $2)(create ph-w) (if (not-empty? y) (capture y)))
        (if (piece? J-n   $2)(create J-n)  (create J-n y))
        (if (piece? J-s   $2)(create J-s)  (create J-s y))
        (if (piece? J-e   $2)(create J-e)  (create J-e y))
        (if (piece? J-w   $2)(create J-w)  (create J-w y))
        (if (piece? s-v   $2)(create s-v)  (create s-v y))
        (if (piece? s-h   $2)(create s-h)  (create s-h y))
        (if (piece? cr    $2)(create cr)   (create cr y))
     else
        (if not-empty? capture) (if (not-empty? y) (capture y))
     )
     x
  )

  add
))

(define Shift (
  (verify (piece? shift-1 dd5)) ;shift selected
  (verify (not-in-zone? offzone))
  from
  (while (on-board? $1)
     mark (go from) (capture y) back $1 (create $2 y) add
  )
))

(define Shiftp (  ; shift photon
  (verify (piece? shift-1 dd5)) ;shift selected
  (verify (not-in-zone? offzone))
  from
  (while (on-board? $1)
     $1 add 
  )
))


;============================ self drops =============================
(define Trace (
  ztrace to
  (if (piece? trace-2)(change-type trace-1) else (change-type trace-2))

  add
))

(define Del ( ; del board pos
  (verify (not-in-zone? offzone))
  (verify (piece? del-1 dd6)) ;delete selected
  (verify not-empty?)
  capture (capture y)
  add
))

(define Opt ( ; option buttons for drawing
  optz ;option zone
  to mark (verify not-empty?)
  (verify (not-empty? zl1)) ; photon not running

  (if (position? dd1) (change-type run1-1)  else
  (if (position? dd2) (change-type run6-1)  else
  (if (position? dd3) (change-type run-1)   else
  (if (position? dd4) (change-type draw-1)  else
  (if (position? dd5) (change-type shift-1) else
  (if (position? dd6) (change-type del-1) 
  ))))) )

  (if (position? dd3) ;run clicked?  - make ?Rand owner of photons
    a0 (while (on-board? x) x (if photon? flip) )
  )

  ;reset others
  dd1 (if (and not-marked? (not-piece? run1-2)) (change-type run1-2))
  dd2 (if (and not-marked? (not-piece? run6-2)) (change-type run6-2))
  dd3 (if (and not-marked? (not-piece? run-2))  (change-type run-2))
  dd4 (if (and not-marked? (not-piece? draw-2)) (change-type draw-2))
  dd5 (if (and not-marked? (not-piece? shift-2))(change-type shift-2))
  dd6 (if (and not-marked? (not-piece? del-2))  (change-type del-2))

  add
))


(define Clear ( ; delete board content
  dd7 to (change-type clear-2)

  a0
  (while (on-board? x) x
    (if not-empty?
      capture (capture y)
    )
  )
  add
))


(define Draw-p (  ;draw
  (verify (piece? draw-1 dd4)) ;selected
  (verify (not-in-zone? offzone))
  to
  (Paint)
  add
))

(define Paint
    (if (not-empty? aa1)
      (if (piece? s-h) (create s-v)(create s-v y) else
      (if (piece? s-v) (create cr)(create cr y) else
                       (create s-h)(create s-h y)
      ))
    else
    (if (not-empty? aa2) (create Delay) (create Delay y) else
    (if (not-empty? aa3) (create Block) (create Block y) else
    (if (not-empty? aa4)
      (if (piece? J-n) (create J-e)(create J-e y) else
      (if (piece? J-e) (create J-s)(create J-s y) else
      (if (piece? J-s) (create J-w)(create J-w y) else
                           (create J-n)(create J-n y)
      )))
    else
    (if (not-empty? aa5)
      (if (not-piece? M-dn) (create M-dn)(create M-dn y) else
                            (create M-up)(create M-up y)
      )
    else
    (if (not-empty? aa6)
      (if (not-piece? M-df) (create M-df)(create M-df y) else
                            (create M-uf)(create M-uf y)
      )
    else
    (if (not-empty? aa7)
       (if (piece? ph-n)(create ph-e) else
       (if (piece? ph-e)(create ph-s) else
       (if (piece? ph-s)(create ph-w) else
                         (create ph-n)
       )))
       (set-attribute done? false)(set-attribute photon? true)
    ))))) ))
)

;-----------------------------------------------------------------
(define Selgate ( ;select icon image (gate, photon) for drawing
  colzone to

  (if (position? bb1) (change-type s-h-b))
  (if (position? bb2) (change-type Delay))
  (if (position? bb3) (change-type Block))
  (if (position? bb4) (change-type J-n))
  (if (position? bb5) (change-type M-dn))
  (if (position? bb6)(change-type M-df))
  (if (position? bb7)(change-type ph-n))

  w (create arrow) mark
  aa1 (if not-marked? capture)
  (while (on-board? n) n (if not-marked? capture))

    ;automatically select "draw" option
  dd1 (change-type run1-2)
  dd2 (change-type run6-2)
  dd3 (change-type run-2)
  dd4 (change-type draw-1)
  dd5 (change-type shift-2) 
  dd6 (change-type del-2)
  add
))
;================================ pieces ===========================
(define Pm
   (piece  (name $1)
      (image Self "images\photonworld\$1.bmp" ?Rand  "images\photonworld\$1.bmp")
      (moves
        (move-type Standard)
          (Shift s $1)(Shift n $1)(Shift e $1)(Shift w $1)
      ) 
   )
)

(define Ps ;straights and crosses
   (piece  (name $1)
      (image Self "images\photonworld\$1.bmp" "images\photonworld\$1-1.bmp"
             ?Rand  "images\photonworld\$1.bmp" "images\photonworld\$1-1.bmp")
      (moves
        (move-type Standard)
          (Shift s $1)(Shift n $1)(Shift e $1)(Shift w $1)
      )
   )
)

(define PD
   (piece  (name $1)
      (image Self "images\photonworld\$1.bmp" ?Rand  "images\photonworld\$1.bmp")
   )
)

(define Pd
   (piece  (name $1)
      (image Self "images\photonworld\$2.bmp" ?Rand  "images\photonworld\$2.bmp")
   )
)

(define Pdot
   (piece  (name $1)
      (image Self "images\photonworld\Dot.bmp" ?Rand  "images\photonworld\Dot.bmp")
   )
)

(define Pp
   (piece (name ph-$1) ;photons, one for each direction
     (image Self "images\photonworld\ph-$1.bmp")
     (image ?Rand "images\photonworld\ph-$1.bmp")
     (attribute done? false)(attribute photon? true)
      (moves
        (move-type Standard)
          (Shiftp s)(Shiftp n)(Shiftp e)(Shiftp w)
      ) 
   )
)

;============================ board ==========================================
(define Bdef25x25 
  (image "images\photonworld\bd25x25-1.bmp" "images\photonworld\bd25x25-1.bmp"
         "images\photonworld\bd50x50-2.bmp" "images\photonworld\bd50x50-2.bmp")
  (grid
     (start-rectangle 5 5 20 20)
     (dimensions
         ("a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y" (16 0)) ; files
         ("25/24/23/22/21/20/19/18/17/16/15/14/13/12/11/10/9/8/7/6/5/4/3/2/1" (0 16)) ; ranks
         ("a/b" (0 1000)) ;second board
     )
     (directions (n 0 -1 0) (e 1 0 0) (s 0 1 0) (w -1 0 0)
		 (ne 1 -1 0) (nw -1 -1 0) (se 1 1 0) (sw -1 1 0)
             (x 1 0 0) (y 0 0 1)
     )
  )


  (grid  ;photon, photons gates  etc
     (start-rectangle 432 10 452 30)
     (dimensions
         ("aa/bb/cc" (20 0)) ; files
         ("7/6/5/4/3/2/1" (0 20)) ; ranks
     )
     (directions 
         (n 0 -1) (e 1 0) (s 0 1) (w -1 0)
     )
  )

  (grid  ;drawing options
     (start-rectangle 432 250 492 270)
     (dimensions
         ("dd" (60 0)) ; files
         ("7/6/5/4/3/2/1" (0 20)) ; ranks
     )
     (directions
         (n 0 -1) (e 1 0) (s 0 1) (w -1 0)
     )
  )

  (dummy a0 z0
     zl1 zl2 zl3 zl4 zl5 zl6
  )
  (links x
      (a0    a1a)(y1a   a2a)(y2a   a3a)(y3a   a4a)(y4a   a5a)(y5a   a6a)(y6a   a7a)(y7a   a8a)(y8a   a9a)(y9a  a10a)
      (y10a a11a)(y11a a12a)(y12a a13a)(y13a a14a)(y14a a15a)(y15a a16a)(y16a a17a)(y17a a18a)(y18a a19a)(y19a a20a)
      (y20a a21a)(y21a a22a)(y22a a23a)(y23a a24a)(y24a a25a)(y25a z0)
  )
  (links nn
      (zl1 zl2)(zl2 zl3)(zl3 zl4)(zl4 zl5)(zl5 zl6)
  )


  (positions
       (zfromto 0 -3 0 -3); indicate from process finished
       (zdump 0 -4 0 -4) ; dummy drop zone
       (zm1 0 -5 0 -5)(zm2 0 -6 0 -6) ;for dummy moves

       (ztrace 432 190 492 210)

       (zarrow-w 422 230 442 250)
       (zarrow-n 442 230 462 250)
       (zarrow-s 462 230 482 250)
       (zarrow-e 482 230 502 250)
  )

  (zone (name optz)(players Self)(positions ;options (run, draw etc)
    dd1 dd2 dd3 dd4 dd5 dd6
  ))

  (zone (name offzone)(players Self ?Rand)(positions
    aa1 aa2 aa3 aa4 aa5 aa6 aa7
    bb1 bb2 bb3 bb4 bb5 bb6 bb7
    cc1 cc2 cc3 cc4 cc5 cc6 cc7
    dd1 dd2 dd3 dd4 dd5 dd6 dd7
    zfromto zdump zm1 zm2
    a0 z0
    zl1 zl2 zl3 zl4 zl5 zl6
    zarrow-e zarrow-s zarrow-w zarrow-n
    ztrace
  ))

  (zone (name colzone)(players Self ?Rand)(positions ;colours (states)
    bb1 bb2 bb3 bb4 bb5 bb6 bb7
  ))
)

(define Bdef50x50 
  (image "images\photonworld\bd50x50-2.bmp" "images\photonworld\bd50x50.bmp")
  (grid
     (start-rectangle 5 5 12 12)
     (dimensions
         ("a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z/A/B/C/D/E/F/G/H/I/J/K/L/M/N/O/P/Q/R/S/T/U/V/W/X" (8 0)) ; files
         ("50/49/48/47/46/45/44/43/42/41/40/39/38/37/36/35/34/33/32/31/30/29/28/27/26/25/24/23/22/21/20/19/18/17/16/15/14/13/12/11/10/9/8/7/6/5/4/3/2/1" (0 8)) ; ranks
     )
     (directions (n 0 -1) (e 1 0) (s 0 1) (w -1 0)
		 (ne 1 -1) (nw -1 -1) (se 1 1) (sw -1 1)
     )
  )


  (grid  ;photon, photons gates  etc
     (start-rectangle 432 20 452 40)
     (dimensions
         ("aa/bb/cc" (20 0)) ; files
         ("11/10/9/8/7/6/5/4/3/2/1" (0 20)) ; ranks
     )
     (directions 
         (n 0 -1) (e 1 0) (s 0 1) (w -1 0)
     )
  )

  (grid  ;drawing options
     (start-rectangle 432 260 492 280)
     (dimensions
         ("dd" (60 0)) ; files
         ("7/6/5/4/3/2/1" (0 20)) ; ranks
     )
     (directions 
         (n 0 -1) (e 1 0) (s 0 1) (w -1 0)
     )
  )

  (dummy a0 z0)
  (links x
     (a0 a1)(X1 a2)(X2 a3)(X3 a4)(X4 a5)(X5 a6)(X6 a7)(X7 a8)(X8 a9)(X9 a10)
     (X10 a11)(X11 a12)(X12 a13)(X13 a14)(X14 a15)(X15 a16)(X16 a17)(X17 a18)(X18 a19)(X19 a20)
     (X20 a21)(X22 a22)(X22 a23)(X23 a24)(X24 a25)(X25 a26)(X26 a27)(X27 a28)(X28 a29)(X29 a30)
     (X30 a31)(X33 a32)(X32 a33)(X33 a34)(X34 a35)(X35 a36)(X36 a37)(X37 a38)(X38 a39)(X39 a40)
     (X40 a41)(X44 a42)(X42 a43)(X43 a44)(X44 a45)(X45 a46)(X46 a47)(X47 a48)(X48 a49)(X49 a50)
     (X50 z0)
  )
  (positions 
       (zfromto 0 -3 0 -3); indicate from process finished
       (zdump 0 -4 0 -4) ; dummy drop zone
  )

  (zone (name offzone)(players Self ?Rand)(positions
    aa1 aa2 aa3 aa4 aa5 aa6 aa7 aa8 aa9 aa10 aa11
    bb1 bb2 bb3 bb4 bb5 bb6 bb7 bb8 bb9 bb10 bb11
    cc1 cc2 cc3 cc4 cc5 cc6 cc7 cc8 cc9 cc10 cc11
    dd1 dd2 dd3 dd4 dd5 dd6
    zfromto zdump
    a0 z0
  ))

  (zone (name colzone)(players Self ?Rand)(positions ;colours (states)
    bb1 bb2 bb3 bb4 bb5 bb6 bb7 bb8 bb9 bb10 bb11
  ))
)


