ball-fall_2009


Information

Created with NetLogo version NetLogo 4.0.4
Running with NetLogoLite.jar version 404.



Procedures

NetLogo Version: NetLogo 4.0.4

globals
[ last-drop ; use by do-mouse
  mode      ; used by do-mouse
  top-row   ; used by do-fill
  drain-row ; used by do-drain
  field     ; the patches that are not border patches
  border    ; the border patches
]

breed [ balls ]


patches-own
[ void? ; true means patch is empty, and not an obstacle
]

to startup
   setup
end
   
to setup
   ca
   ask patches [ set void? true ]
   reset-screen-regions
   set top-row patches with   [ pycor = max-pycor ]
   set drain-row patches with [ pycor = min-pycor + 1 ]
   make-funnel
   make-splotches
   make-pins
end

to go
   ; if using mouse, all else pauses
   ifelse active-mouse? and mouse-down?
   [ do-mouse ]
   [ set mode 0 ; reset draw / erase mode
     ; evaluate the run-time effect switches
     if drain?   [ do-drain ]
     if fill?    [ do-fill  ]
     ; balls fall
     ask balls [ do-fall ]
   ]
   tick
end

to define-field
   set field patches with 
   [     abs pxcor < max-pxcor
     and pycor > min-pycor
   ]
end

to define-border
   set border patches with
   [ abs pxcor = max-pxcor or pycor = min-pycor
   ]
end

to-report field-not-defined?
   report not is-patch-set? field
end

to-report border-not-defined?
   report not is-patch-set? border
end

to define-screen-regions
   if field-not-defined?   [ define-field  ]
   if border-not-defined?  [ define-border ]
end

to reset-screen-regions
   define-screen-regions
   clear-field
   set-border
end

to clear-field
   ask balls [ pop ]
   ask field [ clear-grid ]
end

to set-border
   ask border 
   [ set void? false
     set pcolor sky
   ]
end
       
to do-drain
   if random 100 < drain-rate
   [ let drain-balls balls with [ pycor = min-pycor + 1 ]
     if any? drain-balls
     [ ask one-of drain-balls [ pop ] ]
   ]
end

to do-fill
   if random 100 < fill-rate
   [ let open-patches top-row with [ void? = true ]
     if any? open-patches
     [ ask one-of open-patches 
       [ make-ball]
     ]
   ]
end

to make-ball ; patch procedure
     sprout 1
     [ set breed balls
       set shape "circle"
       set heading 180
       set void? false
       let new-color who
       if new-color mod 10 <= 2 [ set new-color new-color + 2 ]
       if new-color mod 10 >= 8 [ set new-color new-color + 4 ]
       set color new-color 
     ]
end

to do-fall
   let below patch-at 0 -1
   let beside patches at-points [ [ -1 0 ] [ 1 0 ] ]
   let above patch-at 0  1
   ; if open space directly below, fall straight down
   ifelse [void?] of below = true
   [ set void? true
     set ycor ycor - 1
     set void? false
   ]
   ; otherwise, if patch at left or right is clear,
   ; and patch below that is clear as well, fall down to the side
   [ set beside beside with [ void? = true and [void?] of patch-at 0 -1 = true ]
     ifelse any? beside 
     [ set beside one-of beside
       set void? true
       set xcor [pxcor] of beside
       set ycor pycor - 1
       set void? false
     ]
      ; otherwise, if patch above IS a ball, and any side patch is open, pick a direction, and go that way
     [ set beside beside with [ void? = true ]
       ifelse (     [void?] of above = false
                and ( [ any? balls-here ] of above = true )
                and ( any? beside )
              )
        [ set beside one-of beside 
          set void? true
          set xcor [pxcor] of beside
          set void? false
        ]
        ; otherwise, if patch above is (non ball) obstacle, and exactly one side is open, roll that way
        [ if (     [void?] of above = false
               and ([ not any? balls-here ] of above = true )
               and any? beside with [ void? = true ]
               and any? beside with [ not void? = true ]
          )
          [  
            set beside one-of beside with [ void? = true ]
            set void? true
            set xcor [pxcor] of beside
            set void? false
          ]
        ]
      ]
    ] 
end

to randomize-order
   if any? balls
   [ ask one-of balls
     [ hatch 1 [set color color + 1]
       die
     ]
   ]
end
  
to do-mouse
   let here patch mouse-xcor mouse-ycor
   if last-drop != here
   [ set last-drop here
     ask here
     [ ifelse draw-mode?
         [ if (mode != 2 and pcolor = black) or (mode = 1 and pcolor != blue )
           [ let nib here
             if fat-pen?
             [ set nib (field in-radius-nowrap 1.5 )
                 with [ void? = true or any? balls-here ]
             ]
             ask nib
             [ ask balls-here [ pop ]
               set-grid
             ]
             set mode 1
           ]  
           if (mode != 1 and pcolor = gray ) or (mode = 2 and pcolor != blue )
           [ let nib here
             if fat-pen?
             [ set nib (field in-radius-nowrap 1.5 )
                 with [ void? = false and (pcolor = gray or any? balls-here) ] 
             ]
             ask nib
             [ ask balls-here [ pop ]
               clear-grid
             ]
             set mode 2
           ]
         ]
         [ if not any? balls-here and void? = true
           [ make-ball
           ]
         ]
       ]
     ]
end

to clear-obstacles
   without-interruption
   [ ask patches with [ void? = false and pcolor = gray ] 
     [ clear-grid ]
   ]
end

to clear-particles
   without-interruption
   [ ask balls [ pop ] ]
end

to make-funnel
   let funnel-tip patch 0 (int (max-pycor * .25))
   ask field with [ pycor < ( max-pycor * .75 ) and self != ( funnel-tip ) and ( towards funnel-tip = 135 or towards funnel-tip = 225 ) ]
   [ set-grid ]
   ask field with [ pycor = int( max-pycor * .75 ) and abs pxcor > (max-pxcor * .25) ]
   [ set-grid ]
   ask funnel-tip
   [ clear-grid ]
end   

to make-splotches
   let number 5
   let min-size max-pxcor / 8
   ask n-of number field with [ pycor < max-pycor * -.33 and pycor > max-pycor * -.75 ]
   [ ask field in-radius-nowrap (min-size + random min-size )
     [ set-grid ]
   ]
end

to make-pins
   ask field with [ pycor >= max-pycor * -.2 and pycor <= max-pycor * .2 and  (pycor + 1) mod 2 = 0 and floor (pxcor + pycor / 2) mod 2 = 0 ]
   [ set-grid ]
end

to clear-grid
   set pcolor black
   set void? true
end

to set-grid
   set pcolor gray
   set void? false
end     
     
to pop
   hide-turtle
   set void? true
   die
end   
   

                    


Download Link

View or download the complete model file (to download: right-click, save-link-as):
-- Download ball-fall_2009 --