Skip to content

Commit

Permalink
Remove redundant DualGraph typedef
Browse files Browse the repository at this point in the history
  • Loading branch information
patowen committed Oct 25, 2023
1 parent c6efce7 commit f8f9ec8
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 45 deletions.
8 changes: 4 additions & 4 deletions client/src/prediction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::VecDeque;

use common::{
character_controller,
node::DualGraph,
graph::Graph,
proto::{CharacterInput, Position},
SimConfig,
};
Expand Down Expand Up @@ -35,7 +35,7 @@ impl PredictedMotion {

/// Update for input about to be sent to the server, returning the generation it should be
/// tagged with
pub fn push(&mut self, cfg: &SimConfig, graph: &DualGraph, input: &CharacterInput) -> u16 {
pub fn push(&mut self, cfg: &SimConfig, graph: &Graph, input: &CharacterInput) -> u16 {
character_controller::run_character_step(
cfg,
graph,
Expand All @@ -54,7 +54,7 @@ impl PredictedMotion {
pub fn reconcile(
&mut self,
cfg: &SimConfig,
graph: &DualGraph,
graph: &Graph,
generation: u16,
position: Position,
velocity: na::Vector3<f32>,
Expand Down Expand Up @@ -113,7 +113,7 @@ mod tests {
#[test]
fn wraparound() {
let mock_cfg = SimConfig::from_raw(&common::SimConfigRaw::default());
let mut mock_graph = DualGraph::new();
let mut mock_graph = Graph::new();
common::node::populate_fresh_nodes(&mut mock_graph);
let mock_character_input = CharacterInput {
movement: na::Vector3::x(),
Expand Down
8 changes: 4 additions & 4 deletions client/src/sim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ use crate::{
};
use common::{
character_controller,
graph::NodeId,
node::{populate_fresh_nodes, DualGraph},
graph::{Graph, NodeId},
node::populate_fresh_nodes,
proto::{self, Character, CharacterInput, CharacterState, Command, Component, Position},
sanitize_motion_input, EntityId, GraphEntities, SimConfig, Step,
};

/// Game state
pub struct Sim {
// World state
pub graph: DualGraph,
pub graph: Graph,
pub graph_entities: GraphEntities,
entity_ids: FxHashMap<EntityId, Entity>,
pub world: hecs::World,
Expand Down Expand Up @@ -53,7 +53,7 @@ pub struct Sim {

impl Sim {
pub fn new(cfg: SimConfig, local_character_id: EntityId) -> Self {
let mut graph = DualGraph::new();
let mut graph = Graph::new();
populate_fresh_nodes(&mut graph);
Self {
graph,
Expand Down
6 changes: 3 additions & 3 deletions common/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use common::{
dodeca::{Side, Vertex},
graph::{Graph, NodeId},
node::Chunk,
node::{populate_fresh_nodes, ChunkId, DualGraph},
node::{populate_fresh_nodes, ChunkId},
proto::Position,
traversal::ensure_nearby,
worldgen::ChunkParams,
Expand All @@ -25,7 +25,7 @@ fn build_graph(c: &mut Criterion) {

c.bench_function("nodegen 1000", |b| {
b.iter(|| {
let mut graph = DualGraph::new();
let mut graph = Graph::new();
let mut n = NodeId::ROOT;
for _ in 0..500 {
n = graph.ensure_neighbor(n, Side::A);
Expand All @@ -38,7 +38,7 @@ fn build_graph(c: &mut Criterion) {

c.bench_function("worldgen", |b| {
b.iter(|| {
let mut graph = DualGraph::new();
let mut graph = Graph::new();
ensure_nearby(&mut graph, &Position::origin(), 3.0);
let fresh = graph.fresh().to_vec();
populate_fresh_nodes(&mut graph);
Expand Down
8 changes: 2 additions & 6 deletions common/src/character_controller/collision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
use tracing::error;

use crate::{
graph_collision, math,
node::{ChunkLayout, DualGraph},
proto::Position,
};
use crate::{graph::Graph, graph_collision, math, node::ChunkLayout, proto::Position};

/// Checks for collisions when a character moves with a character-relative displacement vector of `relative_displacement`.
pub fn check_collision(
Expand Down Expand Up @@ -71,7 +67,7 @@ pub fn check_collision(

/// Contains information about the character and the world that is only relevant for collision checking
pub struct CollisionContext<'a> {
pub graph: &'a DualGraph,
pub graph: &'a Graph,
pub chunk_layout: ChunkLayout,
pub radius: f32,
}
Expand Down
5 changes: 3 additions & 2 deletions common/src/character_controller/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ use crate::{
collision::{check_collision, Collision, CollisionContext},
vector_bounds::{BoundedVectors, VectorBound},
},
graph::Graph,
math,
node::{ChunkLayout, DualGraph},
node::ChunkLayout,
proto::{CharacterInput, Position},
sanitize_motion_input,
sim_config::CharacterConfig,
Expand All @@ -21,7 +22,7 @@ use crate::{
/// Runs a single step of character movement
pub fn run_character_step(
sim_config: &SimConfig,
graph: &DualGraph,
graph: &Graph,
position: &mut Position,
velocity: &mut na::Vector3<f32>,
on_ground: &mut bool,
Expand Down
17 changes: 9 additions & 8 deletions common/src/graph_collision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ use fxhash::FxHashSet;
use crate::{
chunk_collision::chunk_sphere_cast,
dodeca::{self, Vertex},
graph::Graph,
math,
node::{Chunk, ChunkId, ChunkLayout, DualGraph},
node::{Chunk, ChunkId, ChunkLayout},
proto::Position,
};

/// Performs sphere casting (swept collision query) against the voxels in the `DualGraph`
/// Performs sphere casting (swept collision query) against the voxels in the `Graph`
///
/// The `ray` parameter and any resulting hit normals are given in the local coordinate system of `position`.
///
Expand All @@ -21,7 +22,7 @@ use crate::{
/// the closest node with ungenerated chunks is greater than `cast_distance + collider_radius + dodeca::BOUNDING_SPHERE_RADIUS`
pub fn sphere_cast(
collider_radius: f32,
graph: &DualGraph,
graph: &Graph,
layout: &ChunkLayout,
position: &Position,
ray: &Ray,
Expand Down Expand Up @@ -201,7 +202,7 @@ impl std::ops::Mul<&Ray> for na::Matrix4<f32> {
mod tests {
use crate::{
dodeca::{Side, Vertex},
graph::NodeId,
graph::{Graph, NodeId},
node::{populate_fresh_nodes, VoxelData},
proto::Position,
traversal::{ensure_nearby, nearby_nodes},
Expand Down Expand Up @@ -260,7 +261,7 @@ mod tests {
fn execute(self) {
let dimension: usize = 12;
let layout = ChunkLayout::new(dimension);
let mut graph = DualGraph::new();
let mut graph = Graph::new();
let graph_radius = 3.0;

// Set up a graph with void chunks
Expand Down Expand Up @@ -342,7 +343,7 @@ mod tests {
}
}

fn populate_voxel(graph: &mut DualGraph, dimension: usize, voxel_location: &VoxelLocation) {
fn populate_voxel(graph: &mut Graph, dimension: usize, voxel_location: &VoxelLocation) {
// Find the ChunkId of the given chunk
let chunk = ChunkId::new(
voxel_location
Expand All @@ -366,7 +367,7 @@ mod tests {
+ voxel_location.coords[2] * (dimension + 2).pow(2)] = Material::Dirt;
}

fn get_voxel_chunk(graph: &DualGraph, voxel_location: &VoxelLocation) -> ChunkId {
fn get_voxel_chunk(graph: &Graph, voxel_location: &VoxelLocation) -> ChunkId {
ChunkId::new(
voxel_location
.node_path
Expand Down Expand Up @@ -513,7 +514,7 @@ mod tests {
fn sphere_cast_near_unloaded_chunk() {
let dimension: usize = 12;
let layout = ChunkLayout::new(dimension);
let mut graph = DualGraph::new();
let mut graph = Graph::new();

let sides = Vertex::A.canonical_sides();

Expand Down
14 changes: 6 additions & 8 deletions common/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ use crate::world::Material;
use crate::worldgen::NodeState;
use crate::{math, Chunks};

pub type DualGraph = Graph;

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct ChunkId {
pub node: NodeId,
Expand All @@ -24,7 +22,7 @@ impl ChunkId {
}
}

impl DualGraph {
impl Graph {
pub fn get_chunk_mut(&mut self, chunk: ChunkId) -> Option<&mut Chunk> {
Some(&mut self.get_mut(chunk.node).as_mut()?.chunks[chunk.vertex])
}
Expand All @@ -43,15 +41,15 @@ impl DualGraph {
}
}

impl Index<ChunkId> for DualGraph {
impl Index<ChunkId> for Graph {
type Output = Chunk;

fn index(&self, chunk: ChunkId) -> &Chunk {
self.get_chunk(chunk).unwrap()
}
}

impl IndexMut<ChunkId> for DualGraph {
impl IndexMut<ChunkId> for Graph {
fn index_mut(&mut self, chunk: ChunkId) -> &mut Chunk {
self.get_chunk_mut(chunk).unwrap()
}
Expand Down Expand Up @@ -147,17 +145,17 @@ impl ChunkLayout {
}
}

/// Ensures that every new node of the given DualGraph is populated with a [Node] and is
/// Ensures that every new node of the given Graph is populated with a [Node] and is
/// ready for world generation.
pub fn populate_fresh_nodes(graph: &mut DualGraph) {
pub fn populate_fresh_nodes(graph: &mut Graph) {
let fresh = graph.fresh().to_vec();
graph.clear_fresh();
for &node in &fresh {
populate_node(graph, node);
}
}

fn populate_node(graph: &mut DualGraph, node: NodeId) {
fn populate_node(graph: &mut Graph, node: NodeId) {
*graph.get_mut(node) = Some(Node {
state: graph
.parent(node)
Expand Down
14 changes: 7 additions & 7 deletions common/src/worldgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use rand_distr::Normal;

use crate::{
dodeca::{Side, Vertex},
graph::NodeId,
graph::{Graph, NodeId},
math,
node::{ChunkId, DualGraph, VoxelData},
node::{ChunkId, VoxelData},
terraingen::VoronoiInfo,
world::Material,
Plane,
Expand Down Expand Up @@ -82,7 +82,7 @@ impl NodeState {
}
}

pub fn child(&self, graph: &DualGraph, node: NodeId, side: Side) -> Self {
pub fn child(&self, graph: &Graph, node: NodeId, side: Side) -> Self {
let mut d = graph
.descenders(node)
.map(|(s, n)| (s, &graph.get(n).as_ref().unwrap().state));
Expand Down Expand Up @@ -181,7 +181,7 @@ impl ChunkParams {
/// Extract data necessary to generate a chunk
///
/// Returns `None` if an unpopulated node is needed.
pub fn new(dimension: u8, graph: &DualGraph, chunk: ChunkId) -> Option<Self> {
pub fn new(dimension: u8, graph: &Graph, chunk: ChunkId) -> Option<Self> {
let state = &graph.get(chunk.node).as_ref()?.state;
Some(Self {
dimension,
Expand Down Expand Up @@ -516,7 +516,7 @@ struct ChunkIncidentEnviroFactors {
///
/// Returns `None` if not all incident nodes are populated.
fn chunk_incident_enviro_factors(
graph: &DualGraph,
graph: &Graph,
chunk: ChunkId,
) -> Option<ChunkIncidentEnviroFactors> {
let mut i = chunk
Expand Down Expand Up @@ -613,7 +613,7 @@ fn hash(a: u64, b: u64) -> u64 {
#[cfg(test)]
mod test {
use super::*;
use crate::node::{DualGraph, Node};
use crate::node::Node;
use crate::Chunks;
use approx::*;

Expand Down Expand Up @@ -676,7 +676,7 @@ mod test {

#[test]
fn check_chunk_incident_max_elevations() {
let mut g = DualGraph::new();
let mut g = Graph::new();
for (i, path) in Vertex::A.dual_vertices().map(|(_, p)| p).enumerate() {
let new_node = path.fold(NodeId::ROOT, |node, side| g.ensure_neighbor(node, side));

Expand Down
6 changes: 3 additions & 3 deletions server/src/sim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use common::{
character_controller, dodeca,
graph::{Graph, NodeId},
math,
node::{populate_fresh_nodes, Chunk, DualGraph},
node::{populate_fresh_nodes, Chunk},
proto::{
Character, CharacterInput, CharacterState, ClientHello, Command, Component, FreshNode,
Position, Spawns, StateDelta,
Expand All @@ -29,7 +29,7 @@ pub struct Sim {
step: Step,
entity_ids: FxHashMap<EntityId, Entity>,
world: hecs::World,
graph: DualGraph,
graph: Graph,
spawns: Vec<Entity>,
despawns: Vec<EntityId>,
graph_entities: GraphEntities,
Expand Down Expand Up @@ -60,7 +60,7 @@ impl Sim {
}

pub fn save(&mut self, save: &mut save::Save) -> Result<(), save::DbError> {
fn path_from_origin(graph: &DualGraph, mut node: NodeId) -> Vec<u32> {
fn path_from_origin(graph: &Graph, mut node: NodeId) -> Vec<u32> {
let mut result = Vec::new();
while let Some(parent) = graph.parent(node) {
result.push(parent as u32);
Expand Down

0 comments on commit f8f9ec8

Please sign in to comment.