From 9bac13fcfd4ae8506bf9b8bcdf5175c55900930d Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Tue, 4 Oct 2022 23:36:22 -0600 Subject: [PATCH] bsputil: add "--findleaf x y z" command --- bsputil/bsputil.cc | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/bsputil/bsputil.cc b/bsputil/bsputil.cc index dbd73606..169afd9c 100644 --- a/bsputil/bsputil.cc +++ b/bsputil/bsputil.cc @@ -456,6 +456,16 @@ static void FindFaces(const mbsp_t *bsp, const qvec3d &pos, const qvec3d &normal } } +static void FindLeaf(const mbsp_t *bsp, const qvec3d &pos) +{ + const mleaf_t *leaf = BSP_FindLeafAtPoint(bsp, &bsp->dmodels[0], pos); + + fmt::print("leaf {}: contents {} ({})\n", + (leaf - bsp->dleafs.data()), + leaf->contents, + contentflags_t{leaf->contents}.to_string(bsp->loadversion->game)); +} + // TODO settings::common_settings bsputil_options; @@ -469,7 +479,7 @@ int main(int argc, char **argv) if (argc == 1) { printf( "usage: bsputil [--replace-entities] [--extract-entities] [--extract-textures] [--convert bsp29|bsp2|bsp2rmq|q2bsp] [--check] [--modelinfo]\n" - "[--check] [--compare otherbsp] [--findfaces x y z nx ny nz] [--settexinfo facenum texinfonum]\n" + "[--check] [--compare otherbsp] [--findfaces x y z nx ny nz] [--findleaf x y z] [--settexinfo facenum texinfonum]\n" "[--decompile] [--decompile-geomonly] [--decompile-hull n] bspfile\n"); exit(1); } @@ -597,7 +607,22 @@ int main(int argc, char **argv) Error("Error reading position/normal\n"); } return 0; - } else if (!strcmp(argv[i], "--settexinfo")) { + } else if (!strcmp(argv[i], "--findleaf")) { + // (i + 1) ... (i + 3) = x y z + // i + 4 = bsp file + + if (i + 4 >= argc) { + Error("--findleaf requires 3 arguments"); + } + + try { + const qvec3d pos{std::stof(argv[i + 1]), std::stof(argv[i + 2]), std::stof(argv[i + 3])}; + FindLeaf(&bsp, pos); + } catch (const std::exception &) { + Error("Error reading position/normal\n"); + } + return 0; + } else if (!strcmp(argv[i], "--settexinfo")) { // (i + 1) facenum // (i + 2) texinfonum