maxpoints=20
read roads
dim points(roads)
dim speedlimit(roads)
dim way(roads)
dim rx(roads,maxpoints)
dim ry(roads,maxpoints)
dim inter(roads,maxpoints)
dim sect(roads,maxpoints)
dim flow(roads,maxpoints)
dim lights(roads,4)
scale=4
traffic_lights=0
for j=1 to roads
  read points(j),speedlimit(j),way(j)
  for i=1 to points(j)
    read rx(j,i),ry(j,i),inter(j,i),sect(j,i),flow(j,i)
    rx(j,i)=rx(j,i)*scale
    ry(j,i)=ry(j,i)*scale
    if flow(j,i)>1
      if lights(flow(j,i),1)=0
        lights(flow(j,i),0)=0
        lights(flow(j,i),1)=j
        lights(flow(j,i),2)=i
        traffic_lights=flow(j,i)
      endif
    endif
  next i
next j
read cars
dim cx(cars) as float
dim cy(cars) as float
dim carroad(cars)
dim carpoint(cars)
dim road(cars)
dim carp(cars)
dim cardir(cars)
dim carturn(cars)
dim carspeed(cars) as float
dim carflow(cars)
for i=1 to cars
  read carroad(i),carpoint(i),cardir(i)
  cx(i)=rx(carroad(i),carpoint(i))
  cy(i)=ry(carroad(i),carpoint(i))
  road(i)=carroad(i)
  carp(i)=carpoint(i)
  carspeed(i)=0.0
  carturn(i)=0
next i
set display mode 1024,768,32
sync on
loops=0
do
  cls
  ink rgb(64,64,64),0
  for j=1 to roads
    for i=1 to points(j)
      if i>1
        for thick=-3 to 3
          line rx(j,i),ry(j,i)+thick,rx(j,i-1),ry(j,i-1)+thick
          line rx(j,i)+thick,ry(j,i),rx(j,i-1)+thick,ry(j,i-1)
        next thick
      endif
      if flow(j,i)=1
        x=rx(j,i)-rx(j,i-1)
        y=ry(j,i)-ry(j,i-1)
        h#=sqrt(x^2+y^2)
        ink rgb(255,0,0),0
        for thick=1 to 3
          if i>1 then circle rx(j,i)-x/h#-6,ry(j,i)-y/h#+6,thick
          if i<points(j) then circle rx(j,i)-x/h#+6,ry(j,i)-y/h#-6,thick
        next thick
        ink rgb(64,64,64),0
      endif
    next i
  next j
  for j=2 to traffic_lights
    if lights(j,0)<1000 then l1=1:l2=1
    if lights(j,0)>999 and lights(j,0)<3000 then l1=3:l2=1
    if lights(j,0)>3599 and lights(j,0)<4000 then l1=2:l2=1
    if lights(j,0)>3999 and lights(j,0)<4500 then l1=1:l2=1
    if lights(j,0)>4499 and lights(j,0)<6000 then l1=1:l2=3
    if lights(j,0)>5999 and lights(j,0)<6500 then l1=1:l2=2
    if lights(j,0)>5999 then lights(j,0)=0
    inc lights(j,0)
    x=rx(lights(j,1),lights(j,2))-rx(lights(j,1),lights(j,2)-1)
    y=ry(lights(j,1),lights(j,2))-ry(lights(j,1),lights(j,2)-1)
    h#=sqrt(x^2+y^2)
    ink rgb(255,0,0),0
    for thick=1 to 2
      if i>1 then circle rx(lights(j,1),lights(j,2))-x/h#-6,ry(lights(j,1),lights(j,2))-y/h#+6,thick
      if i<points(j) then circle rx(lights(j,1),lights(j,2))-x/h#+6,ry(lights(j,1),lights(j,2))-y/h#-6,thick
    next thick
  next j
  ink rgb(128,128,255),0
  for j=1 to cars
    col=0
    for k=1 to cars
      if k<>j
        if int(cx(j)/3)=int(cx(k)/3) and int(cy(j)/3)=int(cy(k)/3) and j>k
          col=1
        endif
      endif
    next k
    if col=0
      if cardir(j)=1
        box cx(j),cy(j),cx(j)+4,cy(j)+4
      else
        box cx(j)-4,cy(j)-4,cx(j),cy(j)
      endif
      if int(cx(j))=rx(road(j),carp(j)) and int(cy(j))=ry(road(j),carp(j))
        if carturn(j)=0 or inter(road(j),carp(j))=0
          carp(j)=carp(j)+cardir(j)
        else
          newroad=inter(road(j),carp(j))
          newpoint=sect(road(j),carp(j))
          cx(j)=rx(newroad,newpoint)
          cy(j)=ry(newroad,newpoint)
          cardir(j)=carturn(j)
          road(j)=newroad
          carp(j)=newpoint+cardir(j)
        endif
        if carp(j)>points(road(j)) or carp(j)=0
          carp(j)=carp(j)-cardir(j)
          newroad=inter(road(j),carp(j))
          newpoint=sect(road(j),carp(j))
          if newroad=0
            newroad=carroad(j)
            newpoint=carpoint(j)
            cx(j)=rx(newroad,newpoint)
            cy(j)=ry(newroad,newpoint)
          endif
          if newpoint+1<=points(newroad)
            cardir(j)=1
          else
            cardir(j)=-1
          endif
          road(j)=newroad
          carp(j)=newpoint+cardir(j)
        endif
        if inter(road(j),carp(j))>0
          if carp(j)<points(road(j)) and carp(j)>0
            turn=rnd(1)
          else
            turn=1
          endif
          if turn=1
            if way(inter(road(j),carp(j)))=0
              carturn(j)=rnd(1)*2-1
              if sect(road(j),carp(j))+carturn(j)>points(inter(road(j),carp(j))) or sect(road(j),carp(j))+carturn(j)=0
                carturn(j)=carturn(j)*-1
              endif
            else
              carturn(j)=way(inter(road(j),carp(j)))
              if sect(road(j),carp(j))+carturn(j)>points(inter(road(j),carp(j))) or sect(road(j),carp(j))+carturn(j)=0
                carturn(j)=0
              endif
            endif
          else
            carturn(j)=0
          endif
        endif
        if flow(road(j),carp(j))>0
          carflow(j)=1
        endif
      endif
      difx=rx(road(j),carp(j))-cx(j)
      dify=ry(road(j),carp(j))-cy(j)
      hyp#=sqrt(difx^2+dify^2)
      if inter(road(j),carp(j))>0 and flow(road(j),carp(j))>0 and hyp#<20.0 and carflow(j)>0
        if carspeed(j)>0.0
          carspeed(j)=carspeed(j)-0.5
          if carspeed(j)<=0.0
            carspeed(j)=0.0
          endif
          if hyp#<3.0 then carspeed(j)=0.0
          if carspeed(j)=0.0
            if flow(road(j),carp(j))=1
              col=0
              for k=1 to cars
                if k<>j
                  if road(k)=inter(road(j),carp(j)) and carp(k)=sect(road(j),carp(j))
                    disx=rx(road(k),carp(k))-cx(k)
                    disy=ry(road(k),carp(k))-cy(k)
                    colhyp#=sqrt(disx^2+disy^2)
                    if colhyp#<50.0
                      col=1
                    endif
                  endif
                endif
              next k
              if col=0 then carflow(j)=0
            endif
          endif
        else
          carspeed(j)=carspeed(j)+0.6
        endif
      else
        if (carp(j)=1 or carp(j)=points(road(j)) or carturn(j)<>0) and hyp#<10.0
          carspeed(j)=carspeed(j)-1.0
          if carspeed(j)<20.0 and carflow(j)=0 then carspeed(j)=20.0
        else
          if carspeed(j)<speedlimit(road(j))
            carspeed(j)=carspeed(j)+0.6
          else
            if carspeed(j)>speedlimit(road(j))
              carspeed(j)=carspeed(j)-0.5
              if carspeed(j)<0.0 then carspeed(j)=0.0
            endif
          endif
        endif
      endif
      cx(j)=cx(j)+difx/hyp#*carspeed(j)/100.0
      cy(j)=cy(j)+dify/hyp#*carspeed(j)/100.0
      col=0
      for k=1 to cars
        if k<>j
          if road(k)=road(j) and carp(k)=carp(j)
            disx=rx(road(k),carp(k))-cx(k)
            disy=ry(road(k),carp(k))-cy(k)
            colhyp#=sqrt(disx^2+disy^2)
            if hyp#>colhyp# and (hyp#-colhyp#)<10.0
              col=k
            endif
          endif
        endif
      next k
      if col>0
        carspeed(j)=carspeed(j)-carspeed(col)/2.0
        if carspeed(j)<0.0 then carspeed(j)=0.0
      endif
    endif
  next j
  inc loops
  sync
loop
data 21
data 20,50,0,28,190,2,1,0,138,190,6,1,0,182,190,0,0,0,187,189,0,0,0,192,185,0,0,0,195,180,0,0,0,196,176,0,0,0,196,172,0,0,0,198,167,0,0,0,202,163,0,0,0,208,160,0,0,0,214,158,0,0,0,236,158,0,0,0,242,156,0,0,0,246,153,0,0,0,249,148,0,0,0,250,143,0,0,0,251,117,7,2,0,251,38,11,6,0,251,16,12,2,0
data 3,50,0,28,190,1,1,0,28,166,3,1,0,28,110,5,1,0
data 3,50,0,28,166,2,2,1,68,166,4,1,0,138,166,6,2,1
data 2,50,0,68,166,3,2,1,68,110,5,2,0
data 2,50,0,28,110,2,3,0,68,110,4,2,0
data 8,60,0,138,190,1,2,1,138,166,3,3,0,138,112,20,1,0,138,108,17,4,0,138,94,18,1,0,138,90,19,1,0,138,76,9,2,0,138,38,11,3,1
data 2,50,0,219,117,8,1,0,251,117,1,18,1
data 2,50,0,219,117,7,1,0,219,38,11,5,1
data 3,50,0,99,76,13,1,0,138,76,6,7,1,188,76,10,1,0
data 3,50,0,188,76,9,3,0,188,38,11,4,1,188,16,12,1,0
data 6,50,0,36,38,15,1,0,99,38,13,2,1,138,38,6,6,0,188,38,10,2,1,219,38,8,2,1,251,38,1,19,1
data 2,50,0,188,16,10,3,0,251,16,1,20,0
data 3,50,0,99,76,9,1,0,99,38,11,2,1,99,15,14,2,0
data 2,50,0,36,15,15,2,0,99,15,13,3,0
data 2,50,0,36,38,11,1,0,36,15,14,1,0
data 6,100,1,0,49,0,0,0,109,91,0,0,0,115,93,17,1,0,157,110,0,0,0,161,111,0,0,0,256,148,0,0,0
data 4,50,1,115,93,16,3,0,121,102,0,0,0,132,107,0,0,0,138,108,6,4,1
data 5,50,-1,138,94,6,5,1,143,94,0,0,0,151,98,0,0,0,154,106,0,0,0,161,110,0,0,0
data 3,50,1,138,90,0,0,0,126,92,0,0,0,109,90,21,2,0
data 4,50,1,138,112,0,0,0,145,112,0,0,0,151,110,0,0,0,157,110,16,4,0
data 6,100,-1,0,48,0,0,0,109,90,0,0,0,115,92,0,0,0,157,109,0,0,0,161,110,18,5,0,256,147,0,0,0
data 30
data 16,1,1
data 16,1,1
data 16,1,1
data 16,1,1
data 16,1,1
data 16,1,1
data 16,1,1
data 16,1,1
data 16,1,1
data 16,1,1
data 16,1,1
data 16,1,1
data 16,1,1
data 16,1,1
data 16,1,1
data 21,6,-1
data 21,6,-1
data 21,6,-1
data 21,6,-1
data 21,6,-1
data 21,6,-1
data 21,6,-1
data 21,6,-1
data 21,6,-1
data 21,6,-1
data 21,6,-1
data 21,6,-1
data 21,6,-1
data 21,6,-1
data 21,6,-1