hexgrid

package module
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2022 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var INVERT = false

Functions

func ConvertDToUV

func ConvertDToUV(dx, dy, dz int) (u, v float64)

Determine UV coordinates from directional coordinates. This is a very simple conversion using the directional constants.

func ConvertDtoXYZ

func ConvertDtoXYZ(dx, dy, dz int) (x, y, z int)

func ConvertXYZtoD

func ConvertXYZtoD(x, y, z int) (dx, dy, dz int)

Helper functions for converting between the different coordinate systems. I have just bundled them all below in for ease of reading.

func FindCellDistance

func FindCellDistance(start, end *Cell) int

Returns the number of tiles needed to travel to get to a cell. Equal to the manhattan distance but for hexgrids.

func FindCellOffset

func FindCellOffset(start, end *Cell) (int, int, int)

Returns the dx, dy, and dz required to go from one cell to another

func GetDataD added in v0.2.3

func GetDataD[T any](g *HexGrid, dx, dy, dz int, defaultData T) (T, error)

func GetDataHash added in v0.2.3

func GetDataHash[T any](g *HexGrid, hash [3]int, defaultData T) (T, error)

func GetDataXYZ added in v0.2.3

func GetDataXYZ[T any](g *HexGrid, x, y, z int, defaultData T) (T, error)

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

func CreateCellFromD(dx, dy, dz int, data any) *Cell

Creates a cell from D values

func CreateCellFromHash

func CreateCellFromHash(hash [3]int, data any) *Cell

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

func CreateCellFromXYZ(x, y, z int, data any) *Cell

Creates a cell from x, y, z values

func (*Cell) GetD

func (c *Cell) GetD() [3]int

Get the directional position of the cell.

func (*Cell) GetHash

func (c *Cell) GetHash() [3]int

Get the hashed position value of the Cell. The hash for a cell is just a triple tuple.

func (*Cell) GetUV

func (c *Cell) GetUV() [2]float64

Get the UV value of the cell

type HexGrid

type HexGrid map[[3]int]*Cell

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

func GenerateRegular(n int, defaultData any) *HexGrid

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) CreateCellFromD

func (g *HexGrid) CreateCellFromD(dx, dy, dz int, data any) *Cell

func (*HexGrid) CreateCellFromHash

func (g *HexGrid) CreateCellFromHash(hash [3]int, data any) *Cell

func (*HexGrid) CreateCellFromXYZ

func (g *HexGrid) CreateCellFromXYZ(x, y, z int, data any) *Cell

func (*HexGrid) Delete

func (g *HexGrid) Delete(c *Cell)

///////////////////////// ADD/DELETE ALGORITHMS // /////////////////////////

func (*HexGrid) FindCell

func (g *HexGrid) FindCell(start *Cell, dx, dy, dz int) *Cell

Returns the cell that is the specified direction away

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) GetD

func (g *HexGrid) GetD(dx, dy, dz int) *Cell

func (*HexGrid) GetHash

func (g *HexGrid) GetHash(hash [3]int) *Cell

func (*HexGrid) GetXYZ

func (g *HexGrid) GetXYZ(x, y, z int) *Cell

func (HexGrid) MarshalJSON added in v0.2.4

func (g HexGrid) MarshalJSON() ([]byte, error)

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

func (g *HexGrid) Rotate(r int)

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

func (g *HexGrid) ToString(displayFunction func(g *HexGrid, c *Cell) string) string

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

func (g *HexGrid) UnmarshalJSON(data []byte) error

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

Jump to

Keyboard shortcuts

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