|
| 1 | +;;; -*- Mode: Lisp; Syntax: Common-Lisp; -*- Author: Peter Norvig |
| 2 | + |
| 3 | +;;;; Algorithms for manipulating objects in a grid |
| 4 | + |
| 5 | +(defun grid-contents (env loc) |
| 6 | + "Return a list of objects in this location, optionally including |
| 7 | + objects that are contained within containers here." |
| 8 | + (aref (grid-environment-grid env) (xy-x loc) (xy-y loc))) |
| 9 | + |
| 10 | +(defsetf grid-contents (env loc) (val) |
| 11 | + `(setf (aref (grid-environment-grid ,env) (xy-x ,loc) (xy-y ,loc)) |
| 12 | + ,val)) |
| 13 | + |
| 14 | +(defun move-object-to (object loc env) |
| 15 | + "Move an object to an absolute location and return that location. However, |
| 16 | + attempting to move into a location with an obstacle fails (returns nil) |
| 17 | + and the object receives a bump." |
| 18 | + (cond ((find-object-if #'obstacle-p loc env) |
| 19 | + (setf (object-bump object) 'bump) |
| 20 | + nil) |
| 21 | + (t (remove-object object env) |
| 22 | + (place-object object loc env) |
| 23 | + loc))) |
| 24 | + |
| 25 | +(defun place-object (object loc env &optional (initial? t)) |
| 26 | + "Put the object in its initial position or a new position in environment." |
| 27 | + ;; Coerce agents into agent-bodies |
| 28 | + (when (agent-p object) |
| 29 | + (pushnew object (environment-agents env)) |
| 30 | + (setf object (agent-body object))) |
| 31 | + ;; Place the object |
| 32 | + (setf (object-loc object) loc) |
| 33 | + (pushnew object (grid-contents env loc)) |
| 34 | + (when initial? |
| 35 | + (push object (grid-environment-objects env))) |
| 36 | + object) |
| 37 | + |
| 38 | +(defun place-in-container (object container env) |
| 39 | + "Put the object inside the container, if there is room." |
| 40 | + ;; First, check to see if there is space |
| 41 | + (when (< (+ (object-size object) |
| 42 | + (sum (object-contents container) #'object-size)) |
| 43 | + (object-max-contents object)) |
| 44 | + ;; If there is, remove it from where it was. |
| 45 | + (remove-object object env) |
| 46 | + ;; Now place it in its new container |
| 47 | + (setf (object-container object) container) |
| 48 | + (setf (object-loc object) (object-loc container)) |
| 49 | + (pushnew object (object-contents container)) |
| 50 | + object)) |
| 51 | + |
| 52 | +(defun remove-object (object env) |
| 53 | + "Remove the object from its current location." |
| 54 | + (let ((loc (object-loc object)) |
| 55 | + (old-container (object-container object))) |
| 56 | + (deletef object (grid-contents env loc)) |
| 57 | + (when old-container |
| 58 | + (deletef object (object-contents old-container)) |
| 59 | + (setf (object-container object) nil)))) |
| 60 | + |
| 61 | +(defun find-object-if (predicate loc env) |
| 62 | + "Return an object in this location that satisfies this predicate." |
| 63 | + (find-if predicate (grid-contents env loc))) |
| 64 | + |
| 65 | +(defun find-neighbor-if (predicate loc env) |
| 66 | + "Return an object in a neighboring square that satisfies the predicate." |
| 67 | + (let ((x (xy-x loc)) |
| 68 | + (y (xy-y loc))) |
| 69 | + ;; Look in the four neighboring squares |
| 70 | + (or (find-object-if predicate (@ x (+ y 1)) env) |
| 71 | + (find-object-if predicate (@ x (- y 1)) env) |
| 72 | + (find-object-if predicate (@ (+ x 1) y) env) |
| 73 | + (find-object-if predicate (@ (- x 1) y) env)))) |
| 74 | + |
| 75 | +(defun find-object-or-neighbor-if (predicate loc env) |
| 76 | + "Return an object either in loc or a neighboring square that satisfies |
| 77 | + the predicate." |
| 78 | + (or (find-object-if predicate loc env) |
| 79 | + (find-neighbor-if predicate loc env))) |
| 80 | + |
| 81 | +(defun near? (loc1 loc2 &optional (tolerance 1)) |
| 82 | + "Are the two locations nearby each other?" |
| 83 | + (and (<= (abs (- (xy-x loc1) (xy-x loc2))) tolerance) |
| 84 | + (<= (abs (- (xy-y loc1) (xy-y loc2))) tolerance))) |
| 85 | + |
| 86 | +;;;; Maintaining and manipulating orientation |
| 87 | + |
| 88 | +(defun add-locs (&rest locations) |
| 89 | + "Coordinate-wise addition of locations: (add-locs '(1 2) '(10 20)) = (11 22)" |
| 90 | + (apply #'mapcar #'+ locations)) |
| 91 | + |
| 92 | +(defun subtract-locs (&rest locations) |
| 93 | + "Coordinate-wise subtraction of locations." |
| 94 | + (apply #'mapcar #'- locations)) |
| 95 | + |
| 96 | +(defun heading->string (heading) |
| 97 | + "Convert a heading like (0 1) to a depictive string like ^." |
| 98 | + (cond ((equal heading '(1 0)) ">") |
| 99 | + ((equal heading '(0 1)) "^") |
| 100 | + ((equal heading '(-1 0)) "<") |
| 101 | + ((equal heading '(0 -1)) "V") |
| 102 | + (t "?"))) |
| 103 | + |
| 104 | +(defun absolute-loc (agent-body offset) |
| 105 | + "Return an absolute location given an offset from an agent, taking the |
| 106 | + agent's orientation into account. An offset of (1 2) means 1 square to |
| 107 | + the right and two ahead of the agent, given its present orientation." |
| 108 | + (let ((x (xy-x offset)) |
| 109 | + (y (xy-y offset)) |
| 110 | + (heading (agent-body-heading agent-body))) |
| 111 | + (add-locs (object-loc agent-body) |
| 112 | + (cond ((equal heading '(1 0)) (@ y (- x))) |
| 113 | + ((equal heading '(0 1)) offset) |
| 114 | + ((equal heading '(-1 0)) (@ (- y) x)) |
| 115 | + ((equal heading '(0 -1)) (@ (- x) (- y))) |
| 116 | + (t "?"))))) |
| 117 | + |
| 118 | +(defun offset-loc (agent-body loc) |
| 119 | + "Return an offset from an agent that corresponds to the absolute loc." |
| 120 | + (let ((x (- (xy-x loc) (xy-x (object-loc agent-body)))) |
| 121 | + (y (- (xy-y loc) (xy-y (object-loc agent-body)))) |
| 122 | + (heading (agent-body-heading agent-body))) |
| 123 | + (cond ((equal heading '(1 0)) (@ (- y) (+ x))) |
| 124 | + ((equal heading '(0 1)) (@ x y)) |
| 125 | + ((equal heading '(-1 0)) (@ (+ y) (- x))) |
| 126 | + ((equal heading '(0 -1)) (@ (- x) (- y))) |
| 127 | + (t "?")))) |
0 commit comments