 
 (defalias 'insertc 'insert-colored-text)
 
-(defvar *width* 80)
-(defvar *height* 30)
-(defvar *jungle* '(45 10 10 10))
-(defvar *plant-energy* 80)
-
-(defvar *plants* (make-hash-table :test #'equal))
-
 (defun random-plant (left top width height)
   (let ((pos (cons (+ left (random width)) (+ top (random height)))))
-    (setf (gethash pos *plants*) t)))
+    (setf (gethash pos evolution-plants) t)))
 
 (defun add-plants ()
-  (apply 'random-plant *jungle*)
-  (random-plant 0 0 *width* *height*))
+  (apply 'random-plant evolution-jungle-rectangle)
+  (random-plant 0 0 evolution-width evolution-height))
 
 (defstruct animal x y energy dir genes)
-
-(defvar *animals*
-  (list (make-animal :x      (ash *width* -1)
-                     :y      (ash *height* -1)
-                     :energy 1000
-                     :dir    0
-                     :genes (loop repeat 8
-                                  collecting (1+ (random 10))))))
 
 (defun move (animal)
   (let ((dir (animal-dir animal))

;; evolution.el -- the evolution demo, ported to EmacsLisp

(defun evolution ()
  (interactive)
  (switch-to-buffer "*evolution*")


(defun eat (animal)
  (let ((pos (cons (animal-x animal) (animal-y animal))))
    (when (gethash pos evolution-plants)
      (incf (animal-energy animal) evolution-plant-energy)
      (remhash pos evolution-plants))))

(defun reproduce (animal)
  (let ((e (animal-energy animal)))
    (when (>= e evolution-reproduction-energy)
      (setf (animal-energy animal) (ash e -1))
      (let ((animal-nu (copy-animal animal))
            (genes     (copy-list (animal-genes animal)))
            (mutation (random 8)))
        (setf (nth mutation genes) (max 1 (+ (nth mutation genes) (random 3) -1)))
        (setf (animal-genes animal) genes)
        (push animal-nu evolution-animals)))))

(defun update-world ()
  (setf evolution-animals (remove-if (lambda (animal)
                               (<= (animal-energy animal) 0))
                             evolution-animals))
  (mapc (lambda (animal)
          (turn animal)
          (move animal)
          (eat animal)
          (reproduce animal))
        evolution-animals)
  (add-plants))

(defun draw-world ()
  (erase-buffer)
  (loop for y
        below evolution-height
        do (progn (newline)
                  (insertc "|" "green" nil)
                  (loop for x
                        below evolution-width
                        do (insert (cond ((some (lambda (animal)
                                                  (and (= (animal-x animal) x)
                                                       (= (animal-y animal) y)))
                                                evolution-animals)
                                          ?M )
                                         ((gethash (cons x y) evolution-plants) ?\* )
                                         (t ?\s ))))
                  (insertc "|" "green" nil)))
  (redisplay t))

(defun evolution ()
  (interactive)
