enshwick 1.0.0-alpha.11

The Enshwick Loader's modding API for Enshrouded: the one crate mods are written against
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
//! The world: components, entities, daytime, game settings, weather, terrain and voxels.

use crate::abi::EntityPlace;

use super::FakeHost;
use super::Installed;
use super::table;

impl FakeHost {
    /// The component of type `type_hash` that `entity` has, holding a copy
    /// of `bytes` that the fake keeps, 16-byte aligned, for the rest of the
    /// process; the mod's writes land in that copy.
    ///
    /// # Examples
    ///
    /// ```
    /// let health = [80.0f32.to_le_bytes(), 100.0f32.to_le_bytes()].concat();
    /// let loader = enshwick::test::FakeHost::new()
    ///     .component(enshwick::Entity(4096), 0x027a_c564, &health)
    ///     .template(enshwick::Entity(4096), [0x3a; 16])
    ///     .install();
    /// # let _ = loader;
    /// ```
    #[must_use]
    pub fn component(mut self, entity: crate::Entity, type_hash: u32, bytes: &[u8]) -> Self {
        let mut words = vec![0u128; bytes.len().div_ceil(16).max(1)].into_boxed_slice();
        for (word, chunk) in words.iter_mut().zip(bytes.chunks(16)) {
            let mut le = [0u8; 16];
            le[..chunk.len()].copy_from_slice(chunk);
            *word = u128::from_le_bytes(le);
        }
        let data = Box::leak(words).as_mut_ptr();
        self.components.insert(
            (entity.0, type_hash),
            (data.expose_provenance(), bytes.len()),
        );
        self
    }

    /// Refuses component writes of type `type_hash`, as the loader refuses
    /// the copies the game keeps for clients.
    ///
    /// # Examples
    ///
    /// ```
    /// let loader = enshwick::test::FakeHost::new().refuse_write(0x216e_b5f9).install();
    /// # let _ = loader;
    /// ```
    #[must_use]
    pub fn refuse_write(mut self, type_hash: u32) -> Self {
        self.refused_writes.insert(type_hash);
        self
    }

    /// Makes the component type `type_hash` the static half of its ECS type,
    /// the template's set-up data: reads find it as
    /// [`FakeHost::component`] gives it, and writes are refused with
    /// [`Status::STATIC_HALF`](crate::abi::Status::STATIC_HALF), as the loader
    /// refuses them.
    ///
    /// # Examples
    ///
    /// ```
    /// let loader = enshwick::test::FakeHost::new().static_half(0x139a_fbec).install();
    /// # let _ = loader;
    /// ```
    #[must_use]
    pub fn static_half(mut self, type_hash: u32) -> Self {
        self.static_halves.insert(type_hash);
        self
    }

    /// An entity in the world with a component of type `type_hash`, at
    /// `position` in metres, for `World::entities`
    /// and `World::near`.
    ///
    /// # Examples
    ///
    /// ```
    /// let loader = enshwick::test::FakeHost::new()
    ///     .placed(0x027a_c564, enshwick::Entity(4096), [1.0, 2.0, 3.0])
    ///     .install();
    /// # let _ = loader;
    /// ```
    #[must_use]
    pub fn placed(mut self, type_hash: u32, entity: crate::Entity, position: [f64; 3]) -> Self {
        self.placed.push((
            type_hash,
            EntityPlace {
                entity: crate::Entity(entity.0),
                reserved: 0,
                position: crate::ecs::fixed(position),
            },
        ));
        self
    }

    /// A map marker of the scene, as the loader reads it from the game's
    /// archive for `Map::markers`.
    ///
    /// # Examples
    ///
    /// ```
    /// use enshwick::abi::MapMarkerInfo;
    ///
    /// let marker = MapMarkerInfo { position: [10.0, 40.0, 20.0], ..MapMarkerInfo::EMPTY };
    /// let loader = enshwick::test::FakeHost::new().map_marker(marker).install();
    /// # let _ = loader;
    /// ```
    #[must_use]
    pub fn map_marker(mut self, marker: crate::abi::MapMarkerInfo) -> Self {
        self.map_markers.push(marker);
        self
    }

    /// The frame of the scene's map, as the loader reads it from the game's
    /// archive for `Map::frame`; without one the
    /// scene has no frame.
    ///
    /// # Examples
    ///
    /// ```
    /// use enshwick::abi::MapFrameInfo;
    ///
    /// let frame = MapFrameInfo { size: [10240.0; 2], metres_per_cell: 8.0, ..MapFrameInfo::EMPTY };
    /// let loader = enshwick::test::FakeHost::new().map_frame(frame).install();
    /// # let _ = loader;
    /// ```
    #[must_use]
    pub fn map_frame(mut self, frame: crate::abi::MapFrameInfo) -> Self {
        self.map_frame = Some(frame);
        self
    }

    /// The template GUID of `entity`, in file order.
    ///
    /// # Examples
    ///
    /// See [`FakeHost::component`].
    #[must_use]
    pub fn template(mut self, entity: crate::Entity, guid: [u8; 16]) -> Self {
        self.templates.insert(entity.0, guid);
        self
    }
}

#[expect(
    clippy::unused_self,
    reason = "the fake's state is process-wide; `self` proves it is installed"
)]
impl Installed {
    /// The accounts on the fake server's ban list, in the order banned.
    ///
    /// # Examples
    ///
    /// ```
    /// let loader = enshwick::test::FakeHost::new().install();
    /// assert!(loader.banned().is_empty());
    /// ```
    #[must_use]
    pub fn banned(&self) -> Vec<crate::PlayerKey> {
        table::with(Vec::new(), |s| s.banned.clone())
    }

    /// The accounts whose players the mod removed from the game, by a kick
    /// or a ban, in order.
    ///
    /// # Examples
    ///
    /// ```
    /// let loader = enshwick::test::FakeHost::new().install();
    /// assert!(loader.removed().is_empty());
    /// ```
    #[must_use]
    pub fn removed(&self) -> Vec<u64> {
        table::with(Vec::new(), |s| s.removed.clone())
    }

    /// The accounts on the fake server's allowlist, in the order listed.
    ///
    /// # Examples
    ///
    /// ```
    /// let loader = enshwick::test::FakeHost::new().install();
    /// assert!(loader.allowlisted().is_empty());
    /// ```
    #[must_use]
    pub fn allowlisted(&self) -> Vec<crate::PlayerKey> {
        table::with(Vec::new(), |s| s.allowlisted.clone())
    }

    /// Stops the world, as between two worlds: the world's reads answer
    /// that none runs.
    ///
    /// # Examples
    ///
    /// ```
    /// let loader = enshwick::test::FakeHost::new().install();
    /// loader.stop_world();
    /// ```
    pub fn stop_world(&self) {
        table::with((), |s| s.edits.stopped = true);
    }

    /// Has the scene's map markers and map frame still being read while
    /// `pending`; as the loader does, the fake answers the first call of
    /// each in a world that way whatever this says.
    ///
    /// # Examples
    ///
    /// ```
    /// let loader = enshwick::test::FakeHost::new().install();
    /// loader.read_map_markers(true);
    /// ```
    pub fn read_map_markers(&self, pending: bool) {
        table::with((), |s| s.edits.markers_pending = pending);
    }

    /// Sets the clock the fake's `time_of_day` reports; until then the
    /// clock is missing, as before a world runs.
    ///
    /// # Examples
    ///
    /// ```
    /// use enshwick::abi::{DaytimeInfo, Header};
    ///
    /// let loader = enshwick::test::FakeHost::new().install();
    /// loader.set_daytime(DaytimeInfo {
    ///     header: Header::of::<DaytimeInfo>(),
    ///     time: 0,
    ///     day_start: 6 * 3_600_000_000_000,
    ///     day_end: 18 * 3_600_000_000_000,
    ///     day_length: 1_800_000_000_000,
    ///     night_length: 720_000_000_000,
    ///     speed: 1.0,
    ///     flags: 0,
    /// });
    /// assert_eq!(loader.daytime().map(|info| info.time), Some(0));
    /// ```
    pub fn set_daytime(&self, info: crate::abi::DaytimeInfo) {
        table::with((), |s| s.daytime = Some(info));
    }

    /// The clock as the mod left it; a time it set moves the clock and its
    /// day flag.
    ///
    /// # Examples
    ///
    /// See [`Installed::set_daytime`].
    #[must_use]
    pub fn daytime(&self) -> Option<crate::abi::DaytimeInfo> {
        table::with(None, |s| s.daytime)
    }

    /// The mod's factor on the clock's speed, once it asked to change it.
    ///
    /// # Examples
    ///
    /// ```
    /// let loader = enshwick::test::FakeHost::new().install();
    /// assert_eq!(loader.clock_speed(), None);
    /// ```
    #[must_use]
    pub fn clock_speed(&self) -> Option<f32> {
        table::with(None, |s| s.clock_speed)
    }

    /// Sets the game settings the fake reports, the engine's settings struct
    /// as bytes of its reflected size, and their preset; until then no
    /// world runs. A change the mod makes replaces them and makes the
    /// preset custom (4).
    ///
    /// # Examples
    ///
    /// ```
    /// let loader = enshwick::test::FakeHost::new().install();
    /// loader.set_game_settings(vec![0; 144], 0);
    /// assert_eq!(loader.game_settings().map(|(bytes, _)| bytes.len()), Some(144));
    /// ```
    pub fn set_game_settings(&self, bytes: Vec<u8>, preset: u32) {
        table::with((), |s| s.game_settings = Some((bytes, preset)));
    }

    /// The game settings' bytes and preset as the mod left them.
    ///
    /// # Examples
    ///
    /// See [`Installed::set_game_settings`].
    #[must_use]
    pub fn game_settings(&self) -> Option<(Vec<u8>, u32)> {
        table::with(None, |s| s.game_settings.clone())
    }

    /// The game settings' bytes the mod kept with the world, by offset:
    /// `None` for a byte no kept field covers; empty before any is kept or
    /// after the mod forgot them.
    ///
    /// # Examples
    ///
    /// ```
    /// let loader = enshwick::test::FakeHost::new().install();
    /// assert!(loader.kept_game_settings().is_empty());
    /// ```
    #[must_use]
    pub fn kept_game_settings(&self) -> Vec<Option<u8>> {
        table::with(Vec::new(), |s| s.kept_settings.clone())
    }

    /// Sets the weather the fake reports: the weights everywhere (clear,
    /// rain, snow, blizzard) and the fronts; until then no world runs.
    ///
    /// # Examples
    ///
    /// ```
    /// let loader = enshwick::test::FakeHost::new().install();
    /// loader.set_weather([1.0, 0.0, 0.0, 0.0], &[]);
    /// assert!(loader.forecasts().is_empty());
    /// ```
    pub fn set_weather(&self, weights: [f32; 4], fronts: &[crate::abi::WeatherFront]) {
        table::with((), |s| s.weather = Some((weights, fronts.to_vec())));
    }

    /// The weather the mod started: effect (`u32::MAX` for the game's
    /// choice) and duration in game nanoseconds.
    ///
    /// # Examples
    ///
    /// See [`Installed::set_weather`].
    #[must_use]
    pub fn forecasts(&self) -> Vec<(u32, i64)> {
        table::with(Vec::new(), |s| s.forecasts.clone())
    }

    /// Sets the terrain materials the fake lists; until then no world runs.
    /// Every edit the mod asks for is recorded and moves one cubic metre.
    ///
    /// # Examples
    ///
    /// ```
    /// let loader = enshwick::test::FakeHost::new().install();
    /// loader.set_terrain(&[]);
    /// assert!(loader.terrain_edits().is_empty());
    /// ```
    pub fn set_terrain(&self, materials: &[crate::abi::TerrainMaterialInfo]) {
        table::with((), |s| s.terrain = Some(materials.to_vec()));
    }

    /// The terrain edits the mod asked for, in order.
    ///
    /// # Examples
    ///
    /// See [`Installed::set_terrain`].
    #[must_use]
    pub fn terrain_edits(&self) -> Vec<crate::abi::TerrainEdit> {
        table::with(Vec::new(), |s| s.edits.terrain.clone())
    }

    /// The labels the mod set, as `(entity, text)`, in order.
    ///
    /// # Examples
    ///
    /// ```
    /// let loader = enshwick::test::FakeHost::new().install();
    /// assert!(loader.labels().is_empty());
    /// ```
    #[must_use]
    pub fn labels(&self) -> Vec<(u32, String)> {
        table::with(Vec::new(), |s| s.labels.clone())
    }

    /// The entities the mod asked the world to destroy, in order.
    ///
    /// # Examples
    ///
    /// ```
    /// let loader = enshwick::test::FakeHost::new().install();
    /// assert!(loader.destroyed().is_empty());
    /// ```
    #[must_use]
    pub fn destroyed(&self) -> Vec<u32> {
        table::with(Vec::new(), |s| s.destroyed.clone())
    }

    /// The components the mod asked to write, `(entity, type hash)`, in
    /// order; the fake hands out the data [`FakeHost::component`] gave.
    ///
    /// # Examples
    ///
    /// ```
    /// let loader = enshwick::test::FakeHost::new().install();
    /// assert!(loader.writes().is_empty());
    /// ```
    #[must_use]
    pub fn writes(&self) -> Vec<(u32, u32)> {
        table::with(Vec::new(), |s| s.writes.clone())
    }

    /// The bytes of the component [`FakeHost::component`] gave `entity`, as
    /// the mod's writes left them.
    ///
    /// # Examples
    ///
    /// ```
    /// let loader = enshwick::test::FakeHost::new().component(enshwick::Entity(7), 1, &[4, 2]).install();
    /// assert_eq!(loader.component_bytes(enshwick::Entity(7), 1), Some(vec![4, 2]));
    /// assert_eq!(loader.component_bytes(enshwick::Entity(7), 2), None);
    /// ```
    #[must_use]
    pub fn component_bytes(&self, entity: crate::Entity, type_hash: u32) -> Option<Vec<u8>> {
        let (address, len) = table::with(None, |s| {
            s.host.components.get(&(entity.0, type_hash)).copied()
        })?;
        let data: *const u8 = core::ptr::with_exposed_provenance(address);
        // SAFETY: `component` leaked `len` initialised bytes at `address`,
        // and no write of the mod's runs while the test reads.
        Some(unsafe { core::slice::from_raw_parts(data, len) }.to_vec())
    }

    /// The game events the mod raised, `(type hash, bytes)`, in order.
    ///
    /// # Examples
    ///
    /// ```
    /// let loader = enshwick::test::FakeHost::new().install();
    /// assert!(loader.emits().is_empty());
    /// ```
    #[must_use]
    pub fn emits(&self) -> Vec<(u32, Vec<u8>)> {
        table::with(Vec::new(), |s| s.emits.clone())
    }

    /// The teleports the mod asked for, in order; an entity the fake does
    /// not know is refused.
    ///
    /// # Examples
    ///
    /// ```
    /// let loader = enshwick::test::FakeHost::new().install();
    /// assert!(loader.teleports().is_empty());
    /// ```
    #[must_use]
    pub fn teleports(&self) -> Vec<crate::abi::TeleportRequest> {
        table::with(Vec::new(), |s| s.teleports.clone())
    }

    /// The spawns the mod asked for, with the entity ids the fake handed
    /// out, in order.
    ///
    /// # Examples
    ///
    /// ```
    /// let loader = enshwick::test::FakeHost::new().install();
    /// assert!(loader.spawns().is_empty());
    /// ```
    #[must_use]
    pub fn spawns(&self) -> Vec<(u32, crate::abi::SpawnRequest)> {
        table::with(Vec::new(), |s| s.spawns.clone())
    }

    /// How many saves the mod asked for.
    ///
    /// # Examples
    ///
    /// ```
    /// let loader = enshwick::test::FakeHost::new().install();
    /// assert_eq!(loader.saves(), 0);
    /// ```
    #[must_use]
    pub fn saves(&self) -> usize {
        table::with(0, |s| s.saves)
    }

    /// The voxel boxes the mod read on the game thread, `(min, size)`, in
    /// order, whatever the read answered.
    ///
    /// # Examples
    ///
    /// ```
    /// let loader = enshwick::test::FakeHost::new().install();
    /// assert!(loader.voxel_reads().is_empty());
    /// ```
    #[must_use]
    pub fn voxel_reads(&self) -> Vec<([u32; 3], [u32; 3])> {
        table::with(Vec::new(), |s| s.edits.voxel_reads.clone())
    }
}

impl FakeHost {
    /// Loaded voxels of the world: the box of `size` from `min`, its
    /// `voxels` x fastest, then y, then z. A read answers from the boxes,
    /// the last given first where they overlap, and finds a box it does not
    /// cover wholly not loaded, as the loader does where the world is not
    /// loaded yet;
    /// once the fake has boxes, a terrain edit whose position lies in none
    /// is not loaded either.
    /// Past the world's edge ([`FakeHost::world_extent`]) a read is outside
    /// the world.
    ///
    /// # Panics
    ///
    /// When `voxels` does not hold one voxel for each place in the box.
    ///
    /// # Examples
    ///
    /// ```
    /// use enshwick::voxels::{Voxel, VoxelMaterial, VoxelPos};
    ///
    /// let rock = Voxel::new(VoxelMaterial::Terrain(9), 255);
    /// let loader = enshwick::test::FakeHost::new()
    ///     .voxels(VoxelPos::new(0, 0, 0), [2, 1, 1], &[rock, Voxel::default()])
    ///     .install();
    /// # let _ = loader;
    /// ```
    #[must_use]
    pub fn voxels(
        mut self,
        min: crate::voxels::VoxelPos,
        size: [u32; 3],
        voxels: &[crate::voxels::Voxel],
    ) -> Self {
        let count = size.iter().map(|&axis| axis as usize).product::<usize>();
        assert_eq!(
            voxels.len(),
            count,
            "a fake voxel box of {size:?} takes {count} voxels"
        );
        self.voxels.push(VoxelGrid {
            min: min.to_array(),
            size,
            voxels: voxels.iter().map(|voxel| voxel.to_abi()).collect(),
        });
        self
    }
}

impl FakeHost {
    /// The world's end on each axis, in voxels: a read of a box past it,
    /// and an edit or a transaction's change whose position lies past it,
    /// is outside the world, as the loader refuses them. The loader takes
    /// the edge across from the game and its own height; the fake checks
    /// an edit's position, where the loader checks every voxel the edit can
    /// write. Without it, the fake has no edge.
    ///
    /// # Examples
    ///
    /// ```
    /// let loader = enshwick::test::FakeHost::new().world_extent([20_480, 2048, 20_480]).install();
    /// # let _ = loader;
    /// ```
    #[must_use]
    pub const fn world_extent(mut self, end: [u32; 3]) -> Self {
        self.extent = Some(end);
        self
    }
}

/// Whether the box of `size` from `min` leaves a world ending at `end`.
pub(crate) fn outside(end: Option<[u32; 3]>, min: [u32; 3], size: [u32; 3]) -> bool {
    end.is_some_and(|end| {
        (0..3).any(|axis| {
            min[axis]
                .checked_add(size[axis])
                .is_none_or(|stop| stop > end[axis])
        })
    })
}

/// Whether the edit's position lies past a world ending at `end`.
pub(crate) fn edit_outside(end: Option<[u32; 3]>, edit: &crate::abi::TerrainEdit) -> bool {
    end.is_some_and(|end| {
        edit.position
            .iter()
            .zip(end)
            .any(|(&metres, stop)| f64::from(metres) * 2.0 >= f64::from(stop))
    })
}

/// Whether the edit's position lies in one of `grids`, when the fake has
/// any: the loader checks every column of bricks the edit can write.
pub(crate) fn edit_loaded(grids: &[VoxelGrid], edit: &crate::abi::TerrainEdit) -> bool {
    #[expect(
        clippy::cast_possible_truncation,
        clippy::cast_sign_loss,
        reason = "a checked edit's position is finite and at 0 or more; `as` saturates"
    )]
    let at = edit.position.map(|metres| (f64::from(metres) * 2.0) as u32);
    grids.is_empty() || grids.iter().any(|grid| grid.get(at).is_some())
}

/// A box of loaded voxels the fake answers reads from.
#[derive(Clone, Debug)]
pub(crate) struct VoxelGrid {
    min: [u32; 3],
    size: [u32; 3],
    voxels: Vec<crate::abi::Voxel>,
}

impl VoxelGrid {
    /// The voxel at `at`, when the grid holds it.
    fn get(&self, at: [u32; 3]) -> Option<crate::abi::Voxel> {
        let mut index = 0usize;
        for axis in (0..3).rev() {
            let offset = at[axis].checked_sub(self.min[axis])?;
            if offset >= self.size[axis] {
                return None;
            }
            index = index * self.size[axis] as usize + offset as usize;
        }
        self.voxels.get(index).copied()
    }

    /// The voxels of the box of `size` from `min`, x fastest, then y, then
    /// z, from the last of `grids` that holds each; `None` when one is in
    /// none of them.
    pub(crate) fn read(
        grids: &[Self],
        min: [u32; 3],
        size: [u32; 3],
    ) -> Option<Vec<crate::abi::Voxel>> {
        let [width, height, depth] = size;
        let mut voxels = Vec::new();
        for z in 0..depth {
            for y in 0..height {
                for x in 0..width {
                    let at = [min[0] + x, min[1] + y, min[2] + z];
                    voxels.push(grids.iter().rev().find_map(|grid| grid.get(at))?);
                }
            }
        }
        Some(voxels)
    }
}

impl FakeHost {
    /// A Flame Altar's building zone from `min` to `max` in metres, where
    /// `World::can_build` asks for
    /// `CanEditBase`.
    ///
    /// # Examples
    ///
    /// ```
    /// let loader = enshwick::test::FakeHost::new()
    ///     .building_zone([0.0, 0.0, 0.0], [40.0, 40.0, 40.0])
    ///     .install();
    /// # let _ = loader;
    /// ```
    #[must_use]
    pub fn building_zone(mut self, min: [f64; 3], max: [f64; 3]) -> Self {
        self.zones.building.push(FakeBox { min, max });
        self
    }

    /// A box of the world's building area; once one is given, a corner
    /// outside every such box is refused, as past the world's edge.
    ///
    /// # Examples
    ///
    /// ```
    /// let loader = enshwick::test::FakeHost::new()
    ///     .building_area([0.0, 0.0, 0.0], [4096.0, 1024.0, 4096.0])
    ///     .install();
    /// # let _ = loader;
    /// ```
    #[must_use]
    pub fn building_area(mut self, min: [f64; 3], max: [f64; 3]) -> Self {
        self.zones.area.push(FakeBox { min, max });
        self
    }

    /// A no-build zone from `min` to `max` in metres; only a `strict` one
    /// refuses an edit, as the game's own rule for an edit without an item.
    ///
    /// # Examples
    ///
    /// ```
    /// let loader = enshwick::test::FakeHost::new()
    ///     .no_build_zone([10.0, 0.0, 10.0], [20.0, 10.0, 20.0], true)
    ///     .install();
    /// # let _ = loader;
    /// ```
    #[must_use]
    pub fn no_build_zone(mut self, min: [f64; 3], max: [f64; 3], strict: bool) -> Self {
        self.zones.no_build.push((FakeBox { min, max }, strict));
        self
    }
}

/// A box in metres.
#[derive(Clone, Copy, Debug)]
pub(crate) struct FakeBox {
    min: [f64; 3],
    max: [f64; 3],
}

impl FakeBox {
    /// Whether the boxes overlap with some thickness on every axis: boxes
    /// that share only a face do not.
    fn overlaps(&self, min: [f64; 3], max: [f64; 3]) -> bool {
        (0..3).all(|a| self.min[a] < max[a] && min[a] < self.max[a])
    }

    /// Whether `point` lies inside, the high faces excluded.
    fn holds(&self, point: [f64; 3]) -> bool {
        (0..3).all(|a| self.min[a] <= point[a] && point[a] < self.max[a])
    }
}

/// The zones the fake answers build rights from.
#[derive(Clone, Debug, Default)]
pub(crate) struct FakeZones {
    building: Vec<FakeBox>,
    area: Vec<FakeBox>,
    no_build: Vec<(FakeBox, bool)>,
}

impl FakeZones {
    /// Whether a save keeps changes in the box from `min` to `max`, as the
    /// loader decides: wholly inside one building zone, half a metre in
    /// from its edges.
    pub(crate) fn kept(&self, min: [f64; 3], max: [f64; 3]) -> bool {
        const MARGIN: f64 = 0.5;
        self.building.iter().any(|zone| {
            (0..3).all(|a| min[a] >= zone.min[a] + MARGIN && max[a] <= zone.max[a] - MARGIN)
        })
    }

    /// The refusal of an edit of the box from `min` to `max` by a player
    /// with the permission mask `permissions`, as the loader decides: the
    /// game's terrain tool rule for the permissions, then the building area
    /// and strict no-build tests Enshwick adds from the building cursor.
    pub(crate) fn refusal(&self, permissions: u8, min: [f64; 3], max: [f64; 3]) -> u32 {
        use crate::abi::{BuildRights, PlayerInfo};
        if permissions & PlayerInfo::CAN_EDIT_WORLD == 0 {
            return BuildRights::NO_CAN_EDIT_WORLD;
        }
        if permissions & PlayerInfo::CAN_EDIT_BASE == 0
            && self.building.iter().any(|zone| zone.overlaps(min, max))
        {
            return BuildRights::NO_CAN_EDIT_BASE;
        }
        let in_area = |corner| self.area.is_empty() || self.area.iter().any(|b| b.holds(corner));
        if !in_area(min) || !in_area(max) {
            return BuildRights::OUTSIDE_BUILDING_ZONE;
        }
        if self
            .no_build
            .iter()
            .any(|(zone, strict)| *strict && zone.overlaps(min, max))
        {
            return BuildRights::INSIDE_STRICT_BUILD_INHIBITOR_ZONE;
        }
        BuildRights::ALLOWED
    }
}