MultiAgent

MultiAgent class for managing multi-agent interactions in the Deliveroo game. It handles desires, intentions, and actions of agents, including pathfinding and communication. It supports both master and slave agents, allowing them to coordinate actions and share information. The class implements a BDI (Belief-Desire-Intention) architecture to manage agent behavior. It includes methods for generating desires, filtering intentions, and executing actions based on the current state of the game. The agent can perform actions such as picking up parcels, depositing them, exploring the map, and avoiding collisions with other agents. It also manages the agent's state, including movement, camping, and collision detection. The class is designed to be extensible, allowing for the addition of new intentions and actions as needed.

Constructor

new MultiAgent(client, me, mate, parcels, mapStore, agentStore, communication, serverConfig, isMaster)

The MultiAgent class implements a BDI architecture to manage agent behavior in the Deliveroo game. It allows agents to generate desires based on the current state of the game, filter those desires into intentions, and execute actions accordingly. The class supports both master and slave agents, enabling them to coordinate actions and share information. It includes methods for pathfinding, collision detection, and managing the agent's state, such as movement, camping, and exploring the map. The agent can perform actions such as picking up parcels, depositing them, exploring the map, and avoiding collisions with other agents. The class is designed to be extensible, allowing for the addition of new intentions and actions as needed. It also includes logging functionality to track agent actions and intentions
Parameters:
NameTypeDescription
clientDeliverooClientThe client instance for API communication.
meMeThe agent's own state.
mateMeThe mate agent's state (for master agents, this is the slave agent).
parcelsParcelsStoreThe store for managing parcels.
mapStoreMapStoreThe store for managing the game map.
agentStoreAgentStoreThe store for managing agents.
communicationCommunicationThe communication model for the agent.
serverConfigServerConfigThe server configuration for the game.
isMasterbooleanFlag to check if the agent is a master agent.
Properties
NameTypeDescription
clientDeliverooClientThe client instance for API communication.

Classes

MultiAgent

Methods

(async) achieveDeposit(isEqualToLastIntention)

This method is responsible for achieving the deposit action by moving to the nearest base and depositing the carried parcels. It checks if the current intention is equal to the last intention and updates the path to the nearest base accordingly. If the agent is already at the base's location, it emits a putdown event to the server. The method uses the `oneStepCheckAgents` method to check for collisions with other agents before performing the deposit action. The agent's state is updated to reflect the deposit action, and it logs the action based on whether it is a master or slave agent.
Parameters:
NameTypeDescription
isEqualToLastIntentionbooleanFlag to check if the intention is equal to the last intention.

(async) achieveDropAndGoAway()

This method is responsible for achieving the drop and go away action by dropping the carried parcels and moving away from the current location. It checks if there are possible moves available and, if so, sets the dropped state, puts down the parcels, and moves away. If there are no possible moves, it communicates with the mate agent to move away. The method updates the agent's state to reflect the drop and go away action, and it logs the action based on whether it is a master or slave agent.

(async) achieveExplore(isEqualToLastIntention)

This method is responsible for achieving the explore action by moving to a random spawn tile or camping on the spawn. It checks if the current intention is equal to the last intention and updates the path to a random spawn tile accordingly. If the agent is camping, it saves the camping start time and checks if the camping time has expired. If the agent is not camping, it moves to a random spawn tile and checks for collisions with other agents. The method updates the agent's state to reflect the explore action, and it logs the action based on whether it is a master or slave agent.
Parameters:
NameTypeDescription
isEqualToLastIntentionbooleanFlag to check if the intention is equal to the last intention.

(async) achieveGoAway()

This method is responsible for achieving the go away action by moving away from the current location. It checks if there are possible moves available and, if so, moves away from the current location. If there are no possible moves, it communicates with the mate agent to move away. The method updates the agent's state to reflect the go away action, and it logs the action based on whether it is a master or slave agent.

(async) achievePickup(p, isEqualToLastIntention, isFromDropped)

This method is responsible for achieving the pickup action by moving towards the specified parcel and picking it up. It checks if the current intention is equal to the last intention and updates the path accordingly. If the agent is already at the parcel's location, it emits a pickup event to the server. If the pickup is from a dropped parcel, it resets the drop communication. The method uses the `oneStepCheckAgents` method to check for collisions with other agents before performing the pickup action. The agent's state is updated to reflect the pickup action, and it logs the action based on whether it is a master or slave agent.
Parameters:
NameTypeDescription
pObjectThe parcel to be picked up.
isEqualToLastIntentionbooleanFlag to check if the intention is equal to the last intention.
isFromDroppedbooleanFlag to check if the pickup is from a dropped parcel.

(async) act()

This method is responsible for executing the agent's actions based on its current intentions. It iterates through the intentions and performs actions such as picking up parcels, depositing them, exploring the map, or moving away from other agents. It checks for conditions such as whether the current intention is equal to the last intention and handles agent collisions. The method uses helper methods to achieve specific actions, such as `achievePickup`, `achieveDeposit`, `achieveDropAndGoAway`, `achieveGoAway`, and `achieveExplore`. The agent's actions are performed in a sequential manner, ensuring that it follows its intentions and updates its state accordingly.

filterIntentions() → {void}

This method filters the agent's desires into intentions by sorting them based on their scores. It ensures that the most desirable actions are prioritized for execution. The intentions are sorted in descending order, with higher scores indicating more desirable actions. The filtered intentions are stored in the `this.intentions` array, which is later used to determine the agent's actions.
Returns:
Type: 
void
Example
// Filter the desires into intentions
agent.filterIntentions();

generateDesires() → {void}

This method is responsible for generating the agent's desires based on the current game state. It evaluates the agent's carried parcels, calculates potential rewards for picking up new parcels, and considers the distance to the nearest base. It also checks for the presence of other agents and their actions, allowing the agent to make informed decisions about its next actions. The desires are sorted by score, with higher scores indicating more desirable actions. The method also handles special cases, such as dropping parcels when close to a mate agent or moving away if another agent is stuck. The generated desires are stored in the `this.desires` array, which is later filtered to create intentions for the agent to act upon.
Returns:
Type: 
void
Example
// Generate desires based on the current game state
agent.generateDesires();

getBasePath(target)

This method is responsible for getting the base path to the nearest base tile. It checks if the agent is already at the base tile and returns if so. If the agent is not at the base tile, it gets a new path to the nearest base using the getNewPath method. If the base tile is not found, it removes the base from the map and sets a timer to restore it later. The method ensures that the agent has a valid path to follow towards the nearest base tile, even if the base is temporarily removed from the map.
Parameters:
NameTypeDescription
targetObjectThe target tile to get the base path to.
Example
agent.getBasePath({ x: 5, y: 10 });

getNewPath(target)

This method is responsible for getting a new A* path to the specified target tile. It removes visible agents from the map to ensure that the pathfinding algorithm does not consider them as obstacles. The calculated path is stored in the `this.path` variable, which is later used for movement actions. The method ensures that the agent has a valid path to follow towards the target tile, even in the presence of other agents.
Parameters:
NameTypeDescription
targetObjectThe target tile to get the path to.
Example
agent.getNewPath({ x: 5, y: 10 });

getPath(target)

This method is responsible for getting the A* path to the specified target tile. It initializes the path index to 0 and calculates the path using the astarSearch function. The calculated path is stored in the `this.path` variable, which is later used for movement actions. The method ensures that the agent has a valid path to follow towards the target tile.
Parameters:
NameTypeDescription
targetObjectThe target tile to get the path to.
Example
agent.getPath({ x: 5, y: 10 });

log(logLevel, …args)

This method logs messages to the console based on the specified log level. It uses the `log` utility function to filter and format the messages according to the log levels defined in the class. The log levels can include master, slave, and action logs, allowing for flexible logging of agent actions and intentions.
Parameters:
NameTypeAttributesDescription
logLevelstringThe log level to filter messages.
argsany<repeatable>
The messages to log.
Examples
// Log a master level message
agent.log(LOG_LEVELS.MASTER, "This is a master level message");
// Log a slave level message
agent.log(LOG_LEVELS.SLAVE, "This is a slave level message");

(async) oneStep()

This method performs one step of the agent path by moving the agent towards the next tile in the path. It checks if the agent is already at the destination tile and does nothing if it is. If the agent is not at the destination tile, it calculates the direction to move and performs the move action. The method updates the agent's state to reflect the movement and increments the path index. It also sets the moving state to false after the move is completed. The method logs the action based on whether it is a master or slave agent.
Example
agent.oneStep();

(async) oneStepCheckAgents(newPathTile)

This method performs a one-step check for agents in the next tile of the path. It checks if any agent is in the tile the agent wants to go to and handles collisions accordingly. If a collision is detected, it sets the colliding state and starts a timer. If the timer expires, it gets a new path or base path based on the last intention. If everything is clear, it performs one step of the agent path. The method updates the agent's state to reflect the one-step check, and it logs the action based on whether it is a master or slave agent.
Parameters:
NameTypeDescription
newPathTileObjectThe new path tile to check for agents.

updateBeliefs()

This method is called to update the agent's beliefs about the game state. It calculates the time difference since the last update and uses it to update the parcels data. The parcels data is updated based on the current frame and the server configuration. This method is essential for keeping the agent's beliefs up-to-date, allowing it to make informed decisions based on the latest game state.
Example
// Update the agent's beliefs
agent.updateBeliefs();