1 (in-package #:clnl-nvm)
3 ; Implementations of all the things the nvm can do.
10 VALUE: a NetLogo value
15 A command that prints the given NetLogo value to the command center.
17 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#show"
18 (format t "Showing: ~A~%" (dump-object value)))
20 (defun lookup-color (color)
21 "LOOKUP-COLOR COLOR => COLOR-NUMBER
25 COLOR: a symbol representing a color
26 COLOR-NUMBER: the NetLogo color integer
30 Returns the number used to represent colors in NetLogo.
32 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#Constants"
51 (defun create-turtle (&optional base-turtle)
53 ((new-turtle (make-turtle
54 :who (coerce *current-id* 'double-float)
55 :color (if base-turtle
56 (turtle-color base-turtle)
57 (coerce (+ 5 (* 10 (clnl-random:next-int 14))) 'double-float))
58 :heading (if base-turtle
59 (turtle-heading base-turtle)
60 (coerce (clnl-random:next-int 360) 'double-float))
61 :shape (breed-default-shape :turtles)
62 :xcor (if base-turtle (turtle-xcor base-turtle) 0d0)
63 :ycor (if base-turtle (turtle-ycor base-turtle) 0d0))))
64 (setf *turtles* (nconc *turtles* (list new-turtle)))
73 RESULT: undefined, commands don't return
77 The turtle or link dies
79 A dead agent ceases to exist. The effects of this include:
80 - The agent will not execute any further code.
81 - The agent will disappear from any agentsets it was in, reducing the size of those agentsets by one.
82 - Any variable that was storing the agent will now instead have nobody in it.
83 - If the dead agent was a turtle, every link connected to it also dies.
84 - If the observer was watching or following the agent, the observer's perspective resets.
86 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#die"
87 (when (not (turtle-p *self*)) (error "Gotta call die in turtle scope, dude (~A)" *self*))
88 (setf (turtle-who *self*) -1)
89 (setf *turtles* (remove *self* *turtles*)))
92 "PATCHES => ALL-PATCHES
96 ALL-PATCHES: a NetLogo agentset, all patches
100 Reports the agentset consisting of all the patches.
102 This agentset is special in that it represents the living patches
103 each time it's used, so changes depending on the state of the engine.
105 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#patches"
109 "TURTLES => ALL-TURTLES
111 ARGUMENTS AND VALUES:
113 ALL-TURTLES: a NetLogo agentset, all turtles
117 Reports the agentset consisting of all the turtles.
119 This agentset is special in that it represents the living turtles
120 each time it's used, so changes depending on the state of the engine.
122 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#turtles"
125 (defun ask (agent-or-agentset fn)
126 "ASK AGENT-OR-AGENTSET FN => RESULT
128 AGENT-OR-AGENTSET: AGENT | AGENTSET
130 ARGUMENTS AND VALUES:
132 FN: a function, run on each agent
133 RESULT: undefined, commands don't return
134 AGENT: a NetLogo agent
135 AGENTSET: a NetLogo agentset
139 ASK is equivalent to ask in NetLogo.
141 The specified AGENTSET or AGENT runs the given FN. In the case of an
142 AGENTSET, the order in which the agents are run is random each time,
143 and only agents that are in the set at the beginning of the call.
145 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#ask"
147 ((agentset-p agent-or-agentset)
149 ((iter (shufflerator (agentset-list agent-or-agentset))))
151 :for agent := (funcall iter)
153 :do (let ((*myself* *self*) (*self* agent)) (funcall fn)))))
154 ((agent-p agent-or-agentset)
155 (let ((*myself* *self*) (*self* agent-or-agentset)) (funcall fn)))
157 (error "Ask requires an agentset or agent but got: ~A" agent-or-agentset))))
159 (defun count (agentset)
162 ARGUMENTS AND VALUES:
164 AGENTSET: a NetLogo agentset
169 COUNT is equivalent to count in NetLogo. Returns N, the number of
172 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#count"
173 (coerce (length (agentset-list agentset)) 'double-float))
175 (defun of (fn agent-or-agentset)
176 "OF FN AGENT-OR-AGENTSET => RESULT
178 AGENT-OR-AGENTSET: AGENT | AGENTSET
179 RESULT: RESULT-LIST | RESULT-VALUE
181 ARGUMENTS AND VALUES:
183 FN: a function, run on each agent
184 AGENT: a NetLogo agent
185 AGENTSET: a NetLogo agentset
187 RESULT-VALUE: a single value
191 OF is equivalent to of in NetLogo.
193 The specified AGENTSET or AGENT runs the given FN. In the case of an
194 AGENTSET, the order in which the agents are run is random each time,
195 and only agents that are in the set at the beginning of the call.
197 RESULT-LIST is returned when the input is an AGENTSET, but RESULT-VALUE
198 is returned when only passed an AGENT.
200 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#of"
202 ((agentset-p agent-or-agentset)
204 ((iter (shufflerator (agentset-list agent-or-agentset))))
206 :for agent := (funcall iter)
208 :collect (let ((*myself* *self*) (*self* agent)) (funcall fn)))))
209 ((agent-p agent-or-agentset)
210 (let ((*myself* *self*) (*self* agent-or-agentset)) (funcall fn)))
212 (error "Of requires an agentset or agent but got: ~A" agent-or-agentset))))
214 (defun with (agentset fn)
215 "WITH AGENTSET FN => RESULT-AGENTSET
217 ARGUMENTS AND VALUES:
219 AGENTSET: a NetLogo agentset
220 FN: a boolean function, run on each agent to determine if included
221 RESULT-AGENTSET: an agentset of valid agents
225 WITH is equivalent to with in NetLogo.
227 Returns a new agentset containing only those agents that reported true
230 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#with"
234 (let ((*myself* *self*) (*self* agent)) (funcall fn)))
235 (agentset-list agentset))
236 (agentset-breed agentset)))
238 (defun shufflerator (agentset-list)
240 ((copy (copy-list agentset-list))
246 ((idx (when (< i (1- (length copy))) (+ i (clnl-random:next-int (- (length copy) i))))))
247 (when idx (setf agent (nth idx copy)))
248 (when idx (setf (nth idx copy) (nth i copy)))
250 (fetch) ; we pre-fetch because netlogo does, rng sync hype!
253 ((> i (length copy)) nil)
254 ((= i (length copy)) (incf i) (car (last copy)))
255 (t (let ((result agent)) (fetch) result)))))))
257 (defun random-float (n)
258 "RANDOM-FLOAT N => RANDOM-NUMBER
260 ARGUMENTS AND VALUES:
262 N: a double, the upper bound of the random float
263 RANDOM-NUMBER: a double, the random result
267 Returns a random number strictly closer to zero than N.
269 If number is positive, returns a random floating point number greater than
270 or equal to 0 but strictly less than number.
272 If number is negative, returns a random floating point number less than or equal
273 to 0, but strictly greater than number.
275 If number is zero, the result is always 0.
277 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#random-float"
278 (clnl-random:next-double n))
281 "RANDOM N => RANDOM-NUMBER
283 ARGUMENTS AND VALUES:
285 N: an integer, the upper bound of the random
286 RANDOM-NUMBER: an integer, the random result
290 Returns a random number strictly closer to zero than N.
292 If number is positive, returns a random integer greater than or equal to 0,
293 but strictly less than number.
295 If number is negative, returns a random integer less than or equal to 0,
296 but strictly greater than number.
298 If number is zero, the result is always 0.
300 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#random"
301 (coerce (clnl-random:next-long (truncate n)) 'double-float))
303 (defun random-xcor ()
304 "RANDOM-XCOR => RANDOM-NUMBER
306 ARGUMENTS AND VALUES:
308 RANDOM-NUMBER: a float, the random result
312 Returns a random floating point number in the allowable range of turtle
313 coordinates along the x axis.
315 These range from min-pxcor - 0.5 (inclusive) to max-pxcor + 0.5 (exclusive)
317 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#random-cor"
319 ((min (- (min-pxcor) 0.5d0))
320 (max (+ (max-pxcor) 0.5d0)))
321 (+ min (clnl-random:next-double (- max min)))))
323 (defun random-ycor ()
324 "RANDOM-YCOR => RANDOM-NUMBER
326 ARGUMENTS AND VALUES:
328 RANDOM-NUMBER: a float, the random result
332 Returns a random floating point number in the allowable range of turtle
333 coordinates along the y axis.
335 These range from min-pycor - 0.5 (inclusive) to max-pycor + 0.5 (exclusive)
337 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#random-cor"
339 ((min (- (min-pycor) 0.5d0))
340 (max (+ (max-pycor) 0.5d0)))
341 (+ min (clnl-random:next-double (- max min)))))
343 (defun one-of (list-or-agentset)
344 "ONE-OF LIST-OR-AGENTSET => RESULT
346 LIST-OR-AGENTSET: LIST | AGENTSET
347 RESULT: RANDOM-VALUE | RANDOM-AGENT | :nobody
349 ARGUMENTS AND VALUES:
352 AGENTSET: An agent set
353 RANDOM-VALUE: a value in LIST
354 RANDOM-AGENT: an agent if AGENTSET is non empty
358 From an AGENTSET, returns a RANDOM-AGENT. If the agentset is empty, returns :nobody.
359 From a list, returns a RANDOM-VALUE. If the list is empty, an error occurs.
361 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#one-of"
363 ((agentset-p list-or-agentset)
365 ((agentset-list (agentset-list list-or-agentset))
366 (length (length agentset-list)))
367 (if (zerop length) :nobody (nth (clnl-random:next-int length) agentset-list))))
368 ((listp list-or-agentset)
370 ((length (length list-or-agentset)))
372 (error "one-of requires a nonempty list")
373 (nth (clnl-random:next-int length) list-or-agentset))))
374 (t (error "one-of requires a list or agentset"))))
377 (when (not (turtle-p *self*)) (error "Gotta call jump in turtle scope, dude (~A)" *self*))
381 (+ (turtle-xcor *self*) (* n (using-cached-sin (turtle-heading *self*))))))
385 (+ (turtle-ycor *self*) (* n (using-cached-cos (turtle-heading *self*)))))))
390 ARGUMENTS AND VALUES:
398 Sets the x-coordinate and y-coordinate for the turle. Equivalent to
399 set xcor x set ycor y, except it happens in one step inside of two.
401 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#setxy"
402 (when (not (turtle-p *self*)) (error "Gotta call setxy in turtle scope, dude (~A)" *self*))
403 (setf (turtle-xcor *self*) (wrap-x *topology* x))
404 (setf (turtle-ycor *self*) (wrap-y *topology* y)))
406 (defun set-default-shape (breed shape)
407 "SET-DEFAULT-SHAPE BREED SHAPE => RESULT
409 ARGUMENTS AND VALUES:
417 Specifies a default initial shape for a BREED. When a turtle, or it changes breeds,
418 its shape is set to the given shape.
420 SET-DEFAULT-SHAPE doesn't affect existing agents, only agents you create afterwards.
422 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#set-default-shape"
423 (when (not (breed-p breed)) (error "Need a valid breed"))
424 (setf (breed-default-shape breed) shape))
429 ARGUMENTS AND VALUES:
431 N: a double, the amount the turtle moves forward
436 Moves the current turtle forward N steps, one step at a time.
438 This moves forward one at a time in order to make the view updates look
439 good in the case of a purposefully slow running instance. If the number
440 is negative, the turtle moves backward.
442 If the current agent is not a turtle, it raises an error.
444 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#forward"
445 (when (not (turtle-p *self*)) (error "Gotta call fd in turtle scope, dude (~A)" *self*))
449 ((< (abs i) 3.2e-15) nil)
450 ((< (abs i) 1d0) (jump i))
451 (t (jump (if (> i 0d0) 1d0 -1d0)) (internal (- i (if (> i 0d0) 1d0 -1d0)))))))
454 (defun turn-right (n)
455 "TURN-RIGHT N => RESULT
457 ARGUMENTS AND VALUES:
459 N: a double, the amount the turtle turns
464 The turtle turns right by number degrees. (If number is negative, it turns left.)
466 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#right"
467 (when (not (turtle-p *self*)) (error "Gotta call fd in turtle scope, dude (~A)" *self*))
469 ((new-heading (+ (turtle-heading *self*) n)))
470 (setf (turtle-heading *self*)
472 ((< new-heading 0) (+ (mod new-heading -360) 360))
473 ((>= new-heading 360) (mod new-heading 360))
477 "TURN-LEFT N => RESULT
479 ARGUMENTS AND VALUES:
481 N: a double, the amount the turtle turns
486 The turtle turns left by number degrees. (If number is negative, it turns right.)
488 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#right"
491 (defun create-turtles (n &optional fn)
492 "CREATE-TURTLES N &optional FN => RESULT
494 ARGUMENTS AND VALUES:
496 N: an integer, the numbers of turtles to create
497 FN: A function, applied to each turtle after creation
502 Creates number new turtles at the origin.
504 New turtles have random integer headings and the color is randomly selected
505 from the 14 primary colors. If a function is supplied, the new turtles
508 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#create-turtles"
510 ((new-turtles (loop :repeat n :collect (create-turtle))))
511 (when fn (ask (list->agentset new-turtles :turtles) fn))))
513 (defun hatch (n &optional fn)
514 "HATCH N &optional FN => RESULT
516 ARGUMENTS AND VALUES:
518 N: an integer, the numbers of turtles to hatch
519 FN: A function, applied to each turtle after creation
524 The turtle in *self* creates N new turtles. Each new turtle inherits of all its
525 variables, including its location, from self.
527 If FN is supplied, the new turtles immediately run it.
529 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#hatch"
530 (when (not (turtle-p *self*)) (error "Can only hatch from turtle scope"))
532 ((new-turtles (loop :repeat n :collect (create-turtle *self*))))
533 (when fn (ask (list->agentset new-turtles :turtles) fn))))
535 (defun reset-ticks ()
536 "RESET-TICKS => RESULT
538 ARGUMENTS AND VALUES:
544 Resets the tick counter to zero, sets up all plots, then updates all plots.
546 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#reset-ticks"
550 "RESET-TICKS => RESULT
552 ARGUMENTS AND VALUES:
558 Advances the tick counter by one and updates all plots.
560 If the tick counter has not been started yet with reset-ticks, an error results.
562 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#tick"
564 (when (not *ticks*) (error "reset-ticks must be called"))
568 "TICKS => CURRENT-TICKS
570 ARGUMENTS AND VALUES:
572 CURRENT-TICKS: A positiv double, representing the current number of ticks
576 Reports the current value of the tick counter. The result is always a number and never negative.
578 If the tick counter has not been started yet with reset-ticks, an error results.
580 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#ticks"
581 (when (not *ticks*) (error "reset-ticks must be called"))
584 (defun create-world (&key dims)
585 "CREATE-WORLD &key DIMS => RESULT
587 DIMS: (:xmin XMIN :xmax XMAX :ymin YMIN :ymax YMAX)
589 ARGUMENTS AND VALUES:
592 XMIN: An integer representing the minimum patch coord in X
593 XMAX: An integer representing the maximum patch coord in X
594 YMIN: An integer representing the minimum patch coord in Y
595 YMAX: An integer representing the maximum patch coord in Y
599 Initializes the world in the NVM.
601 This should be called before using the engine in any real capacity. If
602 called when an engine is already running, it may do somethign weird."
603 (setf *dimensions* dims)
604 (setf *breeds* (list (list :turtles "default")))
608 :for y :from (max-pycor) :downto (min-pycor)
610 :for x :from (min-pxcor) :to (max-pxcor)
612 :xcor (coerce x 'double-float)
613 :ycor (coerce y 'double-float)
616 (setf *current-id* 0))
618 ; These match netlogo's dump
619 (defgeneric dump-object (o))
621 (defmethod dump-object ((n double-float))
622 (multiple-value-bind (int rem) (floor n)
624 (format nil "~A" int)
626 ((output (format nil "~D" n)))
627 ; Someday we'll have d<posint>, but this is not that day!
628 (cl-ppcre:regex-replace "d-" (cl-ppcre:regex-replace "d0" output "") "E-")))))
630 (defmethod dump-object ((o string)) (format nil "~A" (cl-ppcre:regex-replace-all "\"" (format nil "~S" o) "\"\"")))
632 (defmethod dump-object ((o (eql t))) "true")
633 (defmethod dump-object ((o (eql nil))) "false")
635 (defmethod dump-object ((o list))
637 ((agentset-p o) (format nil "(agentset, ~A ~A)" (dump-object (count o)) (string-downcase (agentset-breed o))))
638 (t (format nil "[~{~A~^ ~}]" (mapcar #'dump-object o)))))
640 (defmethod dump-object ((o patch))
641 (format nil "(patch ~A ~A)" (dump-object (patch-xcor o)) (dump-object (patch-ycor o))))
643 (defmethod dump-object ((o turtle)) (format nil "(turtle ~A)" (dump-object (turtle-who o))))
644 (defmethod dump-object ((o (eql :nobody))) (format nil "nobody"))
646 (defun current-state ()
647 "CURRENT-STATE => WORLD-STATE
649 ARGUMENTS AND VALUES:
651 WORLD-STATE: A list, the current state of the whole world
655 Dumps out the state of the world.
657 This is useful for visualizations and also storing in a common lisp
658 data structure for easy usage in a common lisp instance. It's preferable
659 to use this when working with the nvm than the output done by export-world.
661 Currently this only dumps out turtle and patch information.
663 This is called CURRENT-STATE because export-world is an actual primitive
669 :color (turtle-color turtle)
670 :xcor (turtle-xcor turtle)
671 :ycor (turtle-ycor turtle)
672 :heading (turtle-heading turtle)
673 :size (turtle-size turtle)))
678 :color (patch-color patch)
679 :xcor (patch-xcor patch)
680 :ycor (patch-ycor patch)))
683 (defun export-turtles ()
688 "\"who\",\"color\",\"heading\",\"xcor\",\"ycor\",\"shape\",\"label\",\"label-color\","
689 "\"breed\",\"hidden?\",\"size\",\"pen-size\",\"pen-mode\""))
693 "\"~A\",\"~A\",\"~A\",\"~A\",\"~A\",\"~A\",\"~A\",\"~A\",\"{all-turtles}\",\"false\",\"~A\",~A"
694 (dump-object (turtle-who turtle))
695 (dump-object (turtle-color turtle))
696 (dump-object (turtle-heading turtle))
697 (dump-object (turtle-xcor turtle))
698 (dump-object (turtle-ycor turtle))
699 (dump-object (turtle-shape turtle))
700 (dump-object (turtle-label turtle))
701 (dump-object (turtle-label-color turtle))
702 (dump-object (turtle-size turtle))
703 "\"1\",\"\"\"up\"\"\""))
706 (defun export-patches ()
710 "\"pxcor\",\"pycor\",\"pcolor\",\"plabel\",\"plabel-color\"")
714 "\"~A\",\"~A\",\"~A\",\"\"\"\"\"\",\"9.9\""
715 (dump-object (patch-xcor patch))
716 (dump-object (patch-ycor patch))
717 (dump-object (patch-color patch))))
720 (defun export-world ()
721 "EXPORT-WORLD => WORLD-CSV
723 ARGUMENTS AND VALUES:
725 WORLD-CSV: A string, the csv of the world
729 Dumps out a csv matching NetLogo's export world.
731 This is useful for serializing the current state of the engine in order
732 to compare against NetLogo or to reimport later. Contains everything needed
733 to boot up a NetLogo instance in the exact same state."
734 (format nil "~{~A~%~}"
736 (format nil "~S" "RANDOM STATE")
737 (format nil "~S" (clnl-random:export))
739 (format nil "~S" "GLOBALS")
741 "\"min-pxcor\",\"max-pxcor\",\"min-pycor\",\"max-pycor\",\"perspective\",\"subject\","
742 "\"nextIndex\",\"directed-links\",\"ticks\",")
743 (format nil "\"~A\",\"~A\",\"~A\",\"~A\",\"0\",\"nobody\",\"~A\",\"\"\"NEITHER\"\"\",\"-1\""
744 (min-pxcor) (max-pxcor) (min-pycor) (max-pycor) *current-id*)
746 (format nil "~{~A~^~%~}" (export-turtles))
748 (format nil "~{~A~^~%~}" (export-patches))
750 (format nil "~S" "LINKS")
751 "\"end1\",\"end2\",\"color\",\"label\",\"label-color\",\"hidden?\",\"breed\",\"thickness\",\"shape\",\"tie-mode\""