1 ; Copyright 2022 Frank Duncan (frank@consxy.com) under AGPL3. See distributed LICENSE.txt.
2 (in-package #:clnl-nvm)
4 (defvar *current-id* 0)
7 (defvar *turtles-own-vars* nil)
8 (defvar *patches-own-vars* nil)
12 (defvar *dimensions* nil)
13 (defvar *globals* nil)
14 (defvar *topology* :torus)
18 (define-condition stop nil nil)
19 (define-condition death nil nil)
21 (defmacro with-stop-handler (&rest forms)
22 "MACRO WITH-STOP-HANDLER &rest FORMS => HANDLED-FORM
26 FORMS: body to be handled
27 HANDLED-FORM: body with handling
31 WITH-STOP-HANDLER is a convenience macro to handle when
32 programs issue a stop condition. When one does, a simple
34 `(handler-case (progn ,@forms) (stop (s) (declare (ignore s)) :stop)))
36 (defmacro with-stop-and-death-handler (&rest forms)
39 (stop (s) (declare (ignore s)) :stop)
40 (death (d) (declare (ignore d)) :death)))
42 (defmacro defcommand (name args docstring &rest body)
43 `(defun ,name ,args ,docstring ,@body :undefined))
45 (defstruct turtle who breed color heading xcor ycor (label "") label-color size shape own-vars)
46 (defstruct patch color xcor ycor own-vars turtles)
48 (defun agentset-list (agentset)
50 ((eql agentset :turtles) *turtles*)
51 ((eql agentset :patches) *patches*)
52 ((and (listp agentset) (eql :agentset (car agentset))) (cddr agentset))
53 ((find agentset *breeds* :key #'car)
54 (remove agentset *turtles* :key #'turtle-breed :test-not #'eql))
55 (t (error "Doesn't seem to be an agentset: ~A" agentset))))
57 (defun agentset-breed (agentset)
59 ((eql agentset :turtles) :turtles)
60 ((eql agentset :patches) :patches)
61 ((find agentset *breeds* :key #'car) agentset)
62 ((and (listp agentset) (eql :agentset (car agentset))) (second agentset))
63 (t (error "Doesn't seem to be an agentset: ~A" agentset))))
65 (defun list->agentset (list breed)
66 (append (list :agentset breed) list))
72 (find o *breeds* :key #'car)
73 (and (listp o) (eql :agentset (car o)))))
76 (or (turtle-p o) (patch-p o)))
78 (defun breed-p (breed)
79 (find breed *breeds* :key #'car))
81 (defun breed-default-shape (breed)
82 (second (find breed *breeds* :key #'car)))
84 (defsetf breed-default-shape (breed) (shape)
85 `(setf (second (find ,breed *breeds* :key #'car)) ,shape))