replace set_avoided_tags with avoid_tag & add 'cost' argument
This commit is contained in:
@@ -100,22 +100,16 @@ static int l_set_jump_height(lua::State* L) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_set_avoided_tags(lua::State* L) {
|
||||
static int l_avoid_tag(lua::State* L) {
|
||||
if (auto agent = get_agent(L)) {
|
||||
if (!lua::istable(L, 2)) {
|
||||
throw std::runtime_error("array of tags expected");
|
||||
}
|
||||
agent->avoidTags.clear();
|
||||
int len = lua::objlen(L, 2);
|
||||
for (int i = 1; i <= len; i++) {
|
||||
lua::rawgeti(L, i);
|
||||
if (lua::isstring(L, -1)) {
|
||||
int index = content->getTagIndex(lua::tostring(L, -1));
|
||||
if (index != -1) {
|
||||
agent->avoidTags.insert(index);
|
||||
}
|
||||
int index =
|
||||
content->getTagIndex(std::string(lua::require_lstring(L, 2)));
|
||||
if (index != -1) {
|
||||
int cost = lua::tonumber(L, 3);
|
||||
if (cost == 0) {
|
||||
cost = 10;
|
||||
}
|
||||
lua::pop(L);
|
||||
agent->avoidTags.insert({index, cost});
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
@@ -131,6 +125,6 @@ const luaL_Reg pathfindinglib[] = {
|
||||
{"pull_route", lua::wrap<l_pull_route>},
|
||||
{"set_max_visited", lua::wrap<l_set_max_visited_blocks>},
|
||||
{"set_jump_height", lua::wrap<l_set_jump_height>},
|
||||
{"set_avoided_tags", lua::wrap<l_set_avoided_tags>},
|
||||
{"avoid_tag", lua::wrap<l_avoid_tag>},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
+16
-11
@@ -75,7 +75,7 @@ bool Pathfinding::removeAgent(int id) {
|
||||
}
|
||||
|
||||
void Pathfinding::performAllAsync(int stepsPerAgent) {
|
||||
for (auto& [id, agent] : agents) {
|
||||
for (auto& [_, agent] : agents) {
|
||||
if (agent.state.finished) {
|
||||
continue;
|
||||
}
|
||||
@@ -215,7 +215,7 @@ const std::unordered_map<int, Agent>& Pathfinding::getAgents() const {
|
||||
return agents;
|
||||
}
|
||||
|
||||
int Pathfinding::checkPoint(const Agent& agent, int x, int y, int z) {
|
||||
int Pathfinding::checkPoint(const Agent& agent, int x, int y, int z, int& cost) {
|
||||
auto vox = blocks_agent::get(chunks, x, y, z);
|
||||
if (vox == nullptr) {
|
||||
return OBSTACLE;
|
||||
@@ -224,8 +224,9 @@ int Pathfinding::checkPoint(const Agent& agent, int x, int y, int z) {
|
||||
if (def.obstacle) {
|
||||
return OBSTACLE;
|
||||
}
|
||||
for (int tagIndex : agent.avoidTags) {
|
||||
if (def.rt.tags.find(tagIndex) != def.rt.tags.end()) {
|
||||
for (const auto& pair : agent.avoidTags) {
|
||||
if (def.rt.tags.find(pair.first) != def.rt.tags.end()) {
|
||||
cost = pair.second;
|
||||
return NON_PASSABLE;
|
||||
}
|
||||
}
|
||||
@@ -239,23 +240,27 @@ int Pathfinding::getSurfaceAt(
|
||||
|
||||
int status;
|
||||
int surface = pos.y;
|
||||
if ((status = checkPoint(agent, pos.x, surface, pos.z)) == OBSTACLE) {
|
||||
if ((status = checkPoint(agent, pos.x, surface + 1, pos.z)) == OBSTACLE) {
|
||||
int ncost = 0;
|
||||
if ((status = checkPoint(agent, pos.x, surface, pos.z, ncost)) == OBSTACLE) {
|
||||
if ((status = checkPoint(agent, pos.x, surface + 1, pos.z, ncost)) == OBSTACLE) {
|
||||
return NON_PASSABLE;
|
||||
} else if (status == NON_PASSABLE) {
|
||||
++cost;
|
||||
cost += 5;
|
||||
}
|
||||
cost += ncost;
|
||||
return surface + 1;
|
||||
} else {
|
||||
if (status == NON_PASSABLE) {
|
||||
++cost;
|
||||
cost += 5;
|
||||
}
|
||||
if ((status = checkPoint(agent, pos.x, surface - 1, pos.z)) == OBSTACLE) {
|
||||
if ((status = checkPoint(agent, pos.x, surface - 1, pos.z, ncost)) == OBSTACLE) {
|
||||
cost += ncost;
|
||||
return surface;
|
||||
} else if (status == NON_PASSABLE) {
|
||||
++cost;
|
||||
cost += 5;
|
||||
}
|
||||
if ((status = checkPoint(agent, pos.x, surface - 2, pos.z)) == OBSTACLE) {
|
||||
if ((status = checkPoint(agent, pos.x, surface - 2, pos.z, ncost)) == OBSTACLE) {
|
||||
cost += ncost;
|
||||
return surface - 1;
|
||||
}
|
||||
return NON_PASSABLE;
|
||||
|
||||
@@ -61,7 +61,7 @@ namespace voxels {
|
||||
glm::ivec3 target;
|
||||
Route route;
|
||||
State state {};
|
||||
std::set<int> avoidTags;
|
||||
std::set<std::pair<int, int>> avoidTags;
|
||||
};
|
||||
|
||||
class Pathfinding {
|
||||
@@ -90,6 +90,6 @@ namespace voxels {
|
||||
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, int& cost);
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user