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-set fn)
123 "ASK AGENT-SET FN => RESULT
125 ARGUMENTS AND VALUES:
127 AGENT-SET: a NetLogo agentset
128 FN: a function, run on each agent
129 RESULT: undefined, commands don't return
133 ASK is equivalent to ask in NetLogo.
135 The specified AGENT-SET runs the given FN. The order in which the agents
136 are run is random each time, and only agents that are in the set at the
137 beginning of the call.
139 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#ask"
141 ((iter (shufflerator agent-set)))
143 :for agent := (funcall iter)
145 :do (let ((*myself* *self*) (*self* agent)) (funcall fn)))))
147 (defun of (fn agent-set)
148 "OF FN AGENT-SET => RESULT
150 ARGUMENTS AND VALUES:
152 FN: a function, run on each agent
153 AGENT-SET: a NetLogo agentset
158 OF is equivalent to of in NetLogo.
160 The specified AGENT-SET runs the given FN. The order in which the agents
161 are run is random each time, and only agents that are in the set at the
162 beginning of the call. A list is returned of the returned valuse of
165 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#of"
167 ((iter (shufflerator agent-set)))
169 :for agent := (funcall iter)
171 :collect (let ((*myself* *self*) (*self* agent)) (funcall fn)))))
173 (defun shufflerator (agent-set)
175 ((copy (copy-list agent-set))
181 ((idx (when (< i (1- (length copy))) (+ i (clnl-random:next-int (- (length copy) i))))))
182 (when idx (setf agent (nth idx copy)))
183 (when idx (setf (nth idx copy) (nth i copy)))
185 (fetch) ; we pre-fetch because netlogo does, rng sync hype!
188 ((> i (length copy)) nil)
189 ((= i (length copy)) (incf i) (car (last copy)))
190 (t (let ((result agent)) (fetch) result)))))))
192 (defun random-float (n)
193 "RANDOM-FLOAT N => RANDOM-NUMBER
195 ARGUMENTS AND VALUES:
197 N: a double, the upper bound of the random float
198 RANDOM-NUMBER: a double, the random result
202 Returns a random number strictly closer to zero than N.
204 If number is positive, returns a random floating point number greater than
205 or equal to 0 but strictly less than number.
207 If number is negative, returns a random floating point number less than or equal
208 to 0, but strictly greater than number.
210 If number is zero, the result is always 0.
212 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#random-float"
213 (clnl-random:next-double n))
216 (when (not (turtle-p *self*)) (error "Gotta call fd in turtle scope, dude (~A)" *self*))
220 (+ (turtle-xcor *self*) (* n (using-cached-sin (turtle-heading *self*))))))
224 (+ (turtle-ycor *self*) (* n (using-cached-cos (turtle-heading *self*)))))))
229 ARGUMENTS AND VALUES:
231 N: a double, the amount the turtle moves forward
236 Moves the current turtle forward N steps, one step at a time.
238 This moves forward one at a time in order to make the view updates look
239 good in the case of a purposefully slow running instance. If the number
240 is negative, the turtle moves backward.
242 If the current agent is not a turtle, it raises an error.
244 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#forward"
245 (when (not (turtle-p *self*)) (error "Gotta call fd in turtle scope, dude (~A)" *self*))
249 ((< (abs i) 3.2e-15) nil)
250 ((< (abs i) 1d0) (jump i))
251 (t (jump (if (> i 0d0) 1d0 -1d0)) (internal (- i (if (> i 0d0) 1d0 -1d0)))))))
254 (defun turn-right (n)
255 "TURN-RIGHT N => RESULT
257 ARGUMENTS AND VALUES:
259 N: a double, the amount the turtle turns
264 The turtle turns right by number degrees. (If number is negative, it turns left.)
266 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#right"
267 (when (not (turtle-p *self*)) (error "Gotta call fd in turtle scope, dude (~A)" *self*))
269 ((new-heading (+ (turtle-heading *self*) n)))
270 (setf (turtle-heading *self*)
272 ((< new-heading 0) (+ (mod new-heading -360) 360))
273 ((>= new-heading 360) (mod new-heading 360))
277 "TURN-LEFT N => RESULT
279 ARGUMENTS AND VALUES:
281 N: a double, the amount the turtle turns
286 The turtle turns left by number degrees. (If number is negative, it turns right.)
288 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#right"
291 (defun create-turtles (n)
292 "CREATE-TURTLES N => RESULT
294 ARGUMENTS AND VALUES:
296 N: an integer, the numbers of turtles to create
301 Creates number new turtles at the origin.
303 New turtles have random integer headings and the color is randomly selected
304 from the 14 primary colors. If commands are supplied, the new turtles
305 immediately run them (unimplemented).
307 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#create-turtles"
308 (loop :for i :from 1 :to n :do (create-turtle)))
310 (defun reset-ticks ()
311 "RESET-TICKS => RESULT
313 ARGUMENTS AND VALUES:
319 Resets the tick counter to zero, sets up all plots, then updates all plots.
321 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#reset-ticks"
325 "RESET-TICKS => RESULT
327 ARGUMENTS AND VALUES:
333 Advances the tick counter by one and updates all plots.
335 If the tick counter has not been started yet with reset-ticks, an error results.
337 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#tick"
339 (when (not *ticks*) (error "reset-ticks must be called"))
343 "TICKS => CURRENT-TICKS
345 ARGUMENTS AND VALUES:
347 CURRENT-TICKS: A positiv double, representing the current number of ticks
351 Reports the current value of the tick counter. The result is always a number and never negative.
353 If the tick counter has not been started yet with reset-ticks, an error results.
355 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#ticks"
356 (when (not *ticks*) (error "reset-ticks must be called"))
359 (defun create-world (&key dims)
360 "CREATE-WORLD &key DIMS => RESULT
362 DIMS: (:xmin XMIN :xmax XMAX :ymin YMIN :ymax YMAX)
364 ARGUMENTS AND VALUES:
367 XMIN: An integer representing the minimum patch coord in X
368 XMAX: An integer representing the maximum patch coord in X
369 YMIN: An integer representing the minimum patch coord in Y
370 YMAX: An integer representing the maximum patch coord in Y
374 Initializes the world in the NVM.
376 This should be called before using the engine in any real capacity. If
377 called when an engine is already running, it may do somethign weird."
378 (setf *dimensions* dims)
382 :for y :from (max-pycor) :downto (min-pycor)
384 :for x :from (min-pxcor) :to (max-pxcor)
386 :xcor (coerce x 'double-float)
387 :ycor (coerce y 'double-float)
390 (setf *current-id* 0))
392 ; These match netlogo's dump
393 (defgeneric dump-object (o))
395 (defmethod dump-object ((n double-float))
396 (multiple-value-bind (int rem) (floor n)
398 (format nil "~A" int)
400 ((output (format nil "~D" n)))
401 ; Someday we'll have d<posint>, but this is not that day!
402 (cl-ppcre:regex-replace "d-" (cl-ppcre:regex-replace "d0" output "") "E-")))))
404 (defmethod dump-object ((o string)) o)
406 (defmethod dump-object ((o (eql t))) "true")
407 (defmethod dump-object ((o (eql nil))) "false")
409 (defmethod dump-object ((o list)) (format nil "[~{~A~^ ~}]" (mapcar #'dump-object o)))
411 (defun current-state ()
412 "CURRENT-STATE => WORLD-STATE
414 ARGUMENTS AND VALUES:
416 WORLD-STATE: A list, the current state of the whole world
420 Dumps out the state of the world.
422 This is useful for visualizations and also storing in a common lisp
423 data structure for easy usage in a common lisp instance. It's preferable
424 to use this when working with the nvm than the output done by export-world.
426 Currently this only dumps out turtle and patch information.
428 This is called CURRENT-STATE because export-world is an actual primitive
434 :color (turtle-color turtle)
435 :xcor (turtle-xcor turtle)
436 :ycor (turtle-ycor turtle)
437 :heading (turtle-heading turtle)))
442 :color (patch-color patch)
443 :xcor (patch-xcor patch)
444 :ycor (patch-ycor patch)))
447 (defun export-turtles ()
452 "\"who\",\"color\",\"heading\",\"xcor\",\"ycor\",\"shape\",\"label\",\"label-color\","
453 "\"breed\",\"hidden?\",\"size\",\"pen-size\",\"pen-mode\""))
457 "\"~A\",\"~A\",\"~A\",\"~A\",\"~A\",~A"
458 (dump-object (turtle-who turtle))
459 (dump-object (turtle-color turtle))
460 (dump-object (turtle-heading turtle))
461 (dump-object (turtle-xcor turtle))
462 (dump-object (turtle-ycor turtle))
463 "\"\"\"default\"\"\",\"\"\"\"\"\",\"9.9\",\"{all-turtles}\",\"false\",\"1\",\"1\",\"\"\"up\"\"\""))
466 (defun export-patches ()
470 "\"pxcor\",\"pycor\",\"pcolor\",\"plabel\",\"plabel-color\"")
474 "\"~A\",\"~A\",\"~A\",\"\"\"\"\"\",\"9.9\""
475 (dump-object (patch-xcor patch))
476 (dump-object (patch-ycor patch))
477 (dump-object (patch-color patch))))
480 (defun export-world ()
481 "EXPORT-WORLD => WORLD-CSV
483 ARGUMENTS AND VALUES:
485 WORLD-CSV: A string, the csv of the world
489 Dumps out a csv matching NetLogo's export world.
491 This is useful for serializing the current state of the engine in order
492 to compare against NetLogo or to reimport later. Contains everything needed
493 to boot up a NetLogo instance in the exact same state."
494 (format nil "~{~A~%~}"
496 (format nil "~S" "RANDOM STATE")
497 (format nil "~S" (clnl-random:export))
499 (format nil "~S" "GLOBALS")
501 "\"min-pxcor\",\"max-pxcor\",\"min-pycor\",\"max-pycor\",\"perspective\",\"subject\","
502 "\"nextIndex\",\"directed-links\",\"ticks\",")
503 (format nil "\"~A\",\"~A\",\"~A\",\"~A\",\"0\",\"nobody\",\"~A\",\"\"\"NEITHER\"\"\",\"-1\""
504 (min-pxcor) (max-pxcor) (min-pycor) (max-pycor) *current-id*)
506 (format nil "~{~A~^~%~}" (export-turtles))
508 (format nil "~{~A~^~%~}" (export-patches))
510 (format nil "~S" "LINKS")
511 "\"end1\",\"end2\",\"color\",\"label\",\"label-color\",\"hidden?\",\"breed\",\"thickness\",\"shape\",\"tie-mode\""