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))
178 ARGUMENTS AND VALUES:
184 Clears ticks, turtles, patches, globals (unimplemented).
186 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#clear-all"
194 ARGUMENTS AND VALUES:
200 As of yet, this does nothing. A placeholder method for forced dipslay
201 updates from the engine.
203 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#display"
206 (defun of (fn agent-or-agentset)
207 "OF FN AGENT-OR-AGENTSET => RESULT
209 AGENT-OR-AGENTSET: AGENT | AGENTSET
210 RESULT: RESULT-LIST | RESULT-VALUE
212 ARGUMENTS AND VALUES:
214 FN: a function, run on each agent
215 AGENT: a NetLogo agent
216 AGENTSET: a NetLogo agentset
218 RESULT-VALUE: a single value
222 OF is equivalent to of in NetLogo.
224 The specified AGENTSET or AGENT runs the given FN. In the case of an
225 AGENTSET, the order in which the agents are run is random each time,
226 and only agents that are in the set at the beginning of the call.
228 RESULT-LIST is returned when the input is an AGENTSET, but RESULT-VALUE
229 is returned when only passed an AGENT.
231 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#of"
233 ((agentset-p agent-or-agentset)
235 ((iter (shufflerator (agentset-list agent-or-agentset))))
237 :for agent := (funcall iter)
239 :collect (let ((*myself* *self*) (*self* agent)) (funcall fn)))))
240 ((agent-p agent-or-agentset)
241 (let ((*myself* *self*) (*self* agent-or-agentset)) (funcall fn)))
243 (error "Of requires an agentset or agent but got: ~A" agent-or-agentset))))
245 (defun with (agentset fn)
246 "WITH AGENTSET FN => RESULT-AGENTSET
248 ARGUMENTS AND VALUES:
250 AGENTSET: a NetLogo agentset
251 FN: a boolean function, run on each agent to determine if included
252 RESULT-AGENTSET: an agentset of valid agents
256 WITH is equivalent to with in NetLogo.
258 Returns a new agentset containing only those agents that reported true
261 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#with"
265 (let ((*myself* *self*) (*self* agent)) (funcall fn)))
266 (agentset-list agentset))
267 (agentset-breed agentset)))
269 (defun shufflerator (agentset-list)
271 ((copy (copy-list agentset-list))
277 ((idx (when (< i (1- (length copy))) (+ i (clnl-random:next-int (- (length copy) i))))))
278 (when idx (setf agent (nth idx copy)))
279 (when idx (setf (nth idx copy) (nth i copy)))
281 (fetch) ; we pre-fetch because netlogo does, rng sync hype!
284 ((> i (length copy)) nil)
285 ((= i (length copy)) (incf i) (car (last copy)))
286 (t (let ((result agent)) (fetch) result)))))))
288 (defun random-float (n)
289 "RANDOM-FLOAT N => RANDOM-NUMBER
291 ARGUMENTS AND VALUES:
293 N: a double, the upper bound of the random float
294 RANDOM-NUMBER: a double, the random result
298 Returns a random number strictly closer to zero than N.
300 If number is positive, returns a random floating point number greater than
301 or equal to 0 but strictly less than number.
303 If number is negative, returns a random floating point number less than or equal
304 to 0, but strictly greater than number.
306 If number is zero, the result is always 0.
308 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#random-float"
309 (clnl-random:next-double n))
312 "RANDOM N => RANDOM-NUMBER
314 ARGUMENTS AND VALUES:
316 N: an integer, the upper bound of the random
317 RANDOM-NUMBER: an integer, the random result
321 Returns a random number strictly closer to zero than N.
323 If number is positive, returns a random integer greater than or equal to 0,
324 but strictly less than number.
326 If number is negative, returns a random integer less than or equal to 0,
327 but strictly greater than number.
329 If number is zero, the result is always 0.
331 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#random"
332 (coerce (clnl-random:next-long (truncate n)) 'double-float))
334 (defun random-xcor ()
335 "RANDOM-XCOR => RANDOM-NUMBER
337 ARGUMENTS AND VALUES:
339 RANDOM-NUMBER: a float, the random result
343 Returns a random floating point number in the allowable range of turtle
344 coordinates along the x axis.
346 These range from min-pxcor - 0.5 (inclusive) to max-pxcor + 0.5 (exclusive)
348 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#random-cor"
350 ((min (- (min-pxcor) 0.5d0))
351 (max (+ (max-pxcor) 0.5d0)))
352 (+ min (clnl-random:next-double (- max min)))))
354 (defun random-ycor ()
355 "RANDOM-YCOR => RANDOM-NUMBER
357 ARGUMENTS AND VALUES:
359 RANDOM-NUMBER: a float, the random result
363 Returns a random floating point number in the allowable range of turtle
364 coordinates along the y axis.
366 These range from min-pycor - 0.5 (inclusive) to max-pycor + 0.5 (exclusive)
368 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#random-cor"
370 ((min (- (min-pycor) 0.5d0))
371 (max (+ (max-pycor) 0.5d0)))
372 (+ min (clnl-random:next-double (- max min)))))
374 (defun one-of (list-or-agentset)
375 "ONE-OF LIST-OR-AGENTSET => RESULT
377 LIST-OR-AGENTSET: LIST | AGENTSET
378 RESULT: RANDOM-VALUE | RANDOM-AGENT | :nobody
380 ARGUMENTS AND VALUES:
383 AGENTSET: An agent set
384 RANDOM-VALUE: a value in LIST
385 RANDOM-AGENT: an agent if AGENTSET is non empty
389 From an AGENTSET, returns a RANDOM-AGENT. If the agentset is empty, returns :nobody.
390 From a list, returns a RANDOM-VALUE. If the list is empty, an error occurs.
392 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#one-of"
394 ((agentset-p list-or-agentset)
396 ((agentset-list (agentset-list list-or-agentset))
397 (length (length agentset-list)))
398 (if (zerop length) :nobody (nth (clnl-random:next-int length) agentset-list))))
399 ((listp list-or-agentset)
401 ((length (length list-or-agentset)))
403 (error "one-of requires a nonempty list")
404 (nth (clnl-random:next-int length) list-or-agentset))))
405 (t (error "one-of requires a list or agentset"))))
408 (when (not (turtle-p *self*)) (error "Gotta call jump in turtle scope, dude (~A)" *self*))
412 (+ (turtle-xcor *self*) (* n (using-cached-sin (turtle-heading *self*))))))
416 (+ (turtle-ycor *self*) (* n (using-cached-cos (turtle-heading *self*)))))))
421 ARGUMENTS AND VALUES:
429 Sets the x-coordinate and y-coordinate for the turle. Equivalent to
430 set xcor x set ycor y, except it happens in one step inside of two.
432 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#setxy"
433 (when (not (turtle-p *self*)) (error "Gotta call setxy in turtle scope, dude (~A)" *self*))
434 (setf (turtle-xcor *self*) (wrap-x *topology* x))
435 (setf (turtle-ycor *self*) (wrap-y *topology* y)))
437 (defun set-default-shape (breed shape)
438 "SET-DEFAULT-SHAPE BREED SHAPE => RESULT
440 ARGUMENTS AND VALUES:
448 Specifies a default initial shape for a BREED. When a turtle, or it changes breeds,
449 its shape is set to the given shape.
451 SET-DEFAULT-SHAPE doesn't affect existing agents, only agents you create afterwards.
453 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#set-default-shape"
454 (when (not (breed-p breed)) (error "Need a valid breed"))
455 (setf (breed-default-shape breed) shape))
460 ARGUMENTS AND VALUES:
462 N: a double, the amount the turtle moves forward
467 Moves the current turtle forward N steps, one step at a time.
469 This moves forward one at a time in order to make the view updates look
470 good in the case of a purposefully slow running instance. If the number
471 is negative, the turtle moves backward.
473 If the current agent is not a turtle, it raises an error.
475 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#forward"
476 (when (not (turtle-p *self*)) (error "Gotta call fd in turtle scope, dude (~A)" *self*))
480 ((< (abs i) 3.2e-15) nil)
481 ((< (abs i) 1d0) (jump i))
482 (t (jump (if (> i 0d0) 1d0 -1d0)) (internal (- i (if (> i 0d0) 1d0 -1d0)))))))
485 (defun turn-right (n)
486 "TURN-RIGHT N => RESULT
488 ARGUMENTS AND VALUES:
490 N: a double, the amount the turtle turns
495 The turtle turns right by number degrees. (If number is negative, it turns left.)
497 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#right"
498 (when (not (turtle-p *self*)) (error "Gotta call fd in turtle scope, dude (~A)" *self*))
500 ((new-heading (+ (turtle-heading *self*) n)))
501 (setf (turtle-heading *self*)
503 ((< new-heading 0) (+ (mod new-heading -360) 360))
504 ((>= new-heading 360) (mod new-heading 360))
508 "TURN-LEFT N => RESULT
510 ARGUMENTS AND VALUES:
512 N: a double, the amount the turtle turns
517 The turtle turns left by number degrees. (If number is negative, it turns right.)
519 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#right"
522 (defun create-turtles (n &optional fn)
523 "CREATE-TURTLES N &optional FN => RESULT
525 ARGUMENTS AND VALUES:
527 N: an integer, the numbers of turtles to create
528 FN: A function, applied to each turtle after creation
533 Creates number new turtles at the origin.
535 New turtles have random integer headings and the color is randomly selected
536 from the 14 primary colors. If a function is supplied, the new turtles
539 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#create-turtles"
541 ((new-turtles (loop :repeat n :collect (create-turtle))))
542 (when fn (ask (list->agentset new-turtles :turtles) fn))))
544 (defun hatch (n &optional fn)
545 "HATCH N &optional FN => RESULT
547 ARGUMENTS AND VALUES:
549 N: an integer, the numbers of turtles to hatch
550 FN: A function, applied to each turtle after creation
555 The turtle in *self* creates N new turtles. Each new turtle inherits of all its
556 variables, including its location, from self.
558 If FN is supplied, the new turtles immediately run it.
560 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#hatch"
561 (when (not (turtle-p *self*)) (error "Can only hatch from turtle scope"))
563 ((new-turtles (loop :repeat n :collect (create-turtle *self*))))
564 (when fn (ask (list->agentset new-turtles :turtles) fn))))
566 (defun reset-ticks ()
567 "RESET-TICKS => RESULT
569 ARGUMENTS AND VALUES:
575 Resets the tick counter to zero, sets up all plots, then updates all plots.
577 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#reset-ticks"
581 "RESET-TICKS => RESULT
583 ARGUMENTS AND VALUES:
589 Advances the tick counter by one and updates all plots.
591 If the tick counter has not been started yet with reset-ticks, an error results.
593 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#tick"
595 (when (not *ticks*) (error "reset-ticks must be called"))
599 "TICKS => CURRENT-TICKS
601 ARGUMENTS AND VALUES:
603 CURRENT-TICKS: A positiv double, representing the current number of ticks
607 Reports the current value of the tick counter. The result is always a number and never negative.
609 If the tick counter has not been started yet with reset-ticks, an error results.
611 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#ticks"
612 (when (not *ticks*) (error "reset-ticks must be called"))
615 (defun clear-patches ()
619 :for y :from (max-pycor) :downto (min-pycor)
621 :for x :from (min-pxcor) :to (max-pxcor)
623 :xcor (coerce x 'double-float)
624 :ycor (coerce y 'double-float)
627 (defun clear-turtles ()
629 (setf *current-id* 0))
631 (defun clear-ticks ()
634 (defun create-world (&key dims)
635 "CREATE-WORLD &key DIMS => RESULT
637 DIMS: (:xmin XMIN :xmax XMAX :ymin YMIN :ymax YMAX)
639 ARGUMENTS AND VALUES:
642 XMIN: An integer representing the minimum patch coord in X
643 XMAX: An integer representing the maximum patch coord in X
644 YMIN: An integer representing the minimum patch coord in Y
645 YMAX: An integer representing the maximum patch coord in Y
649 Initializes the world in the NVM.
651 This should be called before using the engine in any real capacity. If
652 called when an engine is already running, it may do somethign weird."
653 (setf *dimensions* dims)
654 (setf *breeds* (list (list :turtles "default")))
659 ; These match netlogo's dump
660 (defgeneric dump-object (o))
662 (defmethod dump-object ((n double-float))
663 (multiple-value-bind (int rem) (floor n)
665 (format nil "~A" int)
667 ((output (format nil "~D" n)))
668 ; Someday we'll have d<posint>, but this is not that day!
669 (cl-ppcre:regex-replace "d-" (cl-ppcre:regex-replace "d0" output "") "E-")))))
671 (defmethod dump-object ((o string)) (format nil "~A" (cl-ppcre:regex-replace-all "\"" (format nil "~S" o) "\"\"")))
673 (defmethod dump-object ((o (eql t))) "true")
674 (defmethod dump-object ((o (eql nil))) "false")
676 (defmethod dump-object ((o list))
678 ((agentset-p o) (format nil "(agentset, ~A ~A)" (dump-object (count o)) (string-downcase (agentset-breed o))))
679 (t (format nil "[~{~A~^ ~}]" (mapcar #'dump-object o)))))
681 (defmethod dump-object ((o patch))
682 (format nil "(patch ~A ~A)" (dump-object (patch-xcor o)) (dump-object (patch-ycor o))))
684 (defmethod dump-object ((o turtle)) (format nil "(turtle ~A)" (dump-object (turtle-who o))))
685 (defmethod dump-object ((o (eql :nobody))) (format nil "nobody"))
687 (defun current-state ()
688 "CURRENT-STATE => WORLD-STATE
690 ARGUMENTS AND VALUES:
692 WORLD-STATE: A list, the current state of the whole world
696 Dumps out the state of the world.
698 This is useful for visualizations and also storing in a common lisp
699 data structure for easy usage in a common lisp instance. It's preferable
700 to use this when working with the nvm than the output done by export-world.
702 Currently this only dumps out turtle and patch information.
704 This is called CURRENT-STATE because export-world is an actual primitive
710 :color (turtle-color turtle)
711 :xcor (turtle-xcor turtle)
712 :ycor (turtle-ycor turtle)
713 :heading (turtle-heading turtle)
714 :size (turtle-size turtle)))
719 :color (patch-color patch)
720 :xcor (patch-xcor patch)
721 :ycor (patch-ycor patch)))
724 (defun export-turtles ()
729 "\"who\",\"color\",\"heading\",\"xcor\",\"ycor\",\"shape\",\"label\",\"label-color\","
730 "\"breed\",\"hidden?\",\"size\",\"pen-size\",\"pen-mode\""))
734 "\"~A\",\"~A\",\"~A\",\"~A\",\"~A\",\"~A\",\"~A\",\"~A\",\"{all-turtles}\",\"false\",\"~A\",~A"
735 (dump-object (turtle-who turtle))
736 (dump-object (turtle-color turtle))
737 (dump-object (turtle-heading turtle))
738 (dump-object (turtle-xcor turtle))
739 (dump-object (turtle-ycor turtle))
740 (dump-object (turtle-shape turtle))
741 (dump-object (turtle-label turtle))
742 (dump-object (turtle-label-color turtle))
743 (dump-object (turtle-size turtle))
744 "\"1\",\"\"\"up\"\"\""))
747 (defun export-patches ()
751 "\"pxcor\",\"pycor\",\"pcolor\",\"plabel\",\"plabel-color\"")
755 "\"~A\",\"~A\",\"~A\",\"\"\"\"\"\",\"9.9\""
756 (dump-object (patch-xcor patch))
757 (dump-object (patch-ycor patch))
758 (dump-object (patch-color patch))))
761 (defun export-world ()
762 "EXPORT-WORLD => WORLD-CSV
764 ARGUMENTS AND VALUES:
766 WORLD-CSV: A string, the csv of the world
770 Dumps out a csv matching NetLogo's export world.
772 This is useful for serializing the current state of the engine in order
773 to compare against NetLogo or to reimport later. Contains everything needed
774 to boot up a NetLogo instance in the exact same state."
775 (format nil "~{~A~%~}"
777 (format nil "~S" "RANDOM STATE")
778 (format nil "~S" (clnl-random:export))
780 (format nil "~S" "GLOBALS")
782 "\"min-pxcor\",\"max-pxcor\",\"min-pycor\",\"max-pycor\",\"perspective\",\"subject\","
783 "\"nextIndex\",\"directed-links\",\"ticks\",")
784 (format nil "\"~A\",\"~A\",\"~A\",\"~A\",\"0\",\"nobody\",\"~A\",\"\"\"NEITHER\"\"\",\"-1\""
785 (min-pxcor) (max-pxcor) (min-pycor) (max-pycor) *current-id*)
787 (format nil "~{~A~^~%~}" (export-turtles))
789 (format nil "~{~A~^~%~}" (export-patches))
791 (format nil "~S" "LINKS")
792 "\"end1\",\"end2\",\"color\",\"label\",\"label-color\",\"hidden?\",\"breed\",\"thickness\",\"shape\",\"tie-mode\""