(:magenta 125d0)
(:pink 135d0)))
-(defun create-turtle ()
+(defun create-turtle (&optional base-turtle)
(let
((new-turtle (make-turtle
:who (coerce *current-id* 'double-float)
- :color (coerce (+ 5 (* 10 (clnl-random:next-int 14))) 'double-float)
- :heading (coerce (clnl-random:next-int 360) 'double-float)
- :xcor 0d0
- :ycor 0d0)))
+ :color (if base-turtle
+ (turtle-color base-turtle)
+ (coerce (+ 5 (* 10 (clnl-random:next-int 14))) 'double-float))
+ :heading (if base-turtle
+ (turtle-heading base-turtle)
+ (coerce (clnl-random:next-int 360) 'double-float))
+ :xcor (if base-turtle (turtle-xcor base-turtle) 0d0)
+ :ycor (if base-turtle (turtle-ycor base-turtle) 0d0))))
(setf *turtles* (nconc *turtles* (list new-turtle)))
(incf *current-id*)
new-turtle))
((new-turtles (loop :repeat n :collect (create-turtle))))
(when fn (ask (list->agentset new-turtles :turtles) fn))))
+(defun hatch (n &optional fn)
+ "HATCH N &optional FN => RESULT
+
+ARGUMENTS AND VALUES:
+
+ N: an integer, the numbers of turtles to hatch
+ FN: A function, applied to each turtle after creation
+ RESULT: undefined
+
+DESCRIPTION:
+
+ The turtle in *self* creates N new turtles. Each new turtle inherits of all its
+ variables, including its location, from self.
+
+ If FN is supplied, the new turtles immediately run it.
+
+ See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#hatch"
+ (when (not (turtle-p *self*)) (error "Can only hatch from turtle scope"))
+ (let
+ ((new-turtles (loop :repeat n :collect (create-turtle *self*))))
+ (when fn (ask (list->agentset new-turtles :turtles) fn))))
+
(defun reset-ticks ()
"RESET-TICKS => RESULT
(defprim :display ())
(defprim :with (:agentset :reporter-block) :infix)
(defprim :fd (:number))
-(defprim :hatch (:number :command-block))
+(defprim :hatch (:number (:command-block :optional)))
; (defprim :let (t t)) ; keeping this here, commented out, to note that it has special processing
(defprim :if (:boolean :command-block))
(defprim :if-else (:boolean :command-block :command-block))
(defsimpleprim :crt :command clnl-nvm:create-turtles)
(defsimpleprim :die :command clnl-nvm:die)
(defsimpleprim :fd :command clnl-nvm:forward)
+(defsimpleprim :hatch :command clnl-nvm:hatch)
(defprim :if :command (lambda (pred a) `(when ,pred ,@(make-command-block-inline a))))
(defprim :ifelse :command (lambda (pred a b)
`(if ,pred
(defsimplereportertest "with 3" "patches with [ pcolor = green ]"
"(agentset, 0 patches)"
"E1DE30F072D785E0D0B59F28B0F7853E3D3E0D8B")
+
+(defsimplecommandtest "hatch 1" "crt 10 ask turtles [ hatch 1 ]"
+ "29E3D1D3FAA14FC0D6E03DB1315932EEBC7CB1F1")
+
+(defsimplecommandtest "hatch 2" "crt 10 ask turtles [ hatch 1 [ fd .5 ] ] ask turtles [ fd .5 ]"
+ "58E3CBC869F26B7D9ABC0C05C58C29F2FD588912")