Const winwidth%=600
Const winheight%=500
Const cellsize%=1
Const vmax%=5
Local tf1#=0.1
Local tf2#=0.2
Local d#=0.1
Global cellcount%=winwidth%/cellsize%,round%
Global y%,odiay1%,t1count%,t2count%,carcount%,odspeed%,odjam%
Dim car%(cellcount%),speed%(cellcount%)
Global win%=CreateWindow("Nagel-Schreckenberg",GraphicsWidth()*0.5-winwidth%*0.5,GraphicsHeight()*0.5-(winheight%+100)*0.5,winwidth%,winheight%+100,Desktop(),1+8+32):RestoreWindow win%
Global can%=CreateCanvas(0,50,winwidth%,winheight%+50,win%)
Local pan%=CreatePanel(0,0,winwidth%,80,win%,1)
Local la1%=CreateLabel("vmax-Trödler: "+tf1#,250,4,90,15,pan%)
Local sl1%=CreateSlider(350,2,150,20,pan%,1)
Local la2%=CreateLabel("a-Trödler: "+tf2#,250,29,80,15,pan%)
Local sl2%=CreateSlider(350,25,150,20,pan%,1)
Local la3%=CreateLabel("Dichte: "+d#,5,18,70,15,pan%)
Local sl3%=CreateSlider(80,2,150,45,pan%,1)
Local bu1%=CreateButton(">",550,0,45,45,pan%)
SeedRnd MilliSecs()
Local timer%=CreateTimer(75):clock%=MilliSecs()
Global img%=CreateImage(winwidth%,winheight%-50)
Global dia%=CreateImage(winwidth%,100)
SetSliderRange sl1%,20,120
SetSliderValue sl1%,10
SetSliderRange sl2%,20,120
SetSliderValue sl2%,20
SetSliderRange sl3%,40,140
SetSliderValue sl3,10
Local run%
SetBuffer CanvasBuffer(can%)
DrawCars()
Repeat
Select WaitEvent()
Case $101
Select EventData()
Case 1
Exit
End Select
Case $401
Select EventSource()
Case sl1%
tf1#=Float(1)/Float(100)*Float(SliderValue(sl1%))
SetGadgetText(la1%,"vmax-Trödler: "+tf1#)
Case sl2%
tf2#=Float(1)/Float(100)*Float(SliderValue(sl2%))
SetGadgetText(la2%,"a-Trödler: "+tf2#)
Case sl3%
d#=Float(1)/Float(100)*Float(SliderValue(sl3%))
SetGadgetText(la3%,"Dichte: "+d#)
Case bu1%
If run%=True Then
run%=False
SetGadgetText(bu1%,">")
Else
run%=True
SetGadgetText(bu1%,"II")
End If
End Select
Case $803
Exit
End Select
If run%<>False Then
UpdateCars(d#,tf2#,tf1#)
DrawCars()
End If
WaitTimer(timer%)
Forever
SetBuffer DesktopBuffer()
End
;FUNCTIONS------------------------------------------------------
Function DrawCars()
Local scale%,x%,jamcars%,jam#,djam%,diay1%,diay2%,diay3%,sumspeed%,dspeed%,diaspeed%=2
round%=round%+1
carcount%=0
For x%=0 To cellcount%
s%=Float(255)/Float(vmax%)*Float(speed%(x%))
If car%(x%)=1 Then
Color 255-s%,0+s%,0
carcount%=carcount%+1
sumspeed%=sumspeed%+speed%(x%)
If speed%(x%)=0 Then jamcars%=jamcars%+1
Else
Color 1,1,1
End If
Rect x%*cellsize%,y%,cellsize%,cellsize%,1
Next
FlipCanvas can%
If y%>(winheight%-50)-cellsize%*2 Then
GrabImage(img%,0,0)
DrawImage(img%,0,-cellsize%)
Else
y%=y%+cellsize%
End If
diay1%=winheight%+50-(Float(100)/Float(carcount%)*Float(jamcars%))
diay2%=winheight%+50-(Float(100)/Float(carcount%)*Float(t1count%))
diay3%=winheight%+50-(Float(100)/Float(carcount%)*Float(t2count%))
GrabImage(dia%,0,winheight%-50)
Color 64,64,64
Rect 0,winheight%-50,winwidth%,100,1
DrawImage(dia%,-diaspeed%,winheight%-50)
scale%=Float(winwidth%)/Float(cellcount%*7.5)*Float(1000)
For x%=1 To Float(cellcount%)/Float(scale%)+1
Color 150,150,150
Line Float(x%)*Float(scale%),1,Float(x%)*Float(scale%),winheight%-51
Color 64,64,64
Rect Float(x%)*Float(scale%)-StringWidth(x%+10)*0.5,1,StringWidth(x%+10),15,1
Color 200,200,200
Text Float(x%)*Float(scale%),1,x%,1
Next
If round% Mod 60=0 Then
Color 64,64,64
Rect 0,y%-15,StringWidth(Str(round%/60)+"min")+10,15,1
Color 150,150,150
Line 0,y%-1,winwidth%,y%-1
Text 5,y%-15,Str(round%/60)+"min"
End If
Color 150,100,64
Rect winwidth%-diaspeed%,diay2%,diaspeed%*0.5,100,1
Color 150,64,64
Rect winwidth%-diaspeed%*0.5,diay3%,diaspeed%*0.5,100,1
Color 255,0,0
If odiay1%=0 Then odiay1%=diay1%
Line winwidth%-diaspeed%,odiay1%,winwidth%-1,diay1%
odiay1%=diay1%
t1count%=0
t2count%=0
dspeed%=Float(sumspeed%)/Float(carcount%)*Float(7.5)/Float(1000)*Float(3600)
If dspeed%>odspeed%+10 Or dspeed%odjam%+100 Or djam%cellcount%*d#+1
r%=Rand(cellcount%)
If car%(r%)=1 Then
car%(r%)=0
speed%(r%)=0
carcount%=carcount%-1
End If
Wend
For c%=0 To cellcount%
If car%(c%)=1 Then
If speed%(c%)=t1# Then
speed%(c%)=speed%(c%)+1
Else
t1count%=t1count%+1
End If
Else
speed%(c%)=vmax%
End If
For b%=c%+1 To c%+speed%(c%)
If b%=>cellcount% Then b2%=b%-cellcount% Else b2%=b%
If car%(b2%)=1 Then speed%(c%)=(b%-c%)-1:Exit
Next
If speed%(c%)>0 And Rnd(1)<=t2# Then speed%(c%)=speed%(c%)-1:t2count%=t2count%+1
c2%=c%+speed%(c%)
If c2%>=cellcount% Then c3%=c2%-cellcount% Else c3%=c2%
If car%(c3%)<1 Then
car%(c3%)=1:car%(c%)=0
speed%(c3%)=speed%(c%):speed%(c%)=0
c%=c3%
End If
End If
Next
End Function