fix: agent being stuck in the middle of the board, agent configuration

This commit is contained in:
Marcin Czerniak 2021-04-26 19:51:44 +02:00
parent 36513e5869
commit 17d70aebd2
5 changed files with 18 additions and 16 deletions

View File

@ -8,6 +8,4 @@ window.addEventListener('DOMContentLoaded', () => document.fonts.ready.then(() =
const grid = new Grid(gridCanvas); const grid = new Grid(gridCanvas);
const agent = new Agent(agentCanvas); const agent = new Agent(agentCanvas);
const products = new Products(productsCanvas); const products = new Products(productsCanvas);
window.products = products;
})); }));

View File

@ -3,19 +3,14 @@ class Agent extends AgentController {
super(canvas); super(canvas);
const cycle = async () => { const cycle = async () => {
const jobRequest = Products.instance.getJobRequest();
// await this.takeFromStore(Product.RANDOM_FROM_REGISTRY(), Math.floor(Math.random() * 40) + 10);
// const randomPlace = Grid.instance.randomPlace();
// await this.deliver(randomPlace.x, randomPlace.y);
const jobRequest = window.products.getJobRequest();
if (jobRequest) { if (jobRequest) {
console.log(jobRequest);
const requiredAmount = 50 - jobRequest.amount; const requiredAmount = 50 - jobRequest.amount;
await this.takeFromStore(jobRequest.product, requiredAmount); await this.takeFromStore(jobRequest.product, requiredAmount);
await this.deliver(jobRequest.x, jobRequest.y); await this.deliver(jobRequest.x, jobRequest.y);
} }
await waitFor(2000); await waitFor(AgentWaitTime);
cycle(); cycle();
}; };
@ -39,7 +34,8 @@ class Agent extends AgentController {
*/ */
async deliver(x, y) { async deliver(x, y) {
await this.moveTo(x, y); await this.moveTo(x, y);
window.products.exchange(x, y, this.ownedProduct, 50); await waitFor(AgentWaitTime);
Products.instance.exchange(x, y, this.ownedProduct, 50);
this.ownedProductAmount = 0; this.ownedProductAmount = 0;
this.ownedProduct = Product.REGISTRY.empty; this.ownedProduct = Product.REGISTRY.empty;
} }
@ -50,7 +46,7 @@ class Agent extends AgentController {
*/ */
async takeFromStore(product, amount) { async takeFromStore(product, amount) {
await this.moveTo(9, 8); await this.moveTo(9, 8);
await waitFor(1000); await waitFor(AgentWaitTime);
this.ownedProductAmount = amount; this.ownedProductAmount = amount;
this.ownedProduct = product; this.ownedProduct = product;
} }

View File

@ -18,8 +18,8 @@ class AgentController {
// Pozycja i poruszanie się // Pozycja i poruszanie się
this.position = { x: 6, y: 0 }; this.position = { x: 6, y: 0 };
this.rotation = { z: 0 }; this.rotation = { z: 0 };
this.speed = 30; this.speed = 90 - AgentMoveSpeed;
this.rotationSpeed = 2; this.rotationSpeed = 90 / ( 90 - AgentRotationSpeed);
this.update(); this.update();
} }
@ -64,6 +64,8 @@ class AgentController {
} else { } else {
cycle(); cycle();
} }
} else {
resolve();
} }
} }
@ -156,7 +158,7 @@ class AgentController {
this.rotation = { z: clampRotation(oldZRotation + (this.rotationSpeed * sign)) }; this.rotation = { z: clampRotation(oldZRotation + (this.rotationSpeed * sign)) };
if (Math.abs(oldZRotation - newZRotation) < 1) { if (Math.abs(oldZRotation - newZRotation) < 5) {
resolve(); resolve();
this.rotation = { z: clampRotation(newZRotation) } this.rotation = { z: clampRotation(newZRotation) }
} else { } else {

View File

@ -5,3 +5,7 @@ const StorageCenterLocation = { x: 7, y: 8, w: UnitsInGroupCount, h: 1 }
const UnitsCount = ((ColumnsOfGroupsCount * RowsOfGroupsCount) * UnitsInGroupCount const UnitsCount = ((ColumnsOfGroupsCount * RowsOfGroupsCount) * UnitsInGroupCount
- StorageCenterLocation.w * StorageCenterLocation.h) - StorageCenterLocation.w * StorageCenterLocation.h)
const AgentMoveSpeed = 20 /* 1 - slowest, 89 - fastest */
const AgentRotationSpeed = 20 /* 1 - slowest, 89 - fastest */
const AgentWaitTime = 500 /* in miliseconds */

View File

@ -16,7 +16,6 @@ class Pathfinding {
return Pathfinding.graphSearch({ state }, { state: expectedState }) return Pathfinding.graphSearch({ state }, { state: expectedState })
} }
static graphSearch(node, expectedNode, fringe = [], explored = []) { static graphSearch(node, expectedNode, fringe = [], explored = []) {
fringe.push(node); fringe.push(node);
@ -34,13 +33,16 @@ class Pathfinding {
explored.push(elem); explored.push(elem);
for (const successor of this.successor(elem)) { for (const successor of this.successor(elem)) {
if (explored.every(n => !Pathfinding.reached(n.state, successor.state))) { if (!Pathfinding.isNodeInArr(successor, explored) && !Pathfinding.isNodeInArr(successor, fringe)) {
fringe.push(successor); fringe.push(successor);
} }
} }
} }
} }
static isNodeInArr(node, arr) {
return !arr.every(n => !Pathfinding.reached(n.state, node.state))
}
static successor(node) { static successor(node) {
const list = []; const list = [];