code wiki / (root) / nx_world_seed_test.nx

nx_world_seed_test.nx source

↩ module page · 90 lines · 2921 B

1// nx_world_seed_test.nx -- smoke for nx_world_seed. 2 3import "nx_syscalls.nx" 4import "nx_palette.nx" 5import "nx_tissue.nx" 6import "nx_world_seed.nx" 7 8func main() -> i64 { 9 // xorshift64 is deterministic 10 let v1: nx_size = nx_xorshift64(42) 11 let v2: nx_size = nx_xorshift64(42) 12 if v1 != v2 { return 1 } 13 // Different inputs -> different outputs 14 let v3: nx_size = nx_xorshift64(43) 15 if v1 == v3 { return 2 } 16 17 // State carries through PRNG 18 let st: *NxWorldSeedState = nx_world_seed_state_new(12345) 19 if st.seed != 12345 { return 3 } 20 let r1: nx_size = nx_world_seed_next(st) 21 let r2: nx_size = nx_world_seed_next(st) 22 if r1 == r2 { return 4 } 23 // Different seeds -> different sequences 24 let st2: *NxWorldSeedState = nx_world_seed_state_new(67890) 25 let r1b: nx_size = nx_world_seed_next(st2) 26 if r1 == r1b { return 5 } 27 28 // Generate a world 29 let w1: *NxTissue = nx_tissue_new(8, 8, 8, NX_BPP_3) 30 let cells1: nx_int = nx_world_seed_generate(w1, 42) 31 if cells1 <= 0 { return 6 } 32 // Most cells should be non-zero (terrain fills most of the volume) 33 let nonzero1: nx_size = nx_tissue_count_nonzero(w1) 34 if nonzero1 < 64 { return 7 } // at least 1 layer of stone 35 36 // Generate again with same seed -> bit-identical world 37 let w2: *NxTissue = nx_tissue_new(8, 8, 8, NX_BPP_3) 38 nx_world_seed_generate(w2, 42) 39 var z: nx_size = 0 40 while z < 8 { 41 var y: nx_size = 0 42 while y < 8 { 43 var x: nx_size = 0 44 while x < 8 { 45 if nx_tissue_get(w1, x, y, z) != nx_tissue_get(w2, x, y, z) { return 8 } 46 x = x + 1 47 } 48 y = y + 1 49 } 50 z = z + 1 51 } 52 53 // Different seed -> different world (high probability) 54 let w3: *NxTissue = nx_tissue_new(8, 8, 8, NX_BPP_3) 55 nx_world_seed_generate(w3, 999) 56 var diff: nx_int = 0 57 z = 0 58 while z < 8 { 59 var y: nx_size = 0 60 while y < 8 { 61 var x: nx_size = 0 62 while x < 8 { 63 if nx_tissue_get(w1, x, y, z) != nx_tissue_get(w3, x, y, z) { diff = diff + 1 } 64 x = x + 1 65 } 66 y = y + 1 67 } 68 z = z + 1 69 } 70 if diff == 0 { return 9 } // worlds must differ at some voxel 71 72 // Stone floor (z=0..2) is uniformly stone regardless of seed 73 var bx: nx_size = 0 74 while bx < 8 { 75 var by: nx_size = 0 76 while by < 8 { 77 if nx_tissue_get(w1, bx, by, 0) != NX_WS_PAL_STONE { return 10 } 78 if nx_tissue_get(w1, bx, by, 1) != NX_WS_PAL_STONE { return 11 } 79 if nx_tissue_get(w1, bx, by, 2) != NX_WS_PAL_STONE { return 12 } 80 by = by + 1 81 } 82 bx = bx + 1 83 } 84 85 // Bad tissue refused 86 let nullw: *NxTissue = (0 as i64) as *NxTissue 87 if nx_world_seed_generate(nullw, 1) != NX_WS_ERR_BAD_TISSUE { return 13 } 88 89 return 0 90}