core

package
v0.0.0-...-d65a093 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package core includes all basic functions to generate and simulate the game world.

Index

Constants

View Source
const (
	GameSpeed   = 30  // Number of iterations per second
	MaxSupply   = 15  // Maximum supply distance
	SupplySpeed = 1.0 // This factor affecting the rate of ammunition regeneration
)

game settings

View Source
const (
	BASE      = 'B'
	DIRT      = 'D'
	FOREST    = 'F'
	GRASS     = 'G'
	HILL      = 'H'
	HOLE      = 'O'
	MOUNTAIN  = 'M'
	STRUCTURE = 'S'
	WATER     = 'W'
)

tile types

View Source
const (
	ARTILLERY = 'A'
	SOLDIER   = 'U'
	TANK      = 'T'
)

unit types

View Source
const (
	RED = iota + 1
	BLUE
	GREEN
	YELLOW
	WHITE
	BLACK
)

player

View Source
const (
	FogOfWar   = 0 // no sight of enemy units
	NormalView = 1 // normal enemy units are visible
	CloseView  = 2 // all units visible, including cloaked ones
)

visibility

View Source
const (
	MOVE = "MOVE" // Move activity command name
	FIRE = "FIRE" // Fire activity command name
)

activity

Variables

View Source
var PLAYERS = []byte{RED, BLUE, GREEN, YELLOW, WHITE, BLACK}

PLAYERS is a list of all players

TILES is a list of all tiles

UNITS is a list of all units

Functions

This section is empty.

Types

type Activity

type Activity struct {
	Name  string // Name of the activity (MOVE, FIRE)
	From  [2]int // Starting coordinates of the activity.
	To    [2]int // Destination coordinates of the activity.
	Start uint64 // Start iteration of the activity.
	End   uint64 // End iteration of the activity.
}

Activity represents a unit's ongoing activity or command.

type Tile

type Tile struct {
	Type    byte  // tile type (see TILES)
	ImageID uint8 // ID used by the GUI to display random images
	XCol    int   // Column of the grid
	YRow    int   // Row of the grid

	// unit
	Unit *Unit // Combat unit located on this tile

	// set by 'update'
	Owner      uint8         // Player who last visited the tile
	Visibility map[uint8]int // Visibility level of each player on this tile (FogOfWar, NormalView, CloseView)
	Supply     map[uint8]int // Supply level of each player on this tile
}

Tile represents a single hexagonal tile within the game world grid.

func FindPath

func FindPath(world *World, unitType byte, startTile, goalTile *Tile) []*Tile

FindPath performs the A* algorithm to find the best path between the two specified tiles.

func NewTile

func NewTile(t byte, xCol, yRow int) *Tile

NewTile creates a new Tile object with the specified type and grid coordinates. It initializes the Tile's attributes and generates a random ImageID for GUI display.

func (*Tile) Clone

func (t *Tile) Clone() *Tile

Clone creates a deep copy of a Tile struct using JSON serialization and deserialization. If any error occurs during the cloning process, nil is returned.

type Unit

type Unit struct {
	Player uint8 // Player identifier.
	Type   byte  // Unit type (see UNITS).
	ID     int   // Unique unit identifier.
	Health int   // Current health points of the unit.

	// current commands
	Activity *Activity // Current activity the unit is engaged in.

	// Attributes set by 'update'
	View        int     // Visibility distance.
	CloseView   int     // Close visibility distance (see hidden).
	FireRange   int     // Firing range distance.
	Speed       uint64  // Movement speed (in game iterations).
	FireSpeed   uint64  // Firing speed (in game iterations).
	Hidden      bool    // Indicates if the unit is hidden.
	Armour      int     // Armor strength.
	Demoralized bool    // Indicates if the unit is demoralized (50% reduced damage).
	Ammunition  float32 // Remaining ammunition count.
}

Unit represents a combat unit in the game.

func NewUnit

func NewUnit(player uint8, typ byte) *Unit

NewUnit creates a new unit with the specified player and type. It initializes the unit's attributes and returns a pointer to the newly created Unit struct.

func (*Unit) Clone

func (u *Unit) Clone() *Unit

Clone creates a deep copy of a Unit struct using JSON serialization and deserialization. If any error occurs during the cloning process, nil is returned.

type World

type World struct {
	Tiles         [][]*Tile       // Two-dimensional slice of tile pointers representing the world's tiles.
	XWidth        int             // The width of the world in tiles.
	YHeight       int             // The height of the world in tiles.
	Reinforcement map[uint64]byte // reinforcement for all players with a base. key is iteration, value is unit type.

	Iteration uint64 // Current iteration (game time) of the world.
	Freeze    bool   // if true, the update function has no effect and the world remains frozen
	// contains filtered or unexported fields
}

World represents the game world with its tiles and dimensions.

func Censorship

func Censorship(world *World, player uint8) *World

Censorship applies censorship or information restriction to a given game world. The function takes a world and a player ID (represented as an uint8) as parameters and returns a modified copy of the game world.

The function performs the following steps:

  1. The original game world is cloned using the Clone method to obtain an independent copy of the world for editing. If cloning fails for any reason, nil is returned.

2. The function iterates through all tiles in the copied world and processes them accordingly.

3. For each tile, the following actions are taken:

  • Delivery data for other players is removed, retaining data only for the specified player.
  • Visibility data for other players is removed, retaining data only for the specified player.
  • For tiles that are in Fog of War visibility mode for the specified player and have no visibility, the owner is reset (if it doesn't belong to the specified player), and any hidden units are removed.
  • For tiles in normal view mode that have hidden units, these hidden units are removed.

4. The edited copied world returned.

Overall, the function enforces a form of visibility restriction and information withholding for the specified player in the game world.

func NewWorld

func NewWorld(xWidth, yHeight int) *World

NewWorld creates a new game world with the specified dimensions and initializes its tiles. It takes the width (xWidth) and height (yHeight) of the world grid as parameters and returns a pointer to the newly created World struct. The function populates the grid with Tile objects using the NewTile function, effectively setting up a game world ready for further simulation and interaction.

func (*World) Clone

func (w *World) Clone() *World

Clone creates a deep copy of a World struct using JSON serialization and deserialization. If any error occurs during the cloning process, nil is returned.

func (*World) ExtNeighbors

func (w *World) ExtNeighbors(tile *Tile, radius int) [][]*Tile

ExtNeighbors returns a 2D slice of tiles representing neighboring tiles with an extended radius from the given tile within the game world. The function takes a Tile pointer and an integer radius as input and calculates a set of neighboring tiles up to the specified radius. It utilizes a breadth-first search algorithm to find all tiles within the given radius while avoiding duplicates. The calculated tiles are stored in a 2D slice, where each sub-slice represents tiles at a specific distance from the source tile.

func (*World) Fire

func (w *World) Fire(from, to *Tile, playerFilter uint8) error

Fire initiates a firing command for a unit from one tile to another within the game world. The function takes two Tile pointers, 'from' and 'to', as input and processes the following steps: - It locks the world to prevent concurrent access. - It checks the validity of 'from' and 'to' inputs. - It retrieves the unit associated with the 'from' tile. - It verifies that the unit is not already processing a command. - It checks if the 'to' tile is within the firing range of the 'from' tile based on the unit's attributes. - It checks if the unit has ammunition available for firing. - It decrements the unit's ammunition count by one. - It sets a firing command for the unit, specifying the start and end iterations. - It returns an error if any of the checks fail or if there's an issue with the input.

func (*World) Json

func (w *World) Json() string

Json converts the World object to a JSON-formatted string.

func (*World) Move

func (w *World) Move(from, to *Tile, playerFilter uint8) (newTo *Tile, err error)

Move initiates a movement command for a unit from one tile to another within the game world. The function takes three parameters: the starting tile ('from'), the target tile ('to'), and an optional player filter ('playerFilter') represented as a player ID (uint8). The 'playerFilter' parameter is used to restrict the move commands to be accepted only from the specified player while rejecting moves for other players.

It returns the new target tile and an error if any issue arises during the process. The new target tile parameter corresponds to the 'to' input parameter, unless pathfinding is applied, in which case it is updated to reflect the first tile reached through pathfinding.

The function performs the following steps:

  1. Acquire the lock on the world using a mutex to ensure thread safety and release the lock when the function exits using the 'defer' keyword.
  2. Check the validity of the 'from' and 'to' input tiles and the player's eligibility.
  3. Check if the unit is already performing an activity.
  4. Check if the 'to' tile is a neighbor of the 'from' tile; if not, use pathfinding.
  5. Check the target tile's validity based on unit and tile types.
  6. Create a movement activity command ('Activity') for the unit and mark it as 'busy'.

The function returns the new 'to' tile and nil error if the command is successfully executed. Otherwise, it returns nil and an error indicating the reason for failure.

func (*World) Neighbors

func (w *World) Neighbors(tile *Tile) []*Tile

Neighbors returns a list of neighboring tiles for the given tile within the game world. It takes a Tile pointer as input and calculates the neighboring tiles based on hexagonal grid coordinates. The function checks the adjacent tiles in six directions: top left, top right, right, bottom right, bottom left, and left. It calculates the coordinates of these neighboring tiles and retrieves them using the Tile method of the World struct.

func (*World) PlayerCount

func (w *World) PlayerCount() int

PlayerCount returns the number of players in this world.

func (*World) Tile

func (w *World) Tile(x, y int) *Tile

Tile returns the Tile at the specified coordinates within the game world. It takes the x and y coordinates as parameters and returns a pointer to the Tile object located at the given position. If the coordinates are outside the valid range of the world grid, the function returns nil, indicating that no Tile exists at that position.

func (*World) TileList

func (w *World) TileList(typeFilter byte) []*Tile

TileList returns a list of tiles that match the specified type within the game world. It takes a filter byte as a parameter to indicate the type of tiles to be included in the list. If the filter byte is set to 0, all tiles are included in the list.

func (*World) Units

func (w *World) Units(playerFilter byte) []*Tile

Units returns a list of tiles that contain units belonging to the specified player within the game world. It takes a filter byte as a parameter to indicate the player whose units should be included in the list. If the filter byte is set to 0, units from all players are included in the list.

func (*World) Update

func (w *World) Update()

Update processes a single iteration of the game world, applying various updates and calculations.

It performs the following steps in sequence: - Sets the owner of bases on the map based on unit presence and proximity. - Updates the supply levels on the map, considering changes in base ownership. - Processes movement and firing commands of units. - Spawn reinforcement for players with a base. - Updates unit statistics and attributes, including ammunition and health. - Heals units stationed at bases over time and fixes demoralization status. - Updates visibility ranges for units on the map. - Advances the iteration count to mark the completion of the current iteration.

Jump to

Keyboard shortcuts

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