set display mode 1024,768,32
sync on:sync rate 60
autocam off
color backdrop 0
hide light 0
set ambient light 0
`set camera range 1,10000
fog on
fog color rgb(100,100,250)
fog distance 450
 
global flameimage
global blueflameimage
global whiteflameimage
global theta#
global phase2#
global phase3#
global camangle#
 
make_flame_image()
make_flame()
make_candle()
make_table()
 
numberofclouds=400
cloudseed=freeobject()
create_clouds(cloudseed,numberofclouds)
 
do
  camera()
  flicker()
  move_clouds(cloudseed,numberofclouds)
  sync
loop
 
 
 
function create_clouds(cloudseed,numberofclouds)
 
create bitmap 1,50,50
ink rgb(255,255,200),0
for x=1 to 1000
   ang=rnd(360)
   rad=rnd(20)
   box 25+sin(ang)*rad,25+cos(ang)*rad,rnd(3)+25+sin(ang)*rad,rnd(3)+25+cos(ang)*rad
next x
blur bitmap 1,4
cloudimage = freeimage()
get image cloudimage, 0, 0, 50, 50
 
delete bitmap 1
 
 
for cloudnumber=cloudseed to cloudseed+numberofclouds
make object plain cloudnumber,rnd(50)+100,rnd(50)+100
position object cloudnumber,rnd(1000)-500,rnd(100)+10,rnd(1000)-500
xrotate object cloudnumber,90
texture object cloudnumber,cloudimage
ghost object on cloudnumber,0
disable object zwrite cloudnumber
set object light cloudnumber,0
next n
 
endfunction
 
function move_clouds(cloudseed,numberofclouds)
 
for cloudnumber=cloudseed to cloudseed+numberofclouds
move object down cloudnumber,-.5
if object position z(cloudnumber)<-500 then position object cloudnumber,rnd(1000)-500,rnd(100)+10,500
next cloudnumber
 
endfunction
 
 
function freeobject()
   repeat
      inc n
   until object exist(n) = 0
endfunction n
 
function freeimage()
   repeat
      inc n
   until image exist(n) = 0
endfunction n
 
function make_flame_image()
 
position camera 0,0,-2.1
make object sphere 1,1,50,50
ghost object on 1
color object 1,rgb(255,128,0)
make object sphere 2,0.7,50,50
ghost object on 2
disable object zdepth 2
make light 1
color light 1,rgb(255,255,100)
position light 1,0,0,-10
set light range 1,1200
x1=screen width()*0.35
x2=screen width()*0.65
y1=screen height()*0.3
y2=y1+(x2-x1)
flameimage=1
blueflameimage=2
whiteflameimage=3
sync
get image flameimage,x1,y1,x2,y2,1
create bitmap 1,x2-x1,y2-y1
paste image flameimage,0,0,1
blur bitmap 1,3
get image flameimage,0,0,x2-x1,y2-y1,1
delete bitmap 1
alpha(flameimage)
blue(flameimage,blueflameimage)
white(flameimage,whiteflameimage)
delete object 1
delete object 2
 
endfunction
 
function make_flame()
 
dim height(100) as float
 
for flame=1 to 100
  make object plain flame,0.75,1
  texture object flame,flameimage
  if flame>60 and flame<75 then texture object flame,whiteflameimage
  if flame>90 then texture object flame,blueflameimage
  height(flame)=(100-flame)*0.01
  set object transparency flame,2
  set object light flame,0
next flame
 
 
endfunction
 
function make_candle()
 
make object cylinder 1000,2
position object 1000,0,-2.5,0
scale object 1000,50,200,50
color object 1000,rgb(80,80,80)
make object cylinder 1001,0.2
position object 1001,0,-0.3,0
disable object zdepth 1001
scale object 1001,50,200,50
color object 1001,rgb(10,10,10)
ghost object on 1001,3
 
endfunction
 
function make_table()
 
make object plain 1002,10,10
pitch object down 1002,-90
position object 1002,0,-4,0
create bitmap 1,100,100
ink rgb(150,150,150),0
box 0,0,100,100
for n=1 to 1000
  ink rgb(150+rnd(100),150+rnd(100),150+rnd(100)),0
  x=rnd(90)
  y=rnd(90)
  box x,y,x+rnd(10),y+rnd(10)
next n
ink 0,0
box 45,0,55,50
blur bitmap 1,3
get image 1002,0,0,100,100,1
delete bitmap 1
texture object 1002,1002
 
endfunction
 
function flicker()
 
for flame=1 to 100
  phase#=height(flame)*360
  if height(flame)>=1 then height(flame)=0
  radius#=height(flame)*0.05
  radius2#=radius#*1.1
  radius3#=radius#*1.2
  x#=radius#*sin(theta#+phase#+rnd(200))+radius2#*sin(phase2#+90)+radius3#*sin(phase3#+60)
  y#=radius#*sin(theta#+phase#+90)+radius2#*sin(phase2#+90)+radius3#*sin(phase3#+60)
  scale object flame,101-100*height(flame)/1.05,101-100*height(flame)/1.1,100
  position object flame,0,height(flame),0
  alphavalue=50-height(flame)*50
  if alphavalue<0 then alphavalue=0
  set alpha mapping on flame,alphavalue
  increment#=rnd(5)/20.0+0.1
  inc theta#,increment#
  point object flame,camera position x(),0,camera position z()
  move object flame,-flame*0.01
  if flame>60 and flame<75
    move object flame,0.4
    move object down flame,0.2
    scale object flame,85-75*height(flame),90,100
    set alpha mapping on flame,13
  endif
  move object left flame,x#
  move object down flame,y#
next flame
 
inc phase2#,2
inc phase3#,5
 
endfunction
 
function camera()
 
inc camangle#,0.2
position camera 10*sin(camangle#+180),0,10*cos(camangle#+180)
point camera 0,0,0
 
endfunction
 
function alpha(image)
 
memblock=1
 
 
make memblock from image memblock,image
sx=memblock dword(memblock,0)
sy=memblock dword(memblock,4)
depth=memblock dword(memblock,8)
 
 
bpp=depth/8 `bytes per pixel
for x=0 to sx-1
   for y=0 to sy-1
     pos=(x * bpp) + ((y * bpp) * sx) + 12
 
       b=memblock byte(memblock,pos)
       g=memblock byte(memblock,pos+1)
       r=memblock byte(memblock,pos+2)
 
      a=int((b+g+r)/3.0)
 
      if a>255 then a=255
      if a<0 then a=0
 
      write memblock byte memblock,pos+3,a
 
   next y
next x
 
make image from memblock image,memblock
 
delete memblock memblock
 
endfunction
 
function blue(image,imageout)
 
memblock=1
 
 
make memblock from image memblock,image
sx=memblock dword(memblock,0)
sy=memblock dword(memblock,4)
depth=memblock dword(memblock,8)
 
 
bpp=depth/8 `bytes per pixel
for x=0 to sx-1
   for y=0 to sy-1
     pos=(x * bpp) + ((y * bpp) * sx) + 12
 
       b=memblock byte(memblock,pos)
       g=memblock byte(memblock,pos+1)
       r=memblock byte(memblock,pos+2)
 
      r=int((g+r)/2.0)
      g=int((g+r)/2.0)
      b=int((b+g+r)/3.0)+50
      if b>255 then b=255
 
      write memblock byte memblock,pos,b
      write memblock byte memblock,pos+1,g
      write memblock byte memblock,pos+2,r
 
   next y
next x
 
make image from memblock imageout,memblock
 
delete memblock memblock
 
endfunction
 
function white(image,imageout)
 
memblock=1
 
 
make memblock from image memblock,image
sx=memblock dword(memblock,0)
sy=memblock dword(memblock,4)
depth=memblock dword(memblock,8)
 
 
bpp=depth/8 `bytes per pixel
for x=0 to sx-1
   for y=0 to sy-1
     pos=(x * bpp) + ((y * bpp) * sx) + 12
 
       b=memblock byte(memblock,pos)
       g=memblock byte(memblock,pos+1)
       r=memblock byte(memblock,pos+2)
 
      r=r+100
      g=g+100
      b=b+100
      if r>255 then r=255
      if g>255 then g=255
      if b>255 then b=255
 
      write memblock byte memblock,pos,b
      write memblock byte memblock,pos+1,g
      write memblock byte memblock,pos+2,r
 
   next y
next x
 
make image from memblock imageout,memblock
 
delete memblock memblock
 
endfunction