From 761e20e3747b287f33b79aaf50a43de5cdd1ff8f Mon Sep 17 00:00:00 2001
From: andy <andynewjack@protonmail.com>
Date: Sun, 6 Feb 2022 21:53:11 +0000
Subject: [PATCH] add quest_group for displaying regular or govt quests

---
 data/quests/bb/ep1/multi/quests.toml |  9 --------
 src/ship/packet/handler/quest.rs     | 15 +++++++------
 src/ship/room.rs                     | 32 ++++++++++++++++++++++++++--
 src/ship/ship.rs                     |  4 ++--
 4 files changed, 40 insertions(+), 20 deletions(-)

diff --git a/data/quests/bb/ep1/multi/quests.toml b/data/quests/bb/ep1/multi/quests.toml
index da1697a..061ee38 100644
--- a/data/quests/bb/ep1/multi/quests.toml
+++ b/data/quests/bb/ep1/multi/quests.toml
@@ -133,12 +133,3 @@ dat = "q137-evt-bb.dat"
 bin = "q138-evt-bb.bin"
 dat = "q138-evt-bb.dat"
 
-
-[Shop]
-list_order = 5
-description = "Time to do some shopping!"
-
-[[Shop.quests]]
-bin = "q219-shp-bb.bin"
-dat = "q219-shp-bb.dat"
-
diff --git a/src/ship/packet/handler/quest.rs b/src/ship/packet/handler/quest.rs
index e2b659c..a0c621e 100644
--- a/src/ship/packet/handler/quest.rs
+++ b/src/ship/packet/handler/quest.rs
@@ -36,12 +36,13 @@ fn parse_filename(filename_bytes: &[u8; 16]) -> Result<(u16, u16, QuestFileType)
 }
 
 
-pub fn send_quest_category_list(id: ClientId, client_location: &ClientLocation, rooms: &mut Rooms) -> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, ShipError> {
+pub fn send_quest_category_list(id: ClientId, rql: &RequestQuestList, client_location: &ClientLocation, rooms: &mut Rooms) -> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, ShipError> {
     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 qcl = quest::quest_category_list(&room.quests);
+    let qcl = quest::quest_category_list(&room.quests[rql.flag as usize]);
+    room.set_quest_group(rql.flag as usize);
     Ok(Box::new(vec![(id, SendShipPacket::QuestCategoryList(qcl))].into_iter()))
 }
 
@@ -50,7 +51,7 @@ pub fn select_quest_category(id: ClientId, menuselect: &MenuSelect, client_locat
     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 (_, category_quests) = room.quests.iter()
+    let (_, category_quests) = room.quests[room.quest_group].iter() // TODO: error handling for invalid quest group
         .nth(menuselect.item as usize)
         .ok_or(ShipError::InvalidQuestCategory(menuselect.item))?;
 
@@ -67,7 +68,7 @@ pub fn quest_detail(id: ClientId, questdetailrequest: &QuestDetailRequest, clien
     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 (_, category_quests) = room.quests.iter()
+    let (_, category_quests) = room.quests[room.quest_group].iter()
         .nth(questdetailrequest.category as usize)
         .ok_or(ShipError::InvalidQuestCategory(questdetailrequest.category as u32))?;
 
@@ -87,7 +88,7 @@ pub fn player_chose_quest(id: ClientId, questmenuselect: &QuestMenuSelect, clien
     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 (_, category_quests) = room.quests.iter()
+    let (_, category_quests) = room.quests[room.quest_group].iter()
         .nth(questmenuselect.category as usize)
         .ok_or(ShipError::InvalidQuestCategory(questmenuselect.category as u32))?;
 
@@ -120,7 +121,7 @@ pub fn quest_file_request(id: ClientId, quest_file_request: &QuestFileRequest, c
         .ok_or(ShipError::InvalidRoom(room_id.0 as u32))?;
 
     let (category_id, quest_id, datatype) = parse_filename(&quest_file_request.filename)?;
-    let (_, category_quests) = room.quests.iter()
+    let (_, category_quests) = room.quests[room.quest_group].iter()
         .nth(category_id as usize)
         .ok_or(ShipError::InvalidQuestCategory(category_id as u32))?;
 
@@ -149,7 +150,7 @@ pub fn quest_chunk_ack(id: ClientId, quest_chunk_ack: &QuestChunkAck, client_loc
         .ok_or(ShipError::InvalidRoom(room_id.0 as u32))?;
 
     let (category_id, quest_id, datatype) = parse_filename(&quest_chunk_ack.filename)?;
-    let (_, category_quests) = room.quests.iter()
+    let (_, category_quests) = room.quests[room.quest_group].iter()
         .nth(category_id as usize)
         .ok_or(ShipError::InvalidQuestCategory(category_id as u32))?;
 
diff --git a/src/ship/room.rs b/src/ship/room.rs
index 588870d..2c2e0a2 100644
--- a/src/ship/room.rs
+++ b/src/ship/room.rs
@@ -179,7 +179,8 @@ pub struct RoomState {
     pub monster_stats: Box<HashMap<MonsterType, MonsterStats>>,
     pub map_areas: MapAreaLookup,
     pub rare_monster_table: Box<RareMonsterAppearTable>,
-    pub quests: quests::QuestList,
+    pub quest_group: usize,
+    pub quests: Vec<quests::QuestList>,
     // items on ground
     // enemy info
 }
@@ -215,6 +216,10 @@ impl RoomState {
         difficulty + 0x22
     }
 
+    pub fn set_quest_group(&mut self, group: usize) {
+        self.quest_group = group;
+    }
+
     pub fn from_create_room(create_room: &libpso::packet::ship::CreateRoom, section_id: SectionID) -> Result<RoomState, RoomCreationError> {
         if [create_room.battle, create_room.challenge, create_room.single_player].iter().sum::<u8>() > 1 {
             return Err(RoomCreationError::InvalidMode)
@@ -245,15 +250,36 @@ impl RoomState {
         };
 
         let rare_monster_table = RareMonsterAppearTable::new(room_mode.episode());
+
+        // push the usual set of quests for the selected mode
         let mut qpath = PathBuf::from("data/quests/bb");
         qpath.push(room_mode.episode().to_string());
         qpath.push(room_mode.to_string());
         qpath.push("quests.toml");
-        let room_quests = match quests::load_quests(&mut qpath) {
+        let mut room_quests = Vec::new();
+        let quest_list = match quests::load_quests(&mut qpath) {
             Ok(qlist) => qlist,
             Err(_) => return Err(RoomCreationError::CouldNotLoadQuests),
         };
 
+        room_quests.push(quest_list);
+        
+        // if multiplayer also push the government quests
+        if let RoomMode::Multi {..} = room_mode {
+            qpath = PathBuf::from("data/quests/bb/");
+            qpath.push(room_mode.episode().to_string());
+            qpath.push("government/quests.toml");
+
+            let quest_list = match quests::load_quests(&mut qpath) {
+                Ok(qlist) => qlist,
+                Err(_) => return Err(RoomCreationError::CouldNotLoadQuests),
+            };
+
+            room_quests.push(quest_list);
+        }
+
+
+
         Ok(RoomState {
             monster_stats: Box::new(load_monster_stats_table(&room_mode).map_err(|_| RoomCreationError::CouldNotLoadMonsterStats(room_mode))?),
             mode: room_mode,
@@ -266,6 +292,8 @@ impl RoomState {
             drop_table: Box::new(DropTable::new(room_mode.episode(), room_mode.difficulty(), section_id)),
             bursting: false,
             map_areas: MapAreaLookup::new(&room_mode.episode()),
+            // quests: quest_list,
+            quest_group: 0,
             quests: room_quests,
         })
     }
diff --git a/src/ship/ship.rs b/src/ship/ship.rs
index 2774266..5ae6169 100644
--- a/src/ship/ship.rs
+++ b/src/ship/ship.rs
@@ -708,9 +708,9 @@ impl<EG: EntityGateway> ServerState for ShipServerState<EG> {
                 let block = self.blocks.with_client(id, &self.clients)?;
                 Box::new(handler::lobby::change_lobby(id, pkt.lobby, &mut block.client_location, &self.clients, &mut self.item_manager, &self.level_table, &mut block.rooms, &mut self.entity_gateway).await?.into_iter())
             },
-            RecvShipPacket::RequestQuestList(_) => {
+            RecvShipPacket::RequestQuestList(rql) => {
                 let block = self.blocks.with_client(id, &self.clients)?;
-                handler::quest::send_quest_category_list(id, &block.client_location, &mut block.rooms)?
+                handler::quest::send_quest_category_list(id, rql, &block.client_location, &mut block.rooms)?
             },
             RecvShipPacket::QuestFileRequest(quest_file_request) => {
                 let block = self.blocks.with_client(id, &self.clients)?;