Files

144 lines
6.2 KiB
Python

#!/usr/bin/env python3
# pfQuest SmartArrow: navmesh probe
# Copyright (C) 2026 pfQuest SmartArrow contributors
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version. See LICENSE.md.
"""
Looks straight at the server's movement maps, without the converter, to see
what they say about one spot: which levels there are, and whether a straight
line between two points stays on the navmesh.
python probe_navmesh.py PATH/TO/mmaps --map 1 --from N,W,Z --to N,W [--around N,W]
N and W are the north and west world coordinates that /smartarrow debug
prints ("you: N, W, height Z").
"""
import argparse
import math
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import bake_navmesh as B # noqa: E402
def polys_at(index, cache, m, n, w):
"""Every navmesh polygon under the spot, with its height there."""
found = []
rx, rz = B.to_recast(m, n, w)
for path in index.at_recast(rx, rz):
tile = cache.get(path)
b = B.tile_buckets(tile, m)
lst = b.get((int(math.floor(n / 32.0)), int(math.floor(w / 32.0)))) or []
tr = tile.tris
seen = set()
for t in lst:
o = t * 9
n1, w1 = B.to_world(m, tr[o], tr[o + 2])
n2, w2 = B.to_world(m, tr[o + 3], tr[o + 5])
n3, w3 = B.to_world(m, tr[o + 6], tr[o + 8])
l = B.point_in_tri(n, w, n1, w1, n2, w2, n3, w3)
if l:
h = l[0] * tr[o + 1] + l[1] * tr[o + 4] + l[2] * tr[o + 7]
key = (tile.uid, tile.tpoly[t])
if key not in seen:
seen.add(key)
found.append((h, tile, tile.tpoly[t], tile.tarea[t]))
found.sort(key=lambda x: x[0])
return found
def main():
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument('mmaps')
ap.add_argument('--map', type=int, default=1)
ap.add_argument('--from', dest='frm', required=True, help='N,W,Z')
ap.add_argument('--to', required=True, help='N,W')
ap.add_argument('--around', default=None, help='N,W: list the levels in a cross round this spot')
args = ap.parse_args()
here = os.path.dirname(os.path.abspath(__file__))
found = B.find_tiles(args.mmaps)
tiles = found.get(args.map)
if not tiles:
raise SystemExit('no tiles for map %d' % args.map)
index = B.Index(tiles)
cache = B.TileCache(400)
points = B.walknet_points(os.path.dirname(here))
m, _ = B.detect_mapping(index, cache, points.get(args.map, []), print)
B.MAPPING[0] = m
n0, w0, z0 = [float(x) for x in args.frm.split(',')]
n1, w1 = [float(x) for x in args.to.split(',')[:2]]
if args.around:
cn, cw = [float(x) for x in args.around.split(',')[:2]]
print('\nLevels round %.1f, %.1f (height: area id; 11 ground, 10 steep, 9 water):' % (cn, cw))
for dn, dw in [(0, 0)] + [(d * s, 0) for d in (10, 20, 30, 40, 50) for s in (1, -1)] \
+ [(0, d * s) for d in (10, 20, 30, 40, 50) for s in (1, -1)]:
ps = polys_at(index, cache, m, cn + dn, cw + dw)
print(' %+4d N %+4d W: %s' % (dn, -dw, ', '.join('%.1f:%d' % (h, a) for h, _, _, a in ps) or 'nothing'))
start = polys_at(index, cache, m, n0, w0)
print('\nUnder you (%.1f, %.1f): %s' % (n0, w0, ', '.join('%.1f' % h for h, _, _, _ in start) or 'nothing'))
if not start:
return
h, tile, pi, area = min(start, key=lambda x: abs(x[0] - z0))
print('Starting on the level at %.1f (polygon %d of tile %s)' % (h, pi, os.path.basename(tile.path)))
# the same walk the converter does, told step by step, across tiles
dn, dw = n1 - n0, w1 - w0
length = math.hypot(dn, dw)
t = 0.0
for step in range(400):
wv = tile.world_verts(m)
vs = tile.pverts[pi]
nv = len(vs)
best_t, best_e = None, None
for e in range(nv):
an, aw = wv[vs[e]]
bn, bw = wv[vs[(e + 1) % nv]]
en, ew = bn - an, bw - aw
den = dn * ew - dw * en
if abs(den) < 1e-12:
continue
tt = ((an - n0) * ew - (aw - w0) * en) / den
uu = ((an - n0) * dw - (aw - w0) * dn) / den
if tt > t + 1e-7 and -1e-6 <= uu <= 1 + 1e-6:
if best_t is None or tt < best_t:
best_t, best_e = tt, e
where = lambda tt: '%.0f yd along' % (tt * length)
if best_t is None or best_t >= 1.0:
print(' reached the end on polygon %d: the navmesh lets you walk this line' % pi)
return
nei = tile.pneis[pi][best_e]
pn, pw = n0 + dn * best_t, w0 + dw * best_t
hs = [h for h, _, _, _ in polys_at(index, cache, m, pn + dn / length * 0.3, pw + dw / length * 0.3)]
if nei == 0:
print(' %s: polygon %d ends with nothing beyond it (a wall or a drop): the line stops here' % (where(best_t), pi))
print(' levels just past that edge: %s' % (', '.join('%.1f' % x for x in hs) or 'nothing'))
return
if nei & B.DT_EXT_LINK:
# into the next tile: the polygon there, just past the edge, nearest in height
ahead = polys_at(index, cache, m, pn + dn / length * 0.3, pw + dw / length * 0.3)
cur = [h for h, tl, p, _ in polys_at(index, cache, m, pn - dn / length * 0.3, pw - dw / length * 0.3) if tl is tile and p == pi]
ch = cur[0] if cur else h
nxt = [x for x in ahead if x[1] is not tile]
if not nxt:
print(' %s: tile edge with nothing beyond it' % where(best_t))
return
h, tile, pi, area = min(nxt, key=lambda x: abs(x[0] - ch))
print(' %s: into tile %s, polygon %d at %.1f' % (where(best_t), os.path.basename(tile.path), pi, h))
else:
pi = nei - 1
print(' %s: into polygon %d, levels here %s' % (where(best_t), pi, ', '.join('%.1f' % x for x in hs)))
t = best_t
print(' (stopped after 400 polygons)')
if __name__ == '__main__':
main()