add pathfinding.set_avoided_tags
This commit is contained in:
parent
5204cfedf5
commit
5e3373313b
@ -54,4 +54,7 @@ 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.
|
||||
pathfinding.set_max_visited(agent: int, max_visited: int)
|
||||
|
||||
--- Set a list of tags defining avoided blocks
|
||||
pathfinding.set_avoided_tags(agent: int, tags: table<string>)
|
||||
```
|
||||
|
||||
@ -54,4 +54,7 @@ pathfinding.pull_route(agent: int) --> table<vec3> или nil
|
||||
|
||||
--- Установка максимального количества посещенных блоков для агента. Используется для ограничения объема работы алгоритма поиска пути.
|
||||
pathfinding.set_max_visited(agent: int, max_visited: int)
|
||||
|
||||
--- Установка перечня тегов, определяющих избегаемые блоки
|
||||
pathfinding.set_avoided_tags(agent: int, tags: table<string>)
|
||||
```
|
||||
|
||||
@ -8,5 +8,5 @@
|
||||
"selectable": false,
|
||||
"replaceable": true,
|
||||
"translucent": true,
|
||||
"tags": ["base:liquid"]
|
||||
"tags": ["core:liquid"]
|
||||
}
|
||||
|
||||
@ -6,6 +6,7 @@ local tsf = entity.transform
|
||||
|
||||
agent = pathfinding.create_agent()
|
||||
pathfinding.set_max_visited(agent, 1e4)
|
||||
pathfinding.set_avoided_tags(agent, {"core:liquid"})
|
||||
|
||||
function set_target(new_target)
|
||||
target = new_target
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
#include "api_lua.hpp"
|
||||
|
||||
#include "content/Content.hpp"
|
||||
#include "voxels/Pathfinding.hpp"
|
||||
#include "world/Level.hpp"
|
||||
|
||||
@ -99,6 +100,27 @@ static int l_set_jump_height(lua::State* L) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_set_avoided_tags(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);
|
||||
}
|
||||
}
|
||||
lua::pop(L);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
const luaL_Reg pathfindinglib[] = {
|
||||
{"create_agent", lua::wrap<l_create_agent>},
|
||||
{"remove_agent", lua::wrap<l_remove_agent>},
|
||||
@ -109,5 +131,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>},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
@ -160,7 +160,7 @@ Route Pathfinding::perform(Agent& agent, int maxVisited) {
|
||||
auto pos = node.pos;
|
||||
|
||||
int surface =
|
||||
getSurfaceAt(pos + glm::ivec3(offset.x, 0, offset.y), 1);
|
||||
getSurfaceAt(agent, pos + glm::ivec3(offset.x, 0, offset.y), 1);
|
||||
|
||||
if (surface == -1) {
|
||||
continue;
|
||||
@ -214,7 +214,7 @@ const std::unordered_map<int, Agent>& Pathfinding::getAgents() const {
|
||||
return agents;
|
||||
}
|
||||
|
||||
int Pathfinding::checkPoint(int x, int y, int z) {
|
||||
int Pathfinding::checkPoint(const Agent& agent, int x, int y, int z) {
|
||||
auto vox = blocks_agent::get(chunks, x, y, z);
|
||||
if (vox == nullptr) {
|
||||
return OBSTACLE;
|
||||
@ -223,27 +223,29 @@ int Pathfinding::checkPoint(int x, int y, int z) {
|
||||
if (def.obstacle) {
|
||||
return OBSTACLE;
|
||||
}
|
||||
if (def.translucent) {
|
||||
return NON_PASSABLE;
|
||||
for (int tagIndex : agent.avoidTags) {
|
||||
if (def.rt.tags.find(tagIndex) != def.rt.tags.end()) {
|
||||
return NON_PASSABLE;
|
||||
}
|
||||
}
|
||||
return PASSABLE;
|
||||
}
|
||||
|
||||
int Pathfinding::getSurfaceAt(const glm::ivec3& pos, int maxDelta) {
|
||||
int Pathfinding::getSurfaceAt(const Agent& agent, const glm::ivec3& pos, int maxDelta) {
|
||||
using namespace blocks_agent;
|
||||
|
||||
int status;
|
||||
int surface = pos.y;
|
||||
if (checkPoint(pos.x, surface, pos.z) <= 0) {
|
||||
if (checkPoint(pos.x, surface + 1, pos.z) <= 0)
|
||||
if (checkPoint(agent, pos.x, surface, pos.z) <= 0) {
|
||||
if (checkPoint(agent, pos.x, surface + 1, pos.z) <= 0)
|
||||
return NON_PASSABLE;
|
||||
else
|
||||
return surface + 1;
|
||||
} else if ((status = checkPoint(pos.x, surface - 1, pos.z)) <= 0) {
|
||||
} else if ((status = checkPoint(agent, pos.x, surface - 1, pos.z)) <= 0) {
|
||||
if (status == NON_PASSABLE)
|
||||
return NON_PASSABLE;
|
||||
return surface;
|
||||
} else if (checkPoint(pos.x, surface - 2, pos.z) == 0) {
|
||||
} else if (checkPoint(agent, pos.x, surface - 2, pos.z) == 0) {
|
||||
return surface - 1;
|
||||
}
|
||||
return NON_PASSABLE;
|
||||
|
||||
@ -6,6 +6,7 @@
|
||||
#include <glm/vec3.hpp>
|
||||
#include <memory>
|
||||
#include <queue>
|
||||
#include <set>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
@ -60,6 +61,7 @@ namespace voxels {
|
||||
glm::ivec3 target;
|
||||
Route route;
|
||||
State state {};
|
||||
std::set<int> avoidTags;
|
||||
};
|
||||
|
||||
class Pathfinding {
|
||||
@ -84,8 +86,8 @@ namespace voxels {
|
||||
std::unordered_map<int, Agent> agents;
|
||||
int nextAgent = 1;
|
||||
|
||||
int getSurfaceAt(const glm::ivec3& pos, int maxDelta);
|
||||
int getSurfaceAt(const Agent& agent, const glm::ivec3& pos, int maxDelta);
|
||||
|
||||
int checkPoint(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