wow-addons/ShipRoutes/tools/generate.py
rope 63eebfc8bd Add Ship Routes
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 15:19:19 +00:00

182 lines
6.8 KiB
Python

"""Builds ShipRoutes/Data.lua from the client's TaxiPathNode.db2 (FileDataID 1000437).
usage: python3 generate.py <TaxiPathNode.db2> <Data.lua>
Transports sail along TaxiPath node lists. The client has no record of which paths are
transports, so the paths and the dock names below are curated by hand. A path whose stops
don't match a known dock fails the run instead of guessing.
"""
import collections, math, os, sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from wdc5 import read, f32
# name, instance (map id), world x, world y
DOCKS = [
("Ratchet", 1, -1006, -3842),
("Booty Bay", 0, -14278, 583),
("Menethil Harbor", 0, -3905, -586),
("Menethil Harbor", 0, -3709, -575),
("Theramore Isle", 1, -4016, -4741),
("Rut'theran Village", 1, 8532, 1024),
("Auberdine", 1, 6594, 760),
("Auberdine", 1, 6406, 823),
("Auberdine", 1, 6548, 942),
("Southshore", 0, -1103, -555),
("Stormwind Harbor", 0, -8654, 1344),
("Feathermoon Stronghold", 1, -4197, 3287),
("The Forgotten Coast", 1, -4348, 2444),
("Steamwheedle Port", 1, -6933, -4951),
("Riverglades", 0, -8232, -5801),
("Grom'gol", 0, -12464, 232),
("Grom'gol", 0, -12407, 212),
("Orgrimmar", 1, 1361, -4631),
("Orgrimmar", 1, 1318, -4658),
("Undercity", 0, 2063, 235),
("Undercity", 0, 2062, 293),
("Dalaran", 0, 545, 424),
("Thunder Bluff", 1, -802, 367),
("Zephras Isle", 2991, 1848, 569),
("Zephras Isle", 2991, 1953, 1019),
]
# path id, noun, kind, faction (None = unknown)
# Path 295 (Menethil - Auberdine direct) is still in the file, but 11167 uses the same
# piers with a Southshore stop, so it is the one that sails in Forever.
ROUTES = [
(241, "Boat", "boat", "Neutral"),
(292, "Boat", "boat", "Alliance"),
(293, "Boat", "boat", "Alliance"),
(303, "Boat", "boat", "Alliance"),
(11167, "Boat", "boat", "Alliance"),
(11616, "Boat", "boat", "Alliance"),
(11391, "Boat", "boat", "Neutral"),
(285, "Zeppelin", "air", "Horde"),
(301, "Zeppelin", "air", "Horde"),
(302, "Zeppelin", "air", "Horde"),
(11398, "Airship", "air", None),
(11457, "Airship", "air", None),
]
TELEPORT = 0x1
def dock_name(m, x, y):
best = min((math.hypot(x - dx, y - dy), n) for n, dm, dx, dy in DOCKS if dm == m) if any(d[1] == m for d in DOCKS) else None
if not best or best[0] > 300:
raise SystemExit("no dock near %d %.0f %.0f" % (m, x, y))
return best[1]
def catmull(points, closed):
"""Subdivide a polyline along a uniform Catmull-Rom spline."""
n = len(points)
if n < 3:
return points
out = []
segs = n if closed else n - 1
for i in range(segs):
p0 = points[(i - 1) % n] if closed or i > 0 else points[i]
p1 = points[i]
p2 = points[(i + 1) % n]
p3 = points[(i + 2) % n] if closed or i + 2 < n else p2
steps = max(1, min(6, int(math.hypot(p2[0] - p1[0], p2[1] - p1[1]) // 60)))
for s in range(steps):
t = s / steps
t2, t3 = t * t, t * t * t
out.append(tuple(
0.5 * (2 * p1[k] + (-p0[k] + p2[k]) * t + (2 * p0[k] - 5 * p1[k] + 4 * p2[k] - p3[k]) * t2
+ (-p0[k] + 3 * p1[k] - 3 * p2[k] + p3[k]) * t3)
for k in (0, 1)))
if not closed:
out.append(points[-1])
return out
def build(path, nodes):
nodes = sorted(nodes) # (index, map, x, y, flags, delay)
n = len(nodes)
brk = [bool(nodes[i][4] & TELEPORT) or nodes[i][1] != nodes[(i + 1) % n][1] for i in range(n)]
closed = not any(brk)
start = 0 if closed else (brk.index(True) + 1) % n
order = nodes[start:] + nodes[:start]
brk = brk[start:] + brk[:start]
# stops in loop order, merging a stop that is split across the loop's seam
stops, stop_at = [], {}
for i, (_, m, x, y, _, delay) in enumerate(order):
if delay:
name = dock_name(m, x, y)
if stops and stops[-1][0] == name and stops[-1][1] == m:
stop_at[i] = len(stops) - 1
continue
stops.append((name, m, x, y))
stop_at[i] = len(stops) - 1
if len(stops) > 2 and stops[0][0] == stops[-1][0]:
stops.pop()
stop_at = {i: (s if s < len(stops) else 0) for i, s in stop_at.items()}
parts, cur = [], []
for i, node in enumerate(order):
cur.append(i)
if brk[i] or i == n - 1:
parts.append(cur)
cur = []
out_parts = []
for p in parts:
if len(p) < 2: # a lone node between two teleports draws nothing
continue
pts = catmull([(order[i][2], order[i][3]) for i in p], closed)
# first stop reached after this part ends, in loop order
nxt = None
for k in list(range(p[-1] + 1, n)) + list(range(0, p[-1] + 1)):
if k in stop_at:
nxt = stop_at[k]
break
out_parts.append({"m": order[p[0]][1], "pts": pts, "next": nxt})
return stops, out_parts, closed
def lua_str(s):
return '"' + s.replace("\\", "\\\\").replace('"', '\\"') + '"'
def main():
src, dst = sys.argv[1], sys.argv[2]
rows, _ = read(src)
paths = collections.defaultdict(list)
for r in rows:
loc, _id, p, idx, m, flags, delay, _a, _d = r["fields"]
paths[p].append((idx, m, f32(loc[0]), f32(loc[1]), flags, delay))
lines = [
"-- Generated by tools/generate.py from TaxiPathNode.db2. Do not edit by hand.",
"-- stops: { name, instance, worldX, worldY }",
"-- parts: sailed in order; a gap between parts is a teleport or a change of map.",
"-- next: index of the first stop reached after the part ends.",
"local _, ns = ...",
"ns.routes = {",
]
for path, noun, kind, faction in ROUTES:
if path not in paths:
raise SystemExit("path %d missing" % path)
stops, parts, closed = build(path, paths[path])
print(path, noun, " -> ".join(s[0] for s in stops), "%d parts" % len(parts), file=sys.stderr)
lines.append(" { path = %d, noun = %s, kind = %s, faction = %s, closed = %s," % (
path, lua_str(noun), lua_str(kind), lua_str(faction) if faction else "nil", "true" if closed else "false"))
lines.append(" stops = {")
for name, m, x, y in stops:
lines.append(" { %s, %d, %.1f, %.1f }," % (lua_str(name), m, x, y))
lines.append(" },")
lines.append(" parts = {")
for part in parts:
coords = ", ".join("%.0f, %.0f" % pt for pt in part["pts"])
lines.append(" { m = %d, next = %s, %s }," % (
part["m"], part["next"] + 1 if part["next"] is not None else "nil", coords))
lines.append(" },")
lines.append(" },")
lines.append("}")
open(dst, "w").write("\n".join(lines) + "\n")
main()