Rem *** Include File: NGCollision.dba ***
Rem Created: 10/6/2003 4:48:48 PM
rem Sets up memory data types

#Constant TYPE_NGC_ELLIP=1
#Constant TYPE_NGC_MESH=2

global NGC_debug=0
global NGC_dll


rem *** System Error Handler

   rem Terminates the application if in debug mode and there was an error
   function NGC_endapp()
      if NGC_debug
         exit prompt "Terminating Application","Collision DLL"
         end
      endif
   endfunction

rem *** End System Error Handler


rem *** System Loading/Unloading Functions

   rem Loads the Collision System
   function LoadCollisionDLL()
      NGC_dll = 1
      load dll "DBPro.dll", NGC_dll
      if CheckCollisionDLL( NGC_dll ) = 0
         exit prompt "Collision System could not be loaded","Load Failure"
         end
      endif
   endfunction

   rem Unload the Collision System
   function UnloadCollisionDLL()
      if dll exist(NGC_dll)
         delete dll NGC_dll
         NGC_dll = 0
      endif
   endfunction

   rem Makes sure the collision DLL was loaded properly
   function CheckCollisionDLL( dll )
      if not dll call exist(dll,"StartCollisionPRO") then exitfunction 0
      if not dll call exist(dll,"StartCollisionDebugPRO") then exitfunction 0
      if not dll call exist(dll,"StopCollisionPRO") then exitfunction 0
      if not dll call exist(dll,"SetCollisionPRO") then exitfunction 0
      if not dll call exist(dll,"NewCollObjPRO") then exitfunction 0
      if not dll call exist(dll,"CheckShapePRO") then exitfunction 0
      if not dll call exist(dll,"AddObjShapePRO") then exitfunction 0
      if not dll call exist(dll,"ActivateObjPRO") then exitfunction 0
      if not dll call exist(dll,"DeActivateObjPRO") then exitfunction 0
      if not dll call exist(dll,"SetObjRadiusPRO") then exitfunction 0
      if not dll call exist(dll,"AddMeshTri") then exitfunction 0
      if not dll call exist(dll,"AddMeshPRO") then exitfunction 0
      if not dll call exist(dll,"UpdateObjPRO") then exitfunction 0
      if not dll call exist(dll,"ResetCollisionStackPRO") then exitfunction 0
      if not dll call exist(dll,"PeekCollisionStackPRO") then exitfunction 0
      if not dll call exist(dll,"RunCollisionPRO") then exitfunction 0
      if not dll call exist(dll,"PeekObjXPRO") then exitfunction 0
      if not dll call exist(dll,"PeekObjYPRO") then exitfunction 0
      if not dll call exist(dll,"PeekObjZPRO") then exitfunction 0
      if not dll call exist(dll,"FreeObjPRO") then exitfunction 0
      if not dll call exist(dll,"CountCollisionsPRO") then exitfunction 0
      if not dll call exist(dll,"CollisionHit") then exitfunction 0
      if not dll call exist(dll,"CollisionHitNorm") then exitfunction 0
      if not dll call exist(dll,"CollisionObjHit") then exitfunction 0
      if not dll call exist(dll,"CollisionPolyHit") then exitfunction 0
      if not dll call exist(dll,"CollisionTriCorner") then exitfunction 0
   endfunction 1

rem *** End System Loading/Unloading Functions

rem *** System Init/De-Init Functions

   rem Starts the collision system
   function StartCollisionPRO()
      if NGC_dll = 0 then LoadCollisionDLL()
      ok = call dll(NGC_dll, "StartCollisionPRO")
   endfunction

   rem Puts the collision DLL in debug mode
   function StartCollisionDebugPRO()      `Starts the debug system
      if NGC_dll = 0 then return
      ok = call dll(NGC_dll, "StartCollisionDebugPRO")
      NGC_debug=1
   endfunction

   rem Kills the collision system (unloads the DLL)
   function StopCollisionPRO()
      if dll exist(NGC_dll)
         ok = call dll(NGC_dll, "StopCollisionPRO")
      endif
   endfunction


rem *** End System Init/De-Init Functions

rem *** Object Handling Functions


   rem Sets a collision definition
   function SetCollisionsPro( src_type, dest_type, coll_method, coll_response, dyn_mesh_response )
      ok = call dll(NGC_dll, "SetCollisionPRO", src_type, dest_type, coll_method, coll_response, dyn_mesh_response )
   endfunction

   rem Sets the collision type for this object
   function CollisionTypePRO( object, collID )

      `if object does not exist then exit with an error
      if not object exist(object)
         if NGC_debug
            exit prompt "CollisionTypePRO object does not exist: "+str$(object)+"  Type Setting: "+str$(collID),"Collision DLL"
            end
         endif
      endif

      ok = call dll( NGC_dll, "NewCollObjPRO", object, collID )

      `if collID is positive, then add collision shapes for this object
      if collID > 0

         retval = call dll( NGC_dll, "CheckShapePRO", object, TYPE_NGC_ELLIP )
         if retval
            ok = call dll( NGC_dll, "AddObjShapePRO", object, TYPE_NGC_ELLIP )
         endif

         retval = call dll( NGC_dll, "CheckShapePRO", object, TYPE_NGC_MESH )
         if retval
            `Get object position
            xpos# = object position x(object)
            ypos# = object position y(object)
            zpos# = object position z(object)
            `Places object at 0,0,0 so vert data is local
            position object object, 0, 0, 0
               `Places the mesh into collision memory
               ok = call dll( NGC_dll, "AddObjShapePRO", object, TYPE_NGC_MESH )
               mesh = 99 : make mesh from object mesh,object
               memb = 99 : make memblock from mesh memb, mesh
               MeshPtr = Get Memblock Ptr(memb)
               ok = call dll( NGC_dll, "AddMeshPRO", object, MeshPtr )
               delete memblock memb
               delete mesh mesh
            `Put object back to avoid angry developers
            position object object, xpos#, ypos#, zpos#
         endif

      endif  :  `endif collID is positive


      `Activates the object if it's visible
      if object visible(object)
         ActivateObjPRO( object )
      endif

      `Updates the object
      xpos# = Object Position X(object) : ypos# = Object Position Y(object) : zpos# = Object Position Z(object)
      xang# = Object Angle X(object) : yang# = Object Angle Y(object) : zang# = Object Angle Z(object)
      ok = call dll( NGC_dll, "UpdateObjPRO", object, xpos#, ypos#, zpos#, xang#, yang#, zang# )
      ok = call dll( NGC_dll, "UpdateObjPRO", object, xpos#, ypos#, zpos#, xang#, yang#, zang# )

   endfunction


   `sets the radius of an object
   function SetObjRadiusPRO( object, XSize#, YSize#, ZSize# )
      ok = call dll( NGC_dll, "SetObjRadiusPRO", object, XSize#, YSize#, ZSize# )
   endfunction

   `De-Activates the collision object
   function DeActivateObjPRO( object )
      ok = call dll( NGC_dll, "DeActivateObjPRO", object )
   endfunction

   `Activates the collision object
   function ActivateObjPRO( object )
      ok = call dll( NGC_dll, "ActivateObjPRO", object )
   endfunction


rem *** End Object Handling Functions


rem *** Object FeedBack Functions

function CountCollisionsPRO( object )
   count = call dll( NGC_dll, "CountCollisionsPRO", object)
endfunction count


function CollisionHitX( object, cindex )
   Pos# = call dll( NGC_dll, "CollisionHit", object, cindex, 1)
endfunction Pos#

function CollisionHitY( object, cindex )
   Pos# = call dll( NGC_dll, "CollisionHit", object, cindex, 2)
endfunction Pos#

function CollisionHitZ( object, cindex )
   Pos# = call dll( NGC_dll, "CollisionHit", object, cindex, 3)
endfunction Pos#


function CollisionHitNormX( object, cindex )
   Norm# = call dll( NGC_dll, "CollisionHitNorm", object, cindex, 1)
endfunction Norm#

function CollisionHitNormY( object, cindex )
   Norm# = call dll( NGC_dll, "CollisionHitNorm", object, cindex, 2)
endfunction Norm#

function CollisionHitNormZ( object, cindex )
   Norm# = call dll( NGC_dll, "CollisionHitNorm", object, cindex, 3)
endfunction Norm#


function CollisionObjHit( object, cindex )
   Obj = call dll( NGC_dll, "CollisionObjHit", object, cindex)
endfunction Obj

function CollisionPolyHit( object, cindex )
   Poly = call dll( NGC_dll, "CollisionPolyHit", object, cindex)
endfunction Poly



function CollisionTriCorner( object, cindex, corner, axis$ )
   Pos# = 0
   select axis$
      case "X"
         Pos# = call dll( NGC_dll, "CollisionTriCorner", object, cindex, corner, 1)
      endcase
      case "Y"
         Pos# = call dll( NGC_dll, "CollisionTriCorner", object, cindex, corner, 2)
      endcase
      case "Z"
         Pos# = call dll( NGC_dll, "CollisionTriCorner", object, cindex, corner, 3)
      endcase
   endselect
endfunction Pos#


rem *** End Object FeedBack Functions


rem *** Actual Collision Routines

   rem Runs the collision routines
   function RunCollisionPRO()

      ok = call dll( NGC_dll, "ResetCollisionStackPRO" )
      object = call dll( NGC_dll, "PeekCollisionStackPRO" )
      while object > 0
         if not object exist(object)
            ok = call dll( NGC_dll, "FreeObjPRO", object )
         else
            xpos# = Object Position X(object) : ypos# = Object Position Y(object) : zpos# = Object Position Z(object)
            xang# = Object Angle X(object) : yang# = Object Angle Y(object) : zang# = Object Angle Z(object)
            ok = call dll( NGC_dll, "UpdateObjPRO", object, xpos#, ypos#, zpos#, xang#, yang#, zang# )
         endif
         object = call dll( NGC_dll, "PeekCollisionStackPRO" )
      endwhile

      ok = call dll( NGC_dll, "RunCollisionPRO" )

      ok = call dll( NGC_dll, "ResetCollisionStackPRO" )
      object = call dll( NGC_dll, "PeekCollisionStackPRO" )
      while object > 0
         objx# = call dll(NGC_dll, "PeekObjXPRO", object )
         objy# = call dll(NGC_dll, "PeekObjYPRO", object )
         objz# = call dll(NGC_dll, "PeekObjZPRO", object )
         position object object, objx#, objy#, objz#
         object = call dll( NGC_dll, "PeekCollisionStackPRO" )
      endwhile

   endfunction


