feat: async pathfinding

This commit is contained in:
MihailRis
2025-08-03 01:41:18 +03:00
parent be3fb8346f
commit a78931205f
7 changed files with 168 additions and 112 deletions
+4
View File
@@ -11,6 +11,7 @@
#include "objects/Player.hpp"
#include "physics/Hitbox.hpp"
#include "voxels/Chunks.hpp"
#include "voxels/Pathfinding.hpp"
#include "scripting/scripting.hpp"
#include "lighting/Lighting.hpp"
#include "settings.hpp"
@@ -69,6 +70,9 @@ LevelController::LevelController(
}
void LevelController::update(float delta, bool pause) {
level->pathfinding->performAllAsync(
settings.pathfinding.stepsPerAsyncAgent.get()
);
for (const auto& [_, player] : *level->players) {
if (player->isSuspended()) {
continue;
@@ -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}
};