`##########################################################################
`### Ai Project - Topdown shooting                                        #
`### by Link102                                                           #
`### version 3.0                                                          #
`### 24 Febuari 2008                                                     ##
`#########################################################################
 
`set display mode 1280,1024,32
hide mouse
randomize timer()
print "loading..."
sync on : sync rate 60
 
dim bullet#(50,3)
dim enemy#(10,3)
dim e_speed#(0)
dim player#(2)
dim score(2)
 
start:
for e = 0 to 5 : init_enemy(e) : next e
e_speed#(0) = 1
 
white = rgb(255,255,255)
player#(0) = screen width()/2
player#(1) = screen height()/2
player#(2) = 10.0
 
ink 0,0
 
 
do
   cls white
   set cursor 0,0
   print "Life: ",int(player#(2)*10),"%"
   print "Score: ",score(0)
 
   xvel# = xvel# + keystate(32) - keystate(30)
   yvel# = yvel# + keystate(31) - keystate(17)
 
   if xvel# >  5 then xvel# =  5
   if xvel# < -5 then xvel# = -5
   if yvel# >  5 then yvel# =  5
   if yvel# < -5 then yvel# = -5
 
   if 1 - keystate(32) and 1 - keystate(30)
      if xvel# > 0 then xvel# = xvel# - 1
      if xvel# < 0 then xvel# = xvel# + 1
   endif
   if 1 - keystate(31) and 1 - keystate(17)
      if yvel# > 0 then yvel# = yvel# - 1
      if yvel# < 0 then yvel# = yvel# + 1
   endif
 
   player#(0) = player#(0) + xvel#
   player#(1) = player#(1) + yvel#
 
   if player#(0) < player#(2)                   then player#(0) = player#(2)
   if player#(0) > screen width()  - player#(2) then player#(0) = screen width() - player#(2)
   if player#(1) < player#(2)                   then player#(1) = player#(2)
   if player#(1) > screen height() - player#(2) then player#(1) = screen height() - player#(2)
 
   circle player#(0),player#(1),player#(2)
   draw_cursor(mousex(),mousey(),1)
   update_bullets()
   update_enemy()
   if player#(2) =< 0 then gosub dead
sync
loop
 
dead:
   show mouse
   do
      cls white
      center text screen width() / 2,screen height() / 2 - 20,"You Died - Game over"
      if score(0) > 0 then center text screen width() / 2,screen height() / 2 + 20,"Score: " + str$(score(0))
      if score(1) > 0 then center text screen width() / 2,screen height() / 2 + 40,"Bullets fired: " + str$(score(1))
      if score(2) > 0 then center text screen width() / 2,screen height() / 2 + 60,"Bullets hit: " + str$(score(2))
      if score(1) > 0 then center text screen width() / 2,screen height() / 2 + 80,"Hit ratio: " + str$((100 / score(1)) * score(2))
      if button("Retry",screen width() / 2 - 100,screen height() / 2,screen width() / 2 - 10,screen height() / 2 + 15) then goto start
      if button("Exit",screen width() / 2 + 10,screen height() / 2,screen width() / 2 + 100,screen height() / 2 + 15) then end
   sync
   loop
   hide mouse
return
 
function draw_cursor(x,y,scale)
   dot x,y
   circle x,y,8*scale
   line x,y+6*scale,x,y+10*scale
   line x,y-6*scale,x,y-10*scale
   line x+6*scale,y,x+10*scale,y
   line x-6*scale,y,x-10*scale,y
endfunction
 
function update_bullets()
   if mouseclick() = 1
      rand = rnd(2)-1
      bullet#(nr,0) = player#(0)
      bullet#(nr,1) = player#(1)
      angle# = atanfull(mousex() - player#(0),mousey() - player#(1))
      bullet#(nr,2) = newxvalue(0,angle# + rand,11)
      bullet#(nr,3) = newzvalue(0,angle# + rand,11)
      nr = nr + 1
      if nr = 51 then nr = 0
      score(1) = score(1) + 1
   endif
   for b = 0 to 50
     `position    = position    + velocity
      bullet#(b,0) = bullet#(b,0) + bullet#(b,2)
      bullet#(b,1) = bullet#(b,1) + bullet#(b,3)
      dot bullet#(b,0),bullet#(b,1)
   next b
endfunction
 
function init_enemy(nr)
   rand = rnd(3)
   if rand = 0
      enemy#(nr,0) = -10
      enemy#(nr,1) = rnd(screen height() - 10) + 5
   endif
   if rand = 1
      enemy#(nr,0) = screen width() + 10
      enemy#(nr,1) = rnd(screen height() - 10) + 5
   endif
   if rand = 2
      enemy#(nr,0) = rnd(screen width()  - 10) + 5
      enemy#(nr,1) = -10
   endif
   if rand = 3
      enemy#(nr,0) = rnd(screen width()  - 10) + 5
      enemy#(nr,1) = screen height() + 10
   endif
   enemy#(nr,2) = 10
endfunction
 
function update_enemy()
   for e = 0 to 5
      angle# = atanfull(player#(0)-enemy#(e,0),player#(1)-enemy#(e,1))
      if dist(player#(0),enemy#(e,0),player#(1),enemy#(e,1)) < player#(2) + enemy#(e,2)
         player#(2) = player#(2) - 0.1
      else
         enemy#(e,0) = newxvalue(enemy#(e,0),angle#,e_speed#(0))
         enemy#(e,1) = newzvalue(enemy#(e,1),angle#,e_speed#(0))
      endif
      for b = 0 to 50
         if dist(bullet#(b,0),enemy#(e,0),bullet#(b,1),enemy#(e,1)) < enemy#(e,2) + 5
            score(0) = score(0) + 1
            score(2) = score(2) + 1
            bullet#(b,0) = 0
            bullet#(b,1) = 0
            bullet#(b,2) = 0
            bullet#(b,3) = 0
            if enemy#(e,2) < 3
               score(0) = score(0) + 100
               e_speed#(0) = e_speed#(0) + 0.1
               init_enemy(e)
            else
               enemy#(e,2) = enemy#(e,2) - 1
            endif
         endif
      next b
      circle enemy#(e,0),enemy#(e,1),enemy#(e,2)
   next e
endfunction
 
function dist(x1#,x2#,y1#,y2#)
   d# = sqrt((x2# - x1#)^2+(y2# - y1#)^2)
endfunction d#
 
function button(tag$,x1,y1,x2,y2)
   line_box(x1,y1,x2,y2)
   if mousex() > x1 and mousex() < x2 and mousey() > y1 and mousey() < y2
      line_box(x1 - 1,y1 - 1,x2 + 1,y2 + 1)
      clicked = mouseclick()
   endif
   c = clicked : clicked = 0
   text x1 + (x2 - x1) / 2 - (text width(tag$) / 2),y1 + (y2 - y1) / 2 - (text height(tag$) / 2),tag$
endfunction c
 
function line_box(x1,y1,x2,y2)
   line x1,y1,x2,y1
   line x1,y1,x1,y2
   line x2,y1,x2,y2
   line x1,y2,x2,y2
endfunction