wow-addons/ShipRoutes/tools/generate.py
rope 216e6f90b8 Ship Routes: solid sea lanes around the continents, labelled edge markers
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 15:33:04 +00:00

236 lines
9.7 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.
Each route is drawn as legs between consecutive stops. A leg the ship sails without a
teleport follows its real path. A leg across a teleport (another continent, or a zeppelin
that vanishes mid-flight) follows hand-placed waypoints on the Azeroth world map instead,
so it goes around the continents rather than straight through them.
"""
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),
]
# Where each continent sits on the Azeroth world map (uiMap 947), from UiMapAssignment.db2:
# instance -> (u min, u max, v min, v max, world x min, x max, y min, y max)
ON_WORLD_MAP = {
0: (0.5505, 0.8966, 0.0994, 0.8691, -16000, 6933, -7467, 8000),
1: (0.0399, 0.4083, 0.0855, 0.9234, -12800, 12267, -9600, 6933),
}
# Sea lanes for legs across a teleport, as points on the world map. They were placed on a
# 1517 x 1019 pixel render of uiMap 947; keys are (from, to) and are reversed as needed.
WAYPOINTS_PX = {
("Booty Bay", "Ratchet"): [(930, 770), (880, 710), (770, 600), (600, 585), (430, 550)],
("Menethil Harbor", "Theramore Isle"): [(960, 450), (700, 520), (430, 600)],
("Southshore", "Auberdine"): [(1010, 385), (930, 405), (700, 300), (440, 165), (300, 135), (205, 180)],
("Auberdine", "Menethil Harbor"): [(205, 180), (300, 135), (440, 165), (700, 300), (960, 430)],
("Stormwind Harbor", "Auberdine"): [(900, 590), (700, 380), (440, 165), (300, 135), (205, 180)],
("Steamwheedle Port", "Riverglades"): [(520, 770), (800, 830), (1050, 860), (1150, 770), (1235, 660)],
}
PX_W, PX_H, PX_X, PX_Y = 1517, 1019, 92, 32 # render size and the crop it was taken from
AIR_BEND = 0.12 # air legs without waypoints bow out by this share of their length
TELEPORT = 0x1
WORLD = -1 # instance marker for a point given in world map coordinates
def dock_name(m, x, y):
near = [(math.hypot(x - dx, y - dy), n) for n, dm, dx, dy in DOCKS if dm == m]
if not near or min(near)[0] > 300:
raise SystemExit("no dock near %d %.0f %.0f" % (m, x, y))
return min(near)[1]
def to_world_map(m, x, y):
if m not in ON_WORLD_MAP:
return None
u0, u1, v0, v1, xmin, xmax, ymin, ymax = ON_WORLD_MAP[m]
return u0 + (ymax - y) / (ymax - ymin) * (u1 - u0), v0 + (xmax - x) / (xmax - xmin) * (v1 - v0)
def catmull(points, steps_for):
"""Subdivide a polyline along a uniform Catmull-Rom spline."""
n = len(points)
if n < 3:
return points
out = []
for i in range(n - 1):
p0, p1, p2 = points[max(i - 1, 0)], points[i], points[i + 1]
p3 = points[min(i + 2, n - 1)]
steps = steps_for(p1, p2)
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)))
out.append(points[-1])
return out
def waypoints(kind, a, b):
"""World map points between stops a and b (name, m, x, y), or [] if none apply."""
if (a[0], b[0]) in WAYPOINTS_PX:
px = WAYPOINTS_PX[(a[0], b[0])]
elif (b[0], a[0]) in WAYPOINTS_PX:
px = list(reversed(WAYPOINTS_PX[(b[0], a[0])]))
else:
pa, pb = to_world_map(*a[1:]), to_world_map(*b[1:])
if kind == "air" and pa and pb:
# a gentle arc: a quadratic curve whose middle sits AIR_BEND of the length off
# the straight line (measured with the map's 1.5 aspect), sampled at a few points
dx, dy = (pb[0] - pa[0]) * 1.5, pb[1] - pa[1]
cx = (pa[0] + pb[0]) / 2 - dy * 2 * AIR_BEND / 1.5
cy = (pa[1] + pb[1]) / 2 + dx * 2 * AIR_BEND
return [((1 - t) ** 2 * pa[0] + 2 * (1 - t) * t * cx + t * t * pb[0],
(1 - t) ** 2 * pa[1] + 2 * (1 - t) * t * cy + t * t * pb[1]) for t in (0.25, 0.5, 0.75)]
if pa and pb:
raise SystemExit("no sea lane for %s - %s" % (a[0], b[0]))
return [] # one end isn't on the world map (Zephras Isle): nothing to draw between them
return [((x + PX_X) / PX_W, (y + PX_Y) / PX_H) for x, y in px]
def build(kind, 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)]
# stops in node order, merging a stop that is split across the loop's seam
stops, at = [], []
for i, (_, m, x, y, _, delay) in enumerate(nodes):
if delay:
name = dock_name(m, x, y)
if stops and stops[-1][0] == name and stops[-1][1] == m:
continue
stops.append((name, m, x, y))
at.append(i)
if len(stops) > 2 and stops[0][0] == stops[-1][0]:
stops.pop()
at.pop()
# a two-stop route is one line; longer loops draw every leg
pairs = [(0, 1)] if len(stops) == 2 else [(i, (i + 1) % len(stops)) for i in range(len(stops))]
legs = []
for i, j in pairs:
span = []
k = at[i]
while True:
span.append(k)
if k == at[j]:
break
k = (k + 1) % n
if not any(brk[k] for k in span[:-1]):
m = nodes[at[i]][1]
pts = catmull([(nodes[k][2], nodes[k][3]) for k in span],
lambda p, q: max(1, min(3, int(math.hypot(q[0] - p[0], q[1] - p[1]) // 80))))
legs.append({"from": i, "to": j, "smooth": False, "pts": [(m, x, y) for x, y in pts]})
else:
a, b = stops[i], stops[j]
mid = [(WORLD, u, v) for u, v in waypoints(kind, a, b)]
legs.append({"from": i, "to": j, "smooth": True, "pts": [a[1:]] + mid + [b[1:]]})
return stops, legs
def lua_str(s):
return '"' + s.replace("\\", "\\\\").replace('"', '\\"') + '"'
def fmt(m, a, b):
if m == WORLD:
return "%d, %.4f, %.4f" % (m, a, b)
return "%d, %.0f, %.0f" % (m, a, b)
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 }",
"-- legs: from / to stop index, then points as (instance, worldX, worldY) triples;",
"-- instance -1 is a point on the Azeroth world map (u, v). smooth legs are drawn as a curve.",
"local _, ns = ...",
"ns.routes = {",
]
for path, noun, kind, faction in ROUTES:
if path not in paths:
raise SystemExit("path %d missing" % path)
stops, legs = build(kind, paths[path])
print(path, noun, " -> ".join(s[0] for s in stops), "%d legs" % len(legs), file=sys.stderr)
lines.append(" { path = %d, noun = %s, kind = %s, faction = %s," % (
path, lua_str(noun), lua_str(kind), lua_str(faction) if faction else "nil"))
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(" legs = {")
for leg in legs:
lines.append(" { from = %d, to = %d, smooth = %s, %s }," % (
leg["from"] + 1, leg["to"] + 1, "true" if leg["smooth"] else "false",
", ".join(fmt(*p) for p in leg["pts"])))
lines.append(" },")
lines.append(" },")
lines.append("}")
open(dst, "w").write("\n".join(lines) + "\n")
main()