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 ()
58 :who (coerce *current-id* 'double-float)
59 :color (coerce (+ 5 (* 10 (clnl-random:next-int 14))) 'double-float)
60 :heading (coerce (clnl-random:next-int 360) 'double-float)
70 RESULT: undefined, commands don't return
74 The turtle or link dies
76 A dead agent ceases to exist. The effects of this include:
77 - The agent will not execute any further code.
78 - The agent will disappear from any agentsets it was in, reducing the size of those agentsets by one.
79 - Any variable that was storing the agent will now instead have nobody in it.
80 - If the dead agent was a turtle, every link connected to it also dies.
81 - If the observer was watching or following the agent, the observer's perspective resets.
83 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#die"
84 (when (not (turtle-p *self*)) (error "Gotta call die in turtle scope, dude (~A)" *self*))
85 (setf (turtle-who *self*) -1)
86 (setf *turtles* (remove *self* *turtles*)))
89 "PATCHES => ALL-PATCHES
93 ALL-PATCHES: a NetLogo agentset, all patches
97 Reports the agentset consisting of all the patches.
99 This agentset is special in that it represents the living patches
100 each time it's used, so changes depending on the state of the engine.
102 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#patches"
106 "TURTLES => ALL-TURTLES
108 ARGUMENTS AND VALUES:
110 ALL-TURTLES: a NetLogo agentset, all turtles
114 Reports the agentset consisting of all the turtles.
116 This agentset is special in that it represents the living turtles
117 each time it's used, so changes depending on the state of the engine.
119 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#turtles"
122 (defun ask (agent-or-agent-set fn)
123 "ASK AGENT-OR-AGENT-SET FN => RESULT
125 AGENT-OR-AGENT-SET: AGENT | AGENT-SET
127 ARGUMENTS AND VALUES:
129 FN: a function, run on each agent
130 RESULT: undefined, commands don't return
131 AGENT: a NetLogo agent
132 AGENT-SET: a NetLogo agentset
136 ASK is equivalent to ask in NetLogo.
138 The specified AGENT-SET or AGENT runs the given FN. In the case of an
139 AGENT-SET, the order in which the agents are run is random each time,
140 and only agents that are in the set at the beginning of the call.
142 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#ask"
144 ((agent-set-p agent-or-agent-set)
146 ((iter (shufflerator (agent-set-list agent-or-agent-set))))
148 :for agent := (funcall iter)
150 :do (let ((*myself* *self*) (*self* agent)) (funcall fn)))))
151 ((agent-p agent-or-agent-set)
152 (let ((*myself* *self*) (*self* agent-or-agent-set)) (funcall fn)))
154 (error "Ask requires an agent-set or agent but got: ~A" agent-or-agent-set))))
156 (defun count (agent-set)
157 "COUNT AGENT-SET => N
159 ARGUMENTS AND VALUES:
161 AGENT-SET: a NetLogo agentset
166 COUNT is equivalent to count in NetLogo. Returns N, the number of
169 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#count"
170 (coerce (length (agent-set-list agent-set)) 'double-float))
172 (defun of (fn agent-or-agent-set)
173 "OF FN AGENT-OR-AGENT-SET => RESULT
175 AGENT-OR-AGENT-SET: AGENT | AGENT-SET
176 RESULT: RESULT-LIST | RESULT-VALUE
178 ARGUMENTS AND VALUES:
180 FN: a function, run on each agent
181 AGENT: a NetLogo agent
182 AGENT-SET: a NetLogo agentset
184 RESULT-VALUE: a single value
188 OF is equivalent to of in NetLogo.
190 The specified AGENT-SET or AGENT runs the given FN. In the case of an
191 AGENT-SET, the order in which the agents are run is random each time,
192 and only agents that are in the set at the beginning of the call.
194 RESULT-LIST is returned when the input is an AGENT-SET, but RESULT-VALUE
195 is returned when only passed an AGENT.
197 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#of"
199 ((agent-set-p agent-or-agent-set)
201 ((iter (shufflerator (agent-set-list agent-or-agent-set))))
203 :for agent := (funcall iter)
205 :collect (let ((*myself* *self*) (*self* agent)) (funcall fn)))))
206 ((agent-p agent-or-agent-set)
207 (let ((*myself* *self*) (*self* agent-or-agent-set)) (funcall fn)))
209 (error "Of requires an agent-set or agent but got: ~A" agent-or-agent-set))))
211 (defun shufflerator (agent-set-list)
213 ((copy (copy-list agent-set-list))
219 ((idx (when (< i (1- (length copy))) (+ i (clnl-random:next-int (- (length copy) i))))))
220 (when idx (setf agent (nth idx copy)))
221 (when idx (setf (nth idx copy) (nth i copy)))
223 (fetch) ; we pre-fetch because netlogo does, rng sync hype!
226 ((> i (length copy)) nil)
227 ((= i (length copy)) (incf i) (car (last copy)))
228 (t (let ((result agent)) (fetch) result)))))))
230 (defun random-float (n)
231 "RANDOM-FLOAT N => RANDOM-NUMBER
233 ARGUMENTS AND VALUES:
235 N: a double, the upper bound of the random float
236 RANDOM-NUMBER: a double, the random result
240 Returns a random number strictly closer to zero than N.
242 If number is positive, returns a random floating point number greater than
243 or equal to 0 but strictly less than number.
245 If number is negative, returns a random floating point number less than or equal
246 to 0, but strictly greater than number.
248 If number is zero, the result is always 0.
250 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#random-float"
251 (clnl-random:next-double n))
254 "RANDOM N => RANDOM-NUMBER
256 ARGUMENTS AND VALUES:
258 N: an integer, the upper bound of the random
259 RANDOM-NUMBER: an integer, the random result
263 Returns a random number strictly closer to zero than N.
265 If number is positive, returns a random integer greater than or equal to 0,
266 but strictly less than number.
268 If number is negative, returns a random integer less than or equal to 0,
269 but strictly greater than number.
271 If number is zero, the result is always 0.
273 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#random"
274 (coerce (clnl-random:next-long (truncate n)) 'double-float))
276 (defun random-xcor ()
277 "RANDOM-XCOR => RANDOM-NUMBER
279 ARGUMENTS AND VALUES:
281 RANDOM-NUMBER: a float, the random result
285 Returns a random floating point number in the allowable range of turtle
286 coordinates along the x axis.
288 These range from min-pxcor - 0.5 (inclusive) to max-pxcor + 0.5 (exclusive)
290 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#random-cor"
292 ((min (- (min-pxcor) 0.5d0))
293 (max (+ (max-pxcor) 0.5d0)))
294 (+ min (clnl-random:next-double (- max min)))))
296 (defun random-ycor ()
297 "RANDOM-YCOR => RANDOM-NUMBER
299 ARGUMENTS AND VALUES:
301 RANDOM-NUMBER: a float, the random result
305 Returns a random floating point number in the allowable range of turtle
306 coordinates along the y axis.
308 These range from min-pycor - 0.5 (inclusive) to max-pycor + 0.5 (exclusive)
310 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#random-cor"
312 ((min (- (min-pycor) 0.5d0))
313 (max (+ (max-pycor) 0.5d0)))
314 (+ min (clnl-random:next-double (- max min)))))
316 (defun one-of (agent-set)
317 "ONE-OF AGENT-SET => RESULT
319 RESULT: RANDOM-AGENT | :nobody
321 ARGUMENTS AND VALUES:
323 AGENT-SET: An agent set
324 RANDOM-AGENT: an agent if AGENT-SET is non empty
328 From an agentset, returns a random agent. If the agentset is empty, returns nobody.
330 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#one-of"
332 ((agent-set-list (agent-set-list agent-set))
333 (length (length agent-set-list)))
334 (if (zerop length) :nobody (nth (clnl-random:next-int length) agent-set-list))))
337 (when (not (turtle-p *self*)) (error "Gotta call jump in turtle scope, dude (~A)" *self*))
341 (+ (turtle-xcor *self*) (* n (using-cached-sin (turtle-heading *self*))))))
345 (+ (turtle-ycor *self*) (* n (using-cached-cos (turtle-heading *self*)))))))
350 ARGUMENTS AND VALUES:
358 Sets the x-coordinate and y-coordinate for the turle. Equivalent to
359 set xcor x set ycor y, except it happens in one step inside of two.
361 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#setxy"
362 (when (not (turtle-p *self*)) (error "Gotta call setxy in turtle scope, dude (~A)" *self*))
363 (setf (turtle-xcor *self*) (wrap-x *topology* x))
364 (setf (turtle-ycor *self*) (wrap-y *topology* y)))
369 ARGUMENTS AND VALUES:
371 N: a double, the amount the turtle moves forward
376 Moves the current turtle forward N steps, one step at a time.
378 This moves forward one at a time in order to make the view updates look
379 good in the case of a purposefully slow running instance. If the number
380 is negative, the turtle moves backward.
382 If the current agent is not a turtle, it raises an error.
384 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#forward"
385 (when (not (turtle-p *self*)) (error "Gotta call fd in turtle scope, dude (~A)" *self*))
389 ((< (abs i) 3.2e-15) nil)
390 ((< (abs i) 1d0) (jump i))
391 (t (jump (if (> i 0d0) 1d0 -1d0)) (internal (- i (if (> i 0d0) 1d0 -1d0)))))))
394 (defun turn-right (n)
395 "TURN-RIGHT N => RESULT
397 ARGUMENTS AND VALUES:
399 N: a double, the amount the turtle turns
404 The turtle turns right by number degrees. (If number is negative, it turns left.)
406 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#right"
407 (when (not (turtle-p *self*)) (error "Gotta call fd in turtle scope, dude (~A)" *self*))
409 ((new-heading (+ (turtle-heading *self*) n)))
410 (setf (turtle-heading *self*)
412 ((< new-heading 0) (+ (mod new-heading -360) 360))
413 ((>= new-heading 360) (mod new-heading 360))
417 "TURN-LEFT N => RESULT
419 ARGUMENTS AND VALUES:
421 N: a double, the amount the turtle turns
426 The turtle turns left by number degrees. (If number is negative, it turns right.)
428 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#right"
431 (defun create-turtles (n)
432 "CREATE-TURTLES N => RESULT
434 ARGUMENTS AND VALUES:
436 N: an integer, the numbers of turtles to create
441 Creates number new turtles at the origin.
443 New turtles have random integer headings and the color is randomly selected
444 from the 14 primary colors. If commands are supplied, the new turtles
445 immediately run them (unimplemented).
447 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#create-turtles"
448 (loop :for i :from 1 :to n :do (create-turtle)))
450 (defun reset-ticks ()
451 "RESET-TICKS => RESULT
453 ARGUMENTS AND VALUES:
459 Resets the tick counter to zero, sets up all plots, then updates all plots.
461 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#reset-ticks"
465 "RESET-TICKS => RESULT
467 ARGUMENTS AND VALUES:
473 Advances the tick counter by one and updates all plots.
475 If the tick counter has not been started yet with reset-ticks, an error results.
477 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#tick"
479 (when (not *ticks*) (error "reset-ticks must be called"))
483 "TICKS => CURRENT-TICKS
485 ARGUMENTS AND VALUES:
487 CURRENT-TICKS: A positiv double, representing the current number of ticks
491 Reports the current value of the tick counter. The result is always a number and never negative.
493 If the tick counter has not been started yet with reset-ticks, an error results.
495 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#ticks"
496 (when (not *ticks*) (error "reset-ticks must be called"))
499 (defun create-world (&key dims)
500 "CREATE-WORLD &key DIMS => RESULT
502 DIMS: (:xmin XMIN :xmax XMAX :ymin YMIN :ymax YMAX)
504 ARGUMENTS AND VALUES:
507 XMIN: An integer representing the minimum patch coord in X
508 XMAX: An integer representing the maximum patch coord in X
509 YMIN: An integer representing the minimum patch coord in Y
510 YMAX: An integer representing the maximum patch coord in Y
514 Initializes the world in the NVM.
516 This should be called before using the engine in any real capacity. If
517 called when an engine is already running, it may do somethign weird."
518 (setf *dimensions* dims)
522 :for y :from (max-pycor) :downto (min-pycor)
524 :for x :from (min-pxcor) :to (max-pxcor)
526 :xcor (coerce x 'double-float)
527 :ycor (coerce y 'double-float)
530 (setf *current-id* 0))
532 ; These match netlogo's dump
533 (defgeneric dump-object (o))
535 (defmethod dump-object ((n double-float))
536 (multiple-value-bind (int rem) (floor n)
538 (format nil "~A" int)
540 ((output (format nil "~D" n)))
541 ; Someday we'll have d<posint>, but this is not that day!
542 (cl-ppcre:regex-replace "d-" (cl-ppcre:regex-replace "d0" output "") "E-")))))
544 (defmethod dump-object ((o string)) (format nil "~A" (cl-ppcre:regex-replace-all "\"" (format nil "~S" o) "\"\"")))
546 (defmethod dump-object ((o (eql t))) "true")
547 (defmethod dump-object ((o (eql nil))) "false")
549 (defmethod dump-object ((o list)) (format nil "[~{~A~^ ~}]" (mapcar #'dump-object o)))
551 (defmethod dump-object ((o patch))
552 (format nil "(patch ~A ~A)" (dump-object (patch-xcor o)) (dump-object (patch-ycor o))))
554 (defmethod dump-object ((o turtle)) (format nil "(turtle ~A)" (dump-object (turtle-who o))))
555 (defmethod dump-object ((o (eql :nobody))) (format nil "nobody"))
557 (defun current-state ()
558 "CURRENT-STATE => WORLD-STATE
560 ARGUMENTS AND VALUES:
562 WORLD-STATE: A list, the current state of the whole world
566 Dumps out the state of the world.
568 This is useful for visualizations and also storing in a common lisp
569 data structure for easy usage in a common lisp instance. It's preferable
570 to use this when working with the nvm than the output done by export-world.
572 Currently this only dumps out turtle and patch information.
574 This is called CURRENT-STATE because export-world is an actual primitive
580 :color (turtle-color turtle)
581 :xcor (turtle-xcor turtle)
582 :ycor (turtle-ycor turtle)
583 :heading (turtle-heading turtle)
584 :size (turtle-size turtle)))
589 :color (patch-color patch)
590 :xcor (patch-xcor patch)
591 :ycor (patch-ycor patch)))
594 (defun export-turtles ()
599 "\"who\",\"color\",\"heading\",\"xcor\",\"ycor\",\"shape\",\"label\",\"label-color\","
600 "\"breed\",\"hidden?\",\"size\",\"pen-size\",\"pen-mode\""))
604 "\"~A\",\"~A\",\"~A\",\"~A\",\"~A\",\"\"\"default\"\"\",\"~A\",\"~A\",\"{all-turtles}\",\"false\",\"~A\",~A"
605 (dump-object (turtle-who turtle))
606 (dump-object (turtle-color turtle))
607 (dump-object (turtle-heading turtle))
608 (dump-object (turtle-xcor turtle))
609 (dump-object (turtle-ycor turtle))
610 (dump-object (turtle-label turtle))
611 (dump-object (turtle-label-color turtle))
612 (dump-object (turtle-size turtle))
613 "\"1\",\"\"\"up\"\"\""))
616 (defun export-patches ()
620 "\"pxcor\",\"pycor\",\"pcolor\",\"plabel\",\"plabel-color\"")
624 "\"~A\",\"~A\",\"~A\",\"\"\"\"\"\",\"9.9\""
625 (dump-object (patch-xcor patch))
626 (dump-object (patch-ycor patch))
627 (dump-object (patch-color patch))))
630 (defun export-world ()
631 "EXPORT-WORLD => WORLD-CSV
633 ARGUMENTS AND VALUES:
635 WORLD-CSV: A string, the csv of the world
639 Dumps out a csv matching NetLogo's export world.
641 This is useful for serializing the current state of the engine in order
642 to compare against NetLogo or to reimport later. Contains everything needed
643 to boot up a NetLogo instance in the exact same state."
644 (format nil "~{~A~%~}"
646 (format nil "~S" "RANDOM STATE")
647 (format nil "~S" (clnl-random:export))
649 (format nil "~S" "GLOBALS")
651 "\"min-pxcor\",\"max-pxcor\",\"min-pycor\",\"max-pycor\",\"perspective\",\"subject\","
652 "\"nextIndex\",\"directed-links\",\"ticks\",")
653 (format nil "\"~A\",\"~A\",\"~A\",\"~A\",\"0\",\"nobody\",\"~A\",\"\"\"NEITHER\"\"\",\"-1\""
654 (min-pxcor) (max-pxcor) (min-pycor) (max-pycor) *current-id*)
656 (format nil "~{~A~^~%~}" (export-turtles))
658 (format nil "~{~A~^~%~}" (export-patches))
660 (format nil "~S" "LINKS")
661 "\"end1\",\"end2\",\"color\",\"label\",\"label-color\",\"hidden?\",\"breed\",\"thickness\",\"shape\",\"tie-mode\""