`Bill Robinson
`Coding Challenge - AI
`2-16-0`
sync on:sync rate 60
set display mode 1024,768,32
hide mouse
set ambient light 20
 
dim ball#(200,10)
 
global leftwall=0
global rightwall=1000
global backwall=1000
global floor=0
global gravity#=0
global ballcount=0
global newballflag=1
global width
global height
global paddlewidth=200
global paddleheight=100
global px
global py
global pa#
global pscore
global cscore
global level
global numtargets=8
global targetsleft
global leveldone=0
global specialtarget
 
global compwidth=100
global compheight=100
global compx#
global compy#
global comp_xinc#
 
get image 1,1,1,100,100,1
 
make object plain 101,1000,1000     :`leftwall
yrotate object 101,-90
position object 101,0,500,500
color object 101,rgb(255,0,0)
set object emissive 101,rgb(100,0,0)
 
make object plain 102,1000,1000     :`backwall
position object 102,500,500,1000
color object 102,rgb(0,255,0)
set object emissive 102,rgb(0,70,0)
 
make object plain 103,1000,1000     :`rightwall
yrotate object 103,-90
position object 103,1000,500,500
color object 103,rgb(0,0,255)
set object emissive 103,rgb(0,0,150)
 
make object plain 104,1000,1000     :`floor
xrotate object 104,-90
position object 104,500,0,500
color object 104,rgb(100,100,0)
 
make object plain 105,paddlewidth,paddleheight     :`My Paddle
color object 105,rgb(0,100,100)
fade object 105,70
ghost object on 105
set object emissive 105,rgb(0,150,150)
set object collision on 105
 
make object plain 106,paddlewidth,1                :`My Paddle Shadow
color object 106,rgb(1,1,1)
 
make object plain 110,compwidth,compheight         :`Computer Paddle
color object 110,rgb(200,0,200)
set object emissive 110,rgb(255,0,255)
 
make object plain 111,compwidth,2               :`Computer Shadow
color object 111,rgb(1,1,1)
 
for i=1 to numtargets                           :`Targets
   make object plain 150+i,90,90
   set object collision on 150+i
next i
 
for i=1 to 2
   make object sphere i,30             :`ball#1
   make object sphere 50+i,30          :`ball#1 Shadow
   set object collision on i
   scale object 50+i,100,1,100
   color object 50+i,rgb(1,1,1)
   _init_ball(i)
next i
 
width=screen width()
height=screen height()
 
position camera 0,500,300,-550
xrotate camera 0,0
px1=paddlewidth/2
px2=950                            :`rightwall-padddlewidth
py1=paddleheight
py2=height-paddleheight/2
comp_xinc#=3.0
level=1
 
startnewlevel:
gosub _setuptargets
pa#=0
targetsleft=numtargets
do
   set cursor 1,1:print screen fps()
   score$="Level:"+str$(level)+"   Player:"+str$(pscore)+"   Computer:"+str$(cscore)
   center text 500,10,score$
   text$="Use Z and X keys to AIM PADDLE"
   center text 500,30,text$
   if ballcount=0
      center text 500,300,"Press <space> to fire ball"
   endif
   if keystate(57)=1 then if newballflag>0 then gosub _fire_new_ball:wait 100
   if keystate(44) then if pa#>-35 then dec pa#,2:yrotate object 105,pa#:yrotate object 106,pa#
   if keystate(45) then if pa#<35 then inc pa#,2:yrotate object 105,pa#:yrotate object 106,pa#
   if keystate(42) then pa#=0:yrotate object 105,pa#:yrotate object 106,pa#
   if leveldone=1 then gosub _endlevel:goto startnewlevel
 
   px=mousex():py=height-mousey()
   if px<px1 then px=px1
   if px>px2 then px=px2
   if py<py1 then py=py1
   if py>py2 then py=py2
   position object 105,px,py1,100
   position object 106,px,1,100
 
   position object 110,compx#,py1,800
 
   position object 111,compx#,2,800
   gosub _move_balls
   gosub _comp_paddle
`   gosub _debug
   sync
loop
 
`------------------------
_debug:
set cursor 1,100:print "ballcount=";ballcount
set cursor 1,110:print "hitnum=";hitnum
set cursor 1,120:print "specialtarget=";specialtarget
return
 
`------------------------
_endlevel:
text$="You Finished Level: "+str$(level)
center text 500,200,text$
text$="Press any key for next level"
center text 500,240,text$
sync
for i=1 to ballcount
   ball#(i,1)=0
   _init_ball(i)
next i
leveldone=0
inc level
inc comp_xinc#,0.2
newballflag=1
ballcount=0
wait key
return
 
`------------------------
_setuptargets:
specialtarget=rnd(7)+151
for i=1 to numtargets
   if i+150=specialtarget
      color object 150+i,rgb(255,255,0)
      set object emissive 150+i,rgb(100,100,0)
   else
      color object 150+i,rgb(255,0,0)
      set object emissive 150+i,rgb(100,0,0)
   endif
   position object 150+i,i*120-50,100,backwall-10
   show object 150+i
   set object collision on 150+i
next i
return
 
`------------------------
`computer paddle follows ball
_comp_paddle:
b1x=ball#(1,2):b1y=ball#(1,3):b1z=object position z(1)
b2x=ball#(2,2):b2y=ball#(2,3):b2z=object position z(2)
if b1z>b2z
   if compx#<b1x then inc compx#,comp_xinc#
   if compx#>b1x then dec compx#,comp_xinc#
else
   if compx#<b2x then inc compx#,comp_xinc#
   if compx#>b2x then dec compx#,comp_xinc#
endif
return
 
`------------------------
function _init_ball(ptr)
ball#(ptr,1)=0      :`active? 1=yes
ball#(ptr,2)=900    :`x
ball#(ptr,3)=200    :`y
ball#(ptr,4)=-100   :`z
ball#(ptr,5)=-2     :`xdir
ball#(ptr,6)=0      :`ydir
ball#(ptr,7)=10     :`zdir
position object ptr,ball#(ptr,2),ball#(ptr,3),ball#(ptr,4)
position object 50+ptr,ball#(ptr,2),ball#(ptr,3),ball#(ptr,4)
endfunction
 
`------------------------
_move_balls:
if ballcount>0
for i=1 to 2
   if ball#(i,1)=1
      hitnum=object collision(i,0)
      if hitnum=105                             :`Hit PLAYER paddle
         if ball#(i,7)<0
            if pa#=0
               ball#(i,7)=ball#(i,7)*-1
               ball#(i,4)=ball#(i,4)+ball#(i,7)
            endif
            if pa#>0
               ball#(i,5)=pa#/5
               ball#(i,7)=ball#(i,7)*-1
               ball#(i,4)=ball#(i,4)+ball#(i,7)
            endif
            if pa#<0
               ball#(i,5)=pa#/5
               ball#(i,7)=ball#(i,7)*-1
               ball#(i,4)=ball#(i,4)+ball#(i,7)
            endif
         endif
      endif
 
      if hitnum=110                             :`Hit COMPUTER paddle
         if ball#(i,7)>0
            ball#(i,7)=ball#(i,7)*-1
            ball#(i,4)=ball#(i,4)+ball#(i,7)
         endif
      endif
 
      if hitnum>150 and hitnum<=160             :`Hit TARGET
         hide object hitnum
         set object collision off hitnum
         inc pscore
         dec targetsleft
         if targetsleft=0 then leveldone=1
         if targetsleft>0 and hitnum=specialtarget
            gosub _fire_new_ball
         endif
      endif
 
      bx#=ball#(i,2)+ball#(i,5)
      by#=ball#(i,3)+ball#(i,6)-gravity#
      bz#=ball#(i,4)+ball#(i,7)
 
      if bx#<leftwall+10 or bx#>rightwall-10
         ball#(i,5)=ball#(i,5)*-1
         bx#=bx#+ball#(i,5)
      endif
      if by#<floor
         ball#(i,6)=ball#(i,6)*-1
         by#=by#+ball#(i,6)
      endif
      if bz#>backwall
         ball#(i,7)=ball#(i,7)*-1
         bz#=bz#+ball#(i,7)
      endif
      if bz#<-200
         _init_ball(i)
         ball#(i,1)=0
         hide object i
         dec ballcount
         newballflag=1
         inc cscore
      endif
 
      position object i,bx#,100,bz#
      position object i+50,bx#,2,bz#
      ball#(i,2)=bx#
      ball#(i,3)=by#
      ball#(i,4)=bz#
   endif
next i
endif
return
 
`------------------------
_fire_new_ball:
   ballcount=ballcount+1
   ball#(ballcount,1)=1
   position object ballcount,ball#(ballcount,2),ball#(ballcount,3),ball#(ballcount,4)
   show object ballcount
   newballflag=0
return
end