Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- globals [
- lanes ;a list of the y coordinates different lanes
- ]
- turtles-own[
- speed
- speed-limit
- speed-min
- patience
- target-lane
- top-speed
- ]
- to setup
- clear-all
- draw-road
- create-or-remove-cars
- reset-ticks
- end
- to create-or-remove-cars
- ;to control how many cars we have on the road
- let road-patches patches with [member? pycor lanes]
- if number-of-cars > count road-patches [
- set number-of-cars count road-patches
- ]
- set-default-shape turtles "triangle"
- create-turtles green-cars [
- move-to one-of free road-patches
- set target-lane pycor
- set color green
- set heading 90
- set top-speed 0.5 + random-float 0.5
- set speed 0.5
- ]
- create-turtles red-cars[
- move-to one-of free road-patches
- set target-lane pycor
- set color red
- set heading 90
- set speed 0.56 + random-float 0.64
- set speed-limit 0.65
- set speed-min 0.55
- set patience random max-patience
- ]
- end
- to draw-road
- ask patches[
- set pcolor black - random-float 0.5
- ]
- set lanes n-values number-of-lanes [n -> number-of-lanes - (n * 2) - 1]
- ask patches with [ abs pycor <= number-of-lanes][
- ; set the color of the road
- set pcolor white - 2.5 + random-float 0.25
- ]
- draw-road-lines
- end
- to draw-road-lines
- let y (last lanes) - 1
- while [y <= first lanes + 1][
- if not member? y lanes[
- ;draw lines on road that are not patches
- ifelse abs y = number-of-lanes
- [draw-line y yellow 0] ;yellow for the sides of the road
- [draw-line y white 0.5] ; dashed white line between the lanes
- ]
- set y y + 1 ; move up one patch
- ]
- end
- to draw-line [ y line-color gap ]
- ;with a gap of zero we get a continous line
- ;with a gap greater than zero, we get a dashed line.
- create-turtles 1 [
- setxy (min-pxcor - 0.5) y
- hide-turtle
- set color line-color
- set heading 90
- repeat world-width[
- pen-up
- forward gap
- pen-down
- forward (1 - gap)
- ]
- die
- ]
- end
- to go
- create-or-remove-cars
- ask turtles [ move-forward ]
- ask turtles with [ patience <= 0][choose-new-lane]
- ask turtles with [ycor != target-lane][move-to-target-lane]
- tick
- end
- to move-forward
- set heading 90
- speed-up-car ; we tentatively speed up, but might have to slow down
- let blocking-cars other turtles in-cone (1 + speed) 180 with [y-distance <= 1]
- let blocking-car min-one-of blocking-cars [distance myself]
- if blocking-car != nobody [
- ;match the speed of the car ahead and slow down
- ;prevent from running into turtle
- set speed [ speed ] of blocking-car
- slow-down-car
- ]
- forward speed
- end
- to slow-down-car;;turtle procedure
- ;;slow down so you won't run into turtle infront
- set speed (speed - deceleration)
- if speed < 0 [set speed deceleration]
- ;every slow down loose patience
- set patience patience - 1
- end
- to speed-up-car ;;turtle procedure
- set speed (speed + acceleration)
- if speed > top-speed [ set speed top-speed ]
- end
- to choose-new-lane ;method controlling turtle behavior
- ;choose a new lane among those with the minimum amount of cars
- ;ycor is the distance to your current lane
- let other-lanes remove ycor lanes
- if not empty? other-lanes[
- let min-dist min map [y -> abs (y - ycor) ] other-lanes
- let closest-lanes filter [y -> abs (y - ycor) = min-dist ] other-lanes
- set target-lane one-of closest-lanes
- set patience max-patience
- ]
- end
- to move-to-target-lane; turtle behavior
- set heading ifelse-value (target-lane < ycor)[ 180 ][ 0 ]
- let blocking-cars other turtles in-cone (1 + abs (ycor - target-lane)) 180 with [ x-distance <= 1]
- let blocking-car min-one-of blocking-cars [ distance myself ]
- ifelse blocking-car = nobody [
- forward 0.2
- set ycor precision ycor 1
- ][
- ;slow down if car blocking is behind otherwise speed up
- ifelse towards blocking-car <= 180 [ slow-down-car ][ speed-up-car ]
- ]
- end
- to-report free [road-patches] ;turtle behavior
- let this-car self
- report road-patches with [
- not any? turtles-here with [ self != this-car ]
- ]
- end
- to-report x-distance
- report distancexy [ xcor ] of myself ycor
- end
- to-report y-distance
- report distancexy xcor [ ycor ] of myself
- end
- to-report number-of-lanes
- report 2
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement