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

Commit

Permalink
Optimize zombie behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
Mubelotix committed Nov 17, 2023
1 parent bd5b40a commit 5173b94
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions minecraft-server/src/entities/monsters/zombies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,19 @@ impl Handler<Zombie> {
}
}

pub async fn sleep_ticks(server_msg_rcvr: &mut BroadcastReceiver<ServerMessage>, t: usize) {
let mut i = 0;
while i < t {
let Ok(msg) = server_msg_rcvr.recv().await else {continue};
if matches!(&msg, &ServerMessage::Tick(_)) { i += 1; }
}
}

pub async fn zombie_ai_task<T: EntityDescendant + ZombieDescendant>(h: Handler<T>, mut server_msg_rcvr: BroadcastReceiver<ServerMessage>) where AnyEntity: TryAsEntityRef<T> {
loop {
let Ok(msg) = server_msg_rcvr.recv().await else {continue};
sleep_ticks(&mut server_msg_rcvr, 1).await;

if !matches!(&msg, &ServerMessage::Tick(_)) {
continue;
}

let self_position = h.observe(|e| e.get_entity().position.clone()).await.unwrap();
let mut self_position = h.observe(|e| e.get_entity().position.clone()).await.unwrap();
let chunk = self_position.chunk_column();
let player_positions = h.world.observe_entities(chunk, |entity| {
let network_entity = entity.to_network().unwrap();
Expand All @@ -42,7 +46,7 @@ pub async fn zombie_ai_task<T: EntityDescendant + ZombieDescendant>(h: Handler<T
})
}).await;

let Some((target_position, network_entity)) = player_positions.get(0) else { continue };
let Some((target_position, network_entity)) = player_positions.get(0) else { sleep_ticks(&mut server_msg_rcvr, 100).await; continue };
let target_object = CollisionShape {
x1: target_position.x - network_entity.width() as f64 / 2.0,
y1: target_position.y,
Expand All @@ -51,20 +55,29 @@ pub async fn zombie_ai_task<T: EntityDescendant + ZombieDescendant>(h: Handler<T
y2: target_position.y + network_entity.height() as f64,
z2: target_position.z + network_entity.width() as f64 / 2.0,
};

let mut translation = Translation {
x: target_position.x - self_position.x,
y: target_position.y - self_position.y,
z: target_position.z - self_position.z,
};
translation.set_norm(0.23000000417232513);

let authorized_translation = h.world.try_move(&target_object, &translation).await;
for _ in 0..50 {
let mut translation = Translation {
x: target_position.x - self_position.x,
y: target_position.y - self_position.y,
z: target_position.z - self_position.z,
};
translation.set_norm(0.23000000417232513);

let authorized_translation = h.world.try_move(&target_object, &translation).await;

let new_pos = h.mutate(|e| {
e.get_entity_mut().position += authorized_translation;
(e.get_entity().position.clone(), EntityChanges::position())
}).await;
self_position = match new_pos {
Some(pos) => pos,
None => break,
};

h.mutate(|e| {
e.get_entity_mut().position += authorized_translation;
((), EntityChanges::position())
}).await;
sleep_ticks(&mut server_msg_rcvr, 1).await; // TODO: do while
}

}
}

Expand Down

0 comments on commit 5173b94

Please sign in to comment.