fix: Pathfinding cost
This commit is contained in:
parent
6ea38b9584
commit
c31c838e55
@ -34,7 +34,7 @@ class Pathfinding {
|
|||||||
|
|
||||||
for (const successor of this.successor(elem)) {
|
for (const successor of this.successor(elem)) {
|
||||||
if (!Pathfinding.isNodeInArr(successor, explored) && !Pathfinding.isNodeInArr(successor, fringe)) {
|
if (!Pathfinding.isNodeInArr(successor, explored) && !Pathfinding.isNodeInArr(successor, fringe)) {
|
||||||
Pathfinding.insertWithPriority(successor, fringe);
|
Pathfinding.insertWithPriority(successor, fringe, expectedNode);
|
||||||
} else if (Pathfinding.isNodeInArr(successor, fringe)) {
|
} else if (Pathfinding.isNodeInArr(successor, fringe)) {
|
||||||
const index = Pathfinding.findIndefOf(successor, fringe);
|
const index = Pathfinding.findIndefOf(successor, fringe);
|
||||||
fringe.splice(index, 1, successor);
|
fringe.splice(index, 1, successor);
|
||||||
@ -51,10 +51,9 @@ class Pathfinding {
|
|||||||
return arr.findIndex(n => Pathfinding.reached(n.state, node.state));
|
return arr.findIndex(n => Pathfinding.reached(n.state, node.state));
|
||||||
}
|
}
|
||||||
|
|
||||||
static insertWithPriority(node, arr) {
|
static insertWithPriority(node, arr, destination) {
|
||||||
for (const [i, n] of arr.entries()) {
|
for (const [i, n] of arr.entries()) {
|
||||||
if (node.totalCost < n.totalCost) {
|
if (node.totalCost < n.totalCost) {
|
||||||
console.log('Inserted inside');
|
|
||||||
arr.splice(i, 0, node);
|
arr.splice(i, 0, node);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -65,7 +64,7 @@ class Pathfinding {
|
|||||||
static successor(node) {
|
static successor(node) {
|
||||||
const list = [];
|
const list = [];
|
||||||
const grid = Grid.instance.getGrid();
|
const grid = Grid.instance.getGrid();
|
||||||
let actionCost = CostMap.instance.getCostAt(node.state.position.x, node.state.position.y);
|
let actionCost = CostMap.instance.getCostAt(node.state.position.x, node.state.position.y) + 1;
|
||||||
|
|
||||||
list.push({
|
list.push({
|
||||||
state: { ...node.state, rotation: (node.state.rotation + 270) % 360 },
|
state: { ...node.state, rotation: (node.state.rotation + 270) % 360 },
|
||||||
@ -86,12 +85,12 @@ class Pathfinding {
|
|||||||
const prediction = { x: node.state.position.x + direction.x, y: node.state.position.y + direction.y }
|
const prediction = { x: node.state.position.x + direction.x, y: node.state.position.y + direction.y }
|
||||||
|
|
||||||
if (grid[prediction.x] && grid[prediction.x][prediction.y] == GRID_FIELD_TYPE.PATH) {
|
if (grid[prediction.x] && grid[prediction.x][prediction.y] == GRID_FIELD_TYPE.PATH) {
|
||||||
actionCost = CostMap.instance.getCostAt(prediction.x, prediction.y);
|
actionCost = CostMap.instance.getCostAt(prediction.x, prediction.y) + 0.1;
|
||||||
|
|
||||||
list.push({
|
list.push({
|
||||||
state: { ...node.state, position: prediction },
|
state: { ...node.state, position: prediction },
|
||||||
action: PATHFINDING_ACTION.MOVE,
|
action: PATHFINDING_ACTION.MOVE,
|
||||||
actionCost,
|
actionCost: actionCost + 1,
|
||||||
totalCost: Math.round((node.totalCost + actionCost) * 100) / 100 || actionCost,
|
totalCost: Math.round((node.totalCost + actionCost) * 100) / 100 || actionCost,
|
||||||
parent: node
|
parent: node
|
||||||
});
|
});
|
||||||
@ -113,7 +112,7 @@ class Pathfinding {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static getPriority(node, expectedNode) {
|
static getManhattanDistance(node, expectedNode) {
|
||||||
return Math.abs(node.state.position.x - expectedNode.state.position.x) + Math.abs(node.state.position.y - expectedNode.state.position.y);
|
return Math.abs(node.state.position.x - expectedNode.state.position.x) + Math.abs(node.state.position.y - expectedNode.state.position.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,10 +49,10 @@ class AgentController {
|
|||||||
|
|
||||||
if (nearestPoint) {
|
if (nearestPoint) {
|
||||||
if (nearestPoint.action == PATHFINDING_ACTION.MOVE) {
|
if (nearestPoint.action == PATHFINDING_ACTION.MOVE) {
|
||||||
await this.moveInLine(nearestPoint.state.position);
|
await this.moveInLine(nearestPoint.state.position, nearestPoint.actionCost);
|
||||||
}
|
}
|
||||||
if (nearestPoint.action == PATHFINDING_ACTION.ROTATE) {
|
if (nearestPoint.action == PATHFINDING_ACTION.ROTATE) {
|
||||||
await this.rotate({ z: nearestPoint.state.rotation });
|
await this.rotate({ z: nearestPoint.state.rotation }, nearestPoint.actionCost);
|
||||||
}
|
}
|
||||||
|
|
||||||
nearestPoint = this.currentPath[++this.currentPathIndex];
|
nearestPoint = this.currentPath[++this.currentPathIndex];
|
||||||
@ -77,10 +77,12 @@ class AgentController {
|
|||||||
*
|
*
|
||||||
* @param {{ x: number, y: number }} position
|
* @param {{ x: number, y: number }} position
|
||||||
*/
|
*/
|
||||||
moveInLine(position) {
|
moveInLine(position, actionCost) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const cycle = () => {
|
const cycle = () => {
|
||||||
const { speed } = this;
|
let { speed } = this;
|
||||||
|
speed *= (actionCost - 1) * 4 + 1;
|
||||||
|
|
||||||
this.currentAction = this.actions.IN_MOVE;
|
this.currentAction = this.actions.IN_MOVE;
|
||||||
this.position = {
|
this.position = {
|
||||||
x: this.position.x + (Math.sign(position.x - this.position.x)/speed),
|
x: this.position.x + (Math.sign(position.x - this.position.x)/speed),
|
||||||
@ -135,7 +137,7 @@ class AgentController {
|
|||||||
* @param {{ z: number}} rotation
|
* @param {{ z: number}} rotation
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
rotate(rotation) {
|
rotate(rotation, actionCost) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (this.rotation.z == rotation.z) {
|
if (this.rotation.z == rotation.z) {
|
||||||
resolve();
|
resolve();
|
||||||
@ -155,8 +157,10 @@ class AgentController {
|
|||||||
const newZRotation = rotation.z;
|
const newZRotation = rotation.z;
|
||||||
|
|
||||||
const clampRotation = (rot) => (rot + 360) % 360;
|
const clampRotation = (rot) => (rot + 360) % 360;
|
||||||
|
let { rotationSpeed } = this;
|
||||||
|
rotationSpeed *= clamp(2 - actionCost, 0.1, 1);
|
||||||
|
|
||||||
this.rotation = { z: clampRotation(oldZRotation + (this.rotationSpeed * sign)) };
|
this.rotation = { z: clampRotation(oldZRotation + (rotationSpeed * sign)) };
|
||||||
|
|
||||||
if (Math.abs(oldZRotation - newZRotation) < 5) {
|
if (Math.abs(oldZRotation - newZRotation) < 5) {
|
||||||
resolve();
|
resolve();
|
||||||
|
Loading…
Reference in New Issue
Block a user