1 (in-package #:cl-nl.nvm)
3 ; This is the engine. Yay.
5 (defvar *current-id* 0)
7 (defstruct turtle who color heading xcor ycor)
13 (format t "Showing: ~A~%" (dump-object n)))
15 (defun create-turtle ()
17 (make-turtle :who *current-id*
18 :color (coerce (+ 5 (* 10 (cl-nl.random:next-int 14))) 'double-float)
19 :heading (coerce (cl-nl.random:next-int 360) 'double-float)
25 (defun turtles () *turtles*)
27 (defun ask (agent-set fn)
36 (defun shuffle (agent-set)
38 ((copy (copy-list agent-set)))
40 (loop for i to (- (length copy) 2)
41 for idx = (+ i (cl-nl.random:next-int (- (length copy) i)))
42 for next = (nth idx copy)
43 do (setf (nth idx copy) (nth i copy))
47 (defun random-float (n)
48 (cl-nl.random:next-double n))
51 (when (not (turtle-p *self*)) (error "Gotta call fd in turtle scope, dude"))
52 (setf (turtle-xcor *self*) (+ (turtle-xcor *self*) (sin (* pi (/ (turtle-heading *self*) 180)))))
53 (setf (turtle-ycor *self*) (+ (turtle-ycor *self*) (cos (* pi (/ (turtle-heading *self*) 180))))))
55 (defun create-turtles (n)
56 (loop for i from 1 to n do (create-turtle)))
58 (defun create-world ()
60 (setf *current-id* 0))
62 (defgeneric dump-object (o))
63 (defmethod dump-object ((n double-float))
64 (multiple-value-bind (int rem) (floor n)
67 (format nil "~F" n))))
68 (defmethod dump-object ((o string)) o)
70 (defun export-world ()
71 (format nil "~{~A~%~}"
73 (format nil "~S" "RANDOM STATE")
74 (format nil "~S" (cl-nl.random:export))
76 (format nil "~S" "GLOBALS")
77 "\"min-pxcor\",\"max-pxcor\",\"min-pycor\",\"max-pycor\",\"perspective\",\"subject\",\"nextIndex\",\"directed-links\",\"ticks\","
78 (format nil "\"-1\",\"1\",\"-1\",\"1\",\"0\",\"nobody\",\"~A\",\"\"\"NEITHER\"\"\",\"-1\"" *current-id*)
80 (format nil "~S" "TURTLES")
81 "\"who\",\"color\",\"heading\",\"xcor\",\"ycor\",\"shape\",\"label\",\"label-color\",\"breed\",\"hidden?\",\"size\",\"pen-size\",\"pen-mode\""
82 (format nil "~{~A~%~}"
86 "\"~A\",\"~A\",\"~A\",\"~A\",\"~A\",\"\"\"default\"\"\",\"\"\"\"\"\",\"9.9\",\"{all-turtles}\",\"false\",\"1\",\"1\",\"\"\"up\"\"\""
88 (dump-object (turtle-color turtle))
89 (dump-object (turtle-heading turtle))
90 (dump-object (turtle-xcor turtle))
91 (dump-object (turtle-ycor turtle))
94 (format nil "~S" "PATCHES")
95 "\"pxcor\",\"pycor\",\"pcolor\",\"plabel\",\"plabel-color\""
96 "\"-1\",\"1\",\"0\",\"\"\"\"\"\",\"9.9\""
97 "\"0\",\"1\",\"0\",\"\"\"\"\"\",\"9.9\""
98 "\"1\",\"1\",\"0\",\"\"\"\"\"\",\"9.9\""
99 "\"-1\",\"0\",\"0\",\"\"\"\"\"\",\"9.9\""
100 "\"0\",\"0\",\"0\",\"\"\"\"\"\",\"9.9\""
101 "\"1\",\"0\",\"0\",\"\"\"\"\"\",\"9.9\""
102 "\"-1\",\"-1\",\"0\",\"\"\"\"\"\",\"9.9\""
103 "\"0\",\"-1\",\"0\",\"\"\"\"\"\",\"9.9\""
104 "\"1\",\"-1\",\"0\",\"\"\"\"\"\",\"9.9\""
106 (format nil "~S" "LINKS")
107 "\"end1\",\"end2\",\"color\",\"label\",\"label-color\",\"hidden?\",\"breed\",\"thickness\",\"shape\",\"tie-mode\""