remstart
   ==============================================================
   =  Title  : ai challenge
   =  Author : Latch Grapple
   =  Date   : 02/25/2008
   =  Update :
   =  Version:
   ==============================================================
   Comments
   ==============================================================
remend
 
 
rem =============================================================
rem = SET UP DISPLAY
rem =============================================================
   autocam off
   set display mode 800,600,32
   sync on
   sync rate 60
   hide mouse
   randomize timer()
 
rem =============================================================
rem = TEST DBC Version
rem =============================================================
   tversion:
   Print "Please Enter the version of DBC you are using"
   Print "1 = DBC 1.13 or less"
   Print "2 = DBC 1.20"
   Input "version  :  ";a
   if a < 1 or a > 2
      cls
      goto tversion
   endif
   if a=1 then version=113
   if a=2 then version=120
   cls
 
   create bitmap 1,258,258
   wait 0
 
`version=120
`version=113
 
rem =============================================================
rem = MAIN
rem =============================================================
_main:
   gosub _init
   `position camera 0,0,-50
   do
      gosub _move_main
      gosub _ai_follow
 
      sync
   loop
 
   end
 
rem =============================================================
rem = SUBROUTINES - PROCEDURES
rem =============================================================
   _init:
      gosub _ground
      gosub _characters
      gosub _lighting
 
      position object 1,5000,30,5000
      color backdrop RGB(128,128,255)
      set text font "system",1
      set text opaque
      ink rgb(255,0,0),0
 
      set current bitmap 0
   return
`----------------------------------------------------------------
   _ground:
      ink rgb(100,100,100),0
      box 0,0,100,100
      for n=1 to 10
         blur bitmap 1,3
      next n
      get image 3,0,0,112,112
      sync
      cls 0
      make matrix 1,10000,10000,25,25
      prepare matrix texture 1,3,1,1
      randomize matrix 1,2000
      calc_mat_normals(1,25,25,20.0,20.0)
      update matrix 1
 
 
   return
`----------------------------------------------------------------
   _characters:
      rem textures
      ink 255,0
      dot 0,0
      get image 1,0,0,1,2
      sync
 
      cls
      ink rgb(255,0,0),0
      dot 0,0
      get image 2,0,0,1,2
 
      rem main character
      make object sphere 1,25
      make mesh from object 1,1
      make object box 2,5,30,6
      offset limb 2,0,0,-15,0
      make mesh from object 2,2
      delete object 2
      add limb 1,1,2
      add limb 1,2,2
      add limb 1,3,1
      offset limb 1,1,-6,-6,0
      offset limb 1,2,6,-6,0
      texture object 1,1
      hide limb 1,0
 
      rem animate
      set object keyframe 1,0
      rotate limb 1,1,30,0,0
      rotate limb 1,2,330,0,0
      offset limb 1,3,0,-5,0
      set object keyframe 1,10
      rotate limb 1,1,0,0,0
      rotate limb 1,2,0,0,0
      offset limb 1,3,0,0,0
      set object keyframe 1,20
      rotate limb 1,1,330,0,0
      rotate limb 1,2,30,0,0
      offset limb 1,3,0,-5,0
      set object keyframe 1,30
      rotate limb 1,1,359,0,0
      rotate limb 1,2,1,0,0
      offset limb 1,3,0,-.5,0
      set object keyframe 1,38
 
      scale object 1,400,400,400
 
      set object speed 1,25
 
      loop object 1
      `hide object 1
 
 
      rem enemy
      make object sphere 2,25
      add limb 2,1,2
      add limb 2,2,2
      add limb 2,3,1
      delete mesh 1
      delete mesh 2
      offset limb 2,1,-6,-6,0
      offset limb 2,2,6,-6,0
      texture object 2,2
      hide limb 2,0
      `hide object 2
 
      rem animate
      set object keyframe 2,0
      rotate limb 2,1,60,0,0
      rotate limb 2,2,300,0,0
      offset limb 2,3,0,-5,0
      set object keyframe 2,10
      rotate limb 2,1,0,0,0
      rotate limb 2,2,0,0,0
      offset limb 2,3,0,0,0
      set object keyframe 2,20
      rotate limb 2,1,300,0,0
      rotate limb 2,2,60,0,0
      offset limb 2,3,0,-5,0
      set object keyframe 2,30
      rotate limb 2,1,359,0,0
      rotate limb 2,2,1,0,0
      offset limb 2,3,0,-.5,0
      set object keyframe 2,38
 
      scale object 2,400,400,400
 
      set object speed 2,35
 
      loop object 2
 
      rem collision
      set object collision on 1
      set object collision on 2
      set object collision to boxes 1
      set object collision to boxes 2
 
   return
`---------------------------------------------------------------------
   _move_main:
      objx#=object position x(1)
      objy#=object position y(1)
      objz#=object position z(1)
 
      if objx# > 10000 or objx# < 0 then objx#=5000 : objz#=5000
      if objz# > 10000 or objz# < 0 then objz#=5000 : objx#=5000
 
      oangy#=object angle y(1)
      y#=get ground height(1,objx#,objz#)
      position object 1,objx#,y#+140,objz#
      if y# <= 50
         move object 1,5
      else
         if y# >= oldy#
            rem get the side heights to figure out which way to turn
            rem left
            newx=newxvalue(objx#,wrapvalue(oangy#-90),13)
            newz=newzvalue(objz#,wrapvalue(oangy#-90),13)
            lht=get ground height(1,newx,newz)
            rem right
            newx=newxvalue(objx#,wrapvalue(oangy#+90),13)
            newz=newzvalue(objz#,wrapvalue(oangy#+90),13)
            rht=get ground height(1,newx,newz)
            if rht >= lht
               anginc=-2.2
            else
               anginc=2.7
            endif
            yrotate object 1,wrapvalue(object angle y(1)+anginc)
            move object 1,6
         endif
      endif
 
      oldy#=y#
 
      set camera to follow object position x(1),object position y(1),object position z(1),object angle y(1),500,objy#+100,100,0
      `position camera object position x(1),object position y(1)+300,object position z(1)
      `set camera to object orientation 1
      `move camera -300
      `point camera object position x(1),object position y(1),object position z(1)
 
   return
`---------------------------------------------------------------
   _ai_follow:
      rem make object two chase object 1
      objx#=object position x(1)
      `objy#=object position y(1)
      objz#=object position z(1)
 
      enx#=object position x(2)
      enz#=object position z(2)
      ht#=get ground height(1,enx#,enz#)
      position object 2,enx#,ht#+140,enz#
      eny#=object position y(2)
 
      dist#=sqrt((objx#-enx#)^2+(objz#-enz#)^2)
      if dist# < 5000 and dist# > 800 then center text screen width()/2,100,"HEY WAIT UP!!!"
      if dist# < 400 then center text screen width()/2,0,"HEY BLUE!!!!"
      if dist# < 300 then center text screen width()/2,20,"IT'S ME, RED!!"
      if dist# < 200
         center text screen width()/2,40,"WHERE YA GOING??"
         center text screen width()/2,60,"CAN I COME???"
         center text screen width()/2,80,"CAN I?  CAN I?"
      endif
 
 
      rem calculate a smooth angle to turn towards object 1
      enangy#=object angle y(2)
      point object 2,objx#,eny#,objz#
      newangy#=object angle y(2)
      yrotate object 2,enangy#
      attackang#=curveangle(newangy#,enangy#,30)
      yrotate object 2,attackang#
 
      move object 2,12
 
      bang=object hit(1,2)
      if bang=1
         inc count
         if count >= 5
            tim=timer()
            count=0
         endif
      endif
      if timer()-tim < 2000
         set object speed 1,60
         move object 1,15
         yrotate object 1,wrapvalue(object angle Y(1)+rnd(6)-3)
      else
         set object speed 1,25
      endif
 
 
   return
`---------------------------------------------------------------
   _lighting:
      if version=120
         set ambient light 30
         color ambient light 0
         `set directional light 0,1,-.5,1
         make light 1
         set directional light 1,1,-2,3
      else
         set ambient light 40
         color ambient light rgb(180,180,180)
         make light 1
         set directional light 1,1,-.5,1
      endif
   return
rem =============================================================
rem = FUNCTIONS
rem =============================================================
   Function calc_mat_normals(mat,tilex,tilez,sizex#,sizez#)
   Rem By Lee Bamber From DB Example - Adds shaded areas to matrix to give depth
   rem added tile and tile size factor for normal depth adjustment - latch
      for z=1 to tilez
         for x=1 to tilex
 
            rem Get matrix heights
            h8#=get matrix height(mat,x,z-1)
            h4#=get matrix height(mat,x-1,z)
            h#=get matrix height(mat,x-1,z-1)
            h2#=get matrix height(mat,x,z)
 
            rem Calculate projected angle X using heights
            x1#=(x-1)*sizex# : y1#=h#
            x2#=(x+0)*sizex# : y2#=h4#
            dx#=x2#-x1#
            dy#=y2#-y1#
            ax#=atanfull(dx#,dy#)
            ax#=wrapvalue(90-ax#)
 
            rem Calculate projected angle Z using heights
            z1#=(z-1)*sizez# : y1#=h2#
            z2#=(z+0)*sizez# : y2#=h8#
            dz#=z2#-z1#
            dy#=y2#-y1#
            az#=atanfull(dz#,dy#)
            az#=wrapvalue(90-az#)
 
            rem Make normal from projected angle
            nx#=sin(ax#)
            ny#=cos(ax#)
            nz#=sin(az#)
 
            rem Setting matrix normal for smoothness
            set matrix normal mat,x,z,nx#,ny#,nz#
 
         next x
      next z
      update matrix mat
   EndFunction
rem =============================================================
rem = DATA STATEMENTS
rem =============================================================