diff --git a/src/bin/kawari-world.rs b/src/bin/kawari-world.rs index 3a47492..98c85a4 100644 --- a/src/bin/kawari-world.rs +++ b/src/bin/kawari-world.rs @@ -24,8 +24,8 @@ use kawari::world::{ use kawari::world::{ ChatHandler, Zone, ZoneConnection, ipc::{ - ActorControlCategory, ActorControlSelf, PlayerEntry, PlayerSetup, PlayerSpawn, PlayerStats, - SocialList, + ActorControlCategory, ActorControlSelf, CommonSpawn, PlayerEntry, PlayerSetup, PlayerSpawn, + PlayerStats, SocialList, }, }; @@ -45,6 +45,8 @@ struct ExtraLuaState { #[derive(Default, Debug)] struct Data { clients: HashMap, + // structure temporary, of course + actors: HashMap, } async fn main_loop(mut recv: Receiver) -> Result<(), std::io::Error> { @@ -56,6 +58,23 @@ async fn main_loop(mut recv: Receiver) -> Result<(), std::io::Error> { ToServer::NewClient(handle) => { data.clients.insert(handle.id, handle); } + ToServer::ZoneLoaded(from_id) => { + for (id, handle) in &mut data.clients { + let id = *id; + + if id == from_id { + // send existing player data + for (id, common) in &data.actors { + let msg = + FromServer::ActorSpawn(Actor { id: *id, hp: 100 }, common.clone()); + + let _ = handle.send(msg).unwrap(); + } + + break; + } + } + } ToServer::Message(from_id, msg) => { for (id, handle) in &mut data.clients { let id = *id; @@ -72,6 +91,8 @@ async fn main_loop(mut recv: Receiver) -> Result<(), std::io::Error> { } } ToServer::ActorSpawned(from_id, actor, common) => { + data.actors.insert(actor.id, common.clone()); + for (id, handle) in &mut data.clients { let id = *id; @@ -86,7 +107,7 @@ async fn main_loop(mut recv: Receiver) -> Result<(), std::io::Error> { } } } - ToServer::ActorMoved(from_id, actor_id, position) => { + ToServer::ActorMoved(from_id, actor_id, position, rotation) => { for (id, handle) in &mut data.clients { let id = *id; @@ -94,7 +115,7 @@ async fn main_loop(mut recv: Receiver) -> Result<(), std::io::Error> { continue; } - let msg = FromServer::ActorMove(actor_id, position); + let msg = FromServer::ActorMove(actor_id, position, rotation); if handle.send(msg).is_err() { to_remove.push(id); @@ -374,6 +395,9 @@ async fn client_loop( .unwrap(); } ClientZoneIpcData::FinishLoading { .. } => { + // tell the server we loaded into the zone, so it can start sending us acors + connection.handle.send(ToServer::ZoneLoaded(connection.id)).await; + // send player spawn { let ipc = ServerZoneIpcSegment { @@ -521,7 +545,7 @@ async fn client_loop( connection.player_data.rotation = *rotation; connection.player_data.position = *position; - connection.handle.send(ToServer::ActorMoved(connection.id, connection.player_data.actor_id, *position)).await; + connection.handle.send(ToServer::ActorMoved(connection.id, connection.player_data.actor_id, *position, *rotation)).await; } ClientZoneIpcData::LogOut { .. } => { tracing::info!("Recieved log out from client!"); @@ -980,11 +1004,11 @@ async fn client_loop( } msg = internal_recv.recv() => match msg { Some(msg) => match msg { - FromServer::Message(msg)=>connection.send_message(&msg).await, + FromServer::Message(msg)=> connection.send_message(&msg).await, FromServer::ActorSpawn(actor, common) => { connection.spawn_actor(actor, common).await }, - FromServer::ActorMove(actor_id, position) => connection.set_actor_position(actor_id, position).await, + FromServer::ActorMove(actor_id, position, rotation) => connection.set_actor_position(actor_id, position, rotation).await, }, None => break, } diff --git a/src/common/mod.rs b/src/common/mod.rs index fc50d0d..0c106a0 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -17,7 +17,7 @@ pub use gamedata::GameData; #[binrw] #[brw(little)] -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct ObjectId(pub u32); impl Default for ObjectId { diff --git a/src/world/connection.rs b/src/world/connection.rs index 4ae471a..d8e95ad 100644 --- a/src/world/connection.rs +++ b/src/world/connection.rs @@ -58,7 +58,7 @@ pub enum FromServer { /// An actor has been spawned. ActorSpawn(Actor, CommonSpawn), /// An actor moved to a new position. - ActorMove(u32, Position), + ActorMove(u32, Position, f32), } #[derive(Debug, Clone)] @@ -96,7 +96,8 @@ pub enum ToServer { NewClient(ClientHandle), Message(ClientId, String), ActorSpawned(ClientId, Actor, CommonSpawn), - ActorMoved(ClientId, u32, Position), + ActorMoved(ClientId, u32, Position, f32), + ZoneLoaded(ClientId), FatalError(std::io::Error), } @@ -282,7 +283,7 @@ impl ZoneConnection { } } - pub async fn set_actor_position(&mut self, actor_id: u32, position: Position) { + pub async fn set_actor_position(&mut self, actor_id: u32, position: Position, rotation: f32) { let ipc = ServerZoneIpcSegment { op_code: ServerZoneIpcType::ActorMove, timestamp: timestamp_secs(),