From: Frank Duncan Date: Sun, 1 May 2016 13:37:58 +0000 (-0500) Subject: Prims - Implement hatch X-Git-Tag: v0.1.0~26 X-Git-Url: https://code.consxy.com/gitweb/gitweb.cgi?a=commitdiff_plain;h=1dd12dd;p=clnl Prims - Implement hatch --- diff --git a/src/main/nvm/nvm.lisp b/src/main/nvm/nvm.lisp index 67b3ec4..6fdc812 100644 --- a/src/main/nvm/nvm.lisp +++ b/src/main/nvm/nvm.lisp @@ -48,14 +48,18 @@ DESCRIPTION: (: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)) @@ -485,6 +489,28 @@ DESCRIPTION: ((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 diff --git a/src/main/package.lisp b/src/main/package.lisp index 29f05cc..d88b674 100644 --- a/src/main/package.lisp +++ b/src/main/package.lisp @@ -72,6 +72,7 @@ into an ast that can be transpiled later.")) #:count #:create-turtles #:die + #:hatch #:of #:forward #:lookup-color diff --git a/src/main/parse.lisp b/src/main/parse.lisp index 11af551..640f8c5 100644 --- a/src/main/parse.lisp +++ b/src/main/parse.lisp @@ -254,7 +254,7 @@ DESCRIPTION: (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)) diff --git a/src/main/transpile.lisp b/src/main/transpile.lisp index 86a5657..b12bb14 100644 --- a/src/main/transpile.lisp +++ b/src/main/transpile.lisp @@ -146,6 +146,7 @@ DESCRIPTION: (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 diff --git a/src/test/simpletests.lisp b/src/test/simpletests.lisp index 5443673..dc57793 100644 --- a/src/test/simpletests.lisp +++ b/src/test/simpletests.lisp @@ -231,3 +231,9 @@ (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")