412 lines
20 KiB
Rust
412 lines
20 KiB
Rust
use libpso::packet::ship::*;
|
|
use libpso::packet::messages::*;
|
|
use crate::entity::gateway::EntityGateway;
|
|
use crate::entity::item::Meseta;
|
|
use crate::common::serverstate::ClientId;
|
|
use crate::common::leveltable::CharacterLevelTable;
|
|
use crate::ship::ship::{SendShipPacket, ShipError, Rooms, Clients, ItemDropLocation};
|
|
use crate::ship::location::{ClientLocation, ClientLocationError};
|
|
use crate::ship::items::ClientItemId;
|
|
use crate::ship::packet::builder;
|
|
use crate::ship::items::state::ItemState;
|
|
use crate::ship::items::tasks::{drop_item, drop_partial_item, drop_meseta, equip_item, unequip_item, sort_inventory, use_item, feed_mag, sell_item, take_meseta};
|
|
|
|
pub async fn request_exp<EG: EntityGateway>(id: ClientId,
|
|
request_exp: &RequestExp,
|
|
entity_gateway: &mut EG,
|
|
client_location: &ClientLocation,
|
|
clients: &mut Clients,
|
|
rooms: &mut Rooms,
|
|
level_table: &CharacterLevelTable)
|
|
-> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, anyhow::Error> {
|
|
let client = clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?;
|
|
let area_client = client_location.get_local_client(id).map_err(|err| -> ClientLocationError { err.into() })?;
|
|
let room_id = client_location.get_room(id).map_err(|err| -> ClientLocationError { err.into() })?;
|
|
let room = rooms.get_mut(room_id.0)
|
|
.ok_or(ShipError::InvalidRoom(room_id.0 as u32))?
|
|
.as_mut()
|
|
.ok_or(ShipError::InvalidRoom(room_id.0 as u32))?;
|
|
|
|
let monster = room.maps.enemy_by_id(request_exp.enemy_id as usize)?;
|
|
let monster_stats = room.monster_stats.get(&monster.monster).ok_or(ShipError::UnknownMonster(monster.monster))?;
|
|
|
|
let exp_gain = if request_exp.last_hitter == 1 {
|
|
monster_stats.exp
|
|
}
|
|
else {
|
|
((monster_stats.exp as f32) * 0.8) as u32
|
|
};
|
|
|
|
let clients_in_area = client_location.get_clients_in_room(room_id).map_err(|err| -> ClientLocationError { err.into() })?;
|
|
let gain_exp_pkt = builder::message::character_gained_exp(area_client, exp_gain);
|
|
let mut exp_pkts: Box<dyn Iterator<Item = _> + Send> = Box::new(clients_in_area.clone().into_iter()
|
|
.map(move |c| {
|
|
(c.client, SendShipPacket::Message(Message::new(GameMessage::GiveCharacterExp(gain_exp_pkt.clone()))))
|
|
}));
|
|
|
|
let before_level = level_table.get_level_from_exp(client.character.char_class, client.character.exp);
|
|
let after_level = level_table.get_level_from_exp(client.character.char_class, client.character.exp + exp_gain);
|
|
let level_up = before_level != after_level;
|
|
|
|
if level_up {
|
|
let (_, before_stats) = level_table.get_stats_from_exp(client.character.char_class, client.character.exp);
|
|
let (after_level, after_stats) = level_table.get_stats_from_exp(client.character.char_class, client.character.exp + exp_gain);
|
|
|
|
let level_up_pkt = builder::message::character_leveled_up(area_client, after_level, before_stats, after_stats);
|
|
exp_pkts = Box::new(exp_pkts.chain(clients_in_area.into_iter()
|
|
.map(move |c| {
|
|
(c.client, SendShipPacket::Message(Message::new(GameMessage::PlayerLevelUp(level_up_pkt.clone()))))
|
|
})))
|
|
}
|
|
|
|
client.character.exp += exp_gain;
|
|
entity_gateway.save_character(&client.character).await?;
|
|
|
|
Ok(exp_pkts)
|
|
}
|
|
|
|
pub async fn player_drop_item<EG>(id: ClientId,
|
|
player_drop_item: &PlayerDropItem,
|
|
entity_gateway: &mut EG,
|
|
client_location: &ClientLocation,
|
|
clients: &mut Clients,
|
|
rooms: &mut Rooms,
|
|
item_state: &mut ItemState)
|
|
-> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, anyhow::Error>
|
|
where
|
|
EG: EntityGateway
|
|
{
|
|
let client = clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?;
|
|
let room_id = client_location.get_room(id).map_err(|err| -> ClientLocationError { err.into() })?;
|
|
let room = rooms.get_mut(room_id.0)
|
|
.ok_or(ShipError::InvalidRoom(room_id.0 as u32))?
|
|
.as_mut()
|
|
.ok_or(ShipError::InvalidRoom(room_id.0 as u32))?;
|
|
let area = room.map_areas.get_area_map(player_drop_item.map_area)?;
|
|
drop_item(item_state, entity_gateway, &client.character, &ClientItemId(player_drop_item.item_id), *area, (player_drop_item.x, player_drop_item.y, player_drop_item.z)).await?;
|
|
let clients_in_area = client_location.get_clients_in_room(room_id).map_err(|err| -> ClientLocationError { err.into() })?;
|
|
let pdi = player_drop_item.clone();
|
|
Ok(Box::new(clients_in_area.into_iter()
|
|
.map(move |c| {
|
|
(c.client, SendShipPacket::Message(Message::new(GameMessage::PlayerDropItem(pdi.clone()))))
|
|
})))
|
|
}
|
|
|
|
pub fn drop_coordinates(id: ClientId,
|
|
drop_coordinates: &DropCoordinates,
|
|
client_location: &ClientLocation,
|
|
clients: &mut Clients,
|
|
rooms: &Rooms)
|
|
-> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, anyhow::Error>
|
|
{
|
|
let client = clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?;
|
|
let room_id = client_location.get_room(id).map_err(|err| -> ClientLocationError { err.into() })?;
|
|
let room = rooms.get(room_id.0)
|
|
.ok_or(ShipError::InvalidRoom(room_id.0 as u32))?
|
|
.as_ref()
|
|
.ok_or(ShipError::InvalidRoom(room_id.0 as u32))?;
|
|
|
|
client.item_drop_location = Some(ItemDropLocation {
|
|
map_area: *room.map_areas.get_area_map(drop_coordinates.map_area)?,
|
|
x: drop_coordinates.x,
|
|
z: drop_coordinates.z,
|
|
item_id: ClientItemId(drop_coordinates.item_id),
|
|
});
|
|
|
|
Ok(Box::new(None.into_iter())) // TODO: do we need to send a packet here?
|
|
}
|
|
|
|
pub async fn no_longer_has_item<EG>(id: ClientId,
|
|
no_longer_has_item: &PlayerNoLongerHasItem,
|
|
entity_gateway: &mut EG,
|
|
client_location: &ClientLocation,
|
|
clients: &mut Clients,
|
|
item_state: &mut ItemState)
|
|
-> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, anyhow::Error>
|
|
where
|
|
EG: EntityGateway
|
|
{
|
|
let client = clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?;
|
|
let area_client = client_location.get_local_client(id).map_err(|err| -> ClientLocationError { err.into() })?;
|
|
let room_id = client_location.get_room(id).map_err(|err| -> ClientLocationError { err.into() })?;
|
|
if let Some(drop_location) = client.item_drop_location {
|
|
if drop_location.item_id.0 != no_longer_has_item.item_id {
|
|
return Err(ShipError::DropInvalidItemId(no_longer_has_item.item_id).into());
|
|
}
|
|
|
|
if no_longer_has_item.item_id == 0xFFFFFFFF {
|
|
let dropped_meseta = drop_meseta(item_state, entity_gateway, &client.character, drop_location.map_area, (drop_location.x, drop_location.z), no_longer_has_item.amount).await?;
|
|
|
|
let dropped_meseta_pkt = builder::message::drop_split_meseta_stack(area_client, &dropped_meseta)?;
|
|
let no_longer_has_meseta_pkt = builder::message::player_no_longer_has_meseta(area_client, no_longer_has_item.amount as u32);
|
|
client.item_drop_location = None;
|
|
|
|
let clients_in_area = client_location.get_clients_in_room(room_id).map_err(|err| -> ClientLocationError { err.into() })?;
|
|
Ok(Box::new(clients_in_area.into_iter()
|
|
.flat_map(move |c| {
|
|
std::iter::once((c.client, SendShipPacket::Message(Message::new(GameMessage::DropSplitStack(dropped_meseta_pkt.clone())))))
|
|
.chain(
|
|
if c.client != id {
|
|
Box::new(std::iter::once(
|
|
(c.client, SendShipPacket::Message(Message::new(GameMessage::PlayerNoLongerHasItem(no_longer_has_meseta_pkt.clone()))))
|
|
)) as Box<dyn Iterator<Item = _> + Send>
|
|
}
|
|
else {
|
|
Box::new(std::iter::empty()) as Box<dyn Iterator<Item = _> + Send>
|
|
}
|
|
)
|
|
})
|
|
))
|
|
}
|
|
else {
|
|
let dropped_item = drop_partial_item(item_state, entity_gateway, &client.character, &drop_location.item_id, drop_location.map_area, (drop_location.x, drop_location.z), no_longer_has_item.amount).await?;
|
|
|
|
let dropped_item_pkt = builder::message::drop_split_stack(area_client, &dropped_item)?;
|
|
client.item_drop_location = None;
|
|
|
|
let clients_in_area = client_location.get_clients_in_room(room_id).map_err(|err| -> ClientLocationError { err.into() })?;
|
|
Ok(Box::new(clients_in_area.into_iter()
|
|
.map(move |c| {
|
|
(c.client, SendShipPacket::Message(Message::new(GameMessage::DropSplitStack(dropped_item_pkt.clone()))))
|
|
})))
|
|
}
|
|
}
|
|
else if let Some(_tek) = client.tek {
|
|
let neighbors = client_location.get_client_neighbors(id).map_err(|err| -> ClientLocationError { err.into() })?;
|
|
let no_longer_has_item = no_longer_has_item.clone();
|
|
Ok(Box::new(neighbors.into_iter()
|
|
.map(move |c| {
|
|
(c.client, SendShipPacket::Message(Message::new(GameMessage::PlayerNoLongerHasItem(no_longer_has_item.clone()))))
|
|
})))
|
|
}
|
|
else {
|
|
Err(ShipError::InvalidItem(ClientItemId(no_longer_has_item.item_id)).into())
|
|
}
|
|
}
|
|
|
|
pub fn update_player_position(id: ClientId,
|
|
message: &Message,
|
|
clients: &mut Clients,
|
|
client_location: &ClientLocation,
|
|
rooms: &Rooms)
|
|
-> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, anyhow::Error> {
|
|
let client = clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?;
|
|
if let Ok(room_id) = client_location.get_room(id).map_err(|err| -> ClientLocationError { err.into() }) {
|
|
let room = rooms.get(room_id.0)
|
|
.ok_or(ShipError::InvalidRoom(room_id.0 as u32))?
|
|
.as_ref()
|
|
.ok_or(ShipError::InvalidRoom(room_id.0 as u32))?;
|
|
|
|
match &message.msg {
|
|
GameMessage::PlayerChangedMap(p) => {
|
|
client.x = p.x;
|
|
client.y = p.y;
|
|
client.z = p.z;
|
|
},
|
|
GameMessage::PlayerChangedMap2(p) => {
|
|
client.area = room.map_areas.get_area_map(p.map_area).ok().cloned();
|
|
},
|
|
GameMessage::TellOtherPlayerMyLocation(p) => {
|
|
client.x = p.x;
|
|
client.y = p.y;
|
|
client.z = p.z;
|
|
client.area = room.map_areas.get_area_map(p.map_area).ok().cloned();
|
|
},
|
|
GameMessage::PlayerWarpingToFloor(p) => {
|
|
client.area = room.map_areas.get_area_map(p.area as u16).ok().cloned();
|
|
},
|
|
GameMessage::PlayerTeleported(p) => {
|
|
client.x = p.x;
|
|
client.y = p.y;
|
|
client.z = p.z;
|
|
},
|
|
GameMessage::PlayerStopped(p) => {
|
|
client.x = p.x;
|
|
client.y = p.y;
|
|
client.z = p.z;
|
|
},
|
|
GameMessage::PlayerLoadedIn(p) => {
|
|
client.x = p.x;
|
|
client.y = p.y;
|
|
client.z = p.z;
|
|
},
|
|
GameMessage::PlayerWalking(p) => {
|
|
client.x = p.x;
|
|
client.z = p.z;
|
|
},
|
|
GameMessage::PlayerRunning(p) => {
|
|
client.x = p.x;
|
|
client.z = p.z;
|
|
},
|
|
GameMessage::PlayerWarped(p) => {
|
|
client.x = p.x;
|
|
client.y = p.y;
|
|
},
|
|
// GameMessage::PlayerChangedFloor(p) => {client.area = MapArea::from_value(&room.mode.episode(), p.map).ok();},
|
|
GameMessage::InitializeSpeechNpc(p) => {
|
|
client.x = p.x;
|
|
client.z = p.z;
|
|
}
|
|
_ => {},
|
|
}
|
|
} else {}
|
|
let m = message.clone();
|
|
Ok(Box::new(client_location.get_client_neighbors(id).unwrap().into_iter()
|
|
.map(move |client| {
|
|
(client.client, SendShipPacket::Message(m.clone()))
|
|
})))
|
|
}
|
|
|
|
pub async fn charge_attack<EG>(id: ClientId,
|
|
charge: &ChargeAttack,
|
|
entity_gateway: &mut EG,
|
|
client_location: &ClientLocation,
|
|
clients: &mut Clients,
|
|
item_state: &mut ItemState)
|
|
-> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, anyhow::Error>
|
|
where
|
|
EG: EntityGateway
|
|
{
|
|
let client = clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?;
|
|
|
|
// TODO: should probably validate this to be a legit number, I'd just hardcode 200 but vjaya
|
|
take_meseta(item_state, entity_gateway, &client.character.id, Meseta(charge.meseta)).await?;
|
|
|
|
let charge = charge.clone();
|
|
Ok(Box::new(client_location.get_client_neighbors(id).unwrap().into_iter()
|
|
.map(move |client| {
|
|
(client.client, SendShipPacket::Message(Message::new(GameMessage::ChargeAttack(charge.clone()))))
|
|
})))
|
|
}
|
|
|
|
pub async fn player_uses_item<EG>(id: ClientId,
|
|
player_use_tool: &PlayerUseItem,
|
|
entity_gateway: &mut EG,
|
|
_client_location: &ClientLocation,
|
|
clients: &mut Clients,
|
|
item_state: &mut ItemState)
|
|
-> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, anyhow::Error>
|
|
where
|
|
EG: EntityGateway
|
|
{
|
|
let client = clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?;
|
|
use_item(item_state, entity_gateway, &mut client.character, &ClientItemId(player_use_tool.item_id), 1).await?;
|
|
Ok(Box::new(None.into_iter())) // TODO: should probably tell other players we used an item
|
|
}
|
|
|
|
pub async fn player_used_medical_center<EG>(id: ClientId,
|
|
pumc: &PlayerUsedMedicalCenter,
|
|
entity_gateway: &mut EG,
|
|
client_location: &ClientLocation,
|
|
clients: &mut Clients,
|
|
item_state: &mut ItemState)
|
|
-> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, anyhow::Error>
|
|
where
|
|
EG: EntityGateway
|
|
{
|
|
let client = clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?;
|
|
|
|
take_meseta(item_state, entity_gateway, &client.character.id, Meseta(10)).await?;
|
|
|
|
let pumc = pumc.clone();
|
|
Ok(Box::new(client_location.get_client_neighbors(id).unwrap().into_iter()
|
|
.map(move |client| {
|
|
(client.client, SendShipPacket::Message(Message::new(GameMessage::PlayerUsedMedicalCenter(pumc.clone()))))
|
|
})))
|
|
}
|
|
|
|
|
|
pub async fn player_feed_mag<EG>(id: ClientId,
|
|
mag_feed: &PlayerFeedMag,
|
|
entity_gateway: &mut EG,
|
|
client_location: &ClientLocation,
|
|
clients: &Clients,
|
|
item_state: &mut ItemState)
|
|
-> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, anyhow::Error>
|
|
where
|
|
EG: EntityGateway
|
|
{
|
|
let client = clients.get(&id).ok_or(ShipError::ClientNotFound(id))?;
|
|
feed_mag(item_state, entity_gateway, &client.character, &ClientItemId(mag_feed.mag_id), &ClientItemId(mag_feed.item_id)).await?;
|
|
|
|
let mag_feed = mag_feed.clone();
|
|
Ok(Box::new(client_location.get_client_neighbors(id).unwrap().into_iter()
|
|
.map(move |client| {
|
|
(client.client, SendShipPacket::Message(Message::new(GameMessage::PlayerFeedMag(mag_feed.clone()))))
|
|
})))
|
|
}
|
|
|
|
pub async fn player_equips_item<EG>(id: ClientId,
|
|
pkt: &PlayerEquipItem,
|
|
entity_gateway: &mut EG,
|
|
clients: &Clients,
|
|
item_state: &mut ItemState)
|
|
-> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, anyhow::Error>
|
|
where
|
|
EG: EntityGateway
|
|
{
|
|
let client = clients.get(&id).ok_or(ShipError::ClientNotFound(id))?;
|
|
let equip_slot = if pkt.sub_menu > 0 {
|
|
((pkt.sub_menu & 0x7) - 1) % 4
|
|
}
|
|
else {
|
|
0
|
|
};
|
|
equip_item(item_state, entity_gateway, &client.character, &ClientItemId(pkt.item_id), equip_slot).await?;
|
|
Ok(Box::new(None.into_iter())) // TODO: tell other players you equipped an item
|
|
}
|
|
|
|
pub async fn player_unequips_item<EG>(id: ClientId,
|
|
pkt: &PlayerUnequipItem,
|
|
entity_gateway: &mut EG,
|
|
clients: &Clients,
|
|
item_state: &mut ItemState)
|
|
-> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, anyhow::Error>
|
|
where
|
|
EG: EntityGateway
|
|
{
|
|
let client = clients.get(&id).ok_or(ShipError::ClientNotFound(id))?;
|
|
unequip_item(item_state, entity_gateway, &client.character, &ClientItemId(pkt.item_id)).await?;
|
|
Ok(Box::new(None.into_iter())) // TODO: tell other players if you unequip an item
|
|
}
|
|
|
|
pub async fn player_sorts_items<EG>(id: ClientId,
|
|
pkt: &SortItems,
|
|
entity_gateway: &mut EG,
|
|
clients: &Clients,
|
|
item_state: &mut ItemState)
|
|
-> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, anyhow::Error>
|
|
where
|
|
EG: EntityGateway
|
|
{
|
|
let client = clients.get(&id).ok_or(ShipError::ClientNotFound(id))?;
|
|
let item_ids = pkt.item_ids
|
|
.iter()
|
|
.filter_map(|item_id| {
|
|
if *item_id != 0 {
|
|
Some(ClientItemId(*item_id))
|
|
}
|
|
else {
|
|
None
|
|
}
|
|
})
|
|
.collect();
|
|
sort_inventory(item_state, entity_gateway, &client.character, item_ids).await?;
|
|
Ok(Box::new(None.into_iter())) // TODO: clients probably care about each others item orders
|
|
}
|
|
|
|
pub async fn player_sells_item<EG> (id: ClientId,
|
|
sold_item: &PlayerSoldItem,
|
|
entity_gateway: &mut EG,
|
|
clients: &mut Clients,
|
|
item_state: &mut ItemState)
|
|
-> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, anyhow::Error>
|
|
where
|
|
EG: EntityGateway
|
|
{
|
|
let client = clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?;
|
|
sell_item(item_state, entity_gateway, &client.character, ClientItemId(sold_item.item_id), sold_item.amount as u32).await?;
|
|
// TODO: send the packet to other clients
|
|
Ok(Box::new(None.into_iter()))
|
|
}
|