Skip to content

Commit cef251b

Browse files
committed
nav mesh path finding WIP
Using dijkstra instead of AStar because some paths are extremely more expensive in order to prevent routes that require flying (not possible for players without gauss, stacking, etc.). So far it's working pretty well. It prefers to walk around the entire map and up some stairs instead of getting a bunch of players to stack in order to reach a vent in the ceiling or something. Next major problem is having entities connect leaves together which otherwise wouldn't be, or would be too expensive due to vertical movement (teleports, ladders, water, elevators, trigger_push). Paths also sometimes take odd turns because of going to the center of a leaf before the face of the next leaf.
1 parent 23231e3 commit cef251b

12 files changed

+548
-201
lines changed

src/editor/BspRenderer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -978,8 +978,8 @@ void BspRenderer::generateLeafNavMeshBuffer() {
978978
vector<cVert> wireframeVerts;
979979
vector<FaceMath> faceMaths;
980980

981-
for (int lf = 0; lf < navMesh->numLeaves; lf++) {
982-
LeafMesh& mesh = navMesh->leaves[lf];
981+
for (int lf = 0; lf < navMesh->nodes.size(); lf++) {
982+
LeafNode& mesh = navMesh->nodes[lf];
983983

984984
color = hullColors[hull];
985985
static int r = 0;

src/editor/Gui.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "tinyfiledialogs.h"
1616
#include <algorithm>
1717
#include "BspMerger.h"
18+
#include "LeafNavMesh.h"
1819

1920
// embedded binary data
2021
#include "fonts/robotomono.h"
@@ -1951,8 +1952,15 @@ void Gui::drawDebugWidget() {
19511952
if (i == 0) {
19521953
ImGui::Text("Leaf: %d", leafIdx);
19531954
}
1954-
else {
1955-
ImGui::Text("Pseudo ID: %d", map->get_leaf(localCamera, i));
1955+
else if (i == 3 && g_app->debugLeafNavMesh) {
1956+
int leafIdx = map->get_leaf(localCamera, 3);
1957+
int leafNavIdx = -1;
1958+
1959+
if (leafIdx >= 0 && leafIdx < MAX_MAP_CLIPNODE_LEAVES) {
1960+
leafNavIdx = g_app->debugLeafNavMesh->leafMap[leafIdx];
1961+
}
1962+
1963+
ImGui::Text("Nav ID: %d", leafNavIdx);
19561964
}
19571965
ImGui::Text("Parent Node: %d (child %d)",
19581966
nodeBranch.size() ? nodeBranch[nodeBranch.size() - 1] : headNode,

src/editor/Renderer.cpp

Lines changed: 90 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -456,95 +456,127 @@ void Renderer::renderLoop() {
456456

457457
if (debugLeafNavMesh) {
458458
glLineWidth(1);
459+
glDisable(GL_DEPTH_TEST);
459460

460461
Bsp* map = mapRenderers[0]->map;
461462
int leafIdx = map->get_leaf(cameraOrigin, 3);
462-
int leafNavIdx = MAX_NAV_LEAVES;
463+
int leafNavIdx = -1;
463464

464465
if (leafIdx >= 0 && leafIdx < MAX_MAP_CLIPNODE_LEAVES) {
465466
leafNavIdx = debugLeafNavMesh->leafMap[leafIdx];
466467
}
467468

468-
if (leafNavIdx < MAX_NAV_LEAVES) {
469+
if (leafNavIdx >= 0 && leafNavIdx < debugLeafNavMesh->nodes.size()) {
469470

470471
if (pickInfo.valid && pickInfo.ent && pickInfo.entIdx != 0) {
471472
glDisable(GL_DEPTH_TEST);
472473

473474
int endNode = debugLeafNavMesh->getNodeIdx(map, pickInfo.ent);
474-
vector<int> route = debugLeafNavMesh->AStarRoute(map, leafNavIdx, endNode);
475+
//vector<int> route = debugLeafNavMesh->AStarRoute(leafNavIdx, endNode);
476+
vector<int> route = debugLeafNavMesh->dijkstraRoute(leafNavIdx, endNode);
475477

476478
if (route.size()) {
477-
LeafMesh& firstNode = debugLeafNavMesh->leaves[route[route.size() - 1]];
478-
479-
vec3 lastPos = firstNode.center;
480-
drawBox(firstNode.center, 2, COLOR4(0, 255, 255, 255));
481-
482-
for (int i = route.size() - 2; i >= 0; i--) {
483-
LeafNavNode& node = debugLeafNavMesh->nodes[route[i]];
484-
LeafMesh& mesh = debugLeafNavMesh->leaves[route[i]];
485-
486-
vec3 nodeCenter = mesh.center;
487-
488-
for (int k = 0; k < MAX_NAV_LEAF_LINKS; k++) {
489-
LeafNavLink& link = node.links[k];
490-
491-
if (link.node == route[i + 1]) {
492-
vec3 linkPoint = link.linkArea.center;
493-
494-
drawLine(lastPos, linkPoint, COLOR4(0, 255, 255, 255));
495-
drawLine(linkPoint, mesh.center, COLOR4(0, 255, 255, 255));
479+
LeafNode* lastNode = &debugLeafNavMesh->nodes[route[0]];
480+
481+
vec3 lastPos = lastNode->bottom;
482+
drawBox(lastNode->bottom, 2, COLOR4(0, 255, 255, 255));
483+
484+
for (int i = 1; i < route.size(); i++) {
485+
LeafNode& node = debugLeafNavMesh->nodes[route[i]];
486+
487+
vec3 nodeCenter = node.bottom;
488+
489+
for (int k = 0; k < lastNode->links.size(); k++) {
490+
LeafLink& link = lastNode->links[k];
491+
492+
if (link.node == route[i]) {
493+
vec3 linkPoint = link.bottom;
494+
495+
if (link.baseCost > 16000) {
496+
drawLine(lastPos, linkPoint, COLOR4(255, 0, 0, 255));
497+
drawLine(linkPoint, node.bottom, COLOR4(255, 0, 0, 255));
498+
}
499+
else if (link.baseCost > 0) {
500+
drawLine(lastPos, linkPoint, COLOR4(255, 255, 0, 255));
501+
drawLine(linkPoint, node.bottom, COLOR4(255, 255, 0, 255));
502+
}
503+
else {
504+
drawLine(lastPos, linkPoint, COLOR4(0, 255, 255, 255));
505+
drawLine(linkPoint, node.bottom, COLOR4(0, 255, 255, 255));
506+
}
496507
drawBox(nodeCenter, 2, COLOR4(0, 255, 255, 255));
497508
lastPos = nodeCenter;
498509
break;
499510
}
500511
}
512+
513+
lastNode = &node;
501514
}
502515

503516
drawLine(lastPos, pickInfo.ent->getHullOrigin(map), COLOR4(0, 255, 255, 255));
504517
}
505518
}
506519
else {
507-
LeafNavNode& node = debugLeafNavMesh->nodes[leafNavIdx];
508-
LeafMesh& leaf = debugLeafNavMesh->leaves[leafNavIdx];
520+
LeafNode& node = debugLeafNavMesh->nodes[leafNavIdx];
509521

510-
drawBox(leaf.center, 2, COLOR4(0, 255, 0, 255));
522+
drawBox(node.bottom, 2, COLOR4(0, 255, 0, 255));
511523

512524
std::string linkStr;
513525

514-
for (int i = 0; i < MAX_NAV_LEAF_LINKS; i++) {
515-
LeafNavLink& link = node.links[i];
526+
for (int i = 0; i < node.links.size(); i++) {
527+
LeafLink& link = node.links[i];
516528
if (link.node == -1) {
517529
break;
518530
}
519-
LeafMesh& linkLeaf = debugLeafNavMesh->leaves[link.node];
531+
LeafNode& linkLeaf = debugLeafNavMesh->nodes[link.node];
520532
Polygon3D& linkArea = link.linkArea;
521533

522-
drawLine(leaf.center, linkArea.center, COLOR4(0, 255, 255, 255));
523-
drawLine(linkArea.center, linkLeaf.center, COLOR4(0, 255, 255, 255));
534+
if (link.baseCost > 16000) {
535+
drawLine(node.bottom, link.bottom, COLOR4(255, 0, 0, 255));
536+
drawLine(link.bottom, linkLeaf.bottom, COLOR4(255, 0, 0, 255));
537+
}
538+
else if (link.baseCost > 0) {
539+
drawLine(node.bottom, link.bottom, COLOR4(255, 255, 0, 255));
540+
drawLine(link.bottom, linkLeaf.bottom, COLOR4(255, 255, 0, 255));
541+
}
542+
else {
543+
drawLine(node.bottom, link.bottom, COLOR4(0, 255, 255, 255));
544+
drawLine(link.bottom, linkLeaf.bottom, COLOR4(0, 255, 255, 255));
545+
}
524546

525547
for (int k = 0; k < linkArea.verts.size(); k++) {
526-
drawBox(linkArea.verts[k], 1, COLOR4(255, 255, 0, 255));
548+
//drawBox(linkArea.verts[k], 1, COLOR4(255, 255, 0, 255));
527549
}
528-
drawBox(linkArea.center, 1, COLOR4(0, 255, 0, 255));
529-
drawBox(linkLeaf.center, 2, COLOR4(0, 255, 255, 255));
550+
drawBox(link.bottom, 1, COLOR4(0, 255, 0, 255));
551+
drawBox(linkLeaf.bottom, 2, COLOR4(0, 255, 255, 255));
530552
linkStr += to_string(link.node) + " (" + to_string(linkArea.verts.size()) + "v), ";
531553
}
532554

533555
//logf("Leaf node idx: %d, links: %s\n", leafNavIdx, linkStr.c_str());
534556
}
535557

536558
}
537-
538-
glDisable(GL_DEPTH_TEST);
539559
/*
560+
Polygon3D linkPoly;
561+
LeafNode& node = debugLeafNavMesh->nodes[1441];
562+
for (int i = 0; i < MAX_NAV_LEAF_LINKS; i++) {
563+
LeafLink& link = node.links[i];
564+
565+
if (link.node == 1442) {
566+
linkPoly = link.linkArea;
567+
break;
568+
}
569+
}
570+
571+
drawPolygon3D(linkPoly, COLOR4(255, 255, 255, 255));
572+
540573
colorShader->pushMatrix(MAT_PROJECTION);
541574
colorShader->pushMatrix(MAT_VIEW);
542575
projection.ortho(0, windowWidth, windowHeight, 0, -1.0f, 1.0f);
543576
view.loadIdentity();
544577
colorShader->updateMatrixes();
545578
546-
Line2D edge(vec2(1000, 400), vec2(1400, 630));
547-
drawLine2D(edge.start, edge.end, COLOR4(255, 0, 0, 255));
579+
drawPolygon2D(linkPoly, vec2(800, 100), vec2(500, 500), COLOR4(255, 0, 0, 255));
548580
549581
colorShader->popMatrix(MAT_PROJECTION);
550582
colorShader->popMatrix(MAT_VIEW);
@@ -1799,20 +1831,38 @@ void Renderer::drawBox(vec3 mins, vec3 maxs, COLOR4 color) {
17991831
buffer.draw(GL_TRIANGLES);
18001832
}
18011833

1834+
void Renderer::drawPolygon3D(Polygon3D& poly, COLOR4 color) {
1835+
static cVert verts[64];
1836+
1837+
for (int i = 0; i < poly.verts.size() && i < 64; i++) {
1838+
vec3 pos = poly.verts[i];
1839+
verts[i].x = pos.x;
1840+
verts[i].y = pos.z;
1841+
verts[i].z = -pos.y;
1842+
verts[i].c = color;
1843+
}
1844+
1845+
VertexBuffer buffer(colorShader, COLOR_4B | POS_3F, verts, poly.verts.size());
1846+
buffer.draw(GL_TRIANGLE_FAN);
1847+
}
1848+
18021849
float Renderer::drawPolygon2D(Polygon3D poly, vec2 pos, vec2 maxSz, COLOR4 color) {
18031850
vec2 sz = poly.localMaxs - poly.localMins;
18041851
float scale = min(maxSz.y / sz.y, maxSz.x / sz.x);
18051852

1806-
vec2 offset = poly.localMins * -scale;
1853+
vec2 offset = poly.localMins * -scale + pos;
18071854

18081855
for (int i = 0; i < poly.verts.size(); i++) {
18091856
vec2 v1 = poly.localVerts[i];
1810-
vec2 v2 = poly.localVerts[(i + 1) % debugPoly.verts.size()];
1857+
vec2 v2 = poly.localVerts[(i + 1) % poly.verts.size()];
18111858
drawLine2D(offset + v1*scale, offset + v2 * scale, color);
1859+
if (i == 0) {
1860+
drawLine2D(offset + v1 * scale, offset + (v1 + (v2-v1)*0.5f) * scale, COLOR4(0,255,0,255));
1861+
}
18121862
}
18131863

18141864
{
1815-
vec2 cam = debugPoly.project(cameraOrigin);
1865+
vec2 cam = poly.project(cameraOrigin);
18161866
drawBox2D(offset + cam*scale, 16, poly.isInside(cam) ? COLOR4(0, 255, 0, 255) : COLOR4(255, 32, 0, 255));
18171867
}
18181868

src/editor/Renderer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ class Renderer {
250250
void drawLine2D(vec2 start, vec2 end, COLOR4 color);
251251
void drawBox(vec3 center, float width, COLOR4 color);
252252
void drawBox(vec3 mins, vec3 maxs, COLOR4 color);
253+
void drawPolygon3D(Polygon3D& poly, COLOR4 color);
253254
float drawPolygon2D(Polygon3D poly, vec2 pos, vec2 maxSz, COLOR4 color); // returns render scale
254255
void drawBox2D(vec2 center, float width, COLOR4 color);
255256
void drawPlane(BSPPLANE& plane, COLOR4 color);

0 commit comments

Comments
 (0)