1 (in-package #:clnl-nvm)
3 (defvar *current-id* 0)
5 (defstruct turtle who color heading xcor ycor)
15 VALUE: a NetLogo value
20 A command that prints the given NetLogo value to the command center.
22 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#show"
23 (format t "Showing: ~A~%" (dump-object value)))
25 (defun world-dimensions ()
26 (list :xmin -10 :xmax 10 :ymin -10 :ymax 10))
28 (defun create-turtle ()
36 :color (coerce (+ 5 (* 10 (clnl-random:next-int 14))) 'double-float)
37 :heading (coerce (clnl-random:next-int 360) 'double-float)
43 "TURTLES => ALL-TURTLES
47 ALL-TURTLES: a NetLogo agentset, all turtles
51 Reports the agentset consisting of all the turtles.
53 This agentset is special in that it represents the living turtles
54 each time it's used, so changes depending on the state of the engine.
56 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#turtles"
59 (defun ask (agent-set fn)
60 "ASK AGENT-SET FN => RESULT
64 AGENT-SET: a NetLogo agentset
65 FN: a function, run on each agent
66 RESULT: undefined, commands don't return
70 ASK is equivalent to ask in NetLogo.
72 The specified AGENT-SET runs the given FN. The order in which the agents
73 are run is random each time, and only agents that are in the set at the
74 beginning of the call.
76 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#ask"
78 ((iter (shufflerator agent-set)))
80 :for agent := (funcall iter)
82 :do (let ((*myself* *self*) (*self* agent)) (funcall fn)))))
84 (defun shufflerator (agent-set)
86 ((copy (copy-list agent-set))
92 ((idx (when (< i (1- (length copy))) (+ i (clnl-random:next-int (- (length copy) i))))))
93 (when idx (setf agent (nth idx copy)))
94 (when idx (setf (nth idx copy) (nth i copy)))
96 (fetch) ; we pre-fetch because netlogo does, rng sync hype!
99 ((> i (length copy)) nil)
100 ((= i (length copy)) (incf i) (car (last copy)))
101 (t (let ((result agent)) (fetch) result)))))))
103 (defun random-float (n)
104 "RANDOM-FLOAT N => RANDOM-NUMBER
106 ARGUMENTS AND VALUES:
108 N: a double, the upper bound of the random float
109 RANDOM-NUMBER: a double, the random result
113 Returns a random number strictly closer to zero than N.
115 If number is positive, returns a random floating point number greater than
116 or equal to 0 but strictly less than number.
118 If number is negative, returns a random floating point number less than or equal
119 to 0, but strictly greater than number.
121 If number is zero, the result is always 0.
123 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#random-float"
124 (clnl-random:next-double n))
129 ARGUMENTS AND VALUES:
131 N: a double, the amount the turtle moves forward
136 Moves the current turtle forward N steps, one step at a time.
138 This moves forward one at a time in order to make the view updates look
139 good in the case of a purposefully slow running instance. If the number
140 is negative, the turtle moves backward.
142 If the current agent is not a turtle, it raises an error.
144 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#forward"
145 (when (not (turtle-p *self*)) (error "Gotta call fd in turtle scope, dude (~A)" *self*))
146 (setf (turtle-xcor *self*) (+ (turtle-xcor *self*) (* n (sin (* pi (/ (turtle-heading *self*) 180))))))
147 (setf (turtle-ycor *self*) (+ (turtle-ycor *self*) (* n (cos (* pi (/ (turtle-heading *self*) 180)))))))
149 (defun create-turtles (n)
150 "CREATE-TURTLES N => RESULT
152 ARGUMENTS AND VALUES:
154 N: an integer, the numbers of turtles to create
159 Creates number new turtles at the origin.
161 New turtles have random integer headings and the color is randomly selected
162 from the 14 primary colors. If commands are supplied, the new turtles
163 immediately run them (unimplemented).
165 See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#create-turtles"
166 (loop :for i :from 1 :to n :do (create-turtle)))
168 (defun create-world ()
169 "CREATE-WORLD => RESULT
171 ARGUMENTS AND VALUES:
177 Initializes the world in the NVM.
179 This should be called before using the engine in any real capacity. If
180 called when an engine is already running, it may do somethign weird."
182 (setf *current-id* 0))
184 ; These match netlogo's dump
185 (defgeneric dump-object (o))
187 (defmethod dump-object ((n double-float))
188 (multiple-value-bind (int rem) (floor n)
190 (format nil "~A" int)
192 ((output (format nil "~D" n)))
193 ; Someday we'll have d<posint>, but this is not that day!
194 (cl-ppcre:regex-replace "d-" (cl-ppcre:regex-replace "d0" output "") "E-")))))
196 (defmethod dump-object ((o string)) o)
198 (defun current-state ()
199 "CURRENT-STATE => WORLD-STATE
201 ARGUMENTS AND VALUES:
203 WORLD-STATE: A list, the current state of the whole world
207 Dumps out the state of the world.
209 This is useful for visualizations and also storing in a common lisp
210 data structure for easy usage in a common lisp instance. It's preferable
211 to use this when working with the nvm than the output done by export-world.
213 Currently this only dumps out turtle information.
215 This is called CURRENT-STATE because export-world is an actual primitive
220 :color (turtle-color turtle)
221 :xcor (turtle-xcor turtle)
222 :ycor (turtle-ycor turtle)
223 :heading (turtle-heading turtle)))
226 (defun export-patches ()
228 "\"pxcor\",\"pycor\",\"pcolor\",\"plabel\",\"plabel-color\""
229 "\"-1\",\"1\",\"0\",\"\"\"\"\"\",\"9.9\""
230 "\"0\",\"1\",\"0\",\"\"\"\"\"\",\"9.9\""
231 "\"1\",\"1\",\"0\",\"\"\"\"\"\",\"9.9\""
232 "\"-1\",\"0\",\"0\",\"\"\"\"\"\",\"9.9\""
233 "\"0\",\"0\",\"0\",\"\"\"\"\"\",\"9.9\""
234 "\"1\",\"0\",\"0\",\"\"\"\"\"\",\"9.9\""
235 "\"-1\",\"-1\",\"0\",\"\"\"\"\"\",\"9.9\""
236 "\"0\",\"-1\",\"0\",\"\"\"\"\"\",\"9.9\""
237 "\"1\",\"-1\",\"0\",\"\"\"\"\"\",\"9.9\""))
239 (defun export-world ()
240 "EXPORT-WORLD => WORLD-CSV
242 ARGUMENTS AND VALUES:
244 WORLD-CSV: A string, the csv of the world
248 Dumps out a csv matching NetLogo's export world.
250 This is useful for serializing the current state of the engine in order
251 to compare against NetLogo or to reimport later. Contains everything needed
252 to boot up a NetLogo instance in the exact same state."
253 (format nil "~{~A~%~}"
255 (format nil "~S" "RANDOM STATE")
256 (format nil "~S" (clnl-random:export))
258 (format nil "~S" "GLOBALS")
260 "\"min-pxcor\",\"max-pxcor\",\"min-pycor\",\"max-pycor\",\"perspective\",\"subject\","
261 "\"nextIndex\",\"directed-links\",\"ticks\",")
262 (format nil "\"-1\",\"1\",\"-1\",\"1\",\"0\",\"nobody\",\"~A\",\"\"\"NEITHER\"\"\",\"-1\"" *current-id*)
264 (format nil "~S" "TURTLES")
266 "\"who\",\"color\",\"heading\",\"xcor\",\"ycor\",\"shape\",\"label\",\"label-color\","
267 "\"breed\",\"hidden?\",\"size\",\"pen-size\",\"pen-mode\"")
268 (format nil "~{~A~%~}"
272 "\"~A\",\"~A\",\"~A\",\"~A\",\"~A\",~A"
274 (dump-object (turtle-color turtle))
275 (dump-object (turtle-heading turtle))
276 (dump-object (turtle-xcor turtle))
277 (dump-object (turtle-ycor turtle))
278 "\"\"\"default\"\"\",\"\"\"\"\"\",\"9.9\",\"{all-turtles}\",\"false\",\"1\",\"1\",\"\"\"up\"\"\""))
280 (format nil "~S" "PATCHES")
281 (format nil "~{~A~^~%~}" (export-patches))
283 (format nil "~S" "LINKS")
284 "\"end1\",\"end2\",\"color\",\"label\",\"label-color\",\"hidden?\",\"breed\",\"thickness\",\"shape\",\"tie-mode\""