X-Git-Url: https://code.consxy.com/gitweb/gitweb.cgi?p=clnl;a=blobdiff_plain;f=src%2Fmain%2Fnvm%2Fbase.lisp;h=1a80dc3fa87deea9f4bed2cb8432e232b4fed5e7;hp=9c49c9900bbd35d308e837fbeabc47dbb8a44193;hb=89cb3482de1fefc2d8e5f67e1e04a0790e8cd243;hpb=ef8590acac123b880b2719eaac691af310262cca diff --git a/src/main/nvm/base.lisp b/src/main/nvm/base.lisp index 9c49c99..1a80dc3 100644 --- a/src/main/nvm/base.lisp +++ b/src/main/nvm/base.lisp @@ -3,15 +3,82 @@ (defvar *current-id* 0) (defvar *turtles* nil) +(defvar *turtles-own-vars* nil) +(defvar *patches-own-vars* nil) (defvar *patches* nil) (defvar *myself* nil) (defvar *self* nil) (defvar *dimensions* nil) +(defvar *globals* nil) (defvar *topology* :torus) (defvar *ticks* nil) +(defvar *breeds* nil) -(defstruct turtle who color heading xcor ycor (label "") (label-color 9.9d0) (size 1d0)) -(defstruct patch color xcor ycor) +(define-condition stop nil nil) +(define-condition death nil nil) -(defun agent-set-list (agent-set) - agent-set) +(defmacro with-stop-handler (&rest forms) + "MACRO WITH-STOP-HANDLER &rest FORMS => HANDLED-FORM + +ARGUMENTS AND VALUES: + + FORMS: body to be handled + HANDLED-FORM: body with handling + +DESCRIPTION: + + WITH-STOP-HANDLER is a convenience macro to handle when + programs issue a stop condition. When one does, a simple + :stop is returned." + `(handler-case (progn ,@forms) (stop (s) (declare (ignore s)) :stop))) + +(defmacro with-stop-and-death-handler (&rest forms) + `(handler-case + (progn ,@forms) + (stop (s) (declare (ignore s)) :stop) + (death (d) (declare (ignore d)) :death))) + +(defmacro defcommand (name args docstring &rest body) + `(defun ,name ,args ,docstring ,@body :undefined)) + +(defstruct turtle who breed color heading xcor ycor (label "") label-color size shape own-vars) +(defstruct patch color xcor ycor own-vars turtles) + +(defun agentset-list (agentset) + (cond + ((eql agentset :turtles) *turtles*) + ((eql agentset :patches) *patches*) + ((and (listp agentset) (eql :agentset (car agentset))) (cddr agentset)) + ((find agentset *breeds* :key #'car) + (remove agentset *turtles* :key #'turtle-breed :test-not #'eql)) + (t (error "Doesn't seem to be an agentset: ~A" agentset)))) + +(defun agentset-breed (agentset) + (cond + ((eql agentset :turtles) :turtles) + ((eql agentset :patches) :patches) + ((find agentset *breeds* :key #'car) agentset) + ((and (listp agentset) (eql :agentset (car agentset))) (second agentset)) + (t (error "Doesn't seem to be an agentset: ~A" agentset)))) + +(defun list->agentset (list breed) + (append (list :agentset breed) list)) + +(defun agentset-p (o) + (or + (eql o :turtles) + (eql o :patches) + (find o *breeds* :key #'car) + (and (listp o) (eql :agentset (car o))))) + +(defun agent-p (o) + (or (turtle-p o) (patch-p o))) + +(defun breed-p (breed) + (find breed *breeds* :key #'car)) + +(defun breed-default-shape (breed) + (second (find breed *breeds* :key #'car))) + +(defsetf breed-default-shape (breed) (shape) + `(setf (second (find ,breed *breeds* :key #'car)) ,shape))