Files

210 lines
8.9 KiB
Python

#!/usr/bin/env python3
# pfQuest SmartArrow
# 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. It is distributed WITHOUT ANY
# WARRANTY; see LICENSE.md.
"""Show the path network (the rectangles) round one spot, and plan a walk on it.
python3 probe_rects.py 10421.5 865.1 --z 1319
python3 probe_rects.py 10421.5 865.1 --z 1319 --to 10445 867 [--high]
N and W are the "you:" numbers of /smartarrow debug, --z your height there
(it picks the level you stand on). Prints a map of the 4 yard cells, north
up and west left; each level of a cell shows the letter of its rectangle and
its height. Marks: s steep (left out), u ground under water (left out), x not usable,
~ water, @ the spot. Then each rectangle shown (size, heights, what it
borders), the borders that join ground at very different heights, and with
--to the rectangles and the corners of the walk the arrow plans.
"""
import argparse
import math
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import bake_waypoints as bw
import bake_rects as br
CELL = bw.CELL
NAMES = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
SIDE = ('north', 'west', 'south', 'east')
def level_at(net, i, j, z):
"""the cell's level whose height is nearest z (the first usable one without z)"""
best, bd = None, None
for s in range(net.g.count(i, j)):
c = (i, j, s)
k = net.label.get(c)
if k is None:
continue
h = net.g.surface(i, j, s)[1]
d = 0 if z is None or h is None else abs(h - z)
if bd is None or d < bd:
best, bd = k, d
return best
def main():
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
addon = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
ap.add_argument('n', type=float)
ap.add_argument('w', type=float)
ap.add_argument('--z', type=float, help='your height there')
ap.add_argument('--to', nargs=2, type=float, metavar=('N', 'W'), help='plan a walk to this spot')
ap.add_argument('--toz', type=float, help='the height of the goal (picks its level)')
ap.add_argument('--high', action='store_true', help='to the highest level at the goal')
ap.add_argument('--below', type=float, default=8, help='with --z: show levels down to this far below it (default 8)')
ap.add_argument('--above', type=float, default=35, help='with --z: and up to this far above it (default 35)')
ap.add_argument('--map', type=int, default=1)
ap.add_argument('--size', type=int, default=9, help='cells shown each side of the spot')
ap.add_argument('--navmesh', default=os.path.join(os.path.dirname(addon), 'pfQuest-SmartArrow-Navmesh'))
args = ap.parse_args()
version, maps = bw.load_navmesh(args.navmesh)
zones = bw.load_zones(addon)
net = br.build(bw.Grid(maps[args.map]), bw.load_solids(addon, zones).get(args.map, []),
bw.load_roads(addon, zones).get(args.map, []), lambda s: None, br.load_cuts(addon).get(args.map))
g, b = net.g, net.b
ci, cj = int(math.floor(args.n / CELL)), int(math.floor(args.w / CELL))
size = args.size
def visible(h):
return args.z is None or h is None or args.z - args.below <= h <= args.z + args.above
# the levels shown: all of them without --z, else those near your height
# (the ground at the foot of a tree has the tree's ramp hundreds of yards up above it)
cells = {}
order = []
for i in range(ci + size, ci - size - 1, -1):
for j in range(cj + size, cj - size - 1, -1):
parts = []
for s in range(g.count(i, j)):
cls, h = g.surface(i, j, s)
if not visible(h):
continue
k = net.label.get((i, j, s))
parts.append((s, k, cls, h))
if k is not None and k not in order:
order.append(k)
cells[(i, j)] = parts
long = len(order) > len(NAMES)
shown = {}
for x, k in enumerate(order):
shown[k] = (NAMES[x // len(NAMES) % len(NAMES)] + NAMES[x % len(NAMES)]) if long else NAMES[x]
rows, width = [], 0
for i in range(ci + size, ci - size - 1, -1):
row = []
for j in range(cj + size, cj - size - 1, -1):
parts = []
for s, k, cls, h in cells[(i, j)]:
hh = '----' if h is None else '%d' % int(round(h))
if k is not None:
tag = shown[k]
elif cls == 3:
tag = 's'
elif net.sunk((i, j, s)) is not None:
tag = 'u'
elif not b.ok(i, j, s):
tag = 'x'
else:
tag = '?'
parts.append(tag + ' ' + hh + ('~' if cls == 2 else ''))
text = ' / '.join(parts) or '.'
if (i, j) == (ci, cj):
text = '@' + text
width = max(width, len(text))
row.append(text)
rows.append(((i + 0.5) * CELL, row))
print('North up, west left. Columns from W %.0f (left) to W %.0f (right), 4 yards each.' % (
(cj + size + 0.5) * CELL, (cj - size + 0.5) * CELL))
if args.z is not None:
print('Levels from %.0f to %.0f only (--below, --above change that). Each cell: rectangle, height.' % (
args.z - args.below, args.z + args.above))
for n, row in rows:
print('N %6.0f ' % n + ' | '.join(t.ljust(width) for t in row))
def nm(k):
return shown.get(k, '#%d' % k)
def zr(r):
if r.get('zlo') is None:
return 'no height'
return '%.0f-%.0f%s' % (r['zlo'], r['zhi'], ' est' if r['zkind'] == 2 else '')
def rect_text(k):
r = net.rects[k]
z = zr(r)
return '%s: N %.0f..%.0f W %.0f..%.0f, %d x %d yd, heights %s%s' % (
nm(k), (r['i0']) * CELL, (r['i0'] + r['h']) * CELL,
(r['j0']) * CELL, (r['j0'] + r['w']) * CELL, r['h'] * 4, r['w'] * 4, z,
', water' if r['water'] else '')
print()
print('Rectangles shown, and the borders each has:')
for k in order:
print(' ' + rect_text(k))
for o, side, pos, start, length in net.rects[k]['nb']:
ro = net.rects[o]
print(' %s side, %d cells wide, to %s (heights %s)' % (
SIDE[side], length, nm(o), zr(ro)))
print()
print('Borders between rectangles shown whose heights differ by more than 3 yd (a ledge, a root, a ramp):')
count = 0
for k in order:
r = net.rects[k]
for o, side, pos, start, length in r['nb']:
ro = net.rects[o]
if o in shown and k < o and r.get('zlo') is not None and ro.get('zlo') is not None:
gap = max(r['zlo'] - ro['zhi'], ro['zlo'] - r['zhi'])
if gap > 3:
count += 1
print(' %s (%s) <-> %s (%s), %s side of %s, %d cells' % (
nm(k), zr(r), nm(o), zr(ro), SIDE[side], nm(k), length))
if not count:
print(' none')
if args.to:
a = (args.n, args.w)
t = (args.to[0], args.to[1])
ks = level_at(net, ci, cj, args.z)
ti, tj = int(math.floor(t[0] / CELL)), int(math.floor(t[1] / CELL))
goals = [net.label[(ti, tj, s)] for s in range(g.count(ti, tj)) if (ti, tj, s) in net.label]
print()
if ks is None or not goals:
print('walk: no rectangle %s' % ('where you stand' if ks is None else 'at the goal'))
return
if args.toz is not None:
near = level_at(net, ti, tj, args.toz)
goals = [near] if near is not None else goals
if args.high:
goals = [max(goals, key=lambda k: net.rects[k]['zhi'] if net.rects[k].get('zhi') is not None else -1e9)]
print('walk from rectangle %s to the goal rectangle(s) %s' % (
nm(ks), ', '.join(nm(k) for k in goals)))
pts = net.plan(a, [ks], t, goals)
if pts is None:
print(' no way')
return
chain = sorted(net.order.items(), key=lambda e: e[1])
print(' rectangles in order:')
for k, _ in chain:
print(' ' + rect_text(k))
print(' corners:')
inchain = set(net.order)
for p in pts:
pi, pj = int(math.floor(p[0] / CELL)), int(math.floor(p[1] / CELL))
hs = sorted(set(int(round(g.surface(pi, pj, s)[1])) for s in range(g.count(pi, pj))
if net.label.get((pi, pj, s)) in inchain and g.surface(pi, pj, s)[1] is not None))
print(' N %.1f W %.1f (%+.0f N %+.0f W) ground of the walk here: %s' % (
p[0], p[1], p[0] - a[0], p[1] - a[1], ', '.join(map(str, hs)) or 'no height'))
if __name__ == '__main__':
main()