"""Detailed single-lane commercial skee-ball cabinet. Run with Blender's Python."""
import math
import os
import sys

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import v4lib as v4

v4.reset()

# Materials
red = v4.painted("arcade_red_enamel", (0.64, 0.035, 0.055), roughness=0.28)
dark = v4.painted("charcoal_powdercoat", (0.055, 0.065, 0.075), roughness=0.42)
black = v4.rubber("black_grip_rubber", (0.018, 0.022, 0.024))
steel = v4.metal("brushed_stainless", color=(0.72, 0.76, 0.78), roughness=0.28, brushed=True)
bright = v4.metal("polished_chrome", color=(0.88, 0.91, 0.93), roughness=0.17, brushed=False)
wood = v4.wood("maple_lane", light=(0.78, 0.58, 0.33), dark=(0.48, 0.28, 0.12), grain_scale=7)
wood_edge = v4.wood("maple_endgrain", light=(0.63, 0.40, 0.20), dark=(0.34, 0.19, 0.09), grain_scale=10)
gold = v4.metal("score_ring_brass", color=(0.82, 0.48, 0.12), roughness=0.25, brushed=False)
glass = v4.glass("smoked_target_backing", (0.018, 0.025, 0.035), roughness=0.22)
white = v4.plastic("ivory_print", (0.92, 0.86, 0.68), roughness=0.45)
led_red = v4.emissive("red_score_led", (1.0, 0.015, 0.008), strength=3.0)
led_amber = v4.emissive("amber_lamps", (1.0, 0.28, 0.015), strength=2.0)
ball_orange = v4.plastic("return_ball_orange", (0.98, 0.19, 0.018), roughness=0.22)
ball_blue = v4.plastic("collection_ball_blue", (0.025, 0.20, 0.72), roughness=0.24)
lane_ink = v4.painted("lane_marking_amber", (0.73, 0.39, 0.10), roughness=0.32)
guard_glass = v4.glass("clear_safety_guard", (0.40, 0.68, 0.78), roughness=0.12)

# Main proportions (metres): player at -Y; raised targets at +Y.
W = 0.90
FRAME = v4.body("cabinet")
lane_center = (0, -0.015, 0.872)
lane_rot = (0.205, 0, 0)

def bx(name, size, loc, mat, bevel=0.003, body=FRAME, rot=(0, 0, 0)):
    return v4.box(name, size, loc, rot=rot, bevel=bevel, material=mat, body=body)

def cyl(name, radius, depth, loc, mat, body=FRAME, rot=(0, 0, 0), bevel=0.0008, segments=32):
    return v4.cylinder(name, radius, depth, loc, rot=rot, segments=segments, bevel=bevel, material=mat, body=body)

# Floor-standing cabinet shell, feet, plinth and service panels.
bx("lower_cabinet_core", (0.84, 2.40, 0.40), (0, 0.0, 0.25), dark, 0.014)
bx("front_kick_plate", (0.82, 0.035, 0.20), (0, -1.167, 0.16), steel, 0.006)
bx("base_plinth", (0.88, 2.40, 0.09), (0, 0.0, 0.045), black, 0.018)
for sx in (-1, 1):
    bx(f"base_side_molding_{sx}", (0.036, 2.36, 0.095), (sx * 0.431, 0, 0.13), dark, 0.012)
for sx in (-1, 1):
    x = sx * 0.418
    # Raised side guard follows the lane pitch and meets the scoring tower.
    # The rear ends continue to the solid target bulkhead as cabinet cheeks.
    y0, y1, zbottom = -1.20, 1.78, 0.24
    lane_top = lambda yy: lane_center[2] + (yy - lane_center[1]) * math.sin(lane_rot[0]) + 0.023
    front_top = lane_top(y0) + 0.050
    # Rise an additional 350 mm at the rear to meet the inclined target side and pedestal.
    rear_top = lane_top(y1) + 0.400
    guard_pitch = math.atan2(rear_top - front_top, y1 - y0)
    profile = [(y0, zbottom), (y1, zbottom), (y1, rear_top), (y0, front_top)]
    # Bring the inside faces flush to the target ramp's +/-400 mm side edges.
    x0 = -0.412 if sx < 0 else 0.412
    v4.extrude(f"red_side_skin_{sx}", profile, 0.012, loc=(x0, 0, 0),
               rot=(math.pi / 2, 0, math.pi / 2), bevel=0.003, material=red, body=FRAME)
    top_mid = (front_top + rear_top) / 2
    bx(f"side_top_trim_{sx}", (0.018, y1 - y0, 0.018), (x, (y0 + y1) / 2, top_mid),
       bright, 0.003, rot=(guard_pitch, 0, 0))
    bx(f"lower_side_seam_{sx}", (0.004, 1.92, 0.003), (sx * 0.419, -0.03, 0.31), steel, 0.001)
    # Long brushed rails and four adjustable rubber feet per side.
    bx(f"lane_edge_rail_{sx}", (0.042, 2.24, 0.075), (sx * 0.353, -0.01, 0.9325), dark, 0.018, rot=(0.20, 0, 0))
    bx(f"rail_inlay_{sx}", (0.018, 2.10, 0.006), (sx * 0.353, -0.01, 0.969), bright, 0.003, rot=(0.20, 0, 0))
    for yy in (-0.91, 0.91):
        cyl(f"foot_mount_{sx}_{yy}", 0.034, 0.045, (sx * 0.34, yy, 0.012), steel)
        cyl(f"leveling_foot_{sx}_{yy}", 0.031, 0.028, (sx * 0.34, yy, -0.012), black)
    side_face_x = 0.4245 if sx > 0 else -0.4125
    side_face_rot = (math.pi / 2, 0, sx * math.pi / 2)
    v4.text(f"side_brand_{sx}", "SKEE-BALL", 0.095,
            loc=(side_face_x, -0.08, 0.62), rot=side_face_rot,
            extrude_depth=0.001, material=white, body=FRAME)
    for gi, yy in enumerate((-0.54, 0.38)):
        bx(f"side_art_deco_stripe_{sx}_{gi}", (0.004, 0.64, 0.014),
           (side_face_x, yy, 0.42), gold if gi == 0 else white, 0.002,
           rot=(guard_pitch, 0, 0))

# Clear acrylic side guards protect the launch half and target approach without hiding the lane.
guard_y, guard_len = 0.55, 0.78
guard_z = lane_top(guard_y) + 0.145
for sx in (-1, 1):
    gx = sx * 0.387
    bx(f"upper_lane_clear_guard_{sx}", (0.008, guard_len, 0.25),
       (gx, guard_y, guard_z), guard_glass, 0.003, rot=lane_rot)
    for zoff in (-0.116, 0.116):
        bx(f"clear_guard_rail_{sx}_{zoff}", (0.014, guard_len, 0.014),
           (sx * 0.388, guard_y, guard_z + zoff), bright, 0.003, rot=lane_rot)
    for yy in (guard_y - guard_len / 2 + 0.015, guard_y + guard_len / 2 - 0.015):
        for zoff in (-0.10, 0.10):
            inner_x = sx * 0.392
            outer_x = sx * 0.416
            z = guard_z + (yy - guard_y) * math.sin(lane_rot[0]) + zoff
            v4.rod(f"guard_stanchion_{sx}_{yy}_{zoff}", (inner_x, yy, z),
                   (outer_x, yy, z), 0.005, material=steel, body=FRAME)

# Lower front fascia and coin-door frame, fixed to the cabinet.
bx("front_fascia", (0.84, 0.035, 0.54), (0, -1.175, 0.405), red, 0.009)
bx("coin_frame_outer", (0.56, 0.035, 0.48), (0, -1.198, 0.39), steel, 0.009)
bx("coin_frame_recess", (0.49, 0.012, 0.415), (0, -1.217, 0.39), dark, 0.004)
# Four frame borders fit around (rather than overlap) the inset movable door.
for nm, sz, loc in (
    ("top", (0.52, 0.016, 0.026), (0, -1.231, 0.607)),
    ("bottom", (0.52, 0.016, 0.026), (0, -1.231, 0.173)),
    # Recess the hinge-side border behind the external links for knuckle clearance.
    ("left", (0.018, 0.016, 0.408), (-0.232, -1.215, 0.39)),
    ("right", (0.026, 0.016, 0.408), (0.232, -1.231, 0.39)),
):
    bx("coin_frame_" + nm, sz, loc, bright, 0.003)

# Inclined maple playing lane, mounted above the cabinet shell.
bx("maple_play_lane", (0.65, 2.20, 0.046), lane_center, wood, 0.012, rot=lane_rot)
bx("lane_front_cap", (0.68, 0.075, 0.025), (0, -1.105, 0.664), wood_edge, 0.008, rot=lane_rot)
# Solid sloped bed supports the entire maple deck.
bed_center_z = lane_center[2] - 0.023 - 0.18 + 0.004
bx("solid_sloped_lane_bed", (0.65, 2.18, 0.36),
   (0, lane_center[1], bed_center_z), dark, 0.008, rot=lane_rot)
# A broad, rounded hardwood jump at the regulation launch point breaks the long flat run.
jump_profile = [
    (0.14, lane_top(0.14) - 0.020), (0.77, lane_top(0.77) - 0.020),
    (0.77, lane_top(0.77) + 0.002), (0.68, lane_top(0.68) + 0.014),
    (0.59, lane_top(0.59) + 0.035), (0.50, lane_top(0.50) + 0.064),
    (0.42, lane_top(0.42) + 0.078), (0.34, lane_top(0.34) + 0.064),
    (0.25, lane_top(0.25) + 0.034), (0.18, lane_top(0.18) + 0.012),
    (0.14, lane_top(0.14)),
]
v4.extrude("maple_ball_jump", jump_profile, 0.48, loc=(-0.24, 0, 0),
           rot=(math.pi / 2, 0, math.pi / 2), bevel=0.018, material=wood, body=FRAME)
# Subtle aiming inlays and transverse foul/launch markers are flush with the maple surface.
for sx in (-1, 1):
    bx(f"lane_aim_inlay_{sx}", (0.002, 1.92, 0.002),
       (sx * 0.245, -0.01, lane_top(-0.01) + 0.0005), lane_ink, 0.0008, rot=lane_rot)
for i, yy in enumerate((-0.68, -0.25, 0.06, 0.83)):
    bx(f"lane_target_mark_{i}", (0.40, 0.005, 0.002), (0, yy, lane_top(yy) + 0.0005),
       lane_ink, 0.0008, rot=lane_rot)
# Inclined 45-degree scoring ramp with five open, stepped scoring cups.
target_pitch = math.radians(45)
target_rot = (target_pitch, 0, 0)
target_center = (0, 1.32, 1.346)
target_normal = (0, -math.sin(target_pitch), math.cos(target_pitch))

def target_point(x, v, front=0.0):
    """Point on the target ramp; v runs uphill toward the score hood."""
    return (x,
            target_center[1] + v * math.cos(target_pitch) - front * math.sin(target_pitch),
            target_center[2] + v * math.sin(target_pitch) + front * math.cos(target_pitch))

target_backplate = bx("target_backplate", (0.80, 0.78, 0.048), target_center, red, 0.008, rot=target_rot)
target_insert = bx("target_dark_insert", (0.76, 0.74, 0.014), target_point(0, 0, 0.031),
                   glass, 0.003, rot=target_rot)
# Full-width rear bulkhead sits behind the inclined scoring ramp and supports the score hood.
target_bulkhead = bx("rear_target_pedestal_bulkhead", (0.84, 0.18, 1.90),
                     (0, 1.75, 0.97), dark, 0.012)
target_defs = [
    (0.00, -0.08, 0.282, "10"),
    (0.00, 0.275, 0.074, "50"),
    (-0.300, 0.275, 0.044, "100"), (0.300, 0.275, 0.044, "100"),
]
# The scoring cups stop at their inclined face; the solid rear bulkhead closes their backs.
target_holes = []
for i, (x, v, outer_r, score) in enumerate(target_defs):
    hole_r = outer_r - 0.025
    target_holes.append((x, v, outer_r, hole_r, score))
    # Matching round apertures through the red ramp and smoked insert form real pockets.
    cutter_a = v4.cylinder(f"target_ramp_cut_{i}", hole_r, 0.16, target_point(x, v),
                           rot=target_rot, bevel=0, material=None, body=None)
    v4.boolean(target_backplate, cutter_a)
    insert_hole_r = 0.065 if score == "10" else hole_r
    cutter_b = v4.cylinder(f"target_insert_cut_{i}", insert_hole_r, 0.16, target_point(x, v, 0.031),
                           rot=target_rot, bevel=0, material=None, body=None)
    v4.boolean(target_insert, cutter_b)
    ring_loc = target_point(x, v, 0.041)
    v4.torus(f"target_cup_{score}_{i}", outer_r, 0.010, loc=ring_loc, rot=target_rot,
             segments=56, ring_segments=14, material=gold if score in ("10", "50") else bright, body=FRAME)
    v4.torus(f"target_cup_lip_{score}_{i}", hole_r + 0.003, 0.003, loc=ring_loc, rot=target_rot,
             segments=40, ring_segments=8, material=gold, body=FRAME)
# Concentric stepped scoring rings mark the 20, 30 and 40 bowls inside the broad 10-point catcher.
for score, radius, mat in (("20", 0.220, bright), ("30", 0.158, gold), ("40", 0.096, bright)):
    v4.torus(f"target_nested_ring_{score}", radius, 0.010,
             loc=target_point(0, -0.08, 0.041), rot=target_rot,
             segments=56, ring_segments=14, material=mat, body=FRAME)
# Raised enamel score plaques follow the scoring bands and the top/corner pockets.
for label, x, v, width, height, size in (
    ("10", 0.00, -0.340, 0.112, 0.064, 0.052),
    ("20", -0.320, -0.210, 0.100, 0.064, 0.052),
    ("30", -0.320, -0.090, 0.100, 0.064, 0.052),
    ("40", -0.320, 0.030, 0.100, 0.064, 0.052),
    ("50", 0.000, 0.175, 0.100, 0.056, 0.050),
    ("100", -0.300, 0.345, 0.096, 0.046, 0.038),
    ("100", 0.300, 0.345, 0.096, 0.046, 0.038),
):
    p = target_point(x, v, 0.038)
    bx(f"target_score_badge_frame_{label}_{x}_{v}", (width, height, 0.008),
       p, gold, 0.006, rot=target_rot)
    p_in = target_point(x, v, 0.041)
    bx(f"target_score_badge_{label}_{x}_{v}", (width - 0.008, height - 0.008, 0.004),
       p_in, black, 0.004, rot=target_rot)
    v4.text(f"target_value_{label}_{x}_{v}", label, size,
            loc=target_point(x, v, 0.042), rot=target_rot, extrude_depth=0.003,
            material=led_amber, body=FRAME)
# Bright perimeter rails follow the incline of the scoring ramp.
for sx in (-1, 1):
    bx(f"target_side_trim_{sx}", (0.018, 0.78, 0.018), target_point(sx * 0.39, 0, 0.046),
       bright, 0.003, rot=target_rot)
for sv in (-1, 1):
    bx(f"target_end_trim_{sv}", (0.80, 0.018, 0.018), target_point(0, sv * 0.38, 0.046),
       bright, 0.003, rot=target_rot)

# Behind the score openings, a shallow collector pan carries balls down toward a return chute.
catch_rot = (0.18, 0, 0)
catch_center = (0, 1.43, 0.93)
bx("scoring_ball_catch_pan", (0.68, 0.48, 0.022), catch_center, steel, 0.008, rot=catch_rot)
for sx in (-1, 1):
    bx(f"catch_pan_side_lip_{sx}", (0.024, 0.47, 0.075),
       (sx * 0.326, 1.43, 0.978), dark, 0.006, rot=catch_rot)
bx("catch_pan_front_stop", (0.66, 0.018, 0.07), (0, 1.194, 0.90), steel, 0.004, rot=catch_rot)

# Exposed open-topped return trough runs outside the right guard, from the collection tray to the target end.
trough_x, trough_y, trough_z = 0.52, -0.10, 0.76
trough_pitch = 0.06
trough_rot = (trough_pitch, 0, 0)
trough_len = 2.20
trough_floor = bx("return_trough_floor", (0.115, trough_len, 0.018),
                  (trough_x, trough_y, trough_z), steel, 0.005, rot=trough_rot)
for side, x in (("inner", 0.471), ("outer", 0.569)):
    bx(f"return_trough_side_{side}", (0.016, trough_len, 0.070),
       (x, trough_y, trough_z + 0.030), dark, 0.004, rot=trough_rot)
    bx(f"return_trough_lip_{side}", (0.004, trough_len - 0.025, 0.005),
       (x, trough_y, trough_z + 0.064), bright, 0.002, rot=trough_rot)
bx("return_trough_target_stop", (0.115, 0.018, 0.075),
   (trough_x, trough_y + trough_len / 2, trough_z + 0.034), steel, 0.004, rot=trough_rot)
# Short brackets fasten the exposed channel to the cabinet side skin.
for i, yy in enumerate((-0.87, -0.42, 0.03, 0.48, 0.88)):
    floor_z = trough_z + (yy - trough_y) * math.sin(trough_pitch) - 0.012
    v4.rod(f"return_trough_bracket_{i}", (0.423, yy, floor_z), (0.464, yy, floor_z),
           0.006, material=steel, body=FRAME)

# A framed outer sidecar encloses the return trough; a clear window keeps the moving ball visible.
fair_x = 0.600
bx("return_sidecar_lower_shell", (0.035, 2.34, 0.34), (fair_x, -0.04, 0.40), red, 0.012)
bx("return_sidecar_lower_bead", (0.042, 2.30, 0.025), (fair_x, -0.04, 0.59), dark, 0.008)
bx("return_sidecar_upper_beam", (0.035, 2.34, 0.050), (fair_x, -0.04, 0.987), dark, 0.010)
bx("return_sidecar_glazing", (0.010, 1.92, 0.34), (fair_x + 0.019, -0.03, 0.785),
   guard_glass, 0.006)
for yy in (-1.18, 1.10):
    bx(f"return_sidecar_end_post_{yy}", (0.035, 0.075, 0.42), (fair_x, yy, 0.77), dark, 0.010)
for yy in (-1.00, 0.94):
    bx(f"return_window_end_trim_{yy}", (0.026, 0.025, 0.36),
       (fair_x + 0.020, yy, 0.785), bright, 0.004)
# Cross ties fasten the glazing frame to the outer trough wall inside the sidecar.
for i, yy in enumerate((-0.82, -0.36, 0.10, 0.56, 0.92)):
    for zz in (0.70, 0.86):
        v4.rod(f"sidecar_return_tie_{i}_{zz}", (0.571, yy, zz), (0.625, yy, zz),
               0.006, material=steel, body=FRAME)
# Art-deco striping and product mark finish the integrated return-side fairing.
for i, yy in enumerate((-0.44, 0.44)):
    bx(f"sidecar_graphic_stripe_{i}", (0.004, 1.48, 0.018), (fair_x + 0.019, yy, 0.43),
       gold if i == 0 else white, 0.003)
v4.text("sidecar_brand", "SKEE-BALL", 0.090, loc=(fair_x + 0.017, 0.12, 0.40),
        rot=(math.pi / 2, 0, math.pi / 2), extrude_depth=0.001, material=white, body=FRAME)

# Built-in ball collection tray opens from the enclosed sidecar at the cabinet front.
tray_center = (0.60, -1.28, trough_z + (-1.28 - trough_y) * math.sin(trough_pitch))
bx("built_in_return_tray_pod", (0.39, 0.30, 0.23), (0.60, -1.33, 0.57), red, 0.026)
bx("return_tray_pod_chrome_seam", (0.394, 0.018, 0.012), (0.60, -1.478, 0.60), bright, 0.004)
bx("ball_collection_floor", (0.40, 0.38, 0.018), tray_center, steel, 0.005, rot=trough_rot)
for sx in (-1, 1):
    bx(f"ball_collection_rail_{sx}", (0.016, 0.38, 0.070),
       (tray_center[0] + sx * 0.192, tray_center[1], tray_center[2] + 0.030), dark, 0.004, rot=trough_rot)
bx("ball_collection_front_gate", (0.40, 0.018, 0.070),
   (tray_center[0], tray_center[1] - 0.171, tray_center[2] + 0.030), steel, 0.004, rot=trough_rot)
for i, (xx, yy) in enumerate(((0.48, -1.34), (0.56, -1.34), (0.64, -1.34), (0.72, -1.34),
                              (0.52, -1.23), (0.60, -1.23), (0.68, -1.23))):
    zz = tray_center[2] + (yy - tray_center[1]) * math.sin(trough_pitch) + 0.007 + 0.038
    v4.sphere(f"collected_ball_{i}", 0.038, loc=(xx, yy, zz), segments=40, material=ball_orange, body=FRAME)
# A recessed framed outlet makes the return read as part of the front cabinet, not an add-on tray.
for sx in (-1, 1):
    bx(f"return_outlet_jamb_{sx}", (0.018, 0.025, 0.19),
       (0.60 + sx * 0.175, -1.215, 0.77), bright, 0.004)
for z in (0.675, 0.865):
    bx(f"return_outlet_header_{z}", (0.35, 0.025, 0.018), (0.60, -1.215, z), bright, 0.004)
bx("return_outlet_dark_recess", (0.31, 0.012, 0.16), (0.60, -1.202, 0.77), black, 0.004)

# Score hood above the target panel. Its bezel, score window, LED digits and controls are fixed.
hood_shell = bx("score_hood_shell", (0.88, 0.27, 0.28), (0, 1.75, 2.025), dark, 0.035)
bx("hood_red_crown", (0.85, 0.22, 0.045), (0, 1.75, 2.185), red, 0.018)
bx("hood_marquee_face", (0.66, 0.018, 0.066), (0, 1.606, 2.130), black, 0.006)
bx("hood_marquee_liner", (0.68, 0.008, 0.008), (0, 1.594, 2.095), gold, 0.002)
v4.text("hood_marquee_title", "SKEE-BALL", 0.046, loc=(0, 1.597, 2.132),
        rot=(math.pi / 2, 0, 0), extrude_depth=0.001, material=white, body=FRAME)
bx("score_window_bezel", (0.58, 0.018, 0.118), (0, 1.616, 2.045), bright, 0.012)
bx("score_window", (0.545, 0.010, 0.085), (0, 1.603, 2.045), black, 0.006)
# Four compact seven-segment LED numerals sit on the smoked display glass.
for di, cx in enumerate((-0.195, -0.125, 0.125, 0.195)):
    zc = 2.045
    for seg, (sz, dx, dz) in enumerate((
        ((0.040, 0.008, 0.005), 0, 0.030),
        ((0.040, 0.008, 0.005), 0, 0),
        ((0.040, 0.008, 0.005), 0, -0.030),
        ((0.005, 0.008, 0.022), -0.020, 0.015),
        ((0.005, 0.008, 0.022), 0.020, 0.015),
        ((0.005, 0.008, 0.022), -0.020, -0.015),
        ((0.005, 0.008, 0.022), 0.020, -0.015),
    )):
        bx(f"score_digit_{di}_seg_{seg}", sz, (cx + dx, 1.597, zc + dz), led_red, 0.001)
bx("hood_front_lip", (0.90, 0.025, 0.025), (0, 1.620, 1.91), red, 0.008)
for sx in (-1, 1):
    bx(f"hood_side_trim_{sx}", (0.018, 0.24, 0.02), (sx * 0.439, 1.75, 2.03), bright, 0.006)

# Slot, coin cup and latch details on the hinged service door are part of that moving body.
door = v4.body("coin_door")
door_center = (0, -1.238, 0.39)
v4.box("door_panel", (0.438, 0.022, 0.374), door_center, bevel=0.009, material=dark, body=door)
for x in (-0.207, 0.207):
    v4.box(f"door_border_side_{x}", (0.008, 0.008, 0.35), (x, -1.251, 0.39), bevel=0.002, material=steel, body=door)
for z in (0.213, 0.567):
    v4.box(f"door_border_rail_{z}", (0.414, 0.008, 0.008), (0, -1.251, z), bevel=0.002, material=steel, body=door)
v4.box("coin_slot_recess", (0.105, 0.004, 0.026), (0, -1.252, 0.485), bevel=0.004, material=black, body=door)
v4.box("coin_slot_lip", (0.11, 0.005, 0.005), (0, -1.256, 0.501), bevel=0.002, material=bright, body=door)
v4.text("coin_price_label", "25c / PLAY", 0.018, loc=(0, -1.248, 0.444),
        rot=(math.pi / 2, 0, 0), extrude_depth=0.003, material=white, body=door)
v4.box("ticket_slot_recess", (0.132, 0.006, 0.020), (-0.105, -1.252, 0.365),
       bevel=0.004, material=black, body=door)
v4.box("ticket_slot_lip", (0.142, 0.006, 0.006), (-0.105, -1.257, 0.378),
       bevel=0.002, material=bright, body=door)
v4.text("ticket_label", "TICKETS", 0.014, loc=(-0.105, -1.248, 0.398),
        rot=(math.pi / 2, 0, 0), extrude_depth=0.003, material=white, body=door)
cyl("coin_return_cup", 0.040, 0.015, (0, -1.257, 0.287), steel, body=door, rot=(math.pi/2, 0, 0))
bx_door = v4.box("coin_return_dark", (0.060, 0.005, 0.027), (0, -1.267, 0.286), bevel=0.006, material=black, body=door)
v4.box("door_latch", (0.022, 0.014, 0.068), (0.157, -1.254, 0.39), bevel=0.004, material=bright, body=door)
hinge_axis = (-0.260, -1.243, 0.39)
for i, z in enumerate((0.235, 0.545)):
    cyl(f"door_hinge_knuckle_{i}", 0.014, 0.050, (hinge_axis[0], hinge_axis[1], z), bright,
        body=door, segments=24)
    v4.rod(f"door_hinge_link_{i}", (-0.218, -1.238, z), (hinge_axis[0], hinge_axis[1], z),
           0.007, material=steel, body=door)
v4.joint("coin_door_hinge", "revolute", FRAME, door,
         origin=hinge_axis, axis=(0, 0, 1), limits=(-1.30, 0))

# Start button is a separate assembly and retracts 12 mm into the front of the hood.
button = v4.body("start_button")
button_x, button_y, button_z = 0.365, 1.618, 2.055
# Cut a blind clearance cavity through the hood's front wall for the button stroke.
button_bore = v4.cylinder("button_bore_cutter", 0.069, 0.30, (button_x, 1.74, button_z),
                          rot=(math.pi/2, 0, 0), bevel=0, material=None, body=None)
v4.boolean(hood_shell, button_bore)
v4.torus("button_grommet", 0.066, 0.006, loc=(button_x, 1.623, button_z),
         rot=(math.pi/2, 0, 0), material=bright, body=FRAME)
cyl("button_mount_socket", 0.060, 0.018, (button_x, button_y, button_z), bright, body=button, rot=(math.pi/2, 0, 0))
cyl("button_dark_bezel", 0.050, 0.020, (button_x, button_y - 0.016, button_z), black, body=button, rot=(math.pi/2, 0, 0))
cyl("start_button_cap", 0.038, 0.026, (button_x, button_y - 0.034, button_z), red, body=button, rot=(math.pi/2, 0, 0))
v4.joint("start_button_slide", "prismatic", FRAME, button,
         origin=(button_x, button_y, button_z), axis=(0, 1, 0), limits=(0, 0.012))

# One visible 76 mm striped ball, seated in the exterior return channel and free to roll.
ball = v4.body("return_ball")
ball_y = -0.36
ball_loc = (trough_x, ball_y,
            trough_z + (ball_y - trough_y) * math.sin(trough_pitch) + 0.009 + 0.038)
v4.sphere("orange_game_ball", 0.038, loc=ball_loc, segments=48, material=ball_orange, body=ball)
# Contrasting equatorial bands are flush, thin spherical details to make rotation legible.
v4.torus("ball_white_equator", 0.036, 0.0025, loc=ball_loc, rot=(0, 0, 0),
         segments=48, ring_segments=8, material=white, body=ball)
v4.torus("ball_gold_equator", 0.036, 0.0016, loc=(ball_loc[0], ball_loc[1], ball_loc[2] + 0.009),
         rot=(0, 0, 0), segments=48, ring_segments=8, material=gold, body=ball)
v4.joint("return_ball_roll", "generic", FRAME, ball,
         origin=ball_loc, axis=(0, -math.sin(trough_pitch), math.cos(trough_pitch)), xdir=(1, 0, 0),
         dofs=[("transY", (-0.35, 0.90)), ("rotX", None)])

# The back of the score post gets a service-panel seam, cooling louvers and fasteners.
bx("rear_post_service_panel", (0.64, 0.014, 0.98), (0, 1.848, 1.02), steel, 0.008)
for sx in (-1, 1):
    bx(f"rear_post_panel_bead_{sx}", (0.012, 0.018, 1.00), (sx * 0.326, 1.857, 1.02), bright, 0.003)
for i in range(7):
    z = 0.88 + i * 0.045
    bx(f"rear_post_vent_{i}", (0.38, 0.018, 0.012), (0, 1.858, z), black, 0.004)
    bx(f"rear_post_vent_lip_{i}", (0.38, 0.008, 0.003), (0, 1.869, z + 0.008), bright, 0.001)
bx("rear_service_badge", (0.19, 0.010, 0.038), (0, 1.852, 0.56), dark, 0.003)
for sx in (-1, 1):
    for z in (0.58, 1.46):
        cyl(f"rear_panel_screw_{sx}_{z}", 0.008, 0.006, (sx * 0.285, 1.858, z), bright,
            rot=(math.pi / 2, 0, 0), segments=16)
bx("rear_lower_kick_closure", (0.84, 0.042, 0.29), (0, 1.19, 0.20), dark, 0.008)

# Small fasteners are mounted into side skins, front fascia, ring panel and hood.
for sx in (-1, 1):
    for yy in (-0.82, -0.34, 0.14, 0.62):
        rivet_x = -0.413 if sx < 0 else 0.419
        cyl(f"skin_rivet_{sx}_{yy}", 0.0055, 0.003, (rivet_x, yy, 0.64), bright,
            rot=(0, math.pi/2, 0), segments=16)

v4.save(os.path.join(os.getcwd(), "asset.blend"))
print("saved asset.blend")
