Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- breed [ lights light ]
- breed [ moths moth ]
- globals
- [
- scale-factor ;; to control the form of the light field
- ]
- lights-own
- [
- intensity
- ]
- moths-own
- [
- ;; +1 means the moths turn to the right to try to evade a bright light
- ;; (and thus circle the light source clockwise). -1 means the moths
- ;; turn to the left (and circle the light counter-clockwise)
- ;; The direction tendency is assigned to each moth when it is created and does not
- ;; change during the moth's lifetime.
- direction
- ]
- patches-own
- [
- light-level ;; represents the light energy from all light sources
- ]
- to setup
- clear-all
- set-default-shape lights "circle 2"
- set-default-shape moths "butterfly"
- set scale-factor 50
- if number-lights > 0
- [
- make-lights number-lights
- ask patches [ generate-field ]
- ]
- make-moths number-moths
- reset-ticks
- end
- to go
- ask moths [ move-thru-field ]
- tick
- end
- ;;;;;;;;;;;;;;;;;;;;;;
- ;; Setup Procedures ;;
- ;;;;;;;;;;;;;;;;;;;;;;
- to make-lights [ number ]
- create-lights number [
- set color white
- jump 10 + random-float (max-pxcor - 30)
- set intensity random luminance + 20
- set size sqrt intensity
- ]
- end
- to make-moths [ number ]
- create-moths number [
- ifelse (random 2 = 0)
- [ set direction 1 ]
- [ set direction -1 ]
- set color white
- jump random-float max-pxcor
- set size 5
- ]
- end
- to generate-field ;; patch procedure
- set light-level 0
- ;; every patch needs to check in with every light
- ask lights
- [ set-field myself ]
- set pcolor scale-color blue (sqrt light-level) 0.1 ( sqrt ( 20 * max [intensity] of lights ) )
- end
- ;; do the calculations for the light on one patch due to one light
- ;; which is proportional to the distance from the light squared.
- to set-field [p] ;; turtle procedure; input p is a patch
- let rsquared (distance p) ^ 2
- let amount intensity * scale-factor
- ifelse rsquared = 0
- [ set amount amount * 1000 ]
- [ set amount amount / rsquared ]
- ask p [ set light-level light-level + amount ]
- end
- ;;;;;;;;;;;;;;;;;;;;;;;;
- ;; Runtime Procedures ;;
- ;;;;;;;;;;;;;;;;;;;;;;;;
- to move-thru-field ;; turtle procedure
- ifelse (light-level <= ( 1 / (10 * sensitivity) ))
- [
- ;; if there is no detectable light move randomly
- rt flutter-amount 45
- ]
- [
- ifelse (random 25 = 0)
- ;; add some additional randomness to the moth's movement, this allows some small
- ;; probability that the moth might "escape" from the light.
- [
- rt flutter-amount 60
- ]
- [
- ;; turn toward the brightest light
- maximize
- ;; if the light ahead is not above the sensitivity threshold head towards the light
- ;; otherwise move randomly
- ifelse ( [light-level] of patch-ahead 1 / light-level > ( 1 + 1 / (10 * sensitivity) ) )
- [
- lt ( direction * turn-angle )
- ]
- [
- rt flutter-amount 60
- ]
- ]
- ]
- if not can-move? 1
- [ maximize ]
- fd 1
- end
- to maximize ;; turtle procedure
- face max-one-of patches in-radius 1 [light-level]
- end
- to-report flutter-amount [limit]
- ;; This routine takes a number as an input and returns a random value between
- ;; (+1 * input value) and (-1 * input value).
- ;; It is used to add a random flutter to the moth's movements
- report random-float (2 * limit) - limit
- end
- ; Copyright 2005 Uri Wilensky.
- ; See Info tab for full copyright and license.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement