mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-06 03:21:49 +00:00
feat: async pathfinding
This commit is contained in:
parent
be3fb8346f
commit
a78931205f
7 changed files with 168 additions and 112 deletions
|
|
@ -31,8 +31,10 @@ static int l_make_route(lua::State* L) {
|
|||
if (auto agent = get_agent(L)) {
|
||||
auto start = lua::tovec3(L, 2);
|
||||
auto target = lua::tovec3(L, 3);
|
||||
auto route = level->pathfinding->perform(*agent, start, target);
|
||||
agent->route = route;
|
||||
agent->state = {};
|
||||
agent->start = start;
|
||||
agent->target = target;
|
||||
auto route = level->pathfinding->perform(*agent);
|
||||
if (!route.found) {
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -46,6 +48,37 @@ static int l_make_route(lua::State* L) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int l_make_route_async(lua::State* L) {
|
||||
if (auto agent = get_agent(L)) {
|
||||
auto start = lua::tovec3(L, 2);
|
||||
auto target = lua::tovec3(L, 3);
|
||||
agent->state = {};
|
||||
agent->start = start;
|
||||
agent->target = target;
|
||||
level->pathfinding->perform(*agent, 0);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_pull_route(lua::State* L) {
|
||||
if (auto agent = get_agent(L)) {
|
||||
auto& route = agent->route;
|
||||
if (!agent->state.finished) {
|
||||
return 0;
|
||||
}
|
||||
if (!route.found) {
|
||||
return lua::createtable(L, 0, 0);
|
||||
}
|
||||
lua::createtable(L, route.nodes.size(), 0);
|
||||
for (int i = 0; i < route.nodes.size(); i++) {
|
||||
lua::pushvec3(L, route.nodes[i].pos);
|
||||
lua::rawseti(L, i + 1);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_set_max_visited_blocks(lua::State* L) {
|
||||
if (auto agent = get_agent(L)) {
|
||||
agent->maxVisitedBlocks = lua::tointeger(L, 2);
|
||||
|
|
@ -58,6 +91,8 @@ const luaL_Reg pathfindinglib[] = {
|
|||
{"set_enabled", lua::wrap<l_set_enabled>},
|
||||
{"is_enabled", lua::wrap<l_is_enabled>},
|
||||
{"make_route", lua::wrap<l_make_route>},
|
||||
{"make_route_async", lua::wrap<l_make_route_async>},
|
||||
{"pull_route", lua::wrap<l_pull_route>},
|
||||
{"set_max_visited", lua::wrap<l_set_max_visited_blocks>},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue