Methods
astarSearch(start, goal, mapStore)
This function implements the A* search algorithm to find the shortest path from start to goal. It uses a heuristic based on the Manhattan distance to prioritize nodes in the search. The function returns an array of coordinates representing the path from start to goal. If no path is found, it returns an empty array.
Parameters:
Name | Type | Description |
---|---|---|
start | Object | Starting coordinates |
goal | Object | Goal coordinates |
mapStore | MapStore | The MapStore instance containing the grid and obstacles |
- Source
buildExecutor(onMove, onPickup, onDeposit) → {PddlExecutor}
Builds a PddlExecutor instance with action handlers for MOVE, PICKUP, and DEPOSIT.
Parameters:
Name | Type | Description |
---|---|---|
onMove | function | Function to call when MOVE action is executed. |
onPickup | function | Function to call when PICKUP action is executed. |
onDeposit | function | Function to call when DEPOSIT action is executed. |
- Source
Returns:
- Configured PddlExecutor instance.
- Type:
- PddlExecutor
checkIfIamAtBase(me, mapStore) → {boolean}
This function checks if the current player's coordinates match any of the base locations in the map store. It uses the coord2Key function to convert the player's coordinates into a key that can be checked against the map store's bases.
Parameters:
Name | Type | Description |
---|---|---|
me | Me | The current player instance. |
mapStore | MapStore | The MapStore instance containing the current map state. |
- Source
Throws:
- - Throws an error if the map store is not initialized or if the player's coordinates are invalid.
- Type
- Error
Returns:
- Returns true if the player is at a base, false otherwise.
- Type:
- boolean
direction(from, to) → {string|null}
Determine the direction from one coordinate to another.
Parameters:
Name | Type | Description |
---|---|---|
from | Object | Starting coordinates |
to | Object | Target coordinates |
- Source
Returns:
- Returns 'right', 'left', 'up', 'down' or null if coordinates are the same
- Type:
- string |
null
easyExplore(client, me, mapStore) → {Promise.<boolean>}
This function finds a random spawn tile on the map and uses the A* search algorithm to calculate the path to that tile. It then moves the agent in the direction of each step in the path. If a valid path is found, it moves the agent step by step. If no spawn tile is found or no valid path exists, it logs an error message and returns false.
Parameters:
Name | Type | Description |
---|---|---|
client | DeliverooClient | The Deliveroo client instance. |
me | Me | The current player instance. |
mapStore | MapStore | The MapStore instance containing the current map state. |
- Source
Returns:
- Returns true if successfully moved to a spawn tile, false otherwise.
- Type:
- Promise.<boolean>
executePlan(rawPlan, onMove, onPickup, onDeposit) → {Promise.<void>}
Executes a PDDL plan using the provided action handlers.
Parameters:
Name | Type | Description |
---|---|---|
rawPlan | Array | The raw plan generated by the PDDL solver. |
onMove | function | Function to call when MOVE action is executed. |
onPickup | function | Function to call when PICKUP action is executed. |
onDeposit | function | Function to call when DEPOSIT action is executed. |
- Source
Throws:
- - Throws an error if the plan execution fails.
- Type
- Error
Returns:
- A promise that resolves when the plan execution is complete.
- Type:
- Promise.<void>
generateDeliverooDomain()
Returns a static PDDL domain definition (deliveroo-domain.pddl).
- Source
generateDeliverooProblem()
Predicates that I have in the domain-file: - (at ?a - agent ?t - tile) -(parcel-at ?p - parcel ?t - tile) - (carrying ?a - agent ?p - parcel) - (base-at ?b - base ?t - tile) - (delivered ?p - parcel) -(adjacent ?t1 - tile ?t2 - tile) So I need to use these predicates to build the problem The PDDL problem definition has the following structure: define (problem PROBLEM_NAME) (:domain DOMAIN_NAME) (:objects OBJ1 OBJ2 ... OBJ_N) (:init ATOM1 ATOM2 ... ATOM_N) (:goal CONDITION_FORMULA) )
getNearTiles(mapStore, me, mate, myPos) → {Array.<string>}
Gets the possible near tiles for movement based on the current position of the agent and its mate.
Parameters:
Name | Type | Description |
---|---|---|
mapStore | MapStore | The MapStore instance containing the current map state. |
me | Me | The current player instance. |
mate | Me | The mate player instance. |
myPos | Object | The current position of the agent. |
- Source
Returns:
- An array of possible movement directions (e.g., ['UP', 'DOWN']).
- Type:
- Array.<string>
getPlan(mapStore, parcelsStore, me, serverConfig) → {Promise.<Array>}
Generates a PDDL plan for the Deliveroo problem using the online solver.
Parameters:
Name | Type | Description |
---|---|---|
mapStore | MapStore | The MapStore instance containing the current map state. |
parcelsStore | ParcelsStore | The ParcelsStore instance containing the current parcels state. |
me | Me | The current player instance. |
serverConfig | ServerConfig | The server configuration instance. |
- Source
Throws:
- - Throws an error if the solver fails to generate a plan.
- Type
- Error
Returns:
- A promise that resolves to the raw plan generated by the solver.
- Type:
- Promise.<Array>
goAway(client, me, mate, mapStore) → {Promise.<void>}
This function iterates a specified number of times, each time selecting a random direction from the possible near tiles around the agent's current position. It moves the agent in that random direction and updates the agent's position for the next iteration. This is useful for simulating random movement away from the current position, which can be useful in various game scenarios.
Parameters:
Name | Type | Description |
---|---|---|
client | DeliverooClient | The Deliveroo client instance. |
me | Me | The current player instance. |
mate | Me | The mate player instance. |
mapStore | MapStore | The MapStore instance containing the current map state. |
- Source
Returns:
- A promise that resolves when the movement is complete.
- Type:
- Promise.<void>
(async) initApp() → {Promise.<Object>}
This function initializes the main components of the application: - DeliverooClient: The client to interact with the Deliveroo API. - Me: Represents the current player. - ParcelsStore: Manages the parcels in the game. - MapStore: Manages the game map - AgentStore: Manages the agents in the game. - ServerConfig: Holds the server configuration.
- Source
Throws:
- - Throws an error if initialization fails.
- Type
- Error
Returns:
- An object containing initialized instances.
- Type:
- Promise.<Object>
(async) main()
Main function to initialize the Deliveroo client, models, and agent. It sets up event listeners for client events and runs the agent's decision-making loop. The agent updates its beliefs, generates desires, filters intentions, and acts based on the current state of the game. The loop continues until the game is terminated, allowing the agent to continuously adapt to the game environment.
- Source
(async) mainMulti()
Main function to initialize the Deliveroo client, models, and agents. It sets up event listeners for client events and runs the agents' decision-making loop. The agents update their beliefs, generate desires, filter intentions, and act based on the current state of the game. The loop continues until the game is terminated, allowing the agents to continuously adapt to the game environment.
- Source
(async) mainSinglePDDL() → {Promise.<void>}
This function initializes the application by creating instances of necessary classes, registers event handlers, waits for the application to be ready, and then starts the main planning loop. It serves as the entry point for the application, ensuring that all components are set up correctly before starting the main logic.
- Source
Throws:
- - Throws an error if the application initialization or planning loop fails.
- Type
- Error
Returns:
- A promise that resolves when the application is fully initialized and the planning loop is running.
- Type:
- Promise.<void>
makeOnDeposit(client, parcels, me) → {function}
This function returns a handler that can be used to emit a putdown action. It uses the client to emit a putdown event with the current parcels and the player's ID. This will trigger the server to handle the deposit logic for the parcels.
Parameters:
Name | Type | Description |
---|---|---|
client | DeliverooClient | The Deliveroo client instance. |
parcels | ParcelsStore | The parcels store instance. |
me | Me | The current player instance. |
- Source
Returns:
- A function that emits a putdown action with the current parcels when called.
- Type:
- function
makeOnMove(client, me) → {function}
This function returns a handler that can be used to move the agent to a specified target position. It calculates the direction from the current position to the target position and uses the client to emit a move action. It also waits for the move to complete and checks if the agent has reached the target position. If the move fails or the agent does not reach the target position, it logs an error and rejects the promise.
Parameters:
Name | Type | Description |
---|---|---|
client | DeliverooClient | The Deliveroo client instance. |
me | Me | The current player instance. |
- Source
Throws:
- - Throws an error if the direction cannot be determined or if the move fails.
- Type
- Error
Returns:
- A function that takes target coordinates and moves the agent.
- Type:
- function
makeOnPickup(client, me) → {function}
This function returns a handler that can be used to emit a pickup action. It uses the client to emit a pickup event, which will trigger the server to handle the pickup logic.
Parameters:
Name | Type | Description |
---|---|---|
client | DeliverooClient | The Deliveroo client instance. |
me | Me | The current player instance. |
- Source
Returns:
- A function that emits a pickup action when called.
- Type:
- function
moveAndWait(client, me, dir) → {Promise.<void>}
This function emits a move command to the Deliveroo client and waits for the player's state to update with the new position. It listens for the 'onYou' event to confirm that the move has been processed and the player's position has been updated.
Parameters:
Name | Type | Description |
---|---|---|
client | DeliverooClient | The Deliveroo client instance. |
me | Me | The current player instance. |
dir | string | The direction to move in (e.g., 'UP', 'DOWN', 'LEFT', 'RIGHT'). |
- Source
Returns:
- A promise that resolves when the move is complete.
- Type:
- Promise.<void>
moveToNearestBase(client, me, mapStore) → {Promise.<boolean>}
This function finds the nearest base on the map and uses the A* search algorithm to calculate the path to that base. It then moves the agent in the direction of the first step in the path. If a valid path is found, it moves the agent and returns true. If no base is found or no valid path exists, it logs an error message and returns false.
Parameters:
Name | Type | Description |
---|---|---|
client | DeliverooClient | The Deliveroo client instance. |
me | Me | The current player instance. |
mapStore | MapStore | The MapStore instance containing the current map state. |
- Source
Returns:
- Returns true if successfully moved to a new base, false otherwise.
- Type:
- Promise.<boolean>
(async) planWithDynamicAgents(mapStore, agentStore, me, parcels, serverConfig) → {Promise.<Array>}
This function generates a plan for the current player based on the current map state, visible agents, and parcels. It temporarily removes agents from the map to avoid interference during planning. After generating the plan, it restores the agents back to their original state on the map. If an error occurs during planning, it logs the error and returns an empty plan.
Parameters:
Name | Type | Description |
---|---|---|
mapStore | MapStore | The MapStore instance containing the current map state. |
agentStore | AgentStore | The AgentStore instance containing the current agents state. |
me | Me | The current player instance. |
parcels | ParcelsStore | The list of parcels to be processed. |
serverConfig | ServerConfig | The server configuration instance. |
- Source
Throws:
- - Throws an error if the planning process fails.
- Type
- Error
Returns:
- A promise that resolves to the raw plan generated by the planner.
- Type:
- Promise.<Array>
randomMoveAndBack(client, me, mapStore) → {Promise.<void>}
This function checks the surrounding tiles for walkable paths and randomly selects one to move in. After moving in the random direction, it immediately moves back to the original position. This is useful for testing movement logic or simulating random behavior in the game.
Parameters:
Name | Type | Description |
---|---|---|
client | DeliverooClient | The Deliveroo client instance. |
me | Me | The current player instance. |
mapStore | MapStore | The MapStore instance containing the current map state. |
- Source
Returns:
- A promise that resolves when the random move and return are complete.
- Type:
- Promise.<void>
registerEventHandlers(context)
This function sets up event listeners for various events emitted by the Deliveroo client. It updates the player state, map tiles, configuration, parcels sensing, and agents sensing. It ensures that the application reacts to changes in the game state and updates the relevant models accordingly.
Parameters:
Name | Type | Description | |||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
context | Object | The context containing initialized instances.Properties
|
- Source
Throws:
- - Throws an error if the client fails to register event handlers.
- Type
- Error
(async) runPlanningLoop(context) → {Promise.<void>}
This function runs the main planning loop for the application. It continuously checks the game state, computes a plan using dynamic agents, and executes the plan. If no plan is found, it attempts to move towards the nearest base. It handles errors during planning and execution, ensuring that the application remains responsive and can recover from failures.
Parameters:
Name | Type | Description | |||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
context | Object | The context containing initialized instances.Properties
|
- Source
Throws:
- - Throws an error if the planning or execution fails.
- Type
- Error
Returns:
- A promise that resolves when the planning loop is complete.
- Type:
- Promise.<void>
sanitizePddlName(raw) → {string}
This function is useful for ensuring that names used in PDDL (Planning Domain Definition Language) are valid and follow the naming conventions required by PDDL, such as using lowercase letters, numbers, and underscores, while avoiding spaces and special characters.
Parameters:
Name | Type | Description |
---|---|---|
raw | string | The raw name to sanitize. |
- Source
Returns:
- The sanitized PDDL name.
- Type:
- string
Example
sanitizePddlName(" My Name! "); // returns "my_name"
sanitizePddlName("Invalid@Name#123"); // returns "invalid_name_123"
sanitizePddlName(" __Extra__Underscores__ "); // returns "extra_underscores"
sanitizePddlName("1234"); // returns "1234"
sanitizePddlName("!@#$%^&*()"); // returns ""
smartMove(client, me, target, mapStore) → {Promise.<void>}
This function calculates the path from the current position to the target position using the A* search algorithm. It then iterates through each step in the path, determines the direction to move in, and calls the `moveAndWait` function to move the agent step by step.
Parameters:
Name | Type | Description |
---|---|---|
client | DeliverooClient | The Deliveroo client instance. |
me | Me | The current player instance. |
target | Object | The target position to move towards. |
mapStore | MapStore | The MapStore instance containing the current map state. |
- Source
Returns:
- A promise that resolves when the movement is complete.
- Type:
- Promise.<void>
smartMoveToNearestBase(client, me, mapStore) → {Promise.<void>}
This function finds the nearest base on the map and moves the agent towards it using the `smartMove` function. If a base is found, it will navigate to that base efficiently, taking into account the current map state and obstacles.
Parameters:
Name | Type | Description |
---|---|---|
client | DeliverooClient | The Deliveroo client instance. |
me | Me | The current player instance. |
mapStore | MapStore | The MapStore instance containing the current map state. |
- Source
Returns:
- A promise that resolves when the movement is complete.
- Type:
- Promise.<void>
smartMoveToNearestBaseAndPutDown(client, me, mapStore, parcels) → {Promise.<void>}
This function finds the nearest base on the map and moves the agent towards it using the `smartMove` function. Once the agent reaches the base, it emits a put down command for all carried parcels. This is useful for efficiently delivering parcels to the nearest base, ensuring that the agent can quickly return to the delivery process.
Parameters:
Name | Type | Description |
---|---|---|
client | DeliverooClient | The Deliveroo client instance. |
me | Me | The current player instance. |
mapStore | MapStore | The MapStore instance containing the current map state. |
parcels | ParcelsStore | The list of parcels to be put down. |
- Source
Returns:
- A promise that resolves when the movement and put down action are complete.
- Type:
- Promise.<void>
(async) waitForAppReady(context) → {Promise.<void>}
This function continuously checks if the player ID is set and the map size is greater than zero. It uses a polling mechanism to wait until these conditions are met before resolving the promise. This is useful to ensure that the application has all necessary data before proceeding with the main logic.
Parameters:
Name | Type | Description | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
context | Object | The context containing initialized instances.Properties
|
- Source
Throws:
- - Throws an error if the application is not ready within a reasonable time.
- Type
- Error
Returns:
- A promise that resolves when the application is ready.
- Type:
- Promise.<void>