Add player money functions. Implement Merchant transaction functions. Add Function Priming classes.

This commit is contained in:
2023-12-17 23:37:37 -06:00
parent 2b3c1ea1b4
commit f1aa872717
19 changed files with 350 additions and 93 deletions
+93 -5
View File
@@ -37,10 +37,16 @@ All rights reserved.
#pragma endregion
#include "Merchant.h"
#include "Crawler.h"
INCLUDE_game
std::map<Chapter,std::vector<Merchant>>Merchant::merchants;
MerchantFunctionPrimingData Merchant::purchaseFunctionPrimed("CanPurchaseItem()");
MerchantFunctionPrimingData Merchant::sellFunctionPrimed("CanSellItem()");
Merchant Merchant::travelingMerchant;
const Merchant&Merchant::GetRandomMerchant(Chapter chapter)const{
const Merchant&Merchant::GetRandomMerchant(Chapter chapter){
return merchants[chapter][rand()%(merchants[chapter].size()-1)];
}
@@ -57,8 +63,8 @@ Merchant&Merchant::AddMerchant(Chapter chapter){
return merchants[chapter].back();
}
void Merchant::AddItem(IT item,uint8_t enhancementLevel){
shopItems.push_back(Item{1,item,enhancementLevel});
void Merchant::AddItem(IT item,uint32_t amt,uint8_t enhancementLevel){
shopItems.push_back(Item{amt,item,enhancementLevel});
}
INCLUDE_DATA
@@ -82,7 +88,12 @@ void Merchant::Initialize(){
std::string itemKey=std::format("Item[{}]",itemNumber);
if(data.HasProperty(itemKey)){
IT itemName=data[itemKey].GetString();
newMerchant.AddItem(itemName);
if(data[itemKey].GetValueCount()>1){
int qty=data[itemKey].GetInt(1);
newMerchant.AddItem(itemName,qty);
}else{
newMerchant.AddItem(itemName,INFINITE);
}
}else{
ERR("Could not find item "<<itemNumber<<" in Merchant "<<merchantCount<<" of Chapter "<<chapter<<"!");
}
@@ -95,4 +106,81 @@ void Merchant::Initialize(){
}
std::cout<<std::format("Added {} merchants to Chapter {}",merchantCount,chapter)<<std::endl;
}
}
}
bool Merchant::CanPurchaseItem(IT item,uint32_t amt)const{
bool itemAvailable=false;
const Item*foundItem=nullptr;
for(const Item&it:shopItems){
if(it==item&&it.Amt()>=amt&&it.CanBePurchased()){
itemAvailable=true;
foundItem=&it;
break;
}
}
purchaseFunctionPrimed.amt=amt;
purchaseFunctionPrimed.item=item;
return purchaseFunctionPrimed=
itemAvailable&&
foundItem!=nullptr&&
game->GetPlayer()->GetMoney()>=foundItem->BuyValue()*amt;
};
bool Merchant::CanSellItem(IT item,uint32_t amt)const{
ItemInfo&it=ITEM_DATA[item];
sellFunctionPrimed.amt=amt;
sellFunctionPrimed.item=item;
return sellFunctionPrimed=
it.CanBeSold()&&
Inventory::GetItemCount(item)>=amt;
};
void Merchant::PurchaseItem(IT item,uint32_t amt){
purchaseFunctionPrimed.Validate(item,amt);
uint32_t totalCost=0U;
for(Item&it:shopItems){
if(it==item){
if(it.Amt()!=INFINITE){
it.SetAmt(it.Amt()-amt);
}
totalCost=it.BuyValue()*amt;
break;
}
}
Inventory::AddItem(item,amt);
game->GetPlayer()->SetMoney(game->GetPlayer()->GetMoney()-totalCost);
purchaseFunctionPrimed=false;
};
void Merchant::SellItem(IT item,uint32_t amt){
sellFunctionPrimed.Validate(item,amt);
uint32_t totalCost=0U;
bool itemFound=false;
for(Item&it:shopItems){
if(it==item){
if(it.Amt()!=INFINITE){
it.SetAmt(it.Amt()+amt);
}
itemFound=true;
break;
}
}
if(!itemFound){
AddItem(item);
}
totalCost=ITEM_DATA[item].GetSellValue()*amt;
Inventory::RemoveItem(item,amt);
game->GetPlayer()->SetMoney(game->GetPlayer()->GetMoney()+totalCost);
sellFunctionPrimed=false;
};
void Merchant::RandomizeTravelingMerchant(){
travelingMerchant=GetRandomMerchant(game->GetCurrentChapter());
};
Merchant&Merchant::GetCurrentTravelingMerchant(){
return travelingMerchant;
};