increase cost if avoided instead of discarding
This commit is contained in:
parent
a621b179d3
commit
3c9c4c403c
@ -159,10 +159,12 @@ Route Pathfinding::perform(Agent& agent, int maxVisited) {
|
|||||||
auto offset = neighbors[i];
|
auto offset = neighbors[i];
|
||||||
auto pos = node.pos;
|
auto pos = node.pos;
|
||||||
|
|
||||||
int surface =
|
float cost = glm::abs(node.pos.y - pos.y) * 10;
|
||||||
getSurfaceAt(agent, pos + glm::ivec3(offset.x, 0, offset.y), 1);
|
int surface = getSurfaceAt(
|
||||||
|
agent, pos + glm::ivec3(offset.x, 0, offset.y), 1, cost
|
||||||
|
);
|
||||||
|
|
||||||
if (surface == -1) {
|
if (surface == NON_PASSABLE) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
pos.y = surface;
|
pos.y = surface;
|
||||||
@ -180,9 +182,8 @@ Route Pathfinding::perform(Agent& agent, int maxVisited) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
int score = glm::abs(node.pos.y - pos.y) * 10;
|
|
||||||
float sum = glm::abs(offset.x) + glm::abs(offset.y);
|
float sum = glm::abs(offset.x) + glm::abs(offset.y);
|
||||||
float gScore = node.gScore + sum + score;
|
float gScore = node.gScore + sum + cost;
|
||||||
const auto& found = state.parents.find(point);
|
const auto& found = state.parents.find(point);
|
||||||
if (found == state.parents.end()) {
|
if (found == state.parents.end()) {
|
||||||
float hScore = heuristic(point, agent.target);
|
float hScore = heuristic(point, agent.target);
|
||||||
@ -231,22 +232,33 @@ int Pathfinding::checkPoint(const Agent& agent, int x, int y, int z) {
|
|||||||
return PASSABLE;
|
return PASSABLE;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Pathfinding::getSurfaceAt(const Agent& agent, const glm::ivec3& pos, int maxDelta) {
|
int Pathfinding::getSurfaceAt(
|
||||||
|
const Agent& agent, const glm::ivec3& pos, int maxDelta, float& cost
|
||||||
|
) {
|
||||||
using namespace blocks_agent;
|
using namespace blocks_agent;
|
||||||
|
|
||||||
int status;
|
int status;
|
||||||
int surface = pos.y;
|
int surface = pos.y;
|
||||||
if (checkPoint(agent, pos.x, surface, pos.z) <= 0) {
|
if ((status = checkPoint(agent, pos.x, surface, pos.z)) == OBSTACLE) {
|
||||||
if (checkPoint(agent, pos.x, surface + 1, pos.z) <= 0)
|
if ((status = checkPoint(agent, pos.x, surface + 1, pos.z)) == OBSTACLE) {
|
||||||
return NON_PASSABLE;
|
return NON_PASSABLE;
|
||||||
else
|
} else if (status == NON_PASSABLE) {
|
||||||
|
++cost;
|
||||||
|
}
|
||||||
return surface + 1;
|
return surface + 1;
|
||||||
} else if ((status = checkPoint(agent, pos.x, surface - 1, pos.z)) <= 0) {
|
} else {
|
||||||
if (status == NON_PASSABLE)
|
if (status == NON_PASSABLE) {
|
||||||
return NON_PASSABLE;
|
++cost;
|
||||||
|
}
|
||||||
|
if ((status = checkPoint(agent, pos.x, surface - 1, pos.z)) == OBSTACLE) {
|
||||||
return surface;
|
return surface;
|
||||||
} else if (checkPoint(agent, pos.x, surface - 2, pos.z) == 0) {
|
} else if (status == NON_PASSABLE) {
|
||||||
|
++cost;
|
||||||
|
}
|
||||||
|
if ((status = checkPoint(agent, pos.x, surface - 2, pos.z)) == OBSTACLE) {
|
||||||
return surface - 1;
|
return surface - 1;
|
||||||
}
|
}
|
||||||
return NON_PASSABLE;
|
return NON_PASSABLE;
|
||||||
|
}
|
||||||
|
return NON_PASSABLE;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -86,7 +86,9 @@ namespace voxels {
|
|||||||
std::unordered_map<int, Agent> agents;
|
std::unordered_map<int, Agent> agents;
|
||||||
int nextAgent = 1;
|
int nextAgent = 1;
|
||||||
|
|
||||||
int getSurfaceAt(const Agent& agent, const glm::ivec3& pos, int maxDelta);
|
int getSurfaceAt(
|
||||||
|
const Agent& agent, const glm::ivec3& pos, int maxDelta, float& cost
|
||||||
|
);
|
||||||
|
|
||||||
int checkPoint(const Agent& agent, int x, int y, int z);
|
int checkPoint(const Agent& agent, int x, int y, int z);
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user