Isoraqathedh

make-piece

Jun 2nd, 2014
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 2.18 KB | None | 0 0
  1. (defun make-piece (x y dirs signatures)
  2.   "The basic method to create a piece."
  3.   (when (or (null dirs) (eql dirs :all)) ;; default value
  4.     (setf dirs *all-dirs*))
  5.   (let (final-list)
  6.     (macrolet ((push-destination-when (direction x y)
  7.                  `(when ,direction
  8.                     (push (make-instance 'destination
  9.                                          :x ,x :y ,y
  10.                                          :signature (or signatures (list :move :capture))) final-list))))
  11.       (destructuring-bind (&key n e s w ne se sw nw nne ene ese sse ssw wsw wnw nnw)
  12.           (loop for i in dirs collect i collect t)
  13.                                         ; This converts the directions list into a plist
  14.                                         ; so I can refer to them individually below
  15.         (cond ((= 0 x y) (push-destination-when t 0 0)) ;; Zero case
  16.               ((and (or  (zerop x) (zerop y))
  17.                     (not (and (zerop x) (zerop y))))
  18.                ;; Orthogonal case
  19.                (let ((length (max x y)))
  20.                  (push-destination-when n length 0)
  21.                  (push-destination-when e 0 length)
  22.                  (push-destination-when s 0 (- length))
  23.                  (push-destination-when w (- length) 0)))
  24.                ((and (= x y) (not (zerop x)) (not (zerop y)))
  25.                 ;; Diagonal case
  26.                 (push-destination-when ne x x)
  27.                 (push-destination-when se x (- x))
  28.                 (push-destination-when sw (- x) (- x))
  29.                 (push-destination-when nw (- x) x))
  30.                (t
  31.                 ;; Hippogonal case
  32.                 (let ((max (max x y)) (min (min x y))
  33.                       (-max (- (max x y))) (-min (- (min x y))))
  34.                   (push-destination-when nne min max)
  35.                   (push-destination-when ene max min)
  36.                   (push-destination-when ese max -min)
  37.                   (push-destination-when sse min -max)
  38.                   (push-destination-when ssw -min -max)
  39.                   (push-destination-when wsw -max -min)
  40.                   (push-destination-when wnw -max min)
  41.                   (push-destination-when nnw -min max))))))
  42.     final-list))
Advertisement
Add Comment
Please, Sign In to add comment