%%HP: T(3)A(D)F(.);
@ by Mattias Dahl & Morgan Lindqvist
DIR 
  U137
    \<< 2 - 7 MOD 2 + DUP INIT (5,5) \-> ANT POS AIM
       \<< 0 POS AIM ANT WFACT 'POS' STO DROP AIM GMARK
          DO 1 + POS AIM GETK DUP 'AIM' STO ANT WFACT 'POS' STO OBJ\->
          UNTIL 1 SWAP 1 -
            START + 
            NEXT 0 SAME
          END MOVES \->TAG
        \>>
      \>>

  GETK
    \<<
      DO
        DO KEY
        UNTIL
        END
      UNTIL
        CASE DUP 25 SAME
        THEN 24 - GMOVE 0
        END DUP DUP DUP 34 SAME SWAP 35 SAME OR SWAP 36 SAME OR
        THEN 32 - GMOVE 0
        END 51 SAME
        THEN 1
        END 0
        END
      END
    \>>

  GMOVE
    \<< \-> P V
      \<< P C\->R
        CASE V 1 SAME
          THEN
            IF DUP 1 SAME NOT
            THEN 1 -
            END
          END V 2 SAME
          THEN SWAP
            IF DUP 1 SAME NOT
            THEN 1 -
            END SWAP
          END V 3 SAME
          THEN
            IF DUP 10 SAME NOT
            THEN 1 +
            END
          END SWAP
          IF DUP 10 SAME NOT
          THEN 1 +
          END SWAP
        END R\->C DUP 'V' STO GMARK P GMARK 
        PICT { # 107d # 0d } V \->STR " " + 1 \->GROB REPL V
      \>>
    \>>

  GMARK
    \<< C\->R 1 - 6 * 1 + R\->B SWAP 1 - 6 * 2 + R\->B SWAP
      2 \->LIST PICT SWAP "*" 1 \->GROB GXOR
    \>>

  INIT
    \<< \-> A
      \<< PICT { # 0d # 0d } # 131d # 64d BLANK REPL { # 0d# 0d } PVIEW 0 10
        FOR X
          # 0d X 6 * R\->B 2 \->LIST DUP 1 # 60d PUT LINE
          6 X * R\->B # 0d 2 \->LIST DUP 2 # 60d PUT LINE
        NEXT
        PICT { # 67d # 0d } "Aiming at (5,5)" 1 \->GROB REPL
        PICT { # 67d # 6d } "MOVES" 1 \->GROB REPL 1 A
        FOR X
          PICT # 67d X 1 + 6 * R\->B 2 \->LIST "SUB :" X + 1 \->GROB REPL
          RAND 10 * IP 1 + RAND 10 * IP 1 + R\->C
        NEXT A \->LIST
      \>>
    \>>

  WFACT
    \<< 0 \-> POS AIM ANT DIS
      \<< POS ANT AIM MSUB 'POS' STO 'DIS' STO 1 ANT
        FOR X PICT # 104d X 1 + 6 * R\->B 2 \->LIST 'DIS' X GET
          IF DUP 0 SAME
          THEN DROP "SUNKEN"
          ELSE \->STR
          END 1 \->GROB REPL
        NEXT PICT {# 104d # 6d } 3 PICK \->STR 1 \->GROB REPL DIS POS
      \>>
    \>>

  MSUB
    \<< \-> POS ANT AIM
      \<< 1 ANT
        FOR X 'POS' X GET
          IF DUP 0 SAME NOT
          THEN AIM - C\->R ABS SWAP ABS MAX
            IF DUP 0 SAME
            THEN 'POS' X 0 PUT
            ELSE
              IF 59 FS?
              THEN 'POS' X GET DUP GMARK GMARK
              END
              IF 60 FS?
              THEN 'POS' DUP X GET C\->R 1 2
                START RAND 3 * IP 1 - + 
                  IF DUP 10 >
                  THEN 1 -
                  END
                  IF DUP 1 <
                  THEN 1 +
                  END SWAP
                NEXT R\->C X SWAP PUT
              END
            END
          END
        NEXT ANT \->LIST POS
      \>>
    \>>

END
