Isoraqathedh

determine-board-size

Mar 29th, 2014
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 2.06 KB | None | 0 0
  1. (defun determine-board-size (piece &key (min-size '((-2 -2) (2 2))) (squarep t) (extra-space 1) (rider-min-iteration 2))
  2.   (labels ((digest-macromoves (macromove)
  3.          ;; (:then then-args piece*)
  4.          (flet ((finite-range-p (args)
  5.               (and (getf args :range)
  6.                (numberp (getf (getf args :range) :distance)))))
  7.            (loop for (x y . args) in (nthcdr 2 macromove)
  8.           sum (if (finite-range-p args)
  9.               (* (getf (getf args :range) :distance) x)
  10.               x) into total-x
  11.           sum (if (finite-range-p args)
  12.               (* (getf (getf args :range) :distance) y)
  13.               y) into total-y
  14.           finally (return (list total-x total-y)))))
  15.        (digest-ranges (range)
  16.          (destructuring-bind (x y &key range &allow-other-keys) range
  17.            (let ((max-distance (getf range :distance)))
  18.          (if (numberp max-distance) ; i.e. the range is finite
  19.              (list (* max-distance x) (* max-distance y))
  20.              (list (* rider-min-iteration x) (* rider-min-iteration y))))))
  21.                     ; show at least that many iterations
  22.        (digest-dest (dest)
  23.          (cond ((eql (first dest) :then) (digest-macromoves dest))
  24.            ((getf (cddr dest) :range) (digest-ranges dest))
  25.            (t dest))))
  26.     (loop for dest in piece
  27.        maximize (first  (digest-dest dest)) into max-x
  28.        minimize (first  (digest-dest dest)) into min-x
  29.        maximize (second (digest-dest dest)) into max-y
  30.        minimize (second (digest-dest dest)) into min-y
  31.        finally (return
  32.          (destructuring-bind ((given-min-x given-min-y) (given-max-x given-max-y))
  33.              (if min-size min-size '((0 0) (0 0)))
  34.            (let ((max-x-pad (max given-max-x (+ max-x extra-space)))
  35.              (min-x-pad (min given-min-x (- min-x extra-space)))
  36.              (max-y-pad (max given-max-y (+ max-y extra-space)))
  37.              (min-y-pad (min given-min-y (- min-y extra-space))))
  38.              (if squarep
  39.              (let ((square-all (max (abs (max max-x-pad max-y-pad))
  40.                        (abs (min min-x-pad min-y-pad)))))
  41.                (list (list (- square-all) (- square-all))
  42.                  (list square-all square-all)))
  43.              (list (list min-x-pad min-y-pad)
  44.                    (list max-x-pad max-y-pad)))))))))
Advertisement
Add Comment
Please, Sign In to add comment