Documentation ¶
Index ¶
- Variables
- func ConvertDToUV(dx, dy, dz int) (u, v float64)
- func ConvertDtoXYZ(dx, dy, dz int) (x, y, z int)
- func ConvertXYZtoD(x, y, z int) (dx, dy, dz int)
- func FindCellDistance(start, end *Cell) int
- func FindCellOffset(start, end *Cell) (int, int, int)
- func GetDataD[T any](g *HexGrid, dx, dy, dz int, defaultData T) (T, error)
- func GetDataHash[T any](g *HexGrid, hash [3]int, defaultData T) (T, error)
- func GetDataXYZ[T any](g *HexGrid, x, y, z int, defaultData T) (T, error)
- type Cell
- type HexGrid
- func (g *HexGrid) CreateCellFromD(dx, dy, dz int, data any) *Cell
- func (g *HexGrid) CreateCellFromHash(hash [3]int, data any) *Cell
- func (g *HexGrid) CreateCellFromXYZ(x, y, z int, data any) *Cell
- func (g *HexGrid) Delete(c *Cell)
- func (g *HexGrid) FindCell(start *Cell, dx, dy, dz int) *Cell
- func (g *HexGrid) FindShortestPath(start *Cell, end *Cell, ...) *PathInformation
- func (g *HexGrid) GetD(dx, dy, dz int) *Cell
- func (g *HexGrid) GetHash(hash [3]int) *Cell
- func (g *HexGrid) GetXYZ(x, y, z int) *Cell
- func (g HexGrid) MarshalJSON() ([]byte, error)
- func (g *HexGrid) Rotate(r int)
- func (g *HexGrid) ToString(displayFunction func(g *HexGrid, c *Cell) string) string
- func (g *HexGrid) UnmarshalJSON(data []byte) error
- type PathInformation
Constants ¶
This section is empty.
Variables ¶
var INVERT = false
Functions ¶
func ConvertDToUV ¶
Determine UV coordinates from directional coordinates. This is a very simple conversion using the directional constants.
func ConvertDtoXYZ ¶
func ConvertXYZtoD ¶
Helper functions for converting between the different coordinate systems. I have just bundled them all below in for ease of reading.
func FindCellDistance ¶
Returns the number of tiles needed to travel to get to a cell. Equal to the manhattan distance but for hexgrids.
func FindCellOffset ¶
Returns the dx, dy, and dz required to go from one cell to another
func GetDataHash ¶ added in v0.2.3
Types ¶
type Cell ¶
type Cell struct { /* xyz position of the cell. Each XYZ position is unique and is what is used to identify a specific cell. These coordinates are slightly harder to intuit, so are not used for more complicated algorithms internally for that reason. From the link, this is the "Cube Coordinate" system. */ X int `json:"x"` Y int `json:"y"` Z int `json:"z"` /* Directional component of the cell from the origin. The directional component is usefull for doing search algorithms, but not as useful for any sort of display. This coordinate system is not from the above link, but it is alluded to. See the note from the page which reads: "There are also cube systems that use q-r, r-s, s-q. One of the interesting properties of that system is that it reveals hexagonal directions." It turns out this is a very powerful property for some algorithms. */ Dx int `json:"dx"` Dy int `json:"dy"` Dz int `json:"dz"` /* UV position of the cell. This information is strictly positional. You can take these UV values to draw the hexagons in their correct location on a screen. From the link, this is the "Axial Coordinate" system. */ U float64 `json:"U"` V float64 `json:"V"` /* Whatever data struct users would like to use to notate information about the cell. */ Data any `json:"data"` }
A Cell is the basic building block for hexgrids. A Cell contains positional information paired with arbitrary data that is customizable by implementation. The basis for some of the coordinate systems used can be found on this link: https://www.redblobgames.com/grids/hexagons/
func CreateCellFromD ¶
Creates a cell from D values
func CreateCellFromHash ¶
Creates a cell from a given XYZ hash. This is a simple binding to make it easier to work with several different coordinate systems at once.
func CreateCellFromXYZ ¶
Creates a cell from x, y, z values
type HexGrid ¶
A Hexgrid is a collection of adjacent hexagons which are all connected. The grid is just a basic hashmap of coordinates to a cell.
func GenerateByProbabilityFunction ¶
func GenerateByProbabilityFunction( n int, probabilityFunction func([3]int) float64, stackProbabilities float64, defaultDataFunction func([3]int) any, ) *HexGrid
Create a random grid based on arbitrary expansion where each of the
outer cells has a probability of being chosen based on some probability function.
n: How large should the generated grid be. Number of cells. Any value less than
or equal to 1 will just result in a single cell at (0, 0, 0).
probabilityFunction: This takes in the cell in terms of dx, dy, and dz
components of the cell as they are easier to work with. You can always convert them into xyz, or uv, using the built-in functions if you'd like.
stackProbabilities: If not zero, a cell will have it's probability of being chosen
raised to the power of the number of adjacent cells that are already in the set. The effect of this is creating shapes with less holes on average. The higher the number, the more "regular" the shape will be. The lower the value, the more irregular. "normal" would be a value of 1.0. A value of zero or less will not function as expected.
defaultDataFunction: What data should be prepopulated in each of the cells based on
it's position. If "nil" is passed, it will just populate every cell's data with nil.
func GenerateRegular ¶
Basic generation function which creates a regular hexagonal grid centered
at 0, 0, 0 with a radius of "n". (A radius of 0 would have a single cell at the center. A radius of 2 corresponds to a regular hexagonal grid with 19 hexes.)
func (*HexGrid) CreateCellFromHash ¶
func (*HexGrid) CreateCellFromXYZ ¶
func (*HexGrid) Delete ¶
///////////////////////// ADD/DELETE ALGORITHMS // /////////////////////////
func (*HexGrid) FindShortestPath ¶
func (g *HexGrid) FindShortestPath( start *Cell, end *Cell, traversableFunction func(*HexGrid, *Cell, []*Cell) (float64, bool), heuristicFunction func(*HexGrid, *Cell, *Cell) float64, ) *PathInformation
Find shortest path takes a start cell and an end cell. It also requires a function which gives the "Cost" of traversing from one cell to another. If that function is nil, it defaults to assigning (1.0, true) to every traversal. This traversable function takes in the full hexgrid, the cell which is currently being prospected, as well as the previous path leading up to this location. If a cell is not traversable, assign the second return value (the bool) to false.
This is an implementation of the A* algorithm applied to a hexagonal grid.
func (HexGrid) MarshalJSON ¶ added in v0.2.4
Function for converting a hexgrid into json format. We need this to handle
the fact that the hexgrid is indexed with a [3]int array, which is not json indexable. We will get around this by converting the hexgrid into a list of cells, instead of a hashable object.
func (*HexGrid) Rotate ¶
Used to rotate the whole hexgrid. r is a value between 1 and 5 which determines
how far counter-clockwise the grid will be rotated.
func (*HexGrid) ToString ¶
Simple printout of the HexGrid. This is mainly just useful for seeing
what cells have data and which don't. You can use the display function parameter if you'd like a more complicated method of visualizing the data. This function should return a single character.
func (*HexGrid) UnmarshalJSON ¶ added in v0.2.4
Function for unmarshalling a hexgrid. This is the inverse operation of thd
above function. Simpler converting the byte array into a hexgrid object.
type PathInformation ¶
type PathInformation struct { Path []*Cell HeuristicValue float64 TraversalValue float64 // contains filtered or unexported fields }
Helper type for doing pathing algorithms