Rename *Data to *State

This commit is contained in:
Ryan 2022-07-27 19:21:11 -07:00
parent 2e22946ffc
commit 35c697ff93
12 changed files with 107 additions and 107 deletions

View file

@ -32,7 +32,7 @@ struct Game {
}
#[derive(Default)]
struct ClientData {
struct ClientState {
/// The client's player entity.
player: EntityId,
/// The extra knockback on the first hit while sprinting.
@ -40,7 +40,7 @@ struct ClientData {
}
#[derive(Default)]
struct EntityData {
struct EntityState {
client: ClientId,
attacked: bool,
attacker_pos: Vec3<f64>,
@ -54,11 +54,11 @@ const SPAWN_POS: BlockPos = BlockPos::new(0, 20, 0);
#[async_trait]
impl Config for Game {
type ChunkData = ();
type ClientData = ClientData;
type EntityData = EntityData;
type ServerData = ();
type WorldData = ();
type ChunkState = ();
type ClientState = ClientState;
type EntityState = EntityState;
type ServerState = ();
type WorldState = ();
fn max_connections(&self) -> usize {
// We want status pings to be successful even if the server is full.
@ -154,14 +154,14 @@ impl Config for Game {
let (player_id, player) = server
.entities
.create_with_uuid(EntityKind::Player, client.uuid(), EntityData::default())
.create_with_uuid(EntityKind::Player, client.uuid(), EntityState::default())
.unwrap();
client.data.player = player_id;
client.data.extra_knockback = true;
client.state.player = player_id;
client.state.extra_knockback = true;
player.data.client = client_id;
player.data.last_attack_time = 0;
player.state.client = client_id;
player.state.last_attack_time = 0;
client.send_message("Welcome to the arena.".italic());
if self.player_count.load(Ordering::SeqCst) <= 1 {
@ -171,7 +171,7 @@ impl Config for Game {
if client.is_disconnected() {
self.player_count.fetch_sub(1, Ordering::SeqCst);
server.entities.delete(client.data.player);
server.entities.delete(client.state.player);
world.meta.player_list_mut().remove(client.uuid());
return false;
}
@ -179,7 +179,7 @@ impl Config for Game {
while let Some(event) = client.pop_event() {
match event {
Event::StartSprinting => {
client.data.extra_knockback = true;
client.state.extra_knockback = true;
}
Event::InteractWithEntity {
id,
@ -187,21 +187,21 @@ impl Config for Game {
..
} => {
if let Some(target) = server.entities.get_mut(id) {
if !target.data.attacked
&& current_tick - target.data.last_attack_time >= 10
&& id != client.data.player
if !target.state.attacked
&& current_tick - target.state.last_attack_time >= 10
&& id != client.state.player
{
target.data.attacked = true;
target.data.attacker_pos = client.position();
target.data.extra_knockback = client.data.extra_knockback;
target.data.last_attack_time = current_tick;
target.state.attacked = true;
target.state.attacker_pos = client.position();
target.state.extra_knockback = client.state.extra_knockback;
target.state.last_attack_time = current_tick;
client.data.extra_knockback = false;
client.state.extra_knockback = false;
}
}
}
Event::ArmSwing(hand) => {
let player = server.entities.get_mut(client.data.player).unwrap();
let player = server.entities.get_mut(client.state.player).unwrap();
match hand {
Hand::Main => player.trigger_event(EntityEvent::SwingMainHand),
Hand::Off => player.trigger_event(EntityEvent::SwingOffHand),
@ -223,7 +223,7 @@ impl Config for Game {
);
}
let player = server.entities.get_mut(client.data.player).unwrap();
let player = server.entities.get_mut(client.state.player).unwrap();
player.set_world(client.world());
player.set_position(client.position());
@ -246,14 +246,14 @@ impl Config for Game {
});
for (_, e) in server.entities.iter_mut() {
if e.data.attacked {
e.data.attacked = false;
let victim = server.clients.get_mut(e.data.client).unwrap();
if e.state.attacked {
e.state.attacked = false;
let victim = server.clients.get_mut(e.state.client).unwrap();
let mut vel = (victim.position() - e.data.attacker_pos).normalized();
let mut vel = (victim.position() - e.state.attacker_pos).normalized();
let knockback_xz = if e.data.extra_knockback { 18.0 } else { 8.0 };
let knockback_y = if e.data.extra_knockback { 8.432 } else { 6.432 };
let knockback_xz = if e.state.extra_knockback { 18.0 } else { 8.0 };
let knockback_y = if e.state.extra_knockback { 8.432 } else { 6.432 };
vel.x *= knockback_xz;
vel.y = knockback_y;

View file

@ -26,7 +26,7 @@ pub fn main() -> ShutdownResult {
Game {
player_count: AtomicUsize::new(0),
},
ServerData {
ServerState {
board: vec![false; SIZE_X * SIZE_Z].into_boxed_slice(),
board_buf: vec![false; SIZE_X * SIZE_Z].into_boxed_slice(),
},
@ -37,13 +37,13 @@ struct Game {
player_count: AtomicUsize,
}
struct ServerData {
struct ServerState {
board: Box<[bool]>,
board_buf: Box<[bool]>,
}
#[derive(Default)]
struct ClientData {
struct ClientState {
/// The client's player entity.
player: EntityId,
}
@ -56,11 +56,11 @@ const BOARD_Y: i32 = 50;
#[async_trait]
impl Config for Game {
type ChunkData = ();
type ClientData = ClientData;
type EntityData = ();
type ServerData = ServerData;
type WorldData = ();
type ChunkState = ();
type ClientState = ClientState;
type EntityState = ();
type ServerState = ServerState;
type WorldState = ();
fn max_connections(&self) -> usize {
// We want status pings to be successful even if the server is full.
@ -145,7 +145,7 @@ impl Config for Game {
None,
);
client.data.player = server
client.state.player = server
.entities
.create_with_uuid(EntityKind::Player, client.uuid(), ())
.unwrap()
@ -157,12 +157,12 @@ impl Config for Game {
if client.is_disconnected() {
self.player_count.fetch_sub(1, Ordering::SeqCst);
server.entities.delete(client.data.player);
server.entities.delete(client.state.player);
world.meta.player_list_mut().remove(client.uuid());
return false;
}
let player = server.entities.get_mut(client.data.player).unwrap();
let player = server.entities.get_mut(client.state.player).unwrap();
if client.position().y <= 0.0 {
client.teleport(spawn_pos, client.yaw(), client.pitch());
@ -175,7 +175,7 @@ impl Config for Game {
&& (0..SIZE_Z as i32).contains(&position.z)
&& position.y == BOARD_Y
{
server.data.board[position.x as usize + position.z as usize * SIZE_X] =
server.state.board[position.x as usize + position.z as usize * SIZE_X] =
true;
}
}
@ -212,7 +212,7 @@ impl Config for Game {
}
server
.data
.state
.board_buf
.par_iter_mut()
.enumerate()
@ -226,21 +226,21 @@ impl Config for Game {
if !(x == cx && z == cz) {
let i = x.rem_euclid(SIZE_X as i32) as usize
+ z.rem_euclid(SIZE_Z as i32) as usize * SIZE_X;
if server.data.board[i] {
if server.state.board[i] {
live_count += 1;
}
}
}
}
if server.data.board[cx as usize + cz as usize * SIZE_X] {
if server.state.board[cx as usize + cz as usize * SIZE_X] {
*cell = (2..=3).contains(&live_count);
} else {
*cell = live_count == 3;
}
});
mem::swap(&mut server.data.board, &mut server.data.board_buf);
mem::swap(&mut server.state.board, &mut server.state.board_buf);
let min_y = server.shared.dimensions().next().unwrap().1.min_y;
@ -256,7 +256,7 @@ impl Config for Game {
let cell_z = chunk_z * 16 + z;
if cell_x < SIZE_X && cell_z < SIZE_Z {
let b = if server.data.board[cell_x + cell_z * SIZE_X] {
let b = if server.state.board[cell_x + cell_z * SIZE_X] {
BlockState::GRASS_BLOCK
} else {
BlockState::DIRT

View file

@ -24,7 +24,7 @@ pub fn main() -> ShutdownResult {
Game {
player_count: AtomicUsize::new(0),
},
ServerData { cows: Vec::new() },
ServerState { cows: Vec::new() },
)
}
@ -32,7 +32,7 @@ struct Game {
player_count: AtomicUsize,
}
struct ServerData {
struct ServerState {
cows: Vec<EntityId>,
}
@ -42,11 +42,11 @@ const SPAWN_POS: BlockPos = BlockPos::new(0, 100, -25);
#[async_trait]
impl Config for Game {
type ChunkData = ();
type ClientData = ();
type EntityData = ();
type ServerData = ServerData;
type WorldData = ();
type ChunkState = ();
type ClientState = ();
type EntityState = ();
type ServerState = ServerState;
type WorldState = ();
fn max_connections(&self) -> usize {
// We want status pings to be successful even if the server is full.
@ -84,7 +84,7 @@ impl Config for Game {
world.chunks.set_block_state(SPAWN_POS, BlockState::BEDROCK);
server.data.cows.extend((0..200).map(|_| {
server.state.cows.extend((0..200).map(|_| {
let (id, e) = server.entities.create(EntityKind::Cow, ());
e.set_world(world_id);
id
@ -157,11 +157,11 @@ impl Config for Game {
let eye_pos = Vec3::new(player_pos.x, player_pos.y + 1.6, player_pos.z);
for (cow_id, p) in server
.data
.state
.cows
.iter()
.cloned()
.zip(fibonacci_spiral(server.data.cows.len()))
.zip(fibonacci_spiral(server.state.cows.len()))
{
let cow = server.entities.get_mut(cow_id).expect("missing cow");
let rotated = p * rot;

View file

@ -40,12 +40,12 @@ const PLAYER_EYE_HEIGHT: f64 = 1.6;
#[async_trait]
impl Config for Game {
type ChunkData = ();
type ClientData = ();
type ChunkState = ();
type ClientState = ();
/// `true` for entities that have been intersected with.
type EntityData = bool;
type ServerData = ();
type WorldData = ();
type EntityState = bool;
type ServerState = ();
type WorldState = ();
fn max_connections(&self) -> usize {
// We want status pings to be successful even if the server is full.
@ -158,7 +158,7 @@ impl Config for Game {
if let Some(hit) = world.spatial_index.raycast(origin, direction, only_sheep) {
if let Some(e) = server.entities.get_mut(hit.entity) {
e.data = true;
e.state = true;
}
}
@ -166,7 +166,7 @@ impl Config for Game {
});
for (_, e) in server.entities.iter_mut() {
let intersected = e.data;
let intersected = e.state;
if let EntityEnum::Sheep(sheep) = &mut e.view_mut() {
if intersected {
sheep.set_color(5);
@ -174,7 +174,7 @@ impl Config for Game {
sheep.set_color(0);
}
}
e.data = false;
e.state = false;
}
}
}

View file

@ -50,11 +50,11 @@ const MAX_PLAYERS: usize = 10;
#[async_trait]
impl Config for Game {
type ChunkData = ();
type ClientData = ();
type EntityData = ();
type ServerData = ();
type WorldData = ();
type ChunkState = ();
type ClientState = ();
type EntityState = ();
type ServerState = ();
type WorldState = ();
fn max_connections(&self) -> usize {
// We want status pings to be successful even if the server is full.

View file

@ -50,7 +50,7 @@ impl<C: Config> Chunks<C> {
/// adjacent to it must also be loaded. It is also important that clients
/// are not spawned within unloaded chunks via
/// [`spawn`](crate::client::Client::spawn).
pub fn create(&mut self, pos: impl Into<ChunkPos>, data: C::ChunkData) -> &mut Chunk<C> {
pub fn create(&mut self, pos: impl Into<ChunkPos>, data: C::ChunkState) -> &mut Chunk<C> {
let section_count = (self.server.dimension(self.dimension).height / 16) as u32;
let chunk = Chunk::new(section_count, self.server.current_tick(), data);
@ -185,8 +185,8 @@ impl<C: Config> Chunks<C> {
/// In addition to blocks, chunks also contain [biomes](crate::biome::Biome).
/// Every 4x4x4 segment of blocks in a chunk corresponds to a biome.
pub struct Chunk<C: Config> {
/// Custom data.
pub data: C::ChunkData,
/// Custom state.
pub state: C::ChunkState,
sections: Box<[ChunkSection]>,
// TODO block_entities: HashMap<u32, BlockEntity>,
/// The MOTION_BLOCKING heightmap
@ -195,7 +195,7 @@ pub struct Chunk<C: Config> {
}
impl<C: Config> Chunk<C> {
pub(crate) fn new(section_count: u32, current_tick: Ticks, data: C::ChunkData) -> Self {
pub(crate) fn new(section_count: u32, current_tick: Ticks, data: C::ChunkState) -> Self {
let sect = ChunkSection {
blocks: [BlockState::AIR.to_raw(); 4096],
modified_count: 1, // Must be >0 so the chunk is initialized.
@ -204,7 +204,7 @@ impl<C: Config> Chunk<C> {
};
let mut chunk = Self {
data,
state: data,
sections: vec![sect; section_count as usize].into(),
heightmap: Vec::new(),
created_tick: current_tick,

View file

@ -166,8 +166,8 @@ impl ClientId {
/// In Valence however, clients and players have been decoupled. This separation
/// was done primarily to enable multithreaded client updates.
pub struct Client<C: Config> {
/// Custom data.
pub data: C::ClientData,
/// Custom state.
pub state: C::ClientState,
/// Setting this to `None` disconnects the client.
send: SendOpt,
recv: Receiver<C2sPlayPacket>,
@ -245,12 +245,12 @@ impl<C: Config> Client<C> {
packet_channels: C2sPacketChannels,
server: &SharedServer<C>,
ncd: NewClientData,
data: C::ClientData,
data: C::ClientState,
) -> Self {
let (send, recv) = packet_channels;
Self {
data,
state: data,
send: Some(send),
recv,
created_tick: server.current_tick(),

View file

@ -21,16 +21,16 @@ use crate::{Ticks, STANDARD_TPS};
#[async_trait]
#[allow(unused_variables)]
pub trait Config: 'static + Sized + Send + Sync + UnwindSafe + RefUnwindSafe {
/// Custom data to store with the [`Server`].
type ServerData: Send + Sync;
/// Custom data to store with every [`Client`](crate::client::Client).
type ClientData: Default + Send + Sync;
/// Custom data to store with every [`Entity`](crate::entity::Entity).
type EntityData: Send + Sync;
/// Custom data to store with every [`World`](crate::world::World).
type WorldData: Send + Sync;
/// Custom data to store with every [`Chunk`](crate::chunk::Chunk).
type ChunkData: Send + Sync;
/// Custom state to store with the [`Server`].
type ServerState: Send + Sync;
/// Custom state to store with every [`Client`](crate::client::Client).
type ClientState: Default + Send + Sync;
/// Custom state to store with every [`Entity`](crate::entity::Entity).
type EntityState: Send + Sync;
/// Custom state to store with every [`World`](crate::world::World).
type WorldState: Send + Sync;
/// Custom state to store with every [`Chunk`](crate::chunk::Chunk).
type ChunkState: Send + Sync;
/// Called once at startup to get the maximum number of simultaneous
/// connections allowed to the server. This includes all

View file

@ -53,7 +53,7 @@ impl<C: Config> Entities<C> {
/// Spawns a new entity with a random UUID. A reference to the entity along
/// with its ID is returned.
pub fn create(&mut self, kind: EntityKind, data: C::EntityData) -> (EntityId, &mut Entity<C>) {
pub fn create(&mut self, kind: EntityKind, data: C::EntityState) -> (EntityId, &mut Entity<C>) {
self.create_with_uuid(kind, Uuid::from_bytes(rand::random()), data)
.expect("UUID collision")
}
@ -67,13 +67,13 @@ impl<C: Config> Entities<C> {
&mut self,
kind: EntityKind,
uuid: Uuid,
data: C::EntityData,
data: C::EntityState,
) -> Option<(EntityId, &mut Entity<C>)> {
match self.uuid_to_entity.entry(uuid) {
Entry::Occupied(_) => None,
Entry::Vacant(ve) => {
let (k, e) = self.sm.insert(Entity {
data,
state: data,
variants: EntityEnum::new(kind),
events: Vec::new(),
flags: EntityFlags(0),
@ -242,7 +242,7 @@ impl EntityId {
/// [`Self::data`].
pub struct Entity<C: Config> {
/// Custom data.
pub data: C::EntityData,
pub state: C::EntityState,
variants: EntityEnum,
flags: EntityFlags,
events: Vec<Event>,

View file

@ -58,11 +58,11 @@
//! struct Game;
//!
//! impl Config for Game {
//! type ChunkData = ();
//! type ClientData = ();
//! type EntityData = ();
//! type ServerData = ();
//! type WorldData = ();
//! type ChunkState = ();
//! type ClientState = ();
//! type EntityState = ();
//! type ServerState = ();
//! type WorldState = ();
//!
//! fn max_connections(&self) -> usize {
//! 256

View file

@ -52,8 +52,8 @@ use crate::{Ticks, PROTOCOL_VERSION, VERSION_NAME};
/// Contains the entire state of a running Minecraft server, accessible from
/// within the [update](crate::config::Config::update) loop.
pub struct Server<C: Config> {
/// Custom data.
pub data: C::ServerData,
/// Custom state.
pub state: C::ServerState,
/// A handle to this server's [`SharedServer`].
pub shared: SharedServer<C>,
/// All of the clients in the server.
@ -248,13 +248,13 @@ impl<C: Config> SharedServer<C> {
///
/// The function returns once the server has shut down, a runtime error
/// occurs, or the configuration is found to be invalid.
pub fn start_server<C: Config>(config: C, data: C::ServerData) -> ShutdownResult {
pub fn start_server<C: Config>(config: C, data: C::ServerState) -> ShutdownResult {
let shared = setup_server(config).map_err(Box::<dyn Error + Send + Sync + 'static>::from)?;
let _guard = shared.tokio_handle().enter();
let mut server = Server {
data,
state: data,
shared: shared.clone(),
clients: Clients::new(),
entities: Entities::new(),
@ -463,7 +463,7 @@ fn join_player<C: Config>(server: &mut Server<C>, msg: NewClientMessage) {
c2s_packet_channels,
&server.shared,
msg.ncd,
C::ClientData::default(),
C::ClientState::default(),
);
server.clients.insert(client);

View file

@ -45,9 +45,9 @@ impl<C: Config> Worlds<C> {
/// Creates a new world on the server with the provided dimension. A
/// reference to the world along with its ID is returned.
pub fn create(&mut self, dim: DimensionId, data: C::WorldData) -> (WorldId, &mut World<C>) {
pub fn create(&mut self, dim: DimensionId, data: C::WorldState) -> (WorldId, &mut World<C>) {
let (id, world) = self.sm.insert(World {
data,
state: data,
spatial_index: SpatialIndex::new(),
chunks: Chunks::new(self.server.clone(), dim),
meta: WorldMeta {
@ -121,8 +121,8 @@ impl<C: Config> Worlds<C> {
/// A space for chunks, entities, and clients to occupy.
pub struct World<C: Config> {
/// Custom data.
pub data: C::WorldData,
/// Custom state.
pub state: C::WorldState,
/// Contains all of the entities in this world.
pub spatial_index: SpatialIndex,
/// All of the chunks in this world.