sync on
Enemy=1000
Enemyx#=-20
Enemyz#=-40
Enemya#=0
viewangle#=140
EnemyStatus$="Idle"
EnemyAttackDistance=4
pathstacksize=0
Dim Pathstack(10)
 
Gosub _BuildWalls
Gosub _BuildPaths
x#=0
z#=40
Me=999
make object cone enemy,5
make object sphere me,4
 
do
   set cursor 0,0
   Print "Enemy Status: "+EnemyStatus$
   gosub _EnemyUpdate
   gosub _getControls
   gosub _PostionObjects:
   sync
loop
 
_PostionObjects:
   tempx#=x#:tempz#=z#
      Gosub _SlideCollision
   x#=tempx#:z#=tempz#
   position object me,x#,2,z#
   cam#=wrapvalue(cam#+.1)
   position camera 0,100,-10
   point camera 0,0,0
   tempx#=enemyx#:tempz#=enemyz#
      Gosub _SlideCollision
   enemyx#=tempx#:enemyz#=tempz#
   position object enemy,enemyx#,2,enemyz#
   rotate object enemy,90,enemya#,0
Return
 
_SlideCollision:
   col_dist=2
   for i=1 to walls
      maxtemp#=0
      for j=0 to 360 step 45
         temp#=intersect object (i,tempx#,2,tempz#,newxvalue(tempx#,j,col_dist),2,newzvalue(tempz#,j,col_dist))
         if temp#>maxtemp#
            maxtemp#=temp#
            maxangle#=j
         endif
      next j
      if maxtemp#>0
         tempx#=newxvalue(tempx#,maxangle#,maxtemp#-col_dist)
         tempz#=newzvalue(tempz#,maxangle#,maxtemp#-col_dist)
      endif
   next i
Return
 
 
_EnemyUpdate:
   angletoplayer#=wrapvalue(atanfull(x#-enemyx#,z#-enemyz#))
   if wrapvalue(enemya#-angletoplayer#+viewangle#*.5)<viewangle#
      distancetoplayer#=sqrt((x#-enemyx#)*(x#-enemyx#)+(z#-enemyz#)*(z#-enemyz#))
      cansee=1
      for i=1 to walls
         temp#=intersect object (i,enemyx#,2,enemyz#,x#,2,z#)
         if temp#>0 and temp#<distancetoplayer# then cansee=0
      next i
      if cansee=1
         enemystatus$="CloseIn"
         newx#=x#
         newz#=z#
         print "Attack Attack!"
      else
         print "No body there"
      endif
   else
      print "out of view field"
   endif
 
   If EnemyStatus$="CloseIn"
      enemyangle#=atanfull(newx#-enemyx#,newz#-enemyz#)
      if wrapvalue(enemyangle#-enemya#)>180
         enemya#=wrapvalue(enemya#-1)
      else
         enemya#=wrapvalue(enemya#+1)
      endif
      tempdist#=sqrt((newx#-enemyx#)*(newx#-enemyx#)+(newz#-enemyz#)*(newz#-enemyz#))
      if tempdist#<2 then EnemyStatus$="Scouting3"
      enemyx#=newxvalue(enemyx#,enemyangle#,.1)
      enemyz#=newzvalue(enemyz#,enemyangle#,.1)
   endif
 
   if EnemyStatus$="Idle"
      if rnd(1000)>997 then EnemyAngle#=(rnd(360)-180)*.001
      enemya#=wrapvalue(enemya#+EnemyAngle#)
   endif
 
   if EnemyStatus$="Tracking"
      if rnd(1000)>980 then TrackingAngle#=(rnd(10)-5)*.3
      if rnd(1000)>990 then scounting3=0:EnemyStatus$="Scouting3"
      enemya#=wrapvalue(enemya#+TrackingAngle#)
   endif
 
   if EnemyStatus$="Scouting3"
      if scouting3=0
         gosub _UpdatePathStack
         scouting3=1
      endif
      if scouting3=1
         old2location=oldlocation
         oldlocation=newlocation
         newlocation=int(rnd(pathstacksize-.5))
         if newlocation=oldlocation then newlocation=int(rnd(pathstacksize-.5))
         if newlocation=old2location then newlocation=int(rnd(pathstacksize-.5))
         newx#=pathpos#(pathstack(newlocation),0)
         newz#=pathpos#(pathstack(newlocation),1)
         Enemystatus$="CloseIn"
         scouting3=0
      endif
   endif
 
   _GetControls:
   if upkey()=1 then z#=z#+.1
   if downkey()=1 then z#=z#-.1
   if leftkey()=1 then x#=x#-.1
   if rightkey()=1 then x#=x#+.1
   if spacekey()=1 then tempx#=x#:tempz#=z#:gosub _UpdatePathStack
Return
 
_BuildWalls:
   For i=1 to 5
      read x#,z#,xp#,zp#
      make object box i,x#,10,z#
      position object i,xp#,5,zp#
   next i
   walls=i-1
   data 20,2,0,10
   data 2,20,-10,0
   data 20,2,0,-10
   data 20,2,10,0
   data 2,20,20,-10
Return
 
_BuildPaths:
   numpaths=8
   Dim PathPos#(numpaths,1)
   for i=0 to numpaths
      read PathPos#(i,0),PathPos#(i,1)
   next i
   data 17,-11
   data 15,25
   data 25,25
   data -15,15
   data -5,5
   data 15,5
   data -5,-5
   data 25,-5
   data -12,-20
Return
 
_UpdatePathStack:
   pathstacksize=0
   for j=0 to numpaths
      cansee=1
      for i=1 to walls
         tempscan#=intersect object (i,tempx#,2,tempz#,pathpos#(j,0),2,pathpos#(j,1))
         if tempscan#>0 then cansee=0
      next i
      if cansee=1 then pathstack(pathstacksize)=j:pathstacksize=pathstacksize+1
   next j
Return