ghast-pathfinder 0.4.2

Physics-exact path planning and path following for the ghast Minecraft Java Edition bot library (1.8.9, 1.12.2, 1.16.5 and 26.1): every move type is validated against the vanilla-exact ghast-physics
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
//! What the planner needs to know about a block state, per protocol version.
//!
//! Every state of a version is reduced once (lazily, on first use) to a small [`Class`]: its
//! collision extent as seen by a player standing at the block centre, fluid and climbing flags,
//! hazards and floor properties. States with the same class share one entry, so the planner's
//! world cache stores a `u16` class index per block.

use std::sync::OnceLock;

use ghast_data::common::{Aabb, NeighbourShape, PhysicsFlags, ShapeDependency};

use crate::Version;

/// Class flag bits.
pub(crate) mod flag {
    /// Has collision boxes (blocks the body unless [`THIN`] and climbable).
    pub(crate) const SOLID: u32 = 1 << 0;
    /// A collision box covers the centre footprint of a standing player (it can carry one).
    pub(crate) const SUPPORT: u32 = 1 << 1;
    /// Collision boxes exist but none reaches the centre footprint (ladders, open trapdoors).
    pub(crate) const THIN: u32 = 1 << 2;
    /// Water (also waterlogged states in 1.16).
    pub(crate) const WATER: u32 = 1 << 3;
    /// Lava: never entered.
    pub(crate) const LAVA: u32 = 1 << 4;
    /// Climbable (ladder, vine; 1.16 also twisting / weeping vines).
    pub(crate) const CLIMB: u32 = 1 << 5;
    /// Never put the body in it (fire, cobweb, berry bush, portals, wither rose, ...).
    pub(crate) const AVOID: u32 = 1 << 6;
    /// Never stand on it (magma, campfire, cactus).
    pub(crate) const AVOID_FLOOR: u32 = 1 << 7;
    /// Slow floor (soul sand, honey): extra cost.
    pub(crate) const SLOW: u32 = 1 << 8;
    /// Slippery floor (ice): extra cost.
    pub(crate) const SLIPPERY: u32 = 1 << 9;
    /// Bouncy floor (slime): extra cost, never landed on from a fall.
    pub(crate) const BOUNCY: u32 = 1 << 10;
    /// Falls when unsupported (sand, gravel, concrete powder, anvils): never dug under.
    pub(crate) const FALLING: u32 = 1 << 11;
    /// Can be dug (has a hardness, diggable, not a fluid).
    pub(crate) const DIGGABLE: u32 = 1 << 12;
    /// Trampled or damaged by landing (farmland, turtle eggs): no falls onto it.
    pub(crate) const FRAGILE: u32 = 1 << 13;
    /// The shape depends on things the planner does not model (doors, scaffolding, moving
    /// pistons, bamboo offsets): treated as a solid obstacle that cannot carry the player.
    pub(crate) const IRREGULAR: u32 = 1 << 14;
    /// Air-like for placing: empty collision, no fluid (a block can be placed into it).
    pub(crate) const REPLACEABLE: u32 = 1 << 15;
}

/// The planner's view of one block state.
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct Class {
    /// [`flag`] bits.
    pub(crate) flags: u32,
    /// Lowest collision bound in the block (block-local y), `1.0` when there is no collision.
    pub(crate) bottom: f32,
    /// Highest bound of the boxes covering the centre footprint (block-local y; may exceed 1
    /// for fences and walls); `0.0` without [`flag::SUPPORT`].
    pub(crate) top: f32,
    /// Highest bound of any box (for clearance above fences next to the path).
    pub(crate) top_any: f32,
    /// Dig hardness (vanilla strength); negative when not diggable.
    pub(crate) hardness: f32,
    /// Side a thin climbable box sits on: 0 none, 1 north (-z), 2 south (+z), 3 west (-x),
    /// 4 east (+x).
    pub(crate) wall: u8,
}

impl Class {
    pub(crate) const UNKNOWN: Class = Class {
        flags: flag::SOLID | flag::AVOID,
        bottom: 0.0,
        top: 0.0,
        top_any: 1.0,
        hardness: -1.0,
        wall: 0,
    };

    #[inline]
    pub(crate) fn has(&self, f: u32) -> bool {
        self.flags & f != 0
    }

    /// Blocks the body of a player passing through the cell between `lo` and `hi` (cell-local
    /// heights). Climbable thin boxes (ladders) do not.
    #[inline]
    pub(crate) fn blocks_between(&self, lo: f32, hi: f32) -> bool {
        if !self.has(flag::SOLID) {
            return false;
        }
        if self.has(flag::THIN) && self.has(flag::CLIMB) {
            return false;
        }
        // Collision occupies [bottom, top_any] of the cell.
        self.top_any > lo + 1e-4 && self.bottom < hi - 1e-4
    }
}

/// Per-version table: state → class index, plus the distinct classes.
pub(crate) struct Table {
    pub(crate) classes: Vec<Class>,
    pub(crate) map: Vec<u16>,
}

impl Table {
    /// The class index of a raw state (out-of-range values read as air, like vanilla).
    #[inline]
    pub(crate) fn index(&self, raw: u32) -> u16 {
        self.map.get(raw as usize).copied().unwrap_or(0)
    }
}

/// The table of a version (built on first use: ~1 ms).
pub(crate) fn table(version: Version) -> &'static Table {
    #[cfg(feature = "v1_8")]
    static T18: OnceLock<Table> = OnceLock::new();
    #[cfg(feature = "v1_12_2")]
    static T12: OnceLock<Table> = OnceLock::new();
    #[cfg(feature = "v1_16_5")]
    static T16: OnceLock<Table> = OnceLock::new();
    #[cfg(feature = "v26_1")]
    static T26: OnceLock<Table> = OnceLock::new();
    #[cfg(feature = "v26_2")]
    static T262: OnceLock<Table> = OnceLock::new();
    match version {
        #[cfg(feature = "v1_8")]
        Version::V1_8 => T18.get_or_init(build_1_8),
        #[cfg(feature = "v1_12_2")]
        Version::V1_12 => T12.get_or_init(build_1_12),
        #[cfg(feature = "v1_16_5")]
        Version::V1_16_5 => T16.get_or_init(build_1_16_5),
        #[cfg(feature = "v26_1")]
        Version::V26_1 => T26.get_or_init(m26_1::build),
        #[cfg(feature = "v26_2")]
        Version::V26_2 => T262.get_or_init(m26_2::build),
    }
}

/// Footprint of a player standing at the block centre (0.3 either side), slightly shrunk so a
/// box that only touches it does not count.
const CORE_MIN: f64 = 0.2 + 1e-3;
const CORE_MAX: f64 = 0.8 - 1e-3;

struct Facts<'a> {
    name: &'a str,
    boxes: &'a [Aabb],
    dependency: ShapeDependency,
    physics: PhysicsFlags,
    slipperiness: f32,
    speed_factor: f64,
    fluid_water: bool,
    fluid_lava: bool,
    hardness: Option<f32>,
    diggable: bool,
}

fn classify(f: &Facts<'_>) -> Class {
    use flag::*;
    let mut flags = 0u32;
    let mut bottom = 1.0f32;
    let mut top = 0.0f32;
    let mut top_any = 0.0f32;
    let mut core = false;
    for b in f.boxes {
        bottom = bottom.min(b.min_y as f32);
        top_any = top_any.max(b.max_y as f32);
        if b.min_x < CORE_MAX && b.max_x > CORE_MIN && b.min_z < CORE_MAX && b.max_z > CORE_MIN {
            core = true;
            top = top.max(b.max_y as f32);
        }
    }
    let mut wall = 0u8;
    if !f.boxes.is_empty() {
        flags |= SOLID;
        if core {
            flags |= SUPPORT;
        } else {
            flags |= THIN;
            // Which side the thin shape hugs (ladders).
            let b = f.boxes[0];
            wall = if b.max_z <= 0.25 {
                1
            } else if b.min_z >= 0.75 {
                2
            } else if b.max_x <= 0.25 {
                3
            } else if b.min_x >= 0.75 {
                4
            } else {
                0
            };
        }
    } else {
        bottom = 1.0;
    }
    let mut irregular_solid = false;
    match f.dependency {
        ShapeDependency::None | ShapeDependency::Neighbours(NeighbourShape::Chest) => {}
        ShapeDependency::Neighbours(NeighbourShape::Fence | NeighbourShape::Wall) => {
            // Isolated post; arms only grow inside the cell. Height 1.5 either way.
        }
        ShapeDependency::Neighbours(NeighbourShape::Pane | NeighbourShape::Stairs) => {
            // Panes: thin arms; stairs: corner shapes. Both still cover part of the core in
            // every configuration except a pane's single arm, which the planner never stands on.
            if f.dependency == ShapeDependency::Neighbours(NeighbourShape::Pane) {
                flags &= !SUPPORT;
                flags |= IRREGULAR;
            }
        }
        ShapeDependency::Neighbours(NeighbourShape::Door | NeighbourShape::ChorusPlant)
        | ShapeDependency::EntityContext
        | ShapeDependency::BlockEntity
        | ShapeDependency::PositionOffset => irregular_solid = true,
        // A shape kind added for a newer version: an irregular solid until classified here.
        _ => irregular_solid = true,
    }
    if irregular_solid {
        flags |= SOLID | IRREGULAR;
        flags &= !(SUPPORT | THIN);
        bottom = 0.0;
        top_any = top_any.max(1.0);
    }
    if flags & IRREGULAR != 0 {
        bottom = 0.0;
        top_any = top_any.max(1.0);
    }
    if f.fluid_water {
        flags |= WATER;
    }
    if f.fluid_lava {
        flags |= LAVA;
    }
    if f.physics.contains(PhysicsFlags::CLIMBABLE) {
        flags |= CLIMB;
    }
    if f.physics.contains(PhysicsFlags::STUCK)
        || f.physics.contains(PhysicsFlags::FIRE)
        || f.physics.contains(PhysicsFlags::BUBBLE_COLUMN)
    {
        flags |= AVOID;
    }
    if f.physics.contains(PhysicsFlags::SLIME) {
        flags |= BOUNCY;
    }
    if f.physics.contains(PhysicsFlags::COLLIDE_SLOWDOWN)
        || f.physics.contains(PhysicsFlags::HONEY)
        || f.speed_factor < 0.99
    {
        flags |= SLOW;
    }
    if f.slipperiness > 0.7 {
        flags |= SLIPPERY;
    }
    match f.name {
        "fire" | "soul_fire" | "web" | "cobweb" | "sweet_berry_bush" | "wither_rose" | "portal"
        | "nether_portal" | "end_portal" | "end_gateway" | "scaffolding" | "powder_snow"
        | "tripwire" | "bubble_column" => flags |= AVOID,
        "magma_block" | "magma" | "campfire" | "soul_campfire" | "cactus" => {
            flags |= AVOID_FLOOR | AVOID;
        }
        // 1.17+: pointed dripstone hurts on landing (and has an offset shape anyway), a lava
        // cauldron burns, a big dripleaf tilts away under the player, sculk sensors and
        // shriekers react to steps (a shrieker can summon a warden).
        "pointed_dripstone" | "lava_cauldron" => flags |= AVOID_FLOOR | AVOID,
        "big_dripleaf" | "sculk_sensor" | "calibrated_sculk_sensor" | "sculk_shrieker" => {
            flags |= AVOID_FLOOR;
        }
        "farmland" | "turtle_egg" => flags |= FRAGILE,
        "sand" | "red_sand" | "gravel" | "anvil" | "chipped_anvil" | "damaged_anvil"
        | "dragon_egg" | "suspicious_sand" | "suspicious_gravel" => flags |= FALLING,
        n if n.ends_with("concrete_powder") => flags |= FALLING,
        _ => {}
    }
    if f.diggable
        && !f.fluid_water
        && !f.fluid_lava
        && f.hardness.is_some_and(|h| h >= 0.0)
        && flags & (IRREGULAR | AVOID) == 0
    {
        flags |= DIGGABLE;
    }
    if f.boxes.is_empty() && !f.fluid_water && !f.fluid_lava && flags & (AVOID | CLIMB) == 0 {
        flags |= REPLACEABLE;
    }
    if flags & SUPPORT == 0 {
        top = 0.0;
    }
    Class {
        flags,
        bottom,
        top,
        top_any,
        hardness: if flags & DIGGABLE != 0 {
            f.hardness.unwrap_or(-1.0)
        } else {
            -1.0
        },
        wall,
    }
}

fn intern(classes: &mut Vec<Class>, c: Class) -> u16 {
    if let Some(i) = classes.iter().position(|e| *e == c) {
        return i as u16;
    }
    classes.push(c);
    (classes.len() - 1) as u16
}

#[cfg(feature = "v1_8")]
fn build_1_8() -> Table {
    use ghast_data::v1_8::BlockState;
    let mut classes = Vec::new();
    let mut map = vec![0u16; 1 << 12];
    // Index 0 must be air.
    let air = classify_1_8(BlockState::AIR);
    intern(&mut classes, air);
    for (raw, slot) in map.iter_mut().enumerate() {
        let s = BlockState::from_raw_vanilla(raw as u16);
        *slot = intern(&mut classes, classify_1_8(s));
    }
    Table { classes, map }
}

#[cfg(feature = "v1_8")]
fn classify_1_8(s: ghast_data::v1_8::BlockState) -> Class {
    let block = s.block();
    let info = block.info();
    let physics = block.physics();
    let collision = s.collision();
    let fluid = s.fluid();
    classify(&Facts {
        name: info.name,
        boxes: collision.boxes,
        dependency: collision.dependency,
        physics: physics.flags,
        slipperiness: physics.slipperiness,
        speed_factor: physics.speed_factor,
        fluid_water: fluid.is_some_and(|f| f.kind == ghast_data::common::FluidKind::Water),
        fluid_lava: fluid.is_some_and(|f| f.kind == ghast_data::common::FluidKind::Lava),
        hardness: info.hardness,
        diggable: info.diggable,
    })
}

#[cfg(feature = "v1_12_2")]
fn build_1_12() -> Table {
    use ghast_data::v1_12::BlockState;
    let mut classes = Vec::new();
    let mut map = vec![0u16; 1 << 12];
    intern(&mut classes, classify_1_12(BlockState::AIR));
    for (raw, slot) in map.iter_mut().enumerate() {
        let s = BlockState::from_raw_vanilla(raw as u16);
        *slot = intern(&mut classes, classify_1_12(s));
    }
    Table { classes, map }
}

#[cfg(feature = "v1_12_2")]
fn classify_1_12(s: ghast_data::v1_12::BlockState) -> Class {
    let block = s.block();
    let info = block.info();
    let physics = block.physics();
    let collision = s.collision();
    let fluid = s.fluid();
    classify(&Facts {
        name: info.name,
        boxes: collision.boxes,
        dependency: collision.dependency,
        physics: physics.flags,
        slipperiness: physics.slipperiness,
        speed_factor: physics.speed_factor,
        fluid_water: fluid.is_some_and(|f| f.kind == ghast_data::common::FluidKind::Water),
        fluid_lava: fluid.is_some_and(|f| f.kind == ghast_data::common::FluidKind::Lava),
        hardness: info.hardness,
        diggable: info.diggable,
    })
}

#[cfg(feature = "v1_16_5")]
fn build_1_16_5() -> Table {
    use ghast_data::v1_16_5::{BlockState, STATE_COUNT};
    let mut classes = Vec::new();
    let mut map = vec![0u16; STATE_COUNT];
    intern(&mut classes, classify_1_16_5(BlockState::AIR));
    for (id, slot) in map.iter_mut().enumerate() {
        let s = BlockState::from_id(id as u16).unwrap_or(BlockState::AIR);
        *slot = intern(&mut classes, classify_1_16_5(s));
    }
    Table { classes, map }
}

#[cfg(feature = "v1_16_5")]
fn classify_1_16_5(s: ghast_data::v1_16_5::BlockState) -> Class {
    let block = s.block();
    let info = block.info();
    let physics = block.physics();
    let collision = s.collision();
    let fluid = s.fluid();
    classify(&Facts {
        name: info.name,
        boxes: collision.boxes,
        dependency: collision.dependency,
        physics: physics.flags,
        slipperiness: physics.slipperiness,
        speed_factor: physics.speed_factor,
        fluid_water: fluid.is_some_and(|f| f.kind == ghast_data::common::FluidKind::Water),
        fluid_lava: fluid.is_some_and(|f| f.kind == ghast_data::common::FluidKind::Lava),
        hardness: info.hardness,
        diggable: info.diggable,
    })
}

/// The class table of a modern version (one row per version: its `ghast-data` module).
#[cfg(ghast_modern)]
macro_rules! modern_classes {
    ($m:ident, $($data:ident)::+) => {
        mod $m {
            use super::*;
            use $($data)::+ as d;

            pub(crate) fn build() -> Table {
                use d::{BlockState, STATE_COUNT};
                let mut classes = Vec::new();
                let mut map = vec![0u16; STATE_COUNT];
                intern(&mut classes, classify_modern(BlockState::AIR));
                for (id, slot) in map.iter_mut().enumerate() {
                    let s = BlockState::from_id(id as u16).unwrap_or(BlockState::AIR);
                    *slot = intern(&mut classes, classify_modern(s));
                }
                Table { classes, map }
            }

            /// The jar's movement records (the version's `ghast-data` module) in the planner's terms.
            pub(crate) fn classify_modern(s: d::BlockState) -> Class {
                use d::{BlockKind, block_flags, state_flags};
                let block = s.block();
                let d = block.data();
                let phys = s.phys();
                let kind = d.kind;
                let dependency = if phys.has(state_flags::CONTEXT) {
                    // Scaffolding, powder snow: the shape depends on the player.
                    ShapeDependency::EntityContext
                } else if phys.has(state_flags::OFFSET) {
                    // Bamboo, pointed dripstone: shifted per position.
                    ShapeDependency::PositionOffset
                } else if phys.has(state_flags::MOVING_PISTON) || block.name().ends_with("shulker_box") {
                    // The shape follows a block entity (moving piston, opening shulker lid).
                    ShapeDependency::BlockEntity
                } else {
                    ShapeDependency::None
                };
                let mut physics = 0u16;
                if d.flags & block_flags::CLIMBABLE != 0 {
                    physics |= PhysicsFlags::CLIMBABLE.0;
                }
                if d.flags & block_flags::FIRE != 0 {
                    physics |= PhysicsFlags::FIRE.0;
                }
                match kind {
                    BlockKind::Cobweb | BlockKind::SweetBerryBush | BlockKind::PowderSnow => {
                        physics |= PhysicsFlags::STUCK.0;
                    }
                    BlockKind::BubbleColumn => physics |= PhysicsFlags::BUBBLE_COLUMN.0,
                    BlockKind::SlimeBlock => physics |= PhysicsFlags::SLIME.0,
                    BlockKind::HoneyBlock => physics |= PhysicsFlags::HONEY.0,
                    _ => {}
                }
                let fluid = phys.fluid() >> 6;
                classify(&Facts {
                    name: block.name(),
                    boxes: s.voxel_shape().boxes,
                    dependency,
                    physics: PhysicsFlags(physics),
                    slipperiness: d.friction,
                    speed_factor: f64::from(d.speed_factor),
                    fluid_water: fluid == 1,
                    fluid_lava: fluid == 2,
                    hardness: d.hardness,
                    diggable: d.diggable,
                })
            }
        }
    };
}

#[cfg(feature = "v26_1")]
modern_classes!(m26_1, ghast_data::v26_1);
#[cfg(feature = "v26_2")]
modern_classes!(m26_2, ghast_data::v26_2);

#[cfg(all(test, feature = "v26_1"))]
mod tests_26_1 {
    use super::*;
    use ghast_data::v26_1::BlockState;

    fn c(spec: &str) -> Class {
        let t = table(Version::V26_1);
        let s = BlockState::parse(spec).unwrap_or_else(|| panic!("{spec}"));
        t.classes[t.index(u32::from(s.id())) as usize]
    }

    #[test]
    fn basic_26_1() {
        assert_eq!(c("air").flags & flag::SOLID, 0);
        assert!(c("air").has(flag::REPLACEABLE));
        assert!(c("cave_air").has(flag::REPLACEABLE));
        let stone = c("stone");
        assert!(stone.has(flag::SOLID | flag::SUPPORT | flag::DIGGABLE));
        assert_eq!(stone.top, 1.0);
        assert!(!c("bedrock").has(flag::DIGGABLE));
        assert_eq!(c("oak_slab[type=bottom]").top, 0.5);
        assert_eq!(c("oak_slab[type=top]").bottom, 0.5);
        assert_eq!(c("soul_sand").top, 0.875);
        assert!(c("soul_sand").has(flag::SLOW));
        assert!(c("honey_block").has(flag::SLOW));
        assert_eq!(c("mud").top, 0.875);
        assert!(!c("mud").has(flag::SLOW | flag::AVOID | flag::AVOID_FLOOR));
        assert!(c("oak_fence").top >= 1.5);
        let ladder = c("ladder[facing=north]");
        assert!(ladder.has(flag::THIN | flag::CLIMB));
        assert!(!ladder.blocks_between(0.0, 1.0));
        assert!(c("vine").has(flag::CLIMB));
        assert!(c("cave_vines").has(flag::CLIMB));
        assert!(c("water").has(flag::WATER));
        assert!(c("oak_slab[type=bottom,waterlogged=true]").has(flag::WATER));
        assert!(c("lava").has(flag::LAVA));
        assert!(c("cobweb").has(flag::AVOID));
        assert!(c("sweet_berry_bush").has(flag::AVOID));
        assert!(c("fire").has(flag::AVOID));
        assert!(c("powder_snow").has(flag::AVOID | flag::IRREGULAR));
        assert!(c("scaffolding").has(flag::AVOID));
        assert!(c("magma_block").has(flag::AVOID_FLOOR));
        assert!(c("campfire").has(flag::AVOID_FLOOR));
        assert!(c("pointed_dripstone").has(flag::AVOID_FLOOR | flag::AVOID));
        assert!(c("big_dripleaf").has(flag::AVOID_FLOOR));
        assert!(c("sculk_shrieker").has(flag::AVOID_FLOOR));
        assert!(c("slime_block").has(flag::BOUNCY));
        assert!(c("ice").has(flag::SLIPPERY));
        assert!(c("blue_ice").has(flag::SLIPPERY));
        assert!(c("sand").has(flag::FALLING));
        assert!(c("suspicious_gravel").has(flag::FALLING));
        assert!(c("white_concrete_powder").has(flag::FALLING));
        assert!(c("farmland").has(flag::FRAGILE));
        assert!(c("bamboo").has(flag::IRREGULAR));
        assert!(c("shulker_box").has(flag::IRREGULAR));
        // Copper grates and sculk are ordinary floors.
        for name in ["copper_grate", "sculk", "deepslate", "tuff", "moss_block"] {
            let k = c(name);
            assert!(k.has(flag::SUPPORT), "{name}");
            assert_eq!(k.top, 1.0, "{name}");
            assert!(
                !k.has(flag::AVOID | flag::AVOID_FLOOR | flag::IRREGULAR),
                "{name}"
            );
        }
        assert!(c("moss_carpet").has(flag::SUPPORT));
        assert!(!c("short_grass").has(flag::SOLID));
        assert!(table(Version::V26_1).classes.len() < u16::MAX as usize);
    }
}

#[cfg(all(test, feature = "v1_8", feature = "v1_12_2", feature = "v1_16_5"))]
mod tests {
    use super::*;

    fn class_1_8(id: u8, meta: u8) -> Class {
        let t = table(Version::V1_8);
        t.classes[t.index(u32::from(id) << 4 | u32::from(meta)) as usize]
    }

    #[test]
    fn basic_1_8() {
        let air = class_1_8(0, 0);
        assert_eq!(air.flags & flag::SOLID, 0);
        assert!(air.has(flag::REPLACEABLE));
        let stone = class_1_8(1, 0);
        assert!(stone.has(flag::SOLID | flag::SUPPORT));
        assert!(stone.has(flag::DIGGABLE));
        assert_eq!(stone.top, 1.0);
        let slab = class_1_8(44, 0);
        assert_eq!(slab.top, 0.5);
        let top_slab = class_1_8(44, 8);
        assert_eq!(top_slab.bottom, 0.5);
        let soul = class_1_8(88, 0);
        assert_eq!(soul.top, 0.875);
        assert!(soul.has(flag::SLOW));
        let fence = class_1_8(85, 0);
        assert_eq!(fence.top, 1.5);
        let ladder = class_1_8(65, 2);
        assert!(ladder.has(flag::THIN | flag::CLIMB));
        assert_eq!(ladder.wall, 2);
        assert!(!ladder.blocks_between(0.0, 1.0));
        let water = class_1_8(9, 0);
        assert!(water.has(flag::WATER));
        let lava = class_1_8(11, 0);
        assert!(lava.has(flag::LAVA));
        assert!(class_1_8(30, 0).has(flag::AVOID)); // web
        assert!(class_1_8(81, 0).has(flag::AVOID_FLOOR)); // cactus
        assert!(class_1_8(12, 0).has(flag::FALLING)); // sand
        assert!(class_1_8(64, 0).has(flag::IRREGULAR)); // door
        assert!(!class_1_8(7, 0).has(flag::DIGGABLE)); // bedrock
    }

    #[test]
    fn basic_1_12() {
        let t = table(Version::V1_12);
        let c = |id: u32, meta: u32| t.classes[t.index(id << 4 | meta) as usize];
        assert_eq!(c(0, 0).flags & flag::SOLID, 0);
        assert!(c(1, 0).has(flag::SOLID | flag::SUPPORT | flag::DIGGABLE));
        assert_eq!(c(44, 0).top, 0.5);
        assert_eq!(c(44, 8).bottom, 0.5);
        assert_eq!(c(88, 0).top, 0.875);
        assert!(c(88, 0).has(flag::SLOW));
        assert_eq!(c(85, 0).top, 1.5);
        let ladder = c(65, 2);
        assert!(ladder.has(flag::THIN | flag::CLIMB));
        assert_eq!(ladder.wall, 2);
        assert!(c(9, 0).has(flag::WATER));
        assert!(c(11, 0).has(flag::LAVA));
        assert!(c(30, 0).has(flag::AVOID)); // web
        assert!(c(213, 0).has(flag::AVOID_FLOOR)); // magma
        assert!(c(165, 0).has(flag::BOUNCY)); // slime
        assert!(c(79, 0).has(flag::SLIPPERY)); // ice
        assert!(c(64, 0).has(flag::IRREGULAR)); // door
        assert!(c(252, 0).has(flag::FALLING)); // concrete powder
        assert!(!c(7, 0).has(flag::DIGGABLE)); // bedrock
    }

    #[test]
    fn basic_1_16_5() {
        use ghast_data::v1_16_5::Block;
        let t = table(Version::V1_16_5);
        let c = |b: Block| t.classes[t.index(u32::from(b.default_state().id())) as usize];
        assert!(c(Block::STONE).has(flag::SUPPORT));
        assert!(c(Block::WATER).has(flag::WATER));
        assert!(c(Block::LADDER).has(flag::CLIMB));
        assert!(c(Block::MAGMA_BLOCK).has(flag::AVOID_FLOOR));
        assert!(c(Block::COBWEB).has(flag::AVOID));
        assert!(c(Block::SOUL_SAND).has(flag::SLOW));
        assert!(c(Block::OAK_FENCE).top >= 1.5);
        assert_eq!(c(Block::AIR).flags & flag::SOLID, 0);
        assert!(t.classes.len() < u16::MAX as usize);
    }
}