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