Wolf sheep works in tests
[clnl] / src / main / nvm / base.lisp
1 (in-package #:clnl-nvm)
2
3 (defvar *current-id* 0)
4
5 (defvar *turtles* nil)
6 (defvar *turtles-own-vars* nil)
7 (defvar *patches-own-vars* nil)
8 (defvar *patches* nil)
9 (defvar *myself* nil)
10 (defvar *self* nil)
11 (defvar *dimensions* nil)
12 (defvar *globals* nil)
13 (defvar *topology* :torus)
14 (defvar *ticks* nil)
15 (defvar *breeds* nil)
16
17 (define-condition stop nil nil)
18 (define-condition death nil nil)
19
20 (defmacro with-stop-handler (&rest forms)
21  "MACRO WITH-STOP-HANDLER &rest FORMS => HANDLED-FORM
22
23 ARGUMENTS AND VALUES:
24
25   FORMS: body to be handled
26   HANDLED-FORM: body with handling
27
28 DESCRIPTION:
29
30   WITH-STOP-HANDLER is a convenience macro to handle when
31   programs issue a stop condition.  When one does, a simple
32   :stop is returned."
33  `(handler-case (progn ,@forms) (stop (s) (declare (ignore s)) :stop)))
34
35 (defmacro with-stop-and-death-handler (&rest forms)
36  `(handler-case
37    (progn ,@forms)
38    (stop (s) (declare (ignore s)) :stop)
39    (death (d) (declare (ignore d)) :death)))
40
41 (defstruct turtle who breed color heading xcor ycor (label "") label-color size shape own-vars)
42 (defstruct patch color xcor ycor own-vars turtles)
43
44 (defun agentset-list (agentset)
45  (cond
46   ((eql agentset :turtles) *turtles*)
47   ((eql agentset :patches) *patches*)
48   ((and (listp agentset) (eql :agentset (car agentset))) (cddr agentset))
49   ((find agentset *breeds* :key #'car)
50    (remove agentset *turtles* :key #'turtle-breed :test-not #'eql))
51   (t (error "Doesn't seem to be an agentset: ~A" agentset))))
52
53 (defun agentset-breed (agentset)
54  (cond
55   ((eql agentset :turtles) :turtles)
56   ((eql agentset :patches) :patches)
57   ((find agentset *breeds* :key #'car) agentset)
58   ((and (listp agentset) (eql :agentset (car agentset))) (second agentset))
59   (t (error "Doesn't seem to be an agentset: ~A" agentset))))
60
61 (defun list->agentset (list breed)
62  (append (list :agentset breed) list))
63
64 (defun agentset-p (o)
65  (or
66   (eql o :turtles)
67   (eql o :patches)
68   (find o *breeds* :key #'car)
69   (and (listp o) (eql :agentset (car o)))))
70
71 (defun agent-p (o)
72  (or (turtle-p o) (patch-p o)))
73
74 (defun breed-p (breed)
75  (find breed *breeds* :key #'car))
76
77 (defun breed-default-shape (breed)
78  (second (find breed *breeds* :key #'car)))
79
80 (defsetf breed-default-shape (breed) (shape)
81  `(setf (second (find ,breed *breeds* :key #'car)) ,shape))