Cuberite
A lightweight, fast and extensible game server for Minecraft
Enchantments.h
Go to the documentation of this file.
1 // Enchantments.h
2 
3 // Declares the cEnchantments class representing a storage for item enchantments and stored-enchantments
4 
5 
6 
7 
8 
9 #pragma once
10 
11 #include "Defines.h"
12 #include "FastRandom.h"
14 
15 
16 
17 // fwd: "WorldStorage/FastNBT.h"
18 class cFastNBTWriter;
19 class cParsedNBT;
20 
21 
22 // fwd:
24 
25 typedef std::vector<cWeightedEnchantment> cWeightedEnchantments;
26 
27 
28 
29 
30 
40 // tolua_begin
42 {
43 public:
48  {
49  // Currently missing: Frost walker, curse of binding, sweeping edge, mending, and curse of vanishing.
60  enchSmite = 17,
69  enchPower = 48,
70  enchPunch = 49,
71  enchFlame = 50,
74  enchLure = 62,
75  } ;
76 
78  cEnchantments(void);
79 
81  cEnchantments(const AString & a_StringSpec);
82 
85  void Add(const cEnchantments & a_Other);
86 
88  void AddFromString(const AString & a_StringSpec);
89 
91  size_t Count(void) const;
92 
94  AString ToString(void) const;
95 
97  unsigned int GetLevel(int a_EnchantmentID) const;
98 
100  void SetLevel(int a_EnchantmentID, unsigned int a_Level);
101 
103  void Clear(void);
104 
106  bool IsEmpty(void) const;
107 
109  bool CanAddEnchantment(int a_EnchantmentID) const;
110 
112  static int StringToEnchantmentID(const AString & a_EnchantmentName);
113 
115  bool operator ==(const cEnchantments & a_Other) const;
116 
117  // tolua_end
118 
123  static int GetXPCostMultiplier(int a_EnchantmentID, bool FromBook);
124 
126  static unsigned int GetLevelCap(int a_EnchantmentID);
127 
129  static void AddItemEnchantmentWeights(cWeightedEnchantments & a_Enchantments, short a_ItemType, unsigned a_EnchantmentLevel);
130 
132  static void AddEnchantmentWeightToVector(cWeightedEnchantments & a_Enchantments, int a_Weight, int a_EnchantmentID, unsigned int a_EnchantmentLevel);
133 
135  static void RemoveEnchantmentWeightFromVector(cWeightedEnchantments & a_Enchantments, int a_EnchantmentID);
136 
138  static void RemoveEnchantmentWeightFromVector(cWeightedEnchantments & a_Enchantments, const cEnchantments & a_Enchantment);
139 
141  static void CheckEnchantmentConflictsFromVector(cWeightedEnchantments & a_Enchantments, const cEnchantments & a_FirstEnchantment);
142 
144  static cEnchantments GetRandomEnchantmentFromVector(const cWeightedEnchantments & a_Enchantments, MTRand & a_Random);
145 
149  static cEnchantments SelectEnchantmentFromVector(const cWeightedEnchantments & a_Enchantments, int a_Seed);
150 
152  bool operator !=(const cEnchantments & a_Other) const;
153 
155  friend void EnchantmentSerializer::WriteToNBTCompound(const cEnchantments & a_Enchantments, cFastNBTWriter & a_Writer, const AString & a_ListTagName);
156 
158  friend void EnchantmentSerializer::ParseFromNBT(cEnchantments & a_Enchantments, const cParsedNBT & a_NBT, int a_EnchListTagIdx);
159 
160 protected:
162  typedef std::map<int, unsigned int> cMap;
163 
166 
167 public:
169  cMap::const_iterator begin() const { return m_Enchantments.begin(); }
170  cMap::const_iterator end() const { return m_Enchantments.end(); }
171 }; // tolua_export
172 
173 
174 
175 
176 // Define the cWeightedEnchantment struct for the Enchanting System to store the EnchantmentWeights:
178 {
179  int m_Weight;
181 };
182 
183 
184 
std::vector< cWeightedEnchantment > cWeightedEnchantments
Definition: Enchantments.h:23
std::string AString
Definition: StringUtils.h:11
void ParseFromNBT(cEnchantments &a_Enchantments, const cParsedNBT &a_NBT, int a_EnchListTagIdx)
Reads the enchantments from the specified NBT list tag (ench or StoredEnchantments)
void WriteToNBTCompound(const cEnchantments &a_Enchantments, cFastNBTWriter &a_Writer, const AString &a_ListTagName)
Writes the enchantments into the specified NBT writer; begins with the LIST tag of the specified name...
Class that stores item enchantments or stored-enchantments The enchantments may be serialized to a st...
Definition: Enchantments.h:42
cMap m_Enchantments
Currently stored enchantments.
Definition: Enchantments.h:165
bool CanAddEnchantment(int a_EnchantmentID) const
Returns true if the given enchantment could be legally added to this object.
void Add(const cEnchantments &a_Other)
Adds the enchantments contained in a_Other into this object.
void Clear(void)
Removes all enchantments.
eEnchantment
Individual enchantment IDs, corresponding to their NBT IDs: https://minecraft.wiki/w/Data_values#Ench...
Definition: Enchantments.h:48
@ enchProjectileProtection
Definition: Enchantments.h:54
void SetLevel(int a_EnchantmentID, unsigned int a_Level)
Sets the level for the specified enchantment, adding it if not stored before or removing it if level ...
size_t Count(void) const
Get the count of enchantments.
static void AddEnchantmentWeightToVector(cWeightedEnchantments &a_Enchantments, int a_Weight, int a_EnchantmentID, unsigned int a_EnchantmentLevel)
Add a enchantment with weight to the vector.
static unsigned int GetLevelCap(int a_EnchantmentID)
Get the maximum level the enchantment can have.
static int StringToEnchantmentID(const AString &a_EnchantmentName)
Converts enchantment name or ID (number in string) to the numeric representation; returns -1 if encha...
static cEnchantments SelectEnchantmentFromVector(const cWeightedEnchantments &a_Enchantments, int a_Seed)
Selects one enchantment from a Vector using cNoise.
unsigned int GetLevel(int a_EnchantmentID) const
Returns the level for the specified enchantment; 0 if not stored.
static void RemoveEnchantmentWeightFromVector(cWeightedEnchantments &a_Enchantments, int a_EnchantmentID)
Remove the entire enchantment (with weight) from the vector.
void AddFromString(const AString &a_StringSpec)
Adds enchantments in the stringspec; if a specified enchantment already exists, overwrites it.
AString ToString(void) const
Serializes all the enchantments into a string.
bool operator!=(const cEnchantments &a_Other) const
Returns true if a_Other doesn't contain exactly the same enchantments and levels.
cMap::const_iterator end() const
Definition: Enchantments.h:170
static cEnchantments GetRandomEnchantmentFromVector(const cWeightedEnchantments &a_Enchantments, MTRand &a_Random)
Gets random enchantment from Vector and returns it, with randomness derived from the provided PRNG.
cMap::const_iterator begin() const
Make this class iterable.
Definition: Enchantments.h:169
static int GetXPCostMultiplier(int a_EnchantmentID, bool FromBook)
Get the XP cost multiplier for the enchantment (for anvils).
static void CheckEnchantmentConflictsFromVector(cWeightedEnchantments &a_Enchantments, const cEnchantments &a_FirstEnchantment)
Check enchantment conflicts from enchantments from the vector.
static void AddItemEnchantmentWeights(cWeightedEnchantments &a_Enchantments, short a_ItemType, unsigned a_EnchantmentLevel)
Add enchantment weights from item to the vector.
cEnchantments(void)
Creates an empty enchantments container.
bool operator==(const cEnchantments &a_Other) const
Returns true if a_Other contains exactly the same enchantments and levels.
bool IsEmpty(void) const
Returns true if there are no enchantments.
std::map< int, unsigned int > cMap
Maps enchantment ID -> enchantment level.
Definition: Enchantments.h:162
cEnchantments m_Enchantments
Definition: Enchantments.h:180
Class to wrap any random engine to provide a more convenient interface.
Definition: FastRandom.h:58
Parses and contains the parsed data Also implements data accessor functions for tree traversal and va...
Definition: FastNBT.h:153