Skip to main content

steel_core/chunk_saver/storage/
ticks.rs

1use super::{
2    BlockPos, BlockRef, ChunkPos, ChunkStorage, FluidRef, PersistentTick, REGISTRY, RegistryExt,
3    SavedTick, TickPriority,
4};
5
6impl ChunkStorage {
7    /// Converts block ticks to persistent format for saving.
8    pub(super) fn block_ticks_to_persistent(
9        ticks: Vec<SavedTick<BlockRef>>,
10        chunk_pos: ChunkPos,
11    ) -> Vec<PersistentTick> {
12        ticks
13            .into_iter()
14            .map(|t| PersistentTick {
15                x: (t.pos.0.x - chunk_pos.0.x * 16) as u8,
16                y: t.pos.0.y as i16,
17                z: (t.pos.0.z - chunk_pos.0.y * 16) as u8,
18                delay: t.delay,
19                priority: t.priority as i8,
20                tick_type: t.tick_type.key.clone(),
21            })
22            .collect()
23    }
24
25    /// Converts fluid ticks to persistent format for saving.
26    pub(super) fn fluid_ticks_to_persistent(
27        ticks: Vec<SavedTick<FluidRef>>,
28        chunk_pos: ChunkPos,
29    ) -> Vec<PersistentTick> {
30        ticks
31            .into_iter()
32            .map(|t| PersistentTick {
33                x: (t.pos.0.x - chunk_pos.0.x * 16) as u8,
34                y: t.pos.0.y as i16,
35                z: (t.pos.0.z - chunk_pos.0.y * 16) as u8,
36                delay: t.delay,
37                priority: t.priority as i8,
38                tick_type: t.tick_type.key.clone(),
39            })
40            .collect()
41    }
42
43    /// Reconstructs saved block ticks from persistent data.
44    pub(super) fn persistent_to_block_saved_ticks(
45        persistent: &[PersistentTick],
46        chunk_pos: ChunkPos,
47    ) -> Vec<SavedTick<BlockRef>> {
48        persistent
49            .iter()
50            .filter_map(|pt| {
51                let block = REGISTRY.blocks.by_key(&pt.tick_type)?;
52                let pos = BlockPos::new(
53                    chunk_pos.0.x * 16 + i32::from(pt.x),
54                    i32::from(pt.y),
55                    chunk_pos.0.y * 16 + i32::from(pt.z),
56                );
57                let priority = TickPriority::by_value(i32::from(pt.priority));
58                Some(SavedTick {
59                    tick_type: block,
60                    pos,
61                    delay: pt.delay,
62                    priority,
63                })
64            })
65            .collect()
66    }
67
68    /// Reconstructs saved fluid ticks from persistent data.
69    pub(super) fn persistent_to_fluid_saved_ticks(
70        persistent: &[PersistentTick],
71        chunk_pos: ChunkPos,
72    ) -> Vec<SavedTick<FluidRef>> {
73        persistent
74            .iter()
75            .filter_map(|pt| {
76                let fluid = REGISTRY.fluids.by_key(&pt.tick_type)?;
77                let pos = BlockPos::new(
78                    chunk_pos.0.x * 16 + i32::from(pt.x),
79                    i32::from(pt.y),
80                    chunk_pos.0.y * 16 + i32::from(pt.z),
81                );
82                let priority = TickPriority::by_value(i32::from(pt.priority));
83                Some(SavedTick {
84                    tick_type: fluid,
85                    pos,
86                    delay: pt.delay,
87                    priority,
88                })
89            })
90            .collect()
91    }
92}