X-Git-Url: https://code.consxy.com/gitweb/gitweb.cgi?p=clnl;a=blobdiff_plain;f=src%2Fmain%2Fnvm.lisp;h=bf8981a2f6e359b01ab8c61787a610792c023633;hp=2b30edf2bcf3921b48b33f853a9bde75fa575cc2;hb=5c8699f151207953f4029e0fc6c488afce99f756;hpb=471de83db1aee70065808cbc061867e3320bf4b7 diff --git a/src/main/nvm.lisp b/src/main/nvm.lisp index 2b30edf..bf8981a 100644 --- a/src/main/nvm.lisp +++ b/src/main/nvm.lisp @@ -1,7 +1,5 @@ (in-package #:clnl-nvm) -; This is the engine. Yay. - (defvar *current-id* 0) (defstruct turtle who color heading xcor ycor) @@ -9,11 +7,20 @@ (defvar *myself* nil) (defvar *self* nil) -(defun show (n) - "Prints value in the Command Center, preceded by this agent, and followed by a carriage return. +(defun show (value) + "SHOW VALUE => RESULT + +ARGUMENTS AND VALUES: + + VALUE: a NetLogo value + RESULT: undefined + +DESCRIPTION: -See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#show" - (format t "Showing: ~A~%" (dump-object n))) + A command that prints the given NetLogo value to the command center. + + See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#show" + (format t "Showing: ~A~%" (dump-object value))) (defun create-turtle () (setf @@ -30,15 +37,40 @@ See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#show" (incf *current-id*)) (defun turtles () - "Reports the agentset consisting of all turtles. + "TURTLES => ALL-TURTLES + +ARGUMENTS AND VALUES: + + ALL-TURTLES: a NetLogo agentset, all turtles -See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#turtles" +DESCRIPTION: + + Reports the agentset consisting of all the turtles. + + This agentset is special in that it represents the living turtles + each time it's used, so changes depending on the state of the engine. + + See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#turtles" *turtles*) (defun ask (agent-set fn) - "The specified agent or agentset runs the given commands. + "ASK AGENT-SET FN => RESULT + +ARGUMENTS AND VALUES: + + AGENT-SET: a NetLogo agentset + FN: a function, run on each agent + RESULT: undefined, commands don't return + +DESCRIPTION: -See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#ask" + ASK is equivalent to ask in NetLogo. + + The specified AGENT-SET runs the given FN. The order in which the agents + are run is random each time, and only agents that are in the set at the + beginning of the call. + + See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#ask" (let ((iter (shufflerator agent-set))) (loop @@ -66,36 +98,83 @@ See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#ask" (t (let ((result agent)) (fetch) result))))))) (defun random-float (n) - "If number is positive, returns a random floating point number greater than -or equal to 0 but strictly less than number. + "RANDOM-FLOAT N => RANDOM-NUMBER + +ARGUMENTS AND VALUES: + + N: a double, the upper bound of the random float + RANDOM-NUMBER: a double, the random result + +DESCRIPTION: + + Returns a random number strictly closer to zero than N. -If number is negative, returns a random floating point number less than or equal -to 0, but strictly greater than number. + If number is positive, returns a random floating point number greater than + or equal to 0 but strictly less than number. -If number is zero, the result is always 0. + If number is negative, returns a random floating point number less than or equal + to 0, but strictly greater than number. -See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#random-float" + If number is zero, the result is always 0. + + See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#random-float" (clnl-random:next-double n)) (defun forward (n) - "The turtle moves forward by number steps, one step at a time. (If number is -negative, the turtle moves backward.) + "FORWARD N => RESULT + +ARGUMENTS AND VALUES: + + N: a double, the amount the turtle moves forward + RESULT: undefined + +DESCRIPTION: + + Moves the current turtle forward N steps, one step at a time. + + This moves forward one at a time in order to make the view updates look + good in the case of a purposefully slow running instance. If the number + is negative, the turtle moves backward. -See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#forward" + If the current agent is not a turtle, it raises an error. + + See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#forward" (when (not (turtle-p *self*)) (error "Gotta call fd in turtle scope, dude (~A)" *self*)) (setf (turtle-xcor *self*) (+ (turtle-xcor *self*) (* n (sin (* pi (/ (turtle-heading *self*) 180)))))) (setf (turtle-ycor *self*) (+ (turtle-ycor *self*) (* n (cos (* pi (/ (turtle-heading *self*) 180))))))) (defun create-turtles (n) - "Creates number new turtles at the origin. New turtles have random integer -headings and the color is randomly selected from the 14 primary colors. + "CREATE-TURTLES N => RESULT + +ARGUMENTS AND VALUES: + + N: an integer, the numbers of turtles to create + RESULT: undefined + +DESCRIPTION: -If commands are supplied, the new turtles immediately run them. + Creates number new turtles at the origin. -See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#create-turtles" + New turtles have random integer headings and the color is randomly selected + from the 14 primary colors. If commands are supplied, the new turtles + immediately run them (unimplemented). + + See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#create-turtles" (loop :for i :from 1 :to n :do (create-turtle))) (defun create-world () + "CREATE-WORLD => RESULT + +ARGUMENTS AND VALUES: + + RESULT: undefined + +DESCRIPTION: + + Initializes the world in the NVM. + + This should be called before using the engine in any real capacity. If + called when an engine is already running, it may do somethign weird." (setf *turtles* nil) (setf *current-id* 0)) @@ -113,7 +192,61 @@ See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#create-turtles" (defmethod dump-object ((o string)) o) +(defun current-state () + "CURRENT-STATE => WORLD-STATE + +ARGUMENTS AND VALUES: + + WORLD-STATE: A list, the current state of the whole world + +DESCRIPTION: + + Dumps out the state of the world. + + This is useful for visualizations and also storing in a common lisp + data structure for easy usage in a common lisp instance. It's preferable + to use this when working with the nvm than the output done by export-world. + + Currently this only dumps out turtle information. + + This is called CURRENT-STATE because export-world is an actual primitive + used by NetLogo." + (mapcar + (lambda (turtle) + (list + :color (turtle-color turtle) + :xcor (turtle-xcor turtle) + :ycor (turtle-ycor turtle) + :heading (turtle-heading turtle))) + *turtles*)) + +(defun export-patches () + (list + "\"pxcor\",\"pycor\",\"pcolor\",\"plabel\",\"plabel-color\"" + "\"-1\",\"1\",\"0\",\"\"\"\"\"\",\"9.9\"" + "\"0\",\"1\",\"0\",\"\"\"\"\"\",\"9.9\"" + "\"1\",\"1\",\"0\",\"\"\"\"\"\",\"9.9\"" + "\"-1\",\"0\",\"0\",\"\"\"\"\"\",\"9.9\"" + "\"0\",\"0\",\"0\",\"\"\"\"\"\",\"9.9\"" + "\"1\",\"0\",\"0\",\"\"\"\"\"\",\"9.9\"" + "\"-1\",\"-1\",\"0\",\"\"\"\"\"\",\"9.9\"" + "\"0\",\"-1\",\"0\",\"\"\"\"\"\",\"9.9\"" + "\"1\",\"-1\",\"0\",\"\"\"\"\"\",\"9.9\"")) + (defun export-world () + "EXPORT-WORLD => WORLD-CSV + +ARGUMENTS AND VALUES: + + WORLD-CSV: A string, the csv of the world + +DESCRIPTION: + + Dumps out a csv matching NetLogo's export world. + + This is useful for serializing the current state of the engine in order + to compare against NetLogo or to reimport later. Contains everything needed + to boot up a NetLogo instance in the exact same state." (format nil "~{~A~%~}" (list (format nil "~S" "RANDOM STATE") @@ -142,16 +275,7 @@ See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#create-turtles" "\"\"\"default\"\"\",\"\"\"\"\"\",\"9.9\",\"{all-turtles}\",\"false\",\"1\",\"1\",\"\"\"up\"\"\"")) *turtles*)) (format nil "~S" "PATCHES") - "\"pxcor\",\"pycor\",\"pcolor\",\"plabel\",\"plabel-color\"" - "\"-1\",\"1\",\"0\",\"\"\"\"\"\",\"9.9\"" - "\"0\",\"1\",\"0\",\"\"\"\"\"\",\"9.9\"" - "\"1\",\"1\",\"0\",\"\"\"\"\"\",\"9.9\"" - "\"-1\",\"0\",\"0\",\"\"\"\"\"\",\"9.9\"" - "\"0\",\"0\",\"0\",\"\"\"\"\"\",\"9.9\"" - "\"1\",\"0\",\"0\",\"\"\"\"\"\",\"9.9\"" - "\"-1\",\"-1\",\"0\",\"\"\"\"\"\",\"9.9\"" - "\"0\",\"-1\",\"0\",\"\"\"\"\"\",\"9.9\"" - "\"1\",\"-1\",\"0\",\"\"\"\"\"\",\"9.9\"" + (format nil "~{~A~^~%~}" (export-patches)) "" (format nil "~S" "LINKS") "\"end1\",\"end2\",\"color\",\"label\",\"label-color\",\"hidden?\",\"breed\",\"thickness\",\"shape\",\"tie-mode\""