2013-07-29 11:13:03 +00:00
// Enchantments.h
// Declares the cEnchantments class representing a storage for item enchantments and stored-enchantments
# pragma once
2014-04-17 11:15:35 +00:00
# include "Defines.h"
2020-10-01 21:33:32 +00:00
# include "FastRandom.h"
2014-01-19 16:52:45 +00:00
# include "WorldStorage/EnchantmentSerializer.h"
2013-07-29 11:13:03 +00:00
2015-05-09 07:25:09 +00:00
// fwd: "WorldStorage/FastNBT.h"
2013-07-29 11:13:03 +00:00
class cFastNBTWriter ;
class cParsedNBT ;
2014-04-17 17:31:43 +00:00
// fwd:
struct cWeightedEnchantment ;
typedef std : : vector < cWeightedEnchantment > cWeightedEnchantments ;
2013-07-29 11:13:03 +00:00
2020-10-01 21:33:32 +00:00
2013-07-29 11:13:03 +00:00
/** Class that stores item enchantments or stored-enchantments
The enchantments may be serialized to a stringspec and read back from such stringspec .
The format for the stringspec is " id=lvl;id=lvl;id=lvl... " , with an optional semicolon at the end ,
mapping each enchantment ' s id onto its level . ID may be either a number or the enchantment name .
Level value of 0 means no such enchantment , and it will not be stored in the m_Enchantments .
Serialization will never put zero - level enchantments into the stringspec and will always use numeric IDs .
*/
2014-01-31 18:46:51 +00:00
2014-01-19 16:52:45 +00:00
// tolua_begin
2013-07-29 11:13:03 +00:00
class cEnchantments
{
public :
2017-08-24 09:19:40 +00:00
/** Individual enchantment IDs, corresponding to their NBT IDs: https://minecraft.gamepedia.com/Data_values#Enchantment_IDs
2014-04-20 12:16:26 +00:00
*/
2016-02-05 21:45:45 +00:00
2014-08-03 05:56:08 +00:00
enum eEnchantment
2013-07-29 11:13:03 +00:00
{
2017-07-28 17:00:20 +00:00
// Currently missing: Frost walker, curse of binding, sweeping edge, mending, and curse of vanishing.
2013-07-29 11:13:03 +00:00
enchProtection = 0 ,
enchFireProtection = 1 ,
enchFeatherFalling = 2 ,
enchBlastProtection = 3 ,
enchProjectileProtection = 4 ,
enchRespiration = 5 ,
enchAquaAffinity = 6 ,
enchThorns = 7 ,
2014-12-22 00:29:34 +00:00
enchDepthStrider = 8 ,
2013-07-29 11:13:03 +00:00
enchSharpness = 16 ,
enchSmite = 17 ,
enchBaneOfArthropods = 18 ,
enchKnockback = 19 ,
enchFireAspect = 20 ,
enchLooting = 21 ,
enchEfficiency = 32 ,
enchSilkTouch = 33 ,
enchUnbreaking = 34 ,
enchFortune = 35 ,
enchPower = 48 ,
enchPunch = 49 ,
enchFlame = 50 ,
enchInfinity = 51 ,
2013-11-13 07:16:35 +00:00
enchLuckOfTheSea = 61 ,
enchLure = 62 ,
2013-07-29 11:13:03 +00:00
} ;
2014-01-31 18:46:51 +00:00
2014-04-20 12:16:26 +00:00
/** Creates an empty enchantments container */
2013-07-29 11:13:03 +00:00
cEnchantments ( void ) ;
2016-02-05 21:45:45 +00:00
2014-04-20 12:16:26 +00:00
/** Creates an enchantments container filled with enchantments parsed from stringspec */
2013-07-29 11:13:03 +00:00
cEnchantments ( const AString & a_StringSpec ) ;
2016-02-05 21:45:45 +00:00
2014-04-20 12:16:26 +00:00
/** Adds the enchantments contained in a_Other into this object.
2016-07-06 10:39:56 +00:00
Existing enchantments are preserved , unless a_Other specifies a different level , in which case the level is changed to the a_Other ' s one . */
2014-04-20 12:16:26 +00:00
void Add ( const cEnchantments & a_Other ) ;
2016-02-05 21:45:45 +00:00
2014-04-20 12:16:26 +00:00
/** Adds enchantments in the stringspec; if a specified enchantment already exists, overwrites it */
2013-07-29 11:13:03 +00:00
void AddFromString ( const AString & a_StringSpec ) ;
2016-02-05 21:45:45 +00:00
2014-04-30 22:47:57 +00:00
/** Get the count of enchantments */
2020-10-10 19:23:25 +00:00
size_t Count ( void ) const ;
2016-02-05 21:45:45 +00:00
2014-04-20 12:16:26 +00:00
/** Serializes all the enchantments into a string */
2013-07-29 11:13:03 +00:00
AString ToString ( void ) const ;
2016-02-05 21:45:45 +00:00
2014-04-20 12:16:26 +00:00
/** Returns the level for the specified enchantment; 0 if not stored */
2015-05-19 18:32:10 +00:00
unsigned int GetLevel ( int a_EnchantmentID ) const ;
2016-02-05 21:45:45 +00:00
2014-04-20 12:16:26 +00:00
/** Sets the level for the specified enchantment, adding it if not stored before or removing it if level <= 0 */
2015-05-19 18:32:10 +00:00
void SetLevel ( int a_EnchantmentID , unsigned int a_Level ) ;
2016-02-05 21:45:45 +00:00
2014-04-20 12:16:26 +00:00
/** Removes all enchantments */
2013-07-29 11:13:03 +00:00
void Clear ( void ) ;
2016-02-05 21:45:45 +00:00
2014-04-20 12:16:26 +00:00
/** Returns true if there are no enchantments */
2013-07-29 11:13:03 +00:00
bool IsEmpty ( void ) const ;
2016-02-05 21:45:45 +00:00
2017-07-28 17:00:20 +00:00
/** Returns true if the given enchantment could be legally added to this object. Note that adding the enchantment may not actually increase the level. */
bool CanAddEnchantment ( int a_EnchantmentID ) const ;
2014-04-23 21:06:29 +00:00
/** Converts enchantment name or ID (number in string) to the numeric representation; returns -1 if enchantment name not found; case insensitive */
2013-07-29 11:13:03 +00:00
static int StringToEnchantmentID ( const AString & a_EnchantmentName ) ;
2016-02-05 21:45:45 +00:00
2014-04-20 12:16:26 +00:00
/** Returns true if a_Other contains exactly the same enchantments and levels */
2013-07-29 11:13:03 +00:00
bool operator = = ( const cEnchantments & a_Other ) const ;
2014-04-17 11:15:35 +00:00
2013-07-29 11:13:03 +00:00
// tolua_end
2014-04-17 11:15:35 +00:00
2017-07-28 17:00:20 +00:00
/** Get the XP cost multiplier for the enchantment (for anvils).
If FromBook is true , then this function returns the XP multiplier if
the enchantment is coming from a book , otherwise it returns the normal
item multiplier . */
static int GetXPCostMultiplier ( int a_EnchantmentID , bool FromBook ) ;
/** Get the maximum level the enchantment can have */
static unsigned int GetLevelCap ( int a_EnchantmentID ) ;
2014-04-17 11:15:35 +00:00
/** Add enchantment weights from item to the vector */
2020-10-05 12:09:42 +00:00
static void AddItemEnchantmentWeights ( cWeightedEnchantments & a_Enchantments , short a_ItemType , unsigned a_EnchantmentLevel ) ;
2014-04-17 11:15:35 +00:00
2014-04-19 20:37:29 +00:00
/** Add a enchantment with weight to the vector */
2015-05-19 18:32:10 +00:00
static void AddEnchantmentWeightToVector ( cWeightedEnchantments & a_Enchantments , int a_Weight , int a_EnchantmentID , unsigned int a_EnchantmentLevel ) ;
2016-02-05 21:45:45 +00:00
2014-04-19 20:37:29 +00:00
/** Remove the entire enchantment (with weight) from the vector */
2014-04-19 13:10:50 +00:00
static void RemoveEnchantmentWeightFromVector ( cWeightedEnchantments & a_Enchantments , int a_EnchantmentID ) ;
2016-02-05 21:45:45 +00:00
2014-04-19 20:37:29 +00:00
/** Remove the entire enchantment (with weight) from the vector */
2014-04-19 13:10:50 +00:00
static void RemoveEnchantmentWeightFromVector ( cWeightedEnchantments & a_Enchantments , const cEnchantments & a_Enchantment ) ;
2014-04-17 11:15:35 +00:00
/** Check enchantment conflicts from enchantments from the vector */
2020-05-14 22:15:35 +00:00
static void CheckEnchantmentConflictsFromVector ( cWeightedEnchantments & a_Enchantments , const cEnchantments & a_FirstEnchantment ) ;
2014-04-17 11:15:35 +00:00
2020-10-01 21:33:32 +00:00
/** Gets random enchantment from Vector and returns it, with randomness derived from the provided PRNG. */
static cEnchantments GetRandomEnchantmentFromVector ( const cWeightedEnchantments & a_Enchantments , MTRand & a_Random ) ;
2014-10-15 12:44:07 +00:00
2014-12-21 18:48:29 +00:00
/** Selects one enchantment from a Vector using cNoise. Mostly used for generators.
Uses the enchantments ' weights for the random distribution .
If a_Enchantments is empty , returns an empty enchantment . */
static cEnchantments SelectEnchantmentFromVector ( const cWeightedEnchantments & a_Enchantments , int a_Seed ) ;
2014-04-17 11:15:35 +00:00
2014-04-20 12:16:26 +00:00
/** Returns true if a_Other doesn't contain exactly the same enchantments and levels */
2013-07-29 11:13:03 +00:00
bool operator ! = ( const cEnchantments & a_Other ) const ;
2016-02-05 21:45:45 +00:00
2014-04-20 12:16:26 +00:00
/** Writes the enchantments into the specified NBT writer; begins with the LIST tag of the specified name ("ench" or "StoredEnchantments") */
2015-05-09 09:16:56 +00:00
friend void EnchantmentSerializer : : WriteToNBTCompound ( const cEnchantments & a_Enchantments , cFastNBTWriter & a_Writer , const AString & a_ListTagName ) ;
2016-02-05 21:45:45 +00:00
2014-04-20 12:16:26 +00:00
/** Reads the enchantments from the specified NBT list tag (ench or StoredEnchantments) */
2015-05-09 09:16:56 +00:00
friend void EnchantmentSerializer : : ParseFromNBT ( cEnchantments & a_Enchantments , const cParsedNBT & a_NBT , int a_EnchListTagIdx ) ;
2014-04-12 12:58:46 +00:00
2013-07-29 11:13:03 +00:00
protected :
2014-04-20 12:16:26 +00:00
/** Maps enchantment ID -> enchantment level */
2015-05-19 18:32:10 +00:00
typedef std : : map < int , unsigned int > cMap ;
2016-02-05 21:45:45 +00:00
2014-04-20 12:16:26 +00:00
/** Currently stored enchantments */
2013-07-29 11:13:03 +00:00
cMap m_Enchantments ;
2017-07-28 17:00:20 +00:00
public :
/** Make this class iterable */
cMap : : const_iterator begin ( ) const { return m_Enchantments . begin ( ) ; }
cMap : : const_iterator end ( ) const { return m_Enchantments . end ( ) ; }
} ; // tolua_export
2013-07-29 11:13:03 +00:00
2014-04-17 17:31:43 +00:00
2014-04-19 20:37:29 +00:00
// Define the cWeightedEnchantment struct for the Enchanting System to store the EnchantmentWeights:
2014-04-17 11:15:35 +00:00
struct cWeightedEnchantment
{
int m_Weight ;
cEnchantments m_Enchantments ;
} ;
2013-07-29 11:13:03 +00:00