gonav

package module
v0.0.0-...-354c86f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 5, 2020 License: AGPL-3.0 Imports: 7 Imported by: 0

README

csgonavparse

This is a clone of the gonav repository, with the appropriate changes to fix bugs that hampered use in Go versions >= 1.14. The original repository description is

A Source Engine bot Nav file parser written in Go. This was written for CS:GO but will likely work with little or no modification for other Source titles. The specifics of the .nav format were reverse engineered using the information on Valve's wiki as a starting point. For more information on Source's Navigation Meshes see Valve's wiki: https://developer.valvesoftware.com/wiki/Navigation_Meshes

We cloned this repository and made the changes as it was necessary for the csgo data parsing package for Python.

Parsing .nav files

First, create a parser and pass in any Reader that contains the binary .nav file. This file game can be found in CSGO's game files. Then, call the Parse() method. Doing so will output a parsed NavMesh object which you can perform operations on. See an example below:

f, _ := os.Open("de_dust2.nav") // Open the file
parser := gonav.Parser{Reader: f} // Create the parser
mesh, _ := parser.Parse() // Parse the file
area := mesh.QuadTreeAreas.FindAreaByPoint(gonav.Vector3{10, 10, 10}, true) // Find the nav area that contains the world point {10, 10} with a Z-value closest to 10
fmt.Println(area)

Further examples are available in the examples directory.

Path Finding

This library also supports path finding across nav meshes via an A* implementation.

bombsiteA := mesh.GetPlaceByName("BombsiteA")
aCenter, _ := bombsiteA.GetEstimatedCenter()
aArea := mesh.GetNearestArea(aCenter, false)
bombsiteB := mesh.GetPlaceByName("BombsiteB")
bCenter, _ := bombsiteB.GetEstimatedCenter()
bArea := mesh.GetNearestArea(bCenter, false)
path, _ := gonav.SimpleBuildShortestPath(aArea, bArea)

for _, currNode := range path.Nodes {
	fmt.Println(currNode.Area)
}

Example output of path finding from the center of A-site on de_nuke to the center of B-site:

Path length: 2381.591
AreaID: 1419 [BombsiteA] @ {{675 -925 -414.96875}, {700 -900 -415.35532}}
AreaID: 7 [BombsiteA] @ {{575 -1325 -415.96875}, {950 -925 -415.96875}}
AreaID: 2285 [BombsiteA] @ {{550 -1300 -415.96875}, {575 -1225 -415.96875}}
AreaID: 3107 [BombsiteA] @ {{500 -1400 -415.96875}, {550 -1225 -415.96875}}
AreaID: 3368 [BombsiteA] @ {{475 -1375 -415.96875}, {500 -1225 -415.96875}}
AreaID: 3076 [BombsiteA] @ {{425 -1375 -415.96875}, {475 -1325 -415.96875}}
AreaID: 3075 [BombsiteA] @ {{425 -1400 -415.96875}, {475 -1375 -415.96875}}
AreaID: 1673 [Vents] @ {{425 -1450 -607.96875}, {625 -1425 -607.96875}}
AreaID: 700 [Vents] @ {{625 -1525 -607.96875}, {650 -1425 -607.96875}}
AreaID: 2478 [Vents] @ {{650 -1450 -607.96875}, {700 -1425 -607.96875}}
AreaID: 2479 [Vents] @ {{700 -1450 -607.96875}, {750 -1425 -607.96875}}
AreaID: 2477 [Vents] @ {{750 -1450 -607.96875}, {825 -1425 -607.96875}}
AreaID: 2300 [Vents] @ {{825 -1450 -607.96875}, {850 -1400 -607.96875}}
AreaID: 2301 [Vents] @ {{825 -1400 -607.96875}, {850 -1350 -607.96875}}
AreaID: 1690 [Vents] @ {{825 -1350 -607.96875}, {850 -1325 -639.96875}}
AreaID: 2265 [BombsiteB] @ {{775 -1325 -639.96875}, {900 -1275 -639.96875}}
AreaID: 3589 [BombsiteB] @ {{900 -1325 -639.96875}, {950 -1275 -639.96875}}
AreaID: 3591 [BombsiteB] @ {{900 -1275 -639.96875}, {950 -1050 -639.96875}}
AreaID: 3593 [BombsiteB] @ {{900 -1050 -639.96875}, {950 -900 -639.96875}}
AreaID: 2490 [BombsiteB] @ {{878 -1050 -597.96875}, {888 -900 -597.96875}}
AreaID: 2690 [BombsiteB] @ {{850 -1050 -767.96875}, {888 -900 -768.9288}}
AreaID: 3500 [BombsiteB] @ {{550 -1075 -770.25446}, {850 -900 -767.96875}}
AreaID: 3502 [BombsiteB] @ {{550 -900 -769.49255}, {850 -725 -767.96875}}

License

The text below is the LICENSE text in the original repository as of May 18, 2020.

This source is licensed under the GNU AFFERO GENERAL PUBLIC LICENSE. If this license is not acceptable for your project, let me know. Additionally, more feature-rich versions of this library exist, written in C# and C++, and can be licensed upon request.

Documentation

Overview

Package gonav provides functionality related to CS:GO Nav Meshes

Package gonav provides functionality related to CS:GO Nav Meshes

Package gonav provides functionality related to CS:GO Nav Meshes

Package gonav provides functionality related to CS:GO Nav Meshes

Package gonav provides functionality related to CS:GO Nav Meshes

Package gonav provides functionality related to CS:GO Nav Meshes

Package gonav provides functionality related to CS:GO Nav Meshes

Package gonav provides functionality related to CS:GO Nav Meshes

Package gonav provides functionality related to CS:GO Nav Meshes

Package gonav provides functionality related to CS:GO Nav Meshes

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HeuristicCalculator

type HeuristicCalculator func(*NavArea, *NavArea) float32

HeuristicCalculator calculates an estimated heurisitic of cost between two (likely disjoint) NavAreas This is used by the A* algorithm so must be admissible AND monotonic

type MeshConnectionCalculator

type MeshConnectionCalculator func(*NavConnection) float32

MeshConnectionCalculator is a func that calculates the cost of a connection in a nav mesh

type MeshLadderCalculator

type MeshLadderCalculator func(*NavLadder, NavLadderDirection, *NavArea, *NavArea) float32

MeshLadderCalculator is a func that calculates the cost of a ladder between two nav areas The first NavArea is the starting area, the second NavArea is the ending area

type NavArea struct {
	ID                           uint32                 // ID of the NavArea
	NorthWest                    Vector3                // Location of the north-west point of this NavArea
	SouthEast                    Vector3                // Location of the south-east point of this NavArea
	Flags                        uint32                 // Bitflags set on this area
	NorthEastZ                   float32                // The Z-coord for the north-east point
	SouthWestZ                   float32                // The Z-coord for the south-west point
	NorthWestLightIntensity      float32                // The light intensity of the north-west corner
	NorthEastLightIntensity      float32                // The light intensity of the north-east corner
	SouthWestLightIntensity      float32                // The light intensity of the south-west corner
	SouthEastLightIntensity      float32                // The light intensity of the south-east corner
	Place                        *NavPlace              // The place this area is in
	Connections                  []*NavConnection       // The connections between this area and other areas
	HidingSpots                  []*NavHidingSpot       // The hiding spots in this NavArea
	EncounterPaths               []*NavEncounterPath    // The encounter paths for this area
	LadderConnections            []*NavLadderConnection // Connections between this area and ladders
	VisibleAreas                 []*NavVisibleArea      // Visible areas
	EarliestOccupyTimeFirstTeam  float32                // The earliest time the first team can occupy this area
	EarliestOccupyTimeSecondTeam float32                // The earliest time the second team can occupy this area
	InheritVisibilityFromAreaID  uint32                 // ID of the area to inherit our visibility from
}

NavArea represents a NavArea as part of a NavMesh

func (area *NavArea) ContainsPoint(point Vector3, allowBelow bool) bool

ContainsPoint determines whether or not the specified point is contained within this area If allowBelow is false, a rough estimation will be performed and if the specified point is below this NavArea, we will return false.

func (area *NavArea) DistanceFromCenter(point Vector3) float32

DistanceFromCenter gets the distance from the specified point to the center of this area

func (area *NavArea) DistanceFromZ(point Vector3) float32

DistanceFromZ gets the distance the specified point is from a rough estimate of the Z-position for this NavArea

func (area *NavArea) GetCenter() Vector3

GetCenter gets the center point of this area.

func (area *NavArea) GetClosestPointInArea(point Vector3) Vector3

GetClosestPointInArea gets the point closest to the specified point that is contained within this Area.

func (area *NavArea) GetNorthEastPoint() Vector3

GetNorthEastPoint builds the north east point from the two known corner points and the known Z value

func (area *NavArea) GetRoughSquaredArea() float32

GetRoughSquaredArea gets a rough estimate of the squared area of the NavArea

func (area *NavArea) GetSouthWestPoint() Vector3

GetSouthWestPoint builds the south west point from the two known corner points and the known Z value

func (area *NavArea) GetZ(x, y float32) (float32, error)

GetZ gets the Z-coord for the specified point within this area. An error is returned if the requested point is not within this area.

func (area *NavArea) String() string

String converts a NavArea into a human readable string

type NavConnection struct {
	SourceArea   *NavArea     // The starting area for this connection
	TargetAreaID uint32       // The ID of the target area for this NavConnection
	TargetArea   *NavArea     // The target area for this connection
	Direction    NavDirection // The direction of the connection between these two areas
}

NavConnection represents a connection between two NavAreas

type NavDirection int

NavDirection represents a cardinal direction

const (
	// NavDirectionNorth is the north cardinal direction
	NavDirectionNorth NavDirection = iota

	// NavDirectionEast is the east cardinal direction
	NavDirectionEast

	// NavDirectionSouth is the south cardinal direction
	NavDirectionSouth

	// NavDirectionWest is the west cardinal direction
	NavDirectionWest

	// NavDirectionMax is the max value for nav directions
	NavDirectionMax
)
type NavEncounterPath struct {
	FromAreaID    uint32              // The ID of the area the path comes from
	FromArea      *NavArea            // The Area the path comes from
	FromDirection NavDirection        // The direction from the source
	ToAreaID      uint32              // The ID of the area the path ends in
	ToArea        *NavArea            // The area the path ends in
	ToDirection   NavDirection        // The direction from the destination
	Spots         []*NavEncounterSpot // The spots along this path
}

NavEncounterPath represents an encounter path

type NavEncounterSpot struct {
	OrderID             uint32  // The ID of the order of this spot
	ParametricDistiance float32 // The parametric distance
}

NavEncounterSpot represents a spot along an encounter path

type NavHidingSpot struct {
	ID       uint32  // ID of the hiding spot
	Location Vector3 // Location of the hiding NavHidingSpot
	Flags    byte    // Bitflags associated with this hiding spot
}

NavHidingSpot represents an identified hiding spot within a NavArea

type NavLadder struct {
	ID               uint32             // The ID of the ladder
	Width            float32            // The width of the ladder
	Length           float32            // The length of the ladder
	Top              Vector3            // The location of the center of the top of the ladder
	Bottom           Vector3            // The location of the center of the bottom of the ladder
	Direction        NavLadderDirection // The direction of the NavLadder
	TopForwardAreaID uint32             // ID of the area connected to the top-forward position of the ladder
	TopForwardArea   *NavArea           // The area connected to the top-forward position of the ladder
	TopLeftAreaID    uint32             // ID of the area connected to the top-left position of the ladder
	TopLeftArea      *NavArea           // The area connected to the top-forward position of the ladder
	TopRightAreaID   uint32             // ID of the area connected to the top-right position of the ladder
	TopRightArea     *NavArea           // The area connected to the top-right position of the ladder
	TopBehindAreaID  uint32             // ID of area connected to the top-behind position of the ladder
	TopBehindArea    *NavArea           // The area connected to the top-behind position of the ladder
	BottomAreaID     uint32             // ID of the area connected to the bottom of the ladder
	BottomArea       *NavArea           // The area connected to the bottom of the ladder
}

NavLadder represents a ladder within the world

type NavLadderConnection struct {
	SourceArea   *NavArea           // The area that is the source of this NavConnection
	TargetID     uint32             // The ID of the ladder target
	TargetLadder *NavLadder         // The ladder
	Direction    NavLadderDirection // The direction of this connection
}

NavLadderConnection represents a connection between an area and a ladder

type NavLadderDirection int

NavLadderDirection represents the direction between a ladder and its connection

const (
	// NavLadderDirectionUp means up the ladder
	NavLadderDirectionUp NavLadderDirection = iota

	// NavLadderDirectionDown means down the ladder
	NavLadderDirectionDown

	// NavLadderDirectionMax the max value for NavLadderDirection's
	NavLadderDirectionMax
)
type NavMesh struct {
	Places         map[uint32]*NavPlace  // Places contained in this NavMesh
	Areas          map[uint32]*NavArea   // Areas contained in this NavMesh
	Ladders        map[uint32]*NavLadder // Ladders contained in this NavMesh
	QuadTreeAreas  *quadTreeNode         // QuadTree used for quickly searching the NavAreas by position
	MajorVersion   uint32                // The major version number of the nav file
	MinorVersion   uint32                // The minor version number of the nav file
	BSPSize        uint32                // The size of the BSP file the nav was generated from
	IsMeshAnalyzed bool                  // Tracks whether or not this NavMesh has been analyzed
}

NavMesh represents an entire parsed Nav Mesh and provides functionality related to the manipulation and searching of the mesh

func (mesh *NavMesh) GetAreaById(id uint32) *NavArea

Returns area given an id

func (mesh *NavMesh) GetNearestArea(point Vector3, allowBelow bool) *NavArea

Finds the area nearest to this point

func (mesh *NavMesh) GetPlaceByName(name string) *NavPlace

GetPlaceByName gets a NavPlace by the specified name string; nil if not found

type NavPlace struct {
	ID    uint32     // ID of the place
	Name  string     // The name of the place
	Areas []*NavArea // Collection of areas in this place
}

NavPlace represents a Place entry in the NavMesh

func (np *NavPlace) GetEstimatedCenter() (Vector3, error)

GetEstimatedCenter gets a rough estimate of the center of this NavPlace

type NavVisibleArea struct {
	VisibleAreaID uint32   // ID of the visible area
	VisibleArea   *NavArea // The visible area
	Attributes    byte     // Bit-wise attributes
}

NavVisibleArea represents a visible area

type Parser

type Parser struct {
	Reader io.Reader
}

Parser provides support for parsing .nav files.

func (*Parser) Parse

func (p *Parser) Parse() (mesh NavMesh, err error)

Parse parses the nav mesh reader supplied to this instance.

type Path

type Path struct {
	Nodes []*PathNode
}

Path represents a path between two points

func BuildShortestPath

func BuildShortestPath(startArea, endArea *NavArea, areaCostCalc MeshConnectionCalculator, ladderCostCalc MeshLadderCalculator, heurisiticCost HeuristicCalculator) (Path, error)

BuildShortestPath builds a path (via PathFinding A*) and returns a Path object containing the start and end nodes of the path startArea and endArea are the starting and ending NavAreas for the path areaCostCalc is a func() that calculates the "cost" of a connection between two NavAreas ladderCostCalc is a func() that calculates the "cost" of a connection via a ladder heurisiticCost is a func() that estimates an admissible AND monotonic cost for two (likely nonadjacent) NavAreas

func SimpleBuildShortestPath

func SimpleBuildShortestPath(startArea, endArea *NavArea) (Path, error)

SimpleBuildShortestPath builds a path (via PathFinding A*) and returns a Path object containing the start and end nodes of the path startArea and endArea are the starting and ending NavAreas for the path This function uses simple default cost and heuristic functions when building the Path. For more control, see BuildShortestPath

func (*Path) GetCost

func (p *Path) GetCost() float32

GetCost gets the total cost of the path

type PathNode

type PathNode struct {
	Area          *NavArea
	PrevNode      *PathNode
	CostFromStart float32
	// contains filtered or unexported fields
}

PathNode is a single node along a path

type Vector3

type Vector3 struct {
	X, Y, Z float32 // The X, Y, and Z coordinates of the vector
}

Vector3 represents a 3D vector or point in space with X, Y, Z components.

func (*Vector3) Add

func (v *Vector3) Add(right Vector3)

Add adds the specified Vector to this one.

func (*Vector3) Div

func (v *Vector3) Div(right float32)

Div divides this vector by the specified scalar.

func (*Vector3) Length

func (v *Vector3) Length() float32

Length gets the length of the Vector.

func (*Vector3) LengthSquared

func (v *Vector3) LengthSquared() float32

LengthSquared gets the square of the length of the Vector. This operation is faster than Length().

func (*Vector3) Mul

func (v *Vector3) Mul(right float32)

Mul multiplies the specified scalar to this vector.

func (*Vector3) Normalize

func (v *Vector3) Normalize()

Normalize normalizes the Vector by setting its length to 1.

func (*Vector3) Sub

func (v *Vector3) Sub(right Vector3)

Sub subtracts the specified Vector from this one.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL