diff --git a/Adventures in Lestoria/Merchant.cpp b/Adventures in Lestoria/Merchant.cpp index dfa55eb6..e28eb7cd 100644 --- a/Adventures in Lestoria/Merchant.cpp +++ b/Adventures in Lestoria/Merchant.cpp @@ -30,7 +30,7 @@ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -Portions of this software are copyright © 2023 The FreeType +Portions of this software are copyright � 2023 The FreeType Project (www.freetype.org). Please see LICENSE_FT.txt for more information. All rights reserved. */ @@ -67,8 +67,9 @@ const std::vector>&Merchant::GetShopItems(ITCategory categor return sortedItems[category]; } -Merchant&Merchant::AddMerchant(Chapter chapter){ +Merchant&Merchant::AddMerchant(Chapter chapter,std::string_view keyName){ merchants[chapter].push_back({}); + merchants[chapter].back().keyName=keyName; return merchants[chapter].back(); } @@ -103,7 +104,7 @@ void Merchant::Initialize(){ utils::datafile&chapterMerchantInfo=DATA["Merchant"][std::format("Chapter {}",chapter)]; for(auto&[key,size]:chapterMerchantInfo){ utils::datafile&data=chapterMerchantInfo.GetProperty(key); - Merchant&newMerchant=AddMerchant(chapter); + Merchant&newMerchant=AddMerchant(chapter,key); for(int itemNumber=1;auto&[key,size]:data){ if(key=="DisplayName")newMerchant.displayName=data[key].GetString(); else @@ -127,6 +128,7 @@ void Merchant::Initialize(){ } merchantCount++; } + if(merchantCount==0)ERR(std::format("WARNING! No merchants available for Chapter {}!",chapter)); std::cout<item,uint32_t amt){ }; void Merchant::RandomizeTravelingMerchant(){ - travelingMerchant=GetRandomMerchant(game->GetCurrentChapter()); - for(auto&[key,items]:ITEM_CATEGORIES){ - Menu::MerchantInventorySlotsUpdated(key); - } - UpdateSortedItemsList(); + SetTravelingMerchant(const_cast(GetRandomMerchant(game->GetCurrentChapter())).GetKeyName()); }; Merchant&Merchant::GetCurrentTravelingMerchant(){ return travelingMerchant; -}; \ No newline at end of file +}; + +std::string_view Merchant::GetKeyName()const{ + return keyName; +} + +void Merchant::SetTravelingMerchant(std::string_view key){ + for(auto&[merchantKey,merchantList]:merchants){ + auto it=std::find_if(merchantList.begin(),merchantList.end(),[&](const Merchant&merchant){return merchant.GetKeyName()==key;}); + if(it!=merchantList.end()){ + travelingMerchant=*it; + for(auto&[key,items]:ITEM_CATEGORIES){ + Menu::MerchantInventorySlotsUpdated(key); + } + UpdateSortedItemsList(); + return; + } + } + std::cout<>&GetShopItems()const; @@ -54,9 +55,10 @@ public: bool CanSellItem(std::weak_ptrIT,uint32_t amt=1U)const; void PurchaseItem(IT item,uint32_t amt=1U); void SellItem(std::weak_ptr,uint32_t amt=1U); + std::string_view GetKeyName()const; public: static void Initialize(); - static Merchant&AddMerchant(Chapter chapter); + static Merchant&AddMerchant(Chapter chapter,std::string_view keyName); private: static MerchantFunctionPrimingData purchaseFunctionPrimed; static MerchantFunctionPrimingData sellFunctionPrimed; @@ -64,6 +66,7 @@ private: static const Merchant&GetRandomMerchant(Chapter chapter); static std::map>merchants; std::string displayName; + std::string keyName; std::vector>shopItems; static std::map>>sortedItems; static void UpdateSortedItemsList(); diff --git a/Adventures in Lestoria/SaveFile.cpp b/Adventures in Lestoria/SaveFile.cpp index cea4dd73..e5c9e2d5 100644 --- a/Adventures in Lestoria/SaveFile.cpp +++ b/Adventures in Lestoria/SaveFile.cpp @@ -119,6 +119,7 @@ const void SaveFile::SaveGame(){ saveFile["Chapter"].SetInt(game->GetCurrentChapter()); saveFile["Save Name"].SetString(std::string(GetSaveFileName())); saveFile["Game Time"].SetReal(game->GetRuntime()); + saveFile["TravelingMerchant"].SetString(std::string(Merchant::GetCurrentTravelingMerchant().GetKeyName())); utils::datafile::Write(saveFile,"save_file_path"_S+std::format("save.{:04}",saveFileID)); utils::datafile metadata; if(onlineMode){ @@ -246,6 +247,7 @@ void SaveFile::LoadFile(){ SaveFile::SetSaveFileName(loadFile["Save Name"].GetString()); game->SetRuntime(loadFile["Game Time"].GetReal()); game->GetPlayer()->RecalculateEquipStats(); + if(loadFile.HasProperty("TravelingMerchant"))Merchant::SetTravelingMerchant(loadFile["TravelingMerchant"].GetString()); GameState::ChangeState(States::OVERWORLD_MAP,0.5f); }else{ diff --git a/Adventures in Lestoria/assets/config/shops/Chapter 2 Merchants.txt b/Adventures in Lestoria/assets/config/shops/Chapter 2 Merchants.txt index d1bc664f..b80091c4 100644 --- a/Adventures in Lestoria/assets/config/shops/Chapter 2 Merchants.txt +++ b/Adventures in Lestoria/assets/config/shops/Chapter 2 Merchants.txt @@ -2,6 +2,12 @@ Merchant { Chapter 2 { - + Chapter2_A + { + DisplayName = Merchant + # Specify items this merchant sells. Add an optional quantity as a second argument. Not specifying a second argument will give the shop infinite supply. + Item[1] = Minor Health Potion + Item[2] = Minor Mana Potion + } } } \ No newline at end of file diff --git a/Adventures in Lestoria/assets/config/shops/Chapter 3 Merchants.txt b/Adventures in Lestoria/assets/config/shops/Chapter 3 Merchants.txt index a5a9f645..63cc1e1c 100644 --- a/Adventures in Lestoria/assets/config/shops/Chapter 3 Merchants.txt +++ b/Adventures in Lestoria/assets/config/shops/Chapter 3 Merchants.txt @@ -2,6 +2,12 @@ Merchant { Chapter 3 { - + Chapter3_A + { + DisplayName = Merchant + # Specify items this merchant sells. Add an optional quantity as a second argument. Not specifying a second argument will give the shop infinite supply. + Item[1] = Minor Health Potion + Item[2] = Minor Mana Potion + } } } \ No newline at end of file diff --git a/Adventures in Lestoria/assets/config/shops/Chapter 4 Merchants.txt b/Adventures in Lestoria/assets/config/shops/Chapter 4 Merchants.txt index 5184e78a..d25ddd0a 100644 --- a/Adventures in Lestoria/assets/config/shops/Chapter 4 Merchants.txt +++ b/Adventures in Lestoria/assets/config/shops/Chapter 4 Merchants.txt @@ -2,6 +2,12 @@ Merchant { Chapter 4 { - + Chapter4_A + { + DisplayName = Merchant + # Specify items this merchant sells. Add an optional quantity as a second argument. Not specifying a second argument will give the shop infinite supply. + Item[1] = Minor Health Potion + Item[2] = Minor Mana Potion + } } } \ No newline at end of file diff --git a/Adventures in Lestoria/assets/config/shops/Chapter 5 Merchants.txt b/Adventures in Lestoria/assets/config/shops/Chapter 5 Merchants.txt index 66f948fd..9a8a4b42 100644 --- a/Adventures in Lestoria/assets/config/shops/Chapter 5 Merchants.txt +++ b/Adventures in Lestoria/assets/config/shops/Chapter 5 Merchants.txt @@ -2,6 +2,12 @@ Merchant { Chapter 5 { - + Chapter5_A + { + DisplayName = Merchant + # Specify items this merchant sells. Add an optional quantity as a second argument. Not specifying a second argument will give the shop infinite supply. + Item[1] = Minor Health Potion + Item[2] = Minor Mana Potion + } } } \ No newline at end of file diff --git a/Adventures in Lestoria/assets/config/shops/Chapter 6 Merchants.txt b/Adventures in Lestoria/assets/config/shops/Chapter 6 Merchants.txt index daa02fff..fa5be5cd 100644 --- a/Adventures in Lestoria/assets/config/shops/Chapter 6 Merchants.txt +++ b/Adventures in Lestoria/assets/config/shops/Chapter 6 Merchants.txt @@ -2,6 +2,12 @@ Merchant { Chapter 6 { - + Chapter6_A + { + DisplayName = Merchant + # Specify items this merchant sells. Add an optional quantity as a second argument. Not specifying a second argument will give the shop infinite supply. + Item[1] = Minor Health Potion + Item[2] = Minor Mana Potion + } } } \ No newline at end of file