Skip to content
This repository has been archived by the owner on Jul 1, 2024. It is now read-only.

Commit

Permalink
Optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
Mubelotix committed Jan 4, 2024
1 parent 790e5ab commit d7c2b46
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 14 deletions.
15 changes: 10 additions & 5 deletions minecraft-server/src/entities/monsters/zombies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ pub struct Zombie {
// https://minecraft.wiki/w/Attribute
const ZOMBIE_BASE_FOLLOW_RANGE: f64 = 35.0;
const ZOMBIE_BASE_MOVEMENT_SPEED: f64 = 0.23;
const ZOMBIE_SEARCH_COOLDOWN: u64 = 20;

pub struct ZombieTask {
newton_task: NewtonTask,
target: Option<Eid>,
last_search_tick: u64,
}

impl ZombieTask {
Expand All @@ -29,7 +31,8 @@ impl ZombieTask {
let Some(newton_task) = NewtonTask::init(&anyentity).await else { return None; };
Some(ZombieTask {
newton_task,
target: None
target: None,
last_search_tick: 0,
})
}

Expand Down Expand Up @@ -132,20 +135,21 @@ impl ZombieTask {
h.world.try_move(&collision_shape, &translation).await
}

pub async fn tick(&mut self, h: Handler<Zombie>, entity_change_set: &EntityChangeSet) {
pub async fn tick(&mut self, h: Handler<Zombie>, tick_id: u64, entity_change_set: &EntityChangeSet) {
// Acquire target if none
let mut positions = None;
if self.target.is_none() {
if self.target.is_none() && self.last_search_tick + ZOMBIE_SEARCH_COOLDOWN < tick_id {
positions = self.acquire_target(&h).await;
self.last_search_tick = tick_id;
}

// Get target position if not already acquired
if positions.is_none() {
if self.target.is_some() && positions.is_none() {
let target_position = self.get_target_position(&h).await;
let self_position = self.get_self_position(&h).await;
positions = match (target_position, self_position) {
(Some(target_position), Some(self_position)) => Some((self_position, target_position)),
_ => return,
_ => None,
};
}

Expand All @@ -161,6 +165,7 @@ impl ZombieTask {
}).await;
}

// Apply gravity and velocity
self.newton_task.tick(h.into(), entity_change_set).await;
}
}
Expand Down
4 changes: 2 additions & 2 deletions minecraft-server/src/entities/tasks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ impl EntityTask {
}
}

pub async fn tick(&mut self, h: Handler<Entity>, entity_change_set: &EntityChangeSet) {
pub async fn tick(&mut self, h: Handler<Entity>, tick_id: u64, entity_change_set: &EntityChangeSet) {
match self {
EntityTask::Zombie(zombie_task) => zombie_task.tick(h.assume_other(), entity_change_set).await,
EntityTask::Zombie(zombie_task) => zombie_task.tick(h.assume_other(), tick_id, entity_change_set).await,
}
}
}
6 changes: 3 additions & 3 deletions minecraft-server/src/server_behavior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::prelude::*;
#[derive(Clone, Debug)]
pub enum ServerMessage {
/// Message indicating a new tick has started
Tick(usize),
Tick(u64),
}

pub struct ServerBehavior {
Expand All @@ -23,11 +23,11 @@ impl ServerBehavior {
// Send ticks to player handlers
let world2: &World = world;
tokio::spawn(async move {
let mut tick_id = 0;
let mut tick_id: u64 = 0;
let mut tick = tokio::time::interval(Duration::from_millis(50));
loop {
tick.tick().await;
world2.tick().await;
world2.tick(tick_id).await;
let _ = sender.send(ServerMessage::Tick(tick_id));
tick_id += 1;
}
Expand Down
4 changes: 2 additions & 2 deletions minecraft-server/src/world/ecs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ impl Entities {
entity
}

pub(super) async fn tick(&self, world: &'static World) {
pub(super) async fn tick(&self, tick_id: u64, world: &'static World) {
let entity_change_set = std::mem::take(&mut *self.change_set.write().await);
let mut tasks = self.tasks.write().await;
for (eid, task) in tasks.iter_mut() {
let h = Handler::<Entity>::assume(*eid, world);
task.tick(h, &entity_change_set).await;
task.tick(h, tick_id, &entity_change_set).await;
}
}
}
4 changes: 2 additions & 2 deletions minecraft-server/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ impl World {
}
}

pub async fn tick(&'static self) {
self.entities.tick(self).await;
pub async fn tick(&'static self, tick_id: u64) {
self.entities.tick(tick_id, self).await;
// TODO: tick world
}
}
Expand Down

0 comments on commit d7c2b46

Please sign in to comment.