Skip to content

The first exam for code agents in the physical world: frozen robot tasks from many benchmark suites, each answered with a program and verified in a separate sandbox. Runs on Harbor.

78.8 KB
Raw Blame
"""Freeski big air, straight air (FIS International Competition Rules Book VI, edition June 2026, 3102.4.1 big air site,
3402 judging, 3610 start; Park & Pipe Judges Handbook 2025): one jump on a level-A big air for a Unitree G1 on
freestyle skis. The robot starts itself from a level start platform, rides the in-run and the kicker, and passes only
if the jump's measured rotation is a straight air and it lands cleanly on its skis and rides away. The course, the start, the
ski-snow model and the trick's measurement come from the Embodied Olympics freeski sport (github.com/CHIGUI0/roboolympics
PR #259, branch sport-freeski, commit d566cffb4, MIT; the shared block below, copied unchanged into every freeski
family, says which parts are verbatim)."""
import math
import xml.etree.ElementTree as ET

import numpy as np

import mujoco

from rfo.task import TaskBase

# ==================================================================================== BEGIN ski-snow block v3
# The equipment and the snow (the same block in alpine_slalom, giant_slalom, snowboard and the freeski
# families). Ported, with its authors'
# permission, from the ski-snow model of the Embodied Olympics freeski sport (github.com/CHIGUI0/roboolympics PR #259,
# branch sport-freeski, commit d566cffb4, sports/freeski/skisnow.py, MIT): frictionless condim-1 contacts of the planks'
# edge spheres with the snow, and, every 1 ms physics step, the tangential forces of the snow on every contact in the
# plank's own frame, plus the air's forces on every link (lift included). It follows that file's final version: 9 spheres
# per edge, a ski of three bodies (its forebody and afterbody on hinges with the ski's bending stiffness), hard well-
# damped snow (timeconst 5 ms, damping ratio 1.5), the forces' slope against the slip capped at K_V (the explicit
# step's stability), and solid planks (boxes over the running surface, shovel and tail, in stiff contact with every
# collision hull of the robot, the core's waist and shoulder shells, and the other plank). Changes in the port: the
# forces are written to d.xfrc_applied (at the bodies' centres of mass, torque included) instead of mj_applyFT; a plank
# is a free body held to the boot by a weld (the binding: rigid, as a real binding is until it releases) because the
# core mounts the robot after the family's build(); the snow is a profile of thin boxes (straight pieces and circular
# arcs in 25 cm steps), smooth enough that no snow pad (freeski's mocap boxes laid under each sphere) is needed; the
# solids collide by contact bits instead of explicit pairs (the robot's geoms do not exist yet at build()); the boot is
# mass on the plank, its forward-flex spring is not modelled; the air's drag is recomputed every physics step.
#
# Why a model of its own: MuJoCo's contact friction is isotropic in the tangent plane, a ski is not: it glides along
# its length and holds across it when it is edged. Per contact of a plank segment with the snow (Fn its normal force):
#   along   -mu_k(v) Fn sat(v_long): kinetic ski-snow friction, mu_k(v) after Wolfsperger, Meyer & Gilgien 2021
#           (Front. Mech. Eng. 7:728722): 0.043 at its optimum (6.5-11.5 m/s), up to +0.02 towards a standstill,
#           +0.003 per m/s above 11.5 m/s
#   across  -C sat(v_lat), C = mu_k Fn + min(Fn tan(phi), S_f e l): the snow carries the edged plank by the volume it
#           displaces (Mossner et al. 2013, J. Glaciol. 59(218):1170; Federolf's race-piste hardness H_V 0.21 N/mm^3 and
#           shear strength S_f 0.52 N/mm^2), so an edge at angle phi sinks e = sqrt(2 Fn tan(phi) / (H_V l)) and the snow
#           can hold its platform only up to its shear strength over the groove; a flat plank skids on its base
#   sidecut the "along" direction follows the arc of radius R_sc cos(phi) the edged plank is bent onto (Howe 2001;
#           Lieu & Mote 1985): an edged plank carves a turn of that radius, a flat one runs straight
# sat(v) = v / sqrt(v^2 + V0^2), V0 = max(0.03 m/s, force / K_V): each force opposes its own segment's sliding and never
# does positive work (an edged plank cannot climb, a sliding one cannot push itself). The snow's normal reaction is
# MuJoCo's soft contact. Air (lift included): every link of the robot and of the planks feels the quasi-steady
# normal-force aerodynamics of its hull's faces: along each of its own axes k, -0.5 rho C_N A_k |v| v_k (Hoerner's
# cross-flow principle; A_k the convex hull of its visual shapes projected along its axis k; C_N 1.17, a flat plate's),
# times the overlap factor (the robot's true silhouette over the links' summed areas, measured once standing). A flat
# inclined link (a ski, a trunk laid over the skis) gets lift across the flow as well as drag along it, a compact one
# drag: the posture decides both. Check: a V-style elite jumper made of plates (body 1.75 x 0.40 m,
# skis 2.60 x 0.11 m, a 35 degree V) gets L/q 0.63 m^2 and D/q 0.55 m^2 at 35 degrees of attack, inside what ski-jumping aerodynamics
# reports for elite flight (Schmolzer & Muller 2005, J. Biomech. 38:1055; Virmavirta et al. 2005, J. Biomech.
# 38:2157: lift areas about 0.6-0.8 m^2, drag areas about 0.5 m^2 at 30-40 degrees). The venue's air density is a
# mountain venue's, 1.052 kg/m^3 (ISA, about 2000 m at -10 C). MuJoCo's own fluid model is off in these worlds (it
# would count the air twice).
SNOW_BIT, PLANK_BIT, SOLID_BIT = 1, 2, 16    # the snow accepts the edge spheres (2), the feet (1), the hulls (4)
SNOW_CONAFF = 1 | 2 | 4 | 8
SOLID_CONAFF = 4 | 16                        # a plank's solid: the robot's hulls (4) and the other plank's solid (16);
#                                              the core's shells (SHELL_BIT) see it through its contype (world.py)
SNOW_RGBA = (0.94, 0.95, 0.98, 1)            # groomed snow (drawn flat white: looks.py leaves white surfaces alone)
SEG = 9                                      # contact spheres per edge along the running surface
SPHERE_R = 0.012
BIND_HALF = 0.18                             # the binding's half length: the rigid middle between the flex hinges (m)
K_V = 3000.0                                 # N s/m: the largest slope of a ski-snow force against the slip
PLANK_SOLREF = "0.005 1.5"                   # hard prepared snow: about 1 mm under a standing robot; well damped
PLANK_SOLIMP = "0.95 0.99 0.002"
SOLID_SOLREF = "0.004 1"                     # a plank against the robot's body and the other plank: stiff (freeski E11)
SOLID_SOLIMP = "0.99 0.999 0.0005"
BIND_SOLREF = "0.002 1"                      # the binding (a weld): as stiff as the 1 ms step allows
BIND_SOLIMP = "0.99 0.999 0.001"            # (0.95 0.99 let a thrashing leg stretch it 2 cm in a stress test)
AIR_RHO = 1.052
C_N = 1.17                                   # a flat plate's normal-force coefficient across the flow (Hoerner 1965,
#                                              Fluid-Dynamic Drag, ch. 3: 1.17 for a square plate normal to the flow)


class Plank:
    """A ski or a snowboard: metres and kilograms. `mount`: the binding centre's offset from the middle of the running
    surface (negative: behind); widths at the tip, the waist and the tail; `radius` the sidecut radius. For a
    snowboard `stance` is the distance between the two bindings' centres (0 for a ski). EI_fore / EI_aft: the bending
    stiffness (N m^2) of the forebody and the afterbody; None: a rigid plank."""

    def __init__(self, model, length, tip_w, waist_w, tail_w, radius, mass, mount, binding_mass, stand_h,
                 shovel=0.16, tail=0.10, tip_rise=0.06, tail_rise=0.025, thick=0.016, boot_mass=0.0, stance=0.0,
                 EI_fore=None, EI_aft=None):
        self.model, self.length, self.tip_w, self.waist_w, self.tail_w = model, length, tip_w, waist_w, tail_w
        self.radius, self.mass, self.mount, self.binding_mass, self.stand_h = radius, mass, mount, binding_mass, stand_h
        self.shovel, self.tail, self.tip_rise, self.tail_rise, self.thick = shovel, tail, tip_rise, tail_rise, thick
        self.boot_mass, self.stance = boot_mass, stance
        self.EI_fore, self.EI_aft = EI_fore, EI_aft

    @property
    def run_len(self):
        return self.length - self.shovel - self.tail

    @property
    def flex(self):
        return self.EI_fore is not None and self.EI_aft is not None

    def half_width(self, s):
        """Half the width at s along the running surface (s = 0 its middle, + forward): the sidecut's parabola."""
        h = 0.5 * self.run_len
        w = self.tip_w if s >= 0 else self.tail_w
        return 0.5 * self.waist_w + 0.5 * (w - self.waist_w) * min((s / h) ** 2, 1.0)

    def tip_x(self):
        """The front end of the plank (the shovel's tip) along it from the middle of the running surface."""
        return 0.5 * self.run_len + self.shovel

    def sphere_s(self):
        """(edge, name, s along the plank from its middle, z of the sphere's bottom, half width) of every contact
        sphere: SEG along each edge, one at the shovel and one at the tail."""
        H, L = 0.5 * self.run_len, self.run_len
        out = []
        for e in ("l", "r"):
            for i in range(SEG):
                s = -H + L * i / (SEG - 1)
                out.append((e, f"{e}{i}", s, 0.0, self.half_width(s)))
            out.append((e, f"{e}tipc", H + 0.55 * self.shovel, 0.55 * self.tip_rise, 0.42 * self.tip_w))
            out.append((e, f"{e}tailc", -H - 0.55 * self.tail, 0.55 * self.tail_rise, 0.42 * self.tail_w))
            # the shovel's and the tail's ends (without them a ski pitched into the snow sank its tip, a solid
            # that does not touch the snow, up to 8 cm: the stress test)
            out.append((e, f"{e}tipe", H + self.shovel - SPHERE_R, self.tip_rise - SPHERE_R, 0.42 * self.tip_w))
            out.append((e, f"{e}taile", -H - self.tail + SPHERE_R, self.tail_rise - SPHERE_R, 0.42 * self.tail_w))
        return out


def _f6(v):
    return " ".join(f"{float(x):.6g}" for x in v)


def _box_inertia(m, a, b, c):
    return m * (b * b + c * c) / 12, m * (a * a + c * c) / 12, m * (a * a + b * b) / 12


def add_plank(sc, name, pk: Plank, rgba=(0.85, 0.12, 0.10, 1), top_rgba=(0.95, 0.95, 0.95, 1), n_bind=1):
    """A free plank `name` (its frame: the middle of the running surface, x forward along it, z up from its base) with
    its binding(s) and boot(s) as mass, looks, two rows of edge spheres (the snow contacts) and raised spheres at the
    shovel and the tail, and solid boxes (`<name>_plate...`) for what the plank hits besides the snow. A flexing plank
    (pk.flex) is three bodies: `name` (the middle under the binding, rigid), `<name>_fore` and `<name>_aft` on hinges
    across the plank at the binding's ends, each with the torsional spring k = 3 EI / l of the ski's bending stiffness
    (a cantilever of length l with the same tip deflection) and a little damping (damping ratio about 0.1). Returns
    the parts' body names."""
    b = sc.body(name, (0.0, 0.0, -50.0), free=True)
    L, H, T = pk.run_len, 0.5 * pk.run_len, pk.thick
    mb = (pk.binding_mass + pk.boot_mass) * n_bind
    tot = pk.length
    if pk.flex:
        xf, xb = pk.mount + BIND_HALF, pk.mount - BIND_HALF          # the hinges along the plank from its middle
        segs = (("", xb, xf), ("_fore", xf, H + pk.shovel), ("_aft", -H - pk.tail, xb))
    else:
        xf, xb = H + pk.shovel, -H - pk.tail
        segs = (("", xb, xf),)
    parts, off = {"": b}, {"": 0.0}
    for nm, x0, x1 in segs[1:]:
        hinge = xf if nm == "_fore" else xb
        bb = ET.SubElement(b, "body", name=f"{name}{nm}", pos=_f6((hinge, 0.0, 0.0)))
        ln = abs(x1 - hinge) - (pk.shovel if nm == "_fore" else pk.tail)
        EI = pk.EI_fore if nm == "_fore" else pk.EI_aft
        k = 3.0 * EI / max(ln, 0.05)
        seg_m = pk.mass * (x1 - x0) / tot
        Iy = seg_m * (x1 - x0) ** 2 / 3.0
        ET.SubElement(bb, "joint", name=f"{name}{nm}_flex", type="hinge", axis="0 1 0", stiffness=f"{k:.5g}",
                      damping=f"{0.2 * math.sqrt(k * Iy):.4g}", range="-0.35 0.35", armature="0.0005")
        parts[nm], off[nm] = bb, hinge
    for nm, x0, x1 in segs:
        seg_m = pk.mass * (x1 - x0) / tot
        c = 0.5 * (x0 + x1) - off[nm]
        if nm == "":                               # the binding(s) and the boot(s) ride on the middle
            m_tot = seg_m + mb
            Ixx, Iyy, Izz = _box_inertia(seg_m, x1 - x0, pk.waist_w, T)
            dx = pk.mount - c
            Ixx += mb * (0.07 ** 2 + 0.06 ** 2) / 12
            Iyy += mb * ((0.3 ** 2 + 0.06 ** 2) / 12 + dx ** 2)
            Izz += mb * ((0.3 ** 2 + 0.07 ** 2) / 12 + dx ** 2)
            ET.SubElement(b, "inertial", pos=_f6(((seg_m * c + mb * pk.mount) / m_tot, 0, T + 0.02)),
                          mass=f"{m_tot:.5g}", diaginertia=_f6((Ixx, Iyy, Izz)))
        else:
            ET.SubElement(parts[nm], "inertial", pos=_f6((c, 0, 0.5 * T)), mass=f"{seg_m:.5g}",
                          diaginertia=_f6(_box_inertia(seg_m, x1 - x0, pk.waist_w, T)))

    def part_of(s):
        return "" if xb <= s <= xf else ("_fore" if s > xf else "_aft")

    n = 12
    for i in range(n):                         # the running surface with its sidecut (looks only)
        s0, s1 = -H + L * i / n, -H + L * (i + 1) / n
        sm = 0.5 * (s0 + s1)
        p = part_of(sm)
        hw = pk.half_width(sm)
        sc.geom(parts[p], f"{name}_v{i}", type="box", pos=(sm - off[p], 0, 0.5 * T), size=(0.5 * (s1 - s0) + 2e-4, hw,
                0.5 * T), rgba=rgba, group=1, mass=0, contype=0, conaffinity=0)
        sc.geom(parts[p], f"{name}_t{i}", type="box", pos=(sm - off[p], 0, T + 0.0006), size=(0.5 * (s1 - s0) + 2e-4,
                0.45 * pk.waist_w, 0.0007), rgba=top_rgba, group=1, mass=0, contype=0, conaffinity=0)
    solid = dict(contype=SOLID_BIT, conaffinity=SOLID_CONAFF, condim=3, priority=1, friction=(0.5, 0.005, 0.0001),
                 solref=SOLID_SOLREF, solimp=SOLID_SOLIMP, group=3, mass=0, rgba=(0.9, 0.3, 0.3, 0.3))
    for nm, sgn, ln, rise, wd in (("tip", 1, pk.shovel, pk.tip_rise, pk.tip_w), ("tail", -1, pk.tail, pk.tail_rise,
                                                                                   pk.tail_w)):
        ang = math.atan2(rise, ln)
        p = part_of(sgn * (H + 0.5 * ln))
        kw = dict(type="box", pos=(sgn * (H + 0.5 * ln) - off[p], 0, 0.5 * T + 0.5 * rise), euler=(0, -sgn * ang, 0),
                  size=(0.5 * math.hypot(ln, rise), 0.40 * wd, 0.5 * T))     # (inside the end spheres' cover)
        sc.geom(parts[p], f"{name}_{nm}", rgba=rgba, group=1, mass=0, contype=0, conaffinity=0, **kw)
        sc.geom(parts[p], f"{name}_plate_{nm}", **kw, **solid)        # the upturned shovel / tail: solid too
    for nm, x0, x1 in segs:                    # the solids over the running surface, one per part
        a, c_ = max(x0, -H), min(x1, H)
        hw = min(pk.half_width(a), pk.half_width(c_), pk.half_width(0.5 * (a + c_)))
        sc.geom(parts[nm], f"{name}_plate{nm}", type="box", pos=(0.5 * (a + c_) - off[nm], 0, 0.5 * T),
                size=(0.5 * (c_ - a), hw, 0.5 * T), **solid)
    offs = [0.0] if n_bind == 1 else [-0.5 * pk.stance, 0.5 * pk.stance]
    for k, o in enumerate(offs):               # binding(s): looks
        x0 = pk.mount + o
        sc.geom(b, f"{name}_bind{k}", type="box", pos=(x0, 0, T + 0.5 * pk.stand_h),
                size=(0.15, 0.03, 0.5 * pk.stand_h - 0.002) if n_bind == 1 else (0.04, 0.13, 0.5 * pk.stand_h - 0.002),
                rgba=(0.18, 0.18, 0.2, 1), group=1, mass=0, contype=0, conaffinity=0)
    r = SPHERE_R
    for e, nm, s_, z, hw in pk.sphere_s():
        sgn = 1 if e == "l" else -1
        p = part_of(s_)
        sc.geom(parts[p], f"{name}_e{nm}", type="sphere", size=(r,), pos=(s_ - off[p], sgn * (hw - r), z + r),
                contype=PLANK_BIT, conaffinity=0, condim=1, priority=2, group=4, mass=0, solref=PLANK_SOLREF,
                solimp=PLANK_SOLIMP, rgba=(0.3, 0.3, 0.9, 0.5))
    return [f"{name}{nm}" for nm, _, _ in segs]


def bind(sc, plank, foot, relpos, relquat=(1, 0, 0, 0), name=None):
    """The binding: a weld of the plank to the foot body; relpos / relquat: the plank's frame in the foot's frame."""
    ET.SubElement(sc.root.find("equality"), "weld", name=name or f"binding_{plank}_{foot}", body1=foot, body2=plank,
                  relpose=_f6(tuple(relpos) + tuple(relquat)), solref=BIND_SOLREF, solimp=BIND_SOLIMP)


def no_fluid(sc):
    """The world's air is this block's drag model: MuJoCo's own fluid forces off (they would count the air twice)."""
    opt = sc.root.find("option")
    for k in ("density", "viscosity"):
        opt.attrib.pop(k, None)


def _hull_poly(pts2):
    p = np.unique(np.round(pts2, 6), axis=0)
    if len(p) < 3:
        return p
    p = p[np.lexsort((p[:, 1], p[:, 0]))]

    def cross(o, a, b):
        return (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0])

    lower, upper = [], []
    for q in p:
        while len(lower) >= 2 and cross(lower[-2], lower[-1], q) <= 0:
            lower.pop()
        lower.append(tuple(q))
    for q in p[::-1]:
        while len(upper) >= 2 and cross(upper[-2], upper[-1], q) <= 0:
            upper.pop()
        upper.append(tuple(q))
    return np.array(lower[:-1] + upper[:-1])


def _hull_area(pts2):
    h = _hull_poly(pts2)
    if len(h) < 3:
        return 0.0
    x, y = h[:, 0], h[:, 1]
    return 0.5 * abs(float(np.dot(x, np.roll(y, -1)) - np.dot(y, np.roll(x, -1))))


def _geom_points(m, g):
    t, size = m.geom_type[g], m.geom_size[g]
    if t == mujoco.mjtGeom.mjGEOM_MESH:
        mid = int(m.geom_dataid[g])
        P = m.mesh_vert[m.mesh_vertadr[mid]:m.mesh_vertadr[mid] + m.mesh_vertnum[mid]].astype(float)
    elif t == mujoco.mjtGeom.mjGEOM_BOX:
        P = np.array([[a, b, c] for a in (-1, 1) for b in (-1, 1) for c in (-1, 1)]) * size
    else:
        r = size[0]
        hz = size[1] if t in (mujoco.mjtGeom.mjGEOM_CAPSULE, mujoco.mjtGeom.mjGEOM_CYLINDER) else 0.0
        a = np.linspace(0, 2 * np.pi, 16, endpoint=False)
        ring = np.stack([r * np.cos(a), r * np.sin(a), 0 * a], 1)
        P = np.concatenate([ring + [0, 0, z] for z in (-hz, hz)] + [np.array([[0, 0, -hz - r], [0, 0, hz + r]])])
    R = np.zeros(9)
    mujoco.mju_quat2Mat(R, m.geom_quat[g])
    return P @ R.reshape(3, 3).T + m.geom_pos[g]


class SnowModel:
    """The snow's tangential forces on the planks and the air's drag on the robot and the planks, written to
    d.xfrc_applied after every physics step from that step's contacts (the forces of the next step)."""

    def __init__(self, w, planks: dict, snow_geoms, H_V=0.21e9, S_f=0.52e6, mu_opt=0.043, cd=None, v0=0.03,
                 phi_max_deg=70.0):
        m = w.m
        self.m, self.H_V, self.S_f, self.mu_opt, self.cd, self.v0 = m, H_V, S_f, mu_opt, C_N if cd is None else cd, v0
        self.phi_max = math.radians(phi_max_deg)
        self.is_snow = np.zeros(m.ngeom, bool)
        self.is_snow[[m.geom(n).id for n in snow_geoms]] = True
        self.seg_body = np.full(m.ngeom, -1, int)
        self.seg_s, self.seg_len, self.seg_w = np.zeros(m.ngeom), np.zeros(m.ngeom), np.zeros(m.ngeom)
        self.radius = np.ones(m.nbody) * 1e9
        self.plank_ids = []
        for name, pk in planks.items():
            L = pk.run_len
            for e, nm, s, _, hw in pk.sphere_s():
                g = m.geom(f"{name}_e{nm}").id
                bid = int(m.geom_bodyid[g])
                if bid not in self.plank_ids:
                    self.plank_ids.append(bid)
                self.radius[bid] = pk.radius
                self.seg_body[g], self.seg_s[g] = bid, s
                if nm[1:] in ("tipc", "tailc", "tipe", "taile"):
                    self.seg_len[g] = 0.5 * L / (SEG - 1)
                    self.seg_w[g] = pk.tip_w if nm[1:].startswith("tip") else pk.tail_w
                else:
                    i = int(nm[1:])
                    self.seg_len[g] = L / (SEG - 1) * (0.5 if i in (0, SEG - 1) else 1.0)
                    self.seg_w[g] = 2 * hw
        self.is_seg = self.seg_body >= 0
        # the drag's links: the robot's bodies with visual shapes, and the planks' parts
        bs, cs, As, pts = [], [], [], []
        for b in sorted(set(w.robot.bodies) | set(self.plank_ids)):
            gs = [g for g in range(m.ngeom) if m.geom_bodyid[g] == b and m.geom_group[g] in (1, 2)]
            if not gs:
                continue
            P = np.concatenate([_geom_points(m, g) for g in gs])
            c = 0.5 * (P.min(0) + P.max(0))
            bs.append(b)
            cs.append(c)
            As.append([_hull_area(P[:, [1, 2]]), _hull_area(P[:, [0, 2]]), _hull_area(P[:, [0, 1]])])
            pts.append(P)
        self.drag_b, self.drag_c, self.drag_A = np.array(bs, int), np.array(cs), np.array(As)
        self.drag_root = m.body_rootid[self.drag_b]
        self.overlap = self._overlap(w, pts)
        self.bodies = np.unique(np.r_[self.drag_b, self.plank_ids]).astype(int)
        self.last = {}

    def _overlap(self, w, pts, res=0.01) -> float:
        """The robot's true silhouette seen from the front over its links' summed areas (standing as at t = 0, before
        the family places it; the planks excluded)."""
        m = self.m
        d0 = mujoco.MjData(m)
        mujoco.mj_kinematics(m, d0)
        rob = [i for i, b in enumerate(self.drag_b) if b not in self.plank_ids]
        u = d0.xmat[w.robot.pelvis].reshape(3, 3)[:, 0]
        e1 = np.cross(u, [0.0, 0.0, 1.0])
        e1 /= np.linalg.norm(e1)
        e2 = np.cross(u, e1)
        polys, summed = [], 0.0
        for i in rob:
            b = self.drag_b[i]
            R = d0.xmat[b].reshape(3, 3)
            W_ = pts[i] @ R.T + d0.xpos[b]
            polys.append(_hull_poly(np.stack([W_ @ e1, W_ @ e2], 1)))
            loc = R.T @ u
            summed += float(np.sum(np.abs(loc) * self.drag_A[i]))
        allp = np.concatenate([p for p in polys if len(p) >= 3])
        lo, hi = allp.min(0) - res, allp.max(0) + res
        gx, gy = np.meshgrid(np.arange(lo[0], hi[0], res), np.arange(lo[1], hi[1], res))
        q = np.stack([gx.ravel(), gy.ravel()], 1)
        inside = np.zeros(len(q), bool)
        for h in polys:
            if len(h) < 3:
                continue
            ok = np.ones(len(q), bool)
            for k in range(len(h)):
                a0, a1 = h[k], h[(k + 1) % len(h)]
                ok &= ((a1[0] - a0[0]) * (q[:, 1] - a0[1]) - (a1[1] - a0[1]) * (q[:, 0] - a0[0])) >= 0
            inside |= ok
        return float(inside.sum() * res * res) / max(summed, 1e-9)

    def mu_k(self, v):
        v = np.abs(v)
        return self.mu_opt + 0.02 * np.clip((6.5 - v) / 3.2, 0.0, 1.0) + 0.003 * np.maximum(v - 11.5, 0.0)

    def apply(self, d) -> None:
        m = self.m
        xf = d.xfrc_applied
        xf[self.bodies] = 0.0
        # the air
        b = self.drag_b
        R = d.xmat[b].reshape(-1, 3, 3)
        cpos = d.xpos[b] + np.einsum("bij,bj->bi", R, self.drag_c)
        cv = d.cvel[b]
        vel = cv[:, 3:] + np.cross(cv[:, :3], cpos - d.subtree_com[self.drag_root])
        sp = np.linalg.norm(vel, axis=1)
        loc = np.einsum("bji,bj->bi", R, vel / np.maximum(sp, 1e-9)[:, None])
        # the quasi-steady normal-force model: each face of a link's hull pushes back along its own normal with
        # 0.5 rho C_N A_k |v| v_k (Hoerner's cross-flow principle): an inclined flat link (a ski, a flat trunk) gets lift
        # across the flow as well as drag along it; a compact link, drag nearly along -v. F . v <= 0 always.
        Fl = -0.5 * AIR_RHO * self.cd * self.overlap * sp[:, None] * (self.drag_A * (loc * sp[:, None]))
        F = np.einsum("bij,bj->bi", R, Fl)
        xf[b, :3] = F
        xf[b, 3:] = np.cross(cpos - d.xipos[b], F)
        self.last = {"drag_n": float(np.linalg.norm(F.sum(0))), "n": 0}
        # the snow
        nc = d.ncon
        if nc == 0:
            return
        con = d.contact
        g1, g2 = con.geom1[:nc], con.geom2[:nc]
        a = self.is_seg[g1] & self.is_snow[g2]
        bb = self.is_seg[g2] & self.is_snow[g1]
        sel = np.nonzero((a | bb) & (con.efc_address[:nc] >= 0))[0]
        if len(sel) == 0:
            return
        seg = np.where(a[sel], g1[sel], g2[sel])
        pos = con.pos[sel]
        nrm = con.frame[sel, :3] * np.where(a[sel], -1.0, 1.0)[:, None]
        fn = np.maximum(d.efc_force[con.efc_address[sel]], 0.0)
        body = self.seg_body[seg]
        Rs = d.xmat[body].reshape(-1, 3, 3)
        xs, ys = Rs[:, :, 0], Rs[:, :, 1]
        cvb = d.cvel[body]
        v = cvb[:, 3:] + np.cross(cvb[:, :3], pos - d.subtree_com[m.body_rootid[body]])

        def dot(u, q):
            return np.sum(u * q, axis=1)

        phi = np.arcsin(np.clip(dot(ys, nrm), -1.0, 1.0))      # + : rolled onto the right (-y) edge
        aphi = np.minimum(np.abs(phi), self.phi_max)
        t0 = xs - dot(xs, nrm)[:, None] * nrm
        t0 /= np.maximum(np.linalg.norm(t0, axis=1), 1e-9)[:, None]
        l0 = np.cross(nrm, t0)
        engage = np.clip(aphi / math.radians(5.0), 0.0, 1.0)    # the sidecut's arc engages from 0 to 5 degrees
        kappa = -np.sign(phi) * engage / (self.radius[body] * np.cos(aphi))
        al = kappa * self.seg_s[seg]
        t = np.cos(al)[:, None] * t0 + np.sin(al)[:, None] * l0
        lat = np.cross(nrm, t)
        vt = v - dot(v, nrm)[:, None] * nrm
        vl, vq = dot(vt, t), dot(vt, lat)
        mu = self.mu_k(np.linalg.norm(vt, axis=1))
        ln = self.seg_len[seg]
        V = fn / self.H_V                                        # the displaced volume (Mossner et al. 2013)
        tp = np.tan(aphi)
        bw = self.seg_w[seg]
        e_wedge = np.sqrt(2.0 * V * np.maximum(tp, 1e-6) / ln)
        e_full = V / (ln * bw) + 0.5 * bw * np.sin(aphi)
        e = np.where(e_wedge <= bw * np.sin(aphi), e_wedge, e_full)
        cap = mu * fn + np.minimum(fn * tp, self.S_f * e * ln)
        vl0 = np.maximum(self.v0, mu * fn / K_V)                 # the forces' slope at zero slip at most K_V
        vq0 = np.maximum(self.v0, cap / K_V)
        f_long = -mu * fn * vl / np.sqrt(vl * vl + vl0 ** 2)
        f_lat = -cap * vq / np.sqrt(vq * vq + vq0 ** 2)
        Fc = f_long[:, None] * t + f_lat[:, None] * lat
        Tc = np.cross(pos - d.xipos[body], Fc)
        np.add.at(xf, (body, slice(0, 3)), Fc)
        np.add.at(xf, (body, slice(3, 6)), Tc)
        self.last.update(n=len(sel), fn=float(fn.sum()), phi_deg=float(np.degrees(np.mean(phi))),
                         f_long=float(f_long.sum()), f_lat=float(f_lat.sum()))


class Digest:
    """The applied forces in the state hash (they carry the last physics step's contact forces into the next)."""

    def __init__(self, w, bodies):
        self.w, self.b = w, np.asarray(sorted(set(bodies)), int)

    def state(self):
        return [round(float(x), 9) for x in self.w.d.xfrc_applied[self.b].ravel()]


class Profile:
    """A snow surface's long profile z(x) of straight lines and circular arcs (freeski's lines.py), extruded across
    the course: built as thin tilted boxes, one per piece (a slab under the surface)."""

    def __init__(self, x0=0.0, z0=0.0, angle_deg=0.0):
        self.pts = [(x0, z0)]
        self.ang = math.radians(angle_deg)
        self.marks = {}
        self.idx = {}

    @property
    def end(self):
        return self.pts[-1]

    def mark(self, name):
        self.marks[name] = self.end
        self.idx[name] = len(self.pts) - 1
        return self

    def line(self, length, angle_deg=None):
        if angle_deg is not None:
            self.ang = math.radians(angle_deg)
        x, z = self.end
        self.pts.append((x + length * math.cos(self.ang), z + length * math.sin(self.ang)))
        return self

    def arc(self, radius, to_angle_deg, step=0.25):
        a1 = math.radians(to_angle_deg)
        da = a1 - self.ang
        if abs(da) < 1e-12:
            return self
        n = max(2, int(math.ceil(abs(da) * radius / step)))
        x, z = self.end
        sgn = 1.0 if da > 0 else -1.0
        cx, cz = x - sgn * radius * math.sin(self.ang), z + sgn * radius * math.cos(self.ang)
        for i in range(1, n + 1):
            a = self.ang + da * i / n
            self.pts.append((cx + sgn * radius * math.sin(a), cz - sgn * radius * math.cos(a)))
        self.ang = a1
        return self

    def arrays(self):
        p = np.array(self.pts)
        return p[:, 0], p[:, 1]

    def z_at(self, x):
        xs, zs = self.arrays()
        return np.interp(x, xs, zs)

    def slope_at(self, x):
        """The surface's angle (rad, + rising along +x) at x."""
        xs, zs = self.arrays()
        i = int(np.clip(np.searchsorted(xs, x) - 1, 0, len(xs) - 2))
        return math.atan2(zs[i + 1] - zs[i], xs[i + 1] - xs[i])

    def build(self, sc, prefix, half_width, rgba=SNOW_RGBA, thick=1.0, y=0.0, i0=0, i1=None, material=None):
        """The snow: one box per piece (pieces i0 .. i1 - 1), overlapping its neighbours by 2 mm (at a convex bend the
        overlap is a ridge of 2 mm x the bend's angle: 0.06 mm on a 25 cm piece of an 8 m arc). half_width and thick
        may be functions of the piece's middle x. Returns the geoms' names."""
        xs, zs = self.arrays()
        i1 = len(xs) - 1 if i1 is None else i1
        names = []
        for i in range(i0, i1):
            dx, dz = xs[i + 1] - xs[i], zs[i + 1] - zs[i]
            L = math.hypot(dx, dz)
            a = math.atan2(dz, dx)
            ux, uz = dx / L, dz / L
            nx, nz = -uz, ux                       # the surface's normal
            xm = 0.5 * (xs[i] + xs[i + 1])
            th = thick(xm) if callable(thick) else thick
            hw = half_width(xm) if callable(half_width) else half_width
            cx = xm - nx * 0.5 * th
            cz = 0.5 * (zs[i] + zs[i + 1]) - nz * 0.5 * th
            nm = f"{prefix}{i}"
            look = dict(material=material) if material else dict(rgba=rgba)
            sc.geom(sc.wb, nm, type="box", pos=(cx, y, cz), size=(0.5 * L + 0.002, hw, 0.5 * th),
                    quat=(math.cos(-a / 2), 0.0, math.sin(-a / 2), 0.0), **look, contype=SNOW_BIT,
                    conaffinity=SNOW_CONAFF, friction=(0.35, 0.005, 0.0001), condim=3, solref=PLANK_SOLREF,
                    solimp=PLANK_SOLIMP, priority=1)    # one snow for the skis and the body: its parameters win
                    #                                     (the body's soft default contact sank it 5-10 cm)
            names.append(nm)
        return names


def rot_y(a):
    return np.array([math.cos(a / 2), 0.0, math.sin(a / 2), 0.0])


def qmul(a, b):
    out = np.zeros(4)
    mujoco.mju_mulQuat(out, np.asarray(a, float), np.asarray(b, float))
    return out


def place_on_slope(w, slope_rad, zfun, planks: dict, legs=None):
    """t = 0: the robot standing perpendicular to the snow (the surface z = zfun(x), falling at slope_rad along +x;
    0: a level start platform), its planks under its boots as the bindings hold them, resting on the snow. planks: name
    -> (foot body, relpos, relquat) of its binding. legs: the 12 leg joint angles to start in (default: as they are);
    the leg motors hold them (the arbiter's stand hold)."""
    m, d, r = w.m, w.d, w.robot
    a = r.arb
    if legs is not None:
        d.qpos[r.qadr[:12]] = legs
    fq = r.fq
    d.qpos[fq + 3:fq + 7] = qmul(rot_y(slope_rad), d.qpos[fq + 3:fq + 7])
    mujoco.mj_kinematics(m, d)
    for name, (foot, relpos, relquat) in planks.items():  # each plank where its binding holds it
        b = m.body(foot).id
        R = d.xmat[b].reshape(3, 3)
        w.put(name, d.xpos[b] + R @ np.asarray(relpos, float), qmul(d.xquat[b], relquat))
    mujoco.mj_kinematics(m, d)
    c = math.cos(slope_rad)
    gaps = [(float(d.geom_xpos[g][2]) - float(zfun(float(d.geom_xpos[g][0])))) * c - SPHERE_R
            for g in range(m.ngeom) if "_e" in m.geom(g).name and m.geom(g).name.rsplit("_e", 1)[0] in planks]
    dz = -(min(gaps) + 0.0003) / c                # the lowest edge sphere 0.3 mm into the snow
    d.qpos[fq + 2] += dz
    for name in planks:
        d.qpos[w.free[name][0] + 2] += dz
    L = a.GROUPS["legs"]
    a.q_a[L] = a.q_b[L] = d.qpos[a.qadr[L]]
    mujoco.mj_kinematics(m, d)


# ------------------------------------------------------------------------ how a ski venue looks (and its solid fences)
# Groomed snow: near-white with a fine grain and a faint corduroy of the piste machine (looks.py leaves white surfaces
# alone, so the snow brings its own material); the course's fences are solid (a robot sliding off the course meets
# them, it never passes through), their posts, banners and markers looks only (merged meshes: looks._Batch).
SNOW_MAT = "eo_snow"
FENCE_RGBA = (0.95, 0.35, 0.08, 0.55)        # an orange safety net
FENCE_CONTACT = dict(contype=1, conaffinity=1 | 4 | 8 | 16, condim=3, friction=(0.6, 0.005, 0.0001),
                     solref="0.02 1", solimp="0.9 0.95 0.001")   # a net: soft (it gives, about 2 cm under a body)


def snow_material(sc) -> str:
    asset = sc.root.find("asset")
    if not any(t.get("name") == SNOW_MAT for t in asset.findall("material")):
        ET.SubElement(asset, "texture", name=SNOW_MAT, type="2d", builtin="checker", rgb1="0.965 0.97 0.985",
                      rgb2="0.935 0.945 0.97", mark="random", markrgb="0.88 0.9 0.95", random="0.06",
                      width="512", height="512")
        ET.SubElement(asset, "material", name=SNOW_MAT, texture=SNOW_MAT, texuniform="true", texrepeat="0.8 6",
                      specular="0.25", shininess="0.3", reflectance="0.02")
    return SNOW_MAT


def snow_side_terrain(sc, pr, prefix, i0, i1, inner, outer, z_of=None, thick=2.0) -> list:
    """Snow terrain beside the course (both sides, from the course's edge `inner` to `outer` m from its centre line),
    flush with the profile's pieces i0 .. i1 - 1 (or with z_of(x), a flat level, when given). Returns the geoms."""
    xs, zs = pr.arrays()
    names = []
    for side in (1, -1):
        y = side * 0.5 * (inner + outer)
        if z_of is None:
            names += pr.build(sc, f"{prefix}{'L' if side > 0 else 'R'}", 0.5 * (outer - inner), thick=thick, y=y,
                              i0=i0, i1=i1, material=snow_material(sc))
        else:
            x0, x1 = xs[i0], xs[i1]
            nm = f"{prefix}{'L' if side > 0 else 'R'}"
            sc.geom(sc.wb, nm, type="box", pos=(0.5 * (x0 + x1), y, z_of - 0.5 * thick),
                    size=(0.5 * (x1 - x0) + 0.002, 0.5 * (outer - inner), 0.5 * thick), material=snow_material(sc),
                    contype=SNOW_BIT, conaffinity=SNOW_CONAFF, friction=(0.35, 0.005, 0.0001), condim=3,
                    solref=PLANK_SOLREF, solimp=PLANK_SOLIMP, priority=1)
            names.append(nm)
    return names


def fence_line(sc, batch, prefix, zfun, x0, x1, y, height=1.2, step=2.0) -> None:
    """A solid safety net along the course at y from x0 to x1, following the snow (zfun(x)): one thin panel per
    `step` m and a post at each panel's end (looks)."""
    n = max(1, int(math.ceil((x1 - x0) / step)))
    for i in range(n):
        a, b = x0 + (x1 - x0) * i / n, x0 + (x1 - x0) * (i + 1) / n
        za, zb = float(zfun(a)), float(zfun(b))
        L = math.hypot(b - a, zb - za)
        ang = math.atan2(zb - za, b - a)
        sc.geom(sc.wb, f"{prefix}{i}", type="box", pos=(0.5 * (a + b), y, 0.5 * (za + zb) + 0.5 * height),
                size=(0.5 * L, 0.01, 0.5 * height), quat=(math.cos(-ang / 2), 0.0, math.sin(-ang / 2), 0.0),
                rgba=FENCE_RGBA, group=1, **FENCE_CONTACT)
        batch.add((0.25, 0.25, 0.27), (b, y, zb + 0.5 * height + 0.05), (0.025, 0.025, 0.5 * height + 0.05))


def arch(batch, x, y, z, half_w, height=3.0, rgb=(0.12, 0.3, 0.75)) -> None:
    """A banner arch across the course at x (looks): two posts and the banner."""
    for s_ in (1, -1):
        batch.add((0.2, 0.2, 0.22), (x, y + s_ * half_w, z + 0.5 * height), (0.08, 0.08, 0.5 * height))
    batch.add(rgb, (x, y, z + height - 0.25), (0.05, half_w, 0.3))
    batch.add((0.95, 0.95, 0.95), (x - 0.06, y, z + height - 0.25), (0.005, 0.8 * half_w, 0.12))
# ====================================================================================== END ski-snow block v3


# ==================================================================================== BEGIN freeski block
# Big air on skis, one jump, one trick (the same block in every freeski_<trick>.py). The course, the start and the trick's measurement follow
# the Embodied Olympics freeski sport (github.com/CHIGUI0/roboolympics PR #259, branch sport-freeski, commit d566cffb4, MIT):
# sports/freeski/course.py BIGAIR (the FIS numbers), the level start platform with a self-start
# (sport.toml D6, D8), and referee.py's trick measurement (D14): the parts marked "verbatim" are copied byte for byte.
#
# Rule book: FIS International Competition Rules (ICR) Book VI, Joint Regulations for Snowboard, Freestyle, Freeski
# and Ski Cross, edition June 2026: 3101.3 (the start platform: "a completely flat, level area"), 3102.4.1 (the big
# air site, level A), 3402 (big air judging: difficulty, execution, amplitude, the landing), 3610 (the start: the
# competitor drops in on the start referee's signal); FIS Snowboard & Freeski Park & Pipe Judges Handbook 2025, 8-11
# (tricks named by their rotation: spins in 180 degree units, flips / inversions; a trick is landed when the rider
# lands on the skis and rides away in control); FIS Specifications for Competition Equipment 2026/27 B.2.2.

# freeski's park ski at the robot's size (Faction Prodigy 1 Grom 135 cm, a twin tip: 115/84/111 mm, sidecut radius 10 m,
# 1.19 kg per ski; FIS sets no limit for freestyle skis, ICR 4010.2, EQ B.2.2); binding 0.65 kg, stand height 35 mm,
# mount 2 cm behind the middle, a junior boot 1.2 kg, bending stiffness EI 60 / 80 N m^2 (fore / aft): freeski's
# estimates (venue.py SKIS["park"], BOOT_MASS; skisnow.py Ski)
PARK_SKI = Plank("Faction Prodigy 1 Grom 135 cm", 1.35, 0.115, 0.084, 0.111, 10.0, 1.19, -0.02, 0.65, 0.035,
                 shovel=0.17, tail=0.15, tip_rise=0.065, tail_rise=0.05, boot_mass=1.2, EI_fore=60.0, EI_aft=80.0)
G1_SOLE_Z, G1_FOOT_X = -0.035, 0.035         # the G1's sole below its ankle-roll link, its foot's middle ahead of it

# verbatim: sports/freeski/course.py BIGAIR (ICR Book VI June 2026, 3102.4.1, p. 67, level A)
BIGAIR = dict(
    start_len=6.0,          # start area >= 5 m (the drop-in platform >= 5 m)
    start_angle=0.0,        # ICR 3101.3: the start platform "should be a completely flat, level area that allows the
                            # competitor to stand in a relaxed position"; the robot drops in by its own action
    rollin_R=8.0,           # the roll-in from the platform onto the drop-in ramp (no FIS radius: our estimate)
    start_back=0.30,        # the robot's boots stand this far behind the platform's edge (the start line)
    inrun_angle=35.0,       # in-run inclination >= 20 deg (our choice within the rule)
    inrun_len=38.0,         # in-run ramp length >= 30 m (our choice: the speed that carries a straight air to the
                            # landing's sweet spot; screening report §4)
    inrun_R=30.0,           # the transitions' radii (no FIS number: our estimate of a smooth shaped transition)
    flat_len=5.0,           # "a flat or light transition of 5-10 m before the kicker"
    kicker_R=18.0,          # the kicker's transition radius (our estimate)
    takeoff=25.0,           # take-off angle >= 25 deg (our choice)
    lip_len=1.0,            # the straight lip after the transition (our estimate)
    table_drop=None,        # the flat between the kicker and the landing at the in-run flat's level: the kicker's height
                            # is measured from it ("bottom of the flat to the lip", 3102.4.1; the city big air's
                            # requirements put pads "on the flat between kicker and landing")
    table_len=15.0,         # take-off to landing >= 15 m (level A / B)
    knoll_R=8.0,            # the knoll's rounding (our estimate)
    landing_angle=36.0,     # landing gradient >= 28 deg (our choice: a common big air landing; it matches the flight path
                            # a straight air comes down on, 41 deg at touchdown)
    landing_len=30.0,       # landing length >= 20 m
    landing_R=30.0,         # the landing's transition to the outrun (our estimate)
    outrun_len=45.0,        # outrun >= 25 m at 0 deg (> 30 m recommended)
    w_inrun=8.0, w_kicker=6.0, w_landing=24.0, w_outrun=30.0,  # widths: drop-in >= 5, kicker >= 5, landing >= 20,
)                                                              # outrun 30 (min 20)
FINISH_PAST_OUTRUN_M = 15.0  # freeski: the run is over (ridden away) past this line, the outrun's start + 15 m


def bigair_profile(B=BIGAIR):
    """The big air's long profile (freeski course.bigair, as straight pieces and arcs): x down the course from the start
    platform's back edge (x = 0, z = 0), the marks start_edge, inrun_end, kicker_start, lip, knoll_start, knoll,
    landing_end, outrun_start, end."""
    p = Profile(0.0, 0.0, -B["start_angle"])
    p.line(B["start_len"]).mark("start_edge")
    p.arc(B["rollin_R"], -B["inrun_angle"])
    p.line(B["inrun_len"], -B["inrun_angle"]).mark("inrun_end")
    p.arc(B["inrun_R"], 0.0)
    p.line(B["flat_len"], 0.0).mark("kicker_start")
    p.arc(B["kicker_R"], B["takeoff"])
    p.line(B["lip_len"], B["takeoff"]).mark("lip")
    lip_x, lip_z = p.end
    table_z = lip_z - B["table_drop"] if B["table_drop"] is not None else p.marks["kicker_start"][1]
    p.pts.append((lip_x + 0.6, table_z))      # the kicker's back face down to the table
    p.mark("table_start")
    p.ang = 0.0
    p.line(B["table_len"] - 0.6, 0.0).mark("knoll_start")
    p.arc(B["knoll_R"], -B["landing_angle"]).mark("knoll")
    p.line(B["landing_len"], -B["landing_angle"]).mark("landing_end")
    p.arc(B["landing_R"], 0.0).mark("outrun_start")
    p.line(B["outrun_len"], 0.0).mark("end")
    return p


# verbatim: sports/freeski/referee.py (the trick's measurement and its name)
class Flight:
    def __init__(self, k, x, speed, v, e_axes, com_z):
        self.k0, self.x0, self.speed0 = k, x, speed
        self.angle0 = math.degrees(math.atan2(v[2], math.hypot(v[0], v[1])))
        self.e_axes = e_axes
        self.rot = np.zeros(3)
        self.min_up = 1.0
        self.peak = com_z
        self.wobble = 0.0  # the body's angular speed squared, integrated (form)
        self.spread = 0.0  # the skis' largest distance apart (m)
        self.landing = None
        self.switch = False  # took off riding backwards (the skis' tips against the travel)
        self.after_min_h = None  # the pelvis's lowest height over the snow in the 1 s after the landing (sitting back)

    def trick(self):
        lat, along = self.e_axes
        spin = math.degrees(abs(float(self.rot[2])))
        flip = math.degrees(max(abs(float(self.rot @ lat)), abs(float(self.rot @ along))))
        inverted = self.min_up < 0.0
        n180 = int(round(spin / 180.0))
        nflip = int(round(flip / 360.0)) if inverted else 0
        if n180 == 0 and nflip == 0:
            name = "straight air"
        elif nflip == 0:
            name = f"{180 * n180}"
        else:
            name = ("cork " if n180 else "") + (f"{180 * n180} " if n180 else "") + \
                   ("double " if nflip == 2 else "triple " if nflip >= 3 else "") + "flip"
        return dict(name=name, spin_deg=round(spin, 1), flip_deg=round(flip, 1), half_turns=n180, flips=nflip,
                    inverted=bool(inverted), offaxis=bool(inverted and n180 > 0))

    def record(self):
        out = dict(x=round(self.x0, 2), speed=round(self.speed0, 2), angle_deg=round(self.angle0, 1),
                   peak_z=round(self.peak, 2), trick=self.trick())
        if self.landing:
            out["landing"] = self.landing
        if self.switch:
            out["switch"] = True
        if self.after_min_h is not None:
            out["after_min_h"] = round(self.after_min_h, 2)
        return out


ROT_TOL = 15.0
D_SPIN = {0: 10.0, 1: 20.0, 2: 30.0, 3: 40.0, 4: 50.0, 5: 60.0, 6: 70.0, 7: 80.0, 8: 90.0}  # by half turns; 9+: 100
D_FLIP = {"back": 45.0, "front": 50.0}  # an on-axis single flip; each further flip +25
D_CORK_PER_FLIP, D_SWITCH = 10.0, 5.0


def classify(f, base_deg):
    """The flight's trick as the judges name it, with its credited and measured rotations. The
    spin is the skis' heading change over the flight, f.spin, not the pelvis's integrated yaw rate f.rot[2]; the rest
    is verbatim."""
    lat, along = f.e_axes
    spin_m = math.degrees(float(f.spin))
    flip_m = math.degrees(float(f.rot @ lat)) - base_deg  # + forward, - backward, the straight air's own removed
    n180 = int(math.floor(abs(spin_m) / 180.0 + 0.5))
    inverted = f.min_up < 0.0
    nflip = int(math.floor(abs(flip_m) / 360.0 + 0.5)) if inverted else 0
    short = abs(abs(spin_m) - 180.0 * n180) + (abs(abs(flip_m) - 360.0 * nflip) if nflip else 0.0)
    sdir = "left" if spin_m > 0 else "right"
    fdir = "back" if flip_m < 0 else "front"
    if n180 == 0 and nflip == 0:
        name, D = "straight air", D_SPIN[0]
    elif nflip == 0:
        name, D = f"{180 * n180} {sdir}", D_SPIN.get(n180, 100.0)
    elif n180 <= 1:  # on axis (a half turn of spin in a flip is a flip's own twist: no cork)
        mult = {1: "", 2: "double ", 3: "triple "}.get(nflip, f"{nflip}x ")
        name, D = f"{mult}{fdir} flip", D_FLIP[fdir] + 25.0 * (nflip - 1)
    else:
        mult = {1: "", 2: "double ", 3: "triple "}.get(nflip, f"{nflip}x ")
        name, D = f"{mult}cork {180 * n180} {sdir}", D_SPIN.get(n180, 100.0) + D_CORK_PER_FLIP * nflip
    if f.switch:
        name, D = "switch " + name, D + D_SWITCH
    return dict(name=name, difficulty=min(100.0, D), half_turns=n180, flips=nflip, spin_deg=round(spin_m, 1),
                flip_deg=round(flip_m, 1), short_deg=round(short, 1), inverted=bool(inverted), switch=bool(f.switch),
                pelvis_spin_deg=round(math.degrees(float(f.rot[2])), 1))
# (end of the verbatim part)


def wrap_pi(a: float) -> float:
    return (a + math.pi) % (2.0 * math.pi) - math.pi


HEADING_MIN_XY = math.sin(math.radians(10.0))   # a ski within 10 degrees of the vertical has no heading


class FlightRec(Flight):
    """freeski's Flight in the state hash, with the skis' heading change over the flight (spin, rad: the mean of the
    two skis' changes, ski_turn)."""
    spin = 0.0
    ski_turn = (0.0, 0.0)

    def state(self):
        return [self.k0, round(self.x0, 9), round(self.speed0, 9), round(self.angle0, 9),
                [round(float(x), 9) for x in self.rot], round(self.min_up, 9), round(self.peak, 9),
                [[round(float(x), 9) for x in a] for a in self.e_axes], self.switch, self.landing,
                round(float(self.spin), 9), [round(float(x), 9) for x in self.ski_turn]]


class FreeskiTask(TaskBase):
    """One big air jump with one named trick. A family sets `trick` (the name the families' texts use), `want`
    (half_turns, flips, flip_dir): flip_dir "back" / "front" for a flip, None for none; half_turns None: any number
    of half turns up to 1 (a flip's own half twist, freeski's rule)."""
    category = "track-and-jumps"
    robot = "unitree_g1"
    fall_rule = False                                # the snow's own fall rule (step)
    legal_ground = ("left_foot", "right_foot")       # the boots on the snow are no fall (an edged ski's boot)
    trick = "straight air"
    want = (0, 0, None)
    time_limit_s = 40.0
    stages = ["start", "take-off", "landing", "ride-away"]
    rulebook = ("FIS International Competition Rules (ICR) Book VI (Joint Regulations for Snowboard, Freestyle, Freeski "
                "and Ski Cross), edition June 2026: 3101.3 (the start platform: a completely flat, level area), "
                "3102.4.1 (the big air site, level A: start area at least 5 m, in-run at least 30 m at 20 degrees or "
                "more, a 5-10 m flat before the kicker, a kicker at least 2.0 m high with a take-off angle of 25 "
                "degrees or more, at least 15 m from the take-off to the landing, a landing of 28 degrees or more, at "
                "least 20 m long and wide, an outrun of at least 25 m), 3402 (big air judging: difficulty, execution, "
                "amplitude, the landing), 3610 (the start: the competitor drops in on the start referee's signal); "
                "FIS Snowboard & Freeski Park & Pipe Judges Handbook, edition 2025, 8-11 (tricks by their rotation: "
                "spins in 180 degree units, flips; a trick counts when it is landed on the skis and ridden away); FIS "
                "Specifications for Competition Equipment 2026/27 B.2.2 (freestyle skis: no limits)")
    deviations = [
        "ICR 3402 / Park & Pipe Judges Handbook 8-11 (judges score each jump 0-100 by impression): one task is one "
        "trick, pass or fail: the trick is measured, not judged (the method of the Embodied Olympics freeski sport, MIT, with "
        "the spin taken from the skis), over the flight only (from the last contact with the snow to the next), so a "
        "rotation on the snow never counts and a wobble back and forth cancels; the spin is the skis' heading change "
        "(each ski's long axis, tail to tip, followed in the horizontal plane every 1 ms and unwrapped, so whole turns "
        "add up; held while a ski points within 10 degrees of the vertical; the mean of the two skis), counted in 180 "
        "degree units (not the pelvis's yaw: the pelvis turns against the skis on the hips and against the trunk at "
        "the waist, so its yaw can count turns the skis never made); the flip is the pelvis's angular velocity "
        "integrated about the horizontal axis across the take-off direction less the straight air's own forward turn "
        "({base_deg} degrees: the lip's "
        "plus the landing's angle), counted in 360 degree units and only if the body went upside down; each to the "
        "nearest unit, so up to half a unit short or over still counts as that unit (a judge would deduct; a "
        "rotation half a unit or more off is the next or the previous trick). A flip with half a turn of spin is "
        "still that flip (freeski's rule: its own twist). A twist made while inverted is measured about the vertical "
        "like any spin (a cork and a twisting flip are not told apart). Grabs, amplitude, style and execution are "
        "not judged.",
        "Park & Pipe Judges Handbook (a landed trick: on the skis, riding away in control): the landing must be "
        "clean: the first contact after the flight is the skis', with both skis pointing the way the trick lands "
        "(forwards after whole turns, backwards after an odd number of half turns) within {land_heading_deg} degrees "
        "(a judge sees a 360 landed sideways or switch as another trick or a crash); from {settle_s} s after that "
        "contact (the landing's impact and its first correction) to the finish line each ski's heading stays within "
        "{ride_turn_deg} degrees of its heading then (no spin on the snow: a judge does not credit rotation made on "
        "the landing, and a skier riding away straight needs at most the landing's {land_heading_deg} degrees plus "
        "the finish line's {ride_heading_deg} degrees of correction; a quarter turn is above that and half the "
        "smallest spin, the 180 of a revert); no other part of the robot (hands included; the "
        "boots on their skis are allowed) touches the snow from then until the finish line {finish_m} m past the "
        "outrun's start, the robot is not down (on the snow with the pelvis less than {fall_h_m} m above it or the "
        "trunk more than {tilt_max_deg} degrees from its normal) for {fall_s} s in all (freeski's fall rule; a "
        "judge would deduct for sitting back), the landing is past the table (a landing on the table "
        "or the knuckle before the knoll fails), and the skis cross the finish line pointing the way the trick "
        "landed (within {ride_heading_deg} degrees).",
        "ICR 3102.4.1 (the big air site): the course is the Embodied Olympics freeski big air (MIT): a level start platform "
        "{start_len} m long, its edge rounded (radius {rollin_R} m, an estimate) onto a {inrun_len} m in-run at "
        "{inrun_angle} degrees, a {inrun_R} m transition, a {flat_len} m flat, a kicker of transition radius "
        "{kicker_R} m with a {lip_len} m straight lip at {takeoff} degrees (2.1 m above the flat, {w_kicker} m wide), "
        "a table {table_len} m long at the flat's level, an {knoll_R} m knoll rounding, a {landing_angle} degree "
        "landing {landing_len} m long and {w_landing} m wide, a {landing_R} m transition and a {outrun_len} m outrun "
        "({w_outrun} m wide); every length within level A's limits, the transitions' radii estimates (FIS gives "
        "none); the snow is a profile of boxes (pieces of 25 cm along the arcs).",
        "ICR 3610 (the start): there is no start gate and nothing holds or pushes the robot: it stands on its skis on "
        "the level start platform, its boots {start_back} m behind the platform's edge (the start line), and the "
        "start referee's signal is given at t = 0; the robot leaves by its own action (it must work itself over the "
        "edge); not started within {dns_s} s is a DNS.",
        "Equipment (EQ B.2.2): no limits for freestyle skis; the skis are the Faction Prodigy 1 Grom 135 cm (115/84/111 "
        "mm, radius 10 m, 1.19 kg), each three bodies (the middle rigid under the binding, the forebody and the "
        "afterbody on hinges with the ski's bending stiffness, EI 60 and 80 N m^2: estimates); binding 0.65 kg "
        "(stand height 35 mm) and boot 1.2 kg as mass on the ski (estimates); the binding holds the foot rigidly and "
        "never releases; the boot's forward flex is not modelled (the ankle joint moves inside it); no poles (big air "
        "athletes ski without them). The skis are solid against the robot's body and each other.",
        "Snow and air: the ski-snow model of the Embodied Olympics freeski sport (MIT, ported): kinetic friction after "
        "Wolfsperger et al. 2021, the edge's hold after Mossner et al. 2013 with Federolf's race-piste snow, hard "
        "well-damped snow; still air at a mountain venue (1.052 kg/m^3), the air's drag and lift on every link (a normal-force model); no "
        "wind, no snow is moved.",
        "Competition format (ICR 3501-3504: qualification and final runs, the best scores added): one jump, one "
        "attempt.",
    ]
    params = dict(
        start_len=6.0, rollin_R=8.0, start_back=0.30, inrun_angle=35.0, inrun_len=38.0, inrun_R=30.0, flat_len=5.0,
        kicker_R=18.0, takeoff=25.0, lip_len=1.0, table_len=15.0, knoll_R=8.0, landing_angle=36.0, landing_len=30.0,
        landing_R=30.0, outrun_len=45.0, w_inrun=8.0, w_kicker=6.0, w_landing=24.0, w_outrun=30.0,
        base_deg=61.0, air_ms=30, fall_h_m=0.40, tilt_max_deg=70.0, fall_s=1.0, finish_m=15.0, ride_heading_deg=30.0,
        land_heading_deg=45.0, settle_s=0.5, ride_turn_deg=90.0, dns_s=20.0, stop_speed_m_s=0.3, stop_s=3.0,
        lip_window_m=4.0, min_angle_deg=10.0)

    sentence = ("Drop in from the start platform, ride the in-run, take off from the kicker, do a {trick} in the air, "
                "land on your skis and ride away past the finish line.")
    rules = ("FIS big air, one jump, judged on one trick: {trick_rule} Your feet are bound to freestyle skis (1.35 m "
             "twin tips). You stand on the level start platform with your boots {start_back} m behind its edge (the "
             "start line); the start signal is given at t = 0 and you must leave the platform by your own action "
             "(nothing holds or pushes you) within {dns_s} s. The course: the platform's edge rounds onto the in-run "
             "({inrun_len} m at {inrun_angle} degrees), a transition and a {flat_len} m flat, then the kicker (it "
             "rises to its lip at {takeoff} degrees, 2.1 m above the flat), a table {table_len} m long at the flat's "
             "level, the knoll, the landing ({landing_angle} degrees, {landing_len} m long), a transition and the "
             "flat outrun. The jump is your flight off the kicker: it starts within {lip_window_m} m of the lip, "
             "rising at {min_angle_deg} degrees or more. The trick is measured in the air only (from the moment your last "
             "contact with the snow ends until the next contact). The spin is your skis' heading change: each ski's "
             "long axis (tail to tip) is followed in the horizontal plane (unwrapped, so whole turns add up; held while "
             "the ski points within 10 degrees of the vertical) and the spin is the mean of the two skis' changes, "
             "counted in half turns of 180 degrees (your pelvis's or trunk's own yaw does not count). The flip is your "
             "pelvis's rotation about the horizontal axis across your take-off direction, after removing the "
             "{base_deg} degrees a straight air turns forward anyway, counted in full "
             "turns of 360 degrees and only if your body went upside down; each to the nearest unit (up to half a "
             "unit short or over still counts as that unit). A flip with half a turn of spin is still that flip. You "
             "succeed when the jump's rotation is the {trick} and the landing is clean: the first contact after the "
             "flight is your skis', on the knoll or the landing hill (past the table), with both skis pointing forwards "
             "(down the course, +x) within {land_heading_deg} degrees at that first contact; from {settle_s} s after "
             "it until both boots cross the finish line each ski's heading stays within {ride_turn_deg} degrees of its "
             "heading at that moment (no spinning on the snow); from the first contact until both boots "
             "cross the finish line ({finish_m} m past the start of the outrun) no part of you but the boots touches "
             "the snow and you are not down for {fall_s} s in all (down: on the snow with your pelvis less than "
             "{fall_h_m} m above it, measured along its normal, or your trunk more than {tilt_max_deg} degrees from "
             "its normal; the time down shrinks again at a quarter of the rate while you are up); and at the finish "
             "line your pelvis is at least {fall_h_m} m above the snow and both skis point forwards (within "
             "{ride_heading_deg} degrees). You fail: on any other trick (more or less rotation, a flip where none is "
             "asked, a different direction of flip, a take-off riding backwards), a landing with a ski pointing more "
             "than {land_heading_deg} degrees from forwards, a spin on the snow (a ski's heading more than "
             "{ride_turn_deg} degrees from where it was {settle_s} s after the landing), on a fall at any time (a part of you other than your boots on the snow, or down for {fall_s} s "
             "in all), a landing on the table, the body first, a "
             "run with no jump, a stop (slower than {stop_speed_m_s} m/s for {stop_s} s after the start), no start "
             "within {dns_s} s, or at {time_limit_s} s.")
    venue = ("x runs down the course from the start platform's back edge; you stand facing +x near the platform's "
             "front edge (the start line, {start_len} m from its back) with your boots {start_back} m behind it; the "
             "platform is level, 8 m wide. Past the edge the in-run falls at {inrun_angle} degrees for {inrun_len} m, "
             "bends (radius {inrun_R} m) into a {flat_len} m flat, and the kicker ({w_kicker} m wide) rises from it "
             "(radius {kicker_R} m) to a {lip_len} m lip at {takeoff} degrees; the lip is about 59 m down the course "
             "and about 23 m below the platform. Behind the lip the table runs {table_len} m from the lip at the "
             "flat's level to the knoll (blue line), the landing falls at {landing_angle} degrees for {landing_len} m "
             "({w_landing} m wide), bends (radius {landing_R} m) into the flat outrun ({outrun_len} m, {w_outrun} m "
             "wide); the finish line (green) is {finish_m} m into the outrun. Red lines mark the start line and the "
             "lip.")
    difficulty = ("The robot must start itself from a standstill on level snow, ride a 35 degree in-run to about 16 "
                  "m/s and leave a 25 degree kicker with the right rotation for its trick, finish the rotation in "
                  "about 1.5 s of air and land on a 36 degree slope with no balance controller of its own.")
    trick_rule = ""

    def __init_subclass__(cls, **kw):
        """A family's texts: the trick's name and rule put into the shared texts."""
        super().__init_subclass__(**kw)
        for k in ("sentence", "rules"):
            setattr(cls, k, getattr(FreeskiTask, k).replace("{trick_rule}", cls.trick_rule).replace("{trick}", cls.trick))

    # ------------------------------------------------------------------ the course
    def course(self):
        B = dict(BIGAIR, **{k: self.p[k] for k in BIGAIR if k in self.p})
        return bigair_profile(B)

    @property
    def start(self):
        p = self.p
        return (p["start_len"] - p["start_back"] - G1_FOOT_X, 0.0, 0.0)

    def build(self, sc):
        p = self.p
        no_fluid(sc)
        pr = self.pr = self.course()
        xs, zs = pr.arrays()
        floor = next(g for g in sc.wb.findall("geom") if g.get("name") == "floor")
        floor.set("pos", f"0 0 {float(zs.min()) - 0.02:.4g}")    # the open ground at the outrun's level
        I = pr.idx
        kz, oz = pr.marks["kicker_start"][1], pr.marks["outrun_start"][1]
        mat = snow_material(sc)
        geoms = []                              # the course stands on its own snow: each box reaches well below it
        geoms += pr.build(sc, "inrun", 0.5 * p["w_inrun"], thick=2.0, i0=0, i1=I["kicker_start"], material=mat)
        geoms += pr.build(sc, "kicker", 0.5 * p["w_kicker"], thick=lambda x: float(pr.z_at(x)) - kz + 3.0,
                          i0=I["kicker_start"], i1=I["lip"], material=mat)   # its back face: the lip's slab's end
        geoms += pr.build(sc, "table", 0.5 * p["w_kicker"], thick=3.0, i0=I["table_start"], i1=I["knoll_start"],
                          material=mat)
        geoms += pr.build(sc, "landing", 0.5 * p["w_landing"], thick=lambda x: float(pr.z_at(x)) - oz + 1.0,
                          i0=I["knoll_start"], i1=I["outrun_start"], material=mat)
        geoms += pr.build(sc, "outrun", 0.5 * p["w_outrun"], thick=1.0, i0=I["outrun_start"], material=mat)
        # the mountain beside the course (to 30 m from its centre line): the in-run's and the landing's own slopes,
        # the flat beside the kicker and the table (the kicker is built on it)
        side = 60.0
        geoms += snow_side_terrain(sc, pr, "side_in", 0, I["kicker_start"], 0.5 * p["w_inrun"], side)
        geoms += snow_side_terrain(sc, pr, "side_k", I["kicker_start"], I["knoll_start"], 0.5 * p["w_kicker"], side,
                                   z_of=kz, thick=3.0)
        geoms += snow_side_terrain(sc, pr, "side_l", I["knoll_start"], I["outrun_start"], 0.5 * p["w_landing"], side)
        geoms += snow_side_terrain(sc, pr, "side_o", I["outrun_start"], len(pr.pts) - 1, 0.5 * p["w_outrun"], side)
        # the looks of a big air: safety nets along the in-run and the landing hill (solid), the start and finish
        # arches, distance markers every 5 m down the landing from the knoll (blue boards beside the course)
        from rfo import looks as _looks
        batch = _looks._Batch(sc, "freeski_look")
        xs_, zs_ = pr.arrays()
        for s_ in (1, -1):
            fence_line(sc, batch, f"net_in{'L' if s_ > 0 else 'R'}", pr.z_at, 0.5, pr.marks["kicker_start"][0],
                       s_ * (0.5 * p["w_inrun"] + 0.4))
            fence_line(sc, batch, f"net_land{'L' if s_ > 0 else 'R'}", pr.z_at, pr.marks["knoll_start"][0],
                       pr.marks["outrun_start"][0], s_ * (0.5 * p["w_landing"] + 0.4))
            fence_line(sc, batch, f"net_out{'L' if s_ > 0 else 'R'}", pr.z_at, pr.marks["outrun_start"][0],
                       float(xs_[-1]) - 1.0, s_ * (0.5 * p["w_outrun"] + 0.4))
        sx, sz = pr.marks["start_edge"]
        arch(batch, sx + 0.8, 0.0, float(pr.z_at(sx + 0.8)), 0.5 * p["w_inrun"] - 0.3, height=3.2,
             rgb=(0.8, 0.1, 0.1))
        fx = self.finish_x()
        arch(batch, fx, 0.0, float(pr.z_at(fx)), 0.5 * p["w_outrun"] - 1.0, height=4.0)
        kx = pr.marks["knoll"][0]
        for i in range(1, 7):
            x = kx + 5.0 * i * math.cos(math.radians(p["landing_angle"]))
            if x > pr.marks["outrun_start"][0]:
                break
            for s_ in (1, -1):
                batch.add((0.1, 0.25, 0.8), (x, s_ * (0.5 * p["w_landing"] + 1.0), float(pr.z_at(x)) + 0.6),
                          (0.04, 0.4, 0.3))
        batch.emit()
        self.snow = geoms
        sc.ground = ["floor"] + list(self.snow)
        for nm, x, rgba, w_ in (("start_line", pr.marks["start_edge"][0] - 0.05, (0.85, 0.12, 0.1, 1), p["w_inrun"]),
                                ("lip_line", pr.marks["lip"][0] - 0.15, (0.85, 0.12, 0.1, 1), p["w_kicker"]),
                                ("knoll_line", pr.marks["knoll_start"][0], (0.1, 0.3, 0.85, 1), p["w_landing"]),
                                ("landing_end_line", pr.marks["landing_end"][0], (0.1, 0.3, 0.85, 1), p["w_landing"]),
                                ("finish_line", self.finish_x(), (0.1, 0.6, 0.25, 1), p["w_outrun"])):
            z = float(pr.z_at(x))
            a = pr.slope_at(x)
            sc.geom(sc.wb, nm, type="box", pos=(x, 0, z + 0.004), size=(0.08, 0.5 * w_, 0.003),
                    quat=(math.cos(-a / 2), 0.0, math.sin(-a / 2), 0.0), rgba=rgba, contype=0, conaffinity=0)
        self.planks = {f"{s}_ski": (PARK_SKI, f"{s}_ankle_roll_link",
                                    (G1_FOOT_X - PARK_SKI.mount, 0.0, G1_SOLE_Z - PARK_SKI.stand_h), (1, 0, 0, 0))
                       for s in ("left", "right")}
        self.parts = {}
        for name in self.planks:
            self.parts[name] = add_plank(sc, name, PARK_SKI, rgba=(0.85, 0.2, 0.15, 1))
        for name, (pk, foot, rp, rq) in self.planks.items():
            bind(sc, name, foot, rp, rq)
        lx, lz = pr.marks["lip"]
        sx, sz = pr.marks["start_edge"]
        sc.camera("venue", (lx + 18.0, -62.0, lz + 6.0), (lx + 20.0, 0.0, lz - 9.0), fovy=60)
        sc.camera("start", (sx - 5.0, -3.0, sz + 2.5), (sx + 2.0, 0.0, sz - 0.5), fovy=55)
        sc.camera("kicker", (lx - 4.0, -9.0, lz + 1.5), (lx + 3.0, 0.0, lz + 0.5), fovy=60)

    def reset(self, w):
        place_on_slope(w, 0.0, self.pr.z_at, {n: (f, rp, rq) for n, (pk, f, rp, rq) in self.planks.items()})

    # ------------------------------------------------------------------ geometry
    def height(self, pos) -> float:
        """A point's height over the snow, along the snow's normal under it."""
        x = float(pos[0])
        return (float(pos[2]) - float(self.pr.z_at(x))) * math.cos(self.pr.slope_at(x))

    def snow_normal(self, x) -> np.ndarray:
        a = self.pr.slope_at(float(x))
        return np.array([-math.sin(a), 0.0, math.cos(a)])

    def finish_x(self):
        return self.pr.marks["outrun_start"][0] + self.p["finish_m"]

    # ------------------------------------------------------------------ the referee
    def referee_reset(self, w, ref):
        m = w.m
        self.ss = SnowModel(w, {n: v[0] for n, v in self.planks.items()}, self.snow)
        self.ski_body = {n: m.body(n).id for n in self.planks}
        self.snow_mask = ref.geoms(names=self.snow)
        self.feet_mask = ref.part("feet")
        self.ski_mask = np.zeros(m.ngeom, bool)
        for n in self.planks:
            self.ski_mask |= ref.geoms(body=n)
        self.trackers = {"phase": "platform", "air_k": 0, "rot": [0.0, 0.0, 0.0], "min_up": 1.0, "peak": 0.0,
                         "flight": None, "slow_k": 0, "k_start": None, "result": None,
                         "down_s": 0.0, "low": None,
                         "head": [self.ski_yaw(w, b) for b in self.ski_body.values()], "head0": None,
                         "land_ph": None, "land_dir": 0.0, "head_ref": None, "ride_turn": 0.0,
                         "snow": Digest(w, list(self.ss.bodies))}
        self.base_deg = float(self.p["base_deg"])
        self.ss.apply(w.d)

    def omega(self, w) -> np.ndarray:
        d, r = w.d, w.robot
        return d.xmat[r.pelvis].reshape(3, 3) @ d.qvel[r.fv + 3:r.fv + 6]

    @staticmethod
    def ski_yaw(w, b):
        """A ski's heading: the angle of its long axis (tail to tip) in the horizontal plane from the course's
        direction (+x), or None while the ski points within 10 degrees of the vertical."""
        x = w.d.xmat[b].reshape(3, 3)[:, 0]
        if math.hypot(float(x[0]), float(x[1])) < HEADING_MIN_XY:
            return None
        return math.atan2(float(x[1]), float(x[0]))

    def track_heading(self, w) -> None:
        """Each ski's heading, unwrapped (followed continuously every 1 ms step, so whole turns add up); held while a
        ski has no heading."""
        hd = self.trackers["head"]
        for i, b in enumerate(self.ski_body.values()):
            h = self.ski_yaw(w, b)
            if h is None:
                continue
            hd[i] = h if hd[i] is None else hd[i] + wrap_pi(h - hd[i])

    def ski_heading_ok(self, w, direction=0.0, tol_deg=None) -> bool:
        """Both skis point within tol_deg (default ride_heading_deg) of `direction` (0 forwards, pi backwards)."""
        tol = math.radians(self.p["ride_heading_deg"] if tol_deg is None else tol_deg)
        for b in self.ski_body.values():
            h = self.ski_yaw(w, b)
            if h is None or abs(wrap_pi(h - direction)) > tol:
                return False
        return True

    def substep(self, w, ref):
        self.ss.apply(w.d)
        tr, d, r, p = self.trackers, w.d, w.robot, self.p
        dt = w.m.opt.timestep
        self.track_heading(w)
        ski_on = ref.touching(self.ski_mask, self.snow_mask)
        body_on = ref.parts_touching(self.snow_mask) - set(self.legal_ground)
        any_on = ski_on or bool(body_on) or ref.touching(self.feet_mask, self.snow_mask)
        # the rotation since the last contact with the snow (a flight is confirmed after air_ms without one)
        if any_on:
            tr["air_k"], tr["rot"], tr["min_up"] = 0, [0.0, 0.0, 0.0], 1.0
            tr["peak"] = float(w.com()[2])
        else:
            if tr["air_k"] == 0:
                v = d.qvel[r.fv:r.fv + 3].copy()
                tr["v0"] = [float(x) for x in v]
                tr["x0"] = float(d.xpos[r.pelvis][0])
                tr["k0"] = ref.ph
                tr["head0"] = list(tr["head"])
            tr["air_k"] += 1
            tr["rot"] = [a + b * dt for a, b in zip(tr["rot"], self.omega(w))]
            tr["min_up"] = min(tr["min_up"], float(d.xmat[r.pelvis].reshape(3, 3)[2, 2]))
            tr["peak"] = max(tr["peak"], float(w.com()[2]))
        ph = tr["phase"]
        if body_on and ph != "done":
            pts = ref.contact_points(ref.part(*sorted(body_on)), self.snow_mask)
            if tr["flight"] is not None and ph == "air":           # the body first: no landing
                return ref.fail("landed body first", on_the_snow=sorted(body_on),
                                x=round(float(pts[0][0]), 2) if len(pts) else None)
            return ref.fail("fell", on_the_snow=sorted(body_on))
        if ph == "platform":
            if min(float(d.xpos[b][0]) for b in self.ski_body.values()) > self.pr.marks["start_edge"][0]:
                ref.stage("start")
                tr["phase"], tr["k_start"] = "inrun", ref.ph
        elif ph == "inrun":
            lip_x = self.pr.marks["lip"][0]
            if tr["air_k"] >= p["air_ms"]:
                v0 = np.array(tr["v0"])
                ang = math.degrees(math.atan2(v0[2], math.hypot(v0[0], v0[1])))
                if abs(tr["x0"] - lip_x) < p["lip_window_m"] and ang > p["min_angle_deg"]:
                    h = np.array([v0[0], v0[1], 0.0])
                    h /= max(float(np.linalg.norm(h)), 1e-9)
                    f = FlightRec(tr["k0"], tr["x0"], float(np.linalg.norm(v0)), v0,
                                  (np.array([-h[1], h[0], 0.0]), h), tr["peak"])
                    tips = [float(d.xmat[b].reshape(3, 3)[:, 0] @ h) for b in self.ski_body.values()]
                    f.switch = bool(np.mean(tips) < 0.0)
                    tr["flight"] = f
                    tr["phase"] = "air"
                    ref.stage("take-off", speed_m_s=round(f.speed0, 2), angle_deg=round(f.angle0, 1))
            if tr["phase"] == "inrun" and float(d.xpos[r.pelvis][0]) > lip_x + p["lip_window_m"]:
                return ref.fail("no jump")
        if tr["phase"] == "air":
            f = tr["flight"]
            if any_on:                                              # the flight is over: the landing
                return self.landing(w, ref, f, ski_on)
            f.rot = np.array(tr["rot"])
            f.min_up = tr["min_up"]
            f.peak = tr["peak"]
        elif tr["phase"] == "landed":
            # no spinning on the snow: from settle_s after the landing to the finish line each ski's heading stays
            # within ride_turn_deg of its heading at that moment
            if tr["head_ref"] is None:
                if (ref.ph - tr["land_ph"]) * dt >= p["settle_s"] - 1e-9:
                    tr["head_ref"] = list(tr["head"])
            else:
                turn = max(abs(a - b) for a, b in zip(tr["head"], tr["head_ref"]))
                tr["ride_turn"] = max(tr["ride_turn"], turn)
                if turn > math.radians(p["ride_turn_deg"]):
                    return ref.fail("spun on the snow", turn_deg=round(math.degrees(turn), 1),
                                    x=round(float(d.xpos[r.pelvis][0]), 2))
            if min(float(d.xpos[b][0]) for b in self.ski_body.values()) > self.finish_x():
                ref.metrics["ride_turn_deg"] = round(math.degrees(tr["ride_turn"]), 1)
                if self.height(d.xpos[r.pelvis]) < p["fall_h_m"]:
                    return ref.fail("not riding away", reason="the pelvis down at the finish line")
                if not self.ski_heading_ok(w, tr["land_dir"]):
                    return ref.fail("not riding away", reason="the skis do not point the trick's way at the finish "
                                                              "line")
                if ref.stage("ride-away"):
                    tr["phase"] = "done"
                    ref.succeed()

    def landing(self, w, ref, f, ski_on):
        tr, d, r = self.trackers, w.d, w.robot
        x = float(d.xpos[r.pelvis][0])
        air = (ref.ph - f.k0) * w.m.opt.timestep
        f.landing = dict(x=round(x, 2), airtime_s=round(air, 3), from_knoll_m=round(x - self.pr.marks["knoll"][0], 2))
        h0, h1 = tr.get("head0"), tr["head"]
        if h0 is not None and None not in h0 and None not in h1:      # (a hand-made flight brings its own spin)
            f.ski_turn = tuple(float(b - a) for a, b in zip(h0, h1))
            f.spin = float(np.mean(f.ski_turn))
        c = classify(f, self.base_deg)
        tr["result"] = c
        for k in ("spin_deg", "flip_deg", "short_deg", "half_turns", "flips", "pelvis_spin_deg"):
            ref.metrics[f"trick_{k}"] = c[k]
        ref.metrics["air_s"] = round(air, 3)
        ref.metrics["height_m"] = round(f.peak - self.pr.marks["lip"][1], 2)
        ref.metrics["from_knoll_m"] = f.landing["from_knoll_m"]
        if not ski_on:
            return ref.fail("landed body first")
        if x < self.pr.marks["knoll_start"][0]:
            return ref.fail("landed on the table", x=round(x, 2))
        if not self.is_trick(c):
            return ref.fail("wrong trick", trick=c["name"], spin_deg=c["spin_deg"], flip_deg=c["flip_deg"])
        # the skis point the way the family's trick lands: forwards after a whole number of turns, backwards after an
        # odd number of half turns (a switch take-off the other way round; a flip family: its measured half turns)
        n180 = c["half_turns"] if self.want[0] is None else self.want[0]
        tr["land_dir"] = math.pi if (n180 + int(c["switch"])) % 2 else 0.0
        yaws = [self.ski_yaw(w, b) for b in self.ski_body.values()]
        ref.metrics["landing_heading_deg"] = [None if h is None else round(math.degrees(wrap_pi(h - tr["land_dir"])), 1)
                                              for h in yaws]
        if not self.ski_heading_ok(w, tr["land_dir"], self.p["land_heading_deg"]):
            return ref.fail("landed facing the wrong way", ski_heading_deg=ref.metrics["landing_heading_deg"],
                            want="backwards" if tr["land_dir"] else "forwards")
        tr["phase"], tr["land_ph"] = "landed", ref.ph
        ref.stage("landing", trick=c["name"], spin_deg=c["spin_deg"], flip_deg=c["flip_deg"], air_s=round(air, 3))

    def is_trick(self, c) -> bool:
        ht, fl, fdir = self.want
        if c["switch"] or c["flips"] != fl:
            return False
        if fl == 0:
            return c["half_turns"] == ht
        if (c["flip_deg"] < 0) != (fdir == "back"):
            return False
        return c["half_turns"] <= 1 if ht is None else c["half_turns"] == ht

    def step(self, w, ref):
        p, tr = self.p, self.trackers
        if ref.failed or ref.success:
            return
        d, r = w.d, w.robot
        pel = d.xpos[r.pelvis]
        if tr["phase"] == "platform":
            if w.t >= p["dns_s"]:
                return ref.fail("did not start", t_s=round(w.t, 2))
            return
        # down on the skis (freeski's fall): on the snow with the pelvis low or the trunk tipped over; a fall when the
        # time down adds up to fall_s (it decays at a quarter of the rate while up: a robot that bounces is still down)
        on = ref.touching(self.ski_mask, self.snow_mask) or ref.touching(self.feet_mask, self.snow_mask)
        down = False
        if on and tr["phase"] != "air":
            h = self.height(pel)
            nrm = self.snow_normal(pel[0])
            up = d.xmat[r.torso].reshape(3, 3)[:, 2]
            tilt = math.degrees(math.acos(float(np.clip(up @ nrm, -1, 1))))
            down = h < p["fall_h_m"] or tilt > p["tilt_max_deg"]
            if down:
                tr["low"] = [round(h, 3), round(tilt, 1)]
        dt = w.CONTROL_DT
        tr["down_s"] = round(tr["down_s"] + dt if down else max(0.0, tr["down_s"] - 0.25 * dt), 6)
        if tr["down_s"] >= p["fall_s"] - 1e-9:
            return ref.fail("fell", pelvis_height_m=tr["low"][0], torso_tilt_deg=tr["low"][1])
        v = float(np.linalg.norm(d.qvel[r.fv:r.fv + 3]))
        ref.metrics["speed_m_s"] = round(v, 3)
        tr["slow_k"] = tr["slow_k"] + 1 if v < p["stop_speed_m_s"] else 0
        if tr["slow_k"] * w.CONTROL_DT >= p["stop_s"]:
            return ref.fail("stopped", speed_m_s=round(v, 3), x=round(float(pel[0]), 2))

    def privileged(self, w):
        pr, d = self.pr, w.d
        pel = d.xpos[w.robot.pelvis]
        return {"pelvis": w.pelvis().tolist(), "pelvis_height_m": self.height(pel),
                "phase": self.trackers.get("phase"), "marks": {k: list(v) for k, v in pr.marks.items()},
                "finish_x": self.finish_x(), "air_rotation_rad": list(self.trackers.get("rot", [0, 0, 0])),
                "ski_heading_unwrapped_rad": list(self.trackers.get("head", [])),
                "ski_heading_at_takeoff_rad": self.trackers.get("head0"),
                "skis": {n: {"pos": d.xpos[b].tolist(), "xmat": d.xmat[b].tolist()} for n, b in self.ski_body.items()},
                "snow_model": dict(self.ss.last)}
# ====================================================================================== END freeski block


class Task(FreeskiTask):
    name = "freeski-straight-air"
    title = "Freeski big air: straight air"
    trick = "straight air"
    want = (0, 0, None)
    trick_rule = ("the straight air: no spin (your skis turn less than 90 degrees about the vertical in the "
                  "air) and no flip.")