ghast-physics 0.4.7

Vanilla-exact, pure and deterministic player movement for the ghast Minecraft Java Edition client library (1.8.9 first), verified bit for bit against the real game
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
//! The crosshair block pick of the voxel-shape versions (1.14 on): which block, face and exact
//! point the player's look ray hits (vanilla `BlockGetter.clip` / `BlockView.raycast` with the
//! outline shape and no fluids, as the client's crosshair target uses it).
//!
//! The ray walks the blocks it crosses in order (a 3-D DDA over the segment shortened by `1e-7`
//! at both ends) and stops at the first block whose outline shape it hits. Per block:
//!
//! * if the ray's first test point (`from + (to - from) * 0.001`) lies inside the outline's grid,
//!   the hit is that point, facing against the ray, flagged `inside`;
//! * otherwise each of the shape's boxes is clipped in the shape's own order and the nearest
//!   entry wins (ties keep the earlier box);
//! * a hit is then re-tested against the block's interaction shape: if that one is hit nearer,
//!   the hit keeps its point and block but takes the interaction hit's face.
//!
//! This module is the version-independent part; [`v1_16_5`] and [`v26_1`] add the eye position,
//! the look vector, the range rules and the per-state shapes. 1.16.5 and 26.1 share every step
//! here bit for bit (checked against both oracles: `tests/golden/{1.16.5,26.1}/raytrace.jsonl.gz`).
//!
//! Entities: [`entity_hit`] is the shared entity part (vanilla `ProjectileUtil.getEntityHitResult`
//! / 1.16.5 `ProjectileUtil.raycast`); the version modules combine it with the block pick as the
//! client does (`pick_target`). The caller lists the pickable entities in the world's iteration
//! order (entity sections in order, then insertion order): it only decides between overlapping
//! boxes that both contain the eye, or an entity after one that contains the eye.
//!
//! Not covered: fluid picks (the crosshair never uses them), items with an attack range component
//! (26.1 spears), multipart entities (ender dragon), and block-entity-dependent outlines (shulker
//! boxes are a full cube here; moving pistons have no outline here).

#![cfg_attr(
    not(any(feature = "v1_16_5", feature = "v26_1", feature = "v26_2")),
    allow(dead_code, unused_imports)
)]

use ghast_data::{Aabb, SelectionShape};

#[cfg(any(feature = "v1_8", feature = "v1_12"))]
pub mod legacy;
#[cfg(feature = "v1_12")]
pub mod v1_12;
#[cfg(feature = "v1_16_5")]
pub mod v1_16_5;
#[cfg(feature = "v1_8")]
pub mod v1_8;
#[cfg(feature = "v26_1")]
pub mod v26_1 {
    //! The 26.1 crosshair block pick (vanilla `LocalPlayer.raycastHitResult`, which `Minecraft.pick`
    //! stores as the crosshair target), without entities.
    //!
    //! The block ray runs to `max(block_range, entity_range)` (the `block_interaction_range` and
    //! `entity_interaction_range` attribute values); a block hit farther than `block_range` (or the
    //! miss at the ray's end beyond it) becomes a miss at that point. Verified against the oracle:
    //! `tests/golden/26.1/raytrace.jsonl.gz`.

    use ghast_data::v26_1 as ver;
    include!("modern_body.rs");
}
#[cfg(feature = "v26_2")]
pub mod v26_2 {
    //! The 26.2 crosshair block pick (vanilla `LocalPlayer.raycastHitResult`, which `Minecraft.pick`
    //! stores as the crosshair target), without entities.
    //!
    //! The block ray runs to `max(block_range, entity_range)` (the `block_interaction_range` and
    //! `entity_interaction_range` attribute values); a block hit farther than `block_range` (or the
    //! miss at the ray's end beyond it) becomes a miss at that point. Verified against the oracle:
    //! `tests/golden/26.2/raytrace.jsonl.gz`.

    use ghast_data::v26_2 as ver;
    include!("modern_body.rs");
}

/// A block face / direction, in vanilla's `Direction` order (its ordinal).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Direction {
    /// -Y.
    Down = 0,
    /// +Y.
    Up = 1,
    /// -Z.
    North = 2,
    /// +Z.
    South = 3,
    /// -X.
    West = 4,
    /// +X.
    East = 5,
}

impl Direction {
    /// Every direction in vanilla's order.
    pub const ALL: [Direction; 6] = [
        Direction::Down,
        Direction::Up,
        Direction::North,
        Direction::South,
        Direction::West,
        Direction::East,
    ];

    /// The unit normal.
    #[must_use]
    pub const fn normal(self) -> [i32; 3] {
        match self {
            Direction::Down => [0, -1, 0],
            Direction::Up => [0, 1, 0],
            Direction::North => [0, 0, -1],
            Direction::South => [0, 0, 1],
            Direction::West => [-1, 0, 0],
            Direction::East => [1, 0, 0],
        }
    }

    /// The opposite direction.
    #[must_use]
    pub const fn opposite(self) -> Direction {
        match self {
            Direction::Down => Direction::Up,
            Direction::Up => Direction::Down,
            Direction::North => Direction::South,
            Direction::South => Direction::North,
            Direction::West => Direction::East,
            Direction::East => Direction::West,
        }
    }

    /// Vanilla's lower-case name (`"down"`, `"up"`, ...).
    #[must_use]
    pub const fn name(self) -> &'static str {
        match self {
            Direction::Down => "down",
            Direction::Up => "up",
            Direction::North => "north",
            Direction::South => "south",
            Direction::West => "west",
            Direction::East => "east",
        }
    }

    /// The direction whose normal has the largest dot product with the vector, computed in `f32`
    /// (vanilla `Direction.getApproximateNearest` / 1.16.5 `Direction.getFacing`; `North` when no
    /// dot product is positive).
    #[must_use]
    pub fn approximate_nearest(dx: f64, dy: f64, dz: f64) -> Direction {
        let (x, y, z) = (dx as f32, dy as f32, dz as f32);
        let mut result = Direction::North;
        let mut highest = f32::from_bits(1); // Float.MIN_VALUE
        for d in Direction::ALL {
            let [nx, ny, nz] = d.normal();
            let dot = x * nx as f32 + y * ny as f32 + z * nz as f32;
            if dot > highest {
                highest = dot;
                result = d;
            }
        }
        result
    }
}

/// An entity the crosshair can pick (spectators and entities that are not pickable left out).
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct PickEntity {
    /// The entity's bounding box (world coordinates).
    pub bb: Aabb,
    /// Its pick radius (`getPickRadius` / 1.16.5 `getTargetingMargin`; 0 for almost every entity).
    pub pick_radius: f32,
    /// It has the same root vehicle as the player (the player's own vehicle and its passengers).
    pub same_root_vehicle: bool,
}

/// An entity pick: the index into the caller's entity list and the point on its (inflated) box.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct EntityHit {
    /// Index of the entity in the slice passed to the pick.
    pub index: usize,
    /// The hit point (the eye itself when the eye is inside the box).
    pub location: [f64; 3],
}

/// The crosshair target: a block (or a miss), or an entity.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Target {
    /// A block hit or a miss.
    Block(BlockHit),
    /// An entity.
    Entity(EntityHit),
}

/// Whether a pick hit a block.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum HitKind {
    /// A block was hit.
    Block,
    /// Nothing within range (vanilla `HitResult.Type.MISS`): `location` is the ray's end (or the
    /// out-of-range hit point), `pos` the block containing it, `face` the direction facing back
    /// along the ray.
    Miss,
}

/// A block pick result (vanilla `BlockHitResult`).
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct BlockHit {
    /// Hit or miss.
    pub kind: HitKind,
    /// The block.
    pub pos: [i32; 3],
    /// The face hit (the side the server is told about when using / placing against the block).
    pub face: Direction,
    /// The exact hit point (world coordinates).
    pub location: [f64; 3],
    /// The ray started inside the block's shape.
    pub inside: bool,
}

impl BlockHit {
    /// `true` for a block hit.
    #[must_use]
    pub fn is_block(&self) -> bool {
        self.kind == HitKind::Block
    }

    /// A miss at `location` (vanilla `BlockHitResult.miss`).
    #[must_use]
    pub fn miss(location: [f64; 3], face: Direction) -> BlockHit {
        BlockHit {
            kind: HitKind::Miss,
            pos: location.map(floor),
            face,
            location,
            inside: false,
        }
    }
}

/// A shape placed at a block for the ray: the block-local shape plus, for offset outlines, the
/// block's model offset at that position (vanilla `shape.move(offset)`).
#[derive(Clone, Copy, Debug)]
pub struct PlacedShape {
    /// The unshifted shape.
    pub shape: &'static SelectionShape,
    /// The model offset, when the shape is moved by one (it then indexes like an array shape).
    pub shift: Option<[f64; 3]>,
}

impl PlacedShape {
    /// A shape that is not moved.
    #[must_use]
    pub const fn fixed(shape: &'static SelectionShape) -> PlacedShape {
        PlacedShape { shape, shift: None }
    }
}

/// `Mth.floor` (Java `(int) Math.floor`, saturating; NaN gives 0 in both).
#[inline]
fn floor(v: f64) -> i32 {
    v.floor() as i32
}

/// `Mth.frac`.
#[inline]
fn frac(v: f64) -> f64 {
    v - (v.floor() as i64) as f64
}

/// `Mth.sign(double)`.
#[inline]
fn sign(v: f64) -> i32 {
    if v == 0.0 {
        0
    } else if v > 0.0 {
        1
    } else {
        -1
    }
}

/// The cell of `coord` along `axis` (vanilla `VoxelShape.findIndex`; `-1..=n`).
fn find_index(p: &PlacedShape, axis: usize, coord: f64) -> i32 {
    let s = p.shape;
    let n = i32::from(s.sizes[axis]);
    match p.shift {
        None if s.cube => {
            // floor(clamp(coord * n, -1, n)) in 26.1, clamp(floor(coord * n), -1, n) in 1.16.5:
            // the same integer for every input.
            let v = coord * f64::from(n);
            let v = if v < -1.0 {
                -1.0
            } else if v > f64::from(n) {
                f64::from(n)
            } else {
                v
            };
            floor(v)
        }
        shift => {
            let pts = match axis {
                0 => s.xs,
                1 => s.ys,
                _ => s.zs,
            };
            let k = shift.map_or(0.0, |d| d[axis]);
            // Mth.binarySearch(0, n + 1, i -> coord < point(i)) - 1
            let (mut from, mut len) = (0usize, pts.len());
            while len > 0 {
                let half = len / 2;
                let middle = from + half;
                let point = if shift.is_some() {
                    pts[middle] + k
                } else {
                    pts[middle]
                };
                if coord < point {
                    len = half;
                } else {
                    from = middle + 1;
                    len -= half + 1;
                }
            }
            from as i32 - 1
        }
    }
}

/// `AABB.clipPoint` for one face plane.
#[allow(clippy::too_many_arguments)]
#[inline]
fn clip_point(
    scale: &mut f64,
    dir: Option<Direction>,
    da: f64,
    db: f64,
    dc: f64,
    point: f64,
    min_b: f64,
    max_b: f64,
    min_c: f64,
    max_c: f64,
    new_dir: Direction,
    from_a: f64,
    from_b: f64,
    from_c: f64,
) -> Option<Direction> {
    let s = (point - from_a) / da;
    let pb = from_b + s * db;
    let pc = from_c + s * dc;
    if 0.0 < s
        && s < *scale
        && min_b - 1.0e-7 < pb
        && pb < max_b + 1.0e-7
        && min_c - 1.0e-7 < pc
        && pc < max_c + 1.0e-7
    {
        *scale = s;
        Some(new_dir)
    } else {
        dir
    }
}

/// `AABB.getDirection` for one (world) box.
#[inline]
fn box_direction(
    b: [f64; 6],
    from: [f64; 3],
    scale: &mut f64,
    mut dir: Option<Direction>,
    d: [f64; 3],
) -> Option<Direction> {
    let [min_x, min_y, min_z, max_x, max_y, max_z] = b;
    let [dx, dy, dz] = d;
    let [fx, fy, fz] = from;
    if dx > 1.0e-7 {
        dir = clip_point(
            scale,
            dir,
            dx,
            dy,
            dz,
            min_x,
            min_y,
            max_y,
            min_z,
            max_z,
            Direction::West,
            fx,
            fy,
            fz,
        );
    } else if dx < -1.0e-7 {
        dir = clip_point(
            scale,
            dir,
            dx,
            dy,
            dz,
            max_x,
            min_y,
            max_y,
            min_z,
            max_z,
            Direction::East,
            fx,
            fy,
            fz,
        );
    }
    if dy > 1.0e-7 {
        dir = clip_point(
            scale,
            dir,
            dy,
            dz,
            dx,
            min_y,
            min_z,
            max_z,
            min_x,
            max_x,
            Direction::Down,
            fy,
            fz,
            fx,
        );
    } else if dy < -1.0e-7 {
        dir = clip_point(
            scale,
            dir,
            dy,
            dz,
            dx,
            max_y,
            min_z,
            max_z,
            min_x,
            max_x,
            Direction::Up,
            fy,
            fz,
            fx,
        );
    }
    if dz > 1.0e-7 {
        dir = clip_point(
            scale,
            dir,
            dz,
            dx,
            dy,
            min_z,
            min_x,
            max_x,
            min_y,
            max_y,
            Direction::North,
            fz,
            fx,
            fy,
        );
    } else if dz < -1.0e-7 {
        dir = clip_point(
            scale,
            dir,
            dz,
            dx,
            dy,
            max_z,
            min_x,
            max_x,
            min_y,
            max_y,
            Direction::South,
            fz,
            fx,
            fy,
        );
    }
    dir
}

/// `AABB.clip(from, to)` for one box.
#[must_use]
pub fn clip_box(b: &Aabb, from: [f64; 3], to: [f64; 3]) -> Option<[f64; 3]> {
    let d = [to[0] - from[0], to[1] - from[1], to[2] - from[2]];
    let mut scale = 1.0;
    let c = [b.min_x, b.min_y, b.min_z, b.max_x, b.max_y, b.max_z];
    box_direction(c, from, &mut scale, None, d)?;
    Some([
        from[0] + scale * d[0],
        from[1] + scale * d[1],
        from[2] + scale * d[2],
    ])
}

/// `AABB.intersects(AABB)`.
#[inline]
fn intersects(a: &Aabb, b: &Aabb) -> bool {
    a.min_x < b.max_x
        && a.max_x > b.min_x
        && a.min_y < b.max_y
        && a.max_y > b.min_y
        && a.min_z < b.max_z
        && a.max_z > b.min_z
}

/// `AABB.contains(Vec3)`.
#[inline]
fn contains(b: &Aabb, p: [f64; 3]) -> bool {
    p[0] >= b.min_x
        && p[0] < b.max_x
        && p[1] >= b.min_y
        && p[1] < b.max_y
        && p[2] >= b.min_z
        && p[2] < b.max_z
}

/// The entity search box: the player's box stretched along `delta`, grown by 1 (vanilla
/// `getBoundingBox().expandTowards(delta).inflate(1.0)`).
#[must_use]
pub fn search_box(player_bb: &Aabb, delta: [f64; 3]) -> Aabb {
    let mut lo = [player_bb.min_x, player_bb.min_y, player_bb.min_z];
    let mut hi = [player_bb.max_x, player_bb.max_y, player_bb.max_z];
    for i in 0..3 {
        if delta[i] < 0.0 {
            lo[i] += delta[i];
        } else if delta[i] > 0.0 {
            hi[i] += delta[i];
        }
    }
    Aabb::new(
        lo[0] - 1.0,
        lo[1] - 1.0,
        lo[2] - 1.0,
        hi[0] + 1.0,
        hi[1] + 1.0,
        hi[2] + 1.0,
    )
}

/// The entity the segment `from -> to` picks among the entities whose box intersects `search`
/// (vanilla `ProjectileUtil.getEntityHitResult`): the nearest box hit within `max_dist_sq`; a box
/// containing `from` counts as distance 0.
#[must_use]
pub fn entity_hit(
    from: [f64; 3],
    to: [f64; 3],
    search: &Aabb,
    entities: &[PickEntity],
    max_dist_sq: f64,
) -> Option<EntityHit> {
    let mut nearest = max_dist_sq;
    let mut hovered = None;
    for (index, e) in entities.iter().enumerate() {
        if !intersects(&e.bb, search) {
            continue;
        }
        let r = f64::from(e.pick_radius);
        let bb = Aabb::new(
            e.bb.min_x - r,
            e.bb.min_y - r,
            e.bb.min_z - r,
            e.bb.max_x + r,
            e.bb.max_y + r,
            e.bb.max_z + r,
        );
        let clip = clip_box(&bb, from, to);
        if contains(&bb, from) {
            if nearest >= 0.0 {
                hovered = Some(EntityHit {
                    index,
                    location: clip.unwrap_or(from),
                });
                nearest = 0.0;
            }
        } else if let Some(location) = clip {
            let dd = length_sqr(location, from);
            if dd < nearest || nearest == 0.0 {
                if e.same_root_vehicle {
                    if nearest == 0.0 {
                        hovered = Some(EntityHit { index, location });
                    }
                } else {
                    hovered = Some(EntityHit { index, location });
                    nearest = dd;
                }
            }
        }
    }
    hovered
}

/// One shape against the segment `from -> to` at block `pos` (vanilla `VoxelShape.clip`).
#[must_use]
pub fn clip_shape(
    p: &PlacedShape,
    from: [f64; 3],
    to: [f64; 3],
    pos: [i32; 3],
) -> Option<BlockHit> {
    let s = p.shape;
    if s.empty {
        return None;
    }
    let d = [to[0] - from[0], to[1] - from[1], to[2] - from[2]];
    if d[0] * d[0] + d[1] * d[1] + d[2] * d[2] < 1.0e-7 {
        return None;
    }
    let test = [
        from[0] + d[0] * 0.001,
        from[1] + d[1] * 0.001,
        from[2] + d[2] * 0.001,
    ];
    let pf = pos.map(f64::from);
    let ix = find_index(p, 0, test[0] - pf[0]);
    let iy = find_index(p, 1, test[1] - pf[1]);
    let iz = find_index(p, 2, test[2] - pf[2]);
    if s.filled(ix, iy, iz) {
        return Some(BlockHit {
            kind: HitKind::Block,
            pos,
            face: Direction::approximate_nearest(d[0], d[1], d[2]).opposite(),
            location: test,
            inside: true,
        });
    }
    // AABB.clip(toAabbs(), from, to, pos)
    let mut scale = 1.0;
    let mut dir = None;
    let k = p.shift.unwrap_or([0.0; 3]);
    let shifted = p.shift.is_some();
    for b in s.boxes {
        let mut c = [b.min_x, b.min_y, b.min_z, b.max_x, b.max_y, b.max_z];
        if shifted {
            for (i, v) in c.iter_mut().enumerate() {
                *v += k[i % 3];
            }
        }
        for (i, v) in c.iter_mut().enumerate() {
            *v += pf[i % 3];
        }
        dir = box_direction(c, from, &mut scale, dir, d);
    }
    dir.map(|face| BlockHit {
        kind: HitKind::Block,
        pos,
        face,
        location: [
            from[0] + scale * d[0],
            from[1] + scale * d[1],
            from[2] + scale * d[2],
        ],
        inside: false,
    })
}

#[inline]
pub(crate) fn length_sqr(a: [f64; 3], from: [f64; 3]) -> f64 {
    let (x, y, z) = (a[0] - from[0], a[1] - from[1], a[2] - from[2]);
    x * x + y * y + z * z
}

/// The outline hit, re-faced by a nearer interaction-shape hit (vanilla
/// `BlockGetter.clipWithInteractionOverride`).
#[must_use]
pub fn clip_block(
    outline: &PlacedShape,
    interaction: &PlacedShape,
    from: [f64; 3],
    to: [f64; 3],
    pos: [i32; 3],
) -> Option<BlockHit> {
    let mut hit = clip_shape(outline, from, to, pos)?;
    if let Some(o) = clip_shape(interaction, from, to, pos)
        && length_sqr(o.location, from) < length_sqr(hit.location, from)
    {
        hit.face = o.face;
    }
    Some(hit)
}

/// Walks the blocks the segment crosses, in order, until `visit` returns a result (vanilla
/// `BlockGetter.traverseBlocks`); `None` when the walk ends without one.
pub fn traverse<T>(
    from: [f64; 3],
    to: [f64; 3],
    mut visit: impl FnMut([i32; 3]) -> Option<T>,
) -> Option<T> {
    // Vec3.equals: Double.compare per component (-0.0 differs from 0.0, NaN equals NaN).
    let same = |a: f64, b: f64| a.to_bits() == b.to_bits() || (a.is_nan() && b.is_nan());
    if same(from[0], to[0]) && same(from[1], to[1]) && same(from[2], to[2]) {
        return None;
    }
    let lerp = |a: f64, b: f64| a + -1.0e-7 * (b - a);
    let end = [
        lerp(to[0], from[0]),
        lerp(to[1], from[1]),
        lerp(to[2], from[2]),
    ];
    let start = [
        lerp(from[0], to[0]),
        lerp(from[1], to[1]),
        lerp(from[2], to[2]),
    ];
    let mut cur = start.map(floor);
    if let Some(r) = visit(cur) {
        return Some(r);
    }
    let d = [end[0] - start[0], end[1] - start[1], end[2] - start[2]];
    let sg = d.map(sign);
    let mut delta = [0.0; 3];
    let mut t = [0.0; 3];
    for i in 0..3 {
        delta[i] = if sg[i] == 0 {
            f64::MAX
        } else {
            f64::from(sg[i]) / d[i]
        };
        t[i] = delta[i]
            * if sg[i] > 0 {
                1.0 - frac(start[i])
            } else {
                frac(start[i])
            };
    }
    while t[0] <= 1.0 || t[1] <= 1.0 || t[2] <= 1.0 {
        let axis = if t[0] < t[1] {
            if t[0] < t[2] { 0 } else { 2 }
        } else if t[1] < t[2] {
            1
        } else {
            2
        };
        cur[axis] += sg[axis];
        t[axis] += delta[axis];
        if let Some(r) = visit(cur) {
            return Some(r);
        }
    }
    None
}

/// The block pick along `from -> to` with the shapes `shapes(pos)` gives at each block crossed
/// (outline, interaction; `None` skips the block), no fluids: the first block hit, else a miss at
/// `to`.
pub fn clip(
    from: [f64; 3],
    to: [f64; 3],
    mut shapes: impl FnMut([i32; 3]) -> Option<(PlacedShape, PlacedShape)>,
) -> BlockHit {
    traverse(from, to, |pos| {
        let (outline, interaction) = shapes(pos)?;
        clip_block(&outline, &interaction, from, to, pos)
    })
    .unwrap_or_else(|| {
        BlockHit::miss(
            to,
            Direction::approximate_nearest(from[0] - to[0], from[1] - to[1], from[2] - to[2]),
        )
    })
}