feat: async pathfinding
This commit is contained in:
@@ -274,6 +274,39 @@ void WorldRenderer::renderLines(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void draw_route(
|
||||||
|
LinesRenderer& lines, const voxels::Agent& agent
|
||||||
|
) {
|
||||||
|
const auto& route = agent.route;
|
||||||
|
if (!route.found)
|
||||||
|
return;
|
||||||
|
|
||||||
|
for (int i = 1; i < route.nodes.size(); i++) {
|
||||||
|
const auto& a = route.nodes.at(i - 1);
|
||||||
|
const auto& b = route.nodes.at(i);
|
||||||
|
|
||||||
|
if (i == 1) {
|
||||||
|
lines.pushLine(
|
||||||
|
glm::vec3(a.pos) + glm::vec3(0.5f),
|
||||||
|
glm::vec3(a.pos) + glm::vec3(0.5f, 1.0f, 0.5f),
|
||||||
|
glm::vec4(1, 1, 1, 1)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
lines.pushLine(
|
||||||
|
glm::vec3(a.pos) + glm::vec3(0.5f),
|
||||||
|
glm::vec3(b.pos) + glm::vec3(0.5f),
|
||||||
|
glm::vec4(1, 0, 1, 1)
|
||||||
|
);
|
||||||
|
|
||||||
|
lines.pushLine(
|
||||||
|
glm::vec3(b.pos) + glm::vec3(0.5f),
|
||||||
|
glm::vec3(b.pos) + glm::vec3(0.5f, 1.0f, 0.5f),
|
||||||
|
glm::vec4(1, 1, 1, 1)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void WorldRenderer::renderFrame(
|
void WorldRenderer::renderFrame(
|
||||||
const DrawContext& pctx,
|
const DrawContext& pctx,
|
||||||
Camera& camera,
|
Camera& camera,
|
||||||
@@ -389,40 +422,7 @@ void WorldRenderer::renderFrame(
|
|||||||
// In-world lines
|
// In-world lines
|
||||||
if (debug) {
|
if (debug) {
|
||||||
for (const auto& [_, agent] : level.pathfinding->getAgents()) {
|
for (const auto& [_, agent] : level.pathfinding->getAgents()) {
|
||||||
const auto& route = agent.route;
|
draw_route(*lines, agent);
|
||||||
if (!route.found)
|
|
||||||
continue;
|
|
||||||
for (const auto& blocked : route.visited) {
|
|
||||||
lines->pushLine(
|
|
||||||
glm::vec3(blocked) + glm::vec3(0.5f),
|
|
||||||
glm::vec3(blocked) + glm::vec3(0.5f, 1.0f, 0.5f),
|
|
||||||
glm::vec4(1, 0, 0, 1)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
for (int i = 1; i < route.nodes.size(); i++) {
|
|
||||||
const auto& a = route.nodes.at(i - 1);
|
|
||||||
const auto& b = route.nodes.at(i);
|
|
||||||
|
|
||||||
if (i == 1) {
|
|
||||||
lines->pushLine(
|
|
||||||
glm::vec3(a.pos) + glm::vec3(0.5f),
|
|
||||||
glm::vec3(a.pos) + glm::vec3(0.5f, 1.0f, 0.5f),
|
|
||||||
glm::vec4(1, 1, 1, 1)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
lines->pushLine(
|
|
||||||
glm::vec3(a.pos) + glm::vec3(0.5f),
|
|
||||||
glm::vec3(b.pos) + glm::vec3(0.5f),
|
|
||||||
glm::vec4(1, 0, 1, 1)
|
|
||||||
);
|
|
||||||
|
|
||||||
lines->pushLine(
|
|
||||||
glm::vec3(b.pos) + glm::vec3(0.5f),
|
|
||||||
glm::vec3(b.pos) + glm::vec3(0.5f, 1.0f, 0.5f),
|
|
||||||
glm::vec4(1, 1, 1, 1)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -83,6 +83,9 @@ SettingsHandler::SettingsHandler(EngineSettings& settings) {
|
|||||||
builder.add("language", &settings.ui.language);
|
builder.add("language", &settings.ui.language);
|
||||||
builder.add("world-preview-size", &settings.ui.worldPreviewSize);
|
builder.add("world-preview-size", &settings.ui.worldPreviewSize);
|
||||||
|
|
||||||
|
builder.section("pathfinding");
|
||||||
|
builder.add("steps-per-async-agent", &settings.pathfinding.stepsPerAsyncAgent);
|
||||||
|
|
||||||
builder.section("debug");
|
builder.section("debug");
|
||||||
builder.add("generator-test-mode", &settings.debug.generatorTestMode);
|
builder.add("generator-test-mode", &settings.debug.generatorTestMode);
|
||||||
builder.add("do-write-lights", &settings.debug.doWriteLights);
|
builder.add("do-write-lights", &settings.debug.doWriteLights);
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
#include "objects/Player.hpp"
|
#include "objects/Player.hpp"
|
||||||
#include "physics/Hitbox.hpp"
|
#include "physics/Hitbox.hpp"
|
||||||
#include "voxels/Chunks.hpp"
|
#include "voxels/Chunks.hpp"
|
||||||
|
#include "voxels/Pathfinding.hpp"
|
||||||
#include "scripting/scripting.hpp"
|
#include "scripting/scripting.hpp"
|
||||||
#include "lighting/Lighting.hpp"
|
#include "lighting/Lighting.hpp"
|
||||||
#include "settings.hpp"
|
#include "settings.hpp"
|
||||||
@@ -69,6 +70,9 @@ LevelController::LevelController(
|
|||||||
}
|
}
|
||||||
|
|
||||||
void LevelController::update(float delta, bool pause) {
|
void LevelController::update(float delta, bool pause) {
|
||||||
|
level->pathfinding->performAllAsync(
|
||||||
|
settings.pathfinding.stepsPerAsyncAgent.get()
|
||||||
|
);
|
||||||
for (const auto& [_, player] : *level->players) {
|
for (const auto& [_, player] : *level->players) {
|
||||||
if (player->isSuspended()) {
|
if (player->isSuspended()) {
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
@@ -31,8 +31,10 @@ static int l_make_route(lua::State* L) {
|
|||||||
if (auto agent = get_agent(L)) {
|
if (auto agent = get_agent(L)) {
|
||||||
auto start = lua::tovec3(L, 2);
|
auto start = lua::tovec3(L, 2);
|
||||||
auto target = lua::tovec3(L, 3);
|
auto target = lua::tovec3(L, 3);
|
||||||
auto route = level->pathfinding->perform(*agent, start, target);
|
agent->state = {};
|
||||||
agent->route = route;
|
agent->start = start;
|
||||||
|
agent->target = target;
|
||||||
|
auto route = level->pathfinding->perform(*agent);
|
||||||
if (!route.found) {
|
if (!route.found) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -46,6 +48,37 @@ static int l_make_route(lua::State* L) {
|
|||||||
return 0;
|
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) {
|
static int l_set_max_visited_blocks(lua::State* L) {
|
||||||
if (auto agent = get_agent(L)) {
|
if (auto agent = get_agent(L)) {
|
||||||
agent->maxVisitedBlocks = lua::tointeger(L, 2);
|
agent->maxVisitedBlocks = lua::tointeger(L, 2);
|
||||||
@@ -58,6 +91,8 @@ const luaL_Reg pathfindinglib[] = {
|
|||||||
{"set_enabled", lua::wrap<l_set_enabled>},
|
{"set_enabled", lua::wrap<l_set_enabled>},
|
||||||
{"is_enabled", lua::wrap<l_is_enabled>},
|
{"is_enabled", lua::wrap<l_is_enabled>},
|
||||||
{"make_route", lua::wrap<l_make_route>},
|
{"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>},
|
{"set_max_visited", lua::wrap<l_set_max_visited_blocks>},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -85,6 +85,11 @@ struct GraphicsSettings {
|
|||||||
IntegerSetting denseRenderDistance {56, 0, 10'000};
|
IntegerSetting denseRenderDistance {56, 0, 10'000};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct PathfindingSettings {
|
||||||
|
/// @brief Max visited blocks by an agent per async tick
|
||||||
|
IntegerSetting stepsPerAsyncAgent {256, 1, 2048};
|
||||||
|
};
|
||||||
|
|
||||||
struct DebugSettings {
|
struct DebugSettings {
|
||||||
/// @brief Turns off chunks saving/loading
|
/// @brief Turns off chunks saving/loading
|
||||||
FlagSetting generatorTestMode {false};
|
FlagSetting generatorTestMode {false};
|
||||||
@@ -109,4 +114,5 @@ struct EngineSettings {
|
|||||||
DebugSettings debug;
|
DebugSettings debug;
|
||||||
UiSettings ui;
|
UiSettings ui;
|
||||||
NetworkSettings network;
|
NetworkSettings network;
|
||||||
|
PathfindingSettings pathfinding;
|
||||||
};
|
};
|
||||||
|
|||||||
+57
-52
@@ -1,8 +1,5 @@
|
|||||||
#include "Pathfinding.hpp"
|
#include "Pathfinding.hpp"
|
||||||
|
|
||||||
#include <queue>
|
|
||||||
#include <unordered_map>
|
|
||||||
|
|
||||||
#include "world/Level.hpp"
|
#include "world/Level.hpp"
|
||||||
#include "voxels/GlobalChunks.hpp"
|
#include "voxels/GlobalChunks.hpp"
|
||||||
#include "voxels/Chunk.hpp"
|
#include "voxels/Chunk.hpp"
|
||||||
@@ -13,19 +10,6 @@ inline constexpr float SQRT2 = 1.4142135623730951f; // sqrt(2)
|
|||||||
|
|
||||||
using namespace voxels;
|
using namespace voxels;
|
||||||
|
|
||||||
struct Node {
|
|
||||||
glm::ivec3 pos;
|
|
||||||
glm::ivec3 parent;
|
|
||||||
float gScore;
|
|
||||||
float fScore;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct NodeLess {
|
|
||||||
bool operator()(const Node& l, const Node& r) const {
|
|
||||||
return l.fScore > r.fScore;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
static float heuristic(const glm::ivec3& a, const glm::ivec3& b) {
|
static float heuristic(const glm::ivec3& a, const glm::ivec3& b) {
|
||||||
return glm::distance(glm::vec3(a), glm::vec3(b));
|
return glm::distance(glm::vec3(a), glm::vec3(b));
|
||||||
}
|
}
|
||||||
@@ -79,50 +63,70 @@ int Pathfinding::createAgent() {
|
|||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
Route Pathfinding::perform(
|
void Pathfinding::performAllAsync(int stepsPerAgent) {
|
||||||
const Agent& agent, const glm::ivec3& start, const glm::ivec3& end
|
for (auto& [id, agent] : agents) {
|
||||||
) {
|
if (agent.state.finished) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
perform(agent, stepsPerAgent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Route Pathfinding::perform(Agent& agent, int maxVisited) {
|
||||||
using namespace blocks_agent;
|
using namespace blocks_agent;
|
||||||
|
|
||||||
Route route {};
|
Route route {};
|
||||||
|
|
||||||
std::priority_queue<Node, std::vector<Node>, NodeLess> queue;
|
State state = std::move(agent.state);
|
||||||
queue.push({start, {}, 0, heuristic(start, end)});
|
if (state.queue.empty()) {
|
||||||
|
state.queue.push({agent.start, {}, 0, heuristic(agent.start, agent.target)});
|
||||||
std::unordered_set<glm::ivec3> blocked;
|
}
|
||||||
std::unordered_map<glm::ivec3, Node> parents;
|
|
||||||
|
|
||||||
const auto& chunks = *level.chunks;
|
const auto& chunks = *level.chunks;
|
||||||
int height = std::max(agent.height, 1);
|
int height = std::max(agent.height, 1);
|
||||||
|
|
||||||
glm::ivec3 nearest = start;
|
if (state.nearest == glm::ivec3(0)) {
|
||||||
float minHScore = heuristic(start, end);
|
state.nearest = agent.start;
|
||||||
|
state.minHScore = heuristic(agent.start, agent.target);
|
||||||
|
}
|
||||||
|
int visited = -1;
|
||||||
|
|
||||||
while (!queue.empty()) {
|
while (!state.queue.empty()) {
|
||||||
if (blocked.size() == agent.maxVisitedBlocks) {
|
if (state.blocked.size() == agent.maxVisitedBlocks) {
|
||||||
if (agent.mayBeIncomplete) {
|
if (agent.mayBeIncomplete) {
|
||||||
restore_route(route, nearest, parents);
|
restore_route(route, state.nearest, state.parents);
|
||||||
route.nodes.push_back({start});
|
route.nodes.push_back({agent.start});
|
||||||
route.found = true;
|
route.found = true;
|
||||||
route.visited = std::move(blocked);
|
state.finished = true;
|
||||||
|
agent.state = std::move(state);
|
||||||
|
agent.route = route;
|
||||||
return route;
|
return route;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
auto node = queue.top();
|
visited++;
|
||||||
queue.pop();
|
if (visited == maxVisited) {
|
||||||
|
state.finished = false;
|
||||||
|
agent.state = std::move(state);
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
if (node.pos.x == end.x &&
|
auto node = state.queue.top();
|
||||||
glm::abs((node.pos.y - end.y) / height) == 0 &&
|
state.queue.pop();
|
||||||
node.pos.z == end.z) {
|
|
||||||
restore_route(route, node.pos, parents);
|
if (node.pos.x == agent.target.x &&
|
||||||
route.nodes.push_back({start});
|
glm::abs((node.pos.y - agent.target.y) / height) == 0 &&
|
||||||
|
node.pos.z == agent.target.z) {
|
||||||
|
restore_route(route, node.pos, state.parents);
|
||||||
|
route.nodes.push_back({agent.start});
|
||||||
route.found = true;
|
route.found = true;
|
||||||
route.visited = std::move(blocked);
|
state.finished = true;
|
||||||
|
agent.state = std::move(state);
|
||||||
|
agent.route = route;
|
||||||
return route;
|
return route;
|
||||||
}
|
}
|
||||||
|
|
||||||
blocked.emplace(node.pos);
|
state.blocked.emplace(node.pos);
|
||||||
glm::ivec2 neighbors[8] {
|
glm::ivec2 neighbors[8] {
|
||||||
{0, 1}, {1, 0}, {0, -1}, {-1, 0},
|
{0, 1}, {1, 0}, {0, -1}, {-1, 0},
|
||||||
{-1, -1}, {1, -1}, {1, 1}, {-1, 1},
|
{-1, -1}, {1, -1}, {1, 1}, {-1, 1},
|
||||||
@@ -139,7 +143,7 @@ Route Pathfinding::perform(
|
|||||||
}
|
}
|
||||||
pos.y = surface;
|
pos.y = surface;
|
||||||
auto point = pos + glm::ivec3(offset.x, 0, offset.y);
|
auto point = pos + glm::ivec3(offset.x, 0, offset.y);
|
||||||
if (blocked.find(point) != blocked.end()) {
|
if (state.blocked.find(point) != state.blocked.end()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -153,22 +157,23 @@ Route Pathfinding::perform(
|
|||||||
int score = glm::abs(node.pos.y - pos.y) * 10;
|
int score = glm::abs(node.pos.y - pos.y) * 10;
|
||||||
float sum = glm::abs(offset.x) + glm::abs(offset.y);
|
float sum = glm::abs(offset.x) + glm::abs(offset.y);
|
||||||
float gScore =
|
float gScore =
|
||||||
node.gScore + glm::max(sum, SQRT2) * 0.5f + sum * 0.5f + score;
|
node.gScore + sum + score;
|
||||||
const auto& found = parents.find(point);
|
const auto& found = state.parents.find(point);
|
||||||
if (found == parents.end()) {
|
if (found == state.parents.end()) {
|
||||||
float hScore = heuristic(point, end);
|
float hScore = heuristic(point, agent.target);
|
||||||
if (hScore < minHScore) {
|
if (hScore < state.minHScore) {
|
||||||
minHScore = hScore;
|
state.minHScore = hScore;
|
||||||
nearest = point;
|
state.nearest = point;
|
||||||
}
|
}
|
||||||
float fScore = gScore + hScore;
|
float fScore = gScore * 0.75f + hScore;
|
||||||
Node nNode {point, node.pos, gScore, fScore};
|
Node nNode {point, node.pos, gScore, fScore};
|
||||||
parents[point] = node;
|
state.parents[point] = node;
|
||||||
queue.push(nNode);
|
state.queue.push(nNode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return route;
|
agent.state = std::move(state);
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
Agent* Pathfinding::getAgent(int id) {
|
Agent* Pathfinding::getAgent(int id) {
|
||||||
|
|||||||
+27
-24
@@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <queue>
|
||||||
#include <glm/vec2.hpp>
|
#include <glm/vec2.hpp>
|
||||||
#include <glm/vec3.hpp>
|
#include <glm/vec3.hpp>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
@@ -21,7 +22,28 @@ namespace voxels {
|
|||||||
struct Route {
|
struct Route {
|
||||||
bool found;
|
bool found;
|
||||||
std::vector<RouteNode> nodes;
|
std::vector<RouteNode> nodes;
|
||||||
std::unordered_set<glm::ivec3> visited;
|
};
|
||||||
|
|
||||||
|
struct Node {
|
||||||
|
glm::ivec3 pos;
|
||||||
|
glm::ivec3 parent;
|
||||||
|
float gScore;
|
||||||
|
float fScore;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct NodeLess {
|
||||||
|
bool operator()(const Node& l, const Node& r) const {
|
||||||
|
return l.fScore > r.fScore;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct State {
|
||||||
|
std::priority_queue<Node, std::vector<Node>, NodeLess> queue;
|
||||||
|
std::unordered_set<glm::ivec3> blocked;
|
||||||
|
std::unordered_map<glm::ivec3, Node> parents;
|
||||||
|
glm::ivec3 nearest;
|
||||||
|
float minHScore;
|
||||||
|
bool finished = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Agent {
|
struct Agent {
|
||||||
@@ -32,26 +54,7 @@ namespace voxels {
|
|||||||
glm::ivec3 start;
|
glm::ivec3 start;
|
||||||
glm::ivec3 target;
|
glm::ivec3 target;
|
||||||
Route route;
|
Route route;
|
||||||
};
|
State state {};
|
||||||
|
|
||||||
struct Map {
|
|
||||||
int width;
|
|
||||||
int height;
|
|
||||||
std::unique_ptr<uint8_t[]> map;
|
|
||||||
|
|
||||||
Map(int width, int height)
|
|
||||||
: width(width),
|
|
||||||
height(height),
|
|
||||||
map(std::make_unique<uint8_t[]>(width * height)) {
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t& operator[](int i) {
|
|
||||||
return map[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
const uint8_t& operator[](int i) const {
|
|
||||||
return map[i];
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class Pathfinding {
|
class Pathfinding {
|
||||||
@@ -60,9 +63,9 @@ namespace voxels {
|
|||||||
|
|
||||||
int createAgent();
|
int createAgent();
|
||||||
|
|
||||||
Route perform(
|
void performAllAsync(int stepsPerAgent);
|
||||||
const Agent& agent, const glm::ivec3& start, const glm::ivec3& end
|
|
||||||
);
|
Route perform(Agent& agent, int maxVisited = -1);
|
||||||
|
|
||||||
Agent* getAgent(int id);
|
Agent* getAgent(int id);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user