replace set_avoided_tags with avoid_tag & add 'cost' argument
This commit is contained in:
parent
3c9c4c403c
commit
c06636053d
@ -55,6 +55,12 @@ pathfinding.pull_route(agent: int) --> table<vec3> or nil
|
|||||||
--- Set the maximum number of visited blocks for the agent. Used to limit the amount of work of the pathfinding algorithm.
|
--- Set the maximum number of visited blocks for the agent. Used to limit the amount of work of the pathfinding algorithm.
|
||||||
pathfinding.set_max_visited(agent: int, max_visited: int)
|
pathfinding.set_max_visited(agent: int, max_visited: int)
|
||||||
|
|
||||||
--- Set a list of tags defining avoided blocks
|
--- Adding an avoided blocks tag
|
||||||
pathfinding.set_avoided_tags(agent: int, tags: table<string>)
|
pathfinding.avoid_tag(
|
||||||
|
agent: int,
|
||||||
|
-- tag for avoided blocks
|
||||||
|
tag: string, [optional],
|
||||||
|
-- cost of crossing a block
|
||||||
|
cost: int = 10
|
||||||
|
)
|
||||||
```
|
```
|
||||||
|
|||||||
@ -55,6 +55,12 @@ pathfinding.pull_route(agent: int) --> table<vec3> или nil
|
|||||||
--- Установка максимального количества посещенных блоков для агента. Используется для ограничения объема работы алгоритма поиска пути.
|
--- Установка максимального количества посещенных блоков для агента. Используется для ограничения объема работы алгоритма поиска пути.
|
||||||
pathfinding.set_max_visited(agent: int, max_visited: int)
|
pathfinding.set_max_visited(agent: int, max_visited: int)
|
||||||
|
|
||||||
--- Установка перечня тегов, определяющих избегаемые блоки
|
--- Добавление тега избегаемых блоков
|
||||||
pathfinding.set_avoided_tags(agent: int, tags: table<string>)
|
pathfinding.avoid_tag(
|
||||||
|
agent: int,
|
||||||
|
-- тег избегаемых блоков
|
||||||
|
tag: string, [опционально],
|
||||||
|
-- стоимость пересечения блока
|
||||||
|
cost: int = 10
|
||||||
|
)
|
||||||
```
|
```
|
||||||
|
|||||||
@ -6,7 +6,7 @@ local tsf = entity.transform
|
|||||||
|
|
||||||
agent = pathfinding.create_agent()
|
agent = pathfinding.create_agent()
|
||||||
pathfinding.set_max_visited(agent, 1e4)
|
pathfinding.set_max_visited(agent, 1e4)
|
||||||
pathfinding.set_avoided_tags(agent, {"core:liquid"})
|
pathfinding.avoid_tag(agent, "core:liquid", 8)
|
||||||
|
|
||||||
function set_target(new_target)
|
function set_target(new_target)
|
||||||
target = new_target
|
target = new_target
|
||||||
|
|||||||
@ -100,22 +100,16 @@ static int l_set_jump_height(lua::State* L) {
|
|||||||
return 0;
|
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 (auto agent = get_agent(L)) {
|
||||||
if (!lua::istable(L, 2)) {
|
int index =
|
||||||
throw std::runtime_error("array of tags expected");
|
content->getTagIndex(std::string(lua::require_lstring(L, 2)));
|
||||||
}
|
if (index != -1) {
|
||||||
agent->avoidTags.clear();
|
int cost = lua::tonumber(L, 3);
|
||||||
int len = lua::objlen(L, 2);
|
if (cost == 0) {
|
||||||
for (int i = 1; i <= len; i++) {
|
cost = 10;
|
||||||
lua::rawgeti(L, i);
|
|
||||||
if (lua::isstring(L, -1)) {
|
|
||||||
int index = content->getTagIndex(lua::tostring(L, -1));
|
|
||||||
if (index != -1) {
|
|
||||||
agent->avoidTags.insert(index);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
lua::pop(L);
|
agent->avoidTags.insert({index, cost});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
@ -131,6 +125,6 @@ const luaL_Reg pathfindinglib[] = {
|
|||||||
{"pull_route", lua::wrap<l_pull_route>},
|
{"pull_route", lua::wrap<l_pull_route>},
|
||||||
{"set_max_visited", lua::wrap<l_set_max_visited_blocks>},
|
{"set_max_visited", lua::wrap<l_set_max_visited_blocks>},
|
||||||
{"set_jump_height", lua::wrap<l_set_jump_height>},
|
{"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}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|||||||
@ -75,7 +75,7 @@ bool Pathfinding::removeAgent(int id) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Pathfinding::performAllAsync(int stepsPerAgent) {
|
void Pathfinding::performAllAsync(int stepsPerAgent) {
|
||||||
for (auto& [id, agent] : agents) {
|
for (auto& [_, agent] : agents) {
|
||||||
if (agent.state.finished) {
|
if (agent.state.finished) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -215,7 +215,7 @@ const std::unordered_map<int, Agent>& Pathfinding::getAgents() const {
|
|||||||
return agents;
|
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);
|
auto vox = blocks_agent::get(chunks, x, y, z);
|
||||||
if (vox == nullptr) {
|
if (vox == nullptr) {
|
||||||
return OBSTACLE;
|
return OBSTACLE;
|
||||||
@ -224,8 +224,9 @@ int Pathfinding::checkPoint(const Agent& agent, int x, int y, int z) {
|
|||||||
if (def.obstacle) {
|
if (def.obstacle) {
|
||||||
return OBSTACLE;
|
return OBSTACLE;
|
||||||
}
|
}
|
||||||
for (int tagIndex : agent.avoidTags) {
|
for (const auto& pair : agent.avoidTags) {
|
||||||
if (def.rt.tags.find(tagIndex) != def.rt.tags.end()) {
|
if (def.rt.tags.find(pair.first) != def.rt.tags.end()) {
|
||||||
|
cost = pair.second;
|
||||||
return NON_PASSABLE;
|
return NON_PASSABLE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -239,23 +240,27 @@ int Pathfinding::getSurfaceAt(
|
|||||||
|
|
||||||
int status;
|
int status;
|
||||||
int surface = pos.y;
|
int surface = pos.y;
|
||||||
if ((status = checkPoint(agent, pos.x, surface, pos.z)) == OBSTACLE) {
|
int ncost = 0;
|
||||||
if ((status = checkPoint(agent, pos.x, surface + 1, pos.z)) == OBSTACLE) {
|
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;
|
return NON_PASSABLE;
|
||||||
} else if (status == NON_PASSABLE) {
|
} else if (status == NON_PASSABLE) {
|
||||||
++cost;
|
cost += 5;
|
||||||
}
|
}
|
||||||
|
cost += ncost;
|
||||||
return surface + 1;
|
return surface + 1;
|
||||||
} else {
|
} else {
|
||||||
if (status == NON_PASSABLE) {
|
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;
|
return surface;
|
||||||
} else if (status == NON_PASSABLE) {
|
} 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 surface - 1;
|
||||||
}
|
}
|
||||||
return NON_PASSABLE;
|
return NON_PASSABLE;
|
||||||
|
|||||||
@ -61,7 +61,7 @@ namespace voxels {
|
|||||||
glm::ivec3 target;
|
glm::ivec3 target;
|
||||||
Route route;
|
Route route;
|
||||||
State state {};
|
State state {};
|
||||||
std::set<int> avoidTags;
|
std::set<std::pair<int, int>> avoidTags;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Pathfinding {
|
class Pathfinding {
|
||||||
@ -90,6 +90,6 @@ namespace voxels {
|
|||||||
const Agent& agent, const glm::ivec3& pos, int maxDelta, float& cost
|
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);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user