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 ()
53 ((new-turtle (make-turtle
54 :who (coerce *current-id* 'double-float)
55 :color (coerce (+ 5 (* 10 (clnl-random:next-int 14))) 'double-float)
56 :heading (coerce (clnl-random:next-int 360) 'double-float)
59 (setf *turtles* (nconc *turtles* (list new-turtle)))
68 RESULT: undefined, commands don't return
72 The turtle or link dies
74 A dead agent ceases to exist. The effects of this include:
75 - The agent will not execute any further code.
76 - The agent will disappear from any agentsets it was in, reducing the size of those agentsets by one.
77 - Any variable that was storing the agent will now instead have nobody in it.
78 - If the dead agent was a turtle, every link connected to it also dies.
79 - If the observer was watching or following the agent, the observer's perspective resets.
81 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#die"
82 (when (not (turtle-p *self*)) (error "Gotta call die in turtle scope, dude (~A)" *self*))
83 (setf (turtle-who *self*) -1)
84 (setf *turtles* (remove *self* *turtles*)))
87 "PATCHES => ALL-PATCHES
91 ALL-PATCHES: a NetLogo agentset, all patches
95 Reports the agentset consisting of all the patches.
97 This agentset is special in that it represents the living patches
98 each time it's used, so changes depending on the state of the engine.
100 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#patches"
104 "TURTLES => ALL-TURTLES
106 ARGUMENTS AND VALUES:
108 ALL-TURTLES: a NetLogo agentset, all turtles
112 Reports the agentset consisting of all the turtles.
114 This agentset is special in that it represents the living turtles
115 each time it's used, so changes depending on the state of the engine.
117 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#turtles"
120 (defun ask (agent-or-agentset fn)
121 "ASK AGENT-OR-AGENTSET FN => RESULT
123 AGENT-OR-AGENTSET: AGENT | AGENTSET
125 ARGUMENTS AND VALUES:
127 FN: a function, run on each agent
128 RESULT: undefined, commands don't return
129 AGENT: a NetLogo agent
130 AGENTSET: a NetLogo agentset
134 ASK is equivalent to ask in NetLogo.
136 The specified AGENTSET or AGENT runs the given FN. In the case of an
137 AGENTSET, the order in which the agents are run is random each time,
138 and only agents that are in the set at the beginning of the call.
140 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#ask"
142 ((agentset-p agent-or-agentset)
144 ((iter (shufflerator (agentset-list agent-or-agentset))))
146 :for agent := (funcall iter)
148 :do (let ((*myself* *self*) (*self* agent)) (funcall fn)))))
149 ((agent-p agent-or-agentset)
150 (let ((*myself* *self*) (*self* agent-or-agentset)) (funcall fn)))
152 (error "Ask requires an agentset or agent but got: ~A" agent-or-agentset))))
154 (defun count (agentset)
157 ARGUMENTS AND VALUES:
159 AGENTSET: a NetLogo agentset
164 COUNT is equivalent to count in NetLogo. Returns N, the number of
167 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#count"
168 (coerce (length (agentset-list agentset)) 'double-float))
170 (defun of (fn agent-or-agentset)
171 "OF FN AGENT-OR-AGENTSET => RESULT
173 AGENT-OR-AGENTSET: AGENT | AGENTSET
174 RESULT: RESULT-LIST | RESULT-VALUE
176 ARGUMENTS AND VALUES:
178 FN: a function, run on each agent
179 AGENT: a NetLogo agent
180 AGENTSET: a NetLogo agentset
182 RESULT-VALUE: a single value
186 OF is equivalent to of in NetLogo.
188 The specified AGENTSET or AGENT runs the given FN. In the case of an
189 AGENTSET, the order in which the agents are run is random each time,
190 and only agents that are in the set at the beginning of the call.
192 RESULT-LIST is returned when the input is an AGENTSET, but RESULT-VALUE
193 is returned when only passed an AGENT.
195 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#of"
197 ((agentset-p agent-or-agentset)
199 ((iter (shufflerator (agentset-list agent-or-agentset))))
201 :for agent := (funcall iter)
203 :collect (let ((*myself* *self*) (*self* agent)) (funcall fn)))))
204 ((agent-p agent-or-agentset)
205 (let ((*myself* *self*) (*self* agent-or-agentset)) (funcall fn)))
207 (error "Of requires an agentset or agent but got: ~A" agent-or-agentset))))
209 (defun shufflerator (agentset-list)
211 ((copy (copy-list agentset-list))
217 ((idx (when (< i (1- (length copy))) (+ i (clnl-random:next-int (- (length copy) i))))))
218 (when idx (setf agent (nth idx copy)))
219 (when idx (setf (nth idx copy) (nth i copy)))
221 (fetch) ; we pre-fetch because netlogo does, rng sync hype!
224 ((> i (length copy)) nil)
225 ((= i (length copy)) (incf i) (car (last copy)))
226 (t (let ((result agent)) (fetch) result)))))))
228 (defun random-float (n)
229 "RANDOM-FLOAT N => RANDOM-NUMBER
231 ARGUMENTS AND VALUES:
233 N: a double, the upper bound of the random float
234 RANDOM-NUMBER: a double, the random result
238 Returns a random number strictly closer to zero than N.
240 If number is positive, returns a random floating point number greater than
241 or equal to 0 but strictly less than number.
243 If number is negative, returns a random floating point number less than or equal
244 to 0, but strictly greater than number.
246 If number is zero, the result is always 0.
248 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#random-float"
249 (clnl-random:next-double n))
252 "RANDOM N => RANDOM-NUMBER
254 ARGUMENTS AND VALUES:
256 N: an integer, the upper bound of the random
257 RANDOM-NUMBER: an integer, the random result
261 Returns a random number strictly closer to zero than N.
263 If number is positive, returns a random integer greater than or equal to 0,
264 but strictly less than number.
266 If number is negative, returns a random integer less than or equal to 0,
267 but strictly greater than number.
269 If number is zero, the result is always 0.
271 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#random"
272 (coerce (clnl-random:next-long (truncate n)) 'double-float))
274 (defun random-xcor ()
275 "RANDOM-XCOR => RANDOM-NUMBER
277 ARGUMENTS AND VALUES:
279 RANDOM-NUMBER: a float, the random result
283 Returns a random floating point number in the allowable range of turtle
284 coordinates along the x axis.
286 These range from min-pxcor - 0.5 (inclusive) to max-pxcor + 0.5 (exclusive)
288 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#random-cor"
290 ((min (- (min-pxcor) 0.5d0))
291 (max (+ (max-pxcor) 0.5d0)))
292 (+ min (clnl-random:next-double (- max min)))))
294 (defun random-ycor ()
295 "RANDOM-YCOR => RANDOM-NUMBER
297 ARGUMENTS AND VALUES:
299 RANDOM-NUMBER: a float, the random result
303 Returns a random floating point number in the allowable range of turtle
304 coordinates along the y axis.
306 These range from min-pycor - 0.5 (inclusive) to max-pycor + 0.5 (exclusive)
308 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#random-cor"
310 ((min (- (min-pycor) 0.5d0))
311 (max (+ (max-pycor) 0.5d0)))
312 (+ min (clnl-random:next-double (- max min)))))
314 (defun one-of (list-or-agentset)
315 "ONE-OF LIST-OR-AGENTSET => RESULT
317 LIST-OR-AGENTSET: LIST | AGENTSET
318 RESULT: RANDOM-VALUE | RANDOM-AGENT | :nobody
320 ARGUMENTS AND VALUES:
323 AGENTSET: An agent set
324 RANDOM-VALUE: a value in LIST
325 RANDOM-AGENT: an agent if AGENTSET is non empty
329 From an AGENTSET, returns a RANDOM-AGENT. If the agentset is empty, returns :nobody.
330 From a list, returns a RANDOM-VALUE. If the list is empty, an error occurs.
332 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#one-of"
334 ((agentset-p list-or-agentset)
336 ((agentset-list (agentset-list list-or-agentset))
337 (length (length agentset-list)))
338 (if (zerop length) :nobody (nth (clnl-random:next-int length) agentset-list))))
339 ((listp list-or-agentset)
341 ((length (length list-or-agentset)))
343 (error "one-of requires a nonempty list")
344 (nth (clnl-random:next-int length) list-or-agentset))))
345 (t (error "one-of requires a list or agentset"))))
348 (when (not (turtle-p *self*)) (error "Gotta call jump in turtle scope, dude (~A)" *self*))
352 (+ (turtle-xcor *self*) (* n (using-cached-sin (turtle-heading *self*))))))
356 (+ (turtle-ycor *self*) (* n (using-cached-cos (turtle-heading *self*)))))))
361 ARGUMENTS AND VALUES:
369 Sets the x-coordinate and y-coordinate for the turle. Equivalent to
370 set xcor x set ycor y, except it happens in one step inside of two.
372 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#setxy"
373 (when (not (turtle-p *self*)) (error "Gotta call setxy in turtle scope, dude (~A)" *self*))
374 (setf (turtle-xcor *self*) (wrap-x *topology* x))
375 (setf (turtle-ycor *self*) (wrap-y *topology* y)))
380 ARGUMENTS AND VALUES:
382 N: a double, the amount the turtle moves forward
387 Moves the current turtle forward N steps, one step at a time.
389 This moves forward one at a time in order to make the view updates look
390 good in the case of a purposefully slow running instance. If the number
391 is negative, the turtle moves backward.
393 If the current agent is not a turtle, it raises an error.
395 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#forward"
396 (when (not (turtle-p *self*)) (error "Gotta call fd in turtle scope, dude (~A)" *self*))
400 ((< (abs i) 3.2e-15) nil)
401 ((< (abs i) 1d0) (jump i))
402 (t (jump (if (> i 0d0) 1d0 -1d0)) (internal (- i (if (> i 0d0) 1d0 -1d0)))))))
405 (defun turn-right (n)
406 "TURN-RIGHT N => RESULT
408 ARGUMENTS AND VALUES:
410 N: a double, the amount the turtle turns
415 The turtle turns right by number degrees. (If number is negative, it turns left.)
417 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#right"
418 (when (not (turtle-p *self*)) (error "Gotta call fd in turtle scope, dude (~A)" *self*))
420 ((new-heading (+ (turtle-heading *self*) n)))
421 (setf (turtle-heading *self*)
423 ((< new-heading 0) (+ (mod new-heading -360) 360))
424 ((>= new-heading 360) (mod new-heading 360))
428 "TURN-LEFT N => RESULT
430 ARGUMENTS AND VALUES:
432 N: a double, the amount the turtle turns
437 The turtle turns left by number degrees. (If number is negative, it turns right.)
439 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#right"
442 (defun create-turtles (n &optional fn)
443 "CREATE-TURTLES N &optional FN => RESULT
445 ARGUMENTS AND VALUES:
447 N: an integer, the numbers of turtles to create
448 FN: A function, applied to each turtle after creation
453 Creates number new turtles at the origin.
455 New turtles have random integer headings and the color is randomly selected
456 from the 14 primary colors. If a function is supplied, the new turtles
459 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#create-turtles"
461 ((new-turtles (loop :repeat n :collect (create-turtle))))
462 (when fn (ask (list->agentset new-turtles) fn))))
464 (defun reset-ticks ()
465 "RESET-TICKS => RESULT
467 ARGUMENTS AND VALUES:
473 Resets the tick counter to zero, sets up all plots, then updates all plots.
475 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#reset-ticks"
479 "RESET-TICKS => RESULT
481 ARGUMENTS AND VALUES:
487 Advances the tick counter by one and updates all plots.
489 If the tick counter has not been started yet with reset-ticks, an error results.
491 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#tick"
493 (when (not *ticks*) (error "reset-ticks must be called"))
497 "TICKS => CURRENT-TICKS
499 ARGUMENTS AND VALUES:
501 CURRENT-TICKS: A positiv double, representing the current number of ticks
505 Reports the current value of the tick counter. The result is always a number and never negative.
507 If the tick counter has not been started yet with reset-ticks, an error results.
509 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#ticks"
510 (when (not *ticks*) (error "reset-ticks must be called"))
513 (defun create-world (&key dims)
514 "CREATE-WORLD &key DIMS => RESULT
516 DIMS: (:xmin XMIN :xmax XMAX :ymin YMIN :ymax YMAX)
518 ARGUMENTS AND VALUES:
521 XMIN: An integer representing the minimum patch coord in X
522 XMAX: An integer representing the maximum patch coord in X
523 YMIN: An integer representing the minimum patch coord in Y
524 YMAX: An integer representing the maximum patch coord in Y
528 Initializes the world in the NVM.
530 This should be called before using the engine in any real capacity. If
531 called when an engine is already running, it may do somethign weird."
532 (setf *dimensions* dims)
536 :for y :from (max-pycor) :downto (min-pycor)
538 :for x :from (min-pxcor) :to (max-pxcor)
540 :xcor (coerce x 'double-float)
541 :ycor (coerce y 'double-float)
544 (setf *current-id* 0))
546 ; These match netlogo's dump
547 (defgeneric dump-object (o))
549 (defmethod dump-object ((n double-float))
550 (multiple-value-bind (int rem) (floor n)
552 (format nil "~A" int)
554 ((output (format nil "~D" n)))
555 ; Someday we'll have d<posint>, but this is not that day!
556 (cl-ppcre:regex-replace "d-" (cl-ppcre:regex-replace "d0" output "") "E-")))))
558 (defmethod dump-object ((o string)) (format nil "~A" (cl-ppcre:regex-replace-all "\"" (format nil "~S" o) "\"\"")))
560 (defmethod dump-object ((o (eql t))) "true")
561 (defmethod dump-object ((o (eql nil))) "false")
563 (defmethod dump-object ((o list)) (format nil "[~{~A~^ ~}]" (mapcar #'dump-object o)))
565 (defmethod dump-object ((o patch))
566 (format nil "(patch ~A ~A)" (dump-object (patch-xcor o)) (dump-object (patch-ycor o))))
568 (defmethod dump-object ((o turtle)) (format nil "(turtle ~A)" (dump-object (turtle-who o))))
569 (defmethod dump-object ((o (eql :nobody))) (format nil "nobody"))
571 (defun current-state ()
572 "CURRENT-STATE => WORLD-STATE
574 ARGUMENTS AND VALUES:
576 WORLD-STATE: A list, the current state of the whole world
580 Dumps out the state of the world.
582 This is useful for visualizations and also storing in a common lisp
583 data structure for easy usage in a common lisp instance. It's preferable
584 to use this when working with the nvm than the output done by export-world.
586 Currently this only dumps out turtle and patch information.
588 This is called CURRENT-STATE because export-world is an actual primitive
594 :color (turtle-color turtle)
595 :xcor (turtle-xcor turtle)
596 :ycor (turtle-ycor turtle)
597 :heading (turtle-heading turtle)
598 :size (turtle-size turtle)))
603 :color (patch-color patch)
604 :xcor (patch-xcor patch)
605 :ycor (patch-ycor patch)))
608 (defun export-turtles ()
613 "\"who\",\"color\",\"heading\",\"xcor\",\"ycor\",\"shape\",\"label\",\"label-color\","
614 "\"breed\",\"hidden?\",\"size\",\"pen-size\",\"pen-mode\""))
618 "\"~A\",\"~A\",\"~A\",\"~A\",\"~A\",\"\"\"default\"\"\",\"~A\",\"~A\",\"{all-turtles}\",\"false\",\"~A\",~A"
619 (dump-object (turtle-who turtle))
620 (dump-object (turtle-color turtle))
621 (dump-object (turtle-heading turtle))
622 (dump-object (turtle-xcor turtle))
623 (dump-object (turtle-ycor turtle))
624 (dump-object (turtle-label turtle))
625 (dump-object (turtle-label-color turtle))
626 (dump-object (turtle-size turtle))
627 "\"1\",\"\"\"up\"\"\""))
630 (defun export-patches ()
634 "\"pxcor\",\"pycor\",\"pcolor\",\"plabel\",\"plabel-color\"")
638 "\"~A\",\"~A\",\"~A\",\"\"\"\"\"\",\"9.9\""
639 (dump-object (patch-xcor patch))
640 (dump-object (patch-ycor patch))
641 (dump-object (patch-color patch))))
644 (defun export-world ()
645 "EXPORT-WORLD => WORLD-CSV
647 ARGUMENTS AND VALUES:
649 WORLD-CSV: A string, the csv of the world
653 Dumps out a csv matching NetLogo's export world.
655 This is useful for serializing the current state of the engine in order
656 to compare against NetLogo or to reimport later. Contains everything needed
657 to boot up a NetLogo instance in the exact same state."
658 (format nil "~{~A~%~}"
660 (format nil "~S" "RANDOM STATE")
661 (format nil "~S" (clnl-random:export))
663 (format nil "~S" "GLOBALS")
665 "\"min-pxcor\",\"max-pxcor\",\"min-pycor\",\"max-pycor\",\"perspective\",\"subject\","
666 "\"nextIndex\",\"directed-links\",\"ticks\",")
667 (format nil "\"~A\",\"~A\",\"~A\",\"~A\",\"0\",\"nobody\",\"~A\",\"\"\"NEITHER\"\"\",\"-1\""
668 (min-pxcor) (max-pxcor) (min-pycor) (max-pycor) *current-id*)
670 (format nil "~{~A~^~%~}" (export-turtles))
672 (format nil "~{~A~^~%~}" (export-patches))
674 (format nil "~S" "LINKS")
675 "\"end1\",\"end2\",\"color\",\"label\",\"label-color\",\"hidden?\",\"breed\",\"thickness\",\"shape\",\"tie-mode\""