MapStore

MapStore class to store the game map and its tiles

Constructor

new MapStore()

This class is responsible for managing the game map, including adding tiles, calculating distances, and handling spawn tiles. It provides methods to add tiles, set their types, calculate distances, find nearest bases, and perform k-means clustering for spawn tile assignment. It also includes methods to reset k-means assignments and calculate the sparseness of spawn tiles. It uses a Map to store tiles and their types, a Set to store bases, and a Map to store spawn tiles. It also maintains a distance matrix for efficient distance calculations between tiles.
Properties
NameTypeDescription
mapMap.<string, number>A map where keys are tile coordinates (as strings) and values are tile types.
basesSet.<string>A set of base tile coordinates.
spawnTilesMap.<string, {coord: string, assignedTo: string, available: boolean}>A map of spawn tiles with their coordinates, assigned agent, and availability status.
mapSizenumberThe size of the map.
distMatArray.<Array.<number>>The distance matrix for all valid tiles in the map.
indexOfMap.<string, number>A map to quickly access the index of a tile based on its coordinates.
isSpawnSparsebooleanA flag indicating whether spawn tiles are sparse based on the server configuration.
Throws:
If the map size is null when calculating distances.
Type
Error

Classes

MapStore

Members

size

This method sets the size of the map, which is used for distance calculations.

Methods

addTile(tile)

This method adds a tile to the map, updates the spawn tiles if the tile is a spawn tile, and adds the tile to the bases set if it is a base tile.
Parameters:
NameTypeDescription
tileObjectThe tile object containing coordinates and type.

calculateDistances() → {void}

This method computes the distance matrix for all valid tiles in the map. It first collects all valid (non-hole) positions, initializes the distance matrix, and then applies the Floyd-Warshall algorithm to compute the shortest distances between all pairs of tiles.
Throws:
If the map size is null.
Type
Error
Returns:
Type: 
void

calculateSparseness(serverConfig) → {void}

This method calculates the ratio of spawn tiles to the total number of cells in the map that are not empty. It also calculates the ratio of spawn tiles to the maximum number of parcels allowed in the server configuration. If both ratios are below the defined thresholds in the config, it sets isSpawnSparse to true.
Parameters:
NameTypeDescription
serverConfigServerConfigThe server configuration containing the maximum number of parcels.
Returns:
Type: 
void

distance(from, to) → {number}

This method retrieves the indices of the given coordinates from the indexOf map and uses them to access the distance matrix. If either coordinate is not found in the indexOf map, it returns Infinity.
Parameters:
NameTypeDescription
fromObjectThe starting coordinates.
toObjectThe destination coordinates.
Returns:
- The distance between the two coordinates.
Type: 
number

kMeans(k, ids, max_iterations, stab_error)

This method initializes k prototypes with random values, associates each spawn tile to the nearest prototype using Euclidean distance, and updates the prototypes to the means of the associated tiles. It continues iterating until the prototypes are stable or the maximum number of iterations is reached.
Parameters:
NameTypeDescription
knumberThe number of clusters (prototypes).
idsArray.<string>The IDs of the agents to assign the spawn tiles to.
max_iterationsnumberThe maximum number of iterations for the k-means algorithm.
stab_errornumberThe threshold for stability of prototypes.
Throws:
If the length of ids is not equal to k.
Type
Error

nearestBase(from) → {Array}

This method iterates through all bases in the map, calculates the distance from the given coordinates to each base, and returns the coordinates of the nearest base along with the minimum distance found.
Parameters:
NameTypeDescription
fromObjectThe starting coordinates.
Returns:
- An array containing the coordinates of the nearest base and the distance to it.
Type: 
Array

printAllDistances() → {void}

This method iterates through all pairs of coordinates in the map and logs the distance between them. It is useful for debugging and understanding the distance relationships in the map.
Returns:
Type: 
void

randomSpawnTile(me) → {Object}

This method filters the spawn tiles to find those that are available and either assigned to the given agent or unassigned. It then selects a random tile from the filtered list and returns its coordinates.
Parameters:
NameTypeDescription
meMeThe agent for whom the spawn tile is being searched.
Returns:
- The coordinates of the randomly selected spawn tile.
Type: 
Object

resetKmeans() → {void}

This method iterates through all spawn tiles and sets their assignedTo property to null, effectively resetting the k-means clustering assignments.
Returns:
Type: 
void

setType(tile, type) → {number}

This method updates the type of a tile in the map. If the old type was SPAWN, it marks the spawn tile as unavailable. If the old type was BASE, it removes the base from the set. It then sets the new type and updates spawn tiles and bases accordingly.
Parameters:
NameTypeDescription
tileObjectThe tile object containing coordinates.
typenumberThe type of the tile to set.
Returns:
- The old type of the tile before the update.
Type: 
number