sync on:sync rate 0:sync
 
set display mode 1024,768,16
 
randomize timer()
 
 
 
make camera 1
color backdrop 1,0x00bfff
set camera range 1,1,500
position light 0,0,600,0
make light 1
position light 1,0,-600,0
make light 2
position light 2,0,0,-600
make light 3
position light 3,0,0,600
make light 4
position light 4,-600,0,0
make light 5
position light 5,600,0,0
 
 
 
move camera 1,-1000
pitch camera up 1,90
move camera 1,10
pitch camera down 1,90
 
 
 
 
global stopNum
global col_check
global car
global dim cars(1000,7)
global dim lights(100,2)
 
ink 0,0
 
 
make_first_car()
 
make_textures()
 
make_stoplight()
 
build_city2()
 
make_cars(300)
 
 
 
 
do
 
   fastsync
   text 0,0,"FPS "+str$(screen fps())
 
   if mouseclick()=1
      if clicked=0
         pitch camera up 1,90
         move camera 1,150
         pitch camera down 1,180
         clicked=1
      endif
   else
      if clicked=1
         clicked=0
         move camera 1,150
         pitch camera up 1,90
      endif
      control camera using arrowkeys 1,8,3
   endif
 
   control_cars()
 
   work_stoplight()
 
 
 
loop
 
 
 
function make_textures()
 
   ink rgb(178,34,34),rgb(178,34,34)
   box 1,1,10,5
   ink rgb(100,100,100),rgb(100,100,100)
   line 1,1,10,1
   line 1,1,1,5
   line 1,5,10,5
   line 10,1,10,5
   get image 1,0,0,11,6,1
   cls
 
 
 
   for j=0 to 150
      for i=0 to 51
         if j mod 2
            paste image 1,i*10,j*5
         else
            paste image 1,i*10-5,j*5
         endif
      next i
   next j
 
   ink rgb(176,226,255),rgb(176,226,255)
   windows()
 
   get image 1,0,0,500,750,1
 
   cls
 
   ink rgb(255,231,186),rgb(255,231,186)
   box 0,0,500,750
   ink rgb(54,100,139),rgb(54,100,139)
   windows()
 
   get image 2,0,0,500,750,1
 
   cls
 
 
   ink rgb(20,20,20),rgb(20,20,20)
   box 0,0,500,750
 
   ink rgb(176,226,255),rgb(176,226,255)
   windows()
 
   get image 3,0,0,500,750,1
   cls
   sync
 
endfunction
 
function windows()
 
 
   for j=5 to 145 step 10
      for i=1 to 50 step 5
         box i*10,j*5,i*10+5,j*5+10
      next i
   next j
 
endfunction
 
function control_cars()
 
   for i=1 to car
      cars(i,5)=object position x(i+5000)
      cars(i,6)=object position z(i+5000)
   next i
 
 
   col_check=col_check+1
   if col_check>5
      col_check=1
   endif
 
 
   for i=1 to car
 
 
         a=wrapvalue(object angle y(i+5000))
 
 
         if cars(i,3)<>2
            if a>85 and z<95
               cars(i,1)=1
               yrotate object i+5000,90
            endif
            if a>265 and a<275
               cars(i,1)=1
               yrotate object i+5000,270
            endif
            if a>175 and a<185
               cars(i,1)=0
               yrotate object i+5000,180
            endif
            if (a>355 and a<365) or (a>-5 and a<5)
               cars(i,1)=0
               yrotate object i+5000,0
            endif
         endif
 
 
 
         moved=0
         no=0
         x=cars(i,5)
         y=cars(i,6)
 
         if y>650 or y<-650 or x>650 or x<-650
            move object i+5000,-1250
         endif
 
 
         for j=1 to stopNum
            x2=object position x(j+1000)
            y2=object position z(j+1000)
            if x>x2-25 and x<x2+25 and y>y2-25 and y<y2+25
               no=1
 
               if cars(i,3)=0
                  cars(i,3)=rnd(1)+1
                  if cars(i,3)=2
                     cars(i,4)=24
                  endif
               endif
 
               if lights(j,2)=cars(i,1)
                  move object i+5000,1
 
                  if cars(i,3)=2
                     if cars(i,4)>0
                        turn object right i+5000,3.75`yrotate object i+5000,object angle y(i+5000)+10
                        cars(i,4)=cars(i,4)-1
                     else
                        if cars(i,1)=1
                           cars(i,1)=0
                        else
                           cars(i,1)=1
                        endif
                        cars(i,3)=1
                        cars(i,4)=100
                     endif
                  endif
 
 
                  moved=1
                  cars(i,2)=1
               else
                  if cars(i,2)=1
                     move object i+5000,1
                     moved=1
                     if cars(i,3)=2
                        if cars(i,4)>0
                           turn object right i+5000,3.75`yrotate object i+5000,object angle y(i+5000)+10
                           cars(i,4)=cars(i,4)-1
                        else
                           if cars(i,1)=1
                              cars(i,1)=0
                           else
                              cars(i,1)=1
                           endif
                           cars(i,3)=1
                           cars(i,4)=100
                        endif
                     endif
                  endif
 
               endif
            endif
         next j
 
         if no=0
            move object i+5000,1
            moved=1
            if cars(i,3)=2
               move object right i+5000,1.78
            endif
            cars(i,2)=0
            cars(i,3)=0
            cars(i,4)=0
         endif
 
 
 
         if moved
 
            for j=1 to car
                if i<>j
                   if car_col(i,j)
 
                     if cars(i,3)=2
                        cars(i,4)=cars(i,4)+1
                        turn object right i+5000,-3.75
                     endif
 
                     move object i+5000,-1
 
                   endif
                endif
            next j
         endif
 
 
   next i
 
 
 
endfunction
 
function car_col(num1,num2)
 
   dist=(cars(num2,5)-cars(num2,5))^2+(cars(num1,6)-cars(num2,6))^2
 
   if dist<400
      yes=object collision(num1+5000,num2+5000)
   endif
 
endfunction yes
 
 
function make_cars(n)
 
   for i=1 to n
      make_car()
   next i
 
endfunction
 
function make_first_car()
 
   make object box 4999,5,5,10
   hide object 4999
   make object collision box 4999,-2.5,0,-7.5,2.5,5,7.5,1
 
endfunction
 
function make_car()
 
   car=car+1
 
   instance object car+5000,4999
   show object car+5000
 
   repeat
      redo=0
      a=rnd(3)
      select a
         case 0
            x=rnd(9)-5
            x=x*100
            cars(car,1)=0
 
            y=rnd(1100)-550
            yrotate object car+5000,0
         endcase
         case 1
            x=rnd(9)-5
            x=x*100
            cars(car,1)=0
 
            y=rnd(1100)-550
 
            yrotate object car+5000,180
         endcase
         case 2
            y=rnd(9)-5
            y=y*100
            cars(car,1)=1
            x=rnd(1100)-550
 
            yrotate object car+5000,90
         endcase
         case 3
            y=rnd(9)-5
            y=y*100
            cars(car,1)=1
 
            x=rnd(1100)-550
 
            yrotate object car+5000,270
         endcase
      endselect
 
      position object car+5000,x,2.6,y
      move object right car+5000,10
 
      for j=1 to stopNum
         x2=object position x(j+1000)
         y2=object position z(j+1000)
         if x>x2-25 and x<x2+25 and y>y2-25 and y<y2+25
            redo=1
         endif
      next j
 
      for j=1 to car-2
          if object collision(car+5000,j+5000)
            redo=1
          endif
      next j
 
   until redo=0
 
   cars(car,2)=1
 
 
endfunction
 
 
function build_city2()
 
   make object plain 1,1200,1100
   pitch object down 1,90
   position object 1,0,0,-50
   color object 1,0
 
   num=1
 
   for x=0 to 10
      for y=0 to 10
         num=num+1
         make object box num,60,rnd(200-50)+50,60
         position object num,x*100-550,object size y(num)/2,y*100-550
         texture object num,rnd(2)+1
      next y
   next x
 
   for x=-5 to 4
      for y=-5 to 4
         make_stoplights(x*100,y*100)
      next y
   next x
 
endfunction
 
function make_stoplights(x,y)
 
   stopNum=stopNum+1
   objnum=stopNum+1000
 
 
   lights(stopNum,1)=rnd(300)
   lights(stopNum,2)=rnd(1)
   clone object objnum,1000
   position object objnum,x,object size y(objnum)/5,y
 
endfunction
 
function make_stoplight()
 
   objnum=1000
 
 
 
   make object cylinder objnum,10
   scale limb objnum,0,5,100,5
   make object box objnum+1,2,3,2
   make mesh from object 1,objnum+1
   delete object objnum+1
   add limb objnum,1,1
   position object objnum,0,5,0
   offset limb objnum,1,0,5,0
   delete mesh 1
   make object sphere objnum+1,1
   make mesh from object 2,objnum+1
   delete object objnum+1
 
 
 
   for i=2 to 13
      add limb objnum,i,2
   next i
   delete mesh 2
 
   for i=1 to 3
      offset limb objnum,i+1,0,i+3,-1
   next i
   for i=1 to 3
      offset limb objnum,i+4,0,i+3,1
   next i
   for i=1 to 3
      offset limb objnum,i+7,1,i+3,0
   next i
   for i=1 to 3
      offset limb objnum,i+10,-1,i+3,0
   next i
 
   for i=2 to 13 step 3
      color limb objnum,i,rgb(0,100,0)
   next i
 
   for i=3 to 13 step 3
      color limb objnum,i,rgb(100,100,0)
   next i
 
   for i=4 to 13 step 3
      color limb objnum,i,rgb(100,0,0)
   next i
 
   hide object objnum
 
 
 
endfunction
 
function work_stoplight()
 
   for j=1 to stopNum
 
      change=0
 
      objnum=j+1000
 
      lights(j,1)=lights(j,1)+1
 
      if lights(j,1)=275
         if lights(j,2)=1
            lights(j,2)=4
         else
            lights(j,2)=3
         endif
      endif
 
      if lights(j,1)>300
         lights(j,1)=0
         if lights(j,2)=4
            lights(j,2)=0
         else
            lights(j,2)=1
         endif
         change=1
      endif
 
      if change=1 or lights(j,1)=250
         for i=2 to 13 step 3
            color limb objnum,i,rgb(0,100,0)
         next i
 
         for i=3 to 13 step 3
            color limb objnum,i,rgb(100,100,0)
         next i
 
         for i=4 to 13 step 3
            color limb objnum,i,rgb(100,0,0)
         next i
 
         select lights(j,2)
            case 0
 
               if lights(j,1)<250
                  color limb objnum,2,rgb(0,255,0)
                  color limb objnum,5,rgb(0,255,0)
               else
                  color limb objnum,3,rgb(255,255,0)
                  color limb objnum,6,rgb(255,255,0)
               endif
 
               color limb objnum,10,rgb(255,0,0)
               color limb objnum,13,rgb(255,0,0)
            endcase
            case 1
               if lights(j,1)<250
                  color limb objnum,8,rgb(0,255,0)
                  color limb objnum,11,rgb(0,255,0)
               else
                  color limb objnum,9,rgb(255,255,0)
                  color limb objnum,12,rgb(255,255,0)
               endif
               color limb objnum,4,rgb(255,0,0)
               color limb objnum,7,rgb(255,0,0)
            endcase
         endselect
      endif
 
   next j
 
endfunction