sync on
autocam off
 
tilecolour=0
heatmap=1
tilescalemax=10
 
roadcolour=rgb(100,100,100)
pavementcolour=rgb(150,150,150)
grasscolour1=rgb(0,100,0)
grasscolour2=rgb(0,80,0)
 
 
ink grasscolour1,0
box 0,0,49,49
ink grasscolour2,0
for count=0 to 200
  dot rnd(49),rnd(49)
next count
get image 1,0,0,49,49,1
 
ink roadcolour,0
box 0,0,99,49
ink pavementcolour,0
box 0,0,49,4
box 0,44,49,49
box 50,0,54,49
box 94,0,99,49
get image 9,0,0,49,49
get image 14,50,0,99,49
 
ink pavementcolour,0
box 0,0,49,49
ink roadcolour,0
fillcircle(49,49,45) 
ink pavementcolour,0
fillcircle(49,49,5)
get image 16,0,0,49,49,1
 
ink pavementcolour,0
box 0,0,49,49
ink roadcolour,0
fillcircle(0,49,45) 
ink pavementcolour,0
fillcircle(0,49,5)
get image 13,0,0,49,49,1
 
ink pavementcolour,0
box 0,0,49,49
ink roadcolour,0
fillcircle(49,0,45) 
ink pavementcolour,0
fillcircle(49,0,5)
get image 10,0,0,49,49,1
 
ink pavementcolour,0
box 0,0,49,49
ink roadcolour,0
fillcircle(0,0,44) 
ink pavementcolour,0
fillcircle(0,0,4)
get image 7,0,0,49,49,1
 
ink roadcolour,0
box 0,0,49,49
ink pavementcolour,0
fillcircle(0,0,5)
fillcircle(49,0,5)
fillcircle(0,49,5)
fillcircle(49,49,5)
get image 19,0,0,49,49,1
 
ink roadcolour,0
box 0,0,49,49
ink pavementcolour,0
fillcircle(49,0,5)
fillcircle(49,49,5)
box 0,0,4,49
get image 18,0,0,49,49,1
 
ink roadcolour,0
box 0,0,49,49
ink pavementcolour,0
fillcircle(0,49,5)
fillcircle(49,49,5)
box 0,0,49,4
get image 17,0,0,49,49,1
 
ink roadcolour,0
box 0,0,49,49
ink pavementcolour,0
fillcircle(0,0,5)
fillcircle(0,49,5)
box 44,0,49,49
get image 15,0,0,49,49,1
 
ink roadcolour,0
box 0,0,49,49
ink pavementcolour,0
fillcircle(0,0,5)
fillcircle(49,0,5)
box 0,44,49,49
get image 11,0,0,49,49,1
 
ink pavementcolour,0
box 0,0,49,49
ink roadcolour,0
fillcircle(24,24,20)
get image 4,0,0,49,49,1
 
ink pavementcolour,0
box 0,0,49,49
ink roadcolour,0
fillcircle(24,24,20)
box 0,4,25,44
get image 5,0,0,49,49,1
 
ink pavementcolour,0
box 0,0,49,49
ink roadcolour,0
fillcircle(24,24,20)
box 4,0,44,25
get image 6,0,0,49,49,1
 
ink pavementcolour,0
box 0,0,49,49
ink roadcolour,0
fillcircle(24,24,20)
box 25,4,49,44
get image 8,0,0,49,49,1
 
ink pavementcolour,0
box 0,0,49,49
ink roadcolour,0
fillcircle(24,24,20)
box 4,25,44,49
get image 12,0,0,49,49,1
 
ink pavementcolour,0
box 0,0,49,49
ink roadcolour,0
box 22,5,27,49
box 19,5,30,28
ink rgb(255,0,0),0
fillcircle(24,9,3)
ink rgb(255,128,0),0
fillcircle(24,16,3)
ink rgb(0,200,0),0
fillcircle(24,23,3)
 
get image 25,0,0,49,49,1
 
ink rgb(50,50,50),0
box 0,0,19,39
ink rgb(128,0,0),0
fillcircle(9,9,4)
ink rgb(128,64,0),0
fillcircle(9,19,4)
ink rgb(0,255,0),0
fillcircle(9,29,5)
get image 26,0,0,19,39,1
 
ink rgb(50,50,50),0
box 0,0,19,39
ink rgb(128,0,0),0
fillcircle(9,9,4)
ink rgb(255,128,0),0
fillcircle(9,19,5)
ink rgb(0,128,0),0
fillcircle(9,29,4)
get image 28,0,0,19,39,1
 
ink rgb(50,50,50),0
box 0,0,19,39
ink rgb(255,0,0),0
fillcircle(9,9,5)
ink rgb(128,64,0),0
fillcircle(9,19,4)
ink rgb(0,128,0),0
fillcircle(9,29,4)
get image 27,0,0,19,39,1
 
type lights
  exist as boolean
  direction as integer
  timecount as integer
  waiting as integer
  changing as integer
endtype
 
global roadcount as integer
dim landobjects(50,50)
dim landtestobjects(50,50)
dim landtile(50,50)
dim landstate(50,50)
dim landoccupied(50,50,8)
dim landheat(50,50)
dim landheated(50,50)
dim landbuildobjects(50,50,4)
dim landlights(50,50) as lights
 
for countx=0 to 50
  for countz=0 to 50
    landobjects(countx,countz)=findobj()
    make object plain landobjects(countx,countz),1,1
    xrotate object landobjects(countx,countz),-90
    position object landobjects(countx,countz),countx,0,countz
    texture object landobjects(countx,countz),1
    landtile(countx,countz)=1
    landstate(countx,countz)=0
  next countz
next countx
 
 
ink rgb(255,255,255),0
box 0,0,50,50
ink 0,0
text 10,15,"dest"
get image 20,1,1,50,50
 
destobj=findobj()
make object plain destobj,1,1
xrotate object destobj,-90
hide object destobj
texture object destobj,20
set alpha mapping on destobj,50
 
ink rgb(255,50,50),0
box 0,0,50,50
ink 0,0
text 10,15,"next"
get image 21,1,1,50,50
 
nextobj=findobj()
make object plain nextobj,1,1
xrotate object nextobj,-90
hide object nextobj
texture object nextobj,21
set alpha mapping on nextobj,50
 
ink rgb(50,50,255),0
box 0,0,50,50
ink 0,0
text 10,15,"curr"
 
get image 22,1,1,50,50
 
currobj=findobj()
make object plain currobj,1,1
xrotate object currobj,-90
hide object currobj
texture object currobj,22
set alpha mapping on currobj,50
 
global trafficlightobj as integer
trafficlightobj=findobj()
make object box trafficlightobj,0.05,0.4,0.05
color object trafficlightobj,rgb(150,150,150)
 
templightobj=findobj()
make object plain templightobj,0.1,0.2
yrotate object templightobj,90
fix object pivot templightobj
 
make mesh from object 1,templightobj
add limb trafficlightobj,1,1
offset limb trafficlightobj,1,-0.05,0.3,0
 
yrotate object templightobj,90
fix object pivot templightobj
delete mesh 1
make mesh from object 1,templightobj
add limb trafficlightobj,2,1
offset limb trafficlightobj,2,0,0.3,0.05
 
yrotate object templightobj,90
fix object pivot templightobj
delete mesh 1
make mesh from object 1,templightobj
add limb trafficlightobj,3,1
offset limb trafficlightobj,3,0.05,0.3,0
 
yrotate object templightobj,90
fix object pivot templightobj
delete mesh 1
make mesh from object 1,templightobj
add limb trafficlightobj,4,1
offset limb trafficlightobj,4,0,0.3,-0.05
 
delete object templightobj
make object plain templightobj,0.1,0.1
xrotate object templightobj,90
fix object pivot templightobj
delete mesh 1
make mesh from object 1,templightobj
add limb trafficlightobj,5,1
offset limb trafficlightobj,5,0,0.2,0
add limb trafficlightobj,6,1
offset limb trafficlightobj,6,0,0.4,0
 
hide object trafficlightobj
//position object trafficlightobj,24.5,0.2,1.5
//newlight=findobj()
//clone object newlight,trafficlightobj
//position object newlight,25.5,0.2,1.5
 
 
cursorx=20
cursorz=20
cursorobj=findobj()
make object box cursorobj,1,0.1,1
position object cursorobj,cursorx,0.05,cursorz
ghost object on cursorobj,1
`set alpha mapping on cursorobj,30
 
cursormode=0
 
position camera 21,10,10
point camera 21,0,21
 
updateland()
 
dim targeterobj(5)
for count=1 to 5
  targeterobj(count)=findobj()
  make object plain targeterobj(count),0.1,0.1
  hide object targeterobj(count)
next count
 
type Car
 obj as integer
 testobj as integer
 colour as integer
 posx as integer
 posy as integer
 destx as integer
 desty as integer
 nextx as integer
 nexty as integer
 prevx as integer
 prevy as integer
 propx as integer
 propy as integer
 sideofroad as integer
 changed as integer
endtype
 
dim cars(0) as Car
 
sw=screen width()
sh=screen height()
camtilt#=50
camzoom#=8
camrot#=0
cammodrange=2
camx#=25
camz#=0
position camera camx#,0,camz#
xrotate camera camtilt#
yrotate camera camrot#
move camera -camzoom#
mmx=mousemovex()
mmy=mousemovey() 
 
time=timer()
 
ink rgb(255,255,255),0
 
do
 
  lasttime=time
  time=timer()
  carupdate#=carupdate#+((time-lasttime)/1000.0)
 
  mx=mousex()
  my=mousey()
  closest=1
  if movingcam=0
    stop=0
    while (closest<>5) AND (stop=0)
      if ((cursorx>50) OR (cursorx<0)) OR ((cursorz>50) OR (cursorz<0)) then stop=1
      position object targeterobj(1),cursorx-1,0,cursorz
      position object targeterobj(2),cursorx,0,cursorz+1
      position object targeterobj(3),cursorx+1,0,cursorz
      position object targeterobj(4),cursorx,0,cursorz-1
      position object targeterobj(5),cursorx,0,cursorz
      mx=mousex()
      my=mousey()
      closestdist=sqrt(sqr(object screen x(targeterobj(1))-mx)+sqr(object screen y(targeterobj(1))-my))
      closest=1
      dist=sqrt(sqr(object screen x(targeterobj(2))-mx)+sqr(object screen y(targeterobj(2))-my))
      if dist<closestdist
        closest=2
        closestdist=dist
      endif
      dist=sqrt(sqr(object screen x(targeterobj(3))-mx)+sqr(object screen y(targeterobj(3))-my))
      if dist<closestdist
        closest=3
        closestdist=dist
      endif
      dist=sqrt(sqr(object screen x(targeterobj(4))-mx)+sqr(object screen y(targeterobj(4))-my))
      if dist<closestdist
        closest=4
        closestdist=dist
      endif
          dist=sqrt(sqr(object screen x(targeterobj(5))-mx)+sqr(object screen y(targeterobj(5))-my))
      if dist<closestdist
        closest=5
      endif
      select closest
        case 1
          cursorx=cursorx-1
          tilechange=0
        endcase
        case 2
          cursorz=cursorz+1
          tilechange=0
        endcase
        case 3
          cursorx=cursorx+1 
          tilechange=0 
        endcase
        case 4
          cursorz=cursorz-1
          tilechange=0
        endcase
      endselect
 
    endwhile
    if cursorx>50 then cursorx=50   
    if cursorx<0 then cursorx=0   
    if cursorz>50 then cursorz=50   
    if cursorz<0 then cursorz=0
    position object cursorobj,cursorx,0.05,cursorz
  endif
 
  mmx=mousemovex()
  mmy=mousemovey()
  movecam=0
  lmb=0
  mmb=0
  rmb=0
 
  mv=mouseclick()
  if int(mv/8)=1
    mv=mv-8
  endif
  if int(mv/4)=1
    mv=mv-4
    mmb=1
  endif
  if int(mv/2)=1
    mv=mv-2
    rmb=1
  endif
  if mv=1
    lmb=1
  endif
 
  if (mmb=1) OR (rmb=1)
    if movingcam=0
      cammousex=mousex()
      cammousey=mousey()
    endif
    if rmb=1
      if (mmx<>0) OR (mmy<>0)
        mcamx#=mcamx#-(mmx/30.0)
        mcamz#=mcamz#+(mmy/30.0)
        movecam=1
      endif
    else
      if (mmx<>0) OR (mmy<>0)
        camrot#=camrot#+(mmx/2)
        camtilt#=camtilt#+(mmy/2)
        movecam=1
      endif
    endif
 
    if camtilt#>90 then camtilt#=90
    if camtilt#<20 then camtilt#=20
    if camrot#>180 then camrot#=180
    if camrot#<-180 then camrot#=-180
    movingcam=1
    hide mouse
  else 
    if movingcam=1
      show mouse
      set cursor cammousex,cammousey
    endif
    movingcam=0
  endif
 
  mz=mousemovez()
  if mz<>0
    camzoom#=camzoom#-(mz/100)
      if camzoom#>50 then camzoom#=50
      if camzoom#<1.5 then camzoom#=1.5
      movecam=1
  endif
 
  if movecam=1
    position camera camx#,0,camz#
    rotate camera 0,0,0
    yrotate camera camrot#
    move camera mcamz#
    yrotate camera camera angle y()+90
    move camera mcamx#
    camx#=camera position x()
    camz#=camera position z()
    yrotate camera camrot#
    xrotate camera camtilt#
 
    move camera -camzoom#
 
    mcamx#=0
    mcamz#=0
  endif
 
  if spacekey() or (lmb=1)
    if (mousex()<120) AND (mousey()<60)
      cursormode=mousex()/60
    else
      select cursormode
        case 0
          if tilechange=0  
            if ((cursorx>-1) AND (cursorx<51)) AND ((cursorz>-1) AND (cursorz<51))
              if changestate=-1
                if landstate(cursorx,cursorz)=1
                  landstate(cursorx,cursorz)=0
                  changestate=0
                else
                  landstate(cursorx,cursorz)=1
                  changestate=1
                endif
              else
                landstate(cursorx,cursorz)=changestate
              endif
              updateland()
            endif
          endif 
          tilechange=1
        endcase
        case 1
          if tilechange=0  
            if ((cursorx>-1) AND (cursorx<51)) AND ((cursorz>-1) AND (cursorz<51))
              if changestate=-1
                if landlights(cursorx,cursorz).exist=1
                  deletelights(cursorx,cursorz)
                  changestate=0
                else
                  if (landlights(cursorx,cursorz).exist=0) AND (landtile(cursorx,cursorz)=19)
                    addlights(cursorx,cursorz)
                  endif
                  changestate=1
                endif
              else
                if changestate=0
                  if landlights(cursorx,cursorz).exist=1
                    deletelights(cursorx,cursorz)         
                  endif         
                else
                  if (landlights(cursorx,cursorz).exist=0) AND (landtile(cursorx,cursorz)=19)
                    addlights(cursorx,cursorz)
                  endif
                endif
              endif
              updateland()
            endif
          endif 
          tilechange=1
        endcase
      endselect
    endif
  else
    tilechange=0
    changestate=-1
  endif
 
  if keystate(15)=1
    if keytab=0
      cursormode=cursormode+1
      if cursormode>1 then cursormode=0
    endif
    keytab=1
  else
    keytab=0
  endif
 
  if  returnkey()
    if ((landoccupied(25,0,1)=0) AND (landoccupied(25,0,2)=0)) AND ((landoccupied(25,0,4)=0) AND (landoccupied(25,0,8)=0))
      addcar()
    endif
  endif
 
  position object cursorobj,cursorx,0.05,cursorz
 
  updatecar=0
  if carupdate#>1.0
    updatecar=1
    carupdate#=0.0
    dim landoccupied(0,0,0)
    dim landoccupied(50,50,8)
    landoccupied(0,0,0)=0
    dim landheated(0,0)
    dim landheated(50,50)
    landheated(0,0)=0
  endif
 
 
 
  for c=1 to array count(cars(0))
 
    if updatecar=1
      if (cars(c).posx<>cars(c).nextx) OR (cars(c).posy<>cars(c).nexty)
        cars(c).prevx=cars(c).posx
        cars(c).prevy=cars(c).posy 
      endif
      cars(c).posx=cars(c).nextx
      cars(c).posy=cars(c).nexty
 
      if tilecolour=1 then position object cars(c).testobj,cars(c).posx,0.01,cars(c).posy
 
      if ((cars(c).posx=cars(c).destx) AND (cars(c).posy=cars(c).desty)) OR (landstate(cars(c).destx,cars(c).desty)=0)
        givedestination(c)
      endif
      dim pathfindnums(0,0)
      dim pathfindnums(50,50)
 
 
      for cx=0 to 50
        for cy=0 to 50
          pathfindnums(cx,cy)=-1
        next cy
      next cx
      pathfindnums(cars(c).destx,cars(c).desty)=1
      pathnum=1  
      largest=0 
      roadmarked=0
      do
        pathnum=pathnum+1
        stop=0
 
        for cx=0 to 50
          for cy=0 to 50
            if ((pathnum-1)-landheat(cx,cy))>0
              if pathfindnums(cx,cy)=(pathnum-1)-landheat(cx,cy)
                roadmarked=roadmarked+1
                if cx>0
                  if ((landstate(cx-1,cy)=1) AND (pathfindnums(cx-1,cy)=-1))
                    pathfindnums(cx-1,cy)=pathnum
                    if c=selectedcar then text object screen x(landobjects(cx-1,cy)),object screen y(landobjects(cx-1,cy)),str$(pathnum)
                    if (cx-1=cars(c).posx) AND (cy=cars(c).posy)
                      stop=1
                    endif
                  endif
                endif
                if cx<50
                  if ((landstate(cx+1,cy)=1) AND (pathfindnums(cx+1,cy)=-1))
                    pathfindnums(cx+1,cy)=pathnum
                    largest=pathnum
                    if c=selectedcar then text object screen x(landobjects(cx+1,cy)),object screen y(landobjects(cx+1,cy)),str$(pathnum)
                    if (cx+1=cars(c).posx) AND (cy=cars(c).posy)
                      stop=1
                    endif
                  endif
                endif
                if cy>0
                  if ((landstate(cx,cy-1)=1) AND (pathfindnums(cx,cy-1)=-1))
                    pathfindnums(cx,cy-1)=pathnum
                    if c=selectedcar then text object screen x(landobjects(cx,cy-1)),object screen y(landobjects(cx,cy-1)),str$(pathnum)
                    if (cx=cars(c).posx) AND (cy-1=cars(c).posy)
                      stop=1
                    endif
                  endif
                endif
                if cy<50
                  if ((landstate(cx,cy+1)=1) AND (pathfindnums(cx,cy+1)=-1))
                    pathfindnums(cx,cy+1)=pathnum
                    if c=selectedcar then text object screen x(landobjects(cx,cy+1)),object screen y(landobjects(cx,cy+1)),str$(pathnum)
                    if (cx=cars(c).posx) AND (cy+1=cars(c).posy)
                      stop=1
                    endif
                  endif
                endif
              endif
            endif
          next cy
        next cx
        if stop=1
          exit
        endif
        if pathnum>1000
          cars(c).destx=cars(c).posx
          cars(c).desty=cars(c).posy
          exit
        endif
      loop
        if (controlkey()) AND (c=selectedcar)
          text 5,25,str$(roadmarked)
          sync
          wait key
        endif  
      if cars(c).posx>0
        if pathfindnums(cars(c).posx-1,cars(c).posy)>-1
          cars(c).nextx=cars(c).posx-1
          cars(c).nexty=cars(c).posy
          cars(c).sideofroad=8
          landoccupied(cars(c).nextx,cars(c).nexty,8)=landoccupied(cars(c).nextx,cars(c).nexty,8)+1
        endif
      endif
      if cars(c).posx<50
        if pathfindnums(cars(c).posx+1,cars(c).posy)>-1
          cars(c).nextx=cars(c).posx+1
          cars(c).nexty=cars(c).posy
          cars(c).sideofroad=2
          landoccupied(cars(c).nextx,cars(c).nexty,2)=landoccupied(cars(c).nextx,cars(c).nexty,2)+1
        endif
      endif
      if cars(c).posy>0
        if pathfindnums(cars(c).posx,cars(c).posy-1)>-1
          cars(c).nextx=cars(c).posx
          cars(c).nexty=cars(c).posy-1
          cars(c).sideofroad=4
          landoccupied(cars(c).nextx,cars(c).nexty,4)=landoccupied(cars(c).nextx,cars(c).nexty,4)+1
        endif
      endif
      if cars(c).posy<50
        if pathfindnums(cars(c).posx,cars(c).posy+1)>-1
          cars(c).nextx=cars(c).posx
          cars(c).nexty=cars(c).posy+1
          cars(c).sideofroad=1
          landoccupied(cars(c).nextx,cars(c).nexty,1)=landoccupied(cars(c).nextx,cars(c).nexty,1)+1
        endif
      endif   
      cars(c).propx=cars(c).nextx
      cars(c).propy=cars(c).nexty
    cars(c).changed=0  
    endif
 
  movecar(cars(c).obj,carupdate#,cars(c).prevx,cars(c).prevy,cars(c).posx,cars(c).posy,cars(c).nextx,cars(c).nexty)
 
  next c
 
 
 
  if updatecar=1
    updatelights()
 
    stoploop=0
    changestext=0
    while stoploop=0
      changes=0
      for c=1 to array count(cars(0))
        if cars(c).changed=0
 
          waitcar=0
          if (cars(c).posx=cars(c).nextx) AND (cars(c).posy=cars(c).nexty)
            waitcar=1
          endif
          if landoccupied(cars(c).nextx,cars(c).nexty,cars(c).sideofroad)-1>0
            waitcar=1
          endif
          `if landoccupied(cars(c).nextx,cars(c).nexty,rotatevalc(cars(c).sideofroad))>0
            `waitcar=1
          `endif
          `if landoccupied(cars(c).nextx,cars(c).nexty,rotatevalcc(cars(c).sideofroad))>0
            `waitcar=1
          `endif
          if (landlights(cars(c).posx,cars(c).posy).exist=1)
            if (cars(c).sideofroad=2) OR (cars(c).sideofroad=8)
              if landlights(cars(c).posx,cars(c).posy).direction=1
                waitcar=1 
                landlights(cars(c).posx,cars(c).posy).waiting=landlights(cars(c).posx,cars(c).posy).waiting+1            
              endif
            else
              if landlights(cars(c).posx,cars(c).posy).direction=0
                waitcar=1 
                landlights(cars(c).posx,cars(c).posy).waiting=landlights(cars(c).posx,cars(c).posy).waiting+1 
              endif
            endif
          else
            select cars(c).sideofroad
              case 1
                if cars(c).nextx<50
                  if landoccupied(cars(c).nextx+1,cars(c).nexty,rotatevalc(cars(c).sideofroad))>0
                    waitcar=1
                  endif
                endif
              endcase
              case 2
                if cars(c).nexty>0
                  if landoccupied(cars(c).nextx,cars(c).nexty-1,rotatevalc(cars(c).sideofroad))>0
                    waitcar=1
                  endif
                endif
              endcase
              case 4
                if cars(c).nextx>0
                  if landoccupied(cars(c).nextx-1,cars(c).nexty,rotatevalc(cars(c).sideofroad))>0
                    waitcar=1
                  endif
                endif
              endcase
              case 8
                if cars(c).nexty<50
                  if landoccupied(cars(c).nextx,cars(c).nexty+1,rotatevalc(cars(c).sideofroad))>0
                    waitcar=1
                  endif
                endif
              endcase
            endselect
          endif
 
          if waitcar=1
            landheat(cars(c).posx,cars(c).posy)=landheat(cars(c).posx,cars(c).posy)+1
            landheated(cars(c).posx,cars(c).posy)=1
            cars(c).changed=1
            changestext=changestext+1
            landoccupied(cars(c).nextx,cars(c).nexty,cars(c).sideofroad)=landoccupied(cars(c).nextx,cars(c).nexty,cars(c).sideofroad)-1
            cars(c).nextx=cars(c).posx
            cars(c).nexty=cars(c).posy
            calculateprevsor(c)
            landoccupied(cars(c).nextx,cars(c).nexty,cars(c).sideofroad)=landoccupied(cars(c).nextx,cars(c).nexty,cars(c).sideofroad)+1
            changes=changes+1
          endif
        endif
      next c
      if changes=0 then stoploop=1
    endwhile
      for cx=0 to 50
        for cy=0 to 50
          if landheated(cx,cy)=0 then landheat(cx,cy)=0
        next cy
      next cx
 
  endif
 
remstart 
for cx=0 to 50
  for cy=0 to 50
    if landheat(cx,cy)>0
      if object in screen(landobjects(cx,cy))
        value#=landheat(cx,cy)/10.0
        if value#>1.0 then value#=1.0   
        ink rgb(255,255-(value#*255.0),255-(value#*255.0)),0
        text object screen x(landobjects(cx,cy)),object screen y(landobjects(cx,cy)), str$(landheat(cx,cy))
 
      endif
    endif
  next cy
next cx
 
 
for c=1 to array count(cars(0))
  if (cars(c).posx=cars(c).propx) AND (cars(c).posy=cars(c).propy)
    text object screen x(cars(c).obj),object screen y(cars(c).obj),"!!!"
  endif
next c
 
if shiftkey()=1
  if keyshift=0
    selectedcar=pick object(mousex(),mousey(),cars(1).obj,cars(array count(cars(0))).obj)
    if selectedcar<>0 then selectedcar=(selectedcar-cars(1).obj)+1
  endif
  keyshift=1
else
  keyshift=0
endif remend
 
if selectedcar<>0
  text object screen x(cars(selectedcar).obj),object screen y(cars(selectedcar).obj),"<--"
  show object destobj
  show object nextobj
  show object currobj
  position object destobj,cars(selectedcar).destx,0.001,cars(selectedcar).desty
  position object nextobj,cars(selectedcar).propx,0.001,cars(selectedcar).propy
  position object currobj,cars(selectedcar).posx,0.001,cars(selectedcar).posy
else
hide object destobj
hide object nextobj
hide object currobj
endif
 
 
ink 0,0   
 
box (cursormode*55)+3,3,((cursormode+1)*55)+1,56
 
ink rgb(255,255,255),0   
paste image 14,5,5
paste image 25,60,5
sync
loop
 
function deletelights(x,y)
  delete object landbuildobjects(x,y,1)
  delete object landbuildobjects(x,y,2)
  delete object landbuildobjects(x,y,3)
  delete object landbuildobjects(x,y,4)
  landlights(x,y).exist=0
endfunction
 
function updatelights
  for cx=0 to 50
    for cy=0 to 50
      if landlights(cx,cy).exist=1
        landlights(cx,cy).timecount=landlights(cx,cy).timecount+1
 
        if landlights(cx,cy).changing=1
          landlights(cx,cy).changing=0
          landlights(cx,cy).timecount=0
          landlights(cx,cy).waiting=0
          if landlights(cx,cy).direction=0
            landlights(cx,cy).direction=1
          else
            landlights(cx,cy).direction=0
          endif          
          for c=1 to 4
            texture limb landbuildobjects(cx,cy,c),1,26+landlights(cx,cy).direction
            texture limb landbuildobjects(cx,cy,c),2,27-landlights(cx,cy).direction
            texture limb landbuildobjects(cx,cy,c),3,26+landlights(cx,cy).direction
            texture limb landbuildobjects(cx,cy,c),4,27-landlights(cx,cy).direction
          next c
        endif
 
        if (landlights(cx,cy).timecount>3) AND (landlights(cx,cy).timecount+landlights(cx,cy).waiting>10)
          landlights(cx,cy).changing=1
          for c=1 to 4
            texture limb landbuildobjects(cx,cy,c),1,28
            texture limb landbuildobjects(cx,cy,c),2,28
            texture limb landbuildobjects(cx,cy,c),3,28
            texture limb landbuildobjects(cx,cy,c),4,28
          next c
        endif
      endif
    next cx
  next cy
endfunction
 
function addlights(x,y)
  landbuildobjects(x,y,1)=findobj()
  clone object landbuildobjects(x,y,1),trafficlightobj
  position object landbuildobjects(x,y,1),x-0.5,0.2,y-0.5
  landbuildobjects(x,y,2)=findobj()
  clone object landbuildobjects(x,y,2),trafficlightobj
  position object landbuildobjects(x,y,2),x-0.5,0.2,y+0.5
  landbuildobjects(x,y,3)=findobj()
  clone object landbuildobjects(x,y,3),trafficlightobj
  position object landbuildobjects(x,y,3),x+0.5,0.2,y+0.5
  landbuildobjects(x,y,4)=findobj()
  clone object landbuildobjects(x,y,4),trafficlightobj
  position object landbuildobjects(x,y,4),x+0.5,0.2,y-0.5
 
  landlights(x,y).exist=1
  for c=1 to 4
    texture limb landbuildobjects(x,y,c),1,26
    texture limb landbuildobjects(x,y,c),2,27
    texture limb landbuildobjects(x,y,c),3,26
    texture limb landbuildobjects(x,y,c),4,27
  next c
endfunction
 
function calculateprevsor(carnum)
  xdif=cars(carnum).prevx-cars(carnum).posx
  ydif=cars(carnum).prevy-cars(carnum).posy
  if (xdif=0) AND (ydif=-1)
    sor=1
  endif
  if (xdif=-1) AND (ydif=0)
    sor=2
  endif
  if (xdif=0) AND (ydif=1)
    sor=4
  endif
  if (xdif=1) AND (ydif=0)
    sor=8
  endif
  cars(carnum).sideofroad=sor
endfunction
 
function movecar(obj,time#,prevx,prevy,posx,posy,nextx,nexty)
  if (offsettovalue(prevx-posx,prevy-posy))<>(offsettovalue(nextx-posx,nexty-posy))
    direction=offsettovalue(prevx-posx,prevy-posy)+offsettovalue(nextx-posx,nexty-posy)
    if (posx=nextx) AND (posy=nexty) 
      direction=offsettovalue(prevx-posx,prevy-posy)+oppval(offsettovalue(prevx-posx,prevy-posy))
      time#=0
    endif
    if (direction=5) or (direction=10) 
      x#=(time#*toward#(nextx,0.5,posx))+((1.0-time#)*toward#(prevx,0.5,posx))
      y#=(time#*toward#(nexty,0.5,posy))+((1.0-time#)*toward#(prevy,0.5,posy))   
    endif
    select direction
      case 5
        angle#=90
        if offsettovalue(prevx-posx,prevy-posy)=4
          y#=y#-0.2
        else
          y#=y#+0.2
        endif
      endcase
      case 10
        angle#=0
        if offsettovalue(prevx-posx,prevy-posy)=8
          x#=x#-0.2
        else
          x#=x#+0.2
        endif
      endcase
      case 12
        if prevy-posy=-1
          x#=(posx+0.5)+rotatex#(-0.7,0,-time#*90.0)
          y#=(posy-0.5)+rotatey#(-0.7,0,-time#*90.0)
          angle#=(90.0*time#)
        else
          x#=(posx+0.5)+rotatex#(0,0.3,time#*90.0)
          y#=(posy-0.5)+rotatey#(0,0.3,time#*90.0)
          angle#=270.0-(90.0*time#)
        endif
      endcase
      case 9
        if prevx-posx=-1
          x#=(posx-0.5)+rotatex#(0,0.7,-time#*90.0)
          y#=(posy-0.5)+rotatey#(0,0.7,-time#*90.0)
          angle#=(90.0*time#)+90.0
        else
          x#=(posx-0.5)+rotatex#(0.3,0,time#*90.0)
          y#=(posy-0.5)+rotatey#(0.3,0,time#*90.0)
          angle#=180.0-(90.0*time#)
        endif
      endcase
      case 3
        if prevy-posy=1
          x#=(posx-0.5)+rotatex#(0.7,0,-time#*90.0)
          y#=(posy+0.5)+rotatey#(0.7,0,-time#*90.0)
          angle#=(90.0*time#)+180.0
        else
          x#=(posx-0.5)+rotatex#(0,-0.3,time#*90.0)
          y#=(posy+0.5)+rotatey#(0,-0.3,time#*90.0)
          angle#=90.0-(90.0*time#)
        endif
      endcase
      case 6
        if prevx-posx=1
          x#=(posx+0.5)+rotatex#(0,-0.7,-time#*90.0)
          y#=(posy+0.5)+rotatey#(0,-0.7,-time#*90.0)
          angle#=(90.0*time#)+270.0
        else
          x#=(posx+0.5)+rotatex#(-0.3,0,time#*90.0)
          y#=(posy+0.5)+rotatey#(-0.3,0,time#*90.0)
          angle#=-(90.0*time#)
        endif
      endcase
    endselect
  else
    select offsettovalue(prevx-posx,prevy-posy)
      case 1
        x#=(posx-0.5)+rotatex#(0,0.2,-time#*180.0)
        y#=(posy)+rotatey#(0,0.2,-time#*180.0)
        angle#=(180.0*time#)+90
      endcase
      case 2
        x#=(posx)+rotatex#(0.2,0,-time#*180.0)
        y#=(posy+0.5)+rotatey#(0.2,0,-time#*180.0)
        angle#=(180.0*time#)
      endcase
      case 4
        x#=(posx+0.5)+rotatex#(0,-0.2,-time#*180.0)
        y#=(posy)+rotatey#(0,-0.2,-time#*180.0)
        angle#=(180.0*time#)+90
      endcase
      case 8
        x#=(posx)+rotatex#(-0.2,0,-time#*180.0)
        y#=(posy-0.5)+rotatey#(-0.2,0,-time#*180.0)
        angle#=(180.0*time#)
      endcase
    endselect
  endif
  yrotate object obj,angle#
  position object obj,x#,0.1,y#
endfunction
 
function rotatex#(x#,y#,value#)
       result#=((x#*cos(value#))-(y#*sin(value#)));;
endfunction result#
 
function rotatey#(x#,y#,value#)
       result#=((x#*sin(value#))+(y#*cos(value#)));
endfunction result#
 
function toward#(value#,amount#,target#)
  ret#=value#
  if value#<target#
    ret#=value#+amount#
  endif
  if value#>target#
    ret#=value#-amount#
  endif
endfunction ret#
 
function offsettovalue(xoffset,yoffset)
ret=0
if xoffset<0 then ret=ret+1
if xoffset>0 then ret=ret+4
if yoffset<0 then ret=ret+8
if yoffset>0 then ret=ret+2
endfunction ret
 
function oppval(value)
  select value
    case 1
      res=4
    endcase
    case 2
      res=8
    endcase
    case 4
      res=1
    endcase
    case 8
      res=2
    endcase
  endselect
endfunction res
 
function rotatevalc(value)
  select value
    case 1
      res=2
    endcase
    case 2
      res=4
    endcase
    case 4
      res=8
    endcase
    case 8
      res=1
    endcase
  endselect
endfunction res
 
function rotatevalcc(value)
  select value
    case 1
      res=8
    endcase
    case 2
      res=1
    endcase
    case 4
      res=2
    endcase
    case 8
      res=4
    endcase
  endselect
endfunction res
 
function contains(value,direction)
  ret=0
  v=value
  if int(v/8)=1
    v=v-8
    if direction=8 then ret=1
  endif
  if int(v/4)=1
    v=v-4
    if direction=4 then ret=1
  endif
  if int(v/2)=1
    v=v-2
    if direction=2 then ret=1
  endif
  if v=1
    if direction=1 then ret=1
  endif
endfunction ret
 
function addcar()
  array insert at bottom cars(0)
  curr=array count(cars(0))
  cars(curr).obj=findobj()
  make object box cars(curr).obj,0.25,0.15,0.4
  position object cars(curr).obj,25,0.1,0
  cars(curr).prevx=25
  cars(curr).prevy=-2
  cars(curr).posx=25
  cars(curr).posy=-1
  cars(curr).nextx=25
  cars(curr).nexty=0
  cars(curr).destx=25
  cars(curr).desty=0
  cars(curr).colour=rgb(rnd(255),rnd(255),rnd(255))
  color object cars(curr).obj,cars(curr).colour
  if tilecolour=1  
    cars(curr).testobj=findobj()
    make object plain cars(curr).testobj,1,1
    xrotate object cars(curr).testobj,-90
    color object cars(curr).testobj,cars(curr).colour
  endif
  landoccupied(25,0,1)=landoccupied(25,0,1)+1
endfunction
 
function givedestination(c)
dim possiblesx(0)
dim possiblesy(0)
 
for countx=0 to 50
  for county=0 to 50
    if (landstate(countx,county)=1) AND ((cars(c).posx<>countx) OR (cars(c).posy<>county))
      array insert at bottom possiblesx(0)
      array insert at bottom possiblesy(0)
      possiblesx(array count(possiblesx(0)))=countx
      possiblesy(array count(possiblesy(0)))=county
    endif
  next county
next countx
num=rnd(array count(possiblesy(0))-1)+1
cars(c).destx=possiblesx(num)
cars(c).desty=possiblesy(num)
endfunction
 
 
 
 
function sqr(value)
ret=value*value
endfunction ret
 
 
function updateland() 
landstate(25,0)=1
landstate(25,1)=1
landstate(25,2)=1
landstate(25,3)=1
landstate(25,4)=1
roadcount=0
for countx=0 to 50
  for countz=0 to 50
    tile=1
    if landstate(countx,countz)=1
      roadcount=roadcount+1
      tile=4
      if countx>0
        if landstate(countx-1,countz)=1
          tile=tile+1
        endif
      endif
      if countz<50
        if landstate(countx,countz+1)=1
          tile=tile+2
        endif
      endif
      if countx<50
        if landstate(countx+1,countz)=1
          tile=tile+4
        endif
      endif
      if countz>0
        if landstate(countx,countz-1)=1
          tile=tile+8
        endif
      endif
    endif
    if tile<>landtile(countx,countz)
      texture object landobjects(countx,countz),tile
      landtile(countx,countz)=tile
    endif
    if (tile<>19) AND (landlights(countx,countz).exist=1) then deletelights(countx,countz)
  next countz
next countx
endfunction
 
function findobj()
res=1
while object exist(res)=1
res=res+1
endwhile
endfunction res
 
function FillCircle( CX as integer, CY as integer, R as integer ) `Not mine!!! From somewhere on the DBPro forums
   local x as integer
   local y as integer
   local i as integer
   local s as integer
 
   s=R*0.70710678 : ` this number is sin(45) - precalculate!
   box CX-s, CY-s, CX+s+1, CY+s+1
   s=s+1 : ` Faster than 'inc s'?
   i=R*R
   for y=s to R
      x=sqrt( i-(y*y) )
 
      ` Draw top and bottom
      box CX-x, CY-y, CX+x+1, CY-y+1
      box CX-x, CY+y, CX+x+1, CY+y+1
 
      ` Draw left and right
      box CX-y, CY-x, CX-y+1, CY+x+1
      box CX+y, CY-x, CX+y+1, CY+x+1
   next y
endfunction