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.
PropertiesName | Type | Description |
---|---|---|
map | Map.<string, number> | A map where keys are tile coordinates (as strings) and values are tile types. |
bases | Set.<string> | A set of base tile coordinates. |
spawnTiles | Map.<string, {coord: string, assignedTo: string, available: boolean}> | A map of spawn tiles with their coordinates, assigned agent, and availability status. |
mapSize | number | The size of the map. |
distMat | Array.<Array.<number>> | The distance matrix for all valid tiles in the map. |
indexOf | Map.<string, number> | A map to quickly access the index of a tile based on its coordinates. |
isSpawnSparse | boolean | A flag indicating whether spawn tiles are sparse based on the server configuration. |
- Source
Throws:
- If the map size is null when calculating distances.
- Type
- Error
Classes
Members
size
This method sets the size of the map, which is used for distance calculations.
- Source
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:
Name | Type | Description |
---|---|---|
tile | Object | The tile object containing coordinates and type. |
- Source
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.
- Source
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:
Name | Type | Description |
---|---|---|
serverConfig | ServerConfig | The server configuration containing the maximum number of parcels. |
- Source
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:
Name | Type | Description |
---|---|---|
from | Object | The starting coordinates. |
to | Object | The destination coordinates. |
- Source
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:
Name | Type | Description |
---|---|---|
k | number | The number of clusters (prototypes). |
ids | Array.<string> | The IDs of the agents to assign the spawn tiles to. |
max_iterations | number | The maximum number of iterations for the k-means algorithm. |
stab_error | number | The threshold for stability of prototypes. |
- Source
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:
Name | Type | Description |
---|---|---|
from | Object | The starting coordinates. |
- Source
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.
- Source
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:
Name | Type | Description |
---|---|---|
me | Me | The agent for whom the spawn tile is being searched. |
- Source
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.
- Source
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:
Name | Type | Description |
---|---|---|
tile | Object | The tile object containing coordinates. |
type | number | The type of the tile to set. |
- Source
Returns:
- The old type of the tile before the update.
- Type:
- number