Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 2.01 KB | None | 0 0
  1. (defparameter *map* (make-hash-table))
  2.  
  3. (define-condition make-error (error)
  4.   ((args :initarg :args :reader make-error-args))
  5.   (:report (lambda (condition stream)
  6.              (format stream "Entry ~a not recognized."
  7.                      (make-error-args condition)))))
  8.  
  9. (defmacro make-room (name &optional (title nil) &body forms)
  10.   (labels ((is-direction-p (dir)
  11.              (or
  12.               (eq dir 'north)
  13.               (eq dir 'south)
  14.               (eq dir 'west)
  15.               (eq dir 'east)
  16.               (eq dir 'up)
  17.               (eq dir 'down))))
  18.     (let* ((exits nil)
  19.            (funcs nil)
  20.            (room
  21.             (loop for f in forms append
  22.                  (let* ((key (car f))
  23.                         (rest (cdr f))
  24.                         (first (car rest)))
  25.                    (cond ((eq key 'loc) `(:loc ',first))
  26.                          ((eq key 'desc) `(:desc ',rest))
  27.                          ((eq key 'flags) `(:flags ',rest))
  28.                          ((eq key 'action) `(:action ,rest))
  29.                          ((eq key 'global) `(:global ',rest))
  30.                          ((is-direction-p key)
  31.                           (cond ((eq (cadr rest) 'if)
  32.                                  (let* ((sym (gensym))
  33.                                         (func (caddr rest))
  34.                                         (else (last rest)))
  35.                                    (push `(,sym ,func) funcs)
  36.                                    (push `(list ',key ',first ,sym ',else) exits)))
  37.                                 (t
  38.                                  (push `(list ',key ',first) exits)))
  39.                           nil)
  40.                          (t
  41.                           (error 'make-error :args f)))))))
  42.       `(let (,@funcs)
  43.          (setf (gethash ',name *map*) (list :title ,title ,@room :exits (list ,@exits)))))))
  44.  
  45. (defun have-boat ()
  46.   (have 'boat))
  47.  
  48. (make-room marsh "Marsh"
  49.   (west marsh-wall if #'have-boat "You are stuck")
  50.   (south stone-arch if #'have-boat "You are stuck"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement