PathFinding Library with Demo


Tweet artificial-intelligence blitz3d code-archives
(Posted 1 month ago) RonTek

Pathfinding lib v1.0. You may use this libary in your games all i ask is to be given some credit cheers ;o)

Author: Andrew Pye

Image

Download Full Source + Media

;=======================
;Demo By Andrew Pye (3D)
;09/02/2003
;=======================

;================
;set the graphics
;================
Graphics3D 800,600,0,2

;========
;includes
;========
Include "PathFinding.bb"

;=====
;Types
;=====
Type bottype
    Field Model
    Field PathNodes$,Currentnode$,LastNode$
End Type

;=====
;const
;=====
Const NumberOfBots=30

;=======
;globals
;=======
Global SCALE=5;scale the map

;==================
;set the frame rate
;==================
Const UPS=60

;=================
;Create the camera
;=================
Global cam=CreateCamera()
l=CreateLight()

;===========
;load a font
;===========
fntArial=LoadFont("Arial",12,False,False,False) 
SetFont fntArial 

;======================
;handles the frame rate
;======================
period=1000/UPS
time=MilliSecs()-period

;==============
;load the level
;==============
level=LoadMesh("testlevel.b3d")
ScaleEntity level,SCALE,SCALE,SCALE

;=================
;load the path map
;=================
LoadPathMap "testlevel.dat"

;==============
;DRAW THE NODES
;==============
Global Marker=CreateSphere()
HideEntity Marker;to help see where the nodes are
DrawNodes

;===============
;CREATE SOM BOTS
;===============
For i=1 To NumberOfBots
    bots.bottype=CreateBot()
Next
SeedRnd (MilliSecs()) 


;=========
;MAIN LOOP
;=========
Repeat

    Repeat
        elapsed=MilliSecs()-time
    Until elapsed
    ticks=elapsed/period
    tween#=Float(elapsed Mod period)/Float(period)

    For k=1 To ticks
        time=time+period

        ;==========
        ;game logic
        ;==========
        MoveCamera;move the camera

        ;========
        ;move bot
        ;========
        For b.bottype=Each bottype
            BotAI b
        Next

        UpdateWorld;update everthing
    Next

    ;===============
    ;rener the world
    ;===============
    RenderWorld tween
    Flip

If KeyHit(59) Then 
  SaveBuffer(FrontBuffer(),"screenshot_"+ss+".bmp")
    ss=ss+1 ;File counter
EndIf


Forever

;==========
;create Bot
;==========
Function CreateBot.BotType()
    ;CREATE BOT type
    b.bottype=New bottype;CREATE BOT type

    ;=============
    ;add the model
    ;=============
    b\model=CreateCube()
    brush=CreateBrush() 
    BrushColor brush,Rnd(0,255),Rnd(0,255),Rnd(0,255) ; Set brush color
    PaintMesh b\model,brush ; Paint mesh with brush

    ;============================
    ;now give it some where to go
    ;============================
    b\PathNodes$=GetAPath$()
    b\Currentnode$=GetNode(b\PathNodes$);extract the first node from the list
    b\PathNodes$=RemoveNode$(b\PathNodes$);clean the node so we dont use it again
    b\LastNode$=sb;used so it knows where the last place it was

    ;now point it at the target
    SlowPointXYZ b\model,Path_Node#(b\Currentnode$,NODE_X),Path_Node#(b\Currentnode$,NODE_Y),Path_Node#(b\Currentnode$,NODE_Z),1

    Return b;Return the bot
End Function

;======
;Bot AI
;======
Function BotAI(b.bottype)

    If Len(b\Currentnode$)>0 Then;something ON ITS MIND 
        ;create a target node pointer
        entity2=CreatePivot()
        PositionEntity entity2,Path_Node#(b\Currentnode$,NODE_X),Path_Node#(b\Currentnode$,NODE_Y),Path_Node#(b\Currentnode$,NODE_Z)

        If EntityDistance(b\model,entity2)<=1.5 Then;check to see if it has got there
            ;get the next node
            b\LastNode$=b\Currentnode$
            b\Currentnode$=GetNode(b\PathNodes$)
            b\PathNodes$=RemoveNode$(b\PathNodes$)
        End If

        If Len(b\Currentnode$)>0 Then
            ;==============
            ;point and move
            ;==============
            SlowPointENT b\model,entity2,0.2
            MoveEntity b\model,0,0,0.1
        End If

        FreeEntity entity2;freeup the entity
    Else
        If Len(b\PathNodes$)<=0 Then b\PathNodes$=GetAPath$(b\LastNode$)
        b\LastNode$=b\Currentnode$
        b\Currentnode$=GetNode(b\PathNodes$)
        b\PathNodes$=RemoveNode$(b\PathNodes$)
    End If


End Function 

;==========================
;generates a new path to go
;==========================
Function GetAPath$(StartFrom=-1)
.retry
    If startfrom=<0 Then
        NodeA=Rnd(1,No_Nodes);gen it a start node
    Else
        NodeA=StartFrom;give it a start node
    End If
    NodeB=Rnd(1,No_Nodes);give it an end node
    Path$=CalcPath$(NodeA,NodeB);calc the path

    If path$=""Then Goto retry;chech there is a path

    Return Path$;return the path

End Function

;==================
;point at an entity
;==================
Function SlowPointENT(entity1,entity2,speed#) 
    ;RotateEntity entity1,0,0,0,True
    dx#=EntityX(entity2)-EntityX(entity1) 
    dy#=EntityY(entity2)-EntityY(entity1) 
    dz#=EntityZ(entity2)-EntityZ(entity1) 
    AlignToVector entity1,dx,dy,dz,3,speed#
    ;filter the roll
    r#=EntityPitch(entity1)
    y#=EntityYaw(entity1)
    RotateEntity entity1,r#,y#,0,True
End Function

;================
;point at xyz pos
;================
Function SlowPointXYZ(entity1,X#,Y#,Z#,speed#) 
    ;RotateEntity entity1,0,0,0,True
    entity2=CreatePivot()
    PositionEntity entity2,X#,Y#,Z#
    dx#=EntityX(entity2)-EntityX(entity1) 
    dy#=EntityY(entity2)-EntityY(entity1) 
    dz#=EntityZ(entity2)-EntityZ(entity1) 
    AlignToVector entity1,dx,dy,dz,3,speed#
    ;filter the roll
    r#=EntityPitch(entity1)
    y#=EntityYaw(entity1)
    RotateEntity entity1,r#,y#,0,True
    FreeEntity entity2
End Function 

;===========================
;handles the camera movement
;===========================
Function MoveCamera()
    If KeyHit(1) End
    turnspeed#=0.2
    movespeed#=0.3
    pitch#=MouseYSpeed()*turnspeed+EntityPitch(cam)
    yaw#=-MouseXSpeed()*turnspeed+EntityYaw(cam)
    If pitch>80 pitch=80
    If pitch<-80 pitch=-80
    RotateEntity cam,0,yaw,0
    TurnEntity cam,pitch,0,0
    MoveMouse GraphicsWidth()/2,GraphicsHeight()/2
    strafe#=Float(KeyDown(205)-KeyDown(203))*movespeed
    motion#=Float(KeyDown(200)-KeyDown(208))*movespeed
    height#=Float(KeyDown(30)-KeyDown(44))*movespeed
    MoveEntity cam,strafe,height,motion
End Function

;===============
;draws the nodes
;===============
Function DrawNodes()
    For i=1 To No_Nodes
        newid=CopyEntity(Marker)
        ScaleEntity newid,0.5,0.5,0.5
        PositionEntity Newid,Path_Node#(i,NODE_X),Path_Node#(i,NODE_Y),Path_Node#(i,NODE_Z);position our entity
    Next 
End Function

Reply To Topic (minimum 10 characters)

Please log in to reply