View Single Post
  #43  
Old 11-19-2002, 02:27 AM
Windcatcher
Demi-God
 
Join Date: Jan 2002
Posts: 1,175
Default

Quester, is this what you have in mind?

Code:
Unit Pathing;

Interface

Const
  MAX_POINTS = 256;             // Arbitrary
  MAX_GROUPS = 256;             // Arbitrary

Type
  TPPointType = (pptLand,pptWater,pptAir,pptWideOpen);
  PPPointGroup = ^TPPointGroup; // C would just use a pointer but Pascal uses these weird constructs :)
  TXYZPoint = Record
    X,Y,Z: Single;              // XYZ coordinates (equivalent to "float")
  End;
  TPPoint = Record
    XYZ        : TXYZPoint;
    _Type      : TPPointType;        // Type of PPoint
    Group      : PPPointGroup;       // Pointer to this group
    OtherGroup : PPPointGroup;       // Connector to another group, or Nil if this point doesn't connect anywhere
  End;
  TPPointGroup = Array[0..MAX_POINTS - 1] Of TPPoint;      // In Delphi I would use open arrays, which are
  TZoneGroups  = Array[0..MAX_GROUPS - 1] Of TPPointGroup; // dynamically allocatable...

Var
  ZoneGroups : TZoneGroups;

Function FindNextPoint(Source,Dest: TXYZPoint; CanTraverse: Set Of TPPointType): TXYZPoint;

Implementation

Function FindNextPoint(Source,Dest: TXYZPoint; CanTraverse: Set Of TPPointType): TXYZPoint;
// ---------------------------------------------------------------------------------
// Given a source point and a desired destination point, return the next point to which
// something can move.
// ---------------------------------------------------------------------------------
Var
  SourcePt : TPPoint;
  DestPt   : TPPoint;

  // Returning a pointer to the TPPoint is less intuitive, but more efficient when
  // implementing this in the server
  Procedure FindNearestPPoint(Const XYZ: TXYZPoint): TPPoint;
  Begin
    .
    .
    .
  End; // FindNearestPoint

Begin
  .
  . // Implement the pathing algorithm here, something I'm not familiar with...
  .
  // Find the nearest TPPoint to the source and destination coordinates

  SourcePt := FindNearestPoint(Source);
  DestPt   := FindNearestPoint(Dest);

  // Attempt to traverse the distance

  If (SourcePt._Type In CanTraverse) And (DestPt._Type In CanTraverse) Then
  Begin
    If (SourcePt._Type = pptWideOpen) And (DestPt._Type = pptWideOpen) And (SourcePt.Group = DestPt.Group) Then
    Begin
      // Both are in the same wide open area; simply move in a straight line
    End
    Else
    Begin
      // Find the nearest available destination, according to the A* pathing algorithm
    End;
  End
  Else
  Begin
    // Can't traverse; do something else here
  End;
End; // FindNextPoint

End.
Reply With Quote