diff options
Diffstat (limited to '')
-rw-r--r-- | src/Mobs/Villager.h | 133 |
1 files changed, 109 insertions, 24 deletions
diff --git a/src/Mobs/Villager.h b/src/Mobs/Villager.h index 6f3e7b4e8..0d8672c46 100644 --- a/src/Mobs/Villager.h +++ b/src/Mobs/Villager.h @@ -3,37 +3,123 @@ #include "PassiveMonster.h" #include "Blocks/ChunkInterface.h" +#include "../UI/WindowOwner.h" + + + + + +struct VillagerTradeOffer +{ + VillagerTradeOffer(const cItem & PrimaryTradeDesire, const cItem & TradeRecompense) : + PrimaryDesire(PrimaryTradeDesire), + Recompense(TradeRecompense), + Transactions(0), + MaximumTransactions(10) + { + } + + VillagerTradeOffer(const cItem & PrimaryTradeDesire, const cItem & SecondaryTradeDesire, const cItem & TradeRecompense) : + PrimaryDesire(PrimaryTradeDesire), + SecondaryDesire(SecondaryTradeDesire), + Recompense(TradeRecompense), + Transactions(0), + MaximumTransactions(10) + { + } + + cItem PrimaryDesire; + cItem SecondaryDesire; + cItem Recompense; + + bool IsTradeExhausted() const + { + return Transactions == MaximumTransactions; + } + + unsigned Transactions; + unsigned MaximumTransactions; +}; + class cVillager : - public cPassiveMonster + public cPassiveMonster, + public cEntityWindowOwner { typedef cPassiveMonster super; public: - enum eVillagerType + enum class VillagerProfession + { + Farmer = 0, + Librarian = 1, + Priest = 2, + Blacksmith = 3, + Butcher = 4, + Nitwit = 5 + }; + + enum class VillagerCareer { - vtFarmer = 0, - vtLibrarian = 1, - vtPriest = 2, - vtBlacksmith = 3, - vtButcher = 4, - vtGeneric = 5, - vtMax - } ; + Farmer, + Fisherman, + Shepherd, + Fletcher, + Librarian, + Cartographer, + Cleric, + Armorer, + WeaponSmith, + ToolSmith, + Butcher, + Leatherworker, + Nitwit + }; + + cVillager(VillagerCareer, unsigned); + CLASS_PROTODEF(cVillager) - cVillager(eVillagerType VillagerType); + /** Converts internal career to equivalent Vanilla profession. + Returns an enumeration with Vanilla-compatible values. */ + static VillagerProfession VillagerCareerToProfession(VillagerCareer); - CLASS_PROTODEF(cVillager) + /** Returns the career of the villager. */ + VillagerCareer GetCareer() const { return m_Career; } + + /** Returns a trading offer of the villager subject to criteria. + If the player has selected the first trade offer, the first available trade offer matching the input is returned, or the first trade offer if nothing was found. + For any other offer selection, the selected offer is returned. */ + VillagerTradeOffer & GetTradeOffer(unsigned, const cItem &, const cItem &); + + /** Returns number of transactions of a single trade can be completed, given input and output items. */ + static unsigned GetTransactionMultiplier(const VillagerTradeOffer &, const cItem &, const cItem &); + + /** Handles events where players interact with an available trade. + This includes adding a desired item to fulfil or exceed the offer, or otherwise interacting with a trade when it can be performed. */ + void HandleTradeAvailable() const; + + /** Handles events where players interact with the trading window. + This event does not include interactions with the currently presented trade itself. */ + void HandleTradeInProgress() const; + + /** Handles events where players interact with an unavailable trade. + This includes adding a desired item to fulfil or exceed the offer, or otherwise interacting with a trade when it can be performed. */ + void HandleTradeUnavailable() const; + + /** Convenience function for setting correct item counts and upgrading trade tiers if necessary. */ + void HandleTransaction(VillagerTradeOffer &, unsigned TransactionMultiplier); // cEntity overrides virtual bool DoTakeDamage(TakeDamageInfo & a_TDI) override; - virtual void Tick (std::chrono::milliseconds a_Dt, cChunk & a_Chunk) override; + virtual void Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) override; + virtual void OnRightClicked(cPlayer &) override; + +private: - // cVillager functions /** return true if the given blocktype are: crops, potatoes or carrots. */ bool IsBlockFarmable(BLOCKTYPE a_BlockType); @@ -48,19 +134,18 @@ public: void HandleFarmerPlaceCrops(); // Get and set functions. - int GetVilType(void) const { return m_Type; } - Vector3i GetCropsPos(void) const { return m_CropsPos; } - bool DoesHaveActionActivated(void) const { return m_VillagerAction; } + Vector3i GetCropsPos(void) const { return m_CropsPos; } + bool DoesHaveActionActivated(void) const { return m_VillagerAction; } -private: + /** Levels-up the villager to the given tier, if not already at maximum for their career. + Levelling-up unlocks additional trades available at that level, and re-enables all previously exhausted trade offers. */ + void UpdateTradeTier(size_t); int m_ActionCountDown; - int m_Type; + VillagerCareer m_Career; + size_t m_TradeTier; + std::vector<VillagerTradeOffer> m_TradeOffers; bool m_VillagerAction; Vector3i m_CropsPos; -} ; - - - - +}; |