2025-05-02 23:51:34 -04:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use tokio::sync::mpsc::Receiver;
|
|
|
|
|
2025-05-08 22:53:36 -04:00
|
|
|
use crate::{
|
2025-05-09 20:09:22 -04:00
|
|
|
common::{ObjectId, Position},
|
2025-05-08 23:03:51 -04:00
|
|
|
ipc::zone::{
|
2025-05-11 10:12:02 -04:00
|
|
|
ActorControl, ActorControlCategory, ActorControlSelf, ActorControlTarget, BattleNpcSubKind,
|
2025-05-09 20:09:22 -04:00
|
|
|
ClientTriggerCommand, CommonSpawn, NpcSpawn, ObjectKind,
|
2025-05-08 23:03:51 -04:00
|
|
|
},
|
2025-05-08 22:53:36 -04:00
|
|
|
};
|
2025-05-02 23:51:34 -04:00
|
|
|
|
|
|
|
use super::{Actor, ClientHandle, ClientId, FromServer, ToServer};
|
|
|
|
|
2025-05-09 19:27:18 -04:00
|
|
|
#[derive(Default, Debug, Clone)]
|
|
|
|
struct Instance {
|
2025-05-02 23:51:34 -04:00
|
|
|
// structure temporary, of course
|
|
|
|
actors: HashMap<ObjectId, CommonSpawn>,
|
|
|
|
}
|
|
|
|
|
2025-05-09 19:27:18 -04:00
|
|
|
#[derive(Default, Debug, Clone)]
|
|
|
|
struct ClientState {
|
|
|
|
zone_id: u16,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default, Debug)]
|
|
|
|
struct WorldServer {
|
|
|
|
clients: HashMap<ClientId, (ClientHandle, ClientState)>,
|
|
|
|
/// Indexed by zone id
|
|
|
|
instances: HashMap<u16, Instance>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WorldServer {
|
|
|
|
/// Finds the instance associated with a zone, or None if it doesn't exist yet.
|
|
|
|
fn find_instance(&self, zone_id: u16) -> Option<&Instance> {
|
|
|
|
self.instances.get(&zone_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Finds the instance associated with a zone, or creates it if it doesn't exist yet
|
|
|
|
fn find_instance_mut(&mut self, zone_id: u16) -> &mut Instance {
|
|
|
|
if self.instances.contains_key(&zone_id) {
|
|
|
|
self.instances.get_mut(&zone_id).unwrap()
|
|
|
|
} else {
|
|
|
|
self.instances.insert(zone_id, Instance::default());
|
|
|
|
self.instances.get_mut(&zone_id).unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Finds the instance associated with an actor, or returns None if they are not found.
|
|
|
|
fn find_actor_instance_mut(&mut self, actor_id: u32) -> Option<&mut Instance> {
|
|
|
|
for (_, instance) in &mut self.instances {
|
|
|
|
if instance.actors.contains_key(&ObjectId(actor_id)) {
|
|
|
|
return Some(instance);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-05-02 23:51:34 -04:00
|
|
|
pub async fn server_main_loop(mut recv: Receiver<ToServer>) -> Result<(), std::io::Error> {
|
|
|
|
let mut data = WorldServer::default();
|
|
|
|
let mut to_remove = Vec::new();
|
|
|
|
|
|
|
|
while let Some(msg) = recv.recv().await {
|
|
|
|
match msg {
|
|
|
|
ToServer::NewClient(handle) => {
|
2025-05-09 19:27:18 -04:00
|
|
|
data.clients
|
|
|
|
.insert(handle.id, (handle, ClientState::default()));
|
2025-05-02 23:51:34 -04:00
|
|
|
}
|
2025-05-09 19:27:18 -04:00
|
|
|
ToServer::ZoneLoaded(from_id, zone_id) => {
|
|
|
|
// create a new instance if nessecary
|
|
|
|
if !data.instances.contains_key(&zone_id) {
|
|
|
|
data.instances.insert(zone_id, Instance::default());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send existing player data, if any
|
|
|
|
if let Some(instance) = data.find_instance(zone_id).cloned() {
|
|
|
|
for (id, (handle, state)) in &mut data.clients {
|
|
|
|
let id = *id;
|
|
|
|
|
|
|
|
if id == from_id {
|
|
|
|
state.zone_id = zone_id;
|
|
|
|
|
|
|
|
// send existing player data
|
|
|
|
for (id, common) in &instance.actors {
|
|
|
|
let msg = FromServer::ActorSpawn(
|
|
|
|
Actor {
|
|
|
|
id: *id,
|
|
|
|
hp: 100,
|
|
|
|
spawn_index: 0,
|
|
|
|
},
|
|
|
|
common.clone(),
|
|
|
|
);
|
|
|
|
|
|
|
|
handle.send(msg).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-05-09 19:42:23 -04:00
|
|
|
let (client, _) = data.clients.get(&from_id).unwrap().clone();
|
|
|
|
|
|
|
|
// add the connection's actor to the table
|
|
|
|
{
|
|
|
|
let instance = data.find_instance_mut(zone_id);
|
|
|
|
instance
|
|
|
|
.actors
|
|
|
|
.insert(ObjectId(client.actor_id), client.common.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then tell any clients in the zone that we spawned
|
2025-05-09 19:27:18 -04:00
|
|
|
for (id, (handle, state)) in &mut data.clients {
|
2025-05-02 23:51:34 -04:00
|
|
|
let id = *id;
|
|
|
|
|
2025-05-09 19:27:18 -04:00
|
|
|
// don't bother telling the client who told us
|
2025-05-02 23:51:34 -04:00
|
|
|
if id == from_id {
|
2025-05-09 19:27:18 -04:00
|
|
|
continue;
|
|
|
|
}
|
2025-05-02 23:51:34 -04:00
|
|
|
|
2025-05-09 19:27:18 -04:00
|
|
|
// skip any clients not in our zone
|
|
|
|
if state.zone_id != zone_id {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2025-05-09 19:42:23 -04:00
|
|
|
let msg = FromServer::ActorSpawn(
|
|
|
|
Actor {
|
|
|
|
id: ObjectId(client.actor_id),
|
|
|
|
hp: 0,
|
|
|
|
spawn_index: 0,
|
|
|
|
},
|
|
|
|
client.common.clone(),
|
|
|
|
);
|
2025-05-02 23:51:34 -04:00
|
|
|
|
2025-05-09 19:27:18 -04:00
|
|
|
if handle.send(msg).is_err() {
|
|
|
|
to_remove.push(id);
|
2025-05-02 23:51:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2025-05-09 19:42:23 -04:00
|
|
|
ToServer::LeftZone(from_id, actor_id, zone_id) => {
|
|
|
|
// when the actor leaves the zone, remove them from the instance
|
|
|
|
let current_instance = data.find_actor_instance_mut(actor_id).unwrap();
|
|
|
|
current_instance.actors.remove(&ObjectId(actor_id));
|
|
|
|
|
|
|
|
// Then tell any clients in the zone that we left
|
|
|
|
for (id, (handle, state)) in &mut data.clients {
|
2025-05-02 23:51:34 -04:00
|
|
|
let id = *id;
|
|
|
|
|
2025-05-09 19:42:23 -04:00
|
|
|
// don't bother telling the client who told us
|
2025-05-02 23:51:34 -04:00
|
|
|
if id == from_id {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2025-05-09 19:42:23 -04:00
|
|
|
// skip any clients not in our zone
|
|
|
|
if state.zone_id != zone_id {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let msg = FromServer::ActorDespawn(actor_id);
|
2025-05-02 23:51:34 -04:00
|
|
|
|
|
|
|
if handle.send(msg).is_err() {
|
|
|
|
to_remove.push(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2025-05-09 19:42:23 -04:00
|
|
|
ToServer::Message(from_id, msg) => {
|
|
|
|
for (id, (handle, _)) in &mut data.clients {
|
2025-05-02 23:51:34 -04:00
|
|
|
let id = *id;
|
|
|
|
|
|
|
|
if id == from_id {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2025-05-09 19:42:23 -04:00
|
|
|
let msg = FromServer::Message(msg.clone());
|
2025-05-02 23:51:34 -04:00
|
|
|
|
|
|
|
if handle.send(msg).is_err() {
|
|
|
|
to_remove.push(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ToServer::ActorMoved(from_id, actor_id, position, rotation) => {
|
2025-05-09 19:27:18 -04:00
|
|
|
if let Some(instance) = data.find_actor_instance_mut(actor_id) {
|
|
|
|
if let Some((_, common)) = instance
|
|
|
|
.actors
|
|
|
|
.iter_mut()
|
|
|
|
.find(|actor| *actor.0 == ObjectId(actor_id))
|
|
|
|
{
|
|
|
|
common.pos = position;
|
|
|
|
common.rotation = rotation;
|
|
|
|
}
|
2025-05-02 23:51:34 -04:00
|
|
|
|
2025-05-09 19:27:18 -04:00
|
|
|
for (id, (handle, _)) in &mut data.clients {
|
|
|
|
let id = *id;
|
2025-05-02 23:51:34 -04:00
|
|
|
|
2025-05-09 19:27:18 -04:00
|
|
|
if id == from_id {
|
|
|
|
continue;
|
|
|
|
}
|
2025-05-02 23:51:34 -04:00
|
|
|
|
2025-05-09 19:27:18 -04:00
|
|
|
let msg = FromServer::ActorMove(actor_id, position, rotation);
|
2025-05-02 23:51:34 -04:00
|
|
|
|
2025-05-09 19:27:18 -04:00
|
|
|
if handle.send(msg).is_err() {
|
|
|
|
to_remove.push(id);
|
|
|
|
}
|
2025-05-02 23:51:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2025-05-08 22:53:36 -04:00
|
|
|
ToServer::ClientTrigger(from_id, from_actor_id, trigger) => {
|
2025-05-09 19:27:18 -04:00
|
|
|
for (id, (handle, _)) in &mut data.clients {
|
2025-05-08 22:53:36 -04:00
|
|
|
let id = *id;
|
|
|
|
|
2025-05-11 10:12:02 -04:00
|
|
|
tracing::info!("{:#?}", trigger);
|
|
|
|
|
|
|
|
// handle player-to-server actions
|
2025-05-08 22:53:36 -04:00
|
|
|
if id == from_id {
|
2025-05-11 10:12:02 -04:00
|
|
|
match &trigger.trigger {
|
|
|
|
ClientTriggerCommand::TeleportQuery { aetheryte_id } => {
|
|
|
|
let msg = FromServer::ActorControlSelf(ActorControlSelf {
|
|
|
|
category: ActorControlCategory::TeleportStart {
|
|
|
|
insufficient_gil: 0,
|
|
|
|
aetheryte_id: *aetheryte_id,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if handle.send(msg).is_err() {
|
|
|
|
to_remove.push(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2025-05-08 22:53:36 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
match &trigger.trigger {
|
|
|
|
ClientTriggerCommand::SetTarget { actor_id } => {
|
|
|
|
let msg = FromServer::ActorControlTarget(
|
|
|
|
from_actor_id,
|
|
|
|
ActorControlTarget {
|
|
|
|
category: ActorControlCategory::SetTarget {
|
|
|
|
actor_id: *actor_id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
if handle.send(msg).is_err() {
|
|
|
|
to_remove.push(id);
|
|
|
|
}
|
|
|
|
}
|
2025-05-08 23:03:51 -04:00
|
|
|
ClientTriggerCommand::ChangePose { unk1, pose } => {
|
|
|
|
let msg = FromServer::ActorControl(
|
|
|
|
from_actor_id,
|
|
|
|
ActorControl {
|
|
|
|
category: ActorControlCategory::Pose {
|
|
|
|
unk1: *unk1,
|
|
|
|
pose: *pose,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
if handle.send(msg).is_err() {
|
|
|
|
to_remove.push(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ClientTriggerCommand::ReapplyPose { unk1, pose } => {
|
|
|
|
let msg = FromServer::ActorControl(
|
|
|
|
from_actor_id,
|
|
|
|
ActorControl {
|
|
|
|
category: ActorControlCategory::Pose {
|
|
|
|
unk1: *unk1,
|
|
|
|
pose: *pose,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
if handle.send(msg).is_err() {
|
|
|
|
to_remove.push(id);
|
|
|
|
}
|
|
|
|
}
|
2025-05-08 22:53:36 -04:00
|
|
|
_ => tracing::warn!("Server doesn't know what to do with {:#?}", trigger),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2025-05-09 20:09:22 -04:00
|
|
|
ToServer::DebugNewNpc(_from_id) => {
|
|
|
|
for (id, (handle, _)) in &mut data.clients {
|
|
|
|
let id = *id;
|
|
|
|
|
|
|
|
let msg = FromServer::SpawnNPC(NpcSpawn {
|
|
|
|
aggression_mode: 1,
|
|
|
|
common: CommonSpawn {
|
|
|
|
hp_curr: 91,
|
|
|
|
hp_max: 91,
|
|
|
|
mp_curr: 100,
|
|
|
|
mp_max: 100,
|
|
|
|
spawn_index: 0, // not needed at this level
|
|
|
|
bnpc_base: 13498, // TODO: changing this prevents it from spawning...
|
|
|
|
bnpc_name: 405,
|
|
|
|
object_kind: ObjectKind::BattleNpc(BattleNpcSubKind::Enemy),
|
|
|
|
level: 1,
|
|
|
|
battalion: 4,
|
|
|
|
model_chara: 297,
|
|
|
|
pos: Position::default(),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
|
|
|
|
if handle.send(msg).is_err() {
|
|
|
|
to_remove.push(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2025-05-02 23:51:34 -04:00
|
|
|
ToServer::Disconnected(from_id) => {
|
|
|
|
to_remove.push(from_id);
|
|
|
|
}
|
|
|
|
ToServer::FatalError(err) => return Err(err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Remove any clients that errored out
|
|
|
|
for id in to_remove {
|
|
|
|
data.clients.remove(&id);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|