AppTitle "Artificial Biosphere" Const windoww%=800*1 Const windowh%=600*1 Const ccw%=160 Const cch%=120 Const fmaxage%=15 Const smaxage%=50 Global win%=CreateWindow("Artificial Biosphere",GraphicsWidth()*0.5-windoww%*0.5,GraphicsHeight()*0.5-(windowh%+200)*0.5,windoww%,windowh%+200,Desktop(),1+32):RestoreWindow win% Global can%=CreateCanvas(0,0,windoww%,windowh%+200,win%) Global cw%=windoww%/ccw% Global ch%=windowh%/cch% Global diagram%=CreateImage(windoww%,200) Global opc%,ofc%,osc%,osat% Global time% Dim phase1%(ccw%,cch%) Dim phase2%(ccw%,cch%),fmoved%(ccw%,cch%),ftime%(ccw%,cch%) Dim phase3%(ccw%,cch%),smoved%(ccw%,cch%),stime%(ccw%,cch%),saturation%(ccw%,cch%) Local frametimer%=CreateTimer(60),clock%=MilliSecs() SetBuffer CanvasBuffer(can%) ClsColor 64,64,64:Cls SeedRnd MilliSecs() InitWorld() DrawWorld() Local rec%=CreateImage(windoww%,windowh%),frame% While KeyHit(1)=False And WaitEvent()<>$803 MoveWorld() If MilliSecs()>clock%+50 Then Cls DrawWorld() clock%=MilliSecs() ;GrabImage(rec%,0,0) ;frame%=frame%+1 ;SaveImage(rec%,"rec/frame"+frame%+".bmp") ;If frame%=200 Then Exit FlipCanvas can% End If WaitTimer(frametimer%) Wend SetBuffer DesktopBuffer() End ;FUNCTIONS---------------------------------------- Function InitWorld() Local n%,x%,y% For x%=0 To ccw%-1 For y%=0 To cch%-1 phase1%(x%,y%)=Rand(10):opc%=opc%+phase1%(x%,y%) If Rand(20)=1 Then phase2%(x%,y%)=1:ftime%(x%,y%)=Rand(fmaxage%-5,fmaxage%):ofc%=ofc%+1 If Rand(15)=1 Then phase3%(x%,y%)=1:stime%(x%,y%)=Rand(smaxage%-5,smaxage%):saturation%(x%,y%)=smaxage%*0.5:osc%=osc%+1:osat%=osc%*smaxage%*0.5 Next Next End Function Function BreedPlankton(x%,y%) If Rand(10)=1 Then phase1%(x%,y%)=phase1%(x%,y%)+Rnd(-1,3) If phase1%(x%,y%)>10 Then phase1%(x%,y%)=10 If phase1%(x%,y%)<0 Then phase1%(x%,y%)=0 End If End Function Function BreedFish(x%,y%,nx%,ny%) Local bx%,by% Select Rand(4) Case 1 If ny%>0 Then by%=ny%-1 Else by%=ny%+cch%-1 If phase2%(x%,by%)=0 And phase3%(x%,by%)=0 And ftime%(x%,y%)>fmaxage%*0.1 And ftime%(x%,y%)<fmaxage%*0.9 And phase1%(x%,y%)>0 Then phase2%(x%,by%)=1:ftime%(x%,by%)=Rand(fmaxage%-5,fmaxage%) Case 2 If ny%<cch%-1 Then by%=ny%+1 Else by%=0 If phase2%(x%,by%)=0 And phase3%(x%,by%)=0 And ftime%(x%,y%)>fmaxage%*0.1 And ftime%(x%,y%)<fmaxage%*0.9 And phase1%(x%,y%)>0 Then phase2%(x%,by%)=1:ftime%(x%,by%)=Rand(fmaxage%-5,fmaxage%) Case 3 If nx%<ccw%-1 Then bx%=nx%+1 Else bx%=0 If phase2%(bx%,y%)=0 And phase3%(bx%,y%)=0 And ftime%(x%,y%)>fmaxage%*0.1 And ftime%(x%,y%)<fmaxage%*0.9 And phase1%(x%,y%)>0 Then phase2%(bx%,y%)=1:ftime%(bx%,y%)=Rand(fmaxage%-5,fmaxage%) Case 4 If nx%>0 Then bx%=nx%-1 Else bx%=nx%+ccw%-1 If phase2%(bx%,y%)=0 And phase3%(bx%,y%)=0 And ftime%(x%,y%)>fmaxage%*0.1 And ftime%(x%,y%)<fmaxage%*0.9 And phase1%(x%,y%)>0 Then phase2%(bx%,y%)=1:ftime%(bx%,y%)=Rand(fmaxage%-5,fmaxage%) End Select End Function Function BreedShark(x%,y%,nx%,ny%) Local bx%,by% Select Rand(4) Case 1 If ny%>0 Then by%=ny%-1 Else by%=ny%+cch%-1 If phase3%(x%,by%)=0 And phase2%(x%,by%)=0 And stime%(x%,y%)>smaxage%*0.25 And stime%(x%,y%)<smaxage%*0.8 And saturation%(x%,y%)>0 Then phase3%(x%,by%)=1:stime%(x%,by%)=Rand(smaxage%-5,smaxage%):saturation%(x%,by%)=smaxage%*0.5 Case 2 If ny%<cch%-1 Then by%=ny%+1 Else by%=0 If phase3%(x%,by%)=0 And phase2%(x%,by%)=0 And stime%(x%,y%)>smaxage%*0.25 And stime%(x%,y%)<smaxage%*0.8 And saturation%(x%,y%)>0 Then phase3%(x%,by%)=1:stime%(x%,by%)=Rand(smaxage%-5,smaxage%):saturation%(x%,by%)=smaxage%*0.5 Case 3 If nx%<ccw%-1 Then bx%=nx%+1 Else bx%=0 If phase3%(bx%,y%)=0 And phase2%(bx%,y%)=0 And stime%(x%,y%)>smaxage%*0.25 And stime%(x%,y%)<smaxage%*0.8 And saturation%(x%,y%)>0 Then phase3%(bx%,y%)=1:stime%(bx%,y%)=Rand(smaxage%-5,smaxage%):saturation%(bx%,y%)=smaxage%*0.5 Case 4 If nx%>0 Then bx%=nx%-1 Else bx%=nx%+ccw%-1 If phase3%(bx%,y%)=0 And phase2%(bx%,y%)=0 And stime%(x%,y%)>smaxage%*0.25 And stime%(x%,y%)<smaxage%*0.8 And saturation%(x%,y%)>0 Then phase3%(bx%,y%)=1:stime%(bx%,y%)=Rand(smaxage%-5,smaxage%):saturation%(bx%,y%)=smaxage%*0.5 End Select End Function Function EatFish(dir%,x%,y%,nx%,ny%) Select dir% Case 1 If phase2%(x%,ny%)=1 Then If Rand(saturation%(x%,ny%)+2)=1 Then phase2%(x%,ny%)=0:ftime%(x%,ny%)=0 saturation%(x%,ny%)=smaxage% End If End If Case 2 If phase2%(nx%,y%)=1 Then If Rand(saturation%(nx%,y%)+2)=1 Then phase2%(nx%,y%)=0:ftime%(nx%,y%)=0 saturation%(nx%,y%)=smaxage% End If End If End Select End Function Function MoveWorld() Local n%,x%,y%,nx%,ny%,bx%,by% For x%=0 To ccw%-1 For y%=0 To cch%-1 BreedPlankton(x%,y%) If Rand(phase1%(x%,y%)+2)=1 Then ftime%(x%,y%)=ftime%(x%,y%)-1 If ftime%(x%,y%)<0 Then phase2%(x%,y%)=0 End If saturation%(x%,y%)=saturation%(x%,y%)-1 If saturation%(x%,y%)<0 Then saturation(x%,y%)=0 If Rand(saturation%(x%,y%)+2)=1 Then stime%(x%,y%)=stime%(x%,y%)-1 If stime%(x%,y%)=0 Then phase3%(x%,y%)=0 End If If phase2%(x%,y%)=1 And fmoved%(x%,y%)=0 Then Select Rand(5) Case 1;up If y%>0 Then ny%=y%-1 Else ny%=y%+cch%-1 If phase2%(x%,ny%)=0 Then phase2%(x%,y%)=0:phase2%(x%,ny%)=1 ftime%(x%,ny%)=ftime%(x%,y%):ftime%(x%,y%)=0 fmoved%(x%,ny%)=1 Else BreedFish(x%,y%,nx%,ny%) End If Case 2;down If y%<cch%-1 Then ny%=y%+1 Else ny%=0 If phase2%(x%,ny%)=0 Then phase2%(x%,y%)=0:phase2%(x%,ny%)=1 ftime%(x%,ny%)=ftime%(x%,y%):ftime%(x%,y%)=0 fmoved%(x%,ny%)=1 Else BreedFish(x%,y%,nx%,ny%) End If Case 3;right If x%<ccw%-1 Then nx%=x%+1 Else nx%=0 If phase2%(nx%,y%)=0 Then phase2%(x%,y%)=0:phase2%(nx%,y%)=1 ftime%(nx%,y%)=ftime%(x%,y%):ftime%(x%,y%)=0 fmoved%(nx%,y%)=1 Else BreedFish(x%,y%,nx%,ny%) End If Case 4;left If x%>0 Then nx%=x%-1 Else nx%=x%+ccw%-1 If phase2%(nx%,y%)=0 Then phase2%(x%,y%)=0:phase2%(nx%,y%)=1 ftime%(nx%,y%)=ftime%(x%,y%):ftime%(x%,y%)=0 fmoved%(nx%,y%)=1 Else BreedFish(x%,y%,nx%,ny%) End If Default fmoved%(x%,y%)=1 End Select phase1%(x%,y%)=phase1%(x%,y%)-Rand(0,2) If phase1%(x%,y%)<0 Then phase1%(x%,y%)=0 End If If phase3%(x%,y%)=1 And smoved%(x%,y%)=0 Then Select Rand(4) Case 1;up If y%>0 Then ny%=y%-1 Else ny%=y%+cch%-1 If phase3%(x%,ny%)=0 Then phase3%(x%,y%)=0:phase3%(x%,ny%)=1 stime%(x%,ny%)=stime%(x%,y%):stime%(x%,y%)=0 saturation%(x%,ny%)=saturation%(x%,y%):saturation%(x%,y%)=0 smoved%(x%,ny%)=1 EatFish(1,x%,y%,nx%,ny%) Else BreedShark(x%,y%,nx%,ny%) End If Case 2;down If y%<cch%-1 Then ny%=y%+1 Else ny%=0 If phase3%(x%,ny%)=0 Then phase3%(x%,y%)=0:phase3%(x%,ny%)=1 stime%(x%,ny%)=stime%(x%,y%):stime%(x%,y%)=0 saturation%(x%,ny%)=saturation%(x%,y%):saturation%(x%,y%)=0 smoved%(x%,ny%)=1 EatFish(1,x%,y%,nx%,ny%) Else BreedShark(x%,y%,nx%,ny%) End If Case 3;right If x%<ccw%-1 Then nx%=x%+1 Else nx%=0 If phase3%(nx%,y%)=0 Then phase3%(x%,y%)=0:phase3%(nx%,y%)=1 stime%(nx%,y%)=stime%(x%,y%):stime%(x%,y%)=0 saturation%(nx%,y%)=saturation%(x%,y%):saturation%(x%,y%)=0 smoved%(nx%,y%)=1 EatFish(2,x%,y%,nx%,ny%) Else BreedShark(x%,y%,nx%,ny%) End If Case 4;left If x%>0 Then nx%=x%-1 Else nx%=x%+ccw%-1 If phase3%(nx%,y%)=0 Then phase3%(x%,y%)=0:phase3%(nx%,y%)=1 stime%(nx%,y%)=stime%(x%,y%):stime%(x%,y%)=0 saturation%(nx%,y%)=saturation%(x%,y%):saturation%(x%,y%)=0 smoved%(nx%,y%)=1 EatFish(2,x%,y%,nx%,ny%) Else BreedShark(x%,y%,nx%,ny%) End If End Select End If Next Next For x%=0 To ccw%-1 For y%=0 To cch%-1 fmoved%(x%,y%)=0 smoved%(x%,y%)=0 Next Next End Function Function DrawWorld() Local n%,x%,y%,pc%,fc%,sc%,cf%,cs%,css%,sat% For x%=0 To ccw%-1 For y%=0 To cch%-1 cf%=136-(Float(136)/Float(fmaxage%)*Float(ftime%(x%,y%))) cs%=136-(Float(136)/Float(smaxage%)*Float(stime%(x%,y%))) css%=Float(10)/Float(100)*Float(saturation%(x%,y%)) pc%=pc%+phase1%(x%,y%) sat%=sat%+saturation%(x%,y%) Color 64+phase1%(x%,y%)^1.2,64+phase1%(x%,y%)^1.5,64+phase1%(x%,y%)^1.2 If phase2%(x%,y%)=1 Then Color 64,200-cf%,200-cf%:fc%=fc+1 If phase3%(x%,y%)=1 Then Color 200-cs%+css%,200-cs%,64:sc%=sc%+1 Rect x%*cw%,y%*ch%,cw%,ch%,1 Next Next DrawImage diagram%,-2,windowh% Color 100,100,100 Line windoww%-2,Int((windowh%+175)-Float(150)/Float(Float(ccw%)*Float(cch%)*Float(10))*Float(opc%)),windoww%-1,Int((windowh%+175)-Float(150)/Float(Float(ccw%)*Float(cch%)*Float(10))*Float(pc%)) Color 64,200,200 Line windoww%-2,Int((windowh%+175)-Float(150)/Float(Float(ccw%)*Float(cch%))*Float(ofc%)),windoww%-1,Int((windowh%+175)-Float(150)/Float(Float(ccw%)*Float(cch%))*Float(fc%)) Color 200,200,10 Line windoww%-2,Int((windowh%+175)-Float(150)/Float(Float(ccw%)*Float(cch%))*Float(osc%)),windoww%-1,Int((windowh%+175)-Float(150)/Float(Float(ccw%)*Float(cch%))*Float(sc%)) Color 20,200,64 Line windoww%-2,Int((windowh%+175)-Float(150)/Float(Float(osc%)*Float(smaxage%)+1)*Float(osat%)),windoww%-1,Int((windowh%+175)-Float(150)/Float(Float(sc%)*Float(smaxage%)+1)*Float(sat%)) GrabImage diagram%,0,windowh% Color 150,150,150 Rect -1,windowh%+25,windoww%+2,151,0 opc%=pc%:ofc%=fc%:osat%=sat%:osc%=sc% time%=time%+1 End Function