the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 146 lines 3.0 kB view raw
1#include "stdafx.h" 2 3#include "MerchantRecipe.h" 4 5void MerchantRecipe::_init(shared_ptr<ItemInstance> buyA, shared_ptr<ItemInstance> buyB, shared_ptr<ItemInstance> sell) 6{ 7 this->buyA = buyA; 8 this->buyB = buyB; 9 this->sell = sell; 10 uses = 0; 11 maxUses = 7; 12} 13 14MerchantRecipe::MerchantRecipe(CompoundTag *tag) 15{ 16 buyA = nullptr; 17 buyB = nullptr; 18 sell = nullptr; 19 uses = 0; 20 load(tag); 21} 22 23MerchantRecipe::MerchantRecipe(shared_ptr<ItemInstance> buyA, shared_ptr<ItemInstance> buyB, shared_ptr<ItemInstance> sell, int uses, int maxUses) 24{ 25 _init(buyA, buyB, sell); 26 this->uses = uses; 27 this->maxUses = maxUses; 28} 29 30MerchantRecipe::MerchantRecipe(shared_ptr<ItemInstance> buy, shared_ptr<ItemInstance> sell) 31{ 32 _init(buy, nullptr, sell); 33} 34 35MerchantRecipe::MerchantRecipe(shared_ptr<ItemInstance> buy, Item *sell) 36{ 37 _init(buy, nullptr, shared_ptr<ItemInstance>(new ItemInstance(sell))); 38} 39 40MerchantRecipe::MerchantRecipe(shared_ptr<ItemInstance> buy, Tile *sell) 41{ 42 _init(buy, nullptr, shared_ptr<ItemInstance>(new ItemInstance(sell))); 43} 44 45shared_ptr<ItemInstance> MerchantRecipe::getBuyAItem() 46{ 47 return buyA; 48} 49 50shared_ptr<ItemInstance> MerchantRecipe::getBuyBItem() 51{ 52 return buyB; 53} 54 55bool MerchantRecipe::hasSecondaryBuyItem() 56{ 57 return buyB != NULL; 58} 59 60shared_ptr<ItemInstance> MerchantRecipe::getSellItem() 61{ 62 return sell; 63} 64 65bool MerchantRecipe::isSame(MerchantRecipe *other) 66{ 67 if (buyA->id != other->buyA->id || sell->id != other->sell->id) 68 { 69 return false; 70 } 71 return (buyB == NULL && other->buyB == NULL) || (buyB != NULL && other->buyB != NULL && buyB->id == other->buyB->id); 72} 73 74bool MerchantRecipe::isSameSameButBetter(MerchantRecipe *other) 75{ 76 // same deal, but cheaper 77 return isSame(other) && (buyA->count < other->buyA->count || (buyB != NULL && buyB->count < other->buyB->count)); 78} 79 80int MerchantRecipe::getUses() 81{ 82 return uses; 83} 84 85int MerchantRecipe::getMaxUses() 86{ 87 return maxUses; 88} 89 90void MerchantRecipe::increaseUses() 91{ 92 uses++; 93} 94 95void MerchantRecipe::increaseMaxUses(int amount) 96{ 97 maxUses += amount; 98} 99 100bool MerchantRecipe::isDeprecated() 101{ 102 return uses >= maxUses; 103} 104 105void MerchantRecipe::enforceDeprecated() 106{ 107 uses = maxUses; 108} 109 110void MerchantRecipe::load(CompoundTag *tag) 111{ 112 CompoundTag *buyTag = tag->getCompound(L"buy"); 113 buyA = ItemInstance::fromTag(buyTag); 114 CompoundTag *sellTag = tag->getCompound(L"sell"); 115 sell = ItemInstance::fromTag(sellTag); 116 if (tag->contains(L"buyB")) 117 { 118 buyB = ItemInstance::fromTag(tag->getCompound(L"buyB")); 119 } 120 if (tag->contains(L"uses")) 121 { 122 uses = tag->getInt(L"uses"); 123 } 124 if (tag->contains(L"maxUses")) 125 { 126 maxUses = tag->getInt(L"maxUses"); 127 } 128 else 129 { 130 maxUses = 7; 131 } 132} 133 134CompoundTag *MerchantRecipe::createTag() 135{ 136 CompoundTag *tag = new CompoundTag(); 137 tag->putCompound(L"buy", buyA->save(new CompoundTag(L"buy"))); 138 tag->putCompound(L"sell", sell->save(new CompoundTag(L"sell"))); 139 if (buyB != NULL) 140 { 141 tag->putCompound(L"buyB", buyB->save(new CompoundTag(L"buyB"))); 142 } 143 tag->putInt(L"uses", uses); 144 tag->putInt(L"maxUses", maxUses); 145 return tag; 146}