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