From 33b80d7235059abe3854abecdf9253b14b01ed1c Mon Sep 17 00:00:00 2001 From: jake Date: Sun, 29 Jan 2023 12:48:42 -0700 Subject: [PATCH] mapbuilder trait -> just a function --- src/ship/map/maps.rs | 47 +++++++++++---------------------- src/ship/packet/handler/room.rs | 6 ++--- src/ship/room.rs | 6 ++--- src/ship/ship.rs | 10 +++---- 4 files changed, 26 insertions(+), 43 deletions(-) diff --git a/src/ship/map/maps.rs b/src/ship/map/maps.rs index 46a7189..94d2193 100644 --- a/src/ship/map/maps.rs +++ b/src/ship/map/maps.rs @@ -358,37 +358,20 @@ impl Maps { } } -pub trait MapBuilder: Send + Sync { - fn generate_maps(&self, room_mode: RoomMode, event: ShipEvent) -> Maps; -} - -#[derive(Clone)] -pub struct FreeRoamMapBuilder { -} - -impl FreeRoamMapBuilder { - pub fn new() -> FreeRoamMapBuilder { - FreeRoamMapBuilder { - } - } -} - -impl MapBuilder for FreeRoamMapBuilder { - fn generate_maps(&self, room_mode: RoomMode, event: ShipEvent) -> Maps { - let rare_monster_table = RareMonsterAppearTable::new(room_mode.episode()); - let map_variants = map_variants(room_mode.episode(), room_mode.player_mode()); - Maps { - enemy_data: map_variants.iter() - .flat_map(|map_variant| { - enemy_data_from_map_data(map_variant, &room_mode.episode()) - }) - .map(|enemy| enemy.map(|enemy| rare_monster_table.apply(enemy, event))) - .collect(), - object_data: map_variants.iter() - .flat_map(|map_variant| { - objects_from_map_data(map_variant.obj_file().into(), &room_mode.episode(), &map_variant.map) - }).collect(), - map_variants, - } +pub fn generate_free_roam_maps(room_mode: RoomMode, event: ShipEvent) -> Maps { + let rare_monster_table = RareMonsterAppearTable::new(room_mode.episode()); + let map_variants = map_variants(room_mode.episode(), room_mode.player_mode()); + Maps { + enemy_data: map_variants.iter() + .flat_map(|map_variant| { + enemy_data_from_map_data(map_variant, &room_mode.episode()) + }) + .map(|enemy| enemy.map(|enemy| rare_monster_table.apply(enemy, event))) + .collect(), + object_data: map_variants.iter() + .flat_map(|map_variant| { + objects_from_map_data(map_variant.obj_file().into(), &room_mode.episode(), &map_variant.map) + }).collect(), + map_variants, } } diff --git a/src/ship/packet/handler/room.rs b/src/ship/packet/handler/room.rs index f5f2a85..0a108ca 100644 --- a/src/ship/packet/handler/room.rs +++ b/src/ship/packet/handler/room.rs @@ -10,8 +10,8 @@ use crate::common::leveltable::LEVEL_TABLE; use crate::entity::character::SectionID; use crate::ship::drops::DropTable; use crate::ship::ship::{SendShipPacket, Clients, ShipEvent}; -use crate::ship::room::{Rooms, Episode, Difficulty, RoomState}; -use crate::ship::map::MapBuilder; +use crate::ship::room::{Rooms, Episode, Difficulty, RoomState, RoomMode}; +use crate::ship::map::Maps; use crate::ship::location::{ClientLocation, RoomId, RoomLobby, GetAreaError}; use crate::ship::packet::builder; use crate::ship::items::state::ItemState; @@ -22,7 +22,7 @@ pub async fn create_room(id: ClientId, clients: &Clients, item_state: &mut ItemState, rooms: &Rooms, - map_builder: Arc>, + map_builder: Arc Maps + Send + Sync>>, drop_table_builder: Arc DropTable + Send + Sync>>, event: ShipEvent) -> Result, anyhow::Error> { diff --git a/src/ship/room.rs b/src/ship/room.rs index 6b9462a..a48bfc3 100644 --- a/src/ship/room.rs +++ b/src/ship/room.rs @@ -8,7 +8,7 @@ use futures::stream::{FuturesOrdered, Stream}; use thiserror::Error; use rand::Rng; -use crate::ship::map::{Maps, MapBuilder}; +use crate::ship::map::Maps; use crate::ship::drops::DropTable; use crate::entity::character::SectionID; use crate::ship::monster::{load_monster_stats_table, MonsterType, MonsterStats}; @@ -358,7 +358,7 @@ impl RoomState { } pub fn from_create_room(create_room: &libpso::packet::ship::CreateRoom, - map_builder: Arc>, + map_builder: Arc Maps + Send + Sync>>, drop_table_builder: Arc DropTable + Send + Sync>>, section_id: SectionID, event: ShipEvent) @@ -425,7 +425,7 @@ impl RoomState { random_seed: rand::thread_rng().gen(), name: String::from_utf16_lossy(&create_room.name).trim_matches(char::from(0)).into(), password: create_room.password, - maps: map_builder.generate_maps(room_mode, event), + maps: map_builder(room_mode, event), section_id, drop_table: Box::new(drop_table_builder(room_mode.episode(), room_mode.difficulty(), section_id)), bursting: false, diff --git a/src/ship/ship.rs b/src/ship/ship.rs index 8576b69..fc5581c 100644 --- a/src/ship/ship.rs +++ b/src/ship/ship.rs @@ -33,7 +33,7 @@ use crate::ship::location::{ClientLocation, RoomLobby, ClientLocationError, Room use crate::ship::drops::DropTable; use crate::ship::items; use crate::ship::room; -use crate::ship::map::{MapBuilder, FreeRoamMapBuilder, MapsError, MapAreaError}; +use crate::ship::map::{Maps, MapsError, MapAreaError, generate_free_roam_maps}; use crate::ship::packet::handler; use crate::ship::shops::{WeaponShop, ToolShop, ArmorShop}; use crate::ship::trade::TradeState; @@ -380,7 +380,7 @@ pub struct ShipServerStateBuilder { port: Option, auth_token: Option, event: Option, - map_builder: Option>, + map_builder: Option Maps + Send + Sync>>, drop_table_builder: Option DropTable + Send + Sync>>, num_blocks: usize, } @@ -439,7 +439,7 @@ impl ShipServerStateBuilder { } #[must_use] - pub fn map_builder(mut self, map_builder: Box) -> ShipServerStateBuilder { + pub fn map_builder(mut self, map_builder: Box Maps + Send + Sync>) -> ShipServerStateBuilder { self.map_builder = Some(map_builder); self } @@ -468,7 +468,7 @@ impl ShipServerStateBuilder { shops: ItemShops::default(), blocks: Blocks(blocks), event: self.event.unwrap_or(ShipEvent::None), - map_builder: Arc::new(self.map_builder.unwrap_or(Box::new(FreeRoamMapBuilder::new()))), + map_builder: Arc::new(self.map_builder.unwrap_or(Box::new(generate_free_roam_maps))), drop_table_builder: Arc::new(self.drop_table_builder.unwrap_or(Box::new(DropTable::new))), auth_token: self.auth_token.unwrap_or_else(|| AuthToken("".into())), @@ -518,7 +518,7 @@ pub struct ShipServerState { ship_list: Arc>>, shipgate_sender: Option>, trades: TradeState, - map_builder: Arc>, + map_builder: Arc Maps + Send + Sync>>, drop_table_builder: Arc DropTable + Send + Sync>>, }