Java StarLogo
2.0
english
`turtle`
breeds [lefts rights swans polices]
turtles-own [speed SpeedLimit]

to setup-cars
  if breed = rights [
    setxy (random (screen-width)) -1 
    seth 90
    setspeed ((random 10) / 10) + 0.1 
    setSpeedLimit 1
    setshape right-car-shape]
  if breed = lefts [
    setxy random (screen-width) 3
    seth 270
    setspeed ((random 10) / 10) + 0.1
    setSpeedLimit 1
    setshape left-car-shape]
end

to swim      ;swams follow the river when they swim
             ;they don't actually flock
  if breed = swans 
     [ifelse xcor < -18 
        [seth 85 + random 10] 
        [ifelse xcor < 5 
           [seth 58 + random 10] 
           [seth 115 + random 10]] 
      jump (random 500) / 10000]
end

to drive
  if breed = rights 
    [ifelse (count-turtles-at 1 0) > 0     ;if there is a turtle 1 space ahead, decelerate 
       [setspeed speed-of one-of-turtles-at 1 0
        decelerate]
       [ifelse lookahead = 2      ;if lookahead=2, check 2 spaces ahead also
         [ifelse (count-turtles-at 1 0) > 0
            [setspeed speed-of one-of-turtles-at 2 0
             decelerate]
            [accelerate]]   ;else accelerate
         [accelerate]]
  if speed < 0.01 [setspeed 0.01]   ;also adjust speed based on SpeedLimit and radar
  if speed > SpeedLimit [setspeed SpeedLimit]
  if radar = true [setspeed .10]
  jump speed] 

  if breed = lefts 
    [ifelse (count-turtles-at (-1) 0) > 0      ;if there is a turtle 1 space ahead, decelerate
       [setspeed speed-of one-of-turtles-at (-1) 0
        decelerate]
       [ifelse lookahead = 2      ;if lookahead=2, check 2 spaces ahead also
         [ifelse (count-turtles-at (-2) 0) > 0
            [setspeed speed-of one-of-turtles-at (-2) 0 
             decelerate]
            [accelerate]]   ;else accelerate
         [accelerate]]
  if speed < 0.01 [setspeed 0.01]      ;also adjust speed based on SpeedLimit and radar
  if speed > SpeedLimit [setspeed SpeedLimit]
  if radar = true [setspeed .10]
  jump speed]
end

to accelerate
  setspeed (speed + (speedup / 10000))
end

to decelerate
  setspeed speed - (slowdown / 1000)
end


`observer`
patches-own [flock radar picture]

to setup
  ask-turtles [die] 
  create-rights number 
  create-lefts int (number / 2)
  ask-turtles [setup-cars if color = gray [setc red]] 
  swan-flock 
  ask-patches [if pc = (red - 2)    ;make sure the radar is off to begin with
                 [setpc picture] 
               setpicture pc setradar false]
end

to setup-police 
  create-polices 1
  ask-polices [setxy 12 1.5 setshape right-car-shape setc blue]
  ask-patches 
     [setradar false         
      if (distance 12 1) < 3.5      ;turn the police radar on
        [setradar true] ]
end

to remove-police
  ask-polices [die]
  ask-patches [if radar = true [setpc picture]   ;turn off the radar
	      setradar false]
end

to see-radar
     ;have the radar=true patches change their color
     ;this means the user can see the radar
     ;the cars (rights and lefts) can always see the radar
  ask-patches [if radar = true [setpc (red - 2)]]
end

to swan-flock
;if the patches are where we want the swans to be, 
;create swans there
  ask-patches [setflock ((distance -22.5 -21.5) < 2)
	      if flock = true [ sprout [ ] ] ]
  ask-turtles [if ycor < -10 [setbreed swans setc white setshape swan-shape]]
  ask-swans [setspeed .01
             seth 350 + random 10]
end

to go
startdrivebutton
startswimbutton
end

to stop-it
stopdrivebutton
stopswimbutton
end


`information`
For use with the Adventures in Modeling book,  by Vanessa Colella, Eric Klopfer, and Mitchel Resnick published by Teachers College Press, 2001.

For more information, see  
<http://education.mit.edu/starlogo/adventures>  

Challenge 10: Traffic  

This project models cars and trucks driving on a highway and swans swimming in a nearby river. Use the buttons "setup", "go", and "stop-it" to control the simulation. The "number" slider  controls the initial number of cars on the road. (You will have to setup the simulation again for a change in this slider to take effect.) The "lookahead" slider controls how many spaces ahead each car looks to see if there is open driving room, and "speedup" and "slowdown" control the rates for acceleration and deceleration.  

The cars in the traffic model follow three very simple rules: 	
If there is a car ahead, then slow down. 	
If there is no car ahead, then speed up. 	
If you see a radar trap, then slow down.  

In this project there are four breeds: 	
breeds [lefts rights swans police] 

If you look through the code, you will see that the swimming turtles have breed = swans and the driving turtles in the top lane have breed = lefts (similarly, those in the bottom lane have breed = rights). This project uses breeds to give different behaviors to different groups of turtles.


`interface`
SLCanvas
top-left 13 222

SLSlider
top-left 62 24
width-height 85 25
name "slider1"
variable "number"
min-value 0
max-value 25
current-value 10
slider-number 0
show-name? false

SLSlider
top-left 62 119
width-height 85 25
name "slider3"
variable "speedup"
min-value 0
max-value 100
current-value 19
slider-number 2
show-name? false

SLSlider
top-left 103 24
width-height 85 25
name "slider2"
variable "lookahead"
min-value 1
max-value 2
current-value 1
slider-number 1
show-name? false

SLButton
turtle-or-observer? observer
top-left 187 28
width-height 81 30
name "button4"
line-to-run "remove-police"
forever? false
button-number 5
show-name? false
whichip 12

SLButton
turtle-or-observer? turtle
top-left 249 56
width-height 58 30
name "swimbutton"
line-to-run "swim"
forever? true
button-number 7
show-name? false
whichip 13

SLButton
turtle-or-observer? turtle
top-left 249 122
width-height 58 30
name "drivebutton"
line-to-run "drive"
forever? true
button-number 2
show-name? false
whichip 14

SLSlider
top-left 103 119
width-height 85 25
name "slider4"
variable "slowdown"
min-value 0
max-value 100
current-value 98
slider-number 3
show-name? false

SLButton
turtle-or-observer? observer
top-left 161 134
width-height 63 30
name "button5"
line-to-run "see-radar"
forever? false
button-number 6
show-name? false
whichip 15

SLButton
turtle-or-observer? observer
top-left 143 28
width-height 81 31
name "button3"
line-to-run "setup-police"
forever? false
button-number 4
show-name? false
whichip 16

SLButton
turtle-or-observer? observer
top-left 13 85
width-height 54 30
name "button7"
line-to-run "go"
forever? false
button-number 8
show-name? false
whichip 17

SLButton
turtle-or-observer? observer
top-left 13 155
width-height 54 30
name "button8"
line-to-run "stop-it"
forever? false
button-number 9
show-name? false
whichip 18

SLButton
turtle-or-observer? observer
top-left 13 14
width-height 54 30
name "button2"
line-to-run "setup"
forever? false
button-number 3
show-name? false
whichip 19


`turtle shapes`
[encoded data, 8450 characters]
lX4e51dVVVVVVVXtt/8HDAjQT/QOFQAAFQ70

`template`
null

`settings`
patch-size 8
num-shapes 256
screen-half-width 25
screen-half-height 25
interface-window-xcor 455
interface-window-ycor 43
interface-window-size 658 446
output-window-xcor 0
output-window-ycor 0
output-window-width 300
output-window-height 219
info-window-xcor 320
info-window-ycor 197
info-window-width 500
info-window-height 419
control-center-xcor 10
control-center-ycor 48
control-center-width 429
control-center-height 580
turtle-command-center-height 150
observer-command-center-height 150

`string table`
H4sIAAAAAAAAAGNgYGAAABzfRCEEAAAAAAAABA==

`symbol table`
H4sIAAAAAAAAAGNgYGBiYE1LzClOZWApKSpNBQDlw1heEQAAAAAAABE=

`double table`
H4sIAAAAAAAAAGNgYJB3CD/AgAwclKC0HpQWgdJuDRA6wAFM27dUPXRfJ1Jt/wAi
b//mDAictd85EwRm2Z+E0jeh9GNjMLB/lgYG9i/B4jPtP0DNh9pv/wPKl4HSURAZ
Bx4I/4AZxB0HTKG0ClQdB5RWgND7YeZKQPUZQWmoPQCRD4p1/AAAAAAAAPw=

`list table`
H4sIAAAAAAAAAJVRSQ7CMAy0E0pFWVRoWQtc+QPiG1w48Ak+zQN4ADOKU6IIIXGY
jj0Zx3YqIlMBVMQBAqgx88KJeMJyh3jucQ6uQTNoA6shT6AvgAZoo486PYiXPjBr
h7HWh3qX3NvrsTe0Gp8VsE5mFaulYQNsEY/AOw1zl8Gnd3AltgNQZnHLvYAO+Y1e
wgUWm539G9sVM+nsh2+PdMx5gAP0q3kL4GzxI7C+VE7c+cIZkveP0Cx2Xzwuzbn3
Z/e+LtbEd63Sc9s9vaPL+0I7Zp48/6uv/Web/1RnGt7YP02TN1XB4zyoAgAAAAAC
qA==

`bytecodes`
H4sIAAAAAAAAAL1W6W7TQBCe9Rk7l5vETUhbSGkpZ7hariIkxE94iYbEoIi2qZK0
FQ/A28E7sbF3ZsdelyIhYSkZez0z+803364Xqkefvy+T8WySLI7ew8/V9QsAOuAu
kuX5mbz9BHRZ6q8njS1/Ox6AUO9E9id2pem56tlS1s7G03sHRCSfo83ML80ZAfgr
69NUIm4of1f6dpWfo8bkvHGUYWhaGouvfHJ4b4DoCBXnK1uROQXLi3gDEG1503Eo
t44NAdo4f1XZWjaWzi9tc5VIjqbUDc9mx9NxIp/3NaICW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