Theme

alex/beta-minecraft-rust

Minecraft Beta 1.7.3 Rewrite

BRANCH main
main
SNAPSHOT Draw terrain GPU-driven from packed per-section buffers with GPU occlusion culling, add a render distance slider (2-128) with a quad/disk shape, cut chunk bookkeeping at high render distances, and fix a light underflow at y=0
DOWNLOAD
Download file

root / crates / server / src / level / ticks.rs

use std::collections::BTreeSet;

use common::world::Block;
use rustc_hash::FxHashSet;

#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
pub struct TickTarget {
    pub x: i32,
    pub y: i32,
    pub z: i32,
    pub block: Block,
}

/// Port of the `scheduledTickTreeSet`/`scheduledTickSet` pair: due ticks ordered by
/// time, then by creation order, with at most one pending tick per block and position.
#[derive(Default)]
pub struct TickList {
    queue: BTreeSet<(i64, u64, TickTarget)>,
    pending: FxHashSet<TickTarget>,
    next_id: u64,
}

impl TickList {
    pub fn schedule(&mut self, target: TickTarget, time: i64) {
        let id = self.next_id;
        self.next_id += 1;
        if self.pending.insert(target) {
            self.queue.insert((time, id, target));
        }
    }

    /// Removes and returns the earliest tick if it is due at `time`.
    pub fn pop_due(&mut self, time: i64) -> Option<TickTarget> {
        let first = self.queue.first().filter(|entry| entry.0 <= time)?;
        let target = first.2;
        self.queue.pop_first();
        self.pending.remove(&target);
        Some(target)
    }

    /// Moves every pending tick by `delta` ticks.
    pub fn shift(&mut self, delta: i64) {
        self.queue = std::mem::take(&mut self.queue)
            .into_iter()
            .map(|(time, id, target)| (time + delta, id, target))
            .collect();
    }

    pub fn len(&self) -> usize {
        self.queue.len()
    }
}