+(defvar *current-id* 0)
+
+(defstruct turtle who color heading xcor ycor)
+(defvar *turtles* nil)
+(defvar *myself* nil)
+(defvar *self* nil)
+
+(defun show (n)
+ (format t "Showing: ~A~%" (dump-object n)))
+
+(defun create-turtle ()
+ (setf
+ *turtles*
+ (nconc
+ *turtles*
+ (list
+ (make-turtle :who *current-id*
+ :color (coerce (+ 5 (* 10 (cl-nl.random:next-int 14))) 'double-float)
+ :heading (coerce (cl-nl.random:next-int 360) 'double-float)
+ :xcor 0d0
+ :ycor 0d0))))
+ (incf *current-id*))
+
+(defun turtles () *turtles*)
+
+(defun ask (agent-set fn)
+ (let
+ ((iter (shufflerator agent-set)))
+ (loop for agent = (funcall iter)
+ while agent
+ do (let ((*myself* *self*) (*self* agent)) (funcall fn)))))
+
+(defun shufflerator (agent-set)
+ (let
+ ((copy (copy-list agent-set))
+ (i 0)
+ (agent nil))
+ (flet
+ ((fetch ()
+ (let
+ ((idx (when (< i (1- (length copy))) (+ i (cl-nl.random:next-int (- (length copy) i))))))
+ (when idx (setf agent (nth idx copy)))
+ (when idx (setf (nth idx copy) (nth i copy)))
+ (incf i))))
+ (fetch) ; we pre-fetch because netlogo does, rng sync hype!
+ (lambda ()
+ (cond
+ ((> i (length copy)) nil)
+ ((= i (length copy)) (incf i) (car (last copy)))
+ (t (let ((result agent)) (fetch) result)))))))
+
+(defun random-float (n)
+ (cl-nl.random:next-double n))
+
+(defun fd (n)
+ (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)))))))
+