maps

package
v1.1.6 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2022 License: AGPL-3.0 Imports: 4 Imported by: 0

README

Game Maps

Game maps are a way to customize the game board independently of the pipeline of game rules, including snake positions, food and hazard spawning. More advanced mechanics, such as snake bots generated by the map, may be available in the future.

Anyone can write a new game map and submit a PR! Currently there are a few additional changes needed behind the scenes for it to appear in the production Battlesnake engine and on play.battlesnake.com, but you'll be able to use your own map right away with the battlesnake CLI.

How to write a map

You'll need to create a new Go type that implements maps.GameMap, and register it under a unique identifier. The methods your game map will need to implement are:

ID

Returns the unique ID this game map will be registered under. This method exists mostly to guard against typos while registering maps.

Meta

Returns some optional metadata about the map, currently name, author, and description. At some point we hope to expose this through the UI to give credit to community map authors.

SetupBoard

Called to generate a new board. The map is responsible for placing all snakes, food, and hazards. An initial rules.BoardState is passed in that will be initialized with the width, height, and snakes with IDs but no bodies. SetupBoard should not modify this BoardState, but instead call methods on the maps.Editor to place snakes and optionally food/hazards.

UpdateBoard

Called to update an existing board every turn. For a map that doesn't spawn food or hazards after initial creation, this method can be a no-op! For maps that just do standard random food spawning, delegating to one of the existing maps is a good way to handle that.

Registering your map

Your map will need to be registered with its own ID using maps.RegisterMap. There are a few automated tests that will be run automatically on any registered map to ensure it appears to work correctly. You can run those tests yourself with:

go test ./maps

Things to watch out for

  • SetupBoard is called before any turns are run and before the game rules are applied. UpdateBoard is called at the end of each turn, after snakes have moved, been eliminated, etc.
  • There's no protection against placing duplicate food/hazards on the same location on the board. Maps need to account for this, especially when generating random food/hazard spawns.
  • All maps that make use of random behaviour should use the GetRand method on the settings object passed in to get a random number generator seeded with the game's seed and current turn. This will ensure the map generates in a reliable way, and will allow reproducing games based on the seed at some point in the near future.

How to test your map

  • You can trigger a game locally using the new map with:
battlesnake play --width 11 --height 11 --name mysnake --url http://example.com/snake --name othersnake --url http://example.com/snake --map MAP_ID  --viewmap

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ArcadeMazeHazards []rules.Point = []rules.Point{}/* 187 elements not displayed */

Functions

func RegisterMap

func RegisterMap(id string, m GameMap)

RegisterMap adds a map to the global registry.

func SetupBoard

func SetupBoard(mapID string, settings rules.Settings, width, height int, snakeIDs []string) (*rules.BoardState, error)

SetupBoard is a shortcut for looking up a map by ID and initializing a new board state with it.

func TestMap added in v1.1.4

func TestMap(id string, m GameMap, callback func())

func UpdateBoard

func UpdateBoard(mapID string, previousBoardState *rules.BoardState, settings rules.Settings) (*rules.BoardState, error)

UpdateBoard is a shortcut for looking up a map by ID and updating an existing board state with it.

Types

type ArcadeMazeMap added in v1.1.5

type ArcadeMazeMap struct{}

func (ArcadeMazeMap) ID added in v1.1.5

func (m ArcadeMazeMap) ID() string

func (ArcadeMazeMap) Meta added in v1.1.5

func (m ArcadeMazeMap) Meta() Metadata

func (ArcadeMazeMap) SetupBoard added in v1.1.5

func (m ArcadeMazeMap) SetupBoard(initialBoardState *rules.BoardState, settings rules.Settings, editor Editor) error

func (ArcadeMazeMap) UpdateBoard added in v1.1.5

func (m ArcadeMazeMap) UpdateBoard(lastBoardState *rules.BoardState, settings rules.Settings, editor Editor) error

type BoardStateEditor

type BoardStateEditor struct {
	*rules.BoardState
}

An Editor backed by a BoardState.

func NewBoardStateEditor

func NewBoardStateEditor(boardState *rules.BoardState) *BoardStateEditor

func (*BoardStateEditor) AddFood

func (editor *BoardStateEditor) AddFood(p rules.Point)

func (*BoardStateEditor) AddHazard

func (editor *BoardStateEditor) AddHazard(p rules.Point)

func (*BoardStateEditor) ClearFood

func (editor *BoardStateEditor) ClearFood()

func (*BoardStateEditor) ClearHazards

func (editor *BoardStateEditor) ClearHazards()

func (*BoardStateEditor) PlaceSnake

func (editor *BoardStateEditor) PlaceSnake(id string, body []rules.Point, health int)

func (*BoardStateEditor) RemoveFood

func (editor *BoardStateEditor) RemoveFood(p rules.Point)

func (*BoardStateEditor) RemoveHazard

func (editor *BoardStateEditor) RemoveHazard(p rules.Point)

type ColumnsHazardsMap added in v1.1.6

type ColumnsHazardsMap struct{}

func (ColumnsHazardsMap) ID added in v1.1.6

func (m ColumnsHazardsMap) ID() string

func (ColumnsHazardsMap) Meta added in v1.1.6

func (m ColumnsHazardsMap) Meta() Metadata

func (ColumnsHazardsMap) SetupBoard added in v1.1.6

func (m ColumnsHazardsMap) SetupBoard(lastBoardState *rules.BoardState, settings rules.Settings, editor Editor) error

func (ColumnsHazardsMap) UpdateBoard added in v1.1.6

func (m ColumnsHazardsMap) UpdateBoard(lastBoardState *rules.BoardState, settings rules.Settings, editor Editor) error

type ConcentricRingsHazardsMap added in v1.1.6

type ConcentricRingsHazardsMap struct{}

func (ConcentricRingsHazardsMap) ID added in v1.1.6

func (ConcentricRingsHazardsMap) Meta added in v1.1.6

func (ConcentricRingsHazardsMap) SetupBoard added in v1.1.6

func (m ConcentricRingsHazardsMap) SetupBoard(lastBoardState *rules.BoardState, settings rules.Settings, editor Editor) error

func (ConcentricRingsHazardsMap) UpdateBoard added in v1.1.6

func (m ConcentricRingsHazardsMap) UpdateBoard(lastBoardState *rules.BoardState, settings rules.Settings, editor Editor) error

type DirectionalExpandingBoxMap added in v1.1.6

type DirectionalExpandingBoxMap struct{}

func (DirectionalExpandingBoxMap) ID added in v1.1.6

func (DirectionalExpandingBoxMap) Meta added in v1.1.6

func (DirectionalExpandingBoxMap) SetupBoard added in v1.1.6

func (m DirectionalExpandingBoxMap) SetupBoard(lastBoardState *rules.BoardState, settings rules.Settings, editor Editor) error

func (DirectionalExpandingBoxMap) UpdateBoard added in v1.1.6

func (m DirectionalExpandingBoxMap) UpdateBoard(lastBoardState *rules.BoardState, settings rules.Settings, editor Editor) error

type Editor

type Editor interface {
	// Clears all food from the board.
	ClearFood()

	// Clears all hazards from the board.
	ClearHazards()

	// Adds a food to the board. Does not check for duplicates.
	AddFood(rules.Point)

	// Adds a hazard to the board. Does not check for duplicates.
	AddHazard(rules.Point)

	// Removes all food from a specific tile on the board.
	RemoveFood(rules.Point)

	// Removes all hazards from a specific tile on the board.
	RemoveHazard(rules.Point)

	// Updates the body and health of a snake.
	PlaceSnake(id string, body []rules.Point, health int)
}

Editor is used by GameMap implementations to modify the board state.

type EmptyMap added in v1.1.2

type EmptyMap struct{}

func (EmptyMap) ID added in v1.1.2

func (m EmptyMap) ID() string

func (EmptyMap) Meta added in v1.1.2

func (m EmptyMap) Meta() Metadata

func (EmptyMap) SetupBoard added in v1.1.2

func (m EmptyMap) SetupBoard(initialBoardState *rules.BoardState, settings rules.Settings, editor Editor) error

func (EmptyMap) UpdateBoard added in v1.1.2

func (m EmptyMap) UpdateBoard(lastBoardState *rules.BoardState, settings rules.Settings, editor Editor) error

type ExpandingBoxMap added in v1.1.6

type ExpandingBoxMap struct{}

func (ExpandingBoxMap) ID added in v1.1.6

func (m ExpandingBoxMap) ID() string

func (ExpandingBoxMap) Meta added in v1.1.6

func (m ExpandingBoxMap) Meta() Metadata

func (ExpandingBoxMap) SetupBoard added in v1.1.6

func (m ExpandingBoxMap) SetupBoard(lastBoardState *rules.BoardState, settings rules.Settings, editor Editor) error

func (ExpandingBoxMap) UpdateBoard added in v1.1.6

func (m ExpandingBoxMap) UpdateBoard(lastBoardState *rules.BoardState, settings rules.Settings, editor Editor) error

type ExpandingScatterMap added in v1.1.6

type ExpandingScatterMap struct{}

func (ExpandingScatterMap) ID added in v1.1.6

func (m ExpandingScatterMap) ID() string

func (ExpandingScatterMap) Meta added in v1.1.6

func (m ExpandingScatterMap) Meta() Metadata

func (ExpandingScatterMap) SetupBoard added in v1.1.6

func (m ExpandingScatterMap) SetupBoard(lastBoardState *rules.BoardState, settings rules.Settings, editor Editor) error

func (ExpandingScatterMap) UpdateBoard added in v1.1.6

func (m ExpandingScatterMap) UpdateBoard(lastBoardState *rules.BoardState, settings rules.Settings, editor Editor) error

type GameMap

type GameMap interface {
	// Return a unique identifier for this map.
	ID() string

	// Return non-functional metadata about this map.
	Meta() Metadata

	// Called to generate a new board. The map is responsible for placing all snakes, food, and hazards.
	SetupBoard(initialBoardState *rules.BoardState, settings rules.Settings, editor Editor) error

	// Called every turn to optionally update the board.
	UpdateBoard(previousBoardState *rules.BoardState, settings rules.Settings, editor Editor) error
}

func GetMap

func GetMap(id string) (GameMap, error)

GetMap returns the map associated with the given ID from the global registry.

type InnerBorderHazardsMap added in v1.1.6

type InnerBorderHazardsMap struct{}

func (InnerBorderHazardsMap) ID added in v1.1.6

func (InnerBorderHazardsMap) Meta added in v1.1.6

func (InnerBorderHazardsMap) SetupBoard added in v1.1.6

func (m InnerBorderHazardsMap) SetupBoard(lastBoardState *rules.BoardState, settings rules.Settings, editor Editor) error

func (InnerBorderHazardsMap) UpdateBoard added in v1.1.6

func (m InnerBorderHazardsMap) UpdateBoard(lastBoardState *rules.BoardState, settings rules.Settings, editor Editor) error

type MapRegistry

type MapRegistry map[string]GameMap

MapRegistry is a mapping of map names to game maps.

func (MapRegistry) GetMap

func (registry MapRegistry) GetMap(id string) (GameMap, error)

GetMap returns the map associated with the given ID.

func (MapRegistry) RegisterMap

func (registry MapRegistry) RegisterMap(id string, m GameMap)

RegisterMap adds a stage to the registry. If a map has already been registered this will panic.

type Metadata

type Metadata struct {
	Name        string
	Author      string
	Description string
}

type RiverAndBridgesHazardsMap added in v1.1.6

type RiverAndBridgesHazardsMap struct{}

func (RiverAndBridgesHazardsMap) ID added in v1.1.6

func (RiverAndBridgesHazardsMap) Meta added in v1.1.6

func (RiverAndBridgesHazardsMap) SetupBoard added in v1.1.6

func (m RiverAndBridgesHazardsMap) SetupBoard(lastBoardState *rules.BoardState, settings rules.Settings, editor Editor) error

func (RiverAndBridgesHazardsMap) UpdateBoard added in v1.1.6

func (m RiverAndBridgesHazardsMap) UpdateBoard(lastBoardState *rules.BoardState, settings rules.Settings, editor Editor) error

type RoyaleHazardsMap added in v1.1.2

type RoyaleHazardsMap struct{}

func (RoyaleHazardsMap) ID added in v1.1.2

func (m RoyaleHazardsMap) ID() string

func (RoyaleHazardsMap) Meta added in v1.1.2

func (m RoyaleHazardsMap) Meta() Metadata

func (RoyaleHazardsMap) SetupBoard added in v1.1.2

func (m RoyaleHazardsMap) SetupBoard(lastBoardState *rules.BoardState, settings rules.Settings, editor Editor) error

func (RoyaleHazardsMap) UpdateBoard added in v1.1.2

func (m RoyaleHazardsMap) UpdateBoard(lastBoardState *rules.BoardState, settings rules.Settings, editor Editor) error

type ScatterFillMap added in v1.1.6

type ScatterFillMap struct{}

func (ScatterFillMap) ID added in v1.1.6

func (m ScatterFillMap) ID() string

func (ScatterFillMap) Meta added in v1.1.6

func (m ScatterFillMap) Meta() Metadata

func (ScatterFillMap) SetupBoard added in v1.1.6

func (m ScatterFillMap) SetupBoard(lastBoardState *rules.BoardState, settings rules.Settings, editor Editor) error

func (ScatterFillMap) UpdateBoard added in v1.1.6

func (m ScatterFillMap) UpdateBoard(lastBoardState *rules.BoardState, settings rules.Settings, editor Editor) error

type SpiralHazardsMap added in v1.1.6

type SpiralHazardsMap struct{}

func (SpiralHazardsMap) ID added in v1.1.6

func (m SpiralHazardsMap) ID() string

func (SpiralHazardsMap) Meta added in v1.1.6

func (m SpiralHazardsMap) Meta() Metadata

func (SpiralHazardsMap) SetupBoard added in v1.1.6

func (m SpiralHazardsMap) SetupBoard(lastBoardState *rules.BoardState, settings rules.Settings, editor Editor) error

func (SpiralHazardsMap) UpdateBoard added in v1.1.6

func (m SpiralHazardsMap) UpdateBoard(lastBoardState *rules.BoardState, settings rules.Settings, editor Editor) error

type StandardMap

type StandardMap struct{}

func (StandardMap) ID

func (m StandardMap) ID() string

func (StandardMap) Meta

func (m StandardMap) Meta() Metadata

func (StandardMap) SetupBoard

func (m StandardMap) SetupBoard(initialBoardState *rules.BoardState, settings rules.Settings, editor Editor) error

func (StandardMap) UpdateBoard

func (m StandardMap) UpdateBoard(lastBoardState *rules.BoardState, settings rules.Settings, editor Editor) error

type StubMap added in v1.1.1

type StubMap struct {
	Id             string
	SnakePositions map[string]rules.Point
	Food           []rules.Point
	Hazards        []rules.Point
	Error          error
}

An implementation of GameMap that just does predetermined placements, for testing.

func (StubMap) ID added in v1.1.1

func (m StubMap) ID() string

func (StubMap) Meta added in v1.1.1

func (StubMap) Meta() Metadata

func (StubMap) SetupBoard added in v1.1.1

func (m StubMap) SetupBoard(initialBoardState *rules.BoardState, settings rules.Settings, editor Editor) error

func (StubMap) UpdateBoard added in v1.1.1

func (m StubMap) UpdateBoard(previousBoardState *rules.BoardState, settings rules.Settings, editor Editor) error

Jump to

Keyboard shortcuts

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