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 ()
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 "TURTLES => ALL-TURTLES
93 ALL-TURTLES: a NetLogo agentset, all turtles
97 Reports the agentset consisting of all the turtles.
99 This agentset is special in that it represents the living turtles
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#turtles"
105 (defun ask (agent-set fn)
106 "ASK AGENT-SET FN => RESULT
108 ARGUMENTS AND VALUES:
110 AGENT-SET: a NetLogo agentset
111 FN: a function, run on each agent
112 RESULT: undefined, commands don't return
116 ASK is equivalent to ask in NetLogo.
118 The specified AGENT-SET runs the given FN. The order in which the agents
119 are run is random each time, and only agents that are in the set at the
120 beginning of the call.
122 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#ask"
124 ((iter (shufflerator agent-set)))
126 :for agent := (funcall iter)
128 :do (let ((*myself* *self*) (*self* agent)) (funcall fn)))))
130 (defun shufflerator (agent-set)
132 ((copy (copy-list agent-set))
138 ((idx (when (< i (1- (length copy))) (+ i (clnl-random:next-int (- (length copy) i))))))
139 (when idx (setf agent (nth idx copy)))
140 (when idx (setf (nth idx copy) (nth i copy)))
142 (fetch) ; we pre-fetch because netlogo does, rng sync hype!
145 ((> i (length copy)) nil)
146 ((= i (length copy)) (incf i) (car (last copy)))
147 (t (let ((result agent)) (fetch) result)))))))
149 (defun random-float (n)
150 "RANDOM-FLOAT N => RANDOM-NUMBER
152 ARGUMENTS AND VALUES:
154 N: a double, the upper bound of the random float
155 RANDOM-NUMBER: a double, the random result
159 Returns a random number strictly closer to zero than N.
161 If number is positive, returns a random floating point number greater than
162 or equal to 0 but strictly less than number.
164 If number is negative, returns a random floating point number less than or equal
165 to 0, but strictly greater than number.
167 If number is zero, the result is always 0.
169 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#random-float"
170 (clnl-random:next-double n))
173 (when (not (turtle-p *self*)) (error "Gotta call fd in turtle scope, dude (~A)" *self*))
177 (+ (turtle-xcor *self*) (* n (using-cached-sin (turtle-heading *self*))))))
181 (+ (turtle-ycor *self*) (* n (using-cached-cos (turtle-heading *self*)))))))
186 ARGUMENTS AND VALUES:
188 N: a double, the amount the turtle moves forward
193 Moves the current turtle forward N steps, one step at a time.
195 This moves forward one at a time in order to make the view updates look
196 good in the case of a purposefully slow running instance. If the number
197 is negative, the turtle moves backward.
199 If the current agent is not a turtle, it raises an error.
201 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#forward"
202 (when (not (turtle-p *self*)) (error "Gotta call fd in turtle scope, dude (~A)" *self*))
206 ((< (abs i) 3.2e-15) nil)
207 ((< (abs i) 1d0) (jump i))
208 (t (jump (if (> i 0d0) 1d0 -1d0)) (internal (- i (if (> i 0d0) 1d0 -1d0)))))))
211 (defun turn-right (n)
212 "TURN-RIGHT N => RESULT
214 ARGUMENTS AND VALUES:
216 N: a double, the amount the turtle turns
221 The turtle turns right by number degrees. (If number is negative, it turns left.)
223 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#right"
224 (when (not (turtle-p *self*)) (error "Gotta call fd in turtle scope, dude (~A)" *self*))
226 ((new-heading (+ (turtle-heading *self*) n)))
227 (setf (turtle-heading *self*)
229 ((< new-heading 0) (+ (mod new-heading -360) 360))
230 ((>= new-heading 360) (mod new-heading 360))
234 "TURN-LEFT N => RESULT
236 ARGUMENTS AND VALUES:
238 N: a double, the amount the turtle turns
243 The turtle turns left by number degrees. (If number is negative, it turns right.)
245 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#right"
248 (defun create-turtles (n)
249 "CREATE-TURTLES N => RESULT
251 ARGUMENTS AND VALUES:
253 N: an integer, the numbers of turtles to create
258 Creates number new turtles at the origin.
260 New turtles have random integer headings and the color is randomly selected
261 from the 14 primary colors. If commands are supplied, the new turtles
262 immediately run them (unimplemented).
264 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#create-turtles"
265 (loop :for i :from 1 :to n :do (create-turtle)))
267 (defun create-world (&key dims)
268 "CREATE-WORLD &key DIMS => RESULT
270 DIMS: (:xmin XMIN :xmax XMAX :ymin YMIN :ymax YMAX)
272 ARGUMENTS AND VALUES:
275 XMIN: An integer representing the minimum patch coord in X
276 XMAX: An integer representing the maximum patch coord in X
277 YMIN: An integer representing the minimum patch coord in Y
278 YMAX: An integer representing the maximum patch coord in Y
282 Initializes the world in the NVM.
284 This should be called before using the engine in any real capacity. If
285 called when an engine is already running, it may do somethign weird."
286 (setf *dimensions* dims)
288 (setf *current-id* 0))
290 ; These match netlogo's dump
291 (defgeneric dump-object (o))
293 (defmethod dump-object ((n double-float))
294 (multiple-value-bind (int rem) (floor n)
296 (format nil "~A" int)
298 ((output (format nil "~D" n)))
299 ; Someday we'll have d<posint>, but this is not that day!
300 (cl-ppcre:regex-replace "d-" (cl-ppcre:regex-replace "d0" output "") "E-")))))
302 (defmethod dump-object ((o string)) o)
304 (defmethod dump-object ((o (eql t))) "true")
305 (defmethod dump-object ((o (eql nil))) "false")
307 (defun current-state ()
308 "CURRENT-STATE => WORLD-STATE
310 ARGUMENTS AND VALUES:
312 WORLD-STATE: A list, the current state of the whole world
316 Dumps out the state of the world.
318 This is useful for visualizations and also storing in a common lisp
319 data structure for easy usage in a common lisp instance. It's preferable
320 to use this when working with the nvm than the output done by export-world.
322 Currently this only dumps out turtle information.
324 This is called CURRENT-STATE because export-world is an actual primitive
329 :color (turtle-color turtle)
330 :xcor (turtle-xcor turtle)
331 :ycor (turtle-ycor turtle)
332 :heading (turtle-heading turtle)))
335 (defun export-patches ()
337 "\"pxcor\",\"pycor\",\"pcolor\",\"plabel\",\"plabel-color\""
338 "\"-1\",\"1\",\"0\",\"\"\"\"\"\",\"9.9\""
339 "\"0\",\"1\",\"0\",\"\"\"\"\"\",\"9.9\""
340 "\"1\",\"1\",\"0\",\"\"\"\"\"\",\"9.9\""
341 "\"-1\",\"0\",\"0\",\"\"\"\"\"\",\"9.9\""
342 "\"0\",\"0\",\"0\",\"\"\"\"\"\",\"9.9\""
343 "\"1\",\"0\",\"0\",\"\"\"\"\"\",\"9.9\""
344 "\"-1\",\"-1\",\"0\",\"\"\"\"\"\",\"9.9\""
345 "\"0\",\"-1\",\"0\",\"\"\"\"\"\",\"9.9\""
346 "\"1\",\"-1\",\"0\",\"\"\"\"\"\",\"9.9\""))
348 (defun export-world ()
349 "EXPORT-WORLD => WORLD-CSV
351 ARGUMENTS AND VALUES:
353 WORLD-CSV: A string, the csv of the world
357 Dumps out a csv matching NetLogo's export world.
359 This is useful for serializing the current state of the engine in order
360 to compare against NetLogo or to reimport later. Contains everything needed
361 to boot up a NetLogo instance in the exact same state."
362 (format nil "~{~A~%~}"
364 (format nil "~S" "RANDOM STATE")
365 (format nil "~S" (clnl-random:export))
367 (format nil "~S" "GLOBALS")
369 "\"min-pxcor\",\"max-pxcor\",\"min-pycor\",\"max-pycor\",\"perspective\",\"subject\","
370 "\"nextIndex\",\"directed-links\",\"ticks\",")
371 (format nil "\"~A\",\"~A\",\"~A\",\"~A\",\"0\",\"nobody\",\"~A\",\"\"\"NEITHER\"\"\",\"-1\""
372 (min-pxcor) (max-pxcor) (min-pycor) (max-pycor) *current-id*)
374 (format nil "~S" "TURTLES")
376 "\"who\",\"color\",\"heading\",\"xcor\",\"ycor\",\"shape\",\"label\",\"label-color\","
377 "\"breed\",\"hidden?\",\"size\",\"pen-size\",\"pen-mode\"")
378 (format nil "~{~A~%~}"
382 "\"~A\",\"~A\",\"~A\",\"~A\",\"~A\",~A"
384 (dump-object (turtle-color turtle))
385 (dump-object (turtle-heading turtle))
386 (dump-object (turtle-xcor turtle))
387 (dump-object (turtle-ycor turtle))
388 "\"\"\"default\"\"\",\"\"\"\"\"\",\"9.9\",\"{all-turtles}\",\"false\",\"1\",\"1\",\"\"\"up\"\"\""))
390 (format nil "~S" "PATCHES")
391 (format nil "~{~A~^~%~}" (export-patches))
393 (format nil "~S" "LINKS")
394 "\"end1\",\"end2\",\"color\",\"label\",\"label-color\",\"hidden?\",\"breed\",\"thickness\",\"shape\",\"tie-mode\""