Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 4.31 KB | None | 0 0
  1. globals [
  2.   lanes ;a list of the y coordinates different lanes
  3. ]
  4.  
  5. turtles-own[
  6.   speed
  7.   speed-limit
  8.   speed-min
  9.   patience
  10.   target-lane
  11.   top-speed
  12. ]
  13.  
  14. to setup
  15.   clear-all
  16.   draw-road
  17.   create-or-remove-cars
  18.   reset-ticks
  19.  
  20. end
  21.  
  22. to create-or-remove-cars
  23.   ;to control how many cars we have on the road
  24.   let road-patches patches with [member? pycor lanes]
  25.   if number-of-cars > count road-patches [
  26.      set number-of-cars count road-patches
  27.  ]
  28.  
  29.   set-default-shape turtles "triangle"
  30.   create-turtles green-cars [
  31.     move-to one-of free road-patches
  32.     set target-lane pycor
  33.     set color green
  34.     set heading 90
  35.     set top-speed 0.5 + random-float 0.5
  36.     set speed 0.5
  37.    
  38.   ]
  39.   create-turtles red-cars[
  40.     move-to one-of free road-patches
  41.     set target-lane pycor
  42.     set color red
  43.     set heading 90
  44.     set speed 0.56 + random-float 0.64
  45.     set speed-limit 0.65
  46.     set speed-min 0.55
  47.     set patience random max-patience
  48.   ]
  49.  
  50. end
  51.  
  52. to draw-road
  53.   ask patches[
  54.     set pcolor black - random-float 0.5
  55.   ]
  56.   set lanes n-values number-of-lanes [n -> number-of-lanes - (n * 2) - 1]
  57.   ask patches with [ abs pycor <= number-of-lanes][
  58.     ; set the color of the road
  59.     set pcolor white - 2.5 + random-float 0.25
  60.   ]
  61.   draw-road-lines
  62. end
  63.  
  64. to draw-road-lines
  65.   let y (last lanes) - 1
  66.   while [y <= first lanes + 1][
  67.     if not member? y lanes[
  68.       ;draw lines on road that are not patches
  69.       ifelse abs y = number-of-lanes
  70.         [draw-line y yellow 0] ;yellow for the sides of the road
  71.         [draw-line y white 0.5] ; dashed white line between the lanes
  72.     ]
  73.     set y y + 1 ; move up one patch
  74.   ]
  75. end
  76.  
  77. to draw-line [ y line-color gap ]
  78.   ;with a gap of zero we get a continous line
  79.   ;with a gap greater than zero, we get a dashed line.
  80.   create-turtles 1 [
  81.     setxy (min-pxcor - 0.5) y
  82.     hide-turtle
  83.     set color line-color
  84.     set heading 90
  85.     repeat world-width[
  86.       pen-up
  87.       forward gap
  88.       pen-down
  89.       forward (1 - gap)
  90.     ]
  91.     die
  92.   ]
  93. end
  94.  
  95. to go
  96.   create-or-remove-cars
  97.   ask turtles [ move-forward ]
  98.   ask turtles with [ patience <= 0][choose-new-lane]
  99.   ask turtles with [ycor != target-lane][move-to-target-lane]
  100.   tick
  101. end
  102.  
  103. to move-forward
  104.   set heading 90
  105.   speed-up-car ; we tentatively speed up, but might have to slow down
  106.   let blocking-cars other turtles in-cone (1 + speed) 180 with [y-distance <= 1]
  107.   let blocking-car min-one-of blocking-cars [distance myself]
  108.   if blocking-car != nobody [
  109.     ;match the speed of the car ahead and slow down
  110.     ;prevent from running into turtle
  111.     set speed [ speed ] of blocking-car
  112.     slow-down-car
  113.   ]
  114.   forward speed
  115. end
  116.  
  117. to slow-down-car;;turtle procedure
  118.   ;;slow down so you won't run into turtle infront
  119.  set speed (speed - deceleration)
  120.  if speed < 0 [set speed deceleration]
  121.  ;every slow down loose patience
  122.  set patience patience - 1
  123. end
  124.  
  125. to speed-up-car ;;turtle procedure
  126.  set speed (speed + acceleration)
  127.  if speed > top-speed [ set speed top-speed ]
  128. end
  129.  
  130. to choose-new-lane ;method controlling turtle behavior
  131.  ;choose a new lane among those with the minimum amount of cars
  132.  ;ycor is the distance to your current lane
  133.  let other-lanes remove ycor lanes
  134.  if not empty? other-lanes[
  135.    let min-dist min map [y -> abs (y - ycor) ] other-lanes
  136.    let closest-lanes filter [y -> abs (y - ycor) = min-dist ] other-lanes
  137.    set target-lane one-of closest-lanes
  138.    set patience max-patience
  139.  ]
  140. end
  141. to move-to-target-lane; turtle behavior
  142.  set heading ifelse-value (target-lane < ycor)[ 180 ][ 0 ]
  143.  let blocking-cars other turtles in-cone (1 + abs (ycor - target-lane)) 180 with [ x-distance <= 1]
  144.  let blocking-car min-one-of blocking-cars [ distance myself ]
  145.  ifelse blocking-car = nobody [
  146.    forward 0.2
  147.    set ycor precision ycor 1
  148.  ][
  149.    ;slow down if car blocking is behind otherwise speed up
  150.    ifelse towards blocking-car <= 180 [ slow-down-car ][ speed-up-car ]
  151.  ]
  152. end
  153.  
  154. to-report free [road-patches] ;turtle behavior
  155.  let this-car self
  156.  report road-patches with [
  157.    not any? turtles-here with [ self != this-car ]
  158.  ]
  159. end
  160.  
  161. to-report x-distance
  162.  report distancexy [ xcor ] of myself ycor
  163. end
  164.  
  165. to-report y-distance
  166.  report distancexy xcor [ ycor ] of myself
  167. end
  168. to-report number-of-lanes
  169.  report 2
  170. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement