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 ()
21 (make-turtle :who *current-id*
22 :color (coerce (+ 5 (* 10 (cl-nl.random:next-int 14))) 'double-float)
23 :heading (coerce (cl-nl.random:next-int 360) 'double-float)
28 (defun turtles () *turtles*)
30 (defun ask (agent-set fn)
32 ((iter (shufflerator agent-set)))
33 (loop for agent = (funcall iter)
35 do (let ((*myself* *self*) (*self* agent)) (funcall fn)))))
37 (defun shufflerator (agent-set)
39 ((copy (copy-list agent-set))
45 ((idx (when (< i (1- (length copy))) (+ i (cl-nl.random:next-int (- (length copy) i))))))
46 (when idx (setf agent (nth idx copy)))
47 (when idx (setf (nth idx copy) (nth i copy)))
49 (fetch) ; we pre-fetch because netlogo does, rng sync hype!
52 ((> i (length copy)) nil)
53 ((= i (length copy)) (incf i) (car (last copy)))
54 (t (let ((result agent)) (fetch) result)))))))
56 (defun random-float (n)
57 (cl-nl.random:next-double n))
60 (when (not (turtle-p *self*)) (error "Gotta call fd in turtle scope, dude (~A)" *self*))
61 (setf (turtle-xcor *self*) (+ (turtle-xcor *self*) (* n (sin (* pi (/ (turtle-heading *self*) 180))))))
62 (setf (turtle-ycor *self*) (+ (turtle-ycor *self*) (* n (cos (* pi (/ (turtle-heading *self*) 180)))))))
64 (defun create-turtles (n)
65 (loop for i from 1 to n do (create-turtle)))
67 (defun create-world ()
69 (setf *current-id* 0))
71 ; These match netlogo's dump
72 (defgeneric dump-object (o))
73 (defmethod dump-object ((n double-float))
74 (multiple-value-bind (int rem) (floor n)
78 ((output (format nil "~D" n)))
79 (cl-ppcre:regex-replace "d-" (cl-ppcre:regex-replace "d0" output "") "E-"))))) ; Someday we'll have d<posint>, but this is not that day!
80 (defmethod dump-object ((o string)) o)
82 (defun export-world ()
83 (format nil "~{~A~%~}"
85 (format nil "~S" "RANDOM STATE")
86 (format nil "~S" (cl-nl.random:export))
88 (format nil "~S" "GLOBALS")
89 "\"min-pxcor\",\"max-pxcor\",\"min-pycor\",\"max-pycor\",\"perspective\",\"subject\",\"nextIndex\",\"directed-links\",\"ticks\","
90 (format nil "\"-1\",\"1\",\"-1\",\"1\",\"0\",\"nobody\",\"~A\",\"\"\"NEITHER\"\"\",\"-1\"" *current-id*)
92 (format nil "~S" "TURTLES")
93 "\"who\",\"color\",\"heading\",\"xcor\",\"ycor\",\"shape\",\"label\",\"label-color\",\"breed\",\"hidden?\",\"size\",\"pen-size\",\"pen-mode\""
94 (format nil "~{~A~%~}"
98 "\"~A\",\"~A\",\"~A\",\"~A\",\"~A\",\"\"\"default\"\"\",\"\"\"\"\"\",\"9.9\",\"{all-turtles}\",\"false\",\"1\",\"1\",\"\"\"up\"\"\""
100 (dump-object (turtle-color turtle))
101 (dump-object (turtle-heading turtle))
102 (dump-object (turtle-xcor turtle))
103 (dump-object (turtle-ycor turtle))
106 (format nil "~S" "PATCHES")
107 "\"pxcor\",\"pycor\",\"pcolor\",\"plabel\",\"plabel-color\""
108 "\"-1\",\"1\",\"0\",\"\"\"\"\"\",\"9.9\""
109 "\"0\",\"1\",\"0\",\"\"\"\"\"\",\"9.9\""
110 "\"1\",\"1\",\"0\",\"\"\"\"\"\",\"9.9\""
111 "\"-1\",\"0\",\"0\",\"\"\"\"\"\",\"9.9\""
112 "\"0\",\"0\",\"0\",\"\"\"\"\"\",\"9.9\""
113 "\"1\",\"0\",\"0\",\"\"\"\"\"\",\"9.9\""
114 "\"-1\",\"-1\",\"0\",\"\"\"\"\"\",\"9.9\""
115 "\"0\",\"-1\",\"0\",\"\"\"\"\"\",\"9.9\""
116 "\"1\",\"-1\",\"0\",\"\"\"\"\"\",\"9.9\""
118 (format nil "~S" "LINKS")
119 "\"end1\",\"end2\",\"color\",\"label\",\"label-color\",\"hidden?\",\"breed\",\"thickness\",\"shape\",\"tie-mode\""