grid

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2018 License: BSD-2-Clause, Zlib Imports: 4 Imported by: 0

Documentation

Overview

Package grid is used to generate layout data for random maze or skirmish levels. Maze levels have dead ends such that there is only one path to get from one spot in the maze to another. Skirmish levels have no dead ends in order to provide plenty of movement options.

Expected usage:

maze := grid.New(PRIM_MAZE)  // Create a type of grid.
maze.Generate(width, height) // Generate the grid.
for x := 0; x < width; x++ {
   for y := 0; y < height; y++ {
      if maze.isOpen(x, y) {
          // Do something with an open area.
      } else {
          // Do something with a wall.
      }
   }
}

Package grid is provided as part of the vu (virtual universe) 3D engine.

Index

Constants

View Source
const (
	// PrimMaze is a Randomized Prim's Algorithm (see wikipedia).
	PrimMaze = iota

	// SparseSkirmish creates a skirmish grid by randomly traversing all the
	// grid locations and adding random walls as long as there are more than
	// two ways out of the grid location.
	SparseSkirmish

	// RoomSkirmish is a skirmish grid created by subdividing the area
	// recursively into rooms and chopping random exits in the room walls.
	RoomSkirmish

	// DenseSkirmish is a corridor only skirmish grid. It is a Prim's maze
	// where the dead-ends have been eliminated.  Additionally each side of
	// the grid is guaranteed to have one exit to the level exterior.
	DenseSkirmish

	// Cave produces interconnected non-square areas resembling a large
	// series of caves.
	Cave

	// Dungeon produces interconnected square areas resembling a series
	// of rooms connected by corridors.
	Dungeon
)

Currently supported grid types that are used as the input to grid.New().

View Source
const (
	// Flat grids use Up/Down prefixes.
	DR = 0 // down and right
	UL = 1 // up and left
	UR = 2 // up and right
	DL = 3 // down and left
	UP = 4 // up
	DN = 5 // down

	// Pointy grids use Right/Left prefixes.
	RT = 0 // right
	LT = 1 // left
	RU = 2 // right and up
	LD = 3 // left and down
	LU = 4 // left and up
	RD = 5 // right and down
)

The six hex grid direction constants and related coordinate differences. The movement directions reflect the movement angles for the two types of hex grids. Note the relative change to the QRS hex coordinate.

    FLAT                   POINTY
-+0  0+-  +0-          0+-       +0-
 UL   UP  UR            LU       RU
    ↖ ⇧ ↗                  ↖  ↗
                    -+0 LT ⇦  ⇨  RT +-0
    ↙ ⇩ ↘                  ↙  ↘
 DL   DN  DR            LD       RD
-0+  0-+  +-0          -0+       0-+

Variables

View Source
var ZH = &Hex{0, 0, 0}

ZH is Zero hex. Used when a non-nil hex is needed. Value is always expected to be 0, 0, 0.

Functions

This section is empty.

Types

type Grid

type Grid interface {
	// Size returns the current size of the grid. This will be 0, 0 if
	// Generate has not yet been called.
	Size() (width, depth int) // The current size of the plan.

	// IsOpen returns true if the cell at the given location is traversable.
	// Otherwise the cell is blocked and can be considered a wall.
	IsOpen(x, y int) bool // Return true if the given location is traversable.

	// Seed can be set to generate the same map each time. Leave it unset
	// to get a random map each time.
	Seed(seed int64)

	// Generate creates a grid full of walls and floors based on
	// the given depth and width.
	//
	// The minimum maze dimension is 7x7, and grids must be odd numbered.
	// The given grids width and height are modified, if necessary, to ensure
	// valid values.
	//
	// Generate needs to be called after Seed and before other grid methods.
	// It can be called any time to create a new grid.
	Generate(width, depth int) Grid

	// Band returns the grid depth based on concentric squares. Numbering
	// starts at 0 on the outside and increases towards the center. Using band
	// implies (makes more sense if) the grid width and height are the same.
	Band(x, y int) int
}

Grid generates a random floor plan where each grid cell is either a wall or a floor. The expected usage is to generate a random level and then to use the level by traversing it with the Size and IsOpen methods.

func New

func New(gridType int) Grid

New creates a new grid based on the given gridType. Returns nil if the gridType is not recognized.

type Hex added in v0.6.2

type Hex struct {
	Q, R, S int32 // Cube coordinates: sum equals 0.
}

Hex represents the location of one hex within a grid of hexes. The locations are relative to the map origin is at 0, 0, 0. The coordinates are uint32 so a unique identifier can be created from the combination of Q and R.

func Diff added in v0.6.2

func Diff(dir int) *Hex

Diff returns the hex that is added to the current hex to move in the given direction. Return the zero-hex if the movement is unrecognized.

func NewHex added in v0.6.2

func NewHex(q, r int32) *Hex

NewHex returns a hex such that the coordinates Q+R-S=0.

func (*Hex) Add added in v0.6.2

func (h *Hex) Add(a, b *Hex) *Hex

Add (+) adds hexes b and a storing the results of the addition in h. Hex h may be used as one or both of the parameters. For example (+=) is

h.Add(h, b)

The updated Hex h is returned.

func (*Hex) Dist added in v0.6.2

func (h *Hex) Dist(a *Hex) int

Dist returns the distance between hexes h and a.

func (*Hex) Eq added in v0.6.2

func (h *Hex) Eq(a *Hex) bool

Eq checks for equality and returns true if Hex h has identical Q,R,S values to the given Hex a.

func (*Hex) ID added in v0.6.2

func (h *Hex) ID() uint64

ID returns a single unique value for this hex tile. It is a bitwise combination of Q and R.

func (*Hex) Len added in v0.6.2

func (h *Hex) Len() int

Len determines the distance of Hex h to the origin 0,0,0.

func (*Hex) Move added in v0.6.2

func (h *Hex) Move(a *Hex, dir int) *Hex

Move returns the next hex h when travelling in the given direction dir from hex a. Hex h may be used as one or both of the parameters, eg:

h.Next(h, N, moveNS)

The updated vector h is returned.

func (*Hex) Mult added in v0.6.2

func (h *Hex) Mult(a *Hex, k int32) *Hex

Mult (*) multiplies hex a by scale k, storing the results of the multiplication in h. Hex h may be used as the parameter. For example (*=) is

h.Mult(h, k)

The updated Hex h is returned.

func (*Hex) Sub added in v0.6.2

func (h *Hex) Sub(a, b *Hex) *Hex

Sub (-) subtracts hex b from a storing the results of the subtraction in h. Hex h may be used as one or both of the parameters. For example (-=) is

h.Sub(h, b)

The updated Hex h is returned.

func (*Hex) ToFlat added in v0.6.2

func (h *Hex) ToFlat(size float64) (x, y float64)

ToFlat returns the 2D flat grid location for this hex location using the given hex size.

func (*Hex) ToPointy added in v0.6.2

func (h *Hex) ToPointy(size float64) (x, y float64)

ToPointy returns the 2D pointy grid location for this hex location using the given hex size.

Jump to

Keyboard shortcuts

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