X-Git-Url: https://code.consxy.com/gitweb/gitweb.cgi?a=blobdiff_plain;f=src%2Fmain%2Fnvm%2Fnvm.lisp;h=67b3ec4855117f4b5d7e904c3335d1fae0908d28;hb=083d6c212147c2242f0513924e931bcdafd641c9;hp=31d2f31ae4287af0e806e3b5f0a182d4cd11c6aa;hpb=5a7fb5cf4e703d4cb8d6b89052265368323edd94;p=clnl diff --git a/src/main/nvm/nvm.lisp b/src/main/nvm/nvm.lisp index 31d2f31..67b3ec4 100644 --- a/src/main/nvm/nvm.lisp +++ b/src/main/nvm/nvm.lisp @@ -49,18 +49,16 @@ DESCRIPTION: (:pink 135d0))) (defun create-turtle () - (setf - *turtles* - (nconc - *turtles* - (list - (make-turtle - :who (coerce *current-id* 'double-float) - :color (coerce (+ 5 (* 10 (clnl-random:next-int 14))) 'double-float) - :heading (coerce (clnl-random:next-int 360) 'double-float) - :xcor 0d0 - :ycor 0d0)))) - (incf *current-id*)) + (let + ((new-turtle (make-turtle + :who (coerce *current-id* 'double-float) + :color (coerce (+ 5 (* 10 (clnl-random:next-int 14))) 'double-float) + :heading (coerce (clnl-random:next-int 360) 'double-float) + :xcor 0d0 + :ycor 0d0))) + (setf *turtles* (nconc *turtles* (list new-turtle))) + (incf *current-id*) + new-turtle)) (defun die () "DIE => RESULT @@ -208,6 +206,30 @@ DESCRIPTION: (t (error "Of requires an agentset or agent but got: ~A" agent-or-agentset)))) +(defun with (agentset fn) + "WITH AGENTSET FN => RESULT-AGENTSET + +ARGUMENTS AND VALUES: + + AGENTSET: a NetLogo agentset + FN: a boolean function, run on each agent to determine if included + RESULT-AGENTSET: an agentset of valid agents + +DESCRIPTION: + + WITH is equivalent to with in NetLogo. + + Returns a new agentset containing only those agents that reported true + when FN is called. + + See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#with" + (list->agentset + (remove-if-not + (lambda (agent) + (let ((*myself* *self*) (*self* agent)) (funcall fn))) + (agentset-list agentset)) + (agentset-breed agentset))) + (defun shufflerator (agentset-list) (let ((copy (copy-list agentset-list)) @@ -441,12 +463,13 @@ DESCRIPTION: See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#right" (turn-right (- n))) -(defun create-turtles (n) - "CREATE-TURTLES N => RESULT +(defun create-turtles (n &optional fn) + "CREATE-TURTLES N &optional FN => RESULT ARGUMENTS AND VALUES: N: an integer, the numbers of turtles to create + FN: A function, applied to each turtle after creation RESULT: undefined DESCRIPTION: @@ -454,11 +477,13 @@ DESCRIPTION: Creates number new turtles at the origin. New turtles have random integer headings and the color is randomly selected - from the 14 primary colors. If commands are supplied, the new turtles - immediately run them (unimplemented). + from the 14 primary colors. If a function is supplied, the new turtles + immediately run it. See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#create-turtles" - (loop :for i :from 1 :to n :do (create-turtle))) + (let + ((new-turtles (loop :repeat n :collect (create-turtle)))) + (when fn (ask (list->agentset new-turtles :turtles) fn)))) (defun reset-ticks () "RESET-TICKS => RESULT @@ -559,7 +584,10 @@ DESCRIPTION: (defmethod dump-object ((o (eql t))) "true") (defmethod dump-object ((o (eql nil))) "false") -(defmethod dump-object ((o list)) (format nil "[~{~A~^ ~}]" (mapcar #'dump-object o))) +(defmethod dump-object ((o list)) + (cond + ((agentset-p o) (format nil "(agentset, ~A ~A)" (dump-object (count o)) (string-downcase (agentset-breed o)))) + (t (format nil "[~{~A~^ ~}]" (mapcar #'dump-object o))))) (defmethod dump-object ((o patch)) (format nil "(patch ~A ~A)" (dump-object (patch-xcor o)) (dump-object (patch-ycor o))))