Documentation ¶
Overview ¶
Package core includes all basic functions to generate and simulate the game world.
Index ¶
- Constants
- Variables
- type Activity
- type Tile
- type Unit
- type World
- func (w *World) Clone() *World
- func (w *World) ExtNeighbors(tile *Tile, radius int) [][]*Tile
- func (w *World) Fire(from, to *Tile, playerFilter uint8) error
- func (w *World) Json() string
- func (w *World) Move(from, to *Tile, playerFilter uint8) (newTo *Tile, err error)
- func (w *World) Neighbors(tile *Tile) []*Tile
- func (w *World) PlayerCount() int
- func (w *World) Tile(x, y int) *Tile
- func (w *World) TileList(typeFilter byte) []*Tile
- func (w *World) Units(playerFilter byte) []*Tile
- func (w *World) Update()
Constants ¶
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
const ( BASE = 'B' DIRT = 'D' FOREST = 'F' GRASS = 'G' HILL = 'H' HOLE = 'O' MOUNTAIN = 'M' STRUCTURE = 'S' WATER = 'W' )
tile types
const ( ARTILLERY = 'A' SOLDIER = 'U' TANK = 'T' )
unit types
const ( RED = iota + 1 BLUE GREEN YELLOW WHITE BLACK )
player
const ( FogOfWar = 0 // no sight of enemy units NormalView = 1 // normal enemy units are visible CloseView = 2 // all units visible, including cloaked ones )
visibility
const ( MOVE = "MOVE" // Move activity command name FIRE = "FIRE" // Fire activity command name )
activity
Variables ¶
PLAYERS is a list of all players
TILES is a list of all tiles
var UNITS = []byte{ARTILLERY, SOLDIER, TANK}
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 ¶
FindPath performs the A* algorithm to find the best path between the two specified tiles.
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.
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 ¶
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:
- 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 ¶
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 ¶
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 ¶
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 ¶
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) Move ¶
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:
- 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.
- Check the validity of the 'from' and 'to' input tiles and the player's eligibility.
- Check if the unit is already performing an activity.
- Check if the 'to' tile is a neighbor of the 'from' tile; if not, use pathfinding.
- Check the target tile's validity based on unit and tile types.
- 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 ¶
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 ¶
PlayerCount returns the number of players in this world.
func (*World) 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 ¶
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 ¶
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.