Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
translate ▾

Modern-Apps.git / games / voxels / src / main / rust / tests / world_edges.rs

world_edges.rs144 lines · 6.6 KiBRaw
1//! Chunk storage, mesh invariants, block predicates and inventory tests.
2//!
3//! Extends the inline suites without duplicating them: `world/save.rs` covers VOX3 round trips,
4//! `raycast.rs` covers sub-voxel hits, and `lib.rs` has the engine smoke test. These tests add
5//! chunk coordinate edges, mesh face-culling invariants on a hand-built chunk, save-codec
6//! corruption handling, block predicate sweeps, and hotbar add/select/consume/move rules.
7
8use voxels_engine::inventory::{Inventory, HOTBAR, SLOTS, STACK};
9use voxels_engine::world::block::{Block, Shape, META_TOP};
10use voxels_engine::world::chunk::{Chunk, ChunkPos, CHUNK_HEIGHT, CHUNK_SIZE};
11use voxels_engine::world::mesher::mesh_chunk;
12use voxels_engine::world::save::{decode_chunk, encode_chunk};
13use voxels_engine::world::{ChunkMap, CHUNK_HEIGHT as MAP_HEIGHT};
14
15fn test_map() -> ChunkMap {
16 let dir = std::env::temp_dir().join("voxels_edges_test").to_string_lossy().into_owned();
17 ChunkMap::new(0xE99E6, dir)
18}
19
20#[test]
21fn chunk_coordinates_handle_negatives_and_boundaries() {
22 assert_eq!(ChunkPos::from_world(-1, -1), ChunkPos(-1, -1));
23 assert_eq!(ChunkPos::from_world(-16, 0), ChunkPos(-1, 0));
24 assert_eq!(ChunkPos::from_world(16, 16), ChunkPos(1, 1));
25 assert_eq!(ChunkPos(2, -3).world_origin(), (2 * CHUNK_SIZE as i32, -3 * CHUNK_SIZE as i32));
26 let mut chunk = Chunk::new(ChunkPos(0, 0));
27 assert_eq!(chunk.get_block(0, CHUNK_HEIGHT, 0), 0, "above the world is air");
28 assert_eq!(chunk.get_block(0, 0, 0), 0);
29 chunk.set_block(0, 0, 0, Block::Stone as u16);
30 assert_eq!(chunk.get_block(0, 0, 0), Block::Stone as u16);
31 chunk.set_block(0, 0, 0, 0);
32 assert!(chunk.sections[0].is_none(), "clearing the last block frees the section");
33 assert_eq!(MAP_HEIGHT, CHUNK_HEIGHT);
34}
35
36#[test]
37fn hidden_interior_faces_are_culled_and_borders_kept() {
38 let mut chunk = Chunk::new(ChunkPos(0, 0));
39 for x in 0..4 {
40 for z in 0..4 {
41 chunk.set_block(x, 64, z, Block::Stone as u16);
42 }
43 }
44 let air = |_: i32, _: i32, _: i32| (0u16, 0u8);
45 let meshes = mesh_chunk(&chunk, &air, &|_, _| [1.0, 1.0, 1.0]);
46 let mesh = meshes.iter().filter_map(|m| m.as_ref()).next().expect("mesh");
47 assert!(!mesh.indices.is_empty());
48 assert_eq!(mesh.indices.len() % 3, 0);
49 for chunk_index in mesh.indices.chunks_exact(3) {
50 for index in chunk_index {
51 assert!(((*index) as usize) < mesh.vertices.len(), "index {index} in range");
52 }
53 }
54 for vertex in &mesh.vertices {
55 assert!(vertex.ao >= 0.0 && vertex.ao <= 1.0, "ao {}", vertex.ao);
56 assert!(vertex.light >= 0.0, "light {}", vertex.light);
57 let normal_sum = vertex.normal[0].abs() + vertex.normal[1].abs() + vertex.normal[2].abs();
58 assert_eq!(normal_sum, 1.0, "axis-aligned normal {:?}", vertex.normal);
59 }
60 let mut lone = Chunk::new(ChunkPos(3, 3));
61 lone.set_block(0, 64, 0, Block::Stone as u16);
62 let lone_meshes = mesh_chunk(&lone, &air, &|_, _| [1.0, 1.0, 1.0]);
63 let lone_mesh = lone_meshes.iter().filter_map(|m| m.as_ref()).next().expect("lone mesh");
64 assert_eq!(lone_mesh.vertices.len(), 24, "a lone cube exposes six quads");
65 assert_eq!(lone_mesh.indices.len(), 36);
66}
67
68#[test]
69fn save_codec_rejects_garbage_and_keeps_meta_flags() {
70 let mut chunk = Chunk::new(ChunkPos(1, 2));
71 chunk.set_block_meta(3, 70, 3, Block::StoneSlab as u16, META_TOP);
72 let encoded = encode_chunk(&chunk);
73 assert_eq!(&encoded[..4], b"VOX3");
74 let mut loaded = Chunk::new(ChunkPos(1, 2));
75 decode_chunk(&encoded, &mut loaded).expect("decode");
76 assert_eq!(loaded.get_block(3, 70, 3), Block::StoneSlab as u16);
77 assert_eq!(loaded.get_meta(3, 70, 3), META_TOP);
78 let mut bad = Chunk::new(ChunkPos(1, 2));
79 assert!(decode_chunk(&[], &mut bad).is_err());
80 assert!(decode_chunk(b"NOPE!", &mut bad).is_err());
81 let mut truncated = encoded.clone();
82 truncated.truncate(10);
83 assert!(decode_chunk(&truncated, &mut bad).is_err());
84 let mut empty_chunk = Chunk::new(ChunkPos(9, 9));
85 let empty_encoded = encode_chunk(&empty_chunk);
86 decode_chunk(&empty_encoded, &mut empty_chunk).expect("empty chunk decodes");
87}
88
89#[test]
90fn block_predicates_cover_shapes_and_light() {
91 assert_eq!(Block::from_id(0), Block::Air);
92 assert_eq!(Block::from_id(9999), Block::Air, "out-of-range ids map to air");
93 assert!(Block::Stone.is_solid());
94 assert!(!Block::Air.is_solid());
95 assert!(!Block::Water.is_solid(), "water is not solid");
96 assert!(!Block::Glass.is_opaque(), "glass is see-through");
97 assert!(Block::Stone.is_opaque());
98 assert_eq!(Block::StoneSlab.shape(), Shape::Slab);
99 assert_eq!(Block::StoneStairs.shape(), Shape::Stairs);
100 assert_eq!(Block::Stone.shape(), Shape::Cube);
101 assert!(!Block::Glass.blocks_light(), "glass lets light through");
102 assert!(Block::StoneSlab.blocks_light(), "a stone slab still casts shadow");
103 assert_eq!(Block::Glowstone.light_emission(), 15);
104 assert_eq!(Block::Stone.light_emission(), 0);
105 assert!(Block::from_id(Block::Glass as u16).is_transparent());
106 assert!(Block::StoneSlab.parent() == Block::Stone);
107 assert_eq!(Block::Stone.slab_of(), Some(Block::StoneSlab));
108 assert_eq!(Block::Dirt.slab_of(), None);
109}
110
111#[test]
112fn inventory_hotbar_rules_hold() {
113 // The rules below are about an empty inventory; `Inventory::default()`
114 // is the new-player starter kit (64 stone among it), not a blank state.
115 let mut inv = Inventory::empty();
116 assert_eq!(inv.selected, 0);
117 inv.select(HOTBAR + 5);
118 assert_eq!(inv.selected, 0, "out-of-hotbar selection is ignored");
119 inv.select(4);
120 assert_eq!(inv.selected, 4);
121 assert!(!inv.try_add_block(0), "air never fits");
122 assert!(inv.try_add_block(Block::Stone as u16));
123 assert_eq!(inv.count_of(Block::Stone as u16), 1);
124 assert_eq!(inv.selected_block(), 0, "slot 4 is still empty");
125 inv.select(0);
126 assert_eq!(inv.selected_block(), Block::Stone as u16);
127 assert_eq!(inv.consume_selected(), Some(Block::Stone as u16));
128 assert_eq!(inv.consume_selected(), None, "slot drained after one consume");
129 for _ in 0..(SLOTS as i32 * STACK + 10) {
130 inv.try_add_block(Block::Dirt as u16);
131 }
132 let total = inv.count_of(Block::Dirt as u16);
133 assert!(total <= (SLOTS as i32) * STACK, "capacity is bounded: {total}");
134 inv.move_item(SLOTS + 1, 0);
135 inv.move_item(0, 0);
136 let mut swap_inv = Inventory::empty();
137 swap_inv.try_add_block(Block::Stone as u16);
138 swap_inv.slots[1].id = Block::Dirt as u16;
139 swap_inv.slots[1].count = 3;
140 swap_inv.move_item(0, 1);
141 assert_eq!(swap_inv.slots[0].id, Block::Dirt as u16);
142 assert_eq!(swap_inv.slots[1].id, Block::Stone as u16);
143 let _ = test_map();
144}