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 create-turtle ()
28 :color (coerce (+ 5 (* 10 (clnl-random:next-int 14))) 'double-float)
29 :heading (coerce (clnl-random:next-int 360) 'double-float)
39 RESULT: undefined, commands don't return
43 The turtle or link dies
45 A dead agent ceases to exist. The effects of this include:
46 - The agent will not execute any further code.
47 - The agent will disappear from any agentsets it was in, reducing the size of those agentsets by one.
48 - Any variable that was storing the agent will now instead have nobody in it.
49 - If the dead agent was a turtle, every link connected to it also dies.
50 - If the observer was watching or following the agent, the observer's perspective resets.
52 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#die"
53 (when (not (turtle-p *self*)) (error "Gotta call die in turtle scope, dude (~A)" *self*))
54 (setf (turtle-who *self*) -1)
55 (setf *turtles* (remove *self* *turtles*)))
58 "TURTLES => ALL-TURTLES
62 ALL-TURTLES: a NetLogo agentset, all turtles
66 Reports the agentset consisting of all the turtles.
68 This agentset is special in that it represents the living turtles
69 each time it's used, so changes depending on the state of the engine.
71 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#turtles"
74 (defun ask (agent-set fn)
75 "ASK AGENT-SET FN => RESULT
79 AGENT-SET: a NetLogo agentset
80 FN: a function, run on each agent
81 RESULT: undefined, commands don't return
85 ASK is equivalent to ask in NetLogo.
87 The specified AGENT-SET runs the given FN. The order in which the agents
88 are run is random each time, and only agents that are in the set at the
89 beginning of the call.
91 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#ask"
93 ((iter (shufflerator agent-set)))
95 :for agent := (funcall iter)
97 :do (let ((*myself* *self*) (*self* agent)) (funcall fn)))))
99 (defun shufflerator (agent-set)
101 ((copy (copy-list agent-set))
107 ((idx (when (< i (1- (length copy))) (+ i (clnl-random:next-int (- (length copy) i))))))
108 (when idx (setf agent (nth idx copy)))
109 (when idx (setf (nth idx copy) (nth i copy)))
111 (fetch) ; we pre-fetch because netlogo does, rng sync hype!
114 ((> i (length copy)) nil)
115 ((= i (length copy)) (incf i) (car (last copy)))
116 (t (let ((result agent)) (fetch) result)))))))
118 (defun random-float (n)
119 "RANDOM-FLOAT N => RANDOM-NUMBER
121 ARGUMENTS AND VALUES:
123 N: a double, the upper bound of the random float
124 RANDOM-NUMBER: a double, the random result
128 Returns a random number strictly closer to zero than N.
130 If number is positive, returns a random floating point number greater than
131 or equal to 0 but strictly less than number.
133 If number is negative, returns a random floating point number less than or equal
134 to 0, but strictly greater than number.
136 If number is zero, the result is always 0.
138 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#random-float"
139 (clnl-random:next-double n))
142 (when (not (turtle-p *self*)) (error "Gotta call fd in turtle scope, dude (~A)" *self*))
146 (+ (turtle-xcor *self*) (* n (strictmath:sin (strictmath:to-radians (turtle-heading *self*)))))))
150 (+ (turtle-ycor *self*) (* n (strictmath:cos (strictmath:to-radians (turtle-heading *self*))))))))
155 ARGUMENTS AND VALUES:
157 N: a double, the amount the turtle moves forward
162 Moves the current turtle forward N steps, one step at a time.
164 This moves forward one at a time in order to make the view updates look
165 good in the case of a purposefully slow running instance. If the number
166 is negative, the turtle moves backward.
168 If the current agent is not a turtle, it raises an error.
170 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#forward"
171 (when (not (turtle-p *self*)) (error "Gotta call fd in turtle scope, dude (~A)" *self*))
175 ((< (abs i) 3.2e-15) nil)
176 ((< (abs i) 1d0) (jump i))
177 (t (jump (if (> i 0d0) 1d0 -1d0)) (internal (- i (if (> i 0d0) 1d0 -1d0)))))))
180 (defun create-turtles (n)
181 "CREATE-TURTLES N => RESULT
183 ARGUMENTS AND VALUES:
185 N: an integer, the numbers of turtles to create
190 Creates number new turtles at the origin.
192 New turtles have random integer headings and the color is randomly selected
193 from the 14 primary colors. If commands are supplied, the new turtles
194 immediately run them (unimplemented).
196 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#create-turtles"
197 (loop :for i :from 1 :to n :do (create-turtle)))
199 (defun create-world (&key dims)
200 "CREATE-WORLD &key DIMS => RESULT
202 DIMS: (:xmin XMIN :xmax XMAX :ymin YMIN :ymax YMAX)
204 ARGUMENTS AND VALUES:
207 XMIN: An integer representing the minimum patch coord in X
208 XMAX: An integer representing the maximum patch coord in X
209 YMIN: An integer representing the minimum patch coord in Y
210 YMAX: An integer representing the maximum patch coord in Y
214 Initializes the world in the NVM.
216 This should be called before using the engine in any real capacity. If
217 called when an engine is already running, it may do somethign weird."
218 (setf *dimensions* dims)
220 (setf *current-id* 0))
222 ; These match netlogo's dump
223 (defgeneric dump-object (o))
225 (defmethod dump-object ((n double-float))
226 (multiple-value-bind (int rem) (floor n)
228 (format nil "~A" int)
230 ((output (format nil "~D" n)))
231 ; Someday we'll have d<posint>, but this is not that day!
232 (cl-ppcre:regex-replace "d-" (cl-ppcre:regex-replace "d0" output "") "E-")))))
234 (defmethod dump-object ((o string)) o)
236 (defmethod dump-object ((o (eql t))) "true")
237 (defmethod dump-object ((o (eql nil))) "false")
239 (defun current-state ()
240 "CURRENT-STATE => WORLD-STATE
242 ARGUMENTS AND VALUES:
244 WORLD-STATE: A list, the current state of the whole world
248 Dumps out the state of the world.
250 This is useful for visualizations and also storing in a common lisp
251 data structure for easy usage in a common lisp instance. It's preferable
252 to use this when working with the nvm than the output done by export-world.
254 Currently this only dumps out turtle information.
256 This is called CURRENT-STATE because export-world is an actual primitive
261 :color (turtle-color turtle)
262 :xcor (turtle-xcor turtle)
263 :ycor (turtle-ycor turtle)
264 :heading (turtle-heading turtle)))
267 (defun export-patches ()
269 "\"pxcor\",\"pycor\",\"pcolor\",\"plabel\",\"plabel-color\""
270 "\"-1\",\"1\",\"0\",\"\"\"\"\"\",\"9.9\""
271 "\"0\",\"1\",\"0\",\"\"\"\"\"\",\"9.9\""
272 "\"1\",\"1\",\"0\",\"\"\"\"\"\",\"9.9\""
273 "\"-1\",\"0\",\"0\",\"\"\"\"\"\",\"9.9\""
274 "\"0\",\"0\",\"0\",\"\"\"\"\"\",\"9.9\""
275 "\"1\",\"0\",\"0\",\"\"\"\"\"\",\"9.9\""
276 "\"-1\",\"-1\",\"0\",\"\"\"\"\"\",\"9.9\""
277 "\"0\",\"-1\",\"0\",\"\"\"\"\"\",\"9.9\""
278 "\"1\",\"-1\",\"0\",\"\"\"\"\"\",\"9.9\""))
280 (defun export-world ()
281 "EXPORT-WORLD => WORLD-CSV
283 ARGUMENTS AND VALUES:
285 WORLD-CSV: A string, the csv of the world
289 Dumps out a csv matching NetLogo's export world.
291 This is useful for serializing the current state of the engine in order
292 to compare against NetLogo or to reimport later. Contains everything needed
293 to boot up a NetLogo instance in the exact same state."
294 (format nil "~{~A~%~}"
296 (format nil "~S" "RANDOM STATE")
297 (format nil "~S" (clnl-random:export))
299 (format nil "~S" "GLOBALS")
301 "\"min-pxcor\",\"max-pxcor\",\"min-pycor\",\"max-pycor\",\"perspective\",\"subject\","
302 "\"nextIndex\",\"directed-links\",\"ticks\",")
303 (format nil "\"~A\",\"~A\",\"~A\",\"~A\",\"0\",\"nobody\",\"~A\",\"\"\"NEITHER\"\"\",\"-1\""
304 (min-pxcor) (max-pxcor) (min-pycor) (max-pycor) *current-id*)
306 (format nil "~S" "TURTLES")
308 "\"who\",\"color\",\"heading\",\"xcor\",\"ycor\",\"shape\",\"label\",\"label-color\","
309 "\"breed\",\"hidden?\",\"size\",\"pen-size\",\"pen-mode\"")
310 (format nil "~{~A~%~}"
314 "\"~A\",\"~A\",\"~A\",\"~A\",\"~A\",~A"
316 (dump-object (turtle-color turtle))
317 (dump-object (turtle-heading turtle))
318 (dump-object (turtle-xcor turtle))
319 (dump-object (turtle-ycor turtle))
320 "\"\"\"default\"\"\",\"\"\"\"\"\",\"9.9\",\"{all-turtles}\",\"false\",\"1\",\"1\",\"\"\"up\"\"\""))
322 (format nil "~S" "PATCHES")
323 (format nil "~{~A~^~%~}" (export-patches))
325 (format nil "~S" "LINKS")
326 "\"end1\",\"end2\",\"color\",\"label\",\"label-color\",\"hidden?\",\"breed\",\"thickness\",\"shape\",\"tie-mode\""