randomize timer()
sync on
set display mode 640,480,32
 
rem sync rate
rate#=100
rem frequence
s#=(1000.0/rate#)
 
global delta#
 
 
rem texture des bombes
for x=0 to 32
   for y=0 to 32
      ink rgb(255,128+rnd(127),0),0 : dot x,y
   next y
next x
get image 1,0,0,32,32
rem texture du sol
for x=0 to 32
   for y=0 to 32
      ink rgb(0,32+rnd(64),0),0 : dot x,y
   next y
next x
get image 2,0,0,32,32
 
 
make matrix 1,10000,10000,50,50 : prepare matrix texture 1,2,1,1
randomize matrix 1,100 : update matrix 1
 
tours=10
 
 
global bullets:global delai:global life
bullets=10
delai=750
life=bullets*delai
 
rem Tours
dim tour(tours)
dim x(tours)
dim y(tours)
dim z(tours)
dim ax#(tours):dim ay#(tours)
dim delai#(tours)
dim current(tours)
dim vies(tours)
dim target(tours)
dim time(tours)
dim distance#(tours)
 
dim bullet(tours,bullets)
dim life#(tours,bullets)
 
rem Ennemis
ennemis=125
dim ennemi(ennemis)
dim ex(ennemis)
dim ey(ennemis)
dim ez(ennemis)
dim vitesse(ennemis)
dim bombe(ennemis)
dim explode(ennemis)
dim mort(ennemis)
dim fini(ennemis)
 
for o=1 to tours
   vies(o)=100
   tour(o)=free_obj()
   make object sphere tour(o),100
   x(o)=rnd(8000)+1000:z(o)=rnd(8000)+1000:y(o)=get ground height(1,x(o),z(o))+50
   position object tour(o),x(o),y(o),z(o)
   ay#(o)=rnd(360)
   current(o)=1
   for b=1 to bullets
      bullet(o,b)=free_obj()
      make object cube bullet(o,b),10 : color object bullet(o,b),rgb(0,0,255) : set object light bullet(o,b),0
   next b
next o
 
for e=1 to ennemis
   ennemi(e)=free_obj()
   make object box ennemi(e),100,80,180
   ex(e)=rnd(8000)+1000:ez(e)=-2000-rnd(8000):ey(e)=350+rnd(450)
   position object ennemi(e),ex(e),ey(e),ez(e)
   vitesse(e)=rnd(500)+800
   bombe(e)=free_obj()
   make object sphere bombe(e),40,6,6
next e
 
 
tour=1
 
start=timer():oldtime=start
 
 fog on : fog distance 3500 : fog color 0 : set camera range 1,4000
 
ink rgb(192,192,192),0
set text font "arial",1
 
rem code pas con du tout trouvÈ sur le forum anglais
make memblock 1, 640*480*4+12
write memblock dword 1, 0, 640
write memblock dword 1, 4, 480
write memblock dword 1, 8, 32
for i=12 to (640*480*4+11) step 4
   write memblock byte 1, i, 0         :`Blue
   write memblock byte 1, i+1, 0       :`Green
   write memblock byte 1, i+2, 0       :`Red
   write memblock byte 1, i+3, 95      :`Alpha
next i
make image from memblock 100, 1
backdrop off
 
do
 
   fps=screen fps()
   mx#=mousemovex()+mx#
   my#=mousemovey()+my#
   mc=mouseclick()
   tabu=keystate(15)
 
   time=timer()
 
   if time=>start+s#
   paste image 100,0,0,1
      set cursor 0,0 : print "fps : ",fps,"/",int(rate#)
 
 
      delta#=(time-oldtime)/1000.0
      oldtime=timer()
 
      dist=(x#-x(tour))^2+(z#-z(tour))^2+(y#-y(tour))^2
      x#=curvevalue(x#,x(tour),1.2):y#=curvevalue(y#,y(tour),1.2):z#=curvevalue(z#,z(tour),1.2)
      ax#=curveangle(ax#,ax#(tour),1.5)
      ay#=curveangle(ay#,ay#(tour),1.5)
 
      amorts=0
      rem si on est a l'interieur
      if dist<10000
         if vies(tour)>0
            ax#(tour)=wrapvalue(ax#(tour)+my#*0.2)
            ay#(tour)=wrapvalue(ay#(tour)+mx#*0.2)
            if mc=1
               shoot(tour)
            endif
            print "health : ",vies(tour)
         else
            for b=1 to 20
               y=rnd(480)
               box 0,y,640,y+1
            next b
         endif
      endif
 
      rem TOURS
      for t=1 to tours
 
         rem IA des alliÈs
         if vies(t)>0
            if t<>tour
 
 
               if time>time(t)+1000+rnd(1000)
                  value=rnd(1):time(t)=time
                  for en=1 to ennemis-1 step 2
                     e=en+value
                     distance#(t)=(x(t)-ex(e))^2+(z(t)-ez(e))^2
                     if distance#(t)<300000
                        target(t)=en : exit
                     else
                        target(t)=0
                     endif
                  next en
 
               endif
               if target(t)>0
                  point object tour(t),ex(target(t)),ey(target(t)),ez(target(t))+vitesse(target(t))
                  ax#(t)=wrapvalue(object angle x(tour(t)))
                  ay#(t)=wrapvalue(object angle y(tour(t)))
                  shoot(t)
               endif
            endif
         else
            inc amorts
         endif
 
         rem Gestion des tirs
         delai#(t)=delai#(t)-delta#*1000
         for b=1 to bullets
             if life#(t,b)>0
                bu=bullet(t,b)
                dec life#(t,b),delta#
                for p=1 to 10
                  move object bu,delta#*100
                  bx=object position x(bu):by=object position y(bu):bz=object position z(bu)
                  for e=1 to ennemis
                     if fini(e)=0
                        if bx=>ex(e)-60 then if bx<=ex(e)+60
                           if by=>ey(e)-45 then if by<=ey(e)+45
                              if bz=>ez(e)-100 then if bz<=ez(e)+100
                                 life#(t,b)=0 : mort(e)=1 :hide object bu : exit
                              endif
                           endif
                        endif
                     endif
                  next en
                  if by<get ground height(1,bx,bz) then life#(t,b)=0 :hide object bu
               next p
            endif
         next b
 
      next t
      rem changement de tourelle
      if tabu=1
         if tab=0
            tab=1 : inc tour
            if tour>tours then tour=1
         endif
      else
         tab=0
      endif
      position camera x#,y#,z# : rotate camera ax#,ay#,0
 
      rem ENNEMIS
      emorts=0
      for e=1 to ennemis
         if fini(e)=0
         move object ennemi(e),delta#*vitesse(e)
         ex(e)=object position x(ennemi(e)):ey(e)=object position y(ennemi(e))-mort(e)*delta#*450:ez(e)=object position z(ennemi(e))
         if mort(e)>0 then roll object right ennemi(e),(-1)^(e)
         if ez(e)=>12000 then ez(e)=0-rnd(2000)
         if ey(e)<get ground height(1,ex(e),ey(e)) then ey(e)=get ground height(1,ex(e),ey(e)) : fini(e)=1 : hide object bombe(e)
         position object ennemi(e),ex(e),ey(e),ez(e)
 
         px=object position x(bombe(e)):pz=object position z(bombe(e))
         py=object position y(bombe(e))
         if py<=get ground height(1,px,pz)
            if explode(e)<=400
               if explode(e)=0
                  ghost object on bombe(e) : texture object bombe(e),1 : set object light bombe(e),0
                  for t=1 to tours
                     bdist=(x(t)-px)^2+(z(t)-pz)^2
                     if bdist<=40000
                        dec vies(t),30+rnd(25)
                        if vies(t)<=0 then color object tour(t),0
                     endif
                  next t
                  scale object bombe(e),400,400,400
               else
                  if explode(e)=>380 then if object visible(bombe(e))=1 then hide object bombe(e)
               endif
               inc explode(e),delta#*1250
            else
               if rnd(100)=0
                  position object bombe(e),ex(e),ey(e),ez(e):scale object bombe(e),100,100,100
                  ghost object off bombe(e):color object bombe(e),rgb(255,255,255) : set object light bombe(e),1
                  explode(e)=0:show object bombe(e)
               endif
            endif
         else
            position object bombe(e),px,py-delta#*750,pz+delta#*vitesse(e)
         endif
         else
            inc emorts
         endif
      next e
 
      print emorts," enemie(s) killed"
      print amorts, " turret(s) destroyed"
 
 
      rem fin de la boucle
      mx#=0 : my#=0
      sync
      start=time
   endif
loop
 
function shoot(tour)
   if delai#(tour)<=0
      delai#(tour)=delai
      b=bullet(tour,current(tour))
      position object b,x(tour),y(tour),z(tour):rotate object b,ax#(tour),ay#(tour),0
      move object down b,5 : move object b,20
      life#(tour,current(tour))=life
      delai#(tour)=delai:show object b
      inc current(tour)
      if current(tour)>bullets then current(tour)=1
   endif
endfunction
 
function free_obj()
   for o=1 to 65535
      if object exist(o)=0 then exit
   next o
endfunction o